Skip to content

Instantly share code, notes, and snippets.

@sjehutch
Created October 29, 2015 19:53
Show Gist options
  • Save sjehutch/913494bb0fea719e154b to your computer and use it in GitHub Desktop.
Save sjehutch/913494bb0fea719e154b to your computer and use it in GitHub Desktop.
adorer-func
public function addorder()
{
if ($this->request->is('post'))
{
//$order = array(3=>3,56=>2,87=>9);
//echo $this->request->data['orderData'] = json_encode($order);
$deliveryId = $this->request->data['deliveryId'];
$stripeCardId = $this->request->data['stripeCardId'];
$deliveryStartDate = $this->request->data['deliveryStartDate'];
$deliveryDate = $this->request->data['deliveryDate'];
$couponCode = $this->request->data['couponCode'];
$orderData = json_decode($this->request->data['items'],true);
$orderDataItems = $orderData['itemDetails'];
$required = array("deliveryId"=>$deliveryId,"deliveryStartDate"=>$deliveryStartDate,"deliveryDate"=>$deliveryDate,"stripeCardId"=>$stripeCardId,"items"=>$orderDataItems);
if($this->validateRequiredFields($required))
{
$customerId = $_SESSION['sessAppUserId'];
$this->loadModel('DeliveryAddress');
$options = array('conditions' => array('DeliveryAddress.id' => $deliveryId,'DeliveryAddress.customerId'=>$customerId));
$data= $this->DeliveryAddress->find("all",$options);
if (empty($data))
{
throw new ApiNoDataFoundException(__('INVALID DELIVERY ADDRESS ID'));
}
else
{
$deliveryDayId = $this->getDayId($deliveryDate);
$delDate = $this->getDate($deliveryDate); // get date
$maxDeliveryTime = $delDate." "."12:00:00";
$finalData = array();
$checkOrderFlag = 0;
$alcoholicFlag = 0 ;
$salesTax = 0;
$orderPrice = 0;
foreach($orderDataItems as $value)
{
$productId = $value['itemId'];
$quantity = $value['quantity'];
$price = $value['price'];
$isTaxable = $value['isTaxable'];
$orderPrice = $orderPrice + CakeNumber::precision(($price * $quantity),2);
$productData = $this->checkProduct($productId);
$stockQuantity = $productData['quantity'];
$stockPrice = $productData['price'];
$isAlcoholic = $productData['isAlcoholic'];
$productStatus = $productData['status'];
///Is taxable calculation//
if($isTaxable == 1){
$salesTax = $salesTax + (($quantity * $price * Configure::read('SALES_TAX'))/100);
}
///End Is taxable calculation//
if($isAlcoholic > 0)
{
$alcoholicFlag = 1;
}
if($stockQuantity < $quantity or $stockPrice != $price or $productStatus !=1) // product status 1=Active,0=inactive,2=deleted
{
$finalData['data'][] = array('itemId'=>$productId,'quantity'=>$quantity,'price'=>$price,'stockQuantity'=>$stockQuantity,'stockPrice'=>$stockPrice,'stockStatus'=>0);
$checkOrderFlag = 1;
}
else
{
$finalData['data'][] = array('itemId'=>$productId,'quantity'=>$quantity,'price'=>$price,'stockQuantity'=>$stockQuantity,'stockPrice'=>$stockPrice,'stockStatus'=>1);
}
}
if($alcoholicFlag == 1 and $deliveryDayId == 1 and strtotime($deliveryDate) <= strtotime($maxDeliveryTime)) // at sunday before noon we can not deliver Alcoholic products $deliveryDayId == 1 means sunday
{
throw new ApiInvalidArgumentException(__("UNABLE TO DELIVER ALCOHOLIC PRODUCTS ON SUNDAY BEFORE 12 NOON"));
}
elseif($checkOrderFlag == 1)
{
throw new ApiNoDataFoundException($finalData);
}
else
{
$couponId = 0.99; // Assign default coupon id greater than 0
$cartMinAmount == 1;
if(isset($couponCode) and $couponCode != "")
{
$couponResult = $this->isCouponExist($couponCode);
$cartMinAmount = $couponResult[0]['coupons']['cartMinAmount'];
//$cartMinAmount =0 means remove check for minimum $35
if($couponResult)
{
$couponId = $couponResult[0]['coupons']['id'];
$orderAmountPlusTax = $orderPrice + $salesTax ;
$discountAmount = $this->getDiscountAmount($couponId,$orderAmountPlusTax);
}
else
{
$couponId = 0;
throw new ApiInvalidArgumentException(__("INVALID COUPON CODE"));
}
}
else
{
$discountAmount = 0;
}
if($couponId > 0)
{
if(($orderPrice+$salesTax)>=35 or $cartMinAmount ==0)
{
if(($orderPrice+$salesTax) > $discountAmount)
{
unset($finalData);
$this->request->data['customerId'] = $customerId;
$this->request->data['deliveryDate'] = $deliveryDate;
if($this->isMaxOrderExceeded($deliveryDate))
{
$this->request->data['deliveryStatus'] = 0;
// Sales tax caclulation /////////////////
//$salesTax = $orderPrice * Configure::read('SALES_TAX')/100 ;
$this->request->data['salesTax'] = $salesTax;
$this->request->data['orderPrice'] = $orderPrice; // update order price with sales tax
if($couponId == 0.99) // If no coupon selected
$couponId = 0;
$this->request->data['couponId'] = $couponId;
/////////////////////////////////////////
if ($this->Order->save($this->request->data))
{
$orderId = $this->Order->getLastInsertID();
$this->loadModel('OrderDetail');
$finalData['orderId'] = $orderId;
foreach($orderDataItems as $value)
{
$orderItem['orderId'] = $orderId;
$productId = $orderItem['productId'] = $value['itemId'];
$quantity = $orderItem['quantity'] = $value['quantity'];
$isTaxable = $orderItem['isTaxable'] = $value['isTaxable'];
$price = $orderItem['price'] = $value['price'];
$this->OrderDetail->create();
$this->OrderDetail->save($orderItem);
unset($orderItem);
}
$fdata[] = $finalData;
$stripeCustomerId = $_SESSION['sessStripeCustomerId'];
//echo $salesTax."======Tax Before Round<br/>";
$salesTax = round($salesTax, 2, PHP_ROUND_HALF_UP);
//echo $orderPrice."====Order Price<br/>";
//echo $salesTax."======Tax<br/>";
//echo $discountAmount."======DiscountAmount<br/>";
$totalAmount = $orderPrice + $salesTax - round($discountAmount, 2, PHP_ROUND_HALF_UP);
//echo $totalAmount."======Total Amount<br/>";
//die;
//if($totalAmount < 0 ) // if discount is greater than order price + sales tax
//$totalAmount = 0;
$charge = $this->Stripe->payment($stripeCustomerId,$stripeCardId,$totalAmount,$orderId);
$chargeId = $charge->id;
/*
$paid = $charge->paid;
$chargeStatus = $charge->status;
$chargeAmount = $charge->amount;*/
//$this->sendOrderEvent($orderId);
if($charge->status == 'succeeded')// order success
{
$this->Order->updateAll(
array('Order.paymentStatus' => 1,'Order.stripeChargeId' => "'".$chargeId."'"), // Fields to update
array('Order.id' => $orderId) // Where Clase
); // Update order table with payment status and charge id
// Update payment status and product quantity for each products
foreach($orderDataItems as $value)
{
$productId = $orderItem['productId'] = $value['itemId'];
$quantity = $orderItem['quantity'] = $value['quantity'];
$this->loadModel('Product');
$this->Product->updateAll(
array('Product.quantity' => "Product.quantity - $quantity" ), // Fields to update
array('Product.id' => $productId) // Where Clase
);
$this->OrderDetail->updateAll(
array('OrderDetail.productPaymentStatus' => "1",'OrderDetail.stripeChargeId' => "'".$chargeId."'" ), // Fields to update
array('OrderDetail.productId' => $productId) // Where Clase
);
}
if(isset($couponCode) and $couponCode != "")
{
if($couponResult)
{
$couponId = $couponResult[0]['coupons']['id'];
$couponCount = $couponResult[0]['coupons']['couponCount']; //
$this->setCouponStatus($couponId,$couponCount);
}
}
$this->sendOrderMail($orderId,$couponId,$couponCode); // used to send order mail
$this->sendOrderEvent($orderId); // used to send order mail
$this->set('data', $fdata);
}
else
{
throw new ApiPaymentException(__("CARD ERROR"));
//throw new ApiRequiredParameterMissing($this->request->data);
}
}
else
{
throw new ApiInvalidArgumentException(__("INVALID DATA"));
}
}
else
{
throw new ApiPaymentException(__("UNABLE TO ADD ORDER AT THIS SLOT,MAX ORDER EXCEEDED"));
}
}
else
{
throw new ApiInvalidArgumentException("Your order amount must be greater than coupon amount!");
}
}
else
{
throw new ApiInvalidArgumentException("Your order must be at least $35.00!");
}
}
}
}
}
else
{
throw new ApiRequiredParameterMissing($required);
//throw new ApiRequiredParameterMissing(__("REQUIRED PARAMETER MISSING"));
}
}
else
{
throw new ApiMethodNotAllowedException(__("INVALID METHOD REQUEST"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment