|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
-
- namespace app\admin\service;
-
- use app\admin\command\Util;
- use app\admin\model\OrderHotel;
- use app\admin\model\OrderItem;
- use app\admin\model\OrderMain;
- use app\admin\model\Purchase;
- use app\admin\model\PurchasePrice;
- use think\Exception;
- use think\Url;
-
- /**
- * Created by PhpStorm.
- * User: nizongfeng
- * Date: 2021/10/24
- * Time: 15:55
- */
- class OrderMainService
- {
-
- /**
- * 保存主订单
- * @param $param
- * @return array
- */
- public function saveOrder($param)
- {
- /**
- * 1.添加主订单
- */
- $orderMainDao = new OrderMainDao();
- $addOrderMain = $orderMainDao->save($param);
- if (!$addOrderMain["flag"]) {
- return $addOrderMain;
- }
- $orderId = $addOrderMain['data'];
- //所有子订单设置为删除
- $orderHotelDao = new OrderHotelDao();
- $orderHotelDao->delete($orderId);
- $orderItemDao = new OrderItemDao();
- $orderItemDao->delete($orderId);
- $purchasePriceDao = new PurchasePriceDao();
- $purchasePriceDao->delete($orderId);
- //循环子订单
- foreach ($param['subOrderList'] as $subOrderParam) {
- /**
- * 2.添加子订单 有则激活更新、无则添加
- */
- if ($subOrderParam['prod_type'] == 'hotel') {
- $subOrderDao = $orderHotelDao;
- } else {
- $subOrderDao = $orderItemDao;
- }
- $addSubOrder = $subOrderDao->modify($subOrderParam, $orderId);
- if (!$addSubOrder['flag']) {
- return $addSubOrder;
- }
- $subOrderId = $addSubOrder['data'];
- $subOrderInfo = $subOrderDao->getInfoById($subOrderId);
- if (!$subOrderInfo['flag']) {
- return $subOrderInfo;
- }
- /**
- * 2.1添加子订单下的采购单 有则更新激活、无则添加
- */
- $purchaseDao = new PurchaseDao();
- if ($subOrderParam['prod_type'] == 'hotel') {
- $addPurchase = $purchaseDao->saveHotelPurchase($subOrderParam, $subOrderInfo['data']);
- } else {
- $addPurchase = $purchaseDao->saveItemPurchase($subOrderParam, $subOrderInfo['data']);
- }
- if (!$addPurchase['flag']) {
- return $addPurchase;
- }
- $purchaseId = $addPurchase['data'];
-
- /**
- * 2.1.1添加采购单的每日价格 先删除、后添加激活
- */
- $purchasePriceDao = new PurchasePriceDao();
- $addPurchasePrice = $purchasePriceDao->saveList($subOrderParam['purchasePriceList'], $orderId, $subOrderParam['prod_type'], $subOrderId, $purchaseId);
- if (!$addPurchasePrice['flag']) {
- return $addPurchasePrice;
- }
-
- /**
- * 2.1.2 计算更新 采购单总金额、成本、产品数量
- */
- $setPurchaseRe = $purchaseDao->setPurchaseAmount($purchaseId);
- if (!$setPurchaseRe['flag']) {
- return $setPurchaseRe;
- }
-
- /**
- * 2.2 计算更新 子订单成本、金额、产品数量
- */
- $setSubOrderRe = $subOrderDao->setSubOrderAmount($subOrderId);
- if (!$setSubOrderRe['flag']) {
- return $setSubOrderRe;
- }
- }
- /**
- * 3 计算更新 主订单成本、金额、产品数量
- */
- $setOrderMainRe = $orderMainDao->setOrderAmount($orderId);
- if (!$setOrderMainRe['flag']) {
- return $setOrderMainRe;
- }
- return Util::returnArrSu("",$orderId);
- }
-
- /**
- * 保存子订单
- * @param $subOrderParam
- * @return array
- */
- public function subOrderSave($subOrderParam)
- {
- $orderId = $subOrderParam['order_id'];
- $orderMainDao = new OrderMainDao();
- /**
- * 2.添加子订单
- */
- if ($subOrderParam['prod_type'] == 'hotel') {
- $subOrderDao = new OrderHotelDao();
- } else {
- $subOrderDao = new OrderItemDao();
- }
- $addSubOrder = $subOrderDao->modify($subOrderParam, $orderId);
- if (!$addSubOrder['flag']) {
- return $addSubOrder;
- }
- $subOrderId = $addSubOrder['data'];
- $subOrderInfo = $subOrderDao->getInfoById($subOrderId);
-
- /**
- * 2.1添加子订单下的采购单
- */
- $purchaseDao = new PurchaseDao();
- if ($subOrderParam['prod_type'] == 'hotel') {
- $addPurchase = $purchaseDao->saveHotelPurchase($subOrderParam, $subOrderInfo);
- } else {
- $addPurchase = $purchaseDao->saveItemPurchase($subOrderParam, $subOrderInfo);
- }
- if (!$addPurchase['flag']) {
- return $addPurchase;
- }
- $purchaseId = $addPurchase['id'];
-
- /**
- * 2.1.1添加采购单的每日价格
- */
- $purchasePriceDao = new PurchasePriceDao();
- $addPurchasePrice = $purchasePriceDao->saveList($subOrderParam['purchasePriceList'], $orderId, $subOrderParam['prod_type'], $subOrderId, $purchaseId);
- if (!$addPurchasePrice['flag']) {
- return $addPurchasePrice;
- }
-
- /**
- * 2.1.2 计算更新 采购单总金额、成本、产品数量
- */
- $setPurchaseRe = $purchaseDao->setPurchaseAmount($purchaseId);
- if (!$setPurchaseRe['flag']) {
- return $setPurchaseRe;
- }
-
- /**
- * 2.2 计算更新 子订单成本、金额、产品数量
- */
- $setSubOrderRe = $subOrderDao->setSubOrderAmount($subOrderId);
- if (!$setSubOrderRe['flag']) {
- return $setSubOrderRe;
- }
-
- /**
- * 3 计算更新 主订单成本、金额、产品数量
- */
- $setOrderMainRe = $orderMainDao->setOrderAmount($orderId);
- if (!$setOrderMainRe['flag']) {
- return $setOrderMainRe;
- }
- return Util::returnArrSu("",$subOrderId);
- }
-
- /**
- * 删除子订单
- * @param $param
- * @return array
- */
- public function delSubOrder($param){
- try {
- if ($param['prod_type'] == 'hotel') {
- $subOrderDao = new OrderHotelDao();
- } else {
- $subOrderDao = new OrderItemDao();
- }
- $subOrderRe = $subOrderDao->getInfoById($param['id']);
- if (!$subOrderRe['flag']) {
- return $subOrderRe;
- }
- $subOrderDao->delById($param['id']);
- //删除采购单
- $purchaseDao = new PurchaseDao();
- $purchaseDao->deleteBySubOrderId($param['id']);
- //删除每日采购单价格
- $purchasePriceDao = new PurchasePriceDao();
- $purchasePriceDao->deleteBySubOrderId($param['id']);
- //重新计算订单总金额
- $orderMainDao = new OrderMainDao();
- $orderMainDao->setOrderAmount($subOrderRe['data']['order_id']);
- return Util::returnArrSu();
- }catch (Exception $e){
- return Util::returnArrEr("删除子订单失败:".$e->getMessage());
- }
- }
-
- /**
- * 获取订单详情
- * @param $id
- * @return array
- */
- public function getOrderInfo($id){
- $orderMainDao = new OrderMainDao();
- $orderMainRe = $orderMainDao->getInfoById($id);
- if (!$orderMainRe['flag']) {
- return $orderMainRe;
- }
- $orderMain = $orderMainRe['data'];
- //获取采购单金额列表
- $purchasePriceDao = new PurchasePriceDao();
- $purchasePriceRe = $purchasePriceDao->getPurchasePriceListByOrderId($id);
- if (!$purchasePriceRe['flag']) {
- return $purchasePriceRe;
- }
- //获取采购单列表
- $purchaseDao = new PurchaseDao();
- $purchaseRe = $purchaseDao->getListByOrderId($id);
- if (!$purchaseRe['flag']) {
- return $purchaseRe;
- }
- //设置采购单展示数据
- $purchaseShow = $purchaseDao->setPurchaseShow($purchaseRe['data'],$purchasePriceRe['data']);
- if (!$purchaseShow['flag']) {
- return $purchaseShow;
- }
- //获取子订单列表
- $orderHotelDao = new OrderHotelDao();
- $orderHotelRe = $orderHotelDao->getListByOrderId($id);
- if (!$orderHotelRe['flag']) {
- return $orderHotelRe;
- }
- $orderItemDao = new OrderItemDao();
- $orderItemRe = $orderItemDao->getListByOrderId($id);
- if (!$orderItemRe['flag']) {
- return $orderItemRe;
- }
- //设置子订单列表
- $subOrderList = $orderMainDao->setSubOrderShow($purchaseShow['data'],$orderHotelRe['data'],$orderItemRe['data']);
- if (!$subOrderList['flag']) {
- return $subOrderList;
- }
- $orderMain["subOrderList"]=$subOrderList['data'];
- return Util::returnArrSu("",$orderMain);
- }
- }
|