酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

468 lines
17 KiB

  1. <?php
  2. namespace app\admin\service;
  3. use app\admin\command\Util;
  4. use app\admin\dao\AdminDao;
  5. use app\admin\dao\CfSuplierInfoDao;
  6. use app\admin\dao\OrderHotelDao;
  7. use app\admin\dao\OrderItemDao;
  8. use app\admin\dao\OrderMainDao;
  9. use app\admin\dao\PurchaseDao;
  10. use app\admin\dao\PurchasePriceDao;
  11. use app\admin\model\OrderHotel;
  12. use app\admin\model\OrderItem;
  13. use app\admin\model\OrderMain;
  14. use think\Exception;
  15. /**
  16. * Created by PhpStorm.
  17. * User: nizongfeng
  18. * Date: 2021/10/24
  19. * Time: 15:55
  20. */
  21. class OrderMainService
  22. {
  23. /**
  24. * 保存主订单
  25. * @param $param
  26. * @return array
  27. */
  28. public function saveOrder($param)
  29. {
  30. /**
  31. * 1.添加主订单
  32. */
  33. $orderMainDao = new OrderMainDao();
  34. $addOrderMain = $orderMainDao->save($param);
  35. if (!$addOrderMain["flag"]) {
  36. return $addOrderMain;
  37. }
  38. if (!isset($param['subOrderList']) || count($param['subOrderList'])==0) {
  39. return Util::returnArrSu("成功",$addOrderMain['data']);
  40. }
  41. $orderId = $addOrderMain['data'];
  42. //所有子订单设置为删除
  43. $orderHotelDao = new OrderHotelDao();
  44. $orderHotelDao->delete($orderId);
  45. $orderItemDao = new OrderItemDao();
  46. $orderItemDao->delete($orderId);
  47. $purchasePriceDao = new PurchasePriceDao();
  48. $purchasePriceDao->deleteByOrderId($orderId);
  49. //循环子订单
  50. foreach ($param['subOrderList'] as $subOrderParam) {
  51. $subOrderParam['create_id'] = $param['create_id'];
  52. $subOrderParam['group_id'] = $param['group_id'];
  53. //1.获取负责人昵称
  54. $adminDao = new AdminDao();
  55. $adminRe = $adminDao->getInfoById($subOrderParam['purchase_user_id']);
  56. if (!$adminRe['flag']) {
  57. return $adminRe;
  58. }
  59. $subOrderParam['purchase_user'] = $adminRe['data']['nickname'];
  60. //2.获取供应商名称
  61. $suplierDao = new CfSuplierInfoDao();
  62. $suplierRe = $suplierDao->getInfoById($subOrderParam['supplier_id']);
  63. if (!$suplierRe['flag']) {
  64. return $suplierRe;
  65. }
  66. $subOrderParam['supplier_name'] = $suplierRe['data']['supplier_name'];
  67. /**
  68. * 2.添加子订单 有则激活更新、无则添加
  69. */
  70. if ($subOrderParam['prod_type'] == 'hotel') {
  71. $subOrderDao = $orderHotelDao;
  72. } else {
  73. $subOrderDao = $orderItemDao;
  74. }
  75. $addSubOrder = $subOrderDao->modify($subOrderParam, $orderId);
  76. if (!$addSubOrder['flag']) {
  77. return $addSubOrder;
  78. }
  79. $subOrderId = $addSubOrder['data'];
  80. $subOrderInfo = $subOrderDao->getInfoById($subOrderId);
  81. if (!$subOrderInfo['flag']) {
  82. return $subOrderInfo;
  83. }
  84. /**
  85. * 2.1添加子订单下的采购单 有则更新激活、无则添加
  86. */
  87. $purchaseDao = new PurchaseDao();
  88. if ($subOrderParam['prod_type'] == 'hotel') {
  89. $addPurchase = $purchaseDao->saveHotelPurchase($subOrderParam, $subOrderInfo['data']);
  90. } else {
  91. $addPurchase = $purchaseDao->saveItemPurchase($subOrderParam, $subOrderInfo['data']);
  92. }
  93. if (!$addPurchase['flag']) {
  94. return $addPurchase;
  95. }
  96. $purchaseId = $addPurchase['data'];
  97. /**
  98. * 2.1.1添加采购单的每日价格 先删除、后添加激活
  99. */
  100. $purchasePriceDao = new PurchasePriceDao();
  101. $addPurchasePrice = $purchasePriceDao->saveList($subOrderParam['purchasePriceList'], $orderId, $subOrderParam['prod_type'], $subOrderId, $purchaseId);
  102. if (!$addPurchasePrice['flag']) {
  103. return $addPurchasePrice;
  104. }
  105. /**
  106. * 2.1.2 计算更新 采购单总金额、成本、产品数量
  107. */
  108. $setPurchaseRe = $purchaseDao->setPurchaseAmount($purchaseId);
  109. if (!$setPurchaseRe['flag']) {
  110. return $setPurchaseRe;
  111. }
  112. /**
  113. * 2.2 计算更新 子订单成本、金额、产品数量
  114. */
  115. $setSubOrderRe = $subOrderDao->setSubOrderAmount($subOrderId);
  116. if (!$setSubOrderRe['flag']) {
  117. return $setSubOrderRe;
  118. }
  119. }
  120. /**
  121. * 3 计算更新 主订单成本、金额、产品数量
  122. */
  123. $setOrderMainRe = $orderMainDao->setOrderAmount($orderId);
  124. if (!$setOrderMainRe['flag']) {
  125. return $setOrderMainRe;
  126. }
  127. return Util::returnArrSu("成功",$orderId);
  128. }
  129. /**
  130. * 保存子订单
  131. * @param $subOrderParam
  132. * @return array
  133. */
  134. public function subOrderSave($subOrderParam)
  135. {
  136. $orderId = $subOrderParam['order_id'];
  137. $orderMainDao = new OrderMainDao();
  138. //1.获取负责人昵称
  139. $adminDao = new AdminDao();
  140. $adminRe = $adminDao->getInfoById($subOrderParam['purchase_user_id']);
  141. if (!$adminRe['flag']) {
  142. return $adminRe;
  143. }
  144. $subOrderParam['purchase_user'] = $adminRe['data']['nickname'];
  145. //2.获取供应商名称
  146. $suplierDao = new CfSuplierInfoDao();
  147. $suplierRe = $suplierDao->getInfoById($subOrderParam['supplier_id']);
  148. if (!$suplierRe['flag']) {
  149. return $suplierRe;
  150. }
  151. $subOrderParam['supplier_name'] = $suplierRe['data']['supplier_name'];
  152. /**
  153. * 2.添加子订单
  154. */
  155. if ($subOrderParam['prod_type'] == 'hotel') {
  156. $subOrderDao = new OrderHotelDao();
  157. } else {
  158. $subOrderDao = new OrderItemDao();
  159. }
  160. $addSubOrder = $subOrderDao->modify($subOrderParam, $orderId);
  161. if (!$addSubOrder['flag']) {
  162. return $addSubOrder;
  163. }
  164. $subOrderId = $addSubOrder['data'];
  165. $subOrderInfo = $subOrderDao->getInfoById($subOrderId);
  166. if (!$subOrderInfo['flag']) {
  167. return $subOrderInfo;
  168. }
  169. /**
  170. * 2.1添加子订单下的采购单
  171. */
  172. $purchaseDao = new PurchaseDao();
  173. if ($subOrderParam['prod_type'] == 'hotel') {
  174. $addPurchase = $purchaseDao->saveHotelPurchase($subOrderParam, $subOrderInfo['data']);
  175. } else {
  176. $addPurchase = $purchaseDao->saveItemPurchase($subOrderParam, $subOrderInfo['data']);
  177. }
  178. if (!$addPurchase['flag']) {
  179. return $addPurchase;
  180. }
  181. $purchaseId = $addPurchase['data'];
  182. /**
  183. * 2.1.1添加采购单的每日价格
  184. */
  185. $purchasePriceDao = new PurchasePriceDao();
  186. $addPurchasePrice = $purchasePriceDao->saveList($subOrderParam['purchasePriceList'], $orderId, $subOrderParam['prod_type'], $subOrderId, $purchaseId);
  187. if (!$addPurchasePrice['flag']) {
  188. return $addPurchasePrice;
  189. }
  190. /**
  191. * 2.1.2 计算更新 采购单总金额、成本、产品数量
  192. */
  193. $setPurchaseRe = $purchaseDao->setPurchaseAmount($purchaseId);
  194. if (!$setPurchaseRe['flag']) {
  195. return $setPurchaseRe;
  196. }
  197. /**
  198. * 2.2 计算更新 子订单成本、金额、产品数量
  199. */
  200. $setSubOrderRe = $subOrderDao->setSubOrderAmount($subOrderId);
  201. if (!$setSubOrderRe['flag']) {
  202. return $setSubOrderRe;
  203. }
  204. /**
  205. * 3 计算更新 主订单成本、金额、产品数量
  206. */
  207. $setOrderMainRe = $orderMainDao->setOrderAmount($orderId);
  208. if (!$setOrderMainRe['flag']) {
  209. return $setOrderMainRe;
  210. }
  211. return Util::returnArrSu("保存成功",$subOrderId);
  212. }
  213. /**
  214. * 删除子订单
  215. * @param $param
  216. * @return array
  217. */
  218. public function delSubOrder($param){
  219. try {
  220. if ($param['prod_type'] == 'hotel') {
  221. $subOrderDao = new OrderHotelDao();
  222. } else {
  223. $subOrderDao = new OrderItemDao();
  224. }
  225. $subOrderRe = $subOrderDao->getInfoById($param['id']);
  226. if (!$subOrderRe['flag']) {
  227. return $subOrderRe;
  228. }
  229. $subOrderDao->delById($param['id']);
  230. //删除采购单
  231. $purchaseDao = new PurchaseDao();
  232. $purchaseDao->deleteBySubOrderId($param['id']);
  233. //删除每日采购单价格
  234. $purchasePriceDao = new PurchasePriceDao();
  235. $purchasePriceDao->deleteBySubOrderId($param['id']);
  236. //重新计算订单总金额
  237. $orderMainDao = new OrderMainDao();
  238. $orderMainDao->setOrderAmount($subOrderRe['data']['order_id']);
  239. return Util::returnArrSu("成功");
  240. }catch (\Exception $e){
  241. return Util::returnArrEr("删除子订单失败:".$e->getMessage());
  242. }
  243. }
  244. /**
  245. * 获取订单详情
  246. * @param $id
  247. * @return array
  248. */
  249. public function getOrderInfo($id){
  250. $orderMainDao = new OrderMainDao();
  251. $orderMainRe = $orderMainDao->getInfoById($id);
  252. if (!$orderMainRe['flag']) {
  253. return $orderMainRe;
  254. }
  255. $orderMain = $orderMainRe['data'];
  256. //获取采购单金额列表
  257. $purchasePriceDao = new PurchasePriceDao();
  258. $purchasePriceRe = $purchasePriceDao->getPurchasePriceListByOrderId($id);
  259. if (!$purchasePriceRe['flag']) {
  260. return $purchasePriceRe;
  261. }
  262. //获取采购单列表
  263. $purchaseDao = new PurchaseDao();
  264. $purchaseRe = $purchaseDao->getListByOrderId($id);
  265. if (!$purchaseRe['flag']) {
  266. return $purchaseRe;
  267. }
  268. //设置采购单展示数据
  269. $purchaseShow = $purchaseDao->setPurchaseShow($purchaseRe['data'],$purchasePriceRe['data']);
  270. if (!$purchaseShow['flag']) {
  271. return $purchaseShow;
  272. }
  273. //获取子订单列表
  274. $orderHotelDao = new OrderHotelDao();
  275. $orderHotelRe = $orderHotelDao->getListByOrderId($id);
  276. if (!$orderHotelRe['flag']) {
  277. return $orderHotelRe;
  278. }
  279. $orderItemDao = new OrderItemDao();
  280. $orderItemRe = $orderItemDao->getListByOrderId($id);
  281. if (!$orderItemRe['flag']) {
  282. return $orderItemRe;
  283. }
  284. //设置子订单列表
  285. $subOrderList = $orderMainDao->setSubOrderShow($purchaseShow['data'],$orderHotelRe['data'],$orderItemRe['data']);
  286. if (!$subOrderList['flag']) {
  287. return $subOrderList;
  288. }
  289. $orderMain["subOrderList"]=$subOrderList['data'];
  290. return Util::returnArrSu("成功",$orderMain);
  291. }
  292. /**
  293. * 获取订单列表
  294. * @param $param
  295. * @return array
  296. */
  297. public function getOrderList($param){
  298. $orderMainDao = new OrderMainDao();
  299. $where = [" a.del_flag = 0 ","a.group_id= {$param['group_id']} "];
  300. if (!empty($param['order_id'])) {
  301. $where[] =" a.id = {$param['order_id']} ";
  302. }
  303. if (!empty($param['channel_id'])) {
  304. $where[] = " a.channel_id = {$param['channel_id']} ";
  305. }
  306. if ($param['order_status'] !== '') {
  307. $where[] = " a.order_status = {$param['order_status']} ";
  308. }
  309. if (!empty($param['commissioner_id'])) {
  310. $where[] = " a.commissioner_id = {$param['commissioner_id']} ";
  311. }
  312. if (!empty($param['user_name'])) {
  313. $where[] = " a.user_name like '%{$param['user_name']}%' ";
  314. }
  315. if (!empty($param['user_phone'])) {
  316. $where[] = " a.user_phone = '{$param['user_phone']}' ";
  317. }
  318. if (!empty($param['create_id'])) {
  319. $where[] = " a.create_id = {$param['create_id']} ";
  320. }
  321. //金额区间查询
  322. if (!empty($param['startMoney'])) {
  323. $where[] = " a.total_amount >= {$param['startMoney']} ";
  324. }
  325. if (!empty($param['endMoney']) ) {
  326. $where[] = " a.total_amount <= {$param['endMoney']} ";
  327. }
  328. //成本区间查询
  329. if (!empty($param['startCost'])) {
  330. $where[] = " a.cost_amount >= {$param['startCost']} ";
  331. }
  332. if (!empty($param['endCost']) ) {
  333. $where[] = " a.cost_amount <= {$param['endCost']} ";
  334. }
  335. //利润区间查询
  336. if (!empty($param['startProfit'])) {
  337. $where[] = " a.profit_amount >= {$param['startProfit']} ";
  338. }
  339. if (!empty($param['endProfit']) ) {
  340. $where[] = " a.profit_amount <= {$param['endProfit']} ";
  341. }
  342. //时间区间查询
  343. if (!empty($param['startTime'])) {
  344. $where[] = " a.create_time >= '{$param['startTime']} 00:00:00' ";
  345. }
  346. if (!empty($param['endTime']) ) {
  347. $where[] = " a.create_time <= '{$param['endTime']} 23:59:59' ";
  348. }
  349. if ($param['receipt_order_status'] !== '') {
  350. $where[] = " a.receipt_order_status = {$param['receipt_order_status']} ";
  351. }
  352. if ($param['receipt_order_id'] !== '') {
  353. $where[] = " a.receipt_order_id = {$param['receipt_order_id']} ";
  354. }
  355. if ($param['channel_order_no'] != '') {
  356. $where[] = " a.channel_order_no = '{$param['channel_order_no']}' ";
  357. }
  358. //子订单查询条件 入住时间
  359. if (!empty($param['startInDate']) && empty($param['endInDate'])) {
  360. $where[] = " (b.check_in_date >= '{$param['startInDate']} 00:00:00' or c.check_in_date >= '{$param['startInDate']} 00:00:00' )";
  361. }
  362. if (!empty($param['endInDate']) && empty($param['startInDate']) ) {
  363. $where[] = " (b.check_in_date <= '{$param['endInDate']} 23:59:59' or c.check_in_date <= '{$param['endInDate']} 23:59:59' ) ";
  364. }
  365. if (!empty($param['startInDate']) && !empty($param['endInDate']) ) {
  366. $where[] = " (
  367. (b.check_in_date >= '{$param['startInDate']} 00:00:00' and b.check_in_date <= '{$param['endInDate']} 23:59:59')
  368. or
  369. (c.check_in_date >= '{$param['startInDate']} 00:00:00' and c.check_in_date <= '{$param['endInDate']} 23:59:59')
  370. ) ";
  371. }
  372. //供应商
  373. if (!empty($param['supplier_id'])) {
  374. $where[] = "(b.supplier_id = {$param['supplier_id']} or c.supplier_id = {$param['supplier_id']})";
  375. }
  376. if (!empty($param['customer_name'])) {
  377. $where[] = "(b.customer_name like '%{$param['customer_name']}%' or c.customer_name like '%{$param['customer_name']}%')";
  378. }
  379. //子订单查询条件 离店时间
  380. if (!empty($param['startOutDate'])) {
  381. $where[] = " b.check_out_date >= '{$param['startOutDate']} 00:00:00' ";
  382. }
  383. if (!empty($param['endOutDate']) ) {
  384. $where[] = " b.check_out_date <= '{$param['endOutDate']} 23:59:59' ";
  385. }
  386. $result = $orderMainDao->getOrderListByWhereStr(join(" and ",$where),$param);
  387. if (!$result['flag'] || $result['data']['total']==0) {
  388. return $result;
  389. }
  390. //获取主订单ID
  391. $ids = [];
  392. foreach ($result['data']['list'] as $info){
  393. $ids[] = $info['id'];
  394. }
  395. //获取子订单列表
  396. $orderHotelDao = new OrderHotelDao();
  397. $orderItemDao = new OrderItemDao();
  398. $orderHotelList = $orderHotelDao->getOrderListByOrderIds($ids);
  399. $orderItemList = $orderItemDao->getOrderListByOrderIds($ids);
  400. //设置主订单列表
  401. $orderMainList = [];
  402. foreach ($result['data']['list'] as $order) {
  403. $order['subOrder']=[];
  404. foreach ($orderHotelList as $hotel){
  405. if ($hotel['order_id'] == $order['id']) {
  406. $hotel["type"]="酒店";
  407. $order['subOrder'][] = $hotel;
  408. }
  409. }
  410. foreach ($orderItemList as $item){
  411. if ($item['order_id'] == $order['id']) {
  412. $item["type"]="附加项目";
  413. $order['subOrder'][] = $item;
  414. }
  415. }
  416. $orderMainList[] = $order;
  417. }
  418. $result['data']['list'] = $orderMainList;
  419. return $result;
  420. }
  421. /**
  422. * 添加财务备注
  423. * @param $params
  424. * @return array
  425. */
  426. public function setFinanceMemo($params){
  427. try{
  428. $data = [
  429. "finance_memo"=>$params['finance_memo']
  430. ];
  431. if ($params['type']=="order") {
  432. $model = new OrderMain();
  433. $model->save($data,["id"=>$params['order_id']]);
  434. }else if ($params['type']=="hotel") {
  435. $model = new OrderHotel();
  436. $model->save($data,["id"=>$params['order_id']]);
  437. }else{
  438. $model = new OrderItem();
  439. $model->save($data,["id"=>$params['order_id']]);
  440. }
  441. return Util::returnArrSu();
  442. }catch (Exception $e){
  443. return Util::returnArrEr("失败".$e->getMessage());
  444. }
  445. }
  446. }