rewrite php array based on condition - php

I have one array and i want change the vehicle price based on number of days here is my array want to rewrite the array,i get number of days i get $rental_days and based on that total price is multiplied,now i want to change it such that if days are 1,2,3,4 than charge will be applied if 5,6,7 than the last price will be applicable then how should i rewrite an array based on this condition (total_price)?
$vehicles[$result['vehicle_id']] = array(
'vehicle_id' => $result['vehicle_id'],
'title' => $result['manufacturer']." ".$result['series'],
'manufacturer' => $result['manufacturer'],
'series' => $result['series'],
'year' => $result['year'],
'class' => $result['class'],
'image' => $image,
'image_thumb' => $image_thumb,
'description' => $description,
'seats' => $vehicle_meta['seats'],
'doors' => $vehicle_meta['doors'],
'conditioning' => $vehicle_meta['conditioning'],
'transmission' => $vehicle_meta['transmission'],
'total_price' => $rc_currency->format($rental_days*$result['rent']),
'daily_price' => $rc_currency->format($result['rent'])
);

If I understand you correctly....
Do your if statement to check for days, then apply the charge to the total at the bottom
Not sure if you want to apply the charge once, or times it by the amount of days as well...I'll leave that to you to figure out
if(($rental_days>0)&&($rental_days<=4)){
$charge = 12.00; //whatever you charge for 4 days
} elseif(($rental_days>4)&&($rental_days<=7)){
$charge = 25.00; //whatever you charge for 567 days
} elseif($rental_days>7){
$charge = 40.00; //whatever you charge for 8 days
} else {$charge = 0;}
$vehicles[$result['vehicle_id']] = array(
'vehicle_id' => $result['vehicle_id'],
'title' => $result['manufacturer']." ".$result['series'],
'manufacturer' => $result['manufacturer'],
'series' => $result['series'],
'year' => $result['year'],
'class' => $result['class'],
'image' => $image,
'image_thumb' => $image_thumb,
'description' => $description,
'seats' => $vehicle_meta['seats'],
'doors' => $vehicle_meta['doors'],
'conditioning' => $vehicle_meta['conditioning'],
'transmission' => $vehicle_meta['transmission'],
'total_price' => $rc_currency->format(($rental_days*$result['rent'])+$charge),
'daily_price' => $rc_currency->format($result['rent'])
);

Related

How do I set a max quantity in darryldecode shopping cart in laravel?

\Cart::add(array(
'id' => $request->test,
'name' => $price->tests->item_name,
'quantity' => 1,
'price' => $price->price,
'attributes' => array(
'lab_logo' => $price->labs->logo,
'lab_name' => $price->labs->name,
'item_number' => $price->tests->item_number
),
));
When i add the same product it increments the item quantity but i want the quantity of the item to be 1 at max.
According to the documentation:
// NOTE: as you can see by default, the quantity update is relative to its current value
// if you want to just totally replace the quantity instead of incrementing or decrementing its current quantity value
// you can pass an array in quantity value like so:
Cart::update(456, array(
'quantity' => array(
'relative' => false,
'value' => 5
),
));
So, if you pass an array on add and override it to not be relative, it should work as expected:
Cart::add(array(
'id' => $request->test,
'name' => $price->tests->item_name,
'quantity' => array(
'relative' => false,
'value' => 1,
),
'price' => $price->price,
'attributes' => array(
'lab_logo' => $price->labs->logo,
'lab_name' => $price->labs->name,
'item_number' => $price->tests->item_number
),
));

DRY array in php

Hello I am trying to apply the DRY concept to my program which now has over 3000 lines of codes just because i have been repeating myself a lot.
I have this array:
// prepare the sales payload
$sales_payload = array(
'organization_id' => $getOrg['data'][0]['id'],
'contact_id' => $contact_id,
'status' => 'Open',
'responsible_employee_id' => 'employee:50c76c15262b12d36d44e34a3f0f8c3d',
'source' => array(
'id' => $source['data'][0]['id'],
'name' => $source['data'][0]['name'],
),
'subject' => " ".str_replace($strToRemove, "", $_POST['billing_myfield12'])." - "."Online marketing Duitsland",
'start_date' => date("Y-m-d"), // set start date on today
'expected_closing_date' => date("Y-m-d",strtotime(date("Y-m-d")."+ 14 days")), // set expected closing date 2 weeks from now
'chance_to_score' => '10%',
'expected_revenue' => 0, //set the expected revenue
'note' => $_POST['order_comments'],
'progress' => array(
'id'=>'salesprogress:200a53bf6d2bbbfe' //fill a valid salesprogress id to set proper sales progress
),
"custom_fields" => [["voorstel_diensten"=>implode("-",$online_marketing_check)]],
);
Is there a way I can dynamically change certain fields only instead of copying the whole thing and manually changing it.
You can declare a function that clones the array and changes it
function arrayClone($args){
$sales_payload = array(
'organization_id' => $getOrg['data'][0]['id'],
'contact_id' => $contact_id,
'status' => 'Open',
'responsible_employee_id' => 'employee:50c76c15262b12d36d44e34a3f0f8c3d',
'source' => array(
'id' => $source['data'][0]['id'],
'name' => $source['data'][0]['name'],
),
'subject' => " ".str_replace($strToRemove, "", $_POST['billing_myfield12'])." - "."Online marketing Duitsland",
'start_date' => date("Y-m-d"), // set start date on today
'expected_closing_date' => date("Y-m-d",strtotime(date("Y-m-d")."+ 14 days")), // set expected closing date 2 weeks from now
'chance_to_score' => '10%',
'expected_revenue' => 0, //set the expected revenue
'note' => $_POST['order_comments'],
'progress' => array(
'id'=>'salesprogress:200a53bf6d2bbbfe' //fill a valid salesprogress id to set proper sales progress
),
"custom_fields" => [["voorstel_diensten"=>implode("-",$online_marketing_check)]],
);
return array_replace_recursive($sales_payload, $args);
}
How tu use it:
$a = arrayClone(['status' => 'Closed', 'contact_id' => 5]);
$b = arrayClone(['status' => 'Closed Now', 'contact_id' => 20]);

Looping an array within an array php

Hi there i need some help to loop an array of items.
The part that I have repeated needs to be looped in order to get multiple items from the database. At this stage I am only to invoice one item where as users generally purchase a few so i need to run a foreach statement I think. Please excuse my ignorance and lack of understanding, I have tried a series of things but they do not seem to work
$json1 = array(
'Date' => $currentDate->format('Y-m-d\TH:i:s'),
'Customer' => array(
'DisplayID' => $phone
),
'CustomerPurchaseOrderNumber' => $ordID,
'Freight' => '0',
'BalanceDueAmount' => '0.0',
'Status' => 'Closed',
'Lines' => /*$orderDetails,*/ array(
array(
'ShipQuantity' => $ShowDetails['quantity'],
'UnitPrice' => $ShowDetails['unit_price'],
'Total' => $ShowDetails['total_price'],
'Item' => array(
'UID' => $productUID,//itemuid(609400gmkit)
),
'Description' => $productTitle. ' ' .$sizeTitle. ' '
.$colorTitle,
'TaxCode' => array(
'UID' => '7d1f2e99-ffe0-4463-9c29-
61d078b392d3'//taxcodeuid(GST)
)
)
),
'Freight' => $shippingprice,//if dg then $10 otherwise $0 unless below
$100
'ShippingMethod' => $ShippingMethod,// if dg the 'MyFreight' otherwise
'transdirect'
);
The portion that i need to loop is as follows :
array(
'ShipQuantity' => $ShowDetails['quantity'],
'UnitPrice' => $ShowDetails['unit_price'],
'Total' => $ShowDetails['total_price'],
'Item' => array(
'UID' => '27d56af8-d3d6-4f0a-b71b-
4586fffbd63a'//itemuid(609400gmkit)
),
'Description' => $productTitle. ' ' .$sizeTitle. ' '
.$colorTitle,
'TaxCode' => array(
'UID' => '7d1f2e99-ffe0-4463-9c29-
61d078b392d3'//taxcodeuid(GST)
)
)*/

Auth.net eCheck implementation using John Conde's AuthnetXML Class

I have an existing implementation of this class processing subscriptions by credit card but I now need to add the facility to accept payment by eCheck but I am unsure of how to change this portion of the code:
'payment' => array(
'creditCard' => array(
'cardNumber' => '4111111111111111',
'expirationDate' => '2016-08'
)
),
I have come up with the following from referencing the AIM guide pdf and AIM guide XML pdf
'payment' => array(
'bankAccount' => array( // x_method equivalent ?
'routingNumber' => '', // x_bank_aba_code equivalent ?
'accountNumber' => '', // x_bank_acct_num equivalent ?
'nameOnAccount' => '', // x_bank_acct_name equivalent ?
'bankName' => '', // x_bank_name equivalent ?
'echeckType' => 'WEB' // x_echeck_type equivalent
/*
x_bank_acct_type has no equivalent ?
*/
)
),
but there appears to be some discrepancies between the required fields?
Any pointers before I start on this would be greatly appreciated.
Looking at Authorize.Net's documentation this should work:
'payment' => array(
'bankAccount' => array(
'accountType' => '', // 'checking'
'routingNumber' => '',
'accountNumber' => '',
'nameOnAccount' => ''
)
),
Following on from John's answer this is the complete code I used to solve this for anyone finding this through a google search:
$xml->ARBCreateSubscriptionRequest(array(
'subscription' => array(
'name' => 'SubscriptionName',
'paymentSchedule' => array(
'interval' => array(
'length' => '1',
'unit' => 'months'
),
'startDate' => date('Y-m-d', time()), // Format: YYYY-MM-DD
'totalOccurrences' => '9999' // To submit a subscription with no end date (an ongoing subscription), this field must be submitted with a value of 9999
),
'amount' => $eCart1->GrandTotal(), // total monthly subscription
'payment' => array(
'bankAccount' => array(
'accountType' => ((isset($_POST["accountType"]))?$_POST["accountType"]:""), // options available are checking or businessChecking in this instance
'routingNumber' => ((isset($_POST["routingNumber"]))?$_POST["routingNumber"]:""),
'accountNumber' => ((isset($_POST["accountNumber"]))?$_POST["accountNumber"]:""),
'nameOnAccount' => ((isset($_POST["nameOnAccount"]))?$_POST["nameOnAccount"]:""),
'echeckType' => ((isset($_POST["echeckType"]))?$_POST["echeckType"]:"") // if businessChecking is chosen then 'CCD' else 'WEB'
)
),
'customer' => array(
'id' => "'".$_SESSION['clientID']."'",
'email' => "'".((isset($_POST["email"]))?$_POST["email"]:"")."'"
),
'billTo' => array(
'firstName' => "'".((isset($_POST["firstname"]))?$_POST["firstname"]:"")."'",
'lastName' => "'".((isset($_POST["lastname"]))?$_POST["lastname"]:"")."'",
'company' => "'".((isset($_POST["company"]))?$_POST["company"]:"")."'",
'address' => "'".((isset($_POST["street1"]))?$_POST["street1"]:"")."'",
'city' => "'".((isset($_POST["city"]))?$_POST["city"]:"")."'",
'state' => "'".((isset($_POST["state_province"]))?$_POST["state_province"]:"")."'",
'zip' => "'".((isset($_POST["postcode"]))?$_POST["postcode"]:"")."'"
)
)
));

How to get all the specific key value from multidimensional array in php?

How can I get the all the 'name' and 'city' value back in an array from the following multidimensional array?
$myarray=Array(Array('name' => 'A','id' => '1', 'phone' => '416-23-55',
Base => Array ('city' => 'toronto'),'EBase' => Array('city' => 'North York'),
'Qty' => '1'), (Array('name' =>'B','id' => '1','phone' => '416-53-66','Base' =>
Array ('city' => 'qing'), 'EBase' => Array('city' => 'chong'),'Qty' => '2')));
I expect the returned value be
$namearray=Array('A','B');
$basecityarray=Array('toronto','qing');
$Ebasecityarray=Array('North York','chong');
Thank you!
You can definitely try something like this:
$shop = array( array( 'Title' => "rose",
'Price' => 1.25,
'Number' => 15
),
array( 'Title' => "daisy",
'Price' => 0.75,
'Number' => 25,
),
array( 'Title' => "orchid",
'Price' => 1.15,
'Number' => 7
)
);
You can access the first row as
echo $shop[0][0]." costs ".$shop[0][1]." and you get ".$shop[0][2]."<br />";
or $shop[0]->Title will return to you rose.

Categories