OrderClass.php 56 KB

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