OrderService.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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. //开历史订单
  284. if (!empty($historyDate)) {
  285. //$returnOrder->payTime = $currentPayTime;
  286. //$returnOrder->save();
  287. //有修改账单日期需要更新到欠款变动明细,财务好对账,多个地方要同步修改,关键词 to_change_custom_debt_pay_time
  288. //找到当前订单客户的账单日期为 $date 的所有订单,然后更新同天内的所有订单的 sameTimeIds(加此次要改变的订单的订单号$id),有多处调用,关键词 update_same_ids
  289. //OrderClass::updateSameTimeIds($returnOrder, $historyDate);
  290. }
  291. //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
  292. if (isset($returnOrder->book) && $returnOrder->book == 0) {
  293. $ghsShopId = $ghs['shopId'] ?? 0;
  294. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
  295. if ($ext->orderFlow == 0) {
  296. $returnId = $returnOrder->id;
  297. $order = OrderClass::getById($returnId, true);
  298. if (!empty($order)) {
  299. OrderClass::confirmReach($order);
  300. }
  301. }
  302. }
  303. }
  304. return $returnOrder;
  305. }
  306. //修改订单
  307. public static function updateOrder($order, $data)
  308. {
  309. $purchaseId = $order->purchaseId ?? 0;
  310. $purchase = PurchaseClass::getLockById($purchaseId);
  311. //修改供货商销售单
  312. $returnOrder = OrderClass::updateOrder($order, $data);
  313. //供货商销售单的 0未欠款 1欠款 转成零售采购单的 1欠款 2未欠款
  314. $debt = $data['debt'] ?? 0;
  315. $purchaseDebt = $debt == 1 ? PurchaseClass::DEBT_YES : PurchaseClass::DEBT_NO;
  316. $payWay = $data['payWay'] ?? dict::getDict('payWay', 'wxPay');
  317. if ($debt == 1) {
  318. $payWay = dict::getDict('payWay', 'debtPay');
  319. }
  320. //自取还是配送订单,销售单与采购单之间转换
  321. $sendType = $data['sendType'] ?? OrderClass::SEND_TYPE_MYSELF;
  322. $getType = $sendType == OrderClass::SEND_TYPE_NO ? PurchaseClass::GET_TYPE_SELF_GET : PurchaseClass::GET_TYPE_SEND;
  323. //供货商价格是可以修改的,修改的价格一起传给采购单保存
  324. $currentProduct = $data['product'];
  325. foreach ($currentProduct as $currentKey => $currentItem) {
  326. if (isset($currentProduct[$currentKey]['price'])) {
  327. $currentProduct[$currentKey]['userPrice'] = $currentProduct[$currentKey]['price'];
  328. unset($currentProduct[$currentKey]['price']);
  329. }
  330. }
  331. $remark = $data['remark'] ?? '';
  332. $purchaseData = [
  333. 'product' => $currentProduct,
  334. 'sendTime' => date("H:i:s"),
  335. 'sendCost' => $data['sendCost'] ?: '0.00',
  336. 'payWay' => $payWay,
  337. 'status' => PurchaseClass::STATUS_UN_PAY,
  338. 'cgStyle' => dict::getDict('cgStyle', 'ghs'),
  339. 'debt' => $purchaseDebt,
  340. 'getType' => $getType,
  341. 'status' => PurchaseClass::STATUS_UN_PAY,
  342. 'remark' => $remark,
  343. ];
  344. if (isset($data['deadline']) && !empty($data['deadline'])) {
  345. $purchaseData['deadline'] = $data['deadline'];
  346. }
  347. if (isset($data['modifyPrice']) && is_numeric($data['modifyPrice'])) {
  348. //优惠之后的金额
  349. if (is_numeric($data['modifyPrice']) == false || $data['modifyPrice'] <= 0) {
  350. util::fail('金额填错了哦');
  351. }
  352. $purchaseData['modifyPrice'] = $data['modifyPrice'];
  353. }
  354. //修改采购单
  355. $purchase = PurchaseClass::updatePurchase($purchase, $purchaseData);
  356. //除了自取订单直接变成已确认,其它都进入待确认流程。订单的确认以欠款或已收款方式确认。 ssh 20210809
  357. if (isset($data['sendType']) && $data['sendType'] == OrderClass::SEND_TYPE_NO) {
  358. PurchaseService::payAfter($purchase, $payWay);
  359. self::payAfter($returnOrder, $payWay);
  360. } else {
  361. //没有进入确认流程的订单也有打印需求
  362. if ($data['needPrint'] == dict::getDict('needPrint', 'need')) {
  363. OrderClass::onlinePrint($returnOrder);
  364. $returnOrder->printNum += 1;
  365. $returnOrder->save();
  366. }
  367. }
  368. return $returnOrder;
  369. }
  370. //自取免配送流程处理 ssh 2021.1.24
  371. public static function withoutSend($order)
  372. {
  373. if (isset($order->status) == false) {
  374. util::fail('没有订单状态');
  375. }
  376. if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
  377. util::fail('订单已完成');
  378. }
  379. //自取订单,订单直接完成。【现在有三个地方有订单完成操作】
  380. $shopId = $order->shopId ?? 0;
  381. $shop = ShopClass::getLockById($shopId);
  382. if (empty($shop)) {
  383. util::fail('没有找到门店3');
  384. }
  385. $shop->unSendOrder -= 1;
  386. $shop->sendingOrder += 1;
  387. $shop->save();
  388. //自取免配送
  389. $order->sendType = OrderClass::SEND_TYPE_NO;
  390. $order->save();
  391. //这个是采购模板的withoutSend,应另外封装方法
  392. $purchaseId = $order->purchaseId ?? 0;
  393. $purchase = PurchaseClass::getById($purchaseId, true);
  394. $pShopId = $purchase->shopId;
  395. $pShop = ShopClass::getLockById($pShopId);
  396. $pShop->cgUnSend -= 1;
  397. $pShop->cgSending += 1;
  398. $pShop->save();
  399. //订单完成
  400. self::finish($order, $shop);
  401. OrderSendClass::withoutSend($order);
  402. return OrderClass::TOTAL_FLOW;
  403. }
  404. //确认送达 ssh 2021.3.4
  405. public static function reach($order)
  406. {
  407. if ($order->status != OrderClass::ORDER_STATUS_SENDING) {
  408. util::fail('订单没有在配送状态');
  409. }
  410. //第三方配送的变更第三方订单状态
  411. if (isset($order->sendType) && $order->sendType == OrderClass::SEND_TYPE_THIRD) {
  412. $express = OrderExpressClass::getByCondition(['orderSn' => $order->orderSn], true);
  413. if (!empty($express)) {
  414. $express->status = OrderExpressClass::STATUS_COMPLETE;
  415. $express->save();
  416. }
  417. }
  418. $shopId = $order->shopId ?? 0;
  419. $shop = ShopClass::getLockById($shopId);
  420. if (empty($shop)) {
  421. util::fail('没有找到门店4');
  422. }
  423. //订单完成
  424. self::finish($order, $shop);
  425. OrderSendClass::reach($order);
  426. //是否微信通知
  427. $noNeedWxNotice = isset(Yii::$app->params['noNeedWxNotice']) ? Yii::$app->params['noNeedWxNotice'] : 0;
  428. if ($noNeedWxNotice == 0) {
  429. //微信通知
  430. WxMessageClass::reachInform($shop, $order);
  431. }
  432. }
  433. //订单完成
  434. public static function finish($order, $shop)
  435. {
  436. //销售单完成
  437. $order = OrderClass::complete($order, $shop);
  438. //采购单完成
  439. $purchaseId = $order->purchaseId ?? 0;
  440. $purchase = PurchaseClass::getById($purchaseId, true);
  441. PurchaseClass::complete($purchase);
  442. }
  443. //获取订单信息 ssh 2019.11.28
  444. public static function getOrderList($where)
  445. {
  446. $data = self::getList('*', $where, 'addTime DESC');
  447. if (isset($data['list']) == false || empty($data['list'])) {
  448. return $data;
  449. }
  450. $orderSns = array_column($data['list'], 'orderSn');
  451. //获取订单的商品信息
  452. $goodsList = OrderGoodsService::getGoodsListByOrderSns($orderSns);
  453. //获取订花人的用户信息
  454. $orderUserIds = array_column($data['list'], 'userId');
  455. $orderUserIds = array_unique($orderUserIds);
  456. $userInfoList = UserService::getUserByIds($orderUserIds);
  457. $sjIdList = array_column($data['list'], 'sjId');
  458. $sjIdList = array_filter(array_unique($sjIdList));
  459. $merchantList = MerchantService::getByIds($sjIdList, null, 'id');
  460. $periodData = [0 => '上午', 1 => '下午', 2 => '晚上'];
  461. foreach ($data['list'] as $key => $val) {
  462. $id = $val['id'];
  463. $userId = $val['userId'];
  464. $data['list'][$key]['goodsInfoList'] = isset($goodsList[$id]) ? $goodsList[$id] : [];
  465. $data['list'][$key]['bookAvatar'] = isset($userInfoList[$userId]['smallAvatarUrl']) ? $userInfoList[$userId]['smallAvatarUrl'] : '';
  466. $sjId = $val['sjId'];
  467. $data['list'][$key]['name'] = isset($merchantList[$sjId]['name']) ? $merchantList[$sjId]['name'] : '';
  468. $data['list'][$key]['reachTime'] = OrderClass::getReachTime($val);
  469. $requireReachTime = '';
  470. if ($val['reachDate'] != '0000-00-00') {
  471. $reachPeriodId = $val['reachPeriod'];
  472. $reachPeriod = isset($periodData[$reachPeriodId]) ? $periodData[$reachPeriodId] : '上午';
  473. $requireReachTime = date("m-d", strtotime($val['reachDate'])) . ' ' . $reachPeriod;
  474. }
  475. $data['list'][$key]['requireReachTime'] = $requireReachTime;
  476. }
  477. return $data;
  478. }
  479. //取客户最近若干条订单 ssh 2019.11.30
  480. public static function getUserOrder($userId, $limit = 10, $order = null, $with = null)
  481. {
  482. $where = ['userId' => $userId, 'payStatus' => 1];
  483. return self::getLimitList('*', $where, $limit, $order, $with);
  484. }
  485. //支付前验证订单 ssh 2019.12.6
  486. public static function checkBeforePay($order, $userId)
  487. {
  488. $now = time();
  489. if (empty($order)) {
  490. util::fail('订单无效');
  491. }
  492. if (isset($order['userId']) && $order['userId'] != $userId) {
  493. util::fail('非法访问');
  494. }
  495. if ($order['status'] != 0) {
  496. util::fail('订单不是待付款状态');
  497. }
  498. if (isset($order['deadline']) && $now > $order['deadline']) {
  499. util::fail('订单已过期');
  500. }
  501. if (ceil($order['actPrice']) <= 0) {
  502. util::fail('订单金额有问题');
  503. }
  504. if ($order['payStatus'] == 1) {
  505. util::fail('您已经付过了');
  506. }
  507. }
  508. //商家验证是否有效 ssh 2019.12.15
  509. public static function valid($order, $shopId)
  510. {
  511. $now = date("Y-m-d H:i:s");
  512. if (empty($order)) {
  513. util::fail('订单无效');
  514. }
  515. if ($order['shopId'] != $shopId) {
  516. util::fail('不是您的订单');
  517. }
  518. if (isset($order['status']) && $order['status'] == OrderClass::ORDER_STATUS_UN_PAY) {
  519. if (isset($order['deadline']) && $now > $order['deadline']) {
  520. util::fail('订单已过期');
  521. }
  522. }
  523. }
  524. //点评 ssh 2019.12.16
  525. public static function comment($data)
  526. {
  527. return OrderClass::comment($data);
  528. }
  529. //订单归类
  530. public static function classify($id, $categoryId, $usageId)
  531. {
  532. $connection = Yii::$app->db;
  533. $transaction = $connection->beginTransaction();
  534. try {
  535. $order = OrderClass::getById($id, true);
  536. if ($order->payStatus != 1) {
  537. util::fail('订单还未付款');
  538. }
  539. if ($order->classify == 1) {
  540. util::fail('已经归类过了');
  541. }
  542. $order->categoryId = $categoryId;
  543. $order->usageId = $usageId;
  544. $order->classify = 1;
  545. $order->save();
  546. //设置流水的分类和用途并分配资金
  547. $setData = [
  548. 'sjId' => $order->sjId,
  549. 'capitalId' => $order->capitalId,
  550. 'time' => $order->payTime,
  551. 'usageId' => $order->usageId,
  552. 'categoryId' => $order->categoryId,
  553. 'amount' => $order->actPrice,
  554. 'payWay' => $order->payWay,
  555. ];
  556. SjCapitalService::orderClassify($setData);
  557. $transaction->commit();
  558. } catch (Exception $e) {
  559. $transaction->rollBack();
  560. util::fail('没有归类成功');
  561. }
  562. }
  563. //更新配送单 ssh 2019.12.17
  564. public static function updateSheet($order, $data)
  565. {
  566. $id = $order['id'];
  567. if ($order['status'] != OrderClass::ORDER_STATUS_UN_SEND) {
  568. util::fail('订单当前不是待配送状态');
  569. }
  570. $data['deliver'] = 1;
  571. OrderClass::updateById($id, $data);
  572. }
  573. //订单详情 ssh 2020.1.2
  574. public static function getOrderById($id)
  575. {
  576. return OrderClass::getOrderById($id);
  577. }
  578. //查看订单详情 ssh 2020.1.4
  579. public static function getOrderBySn($orderSn)
  580. {
  581. return OrderClass::getOrderBySn($orderSn);
  582. }
  583. //计算优惠 ssh 2020.3.9
  584. public static function getDiscountPrice($params)
  585. {
  586. $couponId = isset($params['couponId']) ? $params['couponId'] : 0;
  587. $userId = isset($params['userId']) ? $params['userId'] : 0;
  588. $price = isset($params['price']) ? $params['price'] : 0;
  589. $sourceType = isset($params['sourceType']) ? $params['sourceType'] : 0;
  590. $discountAmount = 0;
  591. $discountType = 0;
  592. $discountPrice = $price;
  593. //使用优惠券
  594. if (!empty($couponId)) {
  595. $discountAmount = CouponService::checkBeforeUse($couponId, $price, $userId);
  596. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  597. $discountType = 1;
  598. } else {
  599. //使用会员
  600. // $asset = UserAssetClass::getByUserId($userId);
  601. // if (isset($asset['member']) && $asset['member'] > 0) {
  602. // $currentDiscount = $asset['discount'];
  603. // $currentPrice = $price * $currentDiscount;
  604. // $newPrice = substr(sprintf("%.3f", $currentPrice), 0, -1);
  605. // $discountType = 2;
  606. // $discountAmount = stringUtil::calcSub($price, $newPrice);
  607. // $discountPrice = $newPrice;
  608. // }
  609. }
  610. //如果没有优惠券和会员优惠,并且是朋友圈收款,则进行随机优惠 ssh 2019.9.12
  611. if ($discountPrice == $price && $sourceType == 3) {
  612. $discountAmount = OrderClass::getRandDiscount($price);
  613. //开发和测试环境直接随机优惠
  614. $env = getenv('YII_ENV');
  615. if ($env == 'local' || $env == 'test') {
  616. $discountAmount = 0.01;
  617. }
  618. $discountPrice = stringUtil::calcSub($price, $discountAmount);
  619. $discountType = 3;
  620. }
  621. $discountType = $discountAmount <= 0 ? 0 : $discountType;
  622. $discountPrice = $discountPrice <= 0 ? 0.01 : $discountPrice;
  623. return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount];
  624. }
  625. //获取订单详情 ssh 2021.1.21
  626. public static function getOrderInfo($id, $showButton = false, $showItem = false, $showExpress = false, $isList = false)
  627. {
  628. $order = OrderClass::getById($id, true);
  629. if (empty($order)) {
  630. util::fail('没有找到订单');
  631. }
  632. $info = $order->attributes;
  633. $respond = OrderClass::groupOrder([$info], $showButton, $showItem, $showExpress, $isList);
  634. return current($respond);
  635. }
  636. //订单失效后的操作 ssh 2021.5.14
  637. public static function expire($order)
  638. {
  639. $purchaseId = $order->purchaseId ?? 0;
  640. $current = PurchaseClass::getById($purchaseId, true);
  641. if (empty($current)) {
  642. util::fail('没有找到采购单');
  643. }
  644. PurchaseService::expire($current);
  645. return true;
  646. }
  647. //支付成功后的流程 ssh 2021.5.14
  648. //$isPurchase 是否零售端采购支付,如果是会引起销售方余额变动
  649. public static function payAfter($order, $payWay, $isPurchase = false)
  650. {
  651. if (isset($order->status) == false) {
  652. util::fail('没有订单状态');
  653. }
  654. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  655. util::fail('重复操作');
  656. }
  657. if (isset($order->debt) == false) {
  658. util::fail('订单有问题,没有欠款项');
  659. }
  660. $orderId = $order->id;
  661. $orderSn = $order->orderSn ?? '';
  662. $customId = $order->customId;
  663. $custom = CustomClass::getLockById($customId);
  664. if (empty($custom)) {
  665. util::fail('没有找到客户信息');
  666. }
  667. //门店资产变化
  668. $amount = $order->actPrice ?? 0.00;
  669. $realPrice = $order->realPrice ?? 0.00;
  670. $currentShopId = $order->shopId ?? 0;
  671. $book = $order->book ?? 0;
  672. $shop = ShopClass::getLockById($currentShopId);
  673. if (empty($shop)) {
  674. util::fail('没有找到门店5');
  675. }
  676. $mainId = $shop->mainId ?? 0;
  677. $main = MainClass::getLockById($mainId);
  678. if (empty($main)) {
  679. util::fail('没有main信息3');
  680. }
  681. $main->unPayOrder -= 1;
  682. //预订单的待发货订单增加
  683. if ($book == 1) {
  684. $main->unSendOrder += 1;
  685. //预订单统计数量增加
  686. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  687. if (!empty($itemList)) {
  688. $bookData = [];
  689. foreach ($itemList as $currentItem) {
  690. $itemRemark = $currentItem->remark ?? 0;
  691. $hasRemark = !empty($itemRemark) ? 1 : 0;
  692. $bookData[] = [
  693. 'productId' => $currentItem->productId,
  694. 'num' => $currentItem->xhNum ?? 0,
  695. 'lastNum' => $currentItem->xhNum ?? 0,
  696. 'remark' => $itemRemark,
  697. 'hasRemark' => $hasRemark,
  698. ];
  699. }
  700. $staffId = $order->shopAdminId ?? 0;
  701. $staffName = $order->shopAdminName ?? '';
  702. $params = [
  703. 'staffId' => $staffId, 'staffName' => $staffName, 'action' => 'add'
  704. ];
  705. BookItemClass::addBatchItem($bookData, $order, $params);
  706. }
  707. } else {
  708. $main->finishOrder += 1;
  709. }
  710. $main->totalOrderNum += 1;
  711. $currentTotalIncome = bcadd($main->totalIncome, $amount, 2);
  712. $main->totalIncome = $currentTotalIncome;
  713. $totalSale = bcadd($main->totalSale, $amount, 2);
  714. $main->totalSale = $totalSale;
  715. if ($payWay == dict::getDict('payWay', 'debtPay')) {
  716. $currentGathering = bcadd($main->mayGathering, $realPrice, 2);
  717. $main->mayGathering = $currentGathering;
  718. if ($custom->debtNum == 0) {
  719. $main->mayGatheringNum += 1;
  720. }
  721. }
  722. $main->save();
  723. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  724. $shopAdminId = $order->shopAdminId ?? 0;
  725. $shopAdminName = $order->shopAdminName ?? '';
  726. $event = '下单';
  727. if ($isPurchase) {
  728. $event = '客户采购';
  729. if (isset($custom->name) && !empty($custom->name)) {
  730. $event = $custom->name . '采购';
  731. }
  732. }
  733. $sjId = $order->sjId ?? 0;
  734. $shopId = $order->shopId ?? 0;
  735. $capital = [
  736. 'capitalType' => $capitalType,
  737. 'relateId' => $orderId,
  738. 'io' => 1,
  739. 'amount' => $amount,
  740. 'totalIncome' => $currentTotalIncome,
  741. 'payWay' => $payWay,
  742. 'event' => $event,
  743. 'mainId' => $mainId,
  744. 'sjId' => $sjId,
  745. 'shopId' => $shopId,
  746. 'shopAdminId' => $shopAdminId,
  747. 'shopAdminName' => $shopAdminName,
  748. 'mainId' => $mainId,
  749. ];
  750. ShopCapitalClass::addCapital($capital);
  751. //客户采购,会增加余额,会增加收入
  752. if ($isPurchase == true) {
  753. //余额支付和在线支付(微信支付、支付宝支付)会增加余额
  754. if ($order->onlinePay == dict::getDict('onlinePay', 'yes') || $payWay == dict::getDict('payWay', 'balancePay')) {
  755. ShopClass::customKdAddBalance($main, $shop, $realPrice, $order, $capitalType);
  756. }
  757. }
  758. //现金付款,现金余额需要增加
  759. if ($payWay == dict::getDict('payWay', 'cash')) {
  760. $moneyBalance = bcadd($main->money, $realPrice, 2);
  761. $main->money = $moneyBalance;
  762. $main->save();
  763. $moneyEvent = $custom->name . "现金下单" . floatval($realPrice) . "元(单号 {$orderSn})";
  764. $moneyRemark = '';
  765. $change = [
  766. 'relateId' => $orderId,
  767. 'amount' => $realPrice,
  768. 'balance' => $moneyBalance,
  769. 'io' => 1,
  770. 'mainId' => $mainId,
  771. 'capitalType' => $capitalType,
  772. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  773. 'event' => $moneyEvent,
  774. 'remark' => $moneyRemark,
  775. ];
  776. ShopMoneyChangeClass::addData($change);
  777. }
  778. //更新订单基础状态和支付状态
  779. $order->payWay = $payWay;
  780. if ($book == 1) {
  781. $order->status = OrderClass::ORDER_STATUS_UN_SEND;
  782. } else {
  783. $order->status = OrderClass::ORDER_STATUS_UN_SEND;
  784. }
  785. if ($payWay == dict::getDict('payWay', 'debtPay')) {
  786. if (isset($order->actPrice) && $order->actPrice == 0) {
  787. $order->debt = OrderClass::DEBT_NO;
  788. } else {
  789. $order->debt = OrderClass::DEBT_YES;
  790. }
  791. $order->remainDebtPrice = $order->orderPrice ?? 0;
  792. $order->debtPrice = $order->orderPrice ?? 0;
  793. }
  794. $order->payStatus = 1;
  795. if (!isset($order->payTime) || $order->payTime == '0000-00-00 00:00:00') {
  796. $order->payTime = date("Y-m-d H:i:s");
  797. }
  798. $order->save();
  799. //新订单进入队列,用于花材的销量统计
  800. OrderClass::newOrderAddQueue($order);
  801. //员工业绩、总业绩增加
  802. $shopAdminId = $order->shopAdminId ?? 0;
  803. if (!empty($shopAdminId)) {
  804. $shopAdmin = ShopAdminClass::getById($shopAdminId, true);
  805. $shopAdmin->kdAmount = bcadd($shopAdmin->kdAmount, $amount, 2);
  806. $shopAdmin->save();
  807. $yjAmount = $order->orderPrice ?? 0;
  808. StatYjClass::addYj($main, $shop, $shopAdminId, $yjAmount);
  809. }
  810. //欠款
  811. if ($payWay == dict::getDict('payWay', 'debtPay')) {
  812. CustomClass::cgDebtAmountAdd($custom, $order);
  813. }
  814. $custom->buyNum += 1;
  815. $custom->buyAmount = bcadd($custom->buyAmount, $amount, 2);
  816. $custom->visitTime = date("Y-m-d H:i:s");
  817. $custom->save();
  818. //收入统计
  819. StatIncomeClass::updateOrInsert($main, $shop, $amount);
  820. //各渠道收入金额统计
  821. StatKdClass::replace($shop, $amount, $payWay);
  822. //今日订单+1
  823. StatOrderClass::updateOrInsert($main, $shop);
  824. //销售统计
  825. StatSaleClass::replace($main, $shop, $amount);
  826. //客户采购统计
  827. StatKhCgClass::replace($order, $amount);
  828. //当天同个人同地址的订单汇总
  829. OrderClass::updateSameTimeIds($order, substr($order->payTime, 0, 10));
  830. }
  831. //发起退款
  832. public static function refund($id, $post)
  833. {
  834. $order = OrderClass::getLockById($id);
  835. if (empty($order)) {
  836. util::fail('没有找到订单');
  837. }
  838. if (isset($order->clearId) && !empty($order->clearId)) {
  839. util::fail('已经结账,不能再售后');
  840. }
  841. if ($order->debt != 1 && $order->addTime < '2023-10-17 00:00:00') {
  842. util::fail('10月17日前订单无法申请退款');
  843. }
  844. //供货商销售单退款
  845. $saleRefund = RefundOrderService::addRefund($post, $order);
  846. $saleRefundId = $saleRefund->id;
  847. $purchaseId = $order->purchaseId;
  848. $cg = PurchaseClass::getLockById($purchaseId);
  849. if (empty($cg)) {
  850. util::fail('没有找到采购订单');
  851. }
  852. $mainId = $cg->mainId ?? 0;
  853. //花材列表结构
  854. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  855. //供货商的花材信息转换成零售店的花材信息
  856. $ghsProduct = $post['product'] ?? '';
  857. $ghsProductIndex = array_column($ghsProduct, null, 'productId');
  858. $ids = array_column($ghsProduct, 'productId');
  859. $ghsProductInfo = ProductClass::getByIds($ids, null, 'itemId');
  860. $ptItemIds = array_keys($ghsProductInfo);
  861. $ptItemIds = array_unique(array_filter($ptItemIds));
  862. $hdProductInfo = ProductClass::getAllByCondition(['mainId' => $mainId, 'itemId' => ['in', $ptItemIds]], null, '*', 'itemId');
  863. $newProduct = [];
  864. foreach ($hdProductInfo as $ptItemId => $hdInfo) {
  865. $ghsItemId = $ghsProductInfo[$ptItemId]['id'] ?? 0;
  866. $current = $ghsProductIndex[$ghsItemId] ?? [];
  867. $hdItemId = $hdInfo['id'] ?? 0;
  868. $cost = $hdInfo['cost'] ?? 0;
  869. $name = $hdInfo['name'] ?? '';
  870. $cover = $hdInfo['cover'] ?? '';
  871. $ratio = $hdInfo['ratio'] ?? 20;
  872. if (empty($current)) {
  873. util::fail('没有找到花材');
  874. }
  875. $current['productId'] = $hdItemId;
  876. $current['ptItemId'] = $ptItemId;
  877. $current['cost'] = $cost;
  878. $current['name'] = $name;
  879. $current['cover'] = $cover;
  880. $current['ratio'] = $ratio;
  881. $newProduct[] = $current;
  882. }
  883. $refundType = $post['refundType'] ?? 0;
  884. $price = $post['price'] ?? 0;
  885. $remark = $post['remark'] ?? '';
  886. $cause = $post['cause'] ?? 0;
  887. $refundSendCost = isset($post['refundSendCost']) && is_numeric($post['refundSendCost']) ? $post['refundSendCost'] : 0;
  888. $refundPackCost = isset($post['refundPackCost']) && is_numeric($post['refundPackCost']) ? $post['refundPackCost'] : 0;
  889. $cgData = [
  890. 'product' => $newProduct,
  891. 'refundType' => $refundType,
  892. 'price' => $price,
  893. 'remark' => $remark,
  894. 'cause' => $cause,
  895. 'refundSendCost' => $refundSendCost,
  896. 'refundPackCost' => $refundPackCost,
  897. ];
  898. $cgRefund = CgRefundService::addRefund($cgData, $cg);
  899. $cgRefundId = $cgRefund->id;
  900. $saleRefund->cgRefundId = $cgRefundId;
  901. $saleRefund->save();
  902. $cgRefund->saleRefundId = $saleRefundId;
  903. $cgRefund->save();
  904. return $saleRefund;
  905. }
  906. }