OrderClass.php 49 KB

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