OrderClass.php 53 KB

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