OrderClass.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. <?php
  2. namespace bizGhs\order\classes;
  3. use biz\shop\classes\ShopAdminClass;
  4. use biz\shop\classes\ShopCapitalClass;
  5. use biz\shop\classes\ShopClass;
  6. use biz\shop\classes\ShopExtClass;
  7. use biz\shop\models\Shop;
  8. use biz\sj\classes\SjClass;
  9. use biz\stat\classes\StatItemClass;
  10. use biz\stat\classes\StatKdClass;
  11. use biz\stat\classes\StatKhCgClass;
  12. use biz\stat\classes\StatOrderCountClass;
  13. use biz\stat\classes\StatOutClass;
  14. use biz\stat\classes\StatRefundClass;
  15. use biz\stat\classes\StatSaleClass;
  16. use biz\stock\classes\StockClass;
  17. use bizGhs\custom\models\Custom;
  18. use bizGhs\express\services\DadaExpressServices;
  19. use bizGhs\order\models\Order;
  20. use bizGhs\order\traits\OrderTrait;
  21. use bizGhs\product\classes\ProductClass;
  22. use bizGhs\admin\classes\AdminClass;
  23. use bizGhs\custom\classes\CustomClass;
  24. use bizGhs\shop\classes\MainClass;
  25. use bizGhs\stat\classes\StatYjClass;
  26. use bizGhs\stock\classes\StockRecordClass;
  27. use bizGhs\stock\services\StockRecordService;
  28. use bizHd\purchase\classes\PurchaseClass;
  29. use bizHd\stat\classes\StatIncomeClass;
  30. use bizHd\stat\classes\StatOrderClass;
  31. use common\components\dict;
  32. use common\components\httpUtil;
  33. use common\components\imgUtil;
  34. use common\components\orderSn;
  35. use common\components\printUtil;
  36. use common\components\stringUtil;
  37. use common\components\util;
  38. use bizGhs\base\classes\BaseClass;
  39. use Yii;
  40. class OrderClass extends BaseClass
  41. {
  42. use OrderTrait;
  43. public static $baseFile = '\bizGhs\order\models\Order';
  44. //已取消
  45. const ORDER_STATUS_CANCEL = 5;
  46. //已完成
  47. const ORDER_STATUS_COMPLETE = 4;
  48. //配送中
  49. const ORDER_STATUS_SENDING = 3;
  50. //待配送
  51. const ORDER_STATUS_UN_SEND = 2;
  52. //待付款,待确认
  53. const ORDER_STATUS_UN_PAY = 1;
  54. //未确认
  55. const SEND_TYPE_UNKNOWN = 1;
  56. //自取
  57. const SEND_TYPE_NO = 2;
  58. //自己送
  59. const SEND_TYPE_MYSELF = 3;
  60. //快递送
  61. const SEND_TYPE_THIRD = 4;
  62. public static $sendTypeMap = [
  63. self::SEND_TYPE_UNKNOWN => '未确认',
  64. self::SEND_TYPE_NO => '自取',
  65. self::SEND_TYPE_MYSELF => '商家配送',
  66. self::SEND_TYPE_THIRD => '快递配送',
  67. ];
  68. //待付款
  69. const PAY_STATUS_UN_PAY = 0;
  70. //已付款
  71. const PAY_STATUS_HAS_PAY = 1;
  72. //待配送
  73. const SEND_STATUS_UN_SEND = 1;
  74. //配送中
  75. const SEND_STATUS_SENDING = 2;
  76. //配送完成
  77. const SEND_STATUS_COMPLETE = 3;
  78. //订单总流程数
  79. const TOTAL_FLOW = 3;
  80. //清算方式
  81. //欠款
  82. const CLEAR_DEBT = 1;
  83. //已付款
  84. const CLEAR_HAS_GATHERING = 2;
  85. //没有欠款
  86. const DEBT_NO = 0;
  87. //欠款
  88. const DEBT_YES = 1;
  89. //正常订单
  90. const REFUND_NO = 1;
  91. //已退款订单
  92. const REFUND_YES = 2;
  93. public static $refundMap = [
  94. self::REFUND_NO => '',
  95. self::REFUND_YES => '有退款',
  96. ];
  97. public static function arrival($order, $data)
  98. {
  99. if ($order->stockChange == 1) {
  100. util::fail('您已操作过了');
  101. }
  102. $sjId = $order->sjId ?? 0;
  103. $shopId = $order->shopId ?? 0;
  104. $mainId = $order->mainId ?? 0;
  105. //product [{"productId":"3674","num":0,"price":1}]
  106. $product = $data['product'] ?? '';
  107. if (empty($product)) {
  108. util::fail('没有花材?');
  109. }
  110. $ids = [];
  111. $format = [];
  112. foreach ($product as $val) {
  113. $productId = $val['productId'] ?? 0;
  114. $ids[] = $productId;
  115. $format[$productId] = $val;
  116. }
  117. $info = ProductClass::getByIds($ids, null, 'id');
  118. $orderSn = $order->orderSn ?? '';
  119. $customName = $order->customName ?? '';
  120. $list = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  121. if (empty($list)) {
  122. util::fail('没有找到订单的花材');
  123. }
  124. $itemPrice = 0;
  125. $weight = 0;
  126. foreach ($list as $key => $val) {
  127. $productId = $val->productId ?? 0;
  128. $num = $format[$productId]['num'] ?? 0;
  129. $unitPrice = $format[$productId]['price'] ?? 0;
  130. $price = bcmul($unitPrice, $num, 2);
  131. $name = $val->name ?? '';
  132. $xhUnitName = $val->xhUnitName ?? '';
  133. $xhPreNum = $val->xhPreNum ?? 0;
  134. if ($unitPrice <= 0) {
  135. util::fail('花材金额需要大于0');
  136. }
  137. if ($num > $xhPreNum) {
  138. util::fail($name . "数量不能超过{$xhPreNum}{$xhUnitName}");
  139. }
  140. $val->cost = $unitPrice;
  141. $val->unitPrice = $unitPrice;
  142. $val->price = $price;
  143. $val->bigNum = $num;
  144. $val->num = $num;
  145. $val->xhNum = $num;
  146. $val->xhUnitPrice = $unitPrice;
  147. $val->xhPrice = $price;
  148. $val->save();
  149. if ($val->xhPreNum > $num) {
  150. //客户下单退货数增加
  151. $subNum = bcsub($val->xhPreNum, $num);
  152. StatKhCgClass::ghsBookOrderRefundNum($order, $subNum);
  153. //花材销量榜减少
  154. StatItemClass::ghsBookOrderRefundNum($order, $val, $subNum);
  155. }
  156. $itemPrice = bcadd($itemPrice, $price, 2);
  157. $productWeight = $info[$productId]['weight'] ?? 0;
  158. $currentWeight = bcmul($productWeight, $num, 2);
  159. $weight = bcadd($currentWeight, $weight, 2);
  160. //减少库存
  161. $unitType = $val->xhUnitType ?? 0;
  162. $bigNum = 0;
  163. $smallNum = 0;
  164. if ($unitType == dict::getDict('unitType', 'big')) {
  165. $bigNum = $num;
  166. $itemNum = $num;
  167. } else {
  168. $smallNum = $num;
  169. $itemNum = bcdiv($num, $val->ratio, 2);
  170. }
  171. $checkStock = true;
  172. $stockInfo = ProductClass::decreaseStock($productId, $bigNum, $smallNum, $checkStock);
  173. $recordData = [];
  174. $recordData['sjId'] = $sjId;
  175. $recordData['shopId'] = $shopId;
  176. $recordData['mainId'] = $mainId;
  177. $recordData['itemId'] = $val->itemId;
  178. $recordData['itemNum'] = $itemNum;
  179. $recordData['oldStock'] = $stockInfo['oldStock'];
  180. $recordData['newStock'] = $stockInfo['newStock'];
  181. $recordData['productId'] = $productId;
  182. $recordData['orderSn'] = $orderSn;
  183. $recordData['relateName'] = $customName;
  184. $recordData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  185. StockRecordClass::addSellStockOrderRecord($recordData);
  186. }
  187. $order->itemPrice = $itemPrice;
  188. $sendCost = 0;
  189. $currentPrice = bcadd($itemPrice, $sendCost, 2);
  190. $shopId = $order->shopId ?? 0;
  191. $shop = ShopClass::getLockById($shopId);
  192. if (empty($shop)) {
  193. util::fail('没有找到门店');
  194. }
  195. $mainId = $shop->mainId ?? 0;
  196. $main = MainClass::getLockById($mainId);
  197. if (empty($main)) {
  198. util::fail('没有main信息');
  199. }
  200. $main->unSendOrder -= 1;
  201. $main->finishOrder += 1;
  202. $main->save();
  203. $kiloFee = $shop->kiloFee ?? 0;
  204. $miniKilo = $shop->miniKilo ?? 0;
  205. $calcWeight = $weight > $miniKilo ? $weight : $miniKilo;
  206. $allKiloFee = bcmul($calcWeight, $kiloFee, 2);
  207. $currentPrice = bcadd($currentPrice, $allKiloFee, 2);
  208. $order->prePrice = $currentPrice;
  209. $order->orderPrice = $currentPrice;
  210. $order->actPrice = $currentPrice;
  211. $order->realPrice = $currentPrice;
  212. $order->stockChange = 1;
  213. $order->status = self::ORDER_STATUS_COMPLETE;
  214. $order->save();
  215. $bookPrice = $order->bookPrice ?? 0;
  216. $mainId = $shop->mainId ?? 0;
  217. $sjId = $shop->sjId ?? 0;
  218. $customId = $order->customId ?? 0;
  219. $relateOrderSn = $order->orderSn ?? '';
  220. $shopAdminId = $data['shopAdminId'] ?? 0;
  221. $shopAdminName = $data['shopAdminName'] ?? '';
  222. $refund = null;
  223. if ($bookPrice != $currentPrice) {
  224. if ($bookPrice > $currentPrice) {
  225. $refundPrice = bcsub($bookPrice, $currentPrice, 2);
  226. $order->refundPrice = $refundPrice;
  227. $order->tkPrice = bcadd($order->tkPrice, $refundPrice, 2);
  228. $order->refund = self::REFUND_YES;
  229. $order->save();
  230. $orderSn = orderSn::getGhsRefundSn();
  231. $book = 1;
  232. $refundData = [
  233. 'mainId' => $mainId,
  234. 'shopId' => $shopId,
  235. 'sjId' => $sjId,
  236. 'customId' => $customId,
  237. 'orderSn' => $orderSn,
  238. 'relateOrderSn' => $relateOrderSn,
  239. 'book' => $book,
  240. 'status' => RefundOrderClass::STATUS_UN_COMPLETE,
  241. 'shopAdminId' => $shopAdminId,
  242. 'shopAdminName' => $shopAdminName,
  243. 'refundPrice' => $refundPrice,
  244. 'refundType' => RefundOrderClass::REFUND_TYPE_MONEY,
  245. ];
  246. //退款
  247. $refund = RefundOrderClass::add($refundData, true);
  248. //余额减少
  249. ShopClass::ghsBookOrderRefundReduceBalance($main, $shop, $refund, $refundPrice);
  250. //总支出增加
  251. $currentTotalExpend = bcadd($main->totalExpend, $refundPrice, 2);
  252. $main->totalExpend = $currentTotalExpend;
  253. $totalRefund = bcadd($main->totalRefund, $refundPrice, 2);
  254. $main->totalRefund = $totalRefund;
  255. $main->save();
  256. //支出流水
  257. $thisType = dict::getDict('capitalType', 'ghsBookRefund', 'id');
  258. $sjId = $order->sjId ?? 0;
  259. $event = "预订单多付退给{$customName}";
  260. $capitalData = [
  261. 'capitalType' => $thisType,
  262. 'io' => 0,
  263. 'totalExpend' => $currentTotalExpend,
  264. 'payWay' => 0,
  265. 'amount' => $refundPrice,
  266. 'sjId' => $sjId,
  267. 'shopId' => $shopId,
  268. 'event' => $event,
  269. 'mainId' => $mainId,
  270. ];
  271. ShopCapitalClass::addCapital($capitalData);
  272. //支出统计
  273. StatOutClass::updateOrInsert($main, $shop, $refundPrice);
  274. //退款统计
  275. $payWay = $order->payWay ?? 0;
  276. StatRefundClass::replace($main, $shop, $payWay, $refundPrice);
  277. //供货商预订单退款给客户,客户采购金额减少
  278. StatKhCgClass::ghsBookOrderChange(false, $order, $refundPrice);
  279. } else {
  280. //欠款
  281. $currentDebt = bcsub($currentPrice, $bookPrice, 2);
  282. $order->debtPrice = $currentDebt;
  283. $order->remainDebtPrice = $currentDebt;
  284. $order->debt = OrderClass::DEBT_YES;
  285. $order->save();
  286. $currentTotalIncome = bcadd($main->totalIncome, $currentDebt, 2);
  287. $main->totalIncome = $currentTotalIncome;
  288. $main->save();
  289. $payWay = dict::getDict('payWay', 'debtPay');
  290. $custom = CustomClass::getLockById($customId);
  291. if (empty($custom)) {
  292. util::fail('没有找到客户');
  293. }
  294. CustomClass::cgDebtAmountAdd($custom, $order, true);
  295. //收入增加
  296. StatIncomeClass::updateOrInsert($main, $shop, $currentDebt);
  297. //收入流水增加
  298. $orderId = $order->id;
  299. $capitalType = dict::getDict('capitalType', 'ghsBookOrderGetBack', 'id');
  300. $shopAdminId = $order->shopAdminId ?? 0;
  301. $shopAdminName = $order->shopAdminName ?? '';
  302. $event = "预订单 {$orderSn} 差价补款";
  303. $mainId = $shop->mainId ?? 0;
  304. $sjId = $order->sjId ?? 0;
  305. $shopId = $order->shopId ?? 0;
  306. $capital = [
  307. 'capitalType' => $capitalType,
  308. 'relateId' => $orderId,
  309. 'io' => 1,
  310. 'amount' => $currentDebt,
  311. 'totalIncome' => $currentTotalIncome,
  312. 'payWay' => $payWay,
  313. 'event' => $event,
  314. 'mainId' => $mainId,
  315. 'sjId' => $sjId,
  316. 'shopId' => $shopId,
  317. 'shopAdminId' => $shopAdminId,
  318. 'shopAdminName' => $shopAdminName,
  319. ];
  320. ShopCapitalClass::addCapital($capital);
  321. //各渠道收入金额统计
  322. StatKdClass::replace($shop, $currentDebt, $payWay);
  323. //每天销售收入统计
  324. StatSaleClass::replace($main, $shop, $currentDebt);
  325. //预订单,客户补款给供货商,客户采购金额增加
  326. StatKhCgClass::ghsBookOrderChange(true, $order, $currentDebt);
  327. }
  328. }
  329. return ['kiloFee' => $kiloFee, 'refund' => $refund, 'miniKilo' => $miniKilo];
  330. }
  331. //添加订单 ssh 2019.12.5
  332. public static function addOrder($data)
  333. {
  334. $orderSn = orderSn::getGhsOrderSn();
  335. $data['orderSn'] = $orderSn;
  336. $data['payStatus'] = self::PAY_STATUS_UN_PAY;
  337. $data['customId'] = isset($data['customId']) && !empty($data['customId']) ? $data['customId'] : 0;
  338. $currentShopId = $data['shopId'] ?? 0;
  339. $shop = ShopClass::getLockById($currentShopId);
  340. if (empty($shop)) {
  341. util::fail('没有找到门店');
  342. }
  343. $mainId = $shop->mainId ?? 0;
  344. $main = MainClass::getLockById($mainId);
  345. if (empty($main)) {
  346. util::fail('没有main信息');
  347. }
  348. //花材
  349. $product = $data['product'];
  350. $book = $data['book'] ?? 0;
  351. $stockMayChange = true;
  352. $data['stockChange'] = 1;
  353. if ($book == 1) {
  354. $stockMayChange = false;
  355. $data['stockChange'] = 0;
  356. }
  357. $respond = OrderItemClass::replaceItem($orderSn, $product, $data, $stockMayChange);
  358. $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
  359. $data['sendCost'] = $sendCost;
  360. $data['bigNum'] = $respond['bigNum'] ?? 0;
  361. $data['smallNum'] = $respond['smallNum'] ?? 0;
  362. $data['itemNum'] = $respond['totalItemNum'] ?? 0;
  363. //默认订单要打单
  364. $data['needPrint'] = $data['needPrint'] ?? dict::getDict('needPrint', 'need');
  365. $weight = $respond['weight'] ?? 0;
  366. $data['weight'] = $weight;
  367. $itemPrice = $respond['price'] ?? 0;
  368. $data['itemPrice'] = $itemPrice;
  369. $currentPrice = bcadd($itemPrice, $sendCost, 2);
  370. //预订单需要增加服务费
  371. if ($book == 1) {
  372. $kiloFee = $shop->kiloFee ?? 0;
  373. $miniKilo = $shop->miniKilo ?? 0;
  374. $calcWeight = $weight > $miniKilo ? $weight : $miniKilo;
  375. $allKiloFee = bcmul($calcWeight, $kiloFee, 2);
  376. $currentPrice = bcadd($currentPrice, $allKiloFee, 2);
  377. $data['allKiloFee'] = $allKiloFee;
  378. $data['kiloFee'] = $kiloFee;
  379. $data['miniKilo'] = $miniKilo;
  380. }
  381. $data['prePrice'] = $currentPrice;
  382. $actPrice = $currentPrice;
  383. $realPrice = $currentPrice;
  384. //商家自己开单时
  385. if (isset($data['modifyPrice'])) {
  386. $modifyPrice = $data['modifyPrice'] ?? 0;
  387. if ($modifyPrice <= 0) {
  388. util::fail('收款金额必须大于0');
  389. }
  390. if ($modifyPrice > $currentPrice) {
  391. //人工和资材费用
  392. $data['labourCost'] = bcsub($modifyPrice, $currentPrice, 2);
  393. }
  394. if ($modifyPrice < $currentPrice) {
  395. //优惠金额
  396. $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
  397. $data['discountType'] = dict::getDict('discountType', 'discount');
  398. }
  399. $actPrice = $modifyPrice;
  400. $realPrice = $modifyPrice;
  401. }
  402. //客户采购时使用红包时
  403. if (isset($data['discountType']) && $data['discountType'] == dict::getDict('discountType', 'hb')) {
  404. $discountAmount = $data['discountAmount'] ?? 0;
  405. $currentDiscountPrice = bcsub($currentPrice, $discountAmount, 2);
  406. $actPrice = $currentDiscountPrice;
  407. $realPrice = $currentDiscountPrice;
  408. }
  409. $data['actPrice'] = $actPrice;
  410. $data['bookPrice'] = $actPrice;
  411. $data['orderPrice'] = $actPrice;
  412. $data['realPrice'] = $realPrice;
  413. $data['sendTimeWant'] = isset($data['sendTimeWant']) && !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
  414. //订单有效期
  415. $validTime = dict::getDict('order_pay_has_time');
  416. $current = time() + $validTime;
  417. $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
  418. $data['status'] = self::ORDER_STATUS_UN_PAY;
  419. $main->totalOrder += 1;
  420. $main->unPayOrder += 1;
  421. $main->save();
  422. //将当天日期+花店每天的总订单数作为编号
  423. $data['sendNum'] = date("j") . StatOrderCountClass::addOrder($shop, $main);
  424. //订单有效期
  425. $currentTime = time();
  426. $validTime = dict::getDict('order_pay_has_time');
  427. $current = $currentTime + $validTime;
  428. $data['deadline'] = isset($data['deadline']) ? $data['deadline'] : date("Y-m-d H:i:s", $current);
  429. //订单自动记为欠款单时间
  430. $localConfirmTime = dict::getDict('local_gys_kd_auto_set_debt_time');
  431. $currentSet = $currentTime + $localConfirmTime;
  432. $data['autoSetTime'] = $data['autoSetTime'] ?? date("Y-m-d H:i:s", $currentSet);
  433. $return = self::add($data, true);
  434. return $return;
  435. }
  436. //修改订单 ssh 2019.12.5
  437. public static function updateOrder($order, $data)
  438. {
  439. $sjId = $order->sjId ?? 0;
  440. $orderSn = $order->orderSn ?? 0;
  441. $currentShopId = $order->shopId ?? 0;
  442. $data['payStatus'] = self::PAY_STATUS_UN_PAY;
  443. //花材列表
  444. $product = $data['product'];
  445. //删除花材再添加
  446. OrderItemClass::deleteByCondition(['orderSn' => $orderSn]);
  447. $respond = OrderItemClass::replaceItem($orderSn, $product, $data);
  448. $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
  449. $data['sendCost'] = $sendCost;
  450. $data['bigNum'] = $respond['bigNum'] ?? 0;
  451. $data['smallNum'] = $respond['smallNum'] ?? 0;
  452. $price = $respond['price'] ?? 0;
  453. $currentPrice = bcadd($price, $sendCost, 2);
  454. $data['prePrice'] = $currentPrice;
  455. if (isset($data['modifyPrice'])) {
  456. //商家打折,修改金额
  457. $modifyPrice = $data['modifyPrice'] ?? 0;
  458. if ($modifyPrice > $currentPrice) {
  459. util::fail('实收金额大于订单总金额');
  460. }
  461. if ($modifyPrice < $currentPrice) {
  462. $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
  463. $data['discountType'] = dict::getDict('discountType', 'discount');
  464. $currentPrice = $modifyPrice;
  465. }
  466. }
  467. $data['actPrice'] = $currentPrice;
  468. $data['realPrice'] = $currentPrice;
  469. //如果有平台补贴
  470. if (isset($data['discountType']) && $data['discountType'] == dict::getDict('discountType', 'usePtCoupon')) {
  471. $discountAmount = $data['discountAmount'] ?? 0;
  472. $data['realPrice'] = bcsub($currentPrice, $discountAmount, 2);
  473. }
  474. $data['sendTime'] = isset($data['sendTime']) && !empty($data['sendTime']) ? $data['sendTime'] : '0000-00-00 00:00:00';
  475. //订单有效期
  476. $validTime = dict::getDict('order_pay_has_time');
  477. $current = time() + $validTime;
  478. $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
  479. $data['status'] = self::ORDER_STATUS_UN_PAY;
  480. //计算总重量(kg)
  481. $weight = 0;
  482. $productMapData = ProductClass::getProductMapData($product);
  483. foreach ($product as $key => $val) {
  484. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  485. $productId = $val['productId'];
  486. $weight = bcadd($w, $weight, 2);
  487. $checkStock = true;
  488. $stockInfo = ProductClass::decreaseStock($val['productId'], $val['bigNum'], $val['smallNum'], $checkStock);
  489. //记录库存流水
  490. $recordData = [];
  491. $recordData['sjId'] = $sjId;
  492. $recordData['shopId'] = $currentShopId;
  493. $recordData['itemId'] = $productMapData[$productId]['itemId'] ?? 0;
  494. $ratio = $productMapData[$productId]['ratio'] ?? 0;
  495. $recordData['itemNum'] = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  496. $recordData['oldStock'] = $stockInfo['oldStock'];
  497. $recordData['newStock'] = $stockInfo['newStock'];
  498. $recordData['productId'] = $productId;
  499. $recordData['orderSn'] = $orderSn;
  500. StockRecordClass::addSellStockOrderRecord($recordData);
  501. }
  502. $data['weight'] = $weight;
  503. $current = time() + 300;
  504. $data['deadline'] = isset($data['deadline']) ? $data['deadline'] : date("Y-m-d H:i:s", $current);
  505. //修改订单暂时不打印
  506. $data['needPrint'] = $data['needPrint'] ?? dict::getDict('needPrint', 'need');
  507. $orderId = $order->id ?? 0;
  508. self::updateById($orderId, $data);
  509. $return = self::getLockById($orderId);
  510. //门店总订单数+1
  511. $shop = ShopClass::getLockById($currentShopId);
  512. if (empty($shop)) {
  513. util::fail('没有找到门店');
  514. }
  515. $shop->unPayOrder += 1;
  516. $shop->cancelOrder -= 1;
  517. $shop->save();
  518. return $return;
  519. }
  520. //根据订单编号查询 ssh 2021.19
  521. public static function getByOrderSn($orderSn, $returnObj = false)
  522. {
  523. return self::getByCondition(['orderSn' => $orderSn], $returnObj);
  524. }
  525. //根据编号列表查订单
  526. public static function getOrderInfoBySnList($snList)
  527. {
  528. $list = self::getAllByCondition(['orderSn' => ['in', $snList]], null, '*', 'orderSn');
  529. $list = self::groupOrder($list, true, false, false, true);
  530. return $list;
  531. }
  532. //显示操作按钮
  533. public static function getShowButton($showButton, $isList, $order)
  534. {
  535. if ($showButton == false) {
  536. return [];
  537. }
  538. $print = ['type' => 'print', 'show' => 1, 'disable' => 0, 'name' => '打印'];
  539. $modifyProduct = ['type' => 'modifyProduct', 'show' => 0, 'disable' => 0, 'name' => '修改花材'];
  540. $confirm = ['type' => 'confirm', 'show' => 0, 'disable' => 1, 'name' => '确认'];
  541. $cancel = ['type' => 'cancel', 'show' => 0, 'disable' => 0, 'name' => '取消'];
  542. $arrival = ['type' => 'arrival', 'show' => 0, 'disable' => 0, 'name' => '到货'];
  543. if ($isList) {
  544. //订单列表页显示的按钮
  545. $send = ['type' => 'send', 'show' => 0, 'disable' => 0, 'name' => '发货'];
  546. $selfGet = ['type' => 'selfGet', 'show' => 0, 'disable' => 1, 'name' => '自取'];
  547. if ($order['status'] == self::ORDER_STATUS_UN_PAY) {
  548. //待确认订单,打印和修改花材按钮可以使用
  549. $send['disable'] = 1;
  550. $selfGet['disable'] = 1;
  551. $print['disable'] = 0;
  552. $confirm['disable'] = 0;
  553. }
  554. if ($order['status'] == self::ORDER_STATUS_UN_SEND) {
  555. //待配送订单,改花材按钮不可点,自取可点
  556. $selfGet['disable'] = 0;
  557. }
  558. //自取订单发货按钮置灰
  559. if ($order['sendType'] == self::SEND_TYPE_NO) {
  560. $send['disable'] = 1;
  561. }
  562. //取消订单置灰
  563. if ($order['status'] == self::ORDER_STATUS_CANCEL) {
  564. $send['disable'] = 1;
  565. $selfGet['disable'] = 1;
  566. $print['disable'] = 1;
  567. }
  568. if ($order['status'] == self::ORDER_STATUS_COMPLETE) {
  569. $send['name'] = '发货记录';
  570. }
  571. return [$send, $selfGet, $print, $confirm];
  572. } else {
  573. //订单详情页显示的按钮
  574. if (isset($order['book']) && $order['book'] == 1 && isset($order['stockChange']) && $order['stockChange'] == 0) {
  575. if ($order['status'] == self::ORDER_STATUS_UN_SEND) {
  576. $arrival['show'] = 1;
  577. }
  578. }
  579. //待确认订单显示延期付和已付款
  580. if ($order['status'] == self::ORDER_STATUS_UN_PAY) {
  581. $debtPay = ['type' => 'debtPay', 'show' => 0, 'disable' => 0, 'name' => '延期收款'];
  582. $hasPay = ['type' => 'hasPay', 'show' => 0, 'disable' => 0, 'name' => '欠款'];
  583. $confirm['disable'] = 0;
  584. return [$print, $debtPay, $hasPay, $confirm, $cancel, $arrival];
  585. }
  586. //待配送订单显示打印、发货和自取
  587. if ($order['status'] == self::ORDER_STATUS_UN_SEND) {
  588. $selfGet = ['type' => 'selfGet', 'show' => 0, 'disable' => 1, 'name' => '自取'];
  589. $send = ['type' => 'send', 'show' => 0, 'disable' => 1, 'name' => '发货'];
  590. if ($order['hasItem'] == 2) {
  591. return [$print, $selfGet, $send, $arrival];
  592. }
  593. $selfGet['disable'] = 0;
  594. $send['disable'] = 0;
  595. return [$print, $selfGet, $send, $arrival];
  596. }
  597. //配送中显示按钮
  598. if ($order['status'] == self::ORDER_STATUS_SENDING) {
  599. $send = ['type' => 'send', 'show' => 0, 'disable' => 0, 'name' => '发货'];
  600. return [$print, $send, $arrival];
  601. }
  602. //完成
  603. if ($order['status'] == self::ORDER_STATUS_COMPLETE) {
  604. $send = ['type' => 'send', 'show' => 0, 'disable' => 0, 'name' => '发货记录'];
  605. //自取订单不显示发货按钮
  606. if ($order['sendType'] == self::SEND_TYPE_NO) {
  607. $send['show'] = 0;
  608. }
  609. return [$print, $send, $arrival];
  610. }
  611. //取消订单置灰
  612. if ($order['status'] == self::ORDER_STATUS_CANCEL) {
  613. $selfGet['disable'] = 1;
  614. $send['disable'] = 1;
  615. $print['disable'] = 1;
  616. return [$modifyProduct, $arrival];
  617. }
  618. return [];
  619. }
  620. }
  621. //显示进度 ssh 2021.3.1
  622. public static function getShowProgress($showProgress, $order)
  623. {
  624. if ($showProgress == false) {
  625. return [];
  626. }
  627. $create = ['name' => '创建', 'complete' => 1];
  628. $unConfirm = ['name' => '待确认', 'complete' => 0];
  629. if ($order['status'] > 0) {
  630. $unConfirm['complete'] = 1;
  631. }
  632. $out = ['name' => '出库', 'complete' => 0];
  633. if ($order['status'] > 1) {
  634. $out['complete'] = 1;
  635. }
  636. return [$create, $unConfirm, $out];
  637. }
  638. //退款等需要使用原来的订单花材结构
  639. public static function getOriginalShowItem($showItem, $order)
  640. {
  641. if ($showItem == false) {
  642. return ['list' => [], 'itemStat' => []];
  643. }
  644. $orderSn = $order['orderSn'];
  645. $list = OrderItemClass::getOrderItem($orderSn);
  646. $totalPrice = 0;
  647. $totalBigNum = 0;
  648. $totalSmallNum = 0;
  649. $kind = 0;
  650. $itemStat = [];
  651. if (!empty($list)) {
  652. foreach ($list as $key => $val) {
  653. $list[$key]['property'] = $val['rank'] . '级';
  654. $price = $val['price'];
  655. $totalPrice = stringUtil::calcAdd($totalPrice, $price);
  656. $kind++;
  657. $currentBigNum = $val['bigNum'];
  658. $currentSmallNum = $val['smallNum'];
  659. $totalBigNum = stringUtil::calcAdd($totalBigNum, $currentBigNum);
  660. $totalSmallNum = stringUtil::calcAdd($totalSmallNum, $currentSmallNum);
  661. }
  662. $itemStat = ['kind' => $kind, 'bigNum' => $totalBigNum, 'smallNun' => $totalSmallNum, 'totalPrice' => $totalPrice];
  663. }
  664. return ['list' => $list, 'itemStat' => $itemStat];
  665. }
  666. public static function getShowItem($showItem, $order)
  667. {
  668. if ($showItem == false) {
  669. return ['list' => [], 'itemStat' => []];
  670. }
  671. $orderSn = $order['orderSn'];
  672. $list = OrderItemClass::getOrderItem($orderSn);
  673. $totalPrice = 0;
  674. $totalBigNum = 0;
  675. $totalSmallNum = 0;
  676. $kind = 0;
  677. $itemStat = [];
  678. //小菊增加部分
  679. $xjAddList = [];
  680. if (!empty($list)) {
  681. foreach ($list as $key => $val) {
  682. $price = $val['price'];
  683. $totalPrice = stringUtil::calcAdd($totalPrice, $price);
  684. $kind++;
  685. $currentBigNum = $val['bigNum'];
  686. $currentSmallNum = $val['smallNum'];
  687. $totalBigNum = stringUtil::calcAdd($totalBigNum, $currentBigNum);
  688. $totalSmallNum = stringUtil::calcAdd($totalSmallNum, $currentSmallNum);
  689. $list[$key]['property'] = '';
  690. //小菊
  691. if (isset($val['variety']) && $val['variety'] == 1) {
  692. $currentItemId = $val['id'] ?? 0;
  693. $xjList = OrderXjClass::getAllByCondition(['itemId' => $currentItemId], null, '*');
  694. if (!empty($xjList)) {
  695. //选了有颜色的小菊才把原小菊删除不显示出来
  696. unset($list[$key]);
  697. foreach ($xjList as $xjKey => $xj) {
  698. $xjBase = $val;
  699. $num = $xj['num'] ?? 0;
  700. $price = $xj['price'] ?? 0;
  701. $name = $xj['name'] ?? '';
  702. $cover = $xj['cover'] ?? '';
  703. if ($num > 0) {
  704. $bigTotalPrice = bcmul($price, $num, 2);
  705. $xjBase['cover'] = imgUtil::groupImg($cover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  706. $xjBase['unitPrice'] = $price;
  707. $xjBase['price'] = floatval($bigTotalPrice);
  708. $xjBase['num'] = floatval($num);
  709. $xjBase['bigNum'] = floatval($num);
  710. $xjBase['name'] = $name;
  711. $xjBase['property'] = '';
  712. $xjBase['userPrice'] = $bigTotalPrice;
  713. $xjBase['xhNum'] = floatval($num);
  714. $xjBase['xhPreNum'] = floatval($num);
  715. $xjBase['xhPrice'] = floatval($bigTotalPrice);
  716. $xjAddList[] = $xjBase;
  717. }
  718. }
  719. }
  720. }
  721. }
  722. if (!empty($xjAddList)) {
  723. $list = array_merge($list, $xjAddList);
  724. }
  725. $itemStat = ['kind' => $kind, 'bigNum' => $totalBigNum, 'smallNun' => $totalSmallNum, 'totalPrice' => $totalPrice];
  726. }
  727. return ['list' => $list, 'itemStat' => $itemStat];
  728. }
  729. //订单列表 ssh 2021.1.19
  730. public static function getOrderList($where)
  731. {
  732. $data = self::getList('*', $where, 'addTime DESC');
  733. if (isset($data['list']) && !empty($data['list'])) {
  734. foreach ($data['list'] as $key => $val) {
  735. $data['list'][$key]['customAvatarUrl'] = imgUtil::groupImg('');
  736. }
  737. }
  738. return $data;
  739. }
  740. //整合订单员工和供货商等信息 ssh 2021.1.19
  741. //$isList 是否列表页
  742. public static function groupOrder($list, $showButton = false, $showItem = false, $showExpress = false, $isList = false)
  743. {
  744. if (empty($list)) {
  745. return $list;
  746. }
  747. $adminIds = array_unique(array_filter(array_column($list, 'adminId')));
  748. $adminInfo = AdminClass::getAdminByIds($adminIds, null, 'id');
  749. $customIds = array_column($list, 'customId');
  750. $customIds = array_unique(array_filter($customIds));
  751. $customInfoList = CustomClass::getByIds($customIds, null, 'id');
  752. foreach ($list as $key => $val) {
  753. $adminId = $val['adminId'] ?? 0;
  754. $list[$key]['adminName'] = $adminInfo[$adminId]['name'] ?? '';
  755. $list[$key]['customAddress'] = $val['fullAddress'] ?? '';
  756. //显示列表页的操作按钮
  757. $list[$key]['buttonList'] = self::getShowButton($showButton, $isList, $val);
  758. //为了列表页的打印按钮能用,列表也强制显示花材
  759. $showItem = true;
  760. //退款需要显示原有的花材结构,不显示小菊的多颜色
  761. $getOriginalItem = Yii::$app->request->get('getOriginalItem', 0);
  762. $getOriginalItem2 = Yii::$app->request->post('getOriginalItem', 0);
  763. if ($getOriginalItem == 1 || $getOriginalItem2 == 1) {
  764. $showItemData = self::getOriginalShowItem($showItem, $val);
  765. } else {
  766. $showItemData = self::getShowItem($showItem, $val);
  767. }
  768. $list[$key]['product'] = $showItemData['list'];
  769. $list[$key]['productStat'] = $showItemData['itemStat'];
  770. $showResetItem = 0;
  771. //待付款订单显示改花材按钮
  772. if ($val['status'] == self::PAY_STATUS_UN_PAY) {
  773. $showResetItem = 1;
  774. }
  775. $list[$key]['showResetItem'] = $showResetItem;
  776. //获取配送进展
  777. $expressProgress = '';
  778. if ($val['sendType'] == 4) {
  779. $expressInfo = OrderExpressClass::getByCondition(['orderSn' => $val['orderSn']]);
  780. if ($expressInfo) {
  781. $expressProgress = ['progressName' => OrderExpressClass::$statusMap[$expressInfo['status']] ?? ''];
  782. }
  783. }
  784. $list[$key]['sendTypeName'] = self::$sendTypeMap[$val['sendType']] ?? '未知';
  785. $list[$key]['expressProgress'] = $expressProgress;
  786. $list[$key]['customAvatarUrl'] = imgUtil::groupImg($val['customAvatar']);
  787. $list[$key]['customName'] = isset($val['customId']) && !empty($val['customId']) ? $val['customName'] : '散客';
  788. $customId = $val['customId'] ?? '';
  789. $debtAmount = $customInfoList[$customId]['debtAmount'] ?? 0.00;
  790. //打印数据组合
  791. $printData = self::getPrintData($val, $debtAmount, $showItemData);
  792. $list[$key]['printData'] = json_encode($printData);
  793. $list[$key]['debtAmount'] = $debtAmount;
  794. $list[$key]['customShopId'] = $customInfoList[$customId]['shopId'] ?? 0;
  795. }
  796. return $list;
  797. }
  798. public static function printGroup($current, $content)
  799. {
  800. //58mm的机器,一行打印16个汉字,32个字母
  801. $name = $current['name'] ?? '';
  802. $content .= $name . '<BR>';
  803. $name = str_repeat(' ', 10);
  804. $productNum = $current['num'] ?? '';
  805. $blankNum = bcsub(12, strlen($productNum));
  806. if ($blankNum > 0) {
  807. $productNum = $productNum . str_repeat(' ', $blankNum);
  808. }
  809. $productPrice = $current['price'] ?? '';
  810. $blankNum = bcsub(7, strlen($productPrice));
  811. if ($blankNum > 0) {
  812. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  813. }
  814. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  815. return $content;
  816. }
  817. //云打印 ssh 20210709
  818. public static function onlinePrint($orderInfo)
  819. {
  820. $showItem = OrderClass::getShowItem(true, $orderInfo);
  821. $mainId = $orderInfo['mainId'] ?? 0;
  822. $staff = ShopAdminClass::getByCondition(['mainId' => $mainId, 'founder' => 2], true);
  823. $mobile = $staff->mobile ?? '';
  824. $customId = $orderInfo['customId'] ?? 0;
  825. $ghsId = $orderInfo['ghsId'] ?? 0;
  826. $c = CustomClass::getById($customId);
  827. $debtAmount = $c['debtAmount'] ?? 0.00;
  828. $fromType = $orderInfo['fromType'] ?? dict::getDict('fromType', 'shop');
  829. $fromTypeMap = dict::getDict('fromTypeMap');
  830. $fromTypeName = $fromTypeMap[$fromType] ?? '门店';
  831. $respond = self::getPrintData($orderInfo, $debtAmount, $showItem);
  832. $content = '<B>' . $respond['sendNum'] . ' ' . $fromTypeName . '</B><BR>';
  833. $content .= '--------------------------------<BR>';
  834. $content .= '<B>' . $respond['custom']['fullAddress'] . '</B><BR>';
  835. $content .= '<B>' . $respond['custom']['customName'] . '</B><BR>';
  836. $content .= '<B>' . $respond['custom']['customMobile'] . '</B><BR>';
  837. $payDate = date("Y-m-d", strtotime($orderInfo['payTime']));
  838. $sendTimWant = $orderInfo['sendTimeWant'] ?? '';
  839. if ($payDate != $sendTimWant) {
  840. $content .= '<BR>';
  841. $content .= '送货时间:<BR>';
  842. $content .= '<B>' . $orderInfo['sendTimeWant'] . '</B><BR>';
  843. }
  844. if (isset($orderInfo['book']) && $orderInfo['book'] == 1) {
  845. $content .= '<B>预订单</B><BR>';
  846. }
  847. if (isset($respond['remark']) && !empty($respond['remark'])) {
  848. $content .= '<BR>';
  849. $content .= '备注:<BR>';
  850. $content .= '<B>' . $respond['remark'] . '</B><BR>';
  851. }
  852. $content .= '<BR>';
  853. $content .= '花材 数量/单价 金额 <BR>';
  854. $content .= '--------------------------------<BR>';
  855. if (isset($respond['product']) && !empty($respond['product'])) {
  856. foreach ($respond['product'] as $current) {
  857. $content = self::printGroup($current, $content);
  858. }
  859. $content .= '--------------------------------<BR>';
  860. }
  861. if (isset($respond['refund']) && !empty($respond['refund'])) {
  862. $content .= '<B>已退款:</B><BR>';
  863. foreach ($respond['refund'] as $current) {
  864. //58mm的机器,一行打印16个汉字,32个字母
  865. $name = $current['name'] ?? '';
  866. $content .= $name . '<BR>';
  867. $name = str_repeat(' ', 10);
  868. $productNum = $current['num'] ?? '';
  869. $blankNum = bcsub(12, strlen($productNum));
  870. if ($blankNum > 0) {
  871. $productNum = $productNum . str_repeat(' ', $blankNum);
  872. }
  873. $productPrice = $current['price'] ?? '';
  874. $blankNum = bcsub(7, strlen($productPrice));
  875. if ($blankNum > 0) {
  876. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  877. }
  878. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  879. }
  880. $content .= '--------------------------------<BR>';
  881. }
  882. if (isset($respond['sendCost']) && $respond['sendCost'] > 0) {
  883. $content .= ' 运费:' . floatval($respond['sendCost']) . '<BR>';
  884. }
  885. $content .= '订单金额:' . floatval($respond['prePrice']) . '<BR>';
  886. if (isset($respond['discountAmount']) && !empty($respond['discountAmount']) && $respond['discountAmount'] > 0) {
  887. $content .= '优惠扣除:-' . floatval($respond['discountAmount']) . '<BR>';
  888. }
  889. if (isset($respond['refundPrice']) && $respond['refundPrice'] > 0) {
  890. $content .= '退款金额:' . floatval($respond['refundPrice']) . '<BR>';
  891. }
  892. $content .= '应付金额:' . floatval($respond['realPrice']) . '<BR>';
  893. if (isset($respond['debt']) && $respond['debt'] == 1) {
  894. $content .= '实付金额:0<BR>';
  895. } else {
  896. $content .= '实付金额:' . floatval($respond['realPrice']) . '<BR>';
  897. }
  898. if ($debtAmount > 0) {
  899. $content .= '累计欠款:' . floatval($debtAmount) . '<BR>';
  900. }
  901. $shopId = $orderInfo['shopId'] ?? 0;
  902. $shop = ShopClass::getById($shopId, true);
  903. $sjId = $shop->sjId ?? 0;
  904. $sj = SjClass::getById($sjId, true);
  905. $sjName = $sj->name ?? '';
  906. $currentName = $sjName;
  907. if (isset($shop->default) == false || $shop->default != 1) {
  908. $shopName = $shop->shopName ?? '';
  909. if (!empty($shopName)) {
  910. $currentName = $currentName . ' ' . $shopName;
  911. }
  912. }
  913. $getInfo = self::getById($orderInfo['id']);
  914. $content .= '--------------------------------<BR>';
  915. $content .= '订单日期:' . $getInfo['addTime'] . '<BR>';
  916. $content .= '订单编号:' . $respond['orderSn'] . '<BR>';
  917. $content .= '门店名称:' . $currentName . '<BR>';
  918. if (!empty($mobile)) {
  919. $content .= '联系电话:' . $mobile . '<BR>';
  920. }
  921. if ($debtAmount > 0) {
  922. $content .= "<BR>";
  923. $content .= "<C>扫码查看所有欠款单</C>";
  924. $content .= "<BR>";
  925. $host = Yii::$app->params['hdHost'];
  926. $content .= "<QR>" . $host . "/#/pagesPurchase/gathering?id=" . $ghsId . "</QR>";
  927. }
  928. $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
  929. $printSn = $ext['printSn'] ?? '';
  930. if (!empty($printSn)) {
  931. $p = new printUtil($printSn);
  932. $p->printMsg($content);
  933. }
  934. }
  935. //打印数据 ssh 20210702
  936. public static function getPrintData($val, $debtAmount, $showItem)
  937. {
  938. $printProduct = [];
  939. if (isset($showItem['list']) && !empty($showItem['list'])) {
  940. foreach ($showItem['list'] as $itemKey => $itemVal) {
  941. $bigNum = $itemVal['bigNum'] ?? 0;
  942. $bigUnitPrice = $itemVal['unitPrice'] ?? 0;
  943. $smallUnitPrice = $itemVal['smallUnitPrice'] ?? 0;
  944. $smallNum = $itemVal['smallNum'] ?? 0;
  945. if ($bigNum > 0) {
  946. $bigTotalPrice = bcmul($bigUnitPrice, $bigNum, 2);
  947. $printProduct[] = [
  948. 'name' => $itemVal['name'] ?? '',
  949. 'num' => floatval($bigNum) . '扎*' . floatval($bigUnitPrice),
  950. 'price' => floatval($bigTotalPrice),
  951. ];
  952. }
  953. if ($smallNum > 0) {
  954. $smallTotalPrice = bcmul($smallUnitPrice, $smallNum, 2);
  955. $printProduct[] = [
  956. 'name' => $itemVal['name'] ?? '',
  957. 'num' => floatval($smallNum) . '支*' . floatval($smallUnitPrice),
  958. 'price' => floatval($smallTotalPrice),
  959. ];
  960. }
  961. }
  962. }
  963. $sendNum = $val['sendNum'] ?? '';
  964. $relateOrderSn = $val['orderSn'] ?? '';
  965. $refundList = [];
  966. $refundData = RefundOrderClass::getAllByCondition(['relateOrderSn' => $relateOrderSn], null, '*', null, true);
  967. if (!empty($refundData)) {
  968. foreach ($refundData as $refund) {
  969. $refundOrderSn = $refund->orderSn ?? '';
  970. $refundProductList = RefundOrderItemClass::getAllByCondition(['orderSn' => $refundOrderSn], null, '*');
  971. if (!empty($refundProductList)) {
  972. foreach ($refundProductList as $currentRefund) {
  973. $num = $currentRefund['xhNum'] ?? 0;
  974. $unitName = $currentRefund['xhUnitName'] ?? '';
  975. $unitPrice = $currentRefund['xhUnitPrice'] ?? 0;
  976. $currentPrice = bcmul($unitPrice, $num, 2);
  977. $refundList[] = [
  978. 'name' => $currentRefund['name'] ?? '',
  979. 'num' => floatval($num) . $unitName . '*' . floatval($unitPrice),
  980. 'price' => floatval($currentPrice),
  981. ];
  982. }
  983. }
  984. }
  985. }
  986. $printData = [
  987. 'address' => '',
  988. 'sendCost' => $val['sendCost'] ?? '0.00',
  989. 'prePrice' => $val['prePrice'] ?? '0.00',
  990. 'discountAmount' => $val['discountAmount'] ?? '0.00',
  991. 'realPrice' => $val['realPrice'] ?? '0.00',
  992. 'refundPrice' => $val['refundPrice'] ?? '0.00',
  993. 'debt' => $val['debt'] ?? 0,
  994. 'orderSn' => $val['orderSn'] ?? '',
  995. 'date' => $val['addTime'] ?? '',
  996. 'remark' => $val['remark'] ?? '',
  997. 'custom' => [
  998. 'customName' => $val['customName'] ?? '',
  999. 'customMobile' => $val['customMobile'] ?? '',
  1000. 'fullAddress' => $val['fullAddress'] ?? '',
  1001. ],
  1002. 'product' => $printProduct,
  1003. 'refund' => $refundList,
  1004. 'sendNum' => $sendNum,
  1005. 'debtAmount' => $debtAmount,
  1006. ];
  1007. return $printData;
  1008. }
  1009. //订单的验证 ssh 2021.3.2
  1010. public static function valid($info, $shopId)
  1011. {
  1012. if (empty($info)) {
  1013. util::fail('没有找到订单');
  1014. }
  1015. if (isset($info['shopId']) && $info['shopId'] == $shopId) {
  1016. return true;
  1017. }
  1018. util::fail('没有权限操作的订单');
  1019. }
  1020. //快递送 ssh 2021.3.16
  1021. public static function thirdSend($order, $expressInfo)
  1022. {
  1023. if ($order->status == self::ORDER_STATUS_UN_PAY) {
  1024. util::fail('订单未付款');
  1025. }
  1026. if ($order->status != self::ORDER_STATUS_UN_SEND) {
  1027. util::fail('不是待配送状态');
  1028. }
  1029. $id = $order->id;
  1030. $expressId = $expressInfo['expressId'] ?? 1;
  1031. $expressName = $expressInfo['deliveryName'] ?? '达达';
  1032. $expressRemark = $expressInfo['remark'] ?? '';
  1033. $order->status = self::ORDER_STATUS_SENDING;
  1034. $order->sendType = self::SEND_TYPE_THIRD;
  1035. $order->expressId = $expressId;
  1036. $order->save();
  1037. $shopId = $order->shopId;
  1038. $shop = Shop::findBySql('select * from ' . Shop::tableName() . ' where id = :id for update', ['id' => $shopId])->one();
  1039. if (empty($shop)) {
  1040. util::fail('没有找到门店');
  1041. }
  1042. $shop->unSendOrder -= 1;
  1043. $shop->sendingOrder += 1;
  1044. $shop->save();
  1045. $express = OrderExpressClass::getByCondition(['orderId' => $id]);
  1046. if (!empty($express)) {
  1047. util::fail('已经发过了');
  1048. }
  1049. //达达
  1050. $dadaRes = DadaExpressServices::addOrder($order, $expressRemark);
  1051. $orderSn = $order->orderSn;
  1052. $sjId = $order->sjId ?? 0;
  1053. /*
  1054. $getInfo = ExpressClass::getExpressInfo($expressId);
  1055. $hour = $expressInfo['sendTime'] ?? '12:00';
  1056. $sendCost = 12;
  1057. $distance = 5;
  1058. $sendTime = strtotime(date("Y-m-d ") . $hour);
  1059. OrderSendClass::thirdSend($order);
  1060. */
  1061. $sendTime = $order->sendTime;
  1062. if ($sendTime == '0000-00-00 00:00:00') {
  1063. $sendTime = time();
  1064. } else {
  1065. $sendTime = strtotime($order->sendTime);
  1066. }
  1067. $expData = [
  1068. 'orderId' => $id,
  1069. 'orderSn' => $orderSn,
  1070. 'sjId' => $sjId,
  1071. 'expressId' => $expressId,
  1072. 'sendTime' => $sendTime,
  1073. 'cost' => $dadaRes['fee'],
  1074. 'distance' => $dadaRes['distance'],
  1075. 'status' => 1,
  1076. 'expressName' => $expressName,
  1077. 'remark' => $expressRemark,
  1078. ];
  1079. OrderExpressClass::addExpress($expData);
  1080. OrderSendClass::thirdSend($order);
  1081. $respond = [
  1082. 'expressName' => $expressName,
  1083. 'expressId' => $expressId,
  1084. 'distance' => $dadaRes['distance'],
  1085. 'cost' => $dadaRes['fee'],
  1086. 'status' => 2,
  1087. 'tip' => 0,
  1088. ];
  1089. //采购单变成已发货
  1090. $purchaseId = $order->purchaseId ?? 0;
  1091. PurchaseClass::sending($purchaseId);
  1092. return $respond;
  1093. }
  1094. //本店送 ssh 2021.1.24
  1095. public static function selfSend($info)
  1096. {
  1097. if ($info->status == self::ORDER_STATUS_UN_PAY) {
  1098. util::fail('订单未付款');
  1099. }
  1100. if ($info->status != self::ORDER_STATUS_UN_SEND) {
  1101. util::fail('不是待配送状态');
  1102. }
  1103. $info->status = self::ORDER_STATUS_SENDING;
  1104. $info->sendType = self::SEND_TYPE_MYSELF;
  1105. $info->save();
  1106. $shopId = $info->shopId;
  1107. $shop = Shop::findBySql('select * from ' . Shop::tableName() . ' where id = :id for update', ['id' => $shopId])->one();
  1108. if (empty($shop)) {
  1109. util::fail('没有找到门店');
  1110. }
  1111. $shop->unSendOrder -= 1;
  1112. $shop->sendingOrder += 1;
  1113. $shop->save();
  1114. OrderSendClass::selfSend($info);
  1115. //采购单变成已发货
  1116. $purchaseId = $info->purchaseId ?? 0;
  1117. PurchaseClass::sending($purchaseId);
  1118. }
  1119. //订单完成 ssh 2020.3.12
  1120. public static function complete($order, $shop)
  1121. {
  1122. $order->status = OrderClass::ORDER_STATUS_COMPLETE;
  1123. $order->currentFlow = self::TOTAL_FLOW;
  1124. $order->totalFlow = self::TOTAL_FLOW;
  1125. $order->save();
  1126. $shop->finishOrder += 1;
  1127. $shop->sendingOrder -= 1;
  1128. $shop->save();
  1129. return $order;
  1130. }
  1131. //修改订单表的配送信息 lqh 2021.3.28
  1132. public static function updateCustomExpressInfo($id, $expressAddInfo)
  1133. {
  1134. $data = [
  1135. 'province' => $expressAddInfo['province'],
  1136. 'city' => $expressAddInfo['city'],
  1137. 'address' => $expressAddInfo['address'],
  1138. 'floor' => $expressAddInfo['floor'],
  1139. 'fullAddress' => $expressAddInfo['city'] . $expressAddInfo['address'] . $expressAddInfo['floor'],
  1140. 'lat' => $expressAddInfo['lat'],
  1141. 'long' => $expressAddInfo['long'],
  1142. 'sendTime' => $expressAddInfo['sendTime'],
  1143. ];
  1144. return self::updateById($id, $data);
  1145. }
  1146. //最客户最近的订单 ssh 2021.5.4
  1147. public static function getCustomOrder($id, $limit = 10, $order = null, $with = null)
  1148. {
  1149. $where = ['customId' => $id, 'payStatus' => 1];
  1150. return self::getLimitList('*', $where, $limit, $order, $with);
  1151. }
  1152. //销售单取消 ssh 2021.5.14
  1153. public static function cancel($id)
  1154. {
  1155. $order = self::getLockById($id);
  1156. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  1157. util::fail("销售单{$id}不是待付款状态,无法取消");
  1158. }
  1159. $order->status = self::ORDER_STATUS_CANCEL;
  1160. $order->save();
  1161. $shopId = $order->shopId;
  1162. $shop = ShopClass::getLockById($shopId);
  1163. if (empty($shop)) {
  1164. util::fail('没有找到门店');
  1165. }
  1166. $mainId = $shop->mainId ?? 0;
  1167. $main = MainClass::getLockById($mainId);
  1168. if (empty($main)) {
  1169. util::fail('没有main信息');
  1170. }
  1171. $main->unPayOrder -= 1;
  1172. $main->cancelOrder += 1;
  1173. $main->save();
  1174. //加回库存
  1175. $orderItem = OrderItemClass::getOrderItem($order->orderSn);
  1176. foreach ($orderItem as $v) {
  1177. $stockInfo = ProductClass::addStockByItemNum($v['productId'], $v['num']);
  1178. $recordData = [];
  1179. $recordData['sjId'] = $order->sjId;
  1180. $recordData['shopId'] = $shopId;
  1181. //增加中央ID
  1182. $recordData['mainId'] = ProductClass::getMainIdByProductId($v['productId']);
  1183. $recordData['itemId'] = $v['itemId'] ?? 0;
  1184. $recordData['itemNum'] = $v['num'];
  1185. $recordData['oldStock'] = $stockInfo['oldStock'];
  1186. $recordData['newStock'] = $stockInfo['newStock'];
  1187. $recordData['productId'] = $v['productId'];
  1188. $recordData['orderSn'] = $order->orderSn;
  1189. $recordData['relateName'] = $order->customName ?? '';
  1190. $recordData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  1191. StockRecordClass::addSellStockOrderCancelRecord($recordData);
  1192. }
  1193. return $order;
  1194. }
  1195. //新订单入队列 ssh 20211012
  1196. public static function newOrderAddQueue($order)
  1197. {
  1198. $shopId = $order->shopId ?? 0;
  1199. if (empty($shopId)) {
  1200. return false;
  1201. }
  1202. $id = $order->id ?? 0;
  1203. if (empty($id)) {
  1204. return false;
  1205. }
  1206. $newOrderKey = 'ghsNewOrder_' . $shopId;
  1207. Yii::$app->redis->executeCommand('LPUSH', [$newOrderKey, $id]);
  1208. }
  1209. }