|
- <?php
-
- namespace app\admin\service;
-
- use app\admin\command\Util;
- use app\admin\dao\AdminDao;
- use app\admin\dao\CfSuplierInfoDao;
- use app\admin\dao\OrderHotelDao;
- use app\admin\dao\OrderItemDao;
- use app\admin\dao\OrderMainDao;
- use app\admin\dao\PurchaseDao;
- use app\admin\dao\PurchasePriceDao;
-
- /**
- * 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;
- }
- if (!isset($param['subOrderList']) || count($param['subOrderList'])==0) {
- return Util::returnArrSu("成功",$addOrderMain['data']);
- }
- $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) {
- $subOrderParam['create_id'] = $param['create_id'];
- $subOrderParam['group_id'] = $param['group_id'];
- //1.获取负责人昵称
- $adminDao = new AdminDao();
- $adminRe = $adminDao->getInfoById($subOrderParam['purchase_user_id']);
- if (!$adminRe['flag']) {
- return $adminRe;
- }
- $subOrderParam['purchase_user'] = $adminRe['data']['nickname'];
- //2.获取供应商名称
- $suplierDao = new CfSuplierInfoDao();
- $suplierRe = $suplierDao->getInfoById($subOrderParam['supplier_id']);
- if (!$suplierRe['flag']) {
- return $suplierRe;
- }
- $subOrderParam['supplier_name'] = $suplierRe['data']['supplier_name'];
- /**
- * 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();
- //1.获取负责人昵称
- $adminDao = new AdminDao();
- $adminRe = $adminDao->getInfoById($subOrderParam['purchase_user_id']);
- if (!$adminRe['flag']) {
- return $adminRe;
- }
- $subOrderParam['purchase_user'] = $adminRe['data']['nickname'];
- //2.获取供应商名称
- $suplierDao = new CfSuplierInfoDao();
- $suplierRe = $suplierDao->getInfoById($subOrderParam['supplier_id']);
- if (!$suplierRe['flag']) {
- return $suplierRe;
- }
- $subOrderParam['supplier_name'] = $suplierRe['data']['supplier_name'];
- /**
- * 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);
- 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("保存成功",$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);
- }
-
- /**
- * 获取订单列表
- * @param $param
- * @return array
- */
- public function getOrderList($param){
- $orderMainDao = new OrderMainDao();
- $where = [" a.del_flag = 0 ","a.group_id= {$param['group_id']} "];
- if (!empty($param['order_id'])) {
- $where[] =" a.id = {$param['order_id']} ";
- }
- if (!empty($param['channel_id'])) {
- $where[] = " a.channel_id = {$param['channel_id']} ";
- }
- if ($param['order_status'] !== '') {
- $where[] = " a.order_status = {$param['order_status']} ";
- }
- if (!empty($param['commissioner_id'])) {
- $where[] = " a.commissioner_id = {$param['commissioner_id']} ";
- }
- if (!empty($param['user_name'])) {
- $where[] = " a.user_name like '%{$param['user_name']}%' ";
- }
- if (!empty($param['user_phone'])) {
- $where[] = " a.user_phone = '{$param['user_phone']}' ";
- }
- if (!empty($param['create_id'])) {
- $where[] = " a.create_id = {$param['create_id']} ";
- }
- //金额区间查询
- if (!empty($param['startMoney'])) {
- $where[] = " a.total_amount >= {$param['startMoney']} ";
- }
- if (!empty($param['endMoney']) ) {
- $where[] = " a.total_amount <= {$param['endMoney']} ";
- }
-
- //成本区间查询
- if (!empty($param['startCost'])) {
- $where[] = " a.cost_amount >= {$param['startCost']} ";
- }
- if (!empty($param['endCost']) ) {
- $where[] = " a.cost_amount <= {$param['endCost']} ";
- }
- //利润区间查询
- if (!empty($param['startProfit'])) {
- $where[] = " a.profit_amount >= {$param['startProfit']} ";
- }
- if (!empty($param['endProfit']) ) {
- $where[] = " a.profit_amount <= {$param['endProfit']} ";
- }
-
- //时间区间查询
- if (!empty($param['startTime'])) {
- $where[] = " a.create_time >= '{$param['startTime']} 00:00:00' ";
- }
- if (!empty($param['endTime']) ) {
- $where[] = " a.create_time <= '{$param['endTime']} 23:59:59' ";
- }
-
- if ($param['receipt_order_status'] !== '') {
- $where[] = " a.receipt_order_status = {$param['receipt_order_status']} ";
- }
- if ($param['receipt_order_id'] !== '') {
- $where[] = " a.receipt_order_id = {$param['receipt_order_id']} ";
- }
- if ($param['channel_order_no'] != '') {
- $where[] = " a.channel_order_no = '{$param['channel_order_no']}' ";
- }
- //子订单查询条件 入住时间
- if (!empty($param['startInDate']) && empty($param['endInDate'])) {
- $where[] = " (b.check_in_date >= '{$param['startInDate']} 00:00:00' or c.check_in_date >= '{$param['startInDate']} 00:00:00' )";
- }
- if (!empty($param['endInDate']) && empty($param['startInDate']) ) {
- $where[] = " (b.check_in_date <= '{$param['endInDate']} 23:59:59' or c.check_in_date <= '{$param['endInDate']} 23:59:59' ) ";
- }
- if (!empty($param['startInDate']) && !empty($param['endInDate']) ) {
- $where[] = " (
- (b.check_in_date >= '{$param['startInDate']} 00:00:00' and b.check_in_date <= '{$param['endInDate']} 23:59:59')
- or
- (c.check_in_date >= '{$param['startInDate']} 00:00:00' and c.check_in_date <= '{$param['endInDate']} 23:59:59')
- ) ";
- }
- //供应商
- if (!empty($param['supplier_id'])) {
- $where[] = "(b.supplier_id = {$param['supplier_id']} or c.supplier_id = {$param['supplier_id']})";
- }
- if (!empty($param['customer_name'])) {
- $where[] = "(b.customer_name like '%{$param['customer_name']}%' or c.customer_name like '%{$param['customer_name']}%')";
- }
- //子订单查询条件 离店时间
- if (!empty($param['startOutDate'])) {
- $where[] = " b.check_out_date >= '{$param['startOutDate']} 00:00:00' ";
- }
- if (!empty($param['endOutDate']) ) {
- $where[] = " b.check_out_date <= '{$param['endOutDate']} 23:59:59' ";
- }
-
-
- $result = $orderMainDao->getOrderListByWhereStr(join(" and ",$where),$param);
- if (!$result['flag'] || $result['data']['total']==0) {
- return $result;
- }
- //获取主订单ID
- $ids = [];
- foreach ($result['data']['list'] as $info){
- $ids[] = $info['id'];
- }
- //获取子订单列表
- $orderHotelDao = new OrderHotelDao();
- $orderItemDao = new OrderItemDao();
- $orderHotelList = $orderHotelDao->getOrderListByOrderIds($ids);
- $orderItemList = $orderItemDao->getOrderListByOrderIds($ids);
- //设置主订单列表
- $orderMainList = [];
- foreach ($result['data']['list'] as $order) {
- $order['subOrder']=[];
- foreach ($orderHotelList as $hotel){
- if ($hotel['order_id'] == $order['id']) {
- $hotel["type"]="酒店";
- $order['subOrder'][] = $hotel;
- }
- }
- foreach ($orderItemList as $item){
- if ($item['order_id'] == $order['id']) {
- $item["type"]="附加项目";
- $order['subOrder'][] = $item;
- }
- }
- $orderMainList[] = $order;
- }
- $result['data']['list'] = $orderMainList;
- return $result;
- }
- }
|