Jump to content

Calculate the best values found in an array


tonton

Recommended Posts

Hi, goodnight,
I have a json data which contains some items (it may have one or more items (with one or two taxes) and one shipping costs (with one or two taxes) :
 
{
   "currency": "CAD",
   "email": "toto@gmail.com",
   "items": [
      {
         "name_title": "pen",
         "quantity": 1,
         "price": "20.00",
         "taxs": [
            {
               "price": "3.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            },
            {
               "price": "1.38",
               "rates": 0.05,
               "name_title": "TaxSecond"
            }
         ]
      }
   ],
   "shipping_cost": [
      {
         "code": "FDX",
         "price": "6.23",
         "name_title": "Fidex",
         "taxs": [
            {
               "price": "1.55",
               "rates": 0.25,
               "name_title": "TaxOne"
            },
            {
               "price": "2.37",
               "rates": 0.38,
               "name_title": "TaxSecond"
            }
         ]
      }
   ],
   "billing": {
      "names": "John Toto"
   },
   "billing": {
      "names": "John Toto"
   },
   "customers": {
      "invoce": null,
      "default_address": {
         "names": "John Toto"
      }
   }
}
With these data, I need the following information: 
 
Find the total price (Item price x quantity + its taxes + shipping costs + shipping taxes) = 20.00 x 1 + 3.0 + 1.00 + 6.23 + 1.55 + 2.37 = 34.15
find information articles and  shipping information and its taxs like this : 
 
name --> pen,
quantity  --> 1
price --> 20.00
 
name --> Fidex
quantity  --> 1 (for this, it is always :  1!)
price --> 6.23
 
name --> TaxOne
quantity  --> 1  (for this, it is always :   1!) 
price --> 4.55
 
name --> TaxSecond
quantity  --> 1  (for this, it is always :  1!)
price --> 3.37
 
How can I change my array as seen above?
In fact, I must build a new array with all this information above.
 
 
like this : 
 
I transform my json data in array
I delete the information on "Billing"; "delivery" and "client"
 
Join taxes --> Items taxes + shipping tax
I find tax items like this:
$taxs_items = $new_array['items']['0']['taxs'];
 

I wonder if this is good way, because of this ['0'] ? If zero change by another number ?

Same for the shipping charges:


	$taxs_billing = $new_array['shipping_cost']['0']['taxs'];

Then I merged the two array
here this is my tax array: 
array(2) {
  ["TaxOne"]=>
  array(3) {
    ["price"]=>
    float(4.55)
    ["rates"]=>
    float(0.15)
    ["name_title"]=>
    string(6) "TaxOne"
  }
  ["TaxSecond"]=>
  array(3) {
    ["price"]=>
    float(3.75)
    ["rates"]=>
    float(0.05)
    ["name_title"]=>
    string(9) "TaxSecond"
  }
}
I also make another array for delivery without taxes
In this array, I have to remove the tax, but how?
I do like that, but it is not good as the others since there is a['0']....
 unset($billing_without_tax['0']['taxs']);
So now I will add items in this array
 
Here's what I get:
array(4) {
  [0]=>
  array(3) {
    ["name_title"]=>
    string(3) "pen"
    ["quantity"]=>
    int(1)
    ["price"]=>
    string(5) "20.00"
  }
  [1]=>
  array(3) {
    ["code"]=>
    string(3) "FDX"
    ["price"]=>
    string(4) "6.23"
    ["name_title"]=>
    string(5) "Fidex"
  }
  ["TaxOne"]=>
  array(3) {
    ["price"]=>
    float(4.55)
    ["rates"]=>
    float(0.15)
    ["name_title"]=>
    string(6) "TaxOne"
  }
  ["TaxSecond"]=>
  array(3) {
    ["price"]=>
    float(3.75)
    ["rates"]=>
    float(0.05)
    ["name_title"]=>
    string(9) "TaxSecond"
  }
}

So now, I'll do my calculation to find the total amount with taxes:
and I find the right value: 34.53
When there is only one tax for an item, the code works too:
 
For exemple, 
I romeve the following tax :
"price": "1.38",
rates:0.05,
"name": "TaxSecond"
 
I find the right value: 33.15 (34.53 - 1.38)
 
The code is capable of calculating the amount according whit quantity: if there are two examples of an article, it find the right value.
By cons, if there are two or three different items it does not add its tax.
 
for exemple :
{
   "currency": "CAD",
   "email": "toto@gmail.com",
   "items": [
      {
         "name_title": "pen",
         "quantity": 2,
         "price": "20.00",
         "taxs": [
            {
               "price": "1.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            }
         ]
      },
	   {
         "name_title": "pensil",
         "quantity": 3,
         "price": "10.00",
         "taxs": [
            {
               "price": "1.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            }
         ]
      },
	  {
         "name_title": "notebook",
         "quantity": 2,
         "price": "5.00",
         "taxs": [
            {
               "price": "1.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            },
            {
               "price": "1.38",
               "rates": 0.05,
               "name_title": "TaxSecond"
            }
         ]
      }
   ],
   "shipping_cost": [
      {
         "code": "FDX",
         "price": "6.23",
         "name_title": "Fidex",
         "taxs": [
            {
               "price": "1.55",
               "rates": 0.25,
               "name_title": "TaxOne"
            },
            {
               "price": "2.37",
               "rates": 0.38,
               "name_title": "TaxSecond"
            }
         ]
      }
   ],
   "billing": {
      "names": "John Toto"
   },
   "billing": {
      "names": "John Toto"
   },
   "customers": {
      "invoce": null,
      "default_address": {
         "names": "John Toto"
      }
   }
Here the calculated transaction : 
 
pen => 2 x 20.00 + 1.00 = 41.00
pensil => 3 x 10.00 + 1.00 = 31.00
"notebook" => 2 x 5.00 + 1.00 + 1.38 = 12.38
shipping => 6.23 + 1.55 + 2.37 = 10.15
sum => 41 + 31 + 12.38 + 10.15 = 94.53
 
by cons, my codes find this : 91.15
therefore missing : tax of items :   1 + 1 + 1 + 1,38
 
Here are my codes

$json = '
{
   "currency": "CAD",
   "email": "toto@gmail.com",
   "items": [
      {
         "name_title": "pen",
         "quantity": 2,
         "price": "20.00",
         "taxs": [
            {
               "price": "1.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            }
         ]
      },
	   {
         "name_title": "pensil",
         "quantity": 3,
         "price": "10.00",
         "taxs": [
            {
               "price": "1.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            }
         ]
      },
	  {
         "name_title": "notebook",
         "quantity": 2,
         "price": "5.00",
         "taxs": [
            {
               "price": "1.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            },
            {
               "price": "1.38",
               "rates": 0.05,
               "name_title": "TaxSecond"
            }
         ]
      }
   ],
   "shipping_cost": [
      {
         "code": "FDX",
         "price": "6.23",
         "name_title": "Fidex",
         "taxs": [
            {
               "price": "1.55",
               "rates": 0.25,
               "name_title": "TaxOne"
            },
            {
               "price": "2.37",
               "rates": 0.38,
               "name_title": "TaxSecond"
            }
         ]
      }
   ],
   "billing": {
      "names": "John Toto"
   },
   "billing": {
      "names": "John Toto"
   },
   "customers": {
      "invoce": null,
      "default_address": {
         "names": "John Toto"
      }
   }
}';

// transforming my data in json table**** ****** ******** *****
$new_array = json_decode($json, true);; 

/*
																							//print_r($new_array);
																							var_dump($new_array);
																							exit();
																							*/
																							
// delete those information "billing" ; "billing" and "customers"  **** ****** ******** *****

unset($new_array['billing']);
unset($new_array['billing']);
unset($new_array['customers']);

/*
																							var_dump($new_array);
																							exit();

*/

//Consolidate taxs --> items of tax + billing  tax **** ****** ******** *****
//find the taxs item
$taxs_items = array();
$taxs_items = $new_array['items']['0']['taxs']; //Is not this good way, with ['0']? If zero exchange ?

/*
																							var_dump($taxs_items);
																							exit();

*/
//find the taxs of shipping
$taxs_billing = array();
$taxs_billing = $new_array['shipping_cost']['0']['taxs']; //Is not this good way, with ['0']? If zero exchange ?										

/*
																							var_dump($taxs_billing);
																							exit();

*/										
										
// merge taxs array
$taxs_items_billing = array_merge($taxs_items, $taxs_billing);

/*
																							var_dump($taxs_items_billing);
																							exit();

*/	

//consolidate taxs sorting by name in a new array
$taxs_items_billing_grouped = array();
 
foreach ($taxs_items_billing as &$allOfTax_value)
	{
		$tax_name = $allOfTax_value['name_title'];
 
        if (isset($taxs_items_billing_grouped[$tax_name])) 
		{
            $taxs_items_billing_grouped[$tax_name]['price'] += $allOfTax_value['price'];
        } else 
		{
            $taxs_items_billing_grouped[$tax_name] = $allOfTax_value;
      
    	}
 
	}
 
// Sort an array by key
ksort($taxs_items_billing_grouped);
 
/*
																							var_dump($taxs_items_billing_grouped);
																							exit();

*/

//array of billing without taxs  **** ****** ******** *****
$billing_without_tax = array();
$billing_without_tax = $new_array['shipping_cost'];
// remove taxs
unset($billing_without_tax['0']['taxs']); ////Is not this good way, with ['0']? If zero exchange ?!!!!!!!!!!!!!!!
/*
																							var_dump($billing_without_tax);
																							exit();

*/

// un nouveau table avec les items  **** ****** ******** *****
$items_shipping_all_tx = array();
$items_shipping_all_tx = $new_array['items'];
// remove taxs
unset($items_shipping_all_tx['0']['taxs']); ////Is not this good way, with ['0']? If zero exchange ?!!!!!!!!!!!!!!!
/*
																							var_dump($items_shipping_all_tx);
																							exit();

*/
// add array of billing
$items_shipping_all_tx = array_merge($items_shipping_all_tx, $billing_without_tax);
/*
																							var_dump($items_shipping_all_tx);
																							exit();

*/
// add array of taxs
$items_shipping_all_tx = array_merge($items_shipping_all_tx, $taxs_items_billing_grouped);
/*
																							var_dump($items_shipping_all_tx);
																							exit();

*/

// find the total price with taxs  **** ****** ******** *****
// here are my variable for calculations 
$price_without_tax_for_each_itmes = 0;
	$price_ht_all_items = 0;  //I will use it later 
	$total_taxs = 0;  //I will use it later 
$total_sums = 0;
								
								
foreach ($items_shipping_all_tx as &$all_value)
	{
		 $total = $all_value['price'];                 //price without tax par item
	 
		 
		 
		 if (!isset($all_value['quantity'])) 
			{
				$quantity = 1; //  such as taxes (by cons I need this information later for my xml file
			} 
			else
			{
				$quantity = $all_value['quantity'];          // quantity per item
			} 
		 
		 
		 
		 $price_without_tax_for_each_itmes  = $total * $quantity;			// price total hors tax par unite

			
		$total_sums += $price_without_tax_for_each_itmes;  // amount : price without tax
		
		
	}
	
	$total_sums = number_format($total_sums, 2);
	
	echo $total_sums;
	exit();
	
									
 
So here are my questions:
How can I change my codes to not use zeros like this :  ['0'] ?
why my codes forget items tax when there are several items ? How do I correct?
 
have a nice night or day
 
Thanks
 
 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Your one example json is invalid

{
   "currency": "CAD",
   "email": "toto@gmail.com",
   "items": [
      {
         "name_title": "pen",
         "quantity": 2,
         "price": "20.00",
         "taxs": [
            {
               "price": "1.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            }
         ]
      },
	   {
         "name_title": "pensil",
         "quantity": 3,
         "price": "10.00",
         "taxs": [
            {
               "price": "1.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            }
         ]
      },
	  {
         "name_title": "notebook",
         "quantity": 2,
         "price": "5.00",
         "taxs": [
            {
               "price": "1.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            },
            {
               "price": "1.38",
               "rates": 0.05,
               "name_title": "TaxSecond"
            }
         ]
      }
   ],
   "shipping_cost": [
      {
         "code": "FDX",
         "price": "6.23",
         "name_title": "Fidex",
         "taxs": [
            {
               "price": "1.55",
               "rates": 0.25,
               "name_title": "TaxOne"
            },
            {
               "price": "2.37",
               "rates": 0.38,
               "name_title": "TaxSecond"
            }
         ]
      }
   ],
   "billing": {
      "names": "John Toto"
   },
   "billing": {
      "names": "John Toto"
   },
   "customers": {
      "invoce": null,
      "default_address": {
         "names": "John Toto"
      }
   }

Parse error on line 77:
...oto" } }
---------------------^
Expecting '}', ',', ']'

 

You can check your json at jsonlint

Link to comment
Share on other sites

Hi QuickOldCar,

 

Thanks, but I check always and I checked it :

 

My json data is valid... 

Here is my json : 

{
   "currency": "CAD",
   "email": "toto@gmail.com",
   "items": [
      {
         "name_title": "pen",
         "quantity": 1,
         "price": "20.00",
         "taxs": [
            {
               "price": "3.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            },
            {
               "price": "1.38",
               "rates": 0.05,
               "name_title": "TaxSecond"
            }
         ]
      }
   ],
   "shipping_cost": [
      {
         "code": "FDX",
         "price": "6.23",
         "name_title": "Fidex",
         "taxs": [
            {
               "price": "1.55",
               "rates": 0.25,
               "name_title": "TaxOne"
            },
            {
               "price": "2.37",
               "rates": 0.38,
               "name_title": "TaxSecond"
            }
         ]
      }
   ],
   "billing": {
      "names": "John Toto"
   },
   "billing": {
      "names": "John Toto"
   },
   "customers": {
      "invoce": null,
      "default_address": {
         "names": "John Toto"
      }
   }
}
But I think, you did not copy all of my json data in my code : in part of my exemple, you are right  last accolade " }" was lost
But in my code, There are 3 accolade " }" at end of my data.
 
Here is my code : 
$json = '
{
   "currency": "CAD",
   "email": "toto@gmail.com",
   "items": [
      {
         "name_title": "pen",
         "quantity": 2,
         "price": "20.00",
         "taxs": [
            {
               "price": "1.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            }
         ]
      },
	   {
         "name_title": "pensil",
         "quantity": 3,
         "price": "10.00",
         "taxs": [
            {
               "price": "1.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            }
         ]
      },
	  {
         "name_title": "notebook",
         "quantity": 2,
         "price": "5.00",
         "taxs": [
            {
               "price": "1.00",
               "rates": 0.15,
               "name_title": "TaxOne"
            },
            {
               "price": "1.38",
               "rates": 0.05,
               "name_title": "TaxSecond"
            }
         ]
      }
   ],
   "shipping_cost": [
      {
         "code": "FDX",
         "price": "6.23",
         "name_title": "Fidex",
         "taxs": [
            {
               "price": "1.55",
               "rates": 0.25,
               "name_title": "TaxOne"
            },
            {
               "price": "2.37",
               "rates": 0.38,
               "name_title": "TaxSecond"
            }
         ]
      }
   ],
   "billing": {
      "names": "John Toto"
   },
   "billing": {
      "names": "John Toto"
   },
   "customers": {
      "invoce": null,
      "default_address": {
         "names": "John Toto"
      }
   }
}';

// transforming my data in json table**** ****** ******** *****
$new_array = json_decode($json, true);; 

/*
																							//print_r($new_array);
																							var_dump($new_array);
																							exit();
																							*/
																							
// delete those information "billing" ; "billing" and "customers"  **** ****** ******** *****

unset($new_array['billing']);
unset($new_array['billing']);
unset($new_array['customers']);

/*
																							var_dump($new_array);
																							exit();

*/

//Consolidate taxs --> items of tax + billing  tax **** ****** ******** *****
//find the taxs item
$taxs_items = array();
$taxs_items = $new_array['items']['0']['taxs']; //Is not this good way, with ['0']? If zero exchange ?

/*
																							var_dump($taxs_items);
																							exit();

*/
//find the taxs of shipping
$taxs_billing = array();
$taxs_billing = $new_array['shipping_cost']['0']['taxs']; //Is not this good way, with ['0']? If zero exchange ?										

/*
																							var_dump($taxs_billing);
																							exit();

*/										
										
// merge taxs array
$taxs_items_billing = array_merge($taxs_items, $taxs_billing);

/*
																							var_dump($taxs_items_billing);
																							exit();

*/	

//consolidate taxs sorting by name in a new array
$taxs_items_billing_grouped = array();
 
foreach ($taxs_items_billing as &$allOfTax_value)
	{
		$tax_name = $allOfTax_value['name_title'];
 
        if (isset($taxs_items_billing_grouped[$tax_name])) 
		{
            $taxs_items_billing_grouped[$tax_name]['price'] += $allOfTax_value['price'];
        } else 
		{
            $taxs_items_billing_grouped[$tax_name] = $allOfTax_value;
      
    	}
 
	}
 
// Sort an array by key
ksort($taxs_items_billing_grouped);
 
/*
																							var_dump($taxs_items_billing_grouped);
																							exit();

*/

//array of billing without taxs  **** ****** ******** *****
$billing_without_tax = array();
$billing_without_tax = $new_array['shipping_cost'];
// remove taxs
unset($billing_without_tax['0']['taxs']); ////Is not this good way, with ['0']? If zero exchange ?!!!!!!!!!!!!!!!
/*
																							var_dump($billing_without_tax);
																							exit();

*/

// un nouveau table avec les items  **** ****** ******** *****
$items_shipping_all_tx = array();
$items_shipping_all_tx = $new_array['items'];
// remove taxs
unset($items_shipping_all_tx['0']['taxs']); ////Is not this good way, with ['0']? If zero exchange ?!!!!!!!!!!!!!!!
/*
																							var_dump($items_shipping_all_tx);
																							exit();

*/
// add array of billing
$items_shipping_all_tx = array_merge($items_shipping_all_tx, $billing_without_tax);
/*
																							var_dump($items_shipping_all_tx);
																							exit();

*/
// add array of taxs
$items_shipping_all_tx = array_merge($items_shipping_all_tx, $taxs_items_billing_grouped);
/*
																							var_dump($items_shipping_all_tx);
																							exit();

*/

// find the total price with taxs  **** ****** ******** *****
// here are my variable for calculations 
$price_without_tax_for_each_itmes = 0;
	$price_ht_all_items = 0;  //I will use it later 
	$total_taxs = 0;  //I will use it later 
$total_sums = 0;
								
								
foreach ($items_shipping_all_tx as &$all_value)
	{
		 $total = $all_value['price'];                 //price without tax par item
	 
		 
		 
		 if (!isset($all_value['quantity'])) 
			{
				$quantity = 1; //  such as taxes (by cons I need this information later for my xml file
			} 
			else
			{
				$quantity = $all_value['quantity'];          // quantity per item
			} 
		 
		 
		 
		 $price_without_tax_for_each_itmes  = $total * $quantity;			// price total hors tax par unite

			
		$total_sums += $price_without_tax_for_each_itmes;  // amount : price without tax
		
		
	}
	
	$total_sums = number_format($total_sums, 2);
	
	echo $total_sums;
	exit();
 
How can I change my codes to not use zeros like this :  ['0'] ?
why my codes forget items tax when there are several items ? How do I correct?

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.