OrderClass.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. <?php
  2. namespace bizHd\order\classes;
  3. use biz\goods\classes\GoodsClass;
  4. use biz\goods\models\Goods;
  5. use biz\shop\classes\ShopCapitalClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopExtClass;
  8. use biz\stat\classes\StatOrderCountClass;
  9. use biz\stock\classes\GoodsStockRecordClass;
  10. use bizGhs\order\classes\CheckOrderClass;
  11. use bizGhs\product\classes\ProductClass;
  12. use bizGhs\stock\classes\StockRecordClass;
  13. use bizHd\custom\classes\CustomClass;
  14. use bizHd\item\classes\ItemClass;
  15. use bizHd\merchant\classes\SjClass;
  16. use bizHd\refund\classes\HdRefundClass;
  17. use bizHd\shop\classes\MainClass;
  18. use bizHd\shop\classes\ShopMoneyChangeClass;
  19. use bizHd\user\classes\UserClass;
  20. use bizHd\work\classes\WorkClass;
  21. use common\components\dict;
  22. use common\components\imgUtil;
  23. use common\components\miniUtil;
  24. use common\components\noticeUtil;
  25. use common\components\orderSn;
  26. use common\components\printUtil;
  27. use common\components\stringUtil;
  28. use common\components\util;
  29. use Yii;
  30. use bizHd\base\classes\BaseClass;
  31. use common\components\lakala\Lakala;
  32. class OrderClass extends BaseClass
  33. {
  34. public static $baseFile = '\bizHd\order\models\Order';
  35. //已取消
  36. const ORDER_STATUS_CANCEL = 5;
  37. //完成
  38. const ORDER_STATUS_COMPLETE = 4;
  39. //配送中
  40. const ORDER_STATUS_SENDING = 3;
  41. //待配送
  42. const ORDER_STATUS_UN_SEND = 2;
  43. //待付款
  44. const ORDER_STATUS_UN_PAY = 1;
  45. //订单总流程数
  46. const ORDER_TOTAL_FLOW = 4;
  47. //未确认配送方式
  48. const SEND_TYPE_UNKNOWN = 0;
  49. //自取
  50. const SEND_TYPE_NO = 1;
  51. //自己送
  52. const SEND_TYPE_MYSELF = 2;
  53. //快递送
  54. const SEND_TYPE_THIRD = 3;
  55. //正常订单
  56. const REFUND_NO = 0;
  57. //已退款订单
  58. const REFUND_YES = 1;
  59. //送达时间段
  60. public static $reachPeriod = [0 => '上午', 1 => '下午', 2 => '晚上'];
  61. //优惠金额
  62. public static $randDiscount = [
  63. 50 => [1, 3],
  64. 100 => [5, 9],
  65. 300 => [10, 15],
  66. 500 => [16, 18],
  67. 1000 => [18, 30],
  68. 20000 => [90, 100],
  69. ];
  70. //因为开负数订单时引导库存变化 ssh 20250731
  71. public static function shPayChange($order, $shop, $staff, $originOrder = null)
  72. {
  73. $forward = $order['forward'] ?? 0;
  74. if ($forward == 0) {
  75. return false;
  76. }
  77. $forwardStock = $order['forwardStock'] ?? 0;
  78. $orderSn = $order['orderSn'] ?? '';
  79. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  80. $orderGoodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  81. if (empty($orderItemList) && empty($orderGoodsList)) {
  82. return false;
  83. }
  84. $shopId = $shop->id ?? 0;
  85. $sjId = $shop->sjId ?? 0;
  86. $adminId = $staff->adminId;
  87. $staffName = $staff->name ?? '';
  88. $mainId = $shop->mainId;
  89. if (!empty($orderItemList)) {
  90. $itemInfo = [];
  91. $ids = array_column($orderItemList, 'itemId');
  92. $infoList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
  93. foreach ($orderItemList as $orderItem) {
  94. $unitType = $orderItem['unitType'] ?? 0;
  95. $num = $orderItem['num'] ?? 0;
  96. $productId = $orderItem['itemId'] ?? 0;
  97. $info = $infoList[$productId] ?? [];
  98. $stock = $info['stock'] ?? 0;
  99. $ratio = $info['ratio'] ?? 20;
  100. $map = ProductClass::formatStock($stock, $ratio);
  101. $bigNum = $map['bigNum'] ?? 0;
  102. $smallNum = $map['smallNum'] ?? 0;
  103. $num = $forwardStock == 0 ? $num * 2 : $num;
  104. if ($unitType == 0) {
  105. $bigNum = $bigNum + $num;
  106. } else {
  107. $smallNum = $smallNum + $num;
  108. }
  109. $itemInfo[] = ['productId' => $productId, 'bigNum' => $bigNum, 'smallNum' => $smallNum];
  110. }
  111. $remark = $forwardStock == 0 ? '开售后付款单,退货并退款' : '开售后付款单,只退款';
  112. $itemData = [
  113. 'remark' => $remark,
  114. 'itemInfo' => $itemInfo,
  115. 'sjId' => $sjId,
  116. 'shopId' => $shopId,
  117. 'adminId' => $adminId,
  118. 'staffName' => $staffName,
  119. ];
  120. CheckOrderClass::opOrder($itemData);
  121. }
  122. if (!empty($orderGoodsList)) {
  123. foreach ($orderGoodsList as $orderGoods) {
  124. $goodsId = $orderGoods['goodsId'] ?? 0;
  125. $num = $orderGoods['num'] ?? 0;
  126. $num = $forwardStock == 0 ? $num * 2 : $num;
  127. $ret = \bizHd\goods\classes\GoodsClass::addStock($goodsId, $mainId, $num);
  128. $oldStock = $ret['oldStock'] ?? 0;
  129. $newStock = $ret['newStock'] ?? 0;
  130. $recordData = [];
  131. $recordData['sjId'] = $sjId;
  132. $recordData['shopId'] = $shopId;
  133. $recordData['mainId'] = $mainId;
  134. $recordData['orderSn'] = '';
  135. $recordData['goodsId'] = $goodsId;
  136. $recordData['goodsNum'] = $num;
  137. $recordData['oldStock'] = $oldStock;
  138. $recordData['newStock'] = $newStock;
  139. $recordData['relateName'] = $staffName;
  140. GoodsStockRecordClass::pdGoods($recordData);
  141. }
  142. }
  143. if (is_object($order) && !empty($originOrder)) {
  144. //建立关系
  145. $originOrder->hasForward = 1;
  146. $originOrder->save();
  147. $orderId = $originOrder->id ?? 0;
  148. $orderSn = $originOrder->orderSn ?? '';
  149. $shOrderId = $order->id ?? 0;
  150. $shOrderSn = $order->orderSn ?? '';
  151. $map = ['orderId' => $orderId, 'orderSn' => $orderSn, 'shOrderId' => $shOrderId, 'shOrderSn' => $shOrderSn];
  152. OrderForwardClass::add($map, true);
  153. }
  154. return true;
  155. }
  156. /**
  157. * @param $order
  158. * @param $mainId
  159. * @return array
  160. * $shAddPay 0走正常流程售后,1走售后付款单
  161. * $shAddPayReason 走售后付款单的原因:0、售后金额大于剩余欠款(剩余欠款可能没有了,可能还有);1、可售后时间到了
  162. */
  163. public static function ifShPay($order, $main, $refundAmount = 0)
  164. {
  165. $shAddPay = 0;
  166. $shAddPayReason = 0;
  167. $payStatus = $order['payStatus'] ?? 0;
  168. if ($payStatus == 1) {
  169. if ($order['payWay'] == 3) {
  170. if ($order['remainDebtPrice'] <= 0) {
  171. //不管是否要售后,没有剩余欠款了,只能走开负数订单模式
  172. $shAddPay = 1;
  173. } else {
  174. if ($refundAmount > 0 && $refundAmount > $order['remainDebtPrice']) {
  175. //如果需要售后,并且售后金额大于剩余欠款,走开负数订单模式
  176. $shAddPay = 1;
  177. }
  178. }
  179. }
  180. $cRet = OrderClass::couldRefund($order, $main);
  181. $could = $cRet['could'];
  182. if (!$could) {
  183. //可售后时间到了
  184. $shAddPay = 1;
  185. $shAddPayReason = 1;
  186. }
  187. }
  188. return ['shAddPay' => $shAddPay, 'shAddPayReason' => $shAddPayReason];
  189. }
  190. //订单是否过了可以售后的时间 ssh 20250730
  191. // $could false 过了售后时间,不能售后,true可以
  192. public static function couldRefund($order, $main)
  193. {
  194. $refundLimit = $main->refundLimit ?? 0;
  195. $payTime = $order['payTime'];
  196. $currentPayTime = strtotime($payTime);
  197. $addDateTime = $refundLimit * 86400;
  198. $couldRefundTime = $currentPayTime + $addDateTime;
  199. $now = time();
  200. $could = $couldRefundTime > $now;
  201. return ['could' => $could];
  202. }
  203. public static function valid($order, $mainId)
  204. {
  205. if (!isset($order->mainId) || $order->mainId != $mainId) {
  206. Yii::info(json_encode($order));
  207. Yii::info('mainId: ' . $mainId);
  208. util::fail('无法访问的订单');
  209. }
  210. return true;
  211. }
  212. //添加订单 ssh 2019.12.5
  213. public static function addOrder($data)
  214. {
  215. $data['orderSn'] = $data['orderSn'] ?? orderSn::getOrderSn();
  216. $data['payStatus'] = $data['payStatus'] ?? 0;
  217. $shopId = $data['shopId'] ?? 0;
  218. //累计订单增加
  219. $shop = ShopClass::getLockById($shopId);
  220. if (empty($shop)) {
  221. util::fail('没有找到门店信息54');
  222. }
  223. $mainId = $shop->mainId ?? 0;
  224. $main = MainClass::getLockById($mainId);
  225. if (empty($main)) {
  226. util::fail('没有找到main信息');
  227. }
  228. $main->unPayOrder += 1;
  229. $main->totalOrder += 1;
  230. $main->save();
  231. $address = $data['address'] ?? '';
  232. $floor = $data['floor'] ?? '';
  233. if (empty($data['fullAddress'])) {
  234. $data['fullAddress'] = $address . $floor;
  235. }
  236. $sendType = $data['sendType'] ?? 0;
  237. if ($sendType == 1) {
  238. //到店自取,默认当天23点前,没有用,为了方便查询排序
  239. $data['reachDate'] = date("Y-m-d");
  240. $data['reachPeriod'] = '22:00';
  241. } else {
  242. //如果不是到店自取的,则必须要有配送时间,默认当天
  243. $data['reachDate'] = !empty($data['reachDate']) ? $data['reachDate'] : date("Y-m-d");
  244. if (empty($data['reachPeriod'])) {
  245. util::fail('请选择配送时间');
  246. }
  247. }
  248. $data['reachTime'] = strtotime($data['reachDate'] . ' ' . $data['reachPeriod']);
  249. //将花店每月的总订单数作为编号
  250. $riseNum = StatOrderCountClass::addOrder($shop, $main);
  251. $data['sendNum'] = date("d") . $riseNum;
  252. $data['totalFlow'] = OrderClass::ORDER_TOTAL_FLOW;
  253. $salt = stringUtil::charsShuffleLowerCase(10);
  254. $data['salt'] = $salt;
  255. return self::add($data, true);
  256. }
  257. public static function getByOrderSn($orderSn)
  258. {
  259. return self::getByCondition(['orderSn' => $orderSn]);
  260. }
  261. //评价 ssh 2019.12.16
  262. public static function comment($data)
  263. {
  264. $id = $data['id'];
  265. unset($data['id']);
  266. return self::updateById($id, $data);
  267. }
  268. public static function getFullInfo($id)
  269. {
  270. $order = self::getById($id);
  271. if (empty($order)) {
  272. return [];
  273. }
  274. $sn = $order['orderSn'] ?? '';
  275. $order['goodsInfoList'] = OrderGoodsClass::getListBySn($sn);
  276. $order['itemInfoList'] = OrderItemClass::getListBySn($sn);
  277. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*');
  278. if (!empty($workList)) {
  279. foreach ($workList as $key => $work) {
  280. $shortCover = $work['cover'] ?? '';
  281. $workList[$key]['shortCover'] = $shortCover;
  282. $workList[$key]['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  283. }
  284. }
  285. $order['workList'] = $workList;
  286. $debtPrice = $order['debtPrice'] ?? 0;
  287. $remainDebtPrice = $order['remainDebtPrice'] ?? 0;
  288. $orderId = $order['id'] ?? 0;
  289. $osList = [];
  290. if ($debtPrice > $remainDebtPrice) {
  291. $osList = OrderSettleClass::getAllByCondition(['orderId' => $orderId, 'status' => 1], null, '*', null, true);
  292. }
  293. //结账记录关系
  294. $order['orderSettleList'] = $osList;
  295. $order['today'] = date("Y-m-d");
  296. $order['tomorrow'] = date("Y-m-d", strtotime("+1 day"));
  297. return $order;
  298. }
  299. //订单
  300. public static function getOrderById($id)
  301. {
  302. $order = self::getById($id);
  303. if (empty($order)) {
  304. return [];
  305. }
  306. $sn = $order['orderSn'] ?? '';
  307. $order['goodsInfoList'] = OrderGoodsClass::getListBySn($sn);
  308. return $order;
  309. }
  310. //根据订单查询 ssh 2020.1.4
  311. public static function getOrderBySn($orderSn)
  312. {
  313. $order = self::getByCondition(['orderSn' => $orderSn]);
  314. if (empty($order)) {
  315. return [];
  316. }
  317. $id = $order['id'];
  318. $order['goodsInfoList'] = OrderGoodsClass::getGoodsListById($id);
  319. return $order;
  320. }
  321. //返回随机优惠金额 ssh 2019.9.12
  322. public static function getRandDiscount($amount)
  323. {
  324. if ($amount <= 0) {
  325. return 0;
  326. }
  327. $randDiscount = self::$randDiscount;
  328. krsort($randDiscount);
  329. $rand = 0;
  330. foreach ($randDiscount as $currentAmount => $val) {
  331. if ($amount >= $currentAmount) {
  332. $rand = rand($val[0], $val[1]);
  333. break;
  334. }
  335. }
  336. return $rand / 10;
  337. }
  338. //第三方支付后回调处理流程 ssh 2021.4.30
  339. public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId)
  340. {
  341. $order = self::getByCondition(['orderSn' => $orderSn], true);
  342. if (empty($order)) {
  343. noticeUtil::push('散客下单,付款成功,支付回调,单号:' . $orderSn . ' 金额:' . $totalFee . ' 回调id:' . $transactionId . ' 附件:' . $attach . ',没有找到订单', '15280215347');
  344. $msg = "没有找到零售订单 orderSn:{$orderSn}";
  345. Yii::info($msg);
  346. util::fail($msg);
  347. }
  348. if ($order->mainPay != $totalFee) {
  349. noticeUtil::push("散客下单,付款成功,支付回调错误,零售订单金额与回调通知金额不一致 orderSn:{$orderSn} {$order->mainPay} {$totalFee} 附件:{$attach}", '15280215347');
  350. $msg = "零售订单金额与回调通知金额不一致 orderSn:{$orderSn} {$order->mainPay} {$totalFee}";
  351. Yii::info($msg);
  352. util::fail($msg);
  353. }
  354. $id = $order->id;
  355. $order = self::getLockById($id);
  356. $date = date("Y-m-d H:i:s");
  357. $order->payTime = $date;
  358. $order->thirdNo = $transactionId;
  359. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  360. $order->save();
  361. self::payAfter($order, $payWay);
  362. }
  363. //订单完成 ssh 2021.4.20
  364. public static function payAfter($order, $payWay)
  365. {
  366. if (empty($order)) {
  367. util::fail('没有找到订单');
  368. }
  369. if (!isset($order->status)) {
  370. util::fail('没有找到订单的状态');
  371. }
  372. $orderSn = $order->orderSn ?? '';
  373. if ($order->status == self::ORDER_STATUS_CANCEL) {
  374. $shopId = $order->shopId;
  375. $shop = ShopClass::getById($shopId, true);
  376. if (empty($shop)) {
  377. noticeUtil::push('注意,零售订单:' . $orderSn . ',付款成功但订单已取消,没有找到门店,关键词 cancel_get_money', '15280215347');
  378. util::stop("SUCCESS");
  379. }
  380. Yii::$app->params['errorReport'] = 1;
  381. try {
  382. //恢复取消的订单
  383. self::recoverOrderToPay($order);
  384. noticeUtil::push('注意,零售订单:' . $orderSn . ',付款成功,取消的订单,已恢复 SUCCESS', '15280215347');
  385. } catch (\Exception $exception) {
  386. $ret = HdRefundClass::onlyRefundAmount($shop, $order);
  387. if ($ret['status'] == 0) {
  388. $errMsg = $exception->getMessage();
  389. noticeUtil::push('注意,零售订单:' . $orderSn . ',付款成功但订单取消。订单没有恢复成功,原因' . $errMsg . ',也没有退款成功。请手动处理。关键词 cancel_get_money', '15280215347');
  390. }
  391. util::stop("SUCCESS");
  392. }
  393. }
  394. if ($order->payStatus == 1) {
  395. noticeUtil::push('注意,零售订单:' . $orderSn . ',已经付过款了,无需重复请求和操作', '15280215347');
  396. util::fail('订单已经付过款了');
  397. }
  398. $orderId = $order->id ?? 0;
  399. $customId = $order->customId ?? 0;
  400. $order->status = self::ORDER_STATUS_UN_SEND; // 付款后,订单状态设置为:已付款(待配送)
  401. $order->payWay = $payWay;
  402. $order->payStatus = 1;
  403. $order->payTime = date("Y-m-d H:i:s");
  404. $order->stockChange = 1;
  405. $order->save();
  406. $myCustom = CustomClass::getLockById($customId);
  407. if (!empty($myCustom)) {
  408. if ($order->forward == 0) {
  409. $myCustom->recentExpend = date("Y-m-d H:i:s");
  410. $myCustom->visitTime = date("Y-m-d H:i:s");
  411. $myCustom->save();
  412. }
  413. }
  414. $realPrice = $order->realPrice ?? 0;
  415. $shopId = $order->shopId;
  416. $shop = ShopClass::getLockById($shopId);
  417. if (empty($shop)) {
  418. util::fail('没有找到门店,编号5115');
  419. }
  420. $mainId = $shop->mainId ?? 0;
  421. $main = MainClass::getLockById($mainId);
  422. if (empty($main)) {
  423. util::fail('没有main信息,编号3096');
  424. }
  425. $main->unSendOrder += 1;
  426. $main->unPayOrder -= 1;
  427. $currentTotalIncome = bcadd($main->totalIncome, $realPrice, 2);
  428. $main->totalIncome = $currentTotalIncome;
  429. $main->save();
  430. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  431. $shopAdminId = $order->shopAdminId ?? 0;
  432. $shopAdminName = $order->shopAdminName ?? '';
  433. $sjId = $order->sjId;
  434. $fromType = $order->fromType ?? 1;
  435. $event = !empty($shopAdminId) ? '员工开单' : '客户下单';
  436. $capital = [
  437. 'capitalType' => $capitalType,
  438. 'relateId' => $orderId,
  439. 'io' => 1,
  440. 'totalIncome' => $currentTotalIncome,
  441. 'payWay' => $payWay,
  442. 'event' => $event,
  443. 'sjId' => $sjId,
  444. 'shopId' => $shopId,
  445. 'shopAdminId' => $shopAdminId,
  446. 'shopAdminName' => $shopAdminName,
  447. 'amount' => $realPrice,
  448. 'fromType' => $fromType,
  449. 'mainId' => $mainId,
  450. ];
  451. ShopCapitalClass::addCapital($capital);
  452. //每天和每月收入增加
  453. //StatIncomeClass::updateOrInsert($main, $shop, $realPrice);
  454. //今日订单+1
  455. //StatOrderClass::updateOrInsert($main, $shop);
  456. //销售收入增加
  457. //StatSaleClass::replace($main, $shop, $realPrice);
  458. //各渠道收入金额统计
  459. //StatKdClass::replace($shop, $realPrice, $payWay);
  460. //当天同个人同地址的订单汇总
  461. $sameDate = date("Y_m_d");
  462. $sameAddress = $order->showAddress ?? '';
  463. $addressMd5 = md5($sameAddress);
  464. $sameKey = $customId . '_' . $addressMd5 . '_' . $sameDate . '_retail';
  465. $sameString = Yii::$app->redis->executeCommand('GET', [$sameKey]);
  466. if (!empty($sameString)) {
  467. $newString = $sameString . ',' . $orderId;
  468. $newIdsArray = explode(',', $newString);
  469. if (count($newIdsArray) <= 90) {
  470. Yii::$app->redis->executeCommand('SETEX', [$sameKey, 186400, $newString]);
  471. foreach ($newIdsArray as $currentOrderId) {
  472. OrderClass::updateById($currentOrderId, ['sameTimeIds' => $newString]);
  473. }
  474. //noticeUtil::push($sameKey . '' . $sameAddress . " 花掌柜当天汇总ids:" . $newString, '15280215347');
  475. }
  476. } else {
  477. Yii::$app->redis->executeCommand('SETEX', [$sameKey, 186400, $orderId]);
  478. $order->sameTimeIds = $orderId;
  479. $order->save();
  480. //noticeUtil::push($sameKey . '' . $sameAddress . " 花掌柜当天汇总ids:" . $orderId, '15280215347');
  481. }
  482. //客户在线支付下单增加商家可提现余额
  483. if (isset($order->onlinePay) && $order->onlinePay == dict::getDict('onlinePay', 'yes')) {
  484. $amount = $order->mainPay;
  485. ShopClass::customKdAddBalance($main, $shop, $amount, $order, $capitalType);
  486. }
  487. //客户使用余额付款
  488. if ($payWay == dict::getDict('payWay', 'balancePay')) {
  489. CustomClass::payToChangeBalance($order);
  490. }
  491. //有欠款时
  492. if ($payWay == dict::getDict('payWay', 'debtPay')) {
  493. if (isset($order->remainDebtPrice) && $order->remainDebtPrice > 0) {
  494. CustomClass::skCgDebtAmountAdd($myCustom, $order);
  495. }
  496. }
  497. //全部现金付款余额增加
  498. if ($payWay == dict::getDict('payWay', 'cash')) {
  499. $moneyBalance = bcadd($main->money, $order->mainPay, 2);
  500. $main->money = $moneyBalance;
  501. $main->save();
  502. $moneyEvent = "客户现金下单" . floatval($order->mainPay) . "元(单号 {$order->orderSn})";
  503. $moneyRemark = '';
  504. $change = [
  505. 'relateId' => $orderId,
  506. 'amount' => $order->mainPay,
  507. 'balance' => $moneyBalance,
  508. 'io' => 1,
  509. 'mainId' => $mainId,
  510. 'capitalType' => $capitalType,
  511. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  512. 'event' => $moneyEvent,
  513. 'remark' => $moneyRemark,
  514. ];
  515. ShopMoneyChangeClass::addData($change);
  516. } else {
  517. //部分现金付款
  518. $orderCash = $order->cash ?? 0;
  519. $orderCash = floatval($orderCash);
  520. if ($orderCash > 0) {
  521. $moneyBalance = bcadd($main->money, $orderCash, 2);
  522. $main->money = $moneyBalance;
  523. $main->save();
  524. $moneyEvent = "客户部分现金付款{$orderCash}元(单号 {$order->orderSn})";
  525. $moneyRemark = '';
  526. $change = [
  527. 'relateId' => $orderId,
  528. 'amount' => $orderCash,
  529. 'balance' => $moneyBalance,
  530. 'io' => 1,
  531. 'mainId' => $mainId,
  532. 'capitalType' => $capitalType,
  533. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  534. 'event' => $moneyEvent,
  535. 'remark' => $moneyRemark,
  536. ];
  537. ShopMoneyChangeClass::addData($change);
  538. }
  539. }
  540. }
  541. //转化出送到时间 ssh 2020.3.15
  542. public static function getReachTime($order)
  543. {
  544. $reachTime = '';
  545. if (isset($order['reachDate']) && !empty($order['reachDate'])) {
  546. $prev = date("n-j", strtotime($order['reachDate']));
  547. $periodData = self::$reachPeriod;
  548. $periodKey = $order['reachPeriod'];
  549. $period = isset($periodData[$periodKey]) ? $periodData[$periodKey] : '上午';
  550. $reachTime = $prev . ' ' . $period;
  551. }
  552. return $reachTime;
  553. }
  554. //支付成功获取unionId ssh 2020.5.12
  555. public static function payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user)
  556. {
  557. $mcId = $merchantExtend['wxPayMerchantId'];
  558. $miniOpenId = isset($user['miniOpenId']) ? $user['miniOpenId'] : '';
  559. $unionId = isset($user['unionId']) ? $user['unionId'] : '';
  560. $userId = $user['id'];
  561. if (empty($unionId)) {
  562. $unionId = miniUtil::getPaidUnionId($merchant, $miniOpenId, $mcId, (string)$orderSn, 0);
  563. if (!empty($unionId)) {
  564. $findUser = UserClass::getByUnionId($unionId);
  565. if (empty($findUser)) {
  566. UserClass::updateByCondition(['id' => $userId], ['unionId' => $unionId]);
  567. } else {
  568. UserClass::mergeUser($findUser, $user);
  569. }
  570. }
  571. }
  572. }
  573. //订单取消 ssh 20220516
  574. public static function setExpire($order, $force = false, $staff = null)
  575. {
  576. $now = time();
  577. if ($order->deadline > $now && $force == false) {
  578. return false;
  579. }
  580. if ($order->status != 1) {
  581. return false;
  582. }
  583. $order->status = 5;
  584. $order->cancelTime = date("Y-m-d H:i:s");
  585. $order->save();
  586. $orderSn = $order->orderSn ?? '';
  587. $relateName = $staff->name ?? '系统';
  588. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  589. if (!empty($itemList)) {
  590. foreach ($itemList as $item) {
  591. $ptItemId = $item->ptItemId ?? 0;
  592. $sjId = $item->sjId ?? 0;
  593. $shopId = $item->shopId ?? 0;
  594. $mainId = $item->mainId ?? 0;
  595. $ratio = $item->ratio ?? 20;
  596. $unitType = $item->unitType ?? 0;
  597. if ($unitType == 0) {
  598. $bigNum = $item->num;
  599. $smallNum = 0;
  600. } else {
  601. $bigNum = 0;
  602. $smallNum = $item->num;
  603. }
  604. $itemId = $item->itemId ?? 0;
  605. $stockInfo = ProductClass::addStock($itemId, $bigNum, $smallNum);
  606. $recordData = [];
  607. $recordData['sjId'] = $sjId;
  608. $recordData['shopId'] = $shopId;
  609. $recordData['mainId'] = $mainId;
  610. $recordData['itemId'] = $ptItemId;
  611. $recordData['itemNum'] = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
  612. $recordData['oldStock'] = $stockInfo['oldStock'];
  613. $recordData['newStock'] = $stockInfo['newStock'];
  614. $recordData['productId'] = $itemId;
  615. $recordData['orderSn'] = $orderSn;
  616. $recordData['relateName'] = $relateName;
  617. StockRecordClass::addSellStockOrderCancelRecord($recordData);
  618. }
  619. }
  620. $goodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  621. if (!empty($goodsList)) {
  622. foreach ($goodsList as $goods) {
  623. $goodsId = $goods->goodsId ?? 0;
  624. $num = $goods->num ?? 0;
  625. $mainId = $goods->mainId ?? 0;
  626. $orderSn = $goods->orderSn ?? '';
  627. $shopId = $goods->shopId ?? 0;
  628. $sjId = $goods->sjId ?? 0;
  629. $params = ['reduceSale' => 1];
  630. $respond = \bizHd\goods\classes\GoodsClass::addStock($goodsId, $mainId, $num, $params);
  631. $oldStock = $respond['oldStock'] ?? 0;
  632. $newStock = $respond['newStock'] ?? 0;
  633. $data = [];
  634. $data['sjId'] = $sjId;
  635. $data['shopId'] = $shopId;
  636. $data['mainId'] = $mainId;
  637. $data['orderSn'] = $orderSn;
  638. $data['goodsId'] = $goodsId;
  639. $data['goodsNum'] = $num;
  640. $data['oldStock'] = $oldStock;
  641. $data['newStock'] = $newStock;
  642. $data['relateName'] = $relateName;
  643. GoodsStockRecordClass::cancelOrder($data);
  644. }
  645. }
  646. $shopId = $order->shopId ?? 0;
  647. $shop = ShopClass::getById($shopId, true);
  648. if (!empty($shop)) {
  649. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  650. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  651. $params = [
  652. 'appid' => 'OP00002119',
  653. 'serial_no' => '018b08cfddbd',
  654. 'merchant_no' => $shop->lklSjNo,
  655. 'term_no' => $shop->lklScanTermNo,
  656. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  657. 'lklCertificatePath' => $lklCertificatePath,
  658. ];
  659. $laResource = new Lakala($params);
  660. $closeParams = [
  661. 'orderSn' => $orderSn,
  662. ];
  663. $response = $laResource->close($closeParams);
  664. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  665. //noticeUtil::push('零售订单:' . $orderSn . ',关单没有成功。', '15280215347');
  666. } else {
  667. //noticeUtil::push('零售订单:' . $orderSn . ',关单成功。', '15280215347');
  668. }
  669. }
  670. }
  671. // 恢复订单 -- 即:把订单的状态从取消状态变成待付款状态
  672. public static function recoverOrderToPay($order)
  673. {
  674. $now = time();
  675. $order->status = 1;
  676. $order->deadline = $now + 86400;
  677. $order->save();
  678. // 重新占用库存(与取消订单时的库存回滚相反)
  679. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $order->orderSn], null, '*', null, true);
  680. if (!empty($itemList)) {
  681. foreach ($itemList as $item) {
  682. $ptItemId = $item->ptItemId ?? 0;
  683. $sjId = $item->sjId ?? 0;
  684. $shopId = $item->shopId ?? 0;
  685. $mainId = $item->mainId ?? 0;
  686. $ratio = $item->ratio ?? 20;
  687. $unitType = $item->unitType ?? 0;
  688. if ($unitType == 0) {
  689. $bigNum = $item->num;
  690. $smallNum = 0;
  691. } else {
  692. $bigNum = 0;
  693. $smallNum = $item->num;
  694. }
  695. $itemId = $item->itemId ?? 0;
  696. // 重新扣减库存(占用库存)
  697. $stockInfo = ProductClass::decreaseStock($itemId, $bigNum, $smallNum, true);
  698. $recordData = [];
  699. $recordData['sjId'] = $sjId;
  700. $recordData['shopId'] = $shopId;
  701. $recordData['mainId'] = $mainId;
  702. $recordData['itemId'] = $ptItemId;
  703. $recordData['itemNum'] = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
  704. $recordData['oldStock'] = $stockInfo['oldStock'];
  705. $recordData['newStock'] = $stockInfo['newStock'];
  706. $recordData['productId'] = $itemId;
  707. $recordData['orderSn'] = $order->orderSn;
  708. $recordData['relateName'] = $order->customName ?? '';
  709. StockRecordClass::hdKdAddRecord($recordData);
  710. }
  711. }
  712. return true;
  713. }
  714. // 扣库存
  715. protected static function decGoodsStock($orderGoods)
  716. {
  717. foreach ($orderGoods as $goods) {
  718. GoodsClass::counters([
  719. 'stock' => -$goods['num']
  720. ], [
  721. 'and',
  722. ['id' => $goods['goodsId']],
  723. ['<>', 'stock', Goods::FULL_STOCK]
  724. ]);
  725. }
  726. }
  727. //云打印 ssh 20210709
  728. public static function onlinePrint($order, $must = false, $port = 'front')
  729. {
  730. if (empty($order)) {
  731. return false;
  732. }
  733. if (!$must) {
  734. if ($order->needPrint != dict::getDict('needPrint', 'need')) {
  735. return false;
  736. }
  737. if ($order->payStatus != 1) {
  738. return false;
  739. }
  740. }
  741. if ($order->forward == 1) {
  742. //售后付款单暂时不能打印
  743. return false;
  744. }
  745. $shopId = $order->shopId ?? 0;
  746. $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
  747. $printSn = $ext['printSn'] ?? '';
  748. if ($port == 'make') {
  749. $printSn = $ext['makePrintSn'] ?? '';
  750. if ($order->fromType == 4) {
  751. $printSn = $ext['mtPrintSn'] ?? '';
  752. }
  753. $workList = WorkClass::getAllByCondition(['orderId' => $order->id], null, '*', null, true);
  754. $hasSh = 0;
  755. if (!empty($workList)) {
  756. foreach ($workList as $work) {
  757. $sh = $work->sh ?? 0;
  758. if ($sh == 1) {
  759. $hasSh = 1;
  760. }
  761. }
  762. }
  763. if ($hasSh == 1) {
  764. $printSn = $ext['printSn'] ?? '';
  765. }
  766. }
  767. if (empty($printSn)) {
  768. return false;
  769. }
  770. $order->printNum += 1;
  771. $order->save();
  772. $customId = $order->customId ?? 0;
  773. $custom = CustomClass::getById($customId);
  774. $debtAmount = $custom['debtAmount'] ?? 0;
  775. $respond = self::getPrintData($order, $debtAmount);
  776. $fromType = $order->fromType ?? dict::getDict('fromType', 'shop');
  777. $fromTypeMap = dict::getDict('fromTypeMap');
  778. $fromTypeName = $fromTypeMap[$fromType] ?? '门店';
  779. //客服下的单则显示客服号和客户名称
  780. if ($fromType == dict::getDict('fromType', 'friend')) {
  781. $staffName = $order->shopAdminName ?? '';
  782. $bookName = $order->bookName ?? '';
  783. $fromTypeName = $staffName . ' ' . $bookName;
  784. }
  785. $content = '';
  786. //$content .= "<CB>【零售】</CB><BR>";
  787. if ($fromType == dict::getDict('fromType', 'mt')) {
  788. $content .= '<B>美团 ' . $order->thirdSn . '</B><BR>';
  789. if (isset($order->goodsNum) && $order->goodsNum > 1) {
  790. $content .= '<B>美团 ' . $order->thirdSn . ' (' . $order->goodsNum . '份)</B><BR>';
  791. }
  792. $content .= '--------------------------------<BR>';
  793. $content .= date("Y-m-d", strtotime($order->addTime)) . '<BR>';
  794. } else {
  795. $content .= '<CB>' . $fromTypeName . ' ' . $respond['sendNum'] . '</CB><BR>';
  796. $content .= '--------------------------------<BR>';
  797. }
  798. $readPeriod = $order->reachPeriod ?? 0;
  799. if ($order->sendType == 1) {
  800. if (isset($order->reachDate) && !empty($order->reachDate) && $order->reachDate != '0000-00-00') {
  801. $currentReachDate = date("m-d", strtotime($order->reachDate));
  802. $content .= '<B>' . $currentReachDate . ' ' . $readPeriod . '前</B><BR>';
  803. $content .= '<B>到店自取</B><BR><BR>';
  804. } else {
  805. $content .= '<B>到店自取</B><BR><BR>';
  806. }
  807. } else {
  808. if ($order->sendType == 0) {
  809. $content .= '<B>送货上门</B><BR><BR>';
  810. } else if ($order->sendType == 2) {
  811. $content .= '<B>请跑腿送</B><BR><BR>';
  812. } else if ($order->sendType == 3) {
  813. $content .= '<B>物流到付</B><BR><BR>';
  814. } else if ($order->sendType == 4) {
  815. $content .= '<B>快递到付</B><BR><BR>';
  816. } else {
  817. $content .= '';
  818. }
  819. if (isset($order->reachDate) && !empty($order->reachDate) && $order->reachDate != '0000-00-00') {
  820. $currentReachDate = date("m-d", strtotime($order->reachDate));
  821. $content .= '<B>' . $currentReachDate . ' ' . $readPeriod . '前</B><BR><BR>';
  822. }
  823. }
  824. if (isset($order->receiveUserName) && !empty($order->receiveUserName)) {
  825. $content .= '收花人:<BR>';
  826. $content .= '<B>' . $order->receiveUserName . '</B><BR>';
  827. }
  828. if (isset($order->receiveMobile) && !empty($order->receiveMobile)) {
  829. $content .= '<B>' . $order->receiveMobile . '</B><BR><BR>';
  830. }
  831. if (isset($order->fullAddress) && !empty($order->fullAddress)) {
  832. $content .= '<B>' . $order->fullAddress . '(' . $order->showAddress . ')' . '</B><BR><BR>';
  833. }
  834. if (isset($order->cardInfo) && !empty($order->cardInfo)) {
  835. $content .= '<B>贺卡内容:' . $order->cardInfo . '</B><BR><BR>';
  836. }
  837. if ($order->anonymity == 1) {
  838. $content .= '<B>匿名派送!!</B><BR><BR>';
  839. } else {
  840. if (isset($order->bookMobile) && !empty($order->bookMobile)) {
  841. $bookName = $order->bookName ?? '';
  842. $bookMobile = $order->bookMobile ?? '';
  843. $content .= '订花人:<BR>';
  844. if (!empty($bookName)) {
  845. $content .= '<B>' . $bookName . '</B><BR>';
  846. }
  847. if (!empty($bookMobile)) {
  848. $content .= '<B>' . $bookMobile . '</B><BR>';
  849. }
  850. }
  851. }
  852. if (isset($respond['remark']) && !empty($respond['remark'])) {
  853. $content .= '<BR>';
  854. $content .= '备注:<BR>';
  855. $content .= '<B>' . $respond['remark'] . '</B><BR>';
  856. }
  857. $content .= '<BR>';
  858. $content .= '商品 数量/单价 金额 <BR>';
  859. $content .= '--------------------------------<BR>';
  860. if (isset($respond['product']) && !empty($respond['product'])) {
  861. foreach ($respond['product'] as $current) {
  862. $content = self::printGroup($current, $content);
  863. }
  864. $content .= '--------------------------------<BR>';
  865. }
  866. if (isset($respond['refund']) && !empty($respond['refund'])) {
  867. $content .= '<B>已退款:</B><BR>';
  868. foreach ($respond['refund'] as $current) {
  869. //58mm的机器,一行打印16个汉字,32个字母
  870. $name = $current['name'] ?? '';
  871. $content .= $name . '<BR>';
  872. $name = str_repeat(' ', 10);
  873. $productNum = $current['num'] ?? '';
  874. $blankNum = bcsub(12, strlen($productNum));
  875. if ($blankNum > 0) {
  876. $productNum = $productNum . str_repeat(' ', $blankNum);
  877. }
  878. $productPrice = $current['price'] ?? '';
  879. $blankNum = bcsub(7, strlen($productPrice));
  880. if ($blankNum > 0) {
  881. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  882. }
  883. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  884. }
  885. $content .= '--------------------------------<BR>';
  886. }
  887. if (isset($order->goodsPrice) && $order->goodsPrice > 0) {
  888. $content .= '商品合计:' . floatval($order->goodsPrice) . '<BR>';
  889. }
  890. if (isset($respond['sendCost']) && $respond['sendCost'] > 0 && $order->sendType == 2) {
  891. $content .= '运费:' . floatval($respond['sendCost']) . '<BR>';
  892. }
  893. if (isset($order->serviceFee) && $order->serviceFee > 0) {
  894. $content .= '手续费:' . floatval($order->serviceFee) . '<BR>';
  895. }
  896. if (isset($order->labourCost) && $order->labourCost > 0) {
  897. $content .= '包装费:' . floatval($order->labourCost) . '<BR>';
  898. }
  899. if (isset($order->prePrice) && $order->prePrice > 0) {
  900. $content .= '总计:' . floatval($order->prePrice) . '<BR>';
  901. }
  902. if (isset($respond['discountAmount']) && !empty($respond['discountAmount']) && $respond['discountAmount'] > 0) {
  903. $content .= '优惠扣除:-' . floatval($respond['discountAmount']) . '<BR>';
  904. }
  905. $content .= '订单金额:' . floatval($respond['orderPrice']) . '<BR>';
  906. if (isset($respond['refundPrice']) && $respond['refundPrice'] > 0) {
  907. $content .= '退款金额:' . floatval($respond['refundPrice']) . '<BR>';
  908. }
  909. if (isset($respond['debt']) && $respond['debt'] == 1) {
  910. $content .= '实付金额:0<BR>';
  911. } else {
  912. $content .= '实付金额:' . floatval($respond['realPrice']) . '<BR>';
  913. }
  914. if (isset($order->remainDebtPrice) && $order->remainDebtPrice > 0) {
  915. $content .= '<B>待付尾款:' . floatval($order->remainDebtPrice) . '</B><BR>';
  916. }
  917. $shop = ShopClass::getById($shopId, true);
  918. $sjId = $shop->sjId ?? 0;
  919. $sj = SjClass::getById($sjId, true);
  920. $sjName = $sj->name ?? '';
  921. $shopName = $shop->shopName ?? '';
  922. $currentName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  923. $content .= '--------------------------------<BR>';
  924. $content .= '订单日期:' . $order->addTime . '<BR>';
  925. $content .= '订单编号:' . $respond['orderSn'] . '<BR>';
  926. $content .= '门店名称:' . $currentName . '<BR>';
  927. $content .= "<BR>";
  928. $p = new printUtil($printSn);
  929. $orderSn = $order->orderSn ?? '';
  930. $workList = WorkClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  931. if (!empty($workList)) {
  932. $content .= "<BR>";
  933. $content .= '--------------------------------<BR>';
  934. foreach ($workList as $work) {
  935. $goodsSn = $work->goodsSn ?? '';
  936. $sn = $work->sn ?? '';
  937. $content .= '<B>制作号:' . $sn . '<B>';
  938. // 1-523 有-特殊符号,使用飞鹅自带的函数处理
  939. $content .= $p->bar_code($goodsSn);
  940. $content .= "<BR>";
  941. }
  942. }
  943. $p->printMsg($content);
  944. }
  945. //打印数据 ssh 20210702
  946. public static function getPrintData($order, $debtAmount)
  947. {
  948. $orderSn = $order->orderSn ?? '';
  949. $printProduct = [];
  950. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  951. if (!empty($orderItemList)) {
  952. foreach ($orderItemList as $itemKey => $itemVal) {
  953. $unitName = $itemVal['unitName'] ?? '';
  954. $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
  955. $printProduct[] = [
  956. 'name' => $itemVal['name'] ?? '',
  957. 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
  958. 'price' => floatval($itemVal['price']),
  959. ];
  960. }
  961. }
  962. $orderGoodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  963. if (!empty($orderGoodsList)) {
  964. foreach ($orderGoodsList as $itemKey => $itemVal) {
  965. $unitName = $itemVal['unitName'] ?? '份';
  966. $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
  967. $printProduct[] = [
  968. 'name' => $itemVal['name'] ?? '',
  969. 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
  970. 'price' => floatval($itemVal['price']),
  971. ];
  972. }
  973. }
  974. $sendNum = $order['sendNum'] ?? '';
  975. //退款记录
  976. $refundList = [];
  977. $printData = [
  978. 'address' => '',
  979. 'sendCost' => $order->sendCost ?? '0.00',
  980. 'prePrice' => $order->prePrice ?? '0.00',
  981. 'orderPrice' => $order->orderPrice ?? '0',
  982. 'discountAmount' => $order->discountAmount ?? '0.00',
  983. 'realPrice' => $order->realPrice ?? '0.00',
  984. 'refundPrice' => $order->refundPrice ?? '0.00',
  985. 'debt' => $order->debt ?? 0,
  986. 'orderSn' => $order->orderSn ?? '',
  987. 'date' => $order->addTime ?? '',
  988. 'remark' => $order->remark ?? '',
  989. 'custom' => [
  990. 'customName' => $order->customName ?? '',
  991. 'customMobile' => $order->customMobile ?? '',
  992. 'fullAddress' => $order->fullAddress ?? '',
  993. ],
  994. 'product' => $printProduct,
  995. 'refund' => $refundList,
  996. 'sendNum' => $sendNum,
  997. 'debtAmount' => $debtAmount,
  998. ];
  999. return $printData;
  1000. }
  1001. public static function printGroup($current, $content)
  1002. {
  1003. //58mm的机器,一行打印16个汉字,32个字母
  1004. $name = $current['name'] ?? '';
  1005. $content .= $name . '<BR>';
  1006. $name = str_repeat(' ', 10);
  1007. $productNum = $current['num'] ?? '';
  1008. $blankNum = bcsub(12, strlen($productNum));
  1009. if ($blankNum > 0) {
  1010. $productNum = $productNum . str_repeat(' ', $blankNum);
  1011. }
  1012. $productPrice = $current['price'] ?? '';
  1013. $blankNum = bcsub(7, strlen($productPrice));
  1014. if ($blankNum > 0) {
  1015. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  1016. }
  1017. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  1018. return $content;
  1019. }
  1020. //扫码收款的,减少库存 ssh 20250822
  1021. public static function reduceMyStock($data, $shop, $custom)
  1022. {
  1023. $mainId = $shop->mainId;
  1024. $customId = $custom->id;
  1025. $orderId = $data['orderId'] ?? '';
  1026. $sjId = $shop->sjId;
  1027. $hdId = $custom->hdId;
  1028. $shopId = $shop->id;
  1029. $order = self::getById($orderId, true);
  1030. if (empty($order)) {
  1031. util::fail('没有找到订单');
  1032. }
  1033. if ($order->mainId != $shop->mainId) {
  1034. util::fail('并不是你的订单');
  1035. }
  1036. if ($order->repeat == 0) {
  1037. util::fail('已经扣过了');
  1038. }
  1039. if ($order->tkPrice > 0) {
  1040. util::fail('已经发生退款,无法操作');
  1041. }
  1042. if ($order->customId != $customId) {
  1043. util::fail('订单对应客户有问题,无法扣库存,编号:' . $customId . ' ' . $order->customId);
  1044. }
  1045. $orderSn = $order->orderSn ?? '';
  1046. $product = $data['product'] ?? [];
  1047. if (empty($product)) {
  1048. util::fail('请选择商品呢');
  1049. }
  1050. $orderItemList = OrderItemClass::getAllbyCondition(['orderSn' => $orderSn], null, '*', null, true);
  1051. if (!empty($orderItemList)) {
  1052. foreach ($orderItemList as $orderItem) {
  1053. $orderItem->unitPrice = 0;
  1054. $orderItem->save();
  1055. }
  1056. }
  1057. $actPrice = $order->actPrice ?? 0;
  1058. if ($actPrice <= 0) {
  1059. util::fail('订单金额为0');
  1060. }
  1061. $ids = array_column($product, 'productId');
  1062. $productInfo = ItemClass::getByIds($ids, null, 'id');
  1063. $goodsPrice = 0;
  1064. foreach ($product as $key => $val) {
  1065. $property = $val['property'] ?? 0;
  1066. if ($property != 1) {
  1067. util::fail('暂时只支持花材');
  1068. }
  1069. $unitPrice = $val['unitPrice'] ?? 0;
  1070. $unitType = $val['unitType'] ?? 0;
  1071. $currentId = $val['productId'] ?? 0;
  1072. $num = $val['num'] ?? 0;
  1073. $currentInfo = $productInfo[$currentId] ?? [];
  1074. if (empty($currentInfo)) {
  1075. util::fail('存在无效花材,编号:' . $currentId);
  1076. }
  1077. $name = $currentInfo['name'] ?? '';
  1078. $cover = $currentInfo['cover'] ?? '';
  1079. $ratio = $currentInfo['ratio'] ?? 20;
  1080. $itemMainId = $currentInfo['mainId'] ?? 0;
  1081. if ($itemMainId != $mainId) {
  1082. util::fail('不是您的花材,请勿使用,编号:' . $mainId . ' ' . $itemMainId);
  1083. }
  1084. if ($unitType == 0) {
  1085. $unitName = $currentInfo['bigUnit'] ?? '';
  1086. $unitId = $currentInfo['bigUnitId'] ?? 0;
  1087. $totalNum = $num;
  1088. } else {
  1089. $unitName = $currentInfo['smallUnit'] ?? '';
  1090. $unitId = $currentInfo['smallUnitId'] ?? 0;
  1091. $totalNum = bcdiv($num, $ratio, 2);
  1092. }
  1093. $ptItemId = $currentInfo['itemId'] ?? 0;
  1094. $classId = $currentInfo['classId'] ?? 0;
  1095. $belongCost = $currentInfo['belongCost'] ?? 0;
  1096. $currentPrice = bcmul($unitPrice, $num, 2);
  1097. $goodsPrice = bcadd($goodsPrice, $currentPrice, 2);
  1098. $currentItemData = [
  1099. 'name' => $name,
  1100. 'cover' => $cover,
  1101. 'ratio' => $ratio,
  1102. 'unitType' => $unitType,
  1103. 'unitName' => $unitName,
  1104. 'unitId' => $unitId,
  1105. 'unitPrice' => $unitPrice,
  1106. 'num' => $num,
  1107. 'orderSn' => $orderSn,
  1108. 'itemId' => $currentId,
  1109. 'ptItemId' => $ptItemId,
  1110. 'totalNum' => $totalNum,
  1111. 'totalPrice' => 0,
  1112. 'customId' => $customId,
  1113. 'hdId' => $hdId,
  1114. 'sjId' => $sjId,
  1115. 'shopId' => $shopId,
  1116. 'mainId' => $mainId,
  1117. 'classId' => $classId,
  1118. 'price' => $currentPrice,
  1119. 'belongCost' => $belongCost,
  1120. ];
  1121. //库存变化在这里
  1122. OrderItemClass::addData($currentItemData, $custom);
  1123. }
  1124. $labourCost = 0;
  1125. $discountType = 0;
  1126. $discountAmount = 0;
  1127. if ($goodsPrice > $actPrice) {
  1128. $prePrice = $goodsPrice;
  1129. $discountAmount = bcsub($goodsPrice, $actPrice, 2);
  1130. if ($discountAmount > 0) {
  1131. $discountType = 3;
  1132. }
  1133. } else {
  1134. $prePrice = $actPrice;
  1135. $labourCost = bcsub($actPrice, $goodsPrice, 2);
  1136. }
  1137. $order->goodsPrice = $goodsPrice;
  1138. $order->prePrice = $prePrice;
  1139. $order->labourCost = $labourCost;
  1140. $order->discountAmount = $discountAmount;
  1141. $order->discountType = $discountType;
  1142. $order->repeat = 0;
  1143. $order->save();
  1144. return $order;
  1145. }
  1146. }