截单反架系统分配货位优化

//控制器(Controller)
class Order_OrderController extends Zend_Controller_Action {

 //截单&生成反架单
 public function draftStopAction() {
      $code = trim($this->_request->getParam('code', ''));
      $stoporderid = trim($this->_request->getParam('stoporderid', ''));
      if ($code) {
           $order = $this->_s->getByOrdersCode($code);
           //可能是物流单
           if (!$order) {
                $ordertemp = Order_Service_OrderShip::getByfield('tracking_number', $code);
                if ($ordertemp) {
                     if ($ordertemp[0]['orders_code']) {
                          $orders_shipment = $ordertemp[0]['orders_code'];
                          $order = Order_Service_Order::getByOrdersCode($orders_shipment);
                     }
                }
           }
      }
      if ($stoporderid) {
           $order_temp = $this->_s->getByfield('order_id', $stoporderid);
           $order = $this->_s->getByOrdersCode($order_temp['orders_code']);
      }
      if (!$order) {
           $data = array(
                   'result' => $code . " 订单不存在",
                   'ask' => 0
           );
           die(Zend_Json::encode($data));
      }
      $wh = User_Service_Warehouse::getWarehouses();
      if ($wh['ask'] == 0) {
           die(json_encode(array('ask' => 0, 'result' => $wh['msg'])));
      }
      if (!in_array($order['warehouse_id'], $wh['ids'])) {
           $wname = $wh['ninfo'][$order['warehouse_id']]['warehouse_name'];
           die(json_encode(array('ask' => 0, 'result' => '你没有仓库【' . $wname . '】的权限')));
      }
      if ($order['underreview_status'] == 3) {
           if ($order['orders_status'] == 0 && $order['process_status'] == 0) {
                $data = array(
                        'result' => '该订单已做截单处理',
                        'ask' => 0
                );
                die(Zend_Json::encode($data));
                exit;
           }
           $row_opm_data = array();
           //根据订单号找配货表
           $row_opm = array();
           $row_opm = Order_Service_OrderPickupMap::getByfield('orders_code', $order['orders_code']);
           //上面这个方法取不到值要重写一下
           if ($row_opm) {
                foreach ($row_opm as $val) {
                     $row_wp['product_id'] = $val['product_id'];
                     $row_wpp = Product_Service_Product::getByProductId($val['product_id']);
                     $row_wp['product_sku'] = $row_wpp['product_sku'];
                     $row_wp['product_title'] = $row_wpp['product_title'];
                     $row_wp['opm_quantity'] = $val['opm_quantity'];
                     $row_wp['wpb_id'] = $val['wpb_id'];
                     $row_wpb = Warehouse_Service_ProductsBatch::getByfield('wpb_id', $row_wp['wpb_id']);
                     $row_wp['ws_code'] = $row_wpb['ws_code'];
                     $row_wp['warehouse_id'] = $row_wpb['warehouse_id'];
                     //查看是否有相关的货架正在盘点
                     $tempCondCheckWP = array(
                             'warehouse_id' => $row_wp['warehouse_id'],
                             'product_id' => $row_wp['product_id'],
                             'ws_code' => $row_wp['ws_code']
                     );
                     $rowResult = Warehouse_Service_Products::getByCondition($tempCondCheckWP);
                     if ($rowResult['wp_status'] == 2) {
                          $data = array(
                                  'result' => '该订单的相关货架' . $row_wp['ws_code'] . '正在进行盘点,无法作截单处理!',
                                  'ask' => 0
                          );
                          die(Zend_Json::encode($data));
                     }
                     $row_opm_data[] = $row_wp;
                }
           }
           $result["orders"] = $order;
           $order['date'] = date('Y-m-d', time());
           $result["product"] = $row_opm_data;
           //生成返架清单
           $orderCodes = array($order['orders_code']);
           $result = Order_Service_BackPutaway::getPrintData($orderCodes);
           if ($result['err_orders']) {
                $data = array(
                        'result' => $result['err_orders'],
                        'ask' => 0
                );
                die(Zend_Json::encode($data));
           }
           //截单
           $stopaction = new Order_Service_OrderProccess($order['orders_code']);
           $result = $stopaction->stop();
           //打印
           $group = array();
           $data = array(
                   "warehouse_id" => $order['warehouse_id'],
                   "orders_code" => $order['orders_code'],
           );
           $back_putaway_item = new Order_Model_BackPutawayItem();
           $bpi_id = $back_putaway_item->getByCond($data);
           if (!empty($bpi_id)) {
                foreach ($bpi_id as $value) {
                     $ws['bp_code'] = $value['bp_code'];
                     $ws['orders_code'] = $value['orders_code'];
                     $ws['warehouse_id'] = $value['warehouse_id'];
                     $ws['ws_code'] = $value['ws_code'];
                     $ws['wpb_id'] = $value['wpb_id'];
                     $ws['opm_quantity'] = $value['bpi_quantity'];
                     $product_id = Product_Service_Product::getByProductId($value['product_id']);
                     $ws['product_sku'] = $product_id['product_sku'];
                     $ws['product_title'] = $product_id['product_title'];
                     $group[] = $ws;
                }
           } else {
                $data = array(
                        'result' => "未获取到此订单 " . $order['orders_code'] . " 返架清单明细信息",
                        'ask' => 0,
                );
                die(Zend_Json::encode($data));
           }
           $arr["product"] = $group;
           $this->view->order = $order;
           $this->view->arr = $arr;
           $html = $this->view->render($this->tplDirectory . "stop-print-detail.tpl");
           if ($result['ask'] == 1) {
                $data = array(
                        'result' => $code . " 订单操作成功",
                        "html" => $html,
                        'ask' => 1
                );
           } else {
                $data = array(
                        'result' => $order['orders_code'] . "|" . $result['msg'],
                        'ask' => 0
                );
           }
           die(Zend_Json::encode($data));
           exit;
      } else {
           $data = array(
                   'result' => $code . " 订单不是待截单状态",
                   'ask' => 0
           );
           die(Zend_Json::encode($data));
      }
      die(Zend_Json::encode($data));
 }

}

//逻辑处理(Service)
class Order_Service_BackPutaway {
/*
* 生成返架清单
* $orderCodes 订单号
*/

 public static function getPrintData($orderCodes) {
      $warehouse = User_Service_Warehouse::getWarehouses();
      $warehouse_products_batch = new Warehouse_Model_ProductsBatch();
      if ($warehouse['ask'] == 0) {
           return array('ask' => 0, 'msg' => $warehouse['msg']);
      }
      $backOrderCodes = array();
      //检查订单是否可以返架
      foreach ($orderCodes as $v) {
           $cr = self::checkOrder($v, $warehouse['ids']);
           if ($cr['ask'] == 0) {
                $errlist[] = $v . ':' . $cr['msg'];
           } else {
                $backOrderCodes[$cr['warehouse_id']][] = $v;
           }
      }
      $getData = array();
      $result = array('data' => '');
      //如果有可返架的订单
      if (count($backOrderCodes) > 0) {
           foreach ($backOrderCodes as $warehouseID => $orders) {
                $category = '';
                $splitArgs = 10;
                $orderArr = array(
                        "orders_code" => $orders,
                );
                $orderby = array(
                        "ws_code asc"
                );
                $createName = '';
                $createrUser = ''; // User_Service_User::getByUserId($opRow['op_creater_id']);
                $createName = ''; //$createrUser['name'];
                $opmModel = new Product_Model_OrderPickupMap();
                $resultTemp = $opmModel->getByOrders($orderArr, $orderby);
                $userName = '';
                $date = date("Y-m-d H:i:s");
                $session = new Zend_Session_Namespace('userAuth');
                $user_id = $session->userID;     //操作人
                $bpModel = new Order_Model_BackPutaway();
                $bpiModel = new Order_Model_BackPutawayItem();
                $adapter = $bpModel->getAdapter();
                $adapter->beginTransaction();
                $getBpCode = self::generateCode();
                try {
                     //创建主表数据
                     $addBpRow = array(
                             'bp_code' => $getBpCode,
                             'bp_create_date' => $date,
                             'bp_create_user_id' => $user_id,
                             'warehouse_id' => $warehouseID,
                     );
                     if (!$bpModel->add($addBpRow)) {
                          throw new Exception('创建返架单失败。');
                     }
                     //创建子表数据
                     foreach ($resultTemp as $k => $v) {
                          $category = Product_Service_Product::getProductCategory($v['product_id']);
                          $resultTemp[$k]['category'] = $category['poc_value_cn'];
                          $productInfo = Product_Service_Product::getByProductId($v['product_id']);
                          $pbRow = Product_Service_ProductBarcode::getProductById($v['product_id']);
                          $resultTemp[$k]['product_id'] = $productInfo['product_id'];
                          $resultTemp[$k]['op_code'] = $v['op_code'];
                          $resultTemp[$k]['customer_id'] = $productInfo['customer_id'];
                          $resultTemp[$k]['product_sku'] = $productInfo['product_sku'];
                          $resultTemp[$k]['product_title'] = $productInfo['product_title_cn'];
                          $resultTemp[$k]['opm_quantity'] = $v['opm_quantity'];
                          $resultTemp[$k]['barcode'] = $pbRow ? $pbRow[0]['pb_code'] : '';
                          if ($userName == '') {
                               $orderRow = Order_Service_Order::getByOrdersCode($v['orders_code']);
                               $pickupUser = User_Service_User::getByUserId($orderRow['pickup_user_id']);
                               $userName = $pickupUser['name'];
                          }
                          //根据配货订单表中的库存批次 ID 查询出产品上架批次表中对应数据
                          $wpb_id = $warehouse_products_batch->lockForUpdate($v['wpb_id']);
                          $ws_code = '';
                          //如果产品上架批次表中产品 ID 等于配货订单表中的产品 ID,并且产品上架批次表中的可用数量加冻结库存大于零,那么截单使用原货位号
                          if ($wpb_id['product_id'] == $v['product_id'] && ($wpb_id['wpb_quantity_hold'] + $wpb_id['wpb_quantity']) > 0) {
                               $ws_code = $v['ws_code'];
                          } else {
                               //如果不相等,那么根据配货订单表中的货位号查询出产品上架批次表中对应数据
                               $wpbInfo = $warehouse_products_batch->getByConditionBatch(array('ws_code' => $v['ws_code'], '*', '', '', '', 0));
                               $false = true;
                               if ($wpbInfo) {
                                    foreach ($wpbInfo as $wpbRow) {
                                         //如果产品上架批次表中的可用数量加冻结库存大于零,并且产品上架批次表中的产品 ID 不等于配货订单表中的产品 ID,那么截单使用推荐货位号
                                         if (($wpbRow['wpb_quantity_hold'] + $wpbRow['wpb_quantity']) > 0 && $wpbRow['product_id'] != $v['product_id']) {
                                              $false = false;
                                              break;
                                         }
                                    }
                                    if ($false) {
                                         foreach ($wpbInfo as $wpbRow) {
                                              //如果为 true 产品上架批次表中的可用数量加冻结库存大于等于零,并且产品上架批次表中的产品 ID 等于配货订单表中的产品 ID,那么截单使用原货位号
                                              if (($wpbRow['wpb_quantity_hold'] + $wpbRow['wpb_quantity']) >= 0 && $wpbRow['product_id'] == $v['product_id']) {
                                                   $ws_code = $wpbRow['ws_code'];
                                                   break;
                                              }
                                         }
                                    }
                               }
                               if ($ws_code == '' && !$false) {
                                    //如果货位号为空,那么根据产品上架批次表中的批次号查询出产品上架批次表中对应数据
                                    $wpbInfo = $warehouse_products_batch->getByConditionBatch(array('putaway_lot_number' => $wpb_id['putaway_lot_number'], '*', 'wpb_quantity', '', '', 0));
                                    if ($wpbInfo) {
                                         foreach ($wpbInfo as $wpbRow) {
                                              //如果产品上架批次表中的可用数量加冻结库存大于零,那么截单使用原货位号
                                              if (($wpbRow['wpb_quantity_hold'] + $wpbRow['wpb_quantity']) > 0) {
                                                   $ws_code = $wpbRow['ws_code'];
                                                   break;
                                              }
                                         }
                                    }
                               }
                          }
                          //如果货位号为空,那么推荐同类型、同仓库 ID 空货位号
                          if ($ws_code == '') {
                               $warehouse_shelf_empty = new Warehouse_Model_ShelfEmpty();
                               $warehouse_shelf = new Warehouse_Model_Shelf();
                               //原货位类型
                               $condition = array(
                                       'ws_code' => $wpb_id['ws_code'],
                                       'warehouse_id' => $wpb_id['warehouse_id'],
                               );
                               $wsc_code = $warehouse_shelf->getByCondition($condition, "*", "", 0, 0);
                               if (!$wsc_code) {
                                    throw new Exception("未找到仓库中的货位号 " . $wpb_id['ws_code'] . " 类型");
                               }
                               //空货位类型
                               $condition = array(
                                       'wse_code_type' => $wsc_code[0]['wsc_code'],
                                       'warehouse_id' => $wsc_code[0]['warehouse_id']
                               );
                               $wse_id = $warehouse_shelf_empty->getByCondition($condition, "*", "", 1, 1);
                               if (!$wse_id) {
                                    throw new Exception("未找到仓库中的 " . $wsc_code[0]['wsc_code'] . " 类型");
                               }
                               $ws_code = $wse_id[0]['ws_code'];
                               //删除仓库中的空货位号被推荐的货位号
                               if (!$warehouse_shelf_empty->delete($wse_id[0]['wse_id'])) {
                                    throw new Exception("删除被推荐的货位号失败");
                               }
                          }
                          if (!empty($ws_code)) {
                               $addBpiRow = array(
                                       'bp_code' => $getBpCode,
                                       'op_code' => $v['op_code'],
                                       'warehouse_id' => $warehouseID,
                                       'orders_code' => $v['orders_code'],
                                       'product_id' => $productInfo['product_id'],
                                       'wpb_id' => $v['wpb_id'],
                                       'ws_code' => $ws_code,
                                       'bpi_quantity' => $v['opm_quantity']
                               );
                               if (!$bpiModel->add($addBpiRow)) {
                                    throw new Exception('添加返架明细数据失败,订单号:' . $v['orders_code']);
                               }
                          } else {
                               throw new Exception("仓库中的空货位里没有此类型货位号 " . $wsc_code[0]['wsc_code']);
                          }
                     }
                     $resultTemp = array_chunk($resultTemp, $splitArgs);
                     $tmp = array();
                     $tmp["product_pages"] = $resultTemp;
                     $tmp["pages"] = count($resultTemp);
                     $tmp['date'] = $date;
                     $tmp['bpcode'] = $getBpCode;
                     $tmp['phname'] = $userName;
                     $tmp['opname'] = $createName;
                     $tmp['warehouseName'] = $warehouse['info'][$warehouseID]['warehouse_name'];
                     $adapter->commit();
                     $result['data'][$warehouseID] = $tmp;
                } catch (Exception $e) {
                     $adapter->rollback();
                     $errlist[] = $e->getMessage();
                }
           }
           if (isset($errlist) && !empty($errlist)) {
                $result['err_orders'] = $errlist;
           } else {
                $result['err_orders'] = array();
           }
           return $result;
      } else {
           if ($errlist) {
                $result['err_orders'] = $errlist;
           }
           return $result;
      }
 }

}

//逻辑处理(Service)
class Order_Service_OrderProccess {

 protected $_order = null;
 protected $_orderCode = null;
 protected $_plan = null;
 protected $_hsSp = false;

 public function __construct($orderCode) {
      if (!empty($orderCode)) {
           $this->_orderCode = $orderCode;
           $this->setOrder();
           $this->setCustomer();
           $this->getShipTypeCode();
      }
      $this->setWarehouse();
      $this->_plan = new Plan_Service_Plan();
 }

 /*
  * 截单
  * 订单删除
  */

 public function stop() {
      $obj = new Order_Model_Order();
      $session = new Zend_Session_Namespace('userAuth');
      $user_id = isset($session->userID) ? $session->userID : 0;
      $dbAdapter = $obj->getAdapter();
      $dbAdapter->beginTransaction();
      try {
           switch ($this->_order['process_status']) {
                //orders_status   状态( 0删除,1,草稿,2已确认,3,已提交 4,On Hold,,5,已发货,6已签收),
                //process_status  状态(0:未提交,1:待下架, 2:下架中, 3:已下架, 4:待发货, 5:已发货,6:完成)
                case 1 :
                     break;
                case 2 :
                     break;
                case 3 :
                     break;
                case 4 :
                     if ($this->_order['problem_status'] == 0) {
                          throw new Exception('订单不是问题单');
                     }
                     break;
                case 5:
                     //只有问题单才可以确认截单
                     if ($this->_order['problem_status'] == 0) {
                          throw new Exception('订单不是问题单');
                     }

                     break;
                default :
                     throw new Exception('状态不正确');
                     break;
           }
           //截单确认
           if ($this->_order['underreview_status'] == 3 && $this->_order['orders_status'] > 0 && $this->_order['process_status'] > 0) {
                if (in_array($this->_order['shipping_method'], Shipping_Service_ShipType::isSendToIndiaShipType())) {
                     $opaObj = new Order_Model_OrderPackageItem();
                     $opaInfo = $opaObj->getRowByfield('orders_code', $this->_order['orders_code']);
                     if ($opaInfo) {
                          if (!$opaObj->updateByOrdersCode(array('is_labelling' => 2), $this->_order['orders_code'])) {
                               throw new Exception('is_labelling状态修改失败!'); //此状态为子库需删除的订单
                          }
                     }
                }
                $date = date("Y-m-d:H:i:s");
                //计算特服单扣除的费用
                $bcondition = array(
                        'warehouse_id' => $this->_order['warehouse_id'],
                        'customer_id' => $this->_order['customer_id'],
                        'orders_code' => $this->_order['orders_code'],
                        'spoStatus' => array('0'),
                        'spo_type' => 3
                );
                $this->backSpecialOrderFee($bcondition);
                //订单退回删除
                $row_order = array(
                        "orders_status" => 0,
                        "process_status" => 0,
                        'problem_status' => 0,
                        "import_status" => Order_Service_Order::isSelfPickup($this->_order['shipping_method']) ? 0 : 1,
                        "last_modified_time" => $date,
                        //"order_stop_date"=>$date,
                        'order_print_area' => 0,
                        "print_time" => '0000-00-00 00:00:00',
                        "pickup_user_id" => 0,
                        "package_user_id" => 0,
                        //"is_pdapickup" => 0
                );
                if ($this->_hsSp) {//有增值服务单时,才修改
                     $row_order['order_type'] = 0;
                     $row_order['package_type'] = 1;
                }
                $this->changeStatus($row_order);
                //更新批次库存
                $this->wareHouseProductBatchBack();
                $returnFee = true;
                Common_Service_Common::logErrorByFile(var_export($this->_order, true), 'jiedan.log');
                if ($this->_order['problem_status'] == 1 && $this->_order['orders_status'] == 3 && $this->_order['process_status'] == 4) {
                     //查询问题件中的process_status状态,如果是5则不要返费,否则需要返费
                     $obj = new Order_Model_ProblemOrder();
                     $condition = array('warehouse_id' => $this->_order['warehouse_id'], 'po_action_type' => 2, 'order_code' => $this->_order['orders_code'], 'po_status' => 4, 'customer_id' => $this->_order['customer_id']);
                     $res = $obj->getByCondition($condition, 'order_process_status', array('po_id desc'), 1, 1);
                     Common_Service_Common::logErrorByFile(var_export($res, true), 'jiedan.log');
                     if ($res && $res[0]['order_process_status'] == 5)
                          $returnFee = false;
                }
                if (!$returnFee) {//已出货截单返架扣除cbm
                     $res = Order_Service_OrderProccess::debitCbm($this->_order);
                     if (!$res['ask'])
                          throw new Exception($res['msg']);
                }
                //清除下架明细
                $this->clearOrderPickupMap();
                //问题单扣包材
                if (in_array($this->_order['process_status'], array(4, 5)) && $this->_order['problem_status'] == 1 && !in_array($this->_order['shipping_method'], Shipping_Service_ShipType::isSendToIndiaShipType())) {
                     $this->deductionPackage(); //扣包材费
                     //删除订单已扫描的包材
                     $pkgs = Order_Service_OrderPackageMap::getByfield('orders_code', $this->_order['orders_code']);
                     if ($pkgs) {
                          foreach ($pkgs as $pkg) {
                               if (!Order_Service_OrderPackageMap::delete($pkg['opmap_id']))
                                    throw new Exception('删除订单包材失败!');
                          }
                     }
                }
                //退回预扣费用
                if ($returnFee) {
                     $this->feeBack();
                }
                //更新总冻结可用库存
                $this->productStocketBack($returnFee);
                //删除订单运输信息
                $this->clearOrderShip();
                $this->createOrderLogNew($this->_order['orders_code'], $this->_order['orders_status'], $this->_order['process_status'], 0, 0, $this->_opCode . '截单确认');
                //统一扣费
                if (!$this->unifiedChargeBack()) {
                     throw new Exception('返回费用失败');
                }
                /*
                 * 截单记录,同步至istore
                 * 如果是STK的产品
                 */
                if (in_array($this->_order['customer_id'], array('STK01'))) {
                     foreach ($this->_order['order_product'] as $orderPro) {
                          $condition = array(
                                  'warehouse_id' => $this->_order['warehouse_id'],
                                  'product_id' => $orderPro['product_id'],
                                  'ref_code' => $orderPro['orders_code'],
                                  'ie_type' => 1,
                                  'user_id' => $user_id,
                                  'ie_note' => json_encode(array('type' => '截单'))
                          );
                          if (!Product_Service_InventoryEvent::checkData($condition)) {
                               throw new Exception('记录截单信息失败');
                          }
                     }
                }
           }
      } catch (Exception $e) {
           $dbAdapter->rollback();
           return array("ask" => 0, "msg" => $e->getMessage()); //var_export($e,true));//
      }
      $dbAdapter->commit();
      return array("ask" => 1, "msg" => "操作成功");
 }

 //不带事的订单删除
 public function orderStop() {
      $obj = new Order_Model_Order();
      try {
           switch ($this->_order['process_status']) {
                //orders_status   状态( 0删除,1,草稿,2已确认,3,已提交 4,On Hold,,5,已发货,6已签收),
                //process_status  状态(0:未提交,1:待下架, 2:下架中, 3:已下架, 4:待发货, 5:已发货,6:完成)
                case 1 :
                     break;
                case 2 :
                     break;
                case 3 :
                     break;
                case 4 :
                     if ($this->_order['problem_status'] == 0) {
                          throw new Exception('不是问题单');
                     }
                     break;
                case 5:
                     //只有问题单才可以确认截单
                     if ($this->_order['problem_status'] == 0) {
                          throw new Exception('订单不是问题单');
                     }
                     break;
                default :
                     throw new Exception('状态不正确');
                     break;
           }
           //截单确认
           if ($this->_order['underreview_status'] == 3 && $this->_order['orders_status'] > 0 && $this->_order['process_status'] > 0) {

                $date = date("Y-m-d:H:i:s");
                //计算特服单扣除的费用
                $bcondition = array(
                        'warehouse_id' => $this->_order['warehouse_id'],
                        'customer_id' => $this->_order['customer_id'],
                        'orders_code' => $this->_order['orders_code'],
                        'spoStatus' => array('0'),
                        'spo_type' => 3
                );
                $this->backSpecialOrderFee($bcondition);
                //订单退回删除
                $row_order = array(
                        "orders_status" => 0,
                        "process_status" => 0,
                        "import_status" => Order_Service_Order::isSelfPickup($this->_order['shipping_method']) ? 0 : 1,
                        "last_modified_time" => $date,
                        'order_print_area' => 0,
                        "print_time" => '0000-00-00 00:00:00',
                        "pickup_user_id" => 0,
                        "problem_status" => 0,
                        //"order_stop_date" =>$date,
                        "package_user_id" => 0,
                );
                if ($this->_hsSp) {//有增值服务单时,才修改
                     $row_order['order_type'] = 0;
                     $row_order['package_type'] = 1;
                }
                //更新批次库存
                $this->wareHouseProductBatchBack();
                $returnFee = true;
                Common_Service_Common::logErrorByFile('1=' . var_export($this->_order, true), 'jiedan.log');
                if ($this->_order['problem_status'] == 1 && $this->_order['orders_status'] == 3 && $this->_order['process_status'] == 4) {
                     //查询问题件中的process_status状态,如果是5则不要返费,否则需要返费
                     $obj = new Order_Model_ProblemOrder();
                     $condition = array('warehouse_id' => $this->_order['warehouse_id'], 'po_action_type' => 2, 'order_code' => $this->_order['orders_code'], 'po_status' => 4, 'customer_id' => $this->_order['customer_id']);
                     $res = $obj->getByCondition($condition, 'order_process_status', array('po_id desc'), 1, 1);
                     Common_Service_Common::logErrorByFile('1=' . var_export($res, true), 'jiedan.log');
                     if ($res && $res[0]['order_process_status'] == 5)
                          $returnFee = false;
                }
                if (!$returnFee) {//已出货截单返架扣除cbm
                     $res = Order_Service_OrderProccess::debitCbm($this->_order);
                     if (!$res['ask'])
                          throw new Exception($res['msg']);
                }
                //清除下架明细
                $this->clearOrderPickupMap();
                //问题单扣包材
                if (in_array($this->_order['process_status'], array(4, 5)) && $this->_order['problem_status'] == 1) {
                     $this->deductionPackage(); //扣包材费
                     //删除订单已扫描的包材
                     $pkgs = Order_Service_OrderPackageMap::getByfield('orders_code', $this->_order['orders_code']);
                     if ($pkgs) {
                          foreach ($pkgs as $pkg) {
                               if (!Order_Service_OrderPackageMap::delete($pkg['opmap_id']))
                                    throw new Exception('删除订单包材失败!');
                          }
                     }
                }
                //退回预扣费用
                if ($returnFee) {
                     $this->feeBack();
                }
                //更新总冻结可用库存
                $this->productStocketBack($returnFee);
                //删除订单运输信息
                $this->clearOrderShip();
                $this->changeStatus($row_order);
                $this->createOrderLogNew($this->_order['orders_code'], $this->_order['orders_status'], $this->_order['process_status'], 0, 0, $this->_opCode . '截单确认');
                //统一扣费
                if (!$this->unifiedChargeBack()) {
                     throw new Exception('返回费用失败');
                }
           }
      } catch (Exception $e) {
           return array("ask" => 0, "msg" => $e->getMessage());
      }
      return array("ask" => 1, "msg" => "操作成功");
 }

 /*
  * 更新冻结批次可用库存
  */

 public function wareHouseProductBatchBack() {
      $session = new Zend_Session_Namespace('userAuth');
      $userName = $session->name;
      //操作人
      $user_id = $session->userID;
      $back_putaway_item = new Order_Model_BackPutawayItem();
      $ordersCodes = $back_putaway_item->getByValueArr($this->_order['orders_code'], "orders_code");
      if (!$ordersCodes) {
           throw new Exception("该订单号" . $this->_order['orders_code'] . "数据不存在");
      }
      foreach ($ordersCodes as $value) {
           $Info = array(
                   'warehouse_id' => $value['warehouse_id'],
                   'ws_code' => $value['ws_code'],
                   'product_id' => $value['product_id'],
           );
           $wpbIds = Warehouse_Service_ProductsBatch::getByCondition($Info);
           $warehouse_products_batch = new Warehouse_Model_ProductsBatch();
           $wpbId = $warehouse_products_batch->lockForUpdate($value['wpb_id']);
           if ($wpbIds) {
                //更新产品上架批次
                $condition = array(
                        'wpb_quantity' => $wpbIds[0]['wpb_quantity'] + $value['bpi_quantity'],
                        'wpb_status' => 1,
                        'receiving_code' => $wpbId['receiving_code'],
                        'putaway_lot_number' => $wpbId['putaway_lot_number'],
                        'rental_time' => $wpbId['rental_time'],
                        'putaway_time' => $wpbId['putaway_time'],
                        'wpb_create_time' => date("Y-m-d H:i:s"),
                        'wpb_last_update' => date("Y-m-d H:i:s"),
                        'qco_code' => $wpbId['qco_code'],
                        'wpb_stock_date' => $wpbId['wpb_stock_date'],
                        'wpb_from' => $wpbId['wpb_from'],
                        'wpb_quantity_hold' => $wpbIds[0]['wpb_quantity_hold'],
                );
                if (!Warehouse_Service_ProductsBatch::update($condition, $wpbIds[0]['wpb_id'])) {
                     throw new Exception("订单 " . $this->_order['orders_code'] . " 更新产品上架批次数量失败");
                }
           } else {
                //写入产品上架批次
                $condition = array(
                        'ws_code' => $value['ws_code'],
                        'warehouse_id' => $value['warehouse_id'],
                        'wpb_status' => 1,
                        'wpb_are_type' => 0,
                        'receiving_code' => $wpbId['receiving_code'],
                        'putaway_lot_number' => $wpbId['putaway_lot_number'],
                        'rental_time' => $wpbId['rental_time'],
                        'putaway_time' => $wpbId['putaway_time'],
                        'insurance_times' => $wpbId['insurance_times'],
                        'product_id' => $value['product_id'],
                        'wpb_quantity' => $value['bpi_quantity'],
                        'wpb_quantity_hold' => 0,
                        'wpb_create_time' => date("Y-m-d H:i:s"),
                        'wpb_last_update' => date("Y-m-d H:i:s"),
                        'qco_code' => $wpbId['qco_code'],
                        'wpb_stock_date' => $wpbId['wpb_stock_date'],
                        'wpb_from' => $wpbId['wpb_from'],
                );
                if (!Warehouse_Service_ProductsBatch::create($condition)) {
                     throw new Exception("订单 " . $this->_order['orders_code'] . " 写入产品上架批次数据失败");
                }
           }
           $wpIds = Warehouse_Service_Products::getByCond($Info, '*', '', 0, 0, 0);
           if ($wpIds) {
                //更新仓库产品信息
                $conditionInfo = array(
                        'wp_quantity' => $wpIds[0]['wp_quantity'] + $value['bpi_quantity'],
                        'wp_status' => 1,
                );
                if (!Warehouse_Service_Products::update($conditionInfo, $wpIds[0]['wp_id'])) {
                     throw new Exception("订单 " . $this->_order['orders_code'] . " 更新仓库产品信息库存失败");
                }
           } else {
                //写入仓库产品信息
                $orderRows = Order_Service_Order::getByOrdersCodes($this->_order['orders_code']);
                $conditionInfo = array(
                        'warehouse_id' => $value['warehouse_id'],
                        'customer_id' => $orderRows[0]['customer_id'],
                        'ws_code' => $value['ws_code'],
                        'product_id' => $value['product_id'],
                        'wp_quantity' => $value['bpi_quantity'],
                        'wp_quantity_hold' => 0,
                        'wp_status' => 1,
                        'wh_create_date' => date("Y-m-d H:i:s"),
                        'wh_last_update' => date("Y-m-d H:i:s"),
                        'wh_primary' => 0,
                );
                if (!Warehouse_Service_Products::add($conditionInfo)) {
                     throw new Exception("订单 " . $this->_order['orders_code'] . " 写入仓库产品信息数据失败");
                }
           }
           //写入日志
           $log = "订单回退,可用库存 +,冻结库存 -,可用数 from " . $condition['wpb_quantity'] . " to " . $wpbId['wpb_quantity'] . ",冻结数 from " . $condition['wpb_quantity_hold'] . " to " . $wpbId['wpb_quantity_hold'];
           $this->createUsage($value['wpb_id'], 2, ($wpbId['wpb_quantity'] + $wpbId['wpb_quantity_hold']), $value['bpi_quantity'], $log);
           //写入货架使用日志
           $conditionLog = array(
                   'warehouse_id' => $value['warehouse_id'],
                   'ws_code' => $value['ws_code'],
                   'wpl_proc_type' => 4,
                   'wpl_refId' => $this->_orderCode,
                   'user_id' => $user_id,
                   'wpl_note' => '订单由下架中变为待下架,回退冻结库存和可用库存,冻结库存由 ' . $wpbId['wpb_quantity_hold'] . ' 变为 ' . $condition['wpb_quantity_hold'] . " 可用库存由 " . $wpbId['wpb_quantity'] . " 变为 " . $condition['wpb_quantity'],
           );
           if (!Warehouse_Service_Products::CreateWarehouseProductsLog($conditionLog)) {
                throw new Exception("订单 " . $this->_orderCode . " 货架 " . $value['ws_code'] . " 写入货架使用日志失败");
           }
           if ($this->_order['process_status'] == 4 || $this->_order['process_status'] == 5) {
                //更新箱子库存
                $containerCondition = array(
                        'product_id' => $value['product_id'],
                        'ws_code' => $value['ws_code'],
                        'warehouse_id' => $this->_order['warehouse_id'],
                );
                $containerInfo = Container_Service_ContainerBinding::getByCondition($containerCondition);
                if (!$containerInfo) {
                     continue;
                }
                $upData = array(
                        'cb_current_quantity' => $containerInfo[0]['cb_current_quantity'] + $value['bpi_quantity'],
                );
                $upResult = Container_Service_ContainerBinding::update($upData, $containerInfo[0]['cb_id']);
                if (!$upResult) {
                     throw new Exception('更新箱子数量失败');
                }
                $cblog = array(
                        'container_code' => $containerInfo[0]['container_code'],
                        'ws_code' => $containerInfo[0]['ws_code'],
                        'product_id' => $containerInfo[0]['product_id'],
                        'log_type' => 2,
                        'note' => '@1102084#:' . $userName . ' | ' . ' 订单回退,可用库存 + ' . $value['bpi_quantity'] . ',' . $this->_order['orders_code'],
                        'cbl_create_time' => date('Y-m-d H:i:s')
                );
                if (!Container_Service_ContainerBindingLog::create($cblog)) {
                     throw new Exception('写箱子日志失败');
                }
           }
      }
 }

}

//模型层(Model)
class Warehouse_Model_ShelfEmpty {
/*
* condition type * || COUNT(*)
* orderbyinteger pageSize integer 分页数
* $page integer 显示数据
*/

 public function getByCondition($condition, $type = '*', $orderby = array('wse_id'), $pageSize = 0, $page = 0) {
      $select = $this->_table->getAdapter()->select();
      $table = $this->_table->info('name');
      $select->from($table, $type);
      $select->where("1 =?", 1);
      if (isset($condition['wse_code_type']) && $condition['wse_code_type'] !== '') {
           $select->where(" wse_code_type = ?", $condition["wse_code_type"]);
      }
      if (isset($condition['warehouse_id']) && $condition['warehouse_id'] !== '') {
           $select->where(" warehouse_id = ?", $condition["warehouse_id"]);
      }
      if ('count(*)' == $type) {
           return $this->_table->getAdapter()->fetchOne($select);
      } else {
           $select->order($orderby);
           if ($pageSize > 0 and $page > 0) {
                $start = ($page - 1) * $pageSize;
                $select->limit($pageSize, $start);
           }
           $sql = $select->__toString();
           return $this->_table->getAdapter()->fetchAll($sql);
      }
 }

}

<!-- Html -->
<{foreach from=$arr.product item=product key=key name=product}>                    
     <tr>
          <td><{$product.ws_code}></td>
          <td><{$product.product_sku}></td>
          <td><{$product.product_title}></td>
          <td><{$product.opm_quantity}></td>
     </tr>
<{/foreach}>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值