酒店预订平台

OrderMainService.php 16 KiB

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