Пример #1
0
 function splitAction()
 {
     if ($this->getRequest()->isPost()) {
         $post = $this->params()->fromPost();
         $table = $post['table-new'];
         $discount = $post['discount'];
         $oldOrder = $post['oldOrder'];
         unset($post['table-new']);
         unset($post['discount']);
         unset($post['oldOrder']);
         if (!empty($post)) {
             $Auth_service = new AuthenticationService();
             $auth = $Auth_service->getIdentity();
             $status = 'pending';
             $orderEntity = new Orders();
             $orderEntity->setTotalCost(0);
             $orderEntity->setTableId($table);
             $orderEntity->setCreateDate(time());
             $orderEntity->setCouponId($discount);
             $orderEntity->setToTalRealCost(0);
             $orderEntity->setUserId($auth->userId);
             $orderEntity->setSurtaxId(0);
             $orderEntity->setIsdelete(0);
             $orderEntity->setStatus($status);
             $orderLastInsert = $this->modelOrder->insert($orderEntity);
             $lastOrderId = $orderLastInsert->getId();
             $totalPrice = 0;
             $oldPrice = 0;
             $newRealCost = 0;
             foreach ($post as $order) {
                 $orderDetailId = $order['orderDetailId'];
                 $oldQty = $order['oldQty'];
                 $qty = $order['qty'];
                 $totalRealCost = $order['menuCost'] * $order['oldQty'];
                 $qty >= $oldQty ? $qty = $oldQty : ($qty = $qty);
                 $orderDetailModel = $this->modelOrderDetail->findOneBy(array('id' => $orderDetailId));
                 // check quantity split every order
                 $orderDetail = new OrderDetail();
                 if ($qty > 0) {
                     if ($qty >= $oldQty) {
                         // update orderId for order detail
                         $orderDetailModel->setOrderId($lastOrderId);
                         if ($order['newCoupon'] != -1) {
                             $Cost = $orderDetailModel->getQuantity() * $orderDetailModel->getMenuCost();
                             $orderDetailModel->setDiscount($order['newCoupon']);
                             $orderDetailModel->setRealCost(Utility::getPriceUseCoupon($Cost, $order['newCoupon']));
                         }
                         $this->modelOrderDetail->edit($orderDetailModel);
                         $newCost = $orderDetailModel->getRealCost();
                     } else {
                         // insert new order detail
                         $realCost = $qty * Utility::getMenuValue($order['menuId'], $order['costType']);
                         $data['menuid'] = $order['menuId'];
                         $data['quantity'] = $qty;
                         $data['menuCost'] = $order['menuCost'];
                         $data['realcost'] = Utility::getPriceUseCoupon($realCost, $order['newCoupon']);
                         $data['orderDetailType'] = $order['costType'];
                         $data['discount'] = $order['newCoupon'];
                         $newCost = $data['realcost'];
                         Utility::insertOrderDetail($data, $lastOrderId);
                         // update old order-item detail
                         $lastQuantity = $order['oldQty'] - $order['qty'];
                         //                            if ($order['discountType'] != '' && $order['discountType'] == 0) {
                         //                                $newRealCost = ($lastQuantity * $order['menuCost']) - $order['discountValue'];
                         //                            } elseif ($order['discountType'] != '' && $order['discountType'] == 1) {
                         //                                $newRealCost = Utility::roundCost(($lastQuantity * $order['menuCost']) - ((($lastQuantity * $order['menuCost']) * $order['discountValue']) / 100));
                         //                            } else {
                         //                                $newRealCost = ($lastQuantity * $order['menuCost']);
                         //                            }   this code replace by static function Utility::getPriceUseCoupon(price,discountId);
                         $newRealCost = Utility::getPriceUseCoupon($lastQuantity * $order['menuCost'], $order['discountId']);
                         $orderDetailModel->setQuantity($lastQuantity);
                         $orderDetailModel->setRealCost($newRealCost);
                         $this->modelOrderDetail->edit($orderDetailModel);
                     }
                 } else {
                     $newRealCost = $orderDetailModel->getRealCost();
                 }
                 $oldPrice += $newRealCost;
                 $totalPrice += $newCost;
             }
             $oldOrderModel = $this->modelOrder->findOneBy(array('id' => $oldOrder));
             $oldOrderModel->setTotalRealCost($oldPrice);
             $oldOrderModel->setTotalCost($oldPrice);
             $this->modelOrder->edit($oldOrderModel);
             $newTotalPrice = $totalPrice;
             //                $coupon = Utility::getCouponInfo($discount);
             //
             //                if ($coupon->getType() == 0) {
             //
             //                    $realCostAfter = Utility::roundCost($newTotalPrice - $coupon->getValue());
             //
             //                } elseif ($coupon->getType() == 1) {
             //
             //                    $realCostAfter = Utility::roundCost($newTotalPrice - ($newTotalPrice * $coupon->getValue()) / 100);
             //
             //                } else {
             //
             //                    $realCostAfter = $newTotalPrice;
             //
             //                } this code replace by function Utility::getPriceUseCoupon(price,discountId);
             $realCostAfter = Utility::getPriceUseCoupon($newTotalPrice, $discount);
             //update last order
             $lastOrder = $this->modelOrder->findOneBy(array('id' => $lastOrderId));
             $lastOrder->setToTalRealCost($realCostAfter);
             $lastOrder->setTotalCost($newTotalPrice);
             $this->modelOrder->edit($lastOrder);
             // update old order
         }
     }
     $this->flashMessenger()->addSuccessMessage('Split order success!');
     return $this->redirect()->toRoute('frontend/child', array('controller' => 'order', 'action' => 'add'));
 }
Пример #2
0
 public function mergeAction()
 {
     if ($this->getRequest()->isPost()) {
         $fromTable = $this->params()->fromPost('fromTable');
         $toOrderId = $this->params()->fromPost('toTable');
         $orderDetailModel = $this->modelOrderDetail->findBy(array('orderId' => $fromTable));
         if (!empty($orderDetailModel) && $fromTable != $toOrderId) {
             foreach ($orderDetailModel as $orderDetail) {
                 $orderDetail->setOrderId($toOrderId);
                 $this->modelOrderDetail->edit($orderDetail);
             }
             $orderFrom = $this->modelOrder->findOneBy(array('id' => $fromTable));
             $totalCostFrom = $orderFrom->getTotalCost();
             $totalRealCostFrom = $orderFrom->getTotalRealCost();
             $orderTo = $this->modelOrder->findOneBy(array('id' => $toOrderId));
             $finalTotalRealCost = $orderTo->getTotalCost() + $totalCostFrom;
             $orderTo->setTotalCost($finalTotalRealCost);
             $finalTotalRealCost = Utility::getPriceUseCoupon($finalTotalRealCost, $orderTo->getCouponId());
             $finalTotalRealCost = Utility::getPriceUseSurtax($finalTotalRealCost, $orderTo->getSurtaxId());
             $orderTo->setTotalRealCost($finalTotalRealCost);
             $this->modelOrder->edit($orderTo);
             $this->modelOrder->delete(array('id' => $fromTable));
             $this->flashMessenger()->addSuccessMessage('Merge order success!');
             return $this->redirect()->toRoute('frontend/child', array('controller' => 'order', 'action' => 'add'));
         } else {
             $this->flashMessenger()->addErrorMessage('Opp! Something Wrong, pls try again...');
             return $this->redirect()->toRoute('frontend/child', array('controller' => 'order', 'action' => 'add'));
         }
     }
 }