OrderClass.php 53 KB

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