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

/*
* 控制器 (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));
                               if ($wpbInfo) {
                                    $false = true;
                                    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;
                                              }
                                         }
                                    }
                               } else {
                                    if ($ws_code == '') {
                                         //如果货位号为空,那么根据产品上架批次表中的批次号查询出产品上架批次表中对应数据
                                         $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;
                                                   }
                                              }
                                         }
                                    }
                               }
                          }
                          var_dump($false);
                          //如果货位号为空,那么推荐同类型、同仓库 ID 空货位号
                          if ($ws_code == '') {
                               $pageSize = 1;
                               $page = 1;
                               do {
                                    //空货位类型
                                    $warehouse_shelf_empty = new Warehouse_Model_ShelfEmpty();
                                    $wse_id = $warehouse_shelf_empty->getByWarehouseShelfEmpty($pageSize, $page);
                                    if (!empty($wse_id)) {
                                         $ar = array(
                                                 'ws_code' => $wse_id['ws_code'],
                                                 'warehouse_id' => $wse_id['warehouse_id']
                                         );
                                         $warehouse_shelf = new Warehouse_Model_Shelf();
                                         $wsCode = $warehouse_shelf->getByCondition($ar);
                                         if (!empty($wsCode)) {
                                              //原货位类型
                                              $arr = array(
                                                      'ws_code' => $wpb_id['ws_code'],
                                                      'warehouse_id' => $wpb_id['warehouse_id'],
                                              );
                                              $wsc_code = $warehouse_shelf->getByCondition($arr);
                                              //如果空货位类型等于原货位类型,并且空货位仓库 ID 等于原货位仓库 ID,那么就使用该推荐的货位号
                                              if ($wsCode[0]['wsc_code'] == $wsc_code[0]['wsc_code'] && $wsCode[0]['warehouse_id'] == $wsc_code[0]['warehouse_id']) {
                                                   $ws_code = $wse_id['ws_code'];
                                                   //删除仓库中的空货位号被推荐的货位号
                                                   if (!$warehouse_shelf_empty->delete($wse_id['wse_id'])) {
                                                        throw new Exception("删除仓库中的空货位号被推荐的货位号失败");
                                                   }
                                                   break;
                                              }
                                         }
                                    }
                                    $page++;
                               } while ($wse_id);
                          }
                          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']
                               );
                               print_r($addBpiRow);
                               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;
      }
 }

}

class Order_Service_OrderProccess {

 protected $_order = null;
 //是否有增值服务
 protected $_hsSp = false;
 protected $_opCode = '';
 protected $_orderCode = null;

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

 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) {
                $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_print_area' => 0,
                        "print_time" => '0000-00-00 00:00:00',
                        "pickup_user_id" => 0,
                        "package_user_id" => 0,
                );
                //有增值服务单时,才修改
                if ($this->_hsSp) {
                     $row_order['order_type'] = 0;
                     $row_order['package_type'] = 1;
                }
                $this->changeStatus($row_order);
                //更新批次库存
                if ($this->_order['process_status'] < 4 && in_array($this->_order['import_status'], array(6, 8))) {
                     $this->wareHouseProductBatchBack(); //2013-6-19
                } else {
                     if ($this->_order['process_status'] == 4 || $this->_order['process_status'] == 5) {
                          $result = $this->backStocketBaseOrder();
                          if ($result['ask'] == 0) {
                               throw new Exception($result['msg']);
                          }
                     }
                }
                $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;
                }
                //已出货截单返架扣除cbm
                if (!$returnFee) {
                     $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->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 wareHouseProductBatchBack() {
      $session = new Zend_Session_Namespace('userAuth');
      $user_id = $session->userID;     //操作人
      //出货单
      $model_orderPickupMap = new Order_Model_OrderPickupMap();
      $orderPickupMaps = $model_orderPickupMap->getByfield("orders_code", $this->_order['orders_code']);
      $wpbModel = new Warehouse_Model_ProductsBatch();
      foreach ($orderPickupMaps as $opm) {
           $data = array(
                   "product_id" => $opm['product_id'],
                   "orders_code" => $opm['orders_code'],
           );
           $back_putaway_item = new Order_Model_BackPutawayItem();
           $orders_code = $back_putaway_item->getByCond($data);
           if (empty($orders_code)) {
                throw new Exception("该订单号" . $this->_order['orders_code'] . "数据不存在");
           }
           $warehouse_productBatch = $wpbModel->lockForUpdate($opm['wpb_id']);
           if ($orders_code[0]['ws_code'] == $opm['ws_code']) {
                $warehouse_productBatch_row = array(
                        'wpb_quantity_hold' => $warehouse_productBatch['wpb_quantity_hold'] - $opm['opm_quantity'],
                        'wpb_quantity' => $warehouse_productBatch['wpb_quantity'] + $opm['opm_quantity'],
                        'wpb_status' => 1
                );
                //更新批次库存
                if (!Warehouse_Service_ProductsBatch :: update($warehouse_productBatch_row, $warehouse_productBatch['wpb_id'])) {
                     throw new Exception($this->_order['orders_code'] . "更新批次库存(wpb_id:" . $opm['wpb_id'] . ")失败");
                }
                $log = "订单回退,冻结库存-,可用库存+,可用数from " . $warehouse_productBatch['wpb_quantity'] . "to " . $warehouse_productBatch_row['wpb_quantity'] .
                        ",冻结数from " . $warehouse_productBatch['wpb_quantity_hold'] . "to " . $warehouse_productBatch_row['wpb_quantity_hold'];
                $this->createUsage($opm['wpb_id'], 2, ($warehouse_productBatch['wpb_quantity'] + $warehouse_productBatch['wpb_quantity_hold']), $opm['opm_quantity'], $log);
                $condition = array(
                        "product_id" => $warehouse_productBatch['product_id'],
                        "ws_code" => $warehouse_productBatch['ws_code'],
                        "warehouse_id" => $this->_order['warehouse_id'],
                );
                $warehouse_product = Warehouse_Service_Products :: getByCondition($condition);
                if (!$warehouse_product) {
                     throw new Exception($this->_order['orders_code'] . "货架产品(ws_code:" . $warehouse_productBatch['ws_code'] . " product_id:" . $warehouse_productBatch['product_id'] . ") 不存在 in warehouse_id", $this->_order['warehouse_id']);
                }
                $wpObj = new Warehouse_Model_Products();
                $warehouse_product = $wpObj->lockForUpdateId($warehouse_product['wp_id']);
                //更新货架产品 数量
                $warehouse_product_row = array(
                        'wp_quantity_hold' => $warehouse_product['wp_quantity_hold'] - $opm['opm_quantity'],
                        'wp_quantity' => $warehouse_product['wp_quantity'] + $opm['opm_quantity'],
                        'wp_status' => 1,
                );
                if (!Warehouse_Service_Products::update($warehouse_product_row, $warehouse_product['wp_id'])) {
                     throw new Exception($this->_order['orders_code'] . "更新货架产品(ws_code:" . $warehouse_productBatch['ws_code'] . " wp_id:" . $warehouse_product['wp_id'] . ") 数量失败");
                }
                $container = array(
                        'quantity' => $opm['opm_quantity'],
                        'product_id' => $warehouse_productBatch['product_id'],
                        'ws_code' => $warehouse_productBatch['ws_code'],
                        'msg' => '订单回退,冻结库存-' . $opm['opm_quantity'] . ',可用库存+' . $opm['opm_quantity'] . $this->_order['orders_code'],
                        'type' => 2,
                        'warehouse_id' => $this->_order['warehouse_id'],
                );
                $containerInfo = Order_Service_OrderProccess::updateContainerBindingHoldQuantity($container);
                if ($containerInfo['ask'] == 0) {
                     throw new Exception($containerInfo['msg']);
                }
                $wp_quantity_hold = $warehouse_product['wp_quantity_hold'] - $opm['opm_quantity'];
                $wp_quantity = $warehouse_product['wp_quantity'] + $opm['opm_quantity'];
                //货架使用日志
                $WarehouseProductsLog = array(
                        'wpl_refId' => $this->_orderCode,
                        'user_id' => $user_id,
                        'warehouse_id' => $warehouse_product['warehouse_id'],
                        'ws_code' => $warehouse_product['ws_code'],
                        'wpl_proc_type' => 4, //状态修改
                        'wpl_note' => '订单由下架中变为待下架,回退冻结库存和可用库存,冻结库存由' . $warehouse_product['wp_quantity_hold'] . '变为' . $wp_quantity_hold . "可用库存由" . $warehouse_product['wp_quantity'] . "变为" . $wp_quantity,
                );
                if (!Warehouse_Service_Products::CreateWarehouseProductsLog($WarehouseProductsLog)) {
                     throw new Exception("订单:" . $this->_orderCode . " 货架 " . $warehouse_product['ws_code'] . " 写入使用日志失败失败");
                }
           } else {
                $data = array(
                        'warehouse_id' => $orders_code[0]['warehouse_id'],
                        'ws_code' => $orders_code[0]['ws_code'],
                        'product_id' => $orders_code[0]['product_id'],
                );
                $wpb_id = Warehouse_Service_ProductsBatch :: getByCondition($data);
                if (!empty($wpb_id)) {
                     //更新产品上架批次
                     $condition = array(
                             'wpb_quantity' => $wpb_id[0]['wpb_quantity'] + $orders_code[0]['bpi_quantity'],
                     );
                     if (!Warehouse_Service_ProductsBatch::update($condition, $wpb_id[0]['wpb_id'])) {
                          throw new Exception("订单 " . $this->_order['orders_code'] . " 更新产品上架批次数量失败");
                     }
                } else {
                     //写入产品上架批次
                     $condition = array(
                             'ws_code' => $orders_code[0]['ws_code'],
                             'warehouse_id' => $orders_code[0]['warehouse_id'],
                             'wpb_status' => 1,
                             'wpb_are_type' => 0,
                             'receiving_code' => $warehouse_productBatch['receiving_code'],
                             'putaway_lot_number' => $warehouse_productBatch['putaway_lot_number'],
                             'rental_time' => $warehouse_productBatch['rental_time'],
                             'putaway_time' => $warehouse_productBatch['putaway_time'],
                             'insurance_times' => $warehouse_productBatch['insurance_times'],
                             'product_id' => $orders_code[0]['product_id'],
                             'wpb_quantity' => $orders_code[0]['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' => $warehouse_productBatch['qco_code'],
                             'wpb_stock_date' => $warehouse_productBatch['wpb_stock_date'],
                             'wpb_from' => $warehouse_productBatch['wpb_from'],
                     );
                     if (!Warehouse_Service_ProductsBatch :: create($condition)) {
                          throw new Exception("订单 " . $this->_order['orders_code'] . " 写入产品上架批次数据失败");
                     }
                }
                $wp_id = Warehouse_Service_Products::getByCond($data);
                if (!empty($wp_id)) {
                     //更新仓库产品信息
                     $conditionInfo = array(
                             'wp_quantity' => $wp_id[0]['wp_quantity'] + $orders_code[0]['bpi_quantity'],
                     );
                     if (!Warehouse_Service_Products::update($conditionInfo, $wp_id[0]['wp_id'])) {
                          throw new Exception("订单 " . $this->_order['orders_code'] . " 更新仓库产品信息库存失败");
                     }
                } else {
                     //写入仓库产品信息
                     $orderRows = Order_Service_Order::getByOrdersCodes($this->_order['orders_code']);
                     $conditionInfo = array(
                             'warehouse_id' => $orders_code[0]['warehouse_id'],
                             'customer_id' => $orderRows[0]['customer_id'],
                             'ws_code' => $orders_code[0]['ws_code'],
                             'product_id' => $orders_code[0]['product_id'],
                             'wp_quantity' => $orders_code[0]['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 = "订单回退,冻结库存,可用库存,可用数由 " . 0 . " 变为 " . $condition['wpb_quantity'] . ",冻结数由 " . 0 . " 变为 " . $condition['wpb_quantity_hold'];
                $this->createUsage($opm['wpb_id'], 2, ($orders_code[0]['bpi_quantity'] + $condition['wpb_quantity_hold']), $opm['opm_quantity'], $log);
                //写入货架使用日志
                $conditionLog = array(
                        'warehouse_id' => $orders_code[0]['warehouse_id'],
                        'ws_code' => $orders_code[0]['ws_code'],
                        'wpl_proc_type' => 4,
                        'wpl_refId' => $this->_orderCode,
                        'user_id' => $user_id,
                        'wpl_note' => '订单由下架中变为待下架,回退冻结库存和可用库存,冻结库存由 ' . 0 . ' 变为 ' . $conditionInfo['wp_quantity_hold'] . " 可用库存由 " . 0 . " 变为 " . $orders_code[0]['bpi_quantity'],
                        'wpl_quantity' => 0,
                );
                if (!Warehouse_Service_Products::CreateWarehouseProductsLog($conditionLog)) {
                     throw new Exception("订单 " . $this->_orderCode . " 货架 " . $orders_code[0]['ws_code'] . " 写入货架使用日志失败");
                }
           }
      }
 }

}

/*
* 模型层(Model)
*/
class Warehouse_Model_ShelfEmpty {

 public function delete($id) {
      $where = $this->_table->getAdapter()->quoteInto("wse_id= ?", $id);
      return $this->_table->delete($where);
 }

 /*
  * $pageSize        integer        分页数
  * $page              integer        显示数据
  */

 public function getByWarehouseShelfEmpty($pageSize, $page) {
      $select = $this->_table->getAdapter()->select();
      $table = $this->_table->info('name');
      $select->from($table, '*');
      if (isset($condition["pageSize"]) && $condition["pageSize"] != '' && isset($condition["page"]) && $condition["page"] != '') {
           $start = ($condition["page"] - 1) * $condition["pageSize"];
           $select->limit($condition["pageSize"], $start);
      }
      if ($pageSize > 0 and $page > 0) {
           $start = ($page - 1) * $pageSize;
           $select->limit($pageSize, $start);
      }
      $sql = $select->__toString();
      return $this->_table->getAdapter()->fetchRow($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}>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值