OrderClass.php 57 KB

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