OrderClass.php 53 KB

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