OrderService.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. <?php
  2. namespace bizGhs\order\services;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\shop\classes\ShopAdminClass;
  5. use biz\shop\classes\ShopCapitalClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopExtClass;
  8. use biz\stat\classes\StatKdClass;
  9. use biz\stat\classes\StatKhCgClass;
  10. use biz\stat\classes\StatSaleClass;
  11. use biz\wx\classes\WxMessageClass;
  12. use bizGhs\base\services\BaseService;
  13. use bizGhs\book\classes\BookItemClass;
  14. use bizGhs\custom\classes\CustomClass;
  15. use bizGhs\custom\classes\CustomDebtChangeClass;
  16. use bizGhs\order\classes\OrderClass;
  17. use bizGhs\order\classes\OrderExpressClass;
  18. use bizGhs\order\classes\OrderItemClass;
  19. use bizGhs\order\classes\OrderSendClass;
  20. use bizGhs\product\classes\ProductClass;
  21. use bizGhs\shop\classes\MainClass;
  22. use bizGhs\shop\classes\ShopMoneyChangeClass;
  23. use bizGhs\stat\classes\StatYjClass;
  24. use bizHd\cg\services\CgRefundService;
  25. use bizHd\purchase\services\PurchaseService;
  26. use bizHd\stat\classes\StatIncomeClass;
  27. use bizHd\stat\classes\StatOrderClass;
  28. use common\components\dict;
  29. use common\components\util;
  30. use bizHd\purchase\classes\PurchaseClass;
  31. use common\components\stringUtil;
  32. use Yii;
  33. class OrderService extends BaseService
  34. {
  35. public static $baseFile = '\bizGhs\order\classes\OrderClass';
  36. public static function arrival($order, $data)
  37. {
  38. $params = ['fhType' => 0, 'fhWl' => '', 'fhWlNo' => '',];
  39. OrderClass::orderSend($order, $params);
  40. //product [{"productId":"3674","num":0,"price":1}]
  41. $product = $data['product'] ?? '';
  42. if (empty($product)) {
  43. util::fail('请填写成本价和到货数量');
  44. }
  45. $product = json_decode($product, true);
  46. if (empty($product)) {
  47. util::fail('没有填写成本价和到货数量');
  48. }
  49. $data['product'] = $product;
  50. //供货商到货
  51. $respond = OrderClass::arrival($order, $data);
  52. $cgId = $order->purchaseId ?? 0;
  53. $cg = PurchaseClass::getById($cgId, true);
  54. if (empty($cg)) {
  55. util::fail('没有找到采购单');
  56. }
  57. //供货商花材转换为零售店花材
  58. $ids = array_column($product, 'productId');
  59. $ghsProductInfo = ProductClass::getByIds($ids);
  60. if (empty($ghsProductInfo)) {
  61. util::fail('没有花材??');
  62. }
  63. $ghsPtIdRelation = [];
  64. foreach ($ghsProductInfo as $ghsProduct) {
  65. $id = $ghsProduct['id'] ?? 0;
  66. $ptItemId = $ghsProduct['itemId'] ?? 0;
  67. $ghsPtIdRelation[$ptItemId] = $id;
  68. }
  69. $ptIds = array_keys($ghsPtIdRelation);
  70. $cgMainId = $cg->mainId ?? 0;
  71. $cgSjId = $cg->sjdId ?? 0;
  72. $cgShopId = $cg->shopId ?? 0;
  73. $cgItem = ProductClass::getAllByCondition(['mainId' => $cgMainId, 'itemId' => ['in', $ptIds]], null, '*');
  74. $ghsHdIdRelate = [];
  75. foreach ($cgItem as $it) {
  76. $ptItemId = $it['itemId'] ?? 0;
  77. $ghsItemId = $ghsPtIdRelation[$ptItemId] ?? 0;
  78. $cgItemId = $it['id'] ?? 0;
  79. $ghsHdIdRelate[$ghsItemId] = $cgItemId;
  80. }
  81. $cgProduct = [];
  82. foreach ($product as $pro) {
  83. $price = $pro['price'] ?? 0;
  84. $num = $pro['num'] ?? 0;
  85. $productId = $pro['productId'] ?? 0;
  86. $currentId = $ghsHdIdRelate[$productId] ?? 0;
  87. $cgProduct[$currentId] = ['num' => $num, 'price' => $price, 'productId' => $currentId];
  88. }
  89. //零售到货
  90. $saleRefund = $respond['refund'] ?? null;
  91. $saleRefundId = 0;
  92. if (!empty($saleRefund)) {
  93. $saleRefundId = $saleRefund->id ?? 0;
  94. }
  95. $kiloFee = $respond['kiloFee'] ?? 0;
  96. $miniKilo = $respond['miniKilo'] ?? 0;
  97. $saleRefundId = $saleRefundId;
  98. $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
  99. $packCost = isset($data['packCost']) && is_numeric($data['packCost']) ? $data['packCost'] : 0;
  100. $cgData = [
  101. 'product' => $cgProduct,
  102. 'kiloFee' => $kiloFee,
  103. 'miniKilo' => $miniKilo,
  104. 'saleRefundId' => $saleRefundId,
  105. 'sjId' => $cgSjId,
  106. 'shopId' => $cgShopId,
  107. 'mainId' => $cgMainId,
  108. 'sendCost' => $sendCost,
  109. 'packCost' => $packCost,
  110. ];
  111. $result = PurchaseClass::arrival($cg, $cgData);
  112. $cgRefund = $result['refund'] ?? null;
  113. if (!empty($cgRefund)) {
  114. $cgRefundId = $cgRefund->id ?? 0;
  115. if (!empty($saleRefund)) {
  116. $saleRefund->cgRefundId = $cgRefundId;
  117. $saleRefund->save();
  118. }
  119. }
  120. //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
  121. if (!empty($cg) && !empty($order)) {
  122. $ghsShopId = $order->shopId ?? 0;
  123. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
  124. if ($ext->orderFlow == 0) {
  125. $newCg = PurchaseClass::getById($cgId, true);
  126. PurchaseClass::confirmTake($newCg);
  127. }
  128. }
  129. }
  130. //添加订单
  131. public static function createNewOrder($data, $custom, $hasPay = 1)
  132. {
  133. //有unitType数据,说明是收银台传过来的数据,转换成和手机端结构一致
  134. $hasUnitType = array_column($data['product'], 'unitType');
  135. if (!empty($hasUnitType)) {
  136. $newProductData = [];
  137. foreach ($data['product'] as $productItem) {
  138. $unitType = $productItem['unitType'] ?? 0;
  139. $num = $productItem['num'] ?? 0;
  140. $productId = $productItem['productId'] ?? 0;
  141. $classId = $productItem['classId'] ?? 0;
  142. $itemId = $productItem['itemId'] ?? 0;
  143. $price = $productItem['price'] ?? 0;
  144. if ($unitType == 0) {
  145. $bigNum = $num;
  146. $smallNum = '';
  147. } else {
  148. $bigNum = '';
  149. $smallNum = $num;
  150. }
  151. $newProductData[] = [
  152. 'productId' => (int)$productId,
  153. 'bigNum' => (int)$bigNum,
  154. 'smallNum' => (int)$smallNum,
  155. 'classId' => (int)$classId,
  156. 'itemId' => (int)$itemId,
  157. 'price' => floatval($price),
  158. ];
  159. }
  160. $data['product'] = $newProductData;
  161. }
  162. //供货商销售单
  163. $returnOrder = OrderClass::addOrder($data, $custom);
  164. $saleId = $returnOrder->id ?? 0;
  165. $customSjId = $custom['sjId'] ?? 0;
  166. $customShopId = $custom['shopId'] ?? 0;
  167. $localOrder = $data['localOrder'] ?? 0;
  168. $transType = $data['transType'] ?? 0;
  169. $ghsShopAdminId = $data['shopAdminId'] ?? 0;
  170. $ghsShopAdminName = $data['shopAdminName'] ?? '';
  171. $customId = $data['customId'] ?? 0;
  172. $ghsId = $data['ghsId'] ?? 0;
  173. $ghs = GhsClass::getById($ghsId);
  174. if (empty($ghs)) {
  175. util::fail('没有找到供货商');
  176. }
  177. $ghsName = $ghs['name'] ?? '';
  178. $ghsAvatar = $ghs['avatar'] ?? '';
  179. $ghsMobile = $ghs['mobile'] ?? '';
  180. $ghsFullAddress = $ghs['fullAddress'] ?? '';
  181. $payWay = $data['payWay'] ?? dict::getDict('payWay', 'wxPay');
  182. //销售单欠款字段与采购单欠款字段转换
  183. $debt = $data['debt'] ?? 0;
  184. $purchaseDebt = $debt == 1 ? PurchaseClass::DEBT_YES : PurchaseClass::DEBT_NO;
  185. if ($debt == 1) {
  186. $payWay = dict::getDict('payWay', 'debtPay');
  187. }
  188. //销售单的自取还是配送字段 与 采购单的转换
  189. $sendType = $data['sendType'] ?? OrderClass::SEND_TYPE_CAR_GET;
  190. $getType = $sendType == OrderClass::SEND_TYPE_THIRD_SEND ? PurchaseClass::GET_TYPE_SEND : PurchaseClass::GET_TYPE_SELF_GET;
  191. //零售采购单时没有传price字段,供货商可以改价会传price字段,这里转成userPrice以便采购单识别使用改的价还是花材原价!
  192. $currentProduct = $data['product'];
  193. foreach ($currentProduct as $currentKey => $currentItem) {
  194. if (isset($currentItem['price'])) {
  195. $currentProduct[$currentKey]['userPrice'] = $currentItem['price'];
  196. unset($currentProduct[$currentKey]['price']);
  197. }
  198. }
  199. $sendTimeWant = !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
  200. $remark = $data['remark'] ?? '';
  201. $xj = $data['xj'] ?? '';
  202. $treeData = $data['treeData'] ?? [];
  203. $book = $data['book'] ?? 0;
  204. //后端开单,涉及花材满减,关键词 back_order_reach_discount_price
  205. $reachDiscountPrice = $data['reachDiscountPrice'] ?? 0;
  206. $wlName = $data['wlName'] ?? '';
  207. $cgMainId = $ghs['ownMainId'] ?? 0;
  208. $purchaseData = [
  209. 'book' => $book,
  210. 'product' => $currentProduct,
  211. 'sendTimeWant' => $sendTimeWant,
  212. 'sendCost' => $data['sendCost'] ?? '0',
  213. 'packCost' => $data['packCost'] ?? '0',
  214. 'ghsShopAdminId' => $ghsShopAdminId,
  215. 'ghsShopAdminName' => $ghsShopAdminName,
  216. 'sjId' => $customSjId,
  217. 'shopId' => $customShopId,
  218. 'saleId' => $saleId,
  219. 'ghsId' => $ghsId,
  220. 'ghsName' => $ghsName,
  221. 'ghsMobile' => $ghsMobile,
  222. 'ghsFullAddress' => $ghsFullAddress,
  223. 'ghsAvatar' => $ghsAvatar,
  224. 'customId' => $customId,
  225. 'payWay' => $payWay,
  226. 'status' => PurchaseClass::STATUS_UN_PAY,
  227. 'cgStyle' => dict::getDict('cgStyle', 'ghs'),
  228. 'debt' => $purchaseDebt,
  229. 'getType' => $getType,
  230. 'sendType' => $sendType,
  231. 'remark' => $remark,
  232. 'xj' => $xj,
  233. 'treeData' => $treeData,
  234. 'discount' => 1,
  235. 'mainId' => $cgMainId,
  236. 'wlName' => $wlName,
  237. 'localOrder' => $localOrder,
  238. 'transType' => $transType,
  239. 'reachDiscountPrice' => $reachDiscountPrice,
  240. ];
  241. if (!empty($data['deadline'])) {
  242. $purchaseData['deadline'] = $data['deadline'];
  243. }
  244. if (isset($data['modifyPrice']) && is_numeric($data['modifyPrice'])) {
  245. $purchaseData['modifyPrice'] = $data['modifyPrice'];
  246. }
  247. $purchase = PurchaseClass::addPurchase($purchaseData, $ghs);
  248. //开历史订单
  249. $historyDate = $data['historyDate'] ?? '';
  250. if (!empty($historyDate)) {
  251. $currentPayTime = $historyDate . ' 12:00:00';
  252. $returnOrder->payTime = $currentPayTime;
  253. $purchase->payTime = $currentPayTime;
  254. $purchase->save();
  255. CustomDebtChangeClass::updateByCondition(['capitalType' => 10, 'relateId' => $returnOrder->id], ['payTime' => $currentPayTime]);
  256. }
  257. $purchaseId = $purchase->id ?? 0;
  258. $returnOrder->purchaseId = $purchaseId;
  259. $returnOrder->save();
  260. //欠款和已付款的走付款后流程
  261. if (in_array($hasPay, [dict::getDict('hasPay', 'payed'), dict::getDict('hasPay', 'debt')])) {
  262. if ($hasPay == dict::getDict('hasPay', 'debt')) {
  263. $shopId = $data['shopId'] ?? 0;
  264. $shop = ShopClass::getById($shopId, true);
  265. if (empty($shop)) {
  266. util::fail('没有门店信息');
  267. }
  268. //老商家允许设置overAllowDebt,新商家已经不允许设置,默认就是 1,不能超过已经设置的授信额度
  269. if (isset($shop->overAllowDebt) && $shop->overAllowDebt == 0) {
  270. $debtLimit = $custom['debtLimit'] ?? 0;
  271. $debtLimit = floatval($debtLimit);
  272. $debtAmount = $custom['debtAmount'] ?? 0;
  273. $currentPrice = $returnOrder->actPrice ?? 0;
  274. $newDebt = bcadd($currentPrice, $debtAmount, 2);
  275. if ($newDebt > $debtLimit) {
  276. util::fail('欠款已超' . $debtLimit . ',请先清账');
  277. }
  278. }
  279. }
  280. //下面二个顺序不能换
  281. PurchaseService::payAfter($purchase, $payWay);
  282. self::payAfter($returnOrder, $payWay);
  283. //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
  284. if (isset($returnOrder->book) && $returnOrder->book == 0) {
  285. $ghsShopId = $ghs['shopId'] ?? 0;
  286. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
  287. if ($ext->orderFlow == 0) {
  288. $returnId = $returnOrder->id;
  289. $order = OrderClass::getById($returnId, true);
  290. if (!empty($order)) {
  291. OrderClass::confirmReach($order);
  292. }
  293. }
  294. }
  295. }
  296. return $returnOrder;
  297. }
  298. //修改订单
  299. public static function updateOrder($order, $data)
  300. {
  301. $purchaseId = $order->purchaseId ?? 0;
  302. $purchase = PurchaseClass::getLockById($purchaseId);
  303. //修改供货商销售单
  304. $returnOrder = OrderClass::updateOrder($order, $data);
  305. //供货商销售单的 0未欠款 1欠款 转成零售采购单的 1欠款 2未欠款
  306. $debt = $data['debt'] ?? 0;
  307. $purchaseDebt = $debt == 1 ? PurchaseClass::DEBT_YES : PurchaseClass::DEBT_NO;
  308. $payWay = $data['payWay'] ?? dict::getDict('payWay', 'wxPay');
  309. if ($debt == 1) {
  310. $payWay = dict::getDict('payWay', 'debtPay');
  311. }
  312. //自取还是配送订单,销售单与采购单之间转换
  313. $sendType = $data['sendType'] ?? OrderClass::SEND_TYPE_MYSELF;
  314. $getType = $sendType == OrderClass::SEND_TYPE_NO ? PurchaseClass::GET_TYPE_SELF_GET : PurchaseClass::GET_TYPE_SEND;
  315. //供货商价格是可以修改的,修改的价格一起传给采购单保存
  316. $currentProduct = $data['product'];
  317. foreach ($currentProduct as $currentKey => $currentItem) {
  318. if (isset($currentProduct[$currentKey]['price'])) {
  319. $currentProduct[$currentKey]['userPrice'] = $currentProduct[$currentKey]['price'];
  320. unset($currentProduct[$currentKey]['price']);
  321. }
  322. }
  323. $remark = $data['remark'] ?? '';
  324. $purchaseData = [
  325. 'product' => $currentProduct,
  326. 'sendTime' => date("H:i:s"),
  327. 'sendCost' => $data['sendCost'] ?: '0.00',
  328. 'payWay' => $payWay,
  329. 'status' => PurchaseClass::STATUS_UN_PAY,
  330. 'cgStyle' => dict::getDict('cgStyle', 'ghs'),
  331. 'debt' => $purchaseDebt,
  332. 'getType' => $getType,
  333. 'status' => PurchaseClass::STATUS_UN_PAY,
  334. 'remark' => $remark,
  335. ];
  336. if (isset($data['deadline']) && !empty($data['deadline'])) {
  337. $purchaseData['deadline'] = $data['deadline'];
  338. }
  339. if (isset($data['modifyPrice']) && is_numeric($data['modifyPrice'])) {
  340. //优惠之后的金额
  341. if (is_numeric($data['modifyPrice']) == false || $data['modifyPrice'] <= 0) {
  342. util::fail('金额填错了哦');
  343. }
  344. $purchaseData['modifyPrice'] = $data['modifyPrice'];
  345. }
  346. //修改采购单
  347. $purchase = PurchaseClass::updatePurchase($purchase, $purchaseData);
  348. //除了自取订单直接变成已确认,其它都进入待确认流程。订单的确认以欠款或已收款方式确认。 ssh 20210809
  349. if (isset($data['sendType']) && $data['sendType'] == OrderClass::SEND_TYPE_NO) {
  350. PurchaseService::payAfter($purchase, $payWay);
  351. self::payAfter($returnOrder, $payWay);
  352. } else {
  353. //没有进入确认流程的订单也有打印需求
  354. if ($data['needPrint'] == dict::getDict('needPrint', 'need')) {
  355. OrderClass::onlinePrint($returnOrder);
  356. $returnOrder->printNum += 1;
  357. $returnOrder->save();
  358. }
  359. }
  360. return $returnOrder;
  361. }
  362. //自取免配送流程处理 ssh 2021.1.24
  363. public static function withoutSend($order)
  364. {
  365. if (isset($order->status) == false) {
  366. util::fail('没有订单状态');
  367. }
  368. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  369. util::fail('订单已完成');
  370. }
  371. //自取订单,订单直接完成。【现在有三个地方有订单完成操作】
  372. $shopId = $order->shopId ?? 0;
  373. $shop = ShopClass::getLockById($shopId);
  374. if (empty($shop)) {
  375. util::fail('没有找到门店3');
  376. }
  377. $shop->unSendOrder -= 1;
  378. $shop->sendingOrder += 1;
  379. $shop->save();
  380. //自取免配送
  381. $order->sendType = OrderClass::SEND_TYPE_NO;
  382. $order->save();
  383. //这个是采购模板的withoutSend,应另外封装方法
  384. $purchaseId = $order->purchaseId ?? 0;
  385. $purchase = PurchaseClass::getById($purchaseId, true);
  386. $pShopId = $purchase->shopId;
  387. $pShop = ShopClass::getLockById($pShopId);
  388. $pShop->cgUnSend -= 1;
  389. $pShop->cgSending += 1;
  390. $pShop->save();
  391. //订单完成
  392. self::finish($order, $shop);
  393. OrderSendClass::withoutSend($order);
  394. return OrderClass::TOTAL_FLOW;
  395. }
  396. //确认送达 ssh 2021.3.4
  397. public static function reach($order)
  398. {
  399. if ($order->status != OrderClass::ORDER_STATUS_SENDING) {
  400. util::fail('订单没有在配送状态');
  401. }
  402. //第三方配送的变更第三方订单状态
  403. if (isset($order->sendType) && $order->sendType == OrderClass::SEND_TYPE_THIRD) {
  404. $express = OrderExpressClass::getByCondition(['orderSn' => $order->orderSn], true);
  405. if (!empty($express)) {
  406. $express->status = OrderExpressClass::STATUS_COMPLETE;
  407. $express->save();
  408. }
  409. }
  410. $shopId = $order->shopId ?? 0;
  411. $shop = ShopClass::getLockById($shopId);
  412. if (empty($shop)) {
  413. util::fail('没有找到门店4');
  414. }
  415. //订单完成
  416. self::finish($order, $shop);
  417. OrderSendClass::reach($order);
  418. //是否微信通知
  419. $noNeedWxNotice = isset(Yii::$app->params['noNeedWxNotice']) ? Yii::$app->params['noNeedWxNotice'] : 0;
  420. if ($noNeedWxNotice == 0) {
  421. //微信通知
  422. WxMessageClass::reachInform($shop, $order);
  423. }
  424. }
  425. //订单完成
  426. public static function finish($order, $shop)
  427. {
  428. //销售单完成
  429. $order = OrderClass::complete($order, $shop);
  430. //采购单完成
  431. $purchaseId = $order->purchaseId ?? 0;
  432. $purchase = PurchaseClass::getById($purchaseId, true);
  433. PurchaseClass::complete($purchase);
  434. }
  435. //获取订单信息 ssh 2019.11.28
  436. public static function getOrderList($where)
  437. {
  438. $data = self::getList('*', $where, 'addTime DESC');
  439. if (isset($data['list']) == false || empty($data['list'])) {
  440. return $data;
  441. }
  442. $orderSns = array_column($data['list'], 'orderSn');
  443. //获取订单的商品信息
  444. $goodsList = OrderGoodsService::getGoodsListByOrderSns($orderSns);
  445. //获取订花人的用户信息
  446. $orderUserIds = array_column($data['list'], 'userId');
  447. $orderUserIds = array_unique($orderUserIds);
  448. $userInfoList = UserService::getUserByIds($orderUserIds);
  449. $sjIdList = array_column($data['list'], 'sjId');
  450. $sjIdList = array_filter(array_unique($sjIdList));
  451. $merchantList = MerchantService::getByIds($sjIdList, null, 'id');
  452. $periodData = [0 => '上午', 1 => '下午', 2 => '晚上'];
  453. foreach ($data['list'] as $key => $val) {
  454. $id = $val['id'];
  455. $userId = $val['userId'];
  456. $data['list'][$key]['goodsInfoList'] = isset($goodsList[$id]) ? $goodsList[$id] : [];
  457. $data['list'][$key]['bookAvatar'] = isset($userInfoList[$userId]['smallAvatarUrl']) ? $userInfoList[$userId]['smallAvatarUrl'] : '';
  458. $sjId = $val['sjId'];
  459. $data['list'][$key]['name'] = isset($merchantList[$sjId]['name']) ? $merchantList[$sjId]['name'] : '';
  460. $data['list'][$key]['reachTime'] = OrderClass::getReachTime($val);
  461. $requireReachTime = '';
  462. if ($val['reachDate'] != '0000-00-00') {
  463. $reachPeriodId = $val['reachPeriod'];
  464. $reachPeriod = isset($periodData[$reachPeriodId]) ? $periodData[$reachPeriodId] : '上午';
  465. $requireReachTime = date("m-d", strtotime($val['reachDate'])) . ' ' . $reachPeriod;
  466. }
  467. $data['list'][$key]['requireReachTime'] = $requireReachTime;
  468. }
  469. return $data;
  470. }
  471. //取客户最近若干条订单 ssh 2019.11.30
  472. public static function getUserOrder($userId, $limit = 10, $order = null, $with = null)
  473. {
  474. $where = ['userId' => $userId, 'payStatus' => 1];
  475. return self::getLimitList('*', $where, $limit, $order, $with);
  476. }
  477. //支付前验证订单 ssh 2019.12.6
  478. public static function checkBeforePay($order, $userId)
  479. {
  480. $now = time();
  481. if (empty($order)) {
  482. util::fail('订单无效');
  483. }
  484. if (isset($order['userId']) && $order['userId'] != $userId) {
  485. util::fail('非法访问');
  486. }
  487. if ($order['status'] != 0) {
  488. util::fail('订单不是待付款状态');
  489. }
  490. if (isset($order['deadline']) && $now > $order['deadline']) {
  491. util::fail('订单已过期');
  492. }
  493. if (ceil($order['actPrice']) <= 0) {
  494. util::fail('订单金额有问题');
  495. }
  496. if ($order['payStatus'] == 1) {
  497. util::fail('您已经付过了');
  498. }
  499. }
  500. //商家验证是否有效 ssh 2019.12.15
  501. public static function valid($order, $shopId)
  502. {
  503. $now = date("Y-m-d H:i:s");
  504. if (empty($order)) {
  505. util::fail('订单无效');
  506. }
  507. if ($order['shopId'] != $shopId) {
  508. util::fail('不是您的订单');
  509. }
  510. if (isset($order['status']) && $order['status'] == OrderClass::ORDER_STATUS_UN_PAY) {
  511. if (isset($order['deadline']) && $now > $order['deadline']) {
  512. util::fail('订单已过期');
  513. }
  514. }
  515. }
  516. //点评 ssh 2019.12.16
  517. public static function comment($data)
  518. {
  519. return OrderClass::comment($data);
  520. }
  521. //订单归类
  522. public static function classify($id, $categoryId, $usageId)
  523. {
  524. $connection = Yii::$app->db;
  525. $transaction = $connection->beginTransaction();
  526. try {
  527. $order = OrderClass::getById($id, true);
  528. if ($order->payStatus != 1) {
  529. util::fail('订单还未付款');
  530. }
  531. if ($order->classify == 1) {
  532. util::fail('已经归类过了');
  533. }
  534. $order->categoryId = $categoryId;
  535. $order->usageId = $usageId;
  536. $order->classify = 1;
  537. $order->save();
  538. //设置流水的分类和用途并分配资金
  539. $setData = [
  540. 'sjId' => $order->sjId,
  541. 'capitalId' => $order->capitalId,
  542. 'time' => $order->payTime,
  543. 'usageId' => $order->usageId,
  544. 'categoryId' => $order->categoryId,
  545. 'amount' => $order->actPrice,
  546. 'payWay' => $order->payWay,
  547. ];
  548. SjCapitalService::orderClassify($setData);
  549. $transaction->commit();
  550. } catch (Exception $e) {
  551. $transaction->rollBack();
  552. util::fail('没有归类成功');
  553. }
  554. }
  555. //更新配送单 ssh 2019.12.17
  556. public static function updateSheet($order, $data)
  557. {
  558. $id = $order['id'];
  559. if ($order['status'] != OrderClass::ORDER_STATUS_UN_SEND) {
  560. util::fail('订单当前不是待配送状态');
  561. }
  562. $data['deliver'] = 1;
  563. OrderClass::updateById($id, $data);
  564. }
  565. //订单详情 ssh 2020.1.2
  566. public static function getOrderById($id)
  567. {
  568. return OrderClass::getOrderById($id);
  569. }
  570. //查看订单详情 ssh 2020.1.4
  571. public static function getOrderBySn($orderSn)
  572. {
  573. return OrderClass::getOrderBySn($orderSn);
  574. }
  575. //计算优惠 ssh 2020.3.9
  576. public static function getDiscountPrice($params)
  577. {
  578. $couponId = isset($params['couponId']) ? $params['couponId'] : 0;
  579. $userId = isset($params['userId']) ? $params['userId'] : 0;
  580. $price = isset($params['price']) ? $params['price'] : 0;
  581. $sourceType = isset($params['sourceType']) ? $params['sourceType'] : 0;
  582. $discountAmount = 0;
  583. $discountType = 0;
  584. $discountPrice = $price;
  585. //使用优惠券
  586. if (!empty($couponId)) {
  587. $discountAmount = CouponService::checkBeforeUse($couponId, $price, $userId);
  588. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  589. $discountType = 1;
  590. } else {
  591. //使用会员
  592. // $asset = UserAssetClass::getByUserId($userId);
  593. // if (isset($asset['member']) && $asset['member'] > 0) {
  594. // $currentDiscount = $asset['discount'];
  595. // $currentPrice = $price * $currentDiscount;
  596. // $newPrice = substr(sprintf("%.3f", $currentPrice), 0, -1);
  597. // $discountType = 2;
  598. // $discountAmount = stringUtil::calcSub($price, $newPrice);
  599. // $discountPrice = $newPrice;
  600. // }
  601. }
  602. //如果没有优惠券和会员优惠,并且是朋友圈收款,则进行随机优惠 ssh 2019.9.12
  603. if ($discountPrice == $price && $sourceType == 3) {
  604. $discountAmount = OrderClass::getRandDiscount($price);
  605. //开发和测试环境直接随机优惠
  606. $env = getenv('YII_ENV');
  607. if ($env == 'local' || $env == 'test') {
  608. $discountAmount = 0.01;
  609. }
  610. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  611. $discountType = 3;
  612. }
  613. $discountType = $discountAmount <= 0 ? 0 : $discountType;
  614. $discountPrice = $discountPrice <= 0 ? 0.01 : $discountPrice;
  615. return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount];
  616. }
  617. //获取订单详情 ssh 2021.1.21
  618. public static function getOrderInfo($id, $showButton = false, $showItem = false, $showExpress = false, $isList = false)
  619. {
  620. $order = OrderClass::getById($id, true);
  621. if (empty($order)) {
  622. util::fail('没有找到订单');
  623. }
  624. $info = $order->attributes;
  625. $respond = OrderClass::groupOrder([$info], $showButton, $showItem, $showExpress, $isList);
  626. return current($respond);
  627. }
  628. //订单失效后的操作 ssh 2021.5.14
  629. public static function expire($order)
  630. {
  631. $purchaseId = $order->purchaseId ?? 0;
  632. $current = PurchaseClass::getById($purchaseId, true);
  633. if (empty($current)) {
  634. util::fail('没有找到采购单');
  635. }
  636. PurchaseService::expire($current);
  637. return true;
  638. }
  639. //支付成功后的流程 ssh 2021.5.14
  640. //$isPurchase 是否零售端采购支付,如果是会引起销售方余额变动
  641. public static function payAfter($order, $payWay, $isPurchase = false)
  642. {
  643. if (isset($order->status) == false) {
  644. util::fail('没有订单状态');
  645. }
  646. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  647. util::fail('重复操作');
  648. }
  649. if (isset($order->debt) == false) {
  650. util::fail('订单有问题,没有欠款项');
  651. }
  652. $orderId = $order->id;
  653. $orderSn = $order->orderSn ?? '';
  654. $customId = $order->customId;
  655. $custom = CustomClass::getLockById($customId);
  656. if (empty($custom)) {
  657. util::fail('没有找到客户信息');
  658. }
  659. //门店资产变化
  660. $amount = $order->actPrice ?? 0.00;
  661. $realPrice = $order->realPrice ?? 0.00;
  662. $currentShopId = $order->shopId ?? 0;
  663. $book = $order->book ?? 0;
  664. $shop = ShopClass::getLockById($currentShopId);
  665. if (empty($shop)) {
  666. util::fail('没有找到门店5');
  667. }
  668. $mainId = $shop->mainId ?? 0;
  669. $main = MainClass::getLockById($mainId);
  670. if (empty($main)) {
  671. util::fail('没有main信息3');
  672. }
  673. $main->unPayOrder -= 1;
  674. //预订单的待发货订单增加
  675. if ($book == 1) {
  676. $main->unSendOrder += 1;
  677. //预订单统计数量增加
  678. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  679. if (!empty($itemList)) {
  680. $bookData = [];
  681. foreach ($itemList as $currentItem) {
  682. $itemRemark = $currentItem->remark ?? 0;
  683. $hasRemark = !empty($itemRemark) ? 1 : 0;
  684. $bookData[] = [
  685. 'productId' => $currentItem->productId,
  686. 'num' => $currentItem->xhNum ?? 0,
  687. 'lastNum' => $currentItem->xhNum ?? 0,
  688. 'remark' => $itemRemark,
  689. 'hasRemark' => $hasRemark,
  690. ];
  691. }
  692. $staffId = $order->shopAdminId ?? 0;
  693. $staffName = $order->shopAdminName ?? '';
  694. $params = [
  695. 'staffId' => $staffId, 'staffName' => $staffName, 'action' => 'add'
  696. ];
  697. BookItemClass::addBatchItem($bookData, $order, $params);
  698. }
  699. } else {
  700. $main->finishOrder += 1;
  701. }
  702. $main->totalOrderNum += 1;
  703. $currentTotalIncome = bcadd($main->totalIncome, $amount, 2);
  704. $main->totalIncome = $currentTotalIncome;
  705. $totalSale = bcadd($main->totalSale, $amount, 2);
  706. $main->totalSale = $totalSale;
  707. if ($payWay == dict::getDict('payWay', 'debtPay')) {
  708. $currentGathering = bcadd($main->mayGathering, $realPrice, 2);
  709. $main->mayGathering = $currentGathering;
  710. if ($custom->debtNum == 0) {
  711. $main->mayGatheringNum += 1;
  712. }
  713. }
  714. $main->save();
  715. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  716. $shopAdminId = $order->shopAdminId ?? 0;
  717. $shopAdminName = $order->shopAdminName ?? '';
  718. $event = '下单';
  719. if ($isPurchase) {
  720. $event = '客户采购';
  721. if (isset($custom->name) && !empty($custom->name)) {
  722. $event = $custom->name . '采购';
  723. }
  724. }
  725. $sjId = $order->sjId ?? 0;
  726. $shopId = $order->shopId ?? 0;
  727. $capital = [
  728. 'capitalType' => $capitalType,
  729. 'relateId' => $orderId,
  730. 'io' => 1,
  731. 'amount' => $amount,
  732. 'totalIncome' => $currentTotalIncome,
  733. 'payWay' => $payWay,
  734. 'event' => $event,
  735. 'mainId' => $mainId,
  736. 'sjId' => $sjId,
  737. 'shopId' => $shopId,
  738. 'shopAdminId' => $shopAdminId,
  739. 'shopAdminName' => $shopAdminName,
  740. 'mainId' => $mainId,
  741. ];
  742. ShopCapitalClass::addCapital($capital);
  743. //客户采购,会增加余额,会增加收入
  744. if ($isPurchase == true) {
  745. //余额支付和在线支付(微信支付、支付宝支付)会增加余额
  746. if ($order->onlinePay == dict::getDict('onlinePay', 'yes') || $payWay == dict::getDict('payWay', 'balancePay')) {
  747. ShopClass::customKdAddBalance($main, $shop, $realPrice, $order, $capitalType);
  748. }
  749. }
  750. //现金付款,现金余额需要增加
  751. if ($payWay == dict::getDict('payWay', 'cash')) {
  752. $moneyBalance = bcadd($main->money, $realPrice, 2);
  753. $main->money = $moneyBalance;
  754. $main->save();
  755. $moneyEvent = $custom->name . "现金下单" . floatval($realPrice) . "元(单号 {$orderSn})";
  756. $moneyRemark = '';
  757. $change = [
  758. 'relateId' => $orderId,
  759. 'amount' => $realPrice,
  760. 'balance' => $moneyBalance,
  761. 'io' => 1,
  762. 'mainId' => $mainId,
  763. 'capitalType' => $capitalType,
  764. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  765. 'event' => $moneyEvent,
  766. 'remark' => $moneyRemark,
  767. ];
  768. ShopMoneyChangeClass::addData($change);
  769. }
  770. //更新订单基础状态和支付状态
  771. $order->payWay = $payWay;
  772. if ($book == 1) {
  773. $order->status = OrderClass::ORDER_STATUS_UN_SEND;
  774. } else {
  775. $order->status = OrderClass::ORDER_STATUS_UN_SEND;
  776. }
  777. if ($payWay == dict::getDict('payWay', 'debtPay')) {
  778. if (isset($order->actPrice) && $order->actPrice == 0) {
  779. $order->debt = OrderClass::DEBT_NO;
  780. } else {
  781. $order->debt = OrderClass::DEBT_YES;
  782. }
  783. $order->remainDebtPrice = $order->orderPrice ?? 0;
  784. $order->debtPrice = $order->orderPrice ?? 0;
  785. }
  786. $order->payStatus = 1;
  787. if (!isset($order->payTime) || $order->payTime == '0000-00-00 00:00:00') {
  788. $order->payTime = date("Y-m-d H:i:s");
  789. }
  790. $order->save();
  791. //新订单进入队列,用于花材的销量统计
  792. OrderClass::newOrderAddQueue($order);
  793. //员工业绩、总业绩增加
  794. $shopAdminId = $order->shopAdminId ?? 0;
  795. if (!empty($shopAdminId)) {
  796. $shopAdmin = ShopAdminClass::getById($shopAdminId, true);
  797. $shopAdmin->kdAmount = bcadd($shopAdmin->kdAmount, $amount, 2);
  798. $shopAdmin->save();
  799. $yjAmount = $order->orderPrice ?? 0;
  800. StatYjClass::addYj($main, $shop, $shopAdminId, $yjAmount);
  801. }
  802. //欠款
  803. if ($payWay == dict::getDict('payWay', 'debtPay')) {
  804. CustomClass::cgDebtAmountAdd($custom, $order);
  805. }
  806. $custom->buyNum += 1;
  807. $custom->buyAmount = bcadd($custom->buyAmount, $amount, 2);
  808. $custom->visitTime = date("Y-m-d H:i:s");
  809. $custom->save();
  810. //收入统计
  811. StatIncomeClass::updateOrInsert($main, $shop, $amount);
  812. //各渠道收入金额统计
  813. StatKdClass::replace($shop, $amount, $payWay);
  814. //今日订单+1
  815. StatOrderClass::updateOrInsert($main, $shop);
  816. //销售统计
  817. StatSaleClass::replace($main, $shop, $amount);
  818. //客户采购统计
  819. StatKhCgClass::replace($order, $amount);
  820. //当天同个人同地址的订单汇总
  821. $prefixDate = substr($order->payTime, 0, 10);
  822. OrderClass::updateSameTimeIds($order, $prefixDate);
  823. OrderClass::updateMergeOrder($order,0);
  824. }
  825. //发起退款
  826. public static function refund($id, $post)
  827. {
  828. $order = OrderClass::getLockById($id);
  829. if (empty($order)) {
  830. util::fail('没有找到订单');
  831. }
  832. if (isset($order->clearId) && !empty($order->clearId)) {
  833. util::fail('已经结账,不能再售后');
  834. }
  835. if ($order->debt != 1 && $order->addTime < '2023-10-17 00:00:00') {
  836. util::fail('10月17日前订单无法申请退款');
  837. }
  838. //供货商销售单退款
  839. $saleRefund = RefundOrderService::addRefund($post, $order);
  840. $saleRefundId = $saleRefund->id;
  841. $purchaseId = $order->purchaseId;
  842. $cg = PurchaseClass::getLockById($purchaseId);
  843. if (empty($cg)) {
  844. util::fail('没有找到采购订单');
  845. }
  846. $mainId = $cg->mainId ?? 0;
  847. //花材列表结构
  848. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  849. //供货商的花材信息转换成零售店的花材信息
  850. $ghsProduct = $post['product'] ?? '';
  851. $ghsProductIndex = array_column($ghsProduct, null, 'productId');
  852. $ids = array_column($ghsProduct, 'productId');
  853. $ghsProductInfo = ProductClass::getByIds($ids, null, 'itemId');
  854. $ptItemIds = array_keys($ghsProductInfo);
  855. $ptItemIds = array_unique(array_filter($ptItemIds));
  856. $hdProductInfo = ProductClass::getAllByCondition(['mainId' => $mainId, 'itemId' => ['in', $ptItemIds]], null, '*', 'itemId');
  857. $newProduct = [];
  858. foreach ($hdProductInfo as $ptItemId => $hdInfo) {
  859. $ghsItemId = $ghsProductInfo[$ptItemId]['id'] ?? 0;
  860. $current = $ghsProductIndex[$ghsItemId] ?? [];
  861. $hdItemId = $hdInfo['id'] ?? 0;
  862. $cost = $hdInfo['cost'] ?? 0;
  863. $name = $hdInfo['name'] ?? '';
  864. $cover = $hdInfo['cover'] ?? '';
  865. $ratio = $hdInfo['ratio'] ?? 20;
  866. if (empty($current)) {
  867. util::fail('没有找到花材');
  868. }
  869. $current['productId'] = $hdItemId;
  870. $current['ptItemId'] = $ptItemId;
  871. $current['cost'] = $cost;
  872. $current['name'] = $name;
  873. $current['cover'] = $cover;
  874. $current['ratio'] = $ratio;
  875. $newProduct[] = $current;
  876. }
  877. $refundType = $post['refundType'] ?? 0;
  878. $price = $post['price'] ?? 0;
  879. $remark = $post['remark'] ?? '';
  880. $cause = $post['cause'] ?? 0;
  881. $refundSendCost = isset($post['refundSendCost']) && is_numeric($post['refundSendCost']) ? $post['refundSendCost'] : 0;
  882. $refundPackCost = isset($post['refundPackCost']) && is_numeric($post['refundPackCost']) ? $post['refundPackCost'] : 0;
  883. $cgData = [
  884. 'product' => $newProduct,
  885. 'refundType' => $refundType,
  886. 'price' => $price,
  887. 'remark' => $remark,
  888. 'cause' => $cause,
  889. 'refundSendCost' => $refundSendCost,
  890. 'refundPackCost' => $refundPackCost,
  891. ];
  892. $cgRefund = CgRefundService::addRefund($cgData, $cg);
  893. $cgRefundId = $cgRefund->id;
  894. $saleRefund->cgRefundId = $cgRefundId;
  895. $saleRefund->save();
  896. $cgRefund->saleRefundId = $saleRefundId;
  897. $cgRefund->save();
  898. return $saleRefund;
  899. }
  900. }