Пример #1
0
 public static function addNewOrder($param, $request, $url = 'admin/child')
 {
     $tables = self::$servicelocator->get('doctrine');
     $table = new orderModel($tables);
     $orderItem = new orderdetailModel($tables);
     $tableModel = new tableModel($tables);
     $transactionModel = new transactionModel($tables);
     $translator = self::translate();
     $id = $param->fromRoute('id');
     $catID = 0;
     $tableManage = $tableModel->findBy(array('isdelete' => 0));
     $save = $param->fromPost('save');
     $status = 'pending';
     $isdelete = 0;
     if ($save == "payment") {
         $status = 'finish';
         $isdelete = 1;
     }
     //insert
     if ($id == '') {
         if ($request->isPost()) {
             $Auth_service = new AuthenticationService();
             $auth = $Auth_service->getIdentity();
             if ($auth->userId) {
                 $tableId = $param->fromPost('table_id');
                 $dataDetail = $param->fromPost('detail');
                 $totalRealCost = $param->fromPost('total_real_cost');
                 $cat = new Orders();
                 $cat->setTotalCost($param->fromPost('total_cost'));
                 $cat->setTableId($param->fromPost('table_id'));
                 $cat->setCreateDate(time());
                 $cat->setCouponId($param->fromPost('coupon_id'));
                 $cat->setToTalRealCost($totalRealCost);
                 $cat->setUserId($auth->userId);
                 $cat->setSurtaxId($param->fromPost('surtax_id'));
                 $cat->setIsdelete(0);
                 $cat->setStatus($status);
                 $cat->setCustomerId(0);
                 $cat->setNewDate(date('Y-m-d H:i:s', time()));
                 $cat = $table->insert($cat);
                 $catID = $cat->getId();
                 if ($param->fromPost('coupon_id') != -1) {
                     $doctrine = self::$servicelocator->get('doctrine');
                     $couponModel = new couponModel($doctrine);
                     $coupon = $couponModel->findOneBy(array('id' => $param->fromPost('coupon_id')));
                     if ($coupon->getReuse() == 0) {
                         if ($save == 'payment') {
                             $coupon->setIsdelete($isdelete);
                             $couponModel->edit($coupon);
                         }
                     }
                 }
                 foreach ($dataDetail as $k => $val) {
                     if ($val['menuid'] != -1) {
                         self::insertOrderDetail($val, $catID);
                     }
                 }
                 if ($save == 'payment') {
                     $url = "http://" . $_SERVER['HTTP_HOST'] . '/frontend/order/detail/' . $catID;
                     header("Location:" . $url);
                     exit;
                 } else {
                     $url = "http://" . $_SERVER['HTTP_HOST'] . '/frontend/order/add';
                     header("Location:" . $url);
                     exit;
                 }
             } else {
                 $url = "http://" . $_SERVER['HTTP_HOST'] . '/frontend/login';
                 header("Location:" . $url);
                 exit;
             }
         }
         //insert new user
         //$this->redirect()->toRoute('admin/child',array('controller'=>'category'));
         return array('title' => $translator->translate('Add new order'), 'data' => null, 'url' => $url, 'orderId' => $catID, 'tables' => $tableManage, 'dataObject' => array());
     } else {
         $cats = $table->findOneBy(array('id' => $id));
         $orderDetail = $orderItem->findBy(array('orderId' => $id, 'isdelete' => 0));
         if ($request->isPost()) {
             $idFormPost = $param->fromPost('id');
             $cat = $table->findOneBy(array('id' => $idFormPost));
             $dataDetail = $param->fromPost('detail');
             $Auth_service = new AuthenticationService();
             $auth = $Auth_service->getIdentity();
             $totalRealCost = $param->fromPost('total_real_cost');
             $totalCost = $param->fromPost('total_cost');
             $tableId = $param->fromPost('table_id');
             $cat->setTotalCost($totalCost);
             $cat->setTableId($param->fromPost('table_id'));
             $cat->setCreateDate(time());
             $cat->setCouponId($param->fromPost('coupon_id'));
             $cat->setToTalRealCost($totalRealCost);
             $cat->setUserId($auth->userId);
             $cat->setSurtaxId($param->fromPost('surtax_id'));
             $cat->setIsdelete(0);
             $cat->setStatus($status);
             $cat->setCustomerId(0);
             $cat->setNewDate(date('Y-m-d H:i:s', time()));
             $table->edit($cat);
             if ($param->fromPost('coupon_id') != -1) {
                 $doctrine = self::$servicelocator->get('doctrine');
                 $couponModel = new couponModel($doctrine);
                 $coupon = $couponModel->findOneBy(array('id' => $param->fromPost('coupon_id')));
                 if ($coupon->getReuse() == 0) {
                     if ($save == 'payment') {
                         $coupon->setIsdelete(1);
                         $couponModel->edit($coupon);
                     }
                 }
             }
             //update order
             // delete order detail
             $orderItem->deleteAll(array('orderId' => $id));
             $transactionModel->deleteAll(array('orderId' => $id));
             // insert order detail
             foreach ($dataDetail as $k => $val) {
                 if ($val['menuid'] != -1) {
                     self::insertOrderDetail($val, $id);
                 }
             }
             if ($save == 'payment') {
                 $url = "http://" . $_SERVER['HTTP_HOST'] . '/frontend/order/detail/' . $id;
                 header("Location:" . $url);
                 exit;
             } else {
                 $url = "http://" . $_SERVER['HTTP_HOST'] . '/frontend/order/add';
                 header("Location:" . $url);
                 exit;
             }
         }
         $cat = $table->convertSingleToArray($cats);
         return array('title' => 'Edit Order', 'data' => $cat, 'url' => $url, 'orderDetails' => $orderDetail, 'orderId' => 0, 'dataObject' => $cats, 'tables' => $tableManage);
     }
 }
Пример #2
0
 function splitAction()
 {
     if ($this->getRequest()->isPost()) {
         $post = $this->params()->fromPost();
         $table = $post['table-new'];
         $discount = -1;
         //    $oldDiscount = $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(-1);
             $orderEntity->setToTalRealCost(0);
             $orderEntity->setUserId($auth->userId);
             $orderEntity->setSurtaxId(0);
             $orderEntity->setIsdelete(0);
             $orderEntity->setStatus($status);
             $orderEntity->setNewDate(date('Y-m-d H:i:s', time()));
             $orderLastInsert = $this->modelOrder->insert($orderEntity);
             $lastOrderId = $orderLastInsert->getId();
             $totalPrice = 0;
             $oldPrice = 0;
             $newRealCost = 0;
             //                echo '<pre>';
             //                print_r($post);die;
             foreach ($post as $order) {
                 $order['newCoupon'] = -1;
                 $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']));
                         $orderDetailModel->setDiscount(-1);
                         $orderDetailModel->setRealCost($Cost);
                         //    }
                         $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['realcost'] = $realCost;
                         $data['orderDetailType'] = $order['costType'];
                         //                            $data['discount'] = $order['newCoupon'];
                         $data['discount'] = -1;
                         $newCost = $data['realcost'];
                         Utility::insertOrderDetail($data, $lastOrderId);
                         // update old order-item detail
                         $lastQuantity = $order['oldQty'] - $order['qty'];
                         $newRealCost = $lastQuantity * $order['menuCost'];
                         $orderDetailModel->setQuantity($newRealCost);
                         $orderDetailModel->setRealCost($newRealCost);
                         $this->modelOrderDetail->edit($orderDetailModel);
                     }
                 } else {
                     //                        $newRealCost = $orderDetailModel->getRealCost();
                 }
             }
             $oldOrderModel = $this->modelOrder->findOneBy(array('id' => $oldOrder));
             $sumCost = $this->modelOrderDetail->createQuerySumOrderDetail($oldOrder);
             $returnCost = $sumCost[0]['realCost'];
             $oldTotalCost = $returnCost;
             //                $oldPrice = Utility::getPriceUseCoupon($oldTotalCost,$oldOrderModel->getCouponId());
             //                $oldPrice = Utility::getPriceUseSurtax($oldPrice,$oldOrderModel->getSurtaxId());
             $oldOrderModel->setTotalRealCost($oldTotalCost);
             $oldOrderModel->setTotalCost($oldTotalCost);
             $this->modelOrder->edit($oldOrderModel);
             $newTotalPrice = $totalPrice;
             $sumCostLast = $this->modelOrderDetail->createQuerySumOrderDetail($lastOrderId);
             $returnCostLast = $sumCostLast[0]['realCost'];
             //                $realCostAfter = Utility::getPriceUseCoupon($returnCostLast,$discount)  ;
             $realCostAfter = $returnCostLast;
             //update last order
             $lastOrder = $this->modelOrder->findOneBy(array('id' => $lastOrderId));
             $lastOrder->setToTalRealCost($realCostAfter);
             $lastOrder->setTotalCost($realCostAfter);
             $this->modelOrder->edit($lastOrder);
             // update old order
         }
     }
     $this->flashMessenger()->addSuccessMessage('Split order success!');
     return $this->redirect()->toRoute('frontend/child', array('controller' => 'order', 'action' => 'add'));
 }