OrderClass.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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. ProductClass::tryRecoverLimitBuyWhenStockRollbackFromZero($itemId, $stockInfo['oldStock']);
  682. $recordData = [];
  683. $recordData['sjId'] = $sjId;
  684. $recordData['shopId'] = $shopId;
  685. $recordData['mainId'] = $mainId;
  686. $recordData['itemId'] = $ptItemId;
  687. $recordData['itemNum'] = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
  688. $recordData['oldStock'] = $stockInfo['oldStock'];
  689. $recordData['newStock'] = $stockInfo['newStock'];
  690. $recordData['productId'] = $itemId;
  691. $recordData['orderSn'] = $orderSn;
  692. $recordData['relateName'] = $relateName;
  693. StockRecordClass::addSellStockOrderCancelRecord($recordData);
  694. }
  695. }
  696. $goodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  697. if (!empty($goodsList)) {
  698. foreach ($goodsList as $goods) {
  699. $goodsId = $goods->goodsId ?? 0;
  700. $num = $goods->num ?? 0;
  701. $mainId = $goods->mainId ?? 0;
  702. $orderSn = $goods->orderSn ?? '';
  703. $shopId = $goods->shopId ?? 0;
  704. $sjId = $goods->sjId ?? 0;
  705. $params = ['reduceSale' => 1];
  706. $respond = \bizHd\goods\classes\GoodsClass::addStock($goodsId, $mainId, $num, $params);
  707. $oldStock = $respond['oldStock'] ?? 0;
  708. $newStock = $respond['newStock'] ?? 0;
  709. $data = [];
  710. $data['sjId'] = $sjId;
  711. $data['shopId'] = $shopId;
  712. $data['mainId'] = $mainId;
  713. $data['orderSn'] = $orderSn;
  714. $data['goodsId'] = $goodsId;
  715. $data['goodsNum'] = $num;
  716. $data['oldStock'] = $oldStock;
  717. $data['newStock'] = $newStock;
  718. $data['relateName'] = $relateName;
  719. GoodsStockRecordClass::cancelOrder($data);
  720. }
  721. }
  722. $shopId = $order->shopId ?? 0;
  723. $shop = ShopClass::getById($shopId, true);
  724. if (!empty($shop)) {
  725. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  726. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  727. $params = [
  728. 'appid' => 'OP00002119',
  729. 'serial_no' => '018b08cfddbd',
  730. 'merchant_no' => $shop->lklSjNo,
  731. 'term_no' => $shop->lklScanTermNo,
  732. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  733. 'lklCertificatePath' => $lklCertificatePath,
  734. ];
  735. $laResource = new Lakala($params);
  736. $closeParams = [
  737. 'orderSn' => $orderSn,
  738. ];
  739. $response = $laResource->close($closeParams);
  740. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  741. //noticeUtil::push('零售订单:' . $orderSn . ',关单没有成功。', '15280215347');
  742. } else {
  743. //noticeUtil::push('零售订单:' . $orderSn . ',关单成功。', '15280215347');
  744. }
  745. }
  746. //清理限购缓存
  747. $customId = $order->customId ?? 0;
  748. if (!empty($customId) && !empty($itemList)) {
  749. foreach ($itemList as $item) {
  750. $productId = $item['itemId'];
  751. $num = floor($item['num']);
  752. if (!empty($productId) && $num > 0) {
  753. \bizHd\product\classes\ProductClass::baseClearLimitBuy($productId, $customId, $num);
  754. }
  755. }
  756. }
  757. }
  758. // 恢复订单 -- 即:把订单的状态从取消状态变成待付款状态
  759. public static function recoverOrderToPay($order)
  760. {
  761. $now = time();
  762. $order->status = 1;
  763. $order->deadline = $now + 86400;
  764. $order->save();
  765. // 重新占用库存(与取消订单时的库存回滚相反)
  766. $itemList = OrderItemClass::getAllByCondition(['orderSn' => $order->orderSn], null, '*', null, true);
  767. if (!empty($itemList)) {
  768. foreach ($itemList as $item) {
  769. $ptItemId = $item->ptItemId ?? 0;
  770. $sjId = $item->sjId ?? 0;
  771. $shopId = $item->shopId ?? 0;
  772. $mainId = $item->mainId ?? 0;
  773. $ratio = $item->ratio ?? 20;
  774. $unitType = $item->unitType ?? 0;
  775. if ($unitType == 0) {
  776. $bigNum = $item->num;
  777. $smallNum = 0;
  778. } else {
  779. $bigNum = 0;
  780. $smallNum = $item->num;
  781. }
  782. $itemId = $item->itemId ?? 0;
  783. // 重新扣减库存(占用库存)
  784. $stockInfo = ProductClass::decreaseStock($itemId, $bigNum, $smallNum, true);
  785. $recordData = [];
  786. $recordData['sjId'] = $sjId;
  787. $recordData['shopId'] = $shopId;
  788. $recordData['mainId'] = $mainId;
  789. $recordData['itemId'] = $ptItemId;
  790. $recordData['itemNum'] = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
  791. $recordData['oldStock'] = $stockInfo['oldStock'];
  792. $recordData['newStock'] = $stockInfo['newStock'];
  793. $recordData['productId'] = $itemId;
  794. $recordData['orderSn'] = $order->orderSn;
  795. $recordData['relateName'] = $order->customName ?? '';
  796. StockRecordClass::hdKdAddRecord($recordData);
  797. }
  798. }
  799. return true;
  800. }
  801. // 扣库存
  802. protected static function decGoodsStock($orderGoods)
  803. {
  804. foreach ($orderGoods as $goods) {
  805. GoodsClass::counters([
  806. 'stock' => -$goods['num']
  807. ], [
  808. 'and',
  809. ['id' => $goods['goodsId']],
  810. ['<>', 'stock', Goods::FULL_STOCK]
  811. ]);
  812. }
  813. }
  814. //云打印 ssh 20210709
  815. public static function onlinePrint($order, $must = false, $port = 'front')
  816. {
  817. if (empty($order)) {
  818. return false;
  819. }
  820. if (!$must) {
  821. if ($order->needPrint != dict::getDict('needPrint', 'need')) {
  822. return false;
  823. }
  824. if ($order->payStatus != 1) {
  825. return false;
  826. }
  827. }
  828. if ($order->forward == 1) {
  829. //售后付款单暂时不能打印
  830. return false;
  831. }
  832. $shopId = $order->shopId ?? 0;
  833. $shop = ShopClass::getById($shopId, true);
  834. $sjId = $shop->sjId ?? 0;
  835. $sj = SjClass::getById($sjId, true);
  836. $sjName = $sj->name ?? '';
  837. $shopName = $shop->shopName ?? '';
  838. $currentName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  839. $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
  840. $printSn = $ext['printSn'] ?? '';
  841. if ($port == 'make') {
  842. $printSn = $ext['makePrintSn'] ?? '';
  843. if ($order->fromType == 4) {
  844. $printSn = $ext['mtPrintSn'] ?? '';
  845. }
  846. $workList = WorkClass::getAllByCondition(['orderId' => $order->id], null, '*', null, true);
  847. $hasSh = 0;
  848. if (!empty($workList)) {
  849. foreach ($workList as $work) {
  850. $sh = $work->sh ?? 0;
  851. if ($sh == 1) {
  852. $hasSh = 1;
  853. }
  854. }
  855. }
  856. if ($hasSh == 1) {
  857. $printSn = $ext['printSn'] ?? '';
  858. }
  859. }
  860. if (empty($printSn)) {
  861. return false;
  862. }
  863. $order->printNum += 1;
  864. $order->save();
  865. $customId = $order->customId ?? 0;
  866. $custom = CustomClass::getById($customId);
  867. $debtAmount = $custom['debtAmount'] ?? 0;
  868. $respond = self::getPrintData($order, $debtAmount);
  869. $fromType = $order->fromType ?? dict::getDict('fromType', 'shop');
  870. $fromTypeMap = dict::getDict('fromTypeMap');
  871. $fromTypeName = $fromTypeMap[$fromType] ?? '门店';
  872. //客服下的单则显示客服号和客户名称
  873. if ($fromType == dict::getDict('fromType', 'friend')) {
  874. $staffName = $order->shopAdminName ?? '';
  875. $bookName = $order->bookName ?? '';
  876. $fromTypeName = $staffName . ' ' . $bookName;
  877. }
  878. $content = '';
  879. //$content .= "<CB>【零售】</CB><BR>";
  880. if ($fromType == dict::getDict('fromType', 'mt')) {
  881. $content .= '<B>美团 ' . $order->thirdSn . '</B><BR>';
  882. if (isset($order->goodsNum) && $order->goodsNum > 1) {
  883. $content .= '<B>美团 ' . $order->thirdSn . ' (' . $order->goodsNum . '份)</B><BR>';
  884. }
  885. $content .= '--------------------------------<BR>';
  886. $content .= date("Y-m-d", strtotime($order->addTime)) . '<BR>';
  887. } else {
  888. $content .= '<CB>' . $currentName . '</CB><BR>';
  889. $content .= '<CB>销售单</CB><BR><BR>';
  890. $content .= '<CB>' . $fromTypeName . ' ' . $respond['sendNum'] . '</CB><BR>';
  891. $content .= '--------------------------------<BR>';
  892. }
  893. $readPeriod = $order->reachPeriod ?? 0;
  894. if ($order->sendType == 1) {
  895. if (!empty($order->reachDate) && $order->reachDate != '0000-00-00') {
  896. $currentReachDate = date("m-d", strtotime($order->reachDate));
  897. $content .= '<B>' . $currentReachDate . ' ' . $readPeriod . '前</B><BR>';
  898. $content .= '<B>到店自取</B><BR><BR>';
  899. } else {
  900. $content .= '<B>到店自取</B><BR><BR>';
  901. }
  902. } else {
  903. if ($order->sendType == 0) {
  904. $content .= '<B>送货上门</B><BR><BR>';
  905. } else if ($order->sendType == 2) {
  906. $content .= '<B>请跑腿送</B><BR><BR>';
  907. } else if ($order->sendType == 3) {
  908. $content .= '<B>物流到付</B><BR><BR>';
  909. } else if ($order->sendType == 4) {
  910. $content .= '<B>快递到付</B><BR><BR>';
  911. } else {
  912. $content .= '';
  913. }
  914. if (!empty($order->reachDate) && $order->reachDate != '0000-00-00') {
  915. $currentReachDate = date("m-d", strtotime($order->reachDate));
  916. $content .= '<B>' . $currentReachDate . ' ' . $readPeriod . '前</B><BR><BR>';
  917. }
  918. }
  919. $defaultCustomId = $shop->defaultCustomId ?? 0;
  920. $fastOrder = $defaultCustomId == $customId ? 1 : 0;
  921. //快捷开单不需要显示这些东西
  922. if ($fastOrder == 0) {
  923. if (!empty($order->receiveUserName)) {
  924. $content .= '收花人:<BR>';
  925. $content .= '<B>' . $order->receiveUserName . '</B><BR>';
  926. }
  927. if (!empty($order->receiveMobile)) {
  928. $content .= '<B>' . $order->receiveMobile . '</B><BR><BR>';
  929. }
  930. if (!empty($order->fullAddress)) {
  931. $content .= '<B>' . $order->fullAddress . '(' . $order->showAddress . ')' . '</B><BR><BR>';
  932. }
  933. if (!empty($order->cardInfo)) {
  934. $content .= '<B>贺卡内容:' . $order->cardInfo . '</B><BR><BR>';
  935. }
  936. if ($order->anonymity == 1) {
  937. $content .= '<B>匿名派送!!</B><BR><BR>';
  938. } else {
  939. if (!empty($order->bookMobile)) {
  940. $bookName = $order->bookName ?? '';
  941. $bookMobile = $order->bookMobile;
  942. $content .= '订花人:<BR>';
  943. if (!empty($bookName)) {
  944. $content .= '<B>' . $bookName . '</B><BR>';
  945. }
  946. $content .= '<B>' . $bookMobile . '</B><BR>';
  947. }
  948. }
  949. }
  950. if (!empty($respond['remark'])) {
  951. $content .= '<BR>';
  952. $content .= '备注:<BR>';
  953. $content .= '<B>' . $respond['remark'] . '</B><BR>';
  954. }
  955. $content .= '<BR>';
  956. $content .= '商品 数量/单价 金额 <BR>';
  957. $content .= '--------------------------------<BR>';
  958. if (!empty($respond['product'])) {
  959. foreach ($respond['product'] as $current) {
  960. $content = self::printGroup($current, $content);
  961. }
  962. $content .= '--------------------------------<BR>';
  963. }
  964. if (!empty($respond['refund'])) {
  965. $content .= '<B>已退款:</B><BR>';
  966. foreach ($respond['refund'] as $current) {
  967. //58mm的机器,一行打印16个汉字,32个字母
  968. $name = $current['name'] ?? '';
  969. $content .= $name . '<BR>';
  970. $name = str_repeat(' ', 10);
  971. $productNum = $current['num'] ?? '';
  972. $blankNum = bcsub(12, strlen($productNum));
  973. if ($blankNum > 0) {
  974. $productNum = $productNum . str_repeat(' ', $blankNum);
  975. }
  976. $productPrice = $current['price'] ?? '';
  977. $blankNum = bcsub(7, strlen($productPrice));
  978. if ($blankNum > 0) {
  979. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  980. }
  981. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  982. }
  983. $content .= '--------------------------------<BR>';
  984. }
  985. if (isset($order->goodsPrice) && $order->goodsPrice > 0) {
  986. $content .= '商品合计:' . floatval($order->goodsPrice) . '<BR>';
  987. }
  988. if (isset($respond['sendCost']) && $respond['sendCost'] > 0 && $order->sendType == 2) {
  989. $content .= '运费:' . floatval($respond['sendCost']) . '<BR>';
  990. }
  991. if (isset($order->serviceFee) && $order->serviceFee > 0) {
  992. $content .= '手续费:' . floatval($order->serviceFee) . '<BR>';
  993. }
  994. if (isset($order->labourCost) && $order->labourCost > 0) {
  995. $content .= '包装费:' . floatval($order->labourCost) . '<BR>';
  996. }
  997. if (isset($order->prePrice) && $order->prePrice > 0) {
  998. $content .= '总计:' . floatval($order->prePrice) . '<BR>';
  999. }
  1000. if (!empty($respond['discountAmount']) && $respond['discountAmount'] > 0) {
  1001. $content .= '优惠扣除:-' . floatval($respond['discountAmount']) . '<BR>';
  1002. }
  1003. $content .= '订单金额:' . floatval($respond['orderPrice']) . '<BR>';
  1004. if (isset($respond['refundPrice']) && $respond['refundPrice'] > 0) {
  1005. $content .= '退款金额:' . floatval($respond['refundPrice']) . '<BR>';
  1006. }
  1007. if (isset($respond['debt']) && $respond['debt'] == 1) {
  1008. $content .= '实付金额:0<BR>';
  1009. } else {
  1010. $content .= '实付金额:' . floatval($respond['realPrice']) . '<BR>';
  1011. }
  1012. if (isset($order->remainDebtPrice) && $order->remainDebtPrice > 0) {
  1013. $content .= '<B>待付尾款:' . floatval($order->remainDebtPrice) . '</B><BR>';
  1014. }
  1015. $content .= '--------------------------------<BR>';
  1016. $balance = $custom['balance'] ?? 0;
  1017. $balance = floatval($balance);
  1018. if ($balance > 0) {
  1019. $content .= '当前账户余额:' . $balance . '元<BR>';
  1020. }
  1021. $content .= '订单日期:' . $order->addTime . '<BR>';
  1022. $content .= '订单编号:' . $respond['orderSn'] . '<BR>';
  1023. $content .= '门店名称:' . $currentName . '<BR>';
  1024. $content .= "<BR>";
  1025. //noticeUtil::push($content, '15280215347');
  1026. $p = new printUtil($printSn);
  1027. $orderSn = $order->orderSn ?? '';
  1028. $workList = WorkClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  1029. if (!empty($workList)) {
  1030. $content .= "<BR>";
  1031. $content .= '--------------------------------<BR>';
  1032. foreach ($workList as $work) {
  1033. $goodsSn = $work->goodsSn ?? '';
  1034. $sn = $work->sn ?? '';
  1035. $content .= '<B>制作号:' . $sn . '<B>';
  1036. // 1-523 有-特殊符号,使用飞鹅自带的函数处理
  1037. $content .= $p->bar_code($goodsSn);
  1038. $content .= "<BR>";
  1039. }
  1040. }
  1041. $p->printMsg($content);
  1042. }
  1043. //打印数据 ssh 20210702
  1044. public static function getPrintData($order, $debtAmount)
  1045. {
  1046. $orderSn = $order->orderSn ?? '';
  1047. $printProduct = [];
  1048. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  1049. if (!empty($orderItemList)) {
  1050. foreach ($orderItemList as $itemKey => $itemVal) {
  1051. $unitName = $itemVal['unitName'] ?? '';
  1052. $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
  1053. $printProduct[] = [
  1054. 'name' => $itemVal['name'] ?? '',
  1055. 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
  1056. 'price' => floatval($itemVal['price']),
  1057. 'sn' => '',
  1058. ];
  1059. }
  1060. }
  1061. $orderGoodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  1062. if (!empty($orderGoodsList)) {
  1063. foreach ($orderGoodsList as $itemKey => $itemVal) {
  1064. $unitName = $itemVal['unitName'] ?? '份';
  1065. $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
  1066. $sn = $itemVal['sn'] ?? '';
  1067. $printProduct[] = [
  1068. 'name' => $itemVal['name'] ?? '',
  1069. 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
  1070. 'price' => floatval($itemVal['price']),
  1071. 'sn' => $sn,
  1072. ];
  1073. }
  1074. }
  1075. $sendNum = $order['sendNum'] ?? '';
  1076. //退款记录
  1077. $refundList = [];
  1078. $printData = [
  1079. 'address' => '',
  1080. 'sendCost' => $order->sendCost ?? '0.00',
  1081. 'prePrice' => $order->prePrice ?? '0.00',
  1082. 'orderPrice' => $order->orderPrice ?? '0',
  1083. 'discountAmount' => $order->discountAmount ?? '0.00',
  1084. 'realPrice' => $order->realPrice ?? '0.00',
  1085. 'refundPrice' => $order->refundPrice ?? '0.00',
  1086. 'debt' => $order->debt ?? 0,
  1087. 'orderSn' => $order->orderSn ?? '',
  1088. 'date' => $order->addTime ?? '',
  1089. 'remark' => $order->remark ?? '',
  1090. 'custom' => [
  1091. 'customName' => $order->customName ?? '',
  1092. 'customMobile' => $order->customMobile ?? '',
  1093. 'fullAddress' => $order->fullAddress ?? '',
  1094. ],
  1095. 'product' => $printProduct,
  1096. 'refund' => $refundList,
  1097. 'sendNum' => $sendNum,
  1098. 'debtAmount' => $debtAmount,
  1099. ];
  1100. return $printData;
  1101. }
  1102. public static function printGroup($current, $content)
  1103. {
  1104. //58mm的机器,一行打印16个汉字,32个字母
  1105. $name = $current['name'] ?? '';
  1106. if (!empty($current['sn'])) {
  1107. $name = '#' . $current['sn'] . ' ' . $name;
  1108. }
  1109. $content .= $name . '<BR>';
  1110. $name = str_repeat(' ', 10);
  1111. $productNum = $current['num'] ?? '';
  1112. $blankNum = bcsub(12, strlen($productNum));
  1113. if ($blankNum > 0) {
  1114. $productNum = $productNum . str_repeat(' ', $blankNum);
  1115. }
  1116. $productPrice = $current['price'] ?? '';
  1117. $blankNum = bcsub(7, strlen($productPrice));
  1118. if ($blankNum > 0) {
  1119. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  1120. }
  1121. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR><BR>';
  1122. return $content;
  1123. }
  1124. //扫码收款的,减少库存 ssh 20250822
  1125. public static function reduceMyStock($data, $shop, $custom)
  1126. {
  1127. $mainId = $shop->mainId;
  1128. $customId = $custom->id;
  1129. $orderId = $data['orderId'] ?? '';
  1130. $sjId = $shop->sjId;
  1131. $hdId = $custom->hdId;
  1132. $shopId = $shop->id;
  1133. $order = self::getById($orderId, true);
  1134. if (empty($order)) {
  1135. util::fail('没有找到订单');
  1136. }
  1137. if ($order->mainId != $shop->mainId) {
  1138. util::fail('并不是你的订单');
  1139. }
  1140. if ($order->repeat == 0) {
  1141. util::fail('已经扣过了');
  1142. }
  1143. if ($order->tkPrice > 0) {
  1144. util::fail('已经发生退款,无法操作');
  1145. }
  1146. if ($order->customId != $customId) {
  1147. util::fail('订单对应客户有问题,无法扣库存,编号:' . $customId . ' ' . $order->customId);
  1148. }
  1149. $orderSn = $order->orderSn ?? '';
  1150. $product = $data['product'] ?? [];
  1151. if (empty($product)) {
  1152. util::fail('请选择商品呢');
  1153. }
  1154. $orderItemList = OrderItemClass::getAllbyCondition(['orderSn' => $orderSn], null, '*', null, true);
  1155. if (!empty($orderItemList)) {
  1156. foreach ($orderItemList as $orderItem) {
  1157. $orderItem->unitPrice = 0;
  1158. $orderItem->save();
  1159. }
  1160. }
  1161. $actPrice = $order->actPrice ?? 0;
  1162. if ($actPrice <= 0) {
  1163. util::fail('订单金额为0');
  1164. }
  1165. $ids = array_column($product, 'productId');
  1166. $productInfo = ItemClass::getByIds($ids, null, 'id');
  1167. $goodsPrice = 0;
  1168. foreach ($product as $key => $val) {
  1169. $property = $val['property'] ?? 0;
  1170. if ($property != 1) {
  1171. util::fail('暂时只支持花材');
  1172. }
  1173. $unitPrice = $val['unitPrice'] ?? 0;
  1174. $unitType = $val['unitType'] ?? 0;
  1175. $currentId = $val['productId'] ?? 0;
  1176. $num = $val['num'] ?? 0;
  1177. $currentInfo = $productInfo[$currentId] ?? [];
  1178. if (empty($currentInfo)) {
  1179. util::fail('存在无效花材,编号:' . $currentId);
  1180. }
  1181. $name = $currentInfo['name'] ?? '';
  1182. $cover = $currentInfo['cover'] ?? '';
  1183. $ratio = $currentInfo['ratio'] ?? 20;
  1184. $itemMainId = $currentInfo['mainId'] ?? 0;
  1185. if ($itemMainId != $mainId) {
  1186. util::fail('不是您的花材,请勿使用,编号:' . $mainId . ' ' . $itemMainId);
  1187. }
  1188. if ($unitType == 0) {
  1189. $unitName = $currentInfo['bigUnit'] ?? '';
  1190. $unitId = $currentInfo['bigUnitId'] ?? 0;
  1191. $totalNum = $num;
  1192. } else {
  1193. $unitName = $currentInfo['smallUnit'] ?? '';
  1194. $unitId = $currentInfo['smallUnitId'] ?? 0;
  1195. $totalNum = bcdiv($num, $ratio, 2);
  1196. }
  1197. $ptItemId = $currentInfo['itemId'] ?? 0;
  1198. $classId = $currentInfo['classId'] ?? 0;
  1199. $belongCost = $currentInfo['belongCost'] ?? 0;
  1200. $currentPrice = bcmul($unitPrice, $num, 2);
  1201. $goodsPrice = bcadd($goodsPrice, $currentPrice, 2);
  1202. $currentItemData = [
  1203. 'name' => $name,
  1204. 'cover' => $cover,
  1205. 'ratio' => $ratio,
  1206. 'unitType' => $unitType,
  1207. 'unitName' => $unitName,
  1208. 'unitId' => $unitId,
  1209. 'unitPrice' => $unitPrice,
  1210. 'num' => $num,
  1211. 'orderSn' => $orderSn,
  1212. 'itemId' => $currentId,
  1213. 'ptItemId' => $ptItemId,
  1214. 'totalNum' => $totalNum,
  1215. 'totalPrice' => 0,
  1216. 'customId' => $customId,
  1217. 'hdId' => $hdId,
  1218. 'sjId' => $sjId,
  1219. 'shopId' => $shopId,
  1220. 'mainId' => $mainId,
  1221. 'classId' => $classId,
  1222. 'price' => $currentPrice,
  1223. 'belongCost' => $belongCost,
  1224. ];
  1225. //库存变化在这里
  1226. OrderItemClass::addData($currentItemData, $custom);
  1227. }
  1228. $labourCost = 0;
  1229. $discountType = 0;
  1230. $discountAmount = 0;
  1231. if ($goodsPrice > $actPrice) {
  1232. $prePrice = $goodsPrice;
  1233. $discountAmount = bcsub($goodsPrice, $actPrice, 2);
  1234. if ($discountAmount > 0) {
  1235. $discountType = 3;
  1236. }
  1237. } else {
  1238. $prePrice = $actPrice;
  1239. $labourCost = bcsub($actPrice, $goodsPrice, 2);
  1240. }
  1241. $order->goodsPrice = $goodsPrice;
  1242. $order->prePrice = $prePrice;
  1243. $order->labourCost = $labourCost;
  1244. $order->discountAmount = $discountAmount;
  1245. $order->discountType = $discountType;
  1246. $order->repeat = 0;
  1247. $order->save();
  1248. return $order;
  1249. }
  1250. public static function debtCount($mainId)
  1251. {
  1252. $debtAmount = 0;
  1253. $orders = self::getAllByCondition(['mainId' => $mainId, 'status' => 4, 'debt' => 1], null, 'remainDebtPrice', null, true);
  1254. foreach ($orders as $order) {
  1255. $remainDebtPrice = $order->remainDebtPrice ?? 0;
  1256. $debtAmount = bcadd($debtAmount, $remainDebtPrice, 2);
  1257. }
  1258. return $debtAmount;
  1259. }
  1260. }