OrderClass.php 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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. $book = $data['book'] ?? 0;
  257. if ($book == 0) {
  258. $actPrice = $data['actPrice'];
  259. $meetNum = $shop->meetNum ?? 0;
  260. $meetAmount = $shop->meetAmount ?? 0;
  261. $cutOption = $shop->cutAmount ?? 0;
  262. $cutStyle = $shop->cutStyle ?? 0;
  263. if ($cutStyle == 0) {
  264. $cutAmount = $cutOption;
  265. } else {
  266. $miniOption = bcsub(1, $cutOption, 2);
  267. $cutAmount = bcmul($actPrice, $miniOption, 2);
  268. }
  269. if ($cutAmount > 0 && $meetNum >= 0 && $meetAmount > 0) {
  270. if (isset($data['bigNum']) && $data['bigNum'] >= $meetNum && $data['actPrice'] >= $meetAmount) {
  271. if ($cutAmount < $actPrice) {
  272. $actPrice = bcsub($actPrice, $cutAmount, 2);
  273. $data['orderReachDiscountPrice'] = $cutAmount;
  274. $data['actPrice'] = $actPrice;
  275. $data['realPrice'] = $actPrice;
  276. $data['mainPay'] = $actPrice;
  277. } else {
  278. noticeUtil::push('订单金额不足满减金额,customId:' . $data['customId'], '15280215347');
  279. }
  280. }
  281. }
  282. }
  283. //将花店每月的总订单数作为编号
  284. $riseNum = StatOrderCountClass::addOrder($shop, $main);
  285. $data['sendNum'] = date("j") . $riseNum;
  286. $data['totalFlow'] = OrderClass::ORDER_TOTAL_FLOW;
  287. $salt = stringUtil::charsShuffleLowerCase(10);
  288. $data['salt'] = $salt;
  289. return self::add($data, true);
  290. }
  291. public static function getByOrderSn($orderSn)
  292. {
  293. return self::getByCondition(['orderSn' => $orderSn]);
  294. }
  295. //评价 ssh 2019.12.16
  296. public static function comment($data)
  297. {
  298. $id = $data['id'];
  299. unset($data['id']);
  300. return self::updateById($id, $data);
  301. }
  302. public static function getFullInfo($id)
  303. {
  304. $order = self::getById($id);
  305. if (empty($order)) {
  306. return [];
  307. }
  308. $sn = $order['orderSn'] ?? '';
  309. $order['goodsInfoList'] = OrderGoodsClass::getListBySn($sn);
  310. $order['itemInfoList'] = OrderItemClass::getListBySn($sn);
  311. $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*');
  312. if (!empty($workList)) {
  313. foreach ($workList as $key => $work) {
  314. $shortCover = $work['cover'] ?? '';
  315. $workList[$key]['shortCover'] = $shortCover;
  316. $workList[$key]['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  317. }
  318. }
  319. $order['workList'] = $workList;
  320. $debtPrice = $order['debtPrice'] ?? 0;
  321. $remainDebtPrice = $order['remainDebtPrice'] ?? 0;
  322. $orderId = $order['id'] ?? 0;
  323. $osList = [];
  324. if ($debtPrice > $remainDebtPrice) {
  325. $osList = OrderSettleClass::getAllByCondition(['orderId' => $orderId, 'status' => 1], null, '*', null, true);
  326. }
  327. //结账记录关系
  328. $order['orderSettleList'] = $osList;
  329. $order['today'] = date("Y-m-d");
  330. $order['tomorrow'] = date("Y-m-d", strtotime("+1 day"));
  331. return $order;
  332. }
  333. //订单
  334. public static function getOrderById($id)
  335. {
  336. $order = self::getById($id);
  337. if (empty($order)) {
  338. return [];
  339. }
  340. $sn = $order['orderSn'] ?? '';
  341. $order['goodsInfoList'] = OrderGoodsClass::getListBySn($sn);
  342. return $order;
  343. }
  344. //根据订单查询 ssh 2020.1.4
  345. public static function getOrderBySn($orderSn)
  346. {
  347. $order = self::getByCondition(['orderSn' => $orderSn]);
  348. if (empty($order)) {
  349. return [];
  350. }
  351. $id = $order['id'];
  352. $order['goodsInfoList'] = OrderGoodsClass::getGoodsListById($id);
  353. return $order;
  354. }
  355. //返回随机优惠金额 ssh 2019.9.12
  356. public static function getRandDiscount($amount)
  357. {
  358. if ($amount <= 0) {
  359. return 0;
  360. }
  361. $randDiscount = self::$randDiscount;
  362. krsort($randDiscount);
  363. $rand = 0;
  364. foreach ($randDiscount as $currentAmount => $val) {
  365. if ($amount >= $currentAmount) {
  366. $rand = rand($val[0], $val[1]);
  367. break;
  368. }
  369. }
  370. return $rand / 10;
  371. }
  372. //第三方支付后回调处理流程 ssh 2021.4.30
  373. public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId)
  374. {
  375. $order = self::getByCondition(['orderSn' => $orderSn], true);
  376. if (empty($order)) {
  377. noticeUtil::push('散客下单,付款成功,支付回调,单号:' . $orderSn . ' 金额:' . $totalFee . ' 回调id:' . $transactionId . ' 附件:' . $attach . ',没有找到订单', '15280215347');
  378. $msg = "没有找到零售订单 orderSn:{$orderSn}";
  379. Yii::info($msg);
  380. util::fail($msg);
  381. }
  382. if ($order->mainPay != $totalFee) {
  383. noticeUtil::push("散客下单,付款成功,支付回调错误,零售订单金额与回调通知金额不一致 orderSn:{$orderSn} {$order->mainPay} {$totalFee} 附件:{$attach}", '15280215347');
  384. $msg = "零售订单金额与回调通知金额不一致 orderSn:{$orderSn} {$order->mainPay} {$totalFee}";
  385. Yii::info($msg);
  386. util::fail($msg);
  387. }
  388. $id = $order->id;
  389. $order = self::getLockById($id);
  390. $date = date("Y-m-d H:i:s");
  391. $order->payTime = $date;
  392. $order->thirdNo = $transactionId;
  393. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  394. $order->save();
  395. $params = [];
  396. self::payAfter($order, $payWay, $params);
  397. }
  398. //订单完成 ssh 2021.4.20
  399. public static function payAfter($order, $payWay, $params = [])
  400. {
  401. if (empty($order)) {
  402. util::fail('没有找到订单');
  403. }
  404. if (!isset($order->status)) {
  405. util::fail('没有找到订单的状态');
  406. }
  407. $orderSn = $order->orderSn ?? '';
  408. if ($order->status == self::ORDER_STATUS_CANCEL) {
  409. $shopId = $order->shopId;
  410. $shop = ShopClass::getById($shopId, true);
  411. if (empty($shop)) {
  412. noticeUtil::push('注意,零售订单:' . $orderSn . ',付款成功但订单已取消,没有找到门店,关键词 cancel_get_money', '15280215347');
  413. util::stop("SUCCESS");
  414. }
  415. Yii::$app->params['errorReport'] = 1;
  416. try {
  417. //恢复取消的订单
  418. self::recoverOrderToPay($order);
  419. noticeUtil::push('注意,零售订单:' . $orderSn . ',付款成功,取消的订单,已恢复 SUCCESS', '15280215347');
  420. } catch (\Exception $exception) {
  421. $ret = HdRefundClass::onlyRefundAmount($shop, $order);
  422. if ($ret['status'] == 0) {
  423. $errMsg = $exception->getMessage();
  424. noticeUtil::push('注意,零售订单:' . $orderSn . ',付款成功但订单取消。订单没有恢复成功,原因' . $errMsg . ',也没有退款成功。请手动处理。关键词 cancel_get_money', '15280215347');
  425. }
  426. util::stop("SUCCESS");
  427. }
  428. }
  429. if ($order->payStatus == 1) {
  430. noticeUtil::push('注意,零售订单:' . $orderSn . ',已经付过款了,无需重复请求和操作', '15280215347');
  431. util::fail('订单已经付过款了');
  432. }
  433. $orderId = $order->id ?? 0;
  434. $customId = $order->customId ?? 0;
  435. $order->status = self::ORDER_STATUS_UN_SEND; // 付款后,订单状态设置为:已付款(待配送)
  436. $order->payWay = $payWay;
  437. $order->payStatus = 1;
  438. if (!empty($params['historyDate'])) {
  439. $order->payTime = $params['historyDate'] . ' ' . date("H:i:s");
  440. } else {
  441. $order->payTime = date("Y-m-d H:i:s");
  442. }
  443. $order->stockChange = 1;
  444. $order->save();
  445. $myCustom = CustomClass::getLockById($customId);
  446. if (!empty($myCustom)) {
  447. $customName = $myCustom->name ?? $order->customName ?? '';
  448. $orderSnText = $order->orderSn ?? '';
  449. $shopAdminId = $order->shopAdminId ?? 0;
  450. $shopAdminName = $order->shopAdminName ?? '';
  451. $eventPrefix = $customName;
  452. if ($eventPrefix === '' && $orderSnText !== '') {
  453. $eventPrefix = '客户';
  454. }
  455. if ($order->forward == 0) { //0 常规订单
  456. $myCustom->recentExpend = date("Y-m-d H:i:s");
  457. $myCustom->visitTime = date("Y-m-d H:i:s");
  458. if ($payWay == dict::getDict('payWay', 'balancePay')) {
  459. $myCustom->recordGrowthAndIntegral = false;
  460. }
  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>' . $currentName . '</CB><BR>';
  878. $content .= '<CB>销售单</CB><BR><BR>';
  879. $content .= '<CB>' . $fromTypeName . ' ' . $respond['sendNum'] . '</CB><BR>';
  880. $content .= '--------------------------------<BR>';
  881. }
  882. $readPeriod = $order->reachPeriod ?? 0;
  883. if ($order->sendType == 1) {
  884. if (!empty($order->reachDate) && $order->reachDate != '0000-00-00') {
  885. $currentReachDate = date("m-d", strtotime($order->reachDate));
  886. $content .= '<B>' . $currentReachDate . ' ' . $readPeriod . '前</B><BR>';
  887. $content .= '<B>到店自取</B><BR><BR>';
  888. } else {
  889. $content .= '<B>到店自取</B><BR><BR>';
  890. }
  891. } else {
  892. if ($order->sendType == 0) {
  893. $content .= '<B>送货上门</B><BR><BR>';
  894. } else if ($order->sendType == 2) {
  895. $content .= '<B>请跑腿送</B><BR><BR>';
  896. } else if ($order->sendType == 3) {
  897. $content .= '<B>物流到付</B><BR><BR>';
  898. } else if ($order->sendType == 4) {
  899. $content .= '<B>快递到付</B><BR><BR>';
  900. } else {
  901. $content .= '';
  902. }
  903. if (!empty($order->reachDate) && $order->reachDate != '0000-00-00') {
  904. $currentReachDate = date("m-d", strtotime($order->reachDate));
  905. $content .= '<B>' . $currentReachDate . ' ' . $readPeriod . '前</B><BR><BR>';
  906. }
  907. }
  908. $defaultCustomId = $shop->defaultCustomId ?? 0;
  909. $fastOrder = $defaultCustomId == $customId ? 1 : 0;
  910. //快捷开单不需要显示这些东西
  911. if ($fastOrder == 0) {
  912. if (!empty($order->receiveUserName)) {
  913. $content .= '收花人:<BR>';
  914. $content .= '<B>' . $order->receiveUserName . '</B><BR>';
  915. }
  916. if (!empty($order->receiveMobile)) {
  917. $content .= '<B>' . $order->receiveMobile . '</B><BR><BR>';
  918. }
  919. if (!empty($order->fullAddress)) {
  920. $content .= '<B>' . $order->fullAddress . '(' . $order->showAddress . ')' . '</B><BR><BR>';
  921. }
  922. if (!empty($order->cardInfo)) {
  923. $content .= '<B>贺卡内容:' . $order->cardInfo . '</B><BR><BR>';
  924. }
  925. if ($order->anonymity == 1) {
  926. $content .= '<B>匿名派送!!</B><BR><BR>';
  927. } else {
  928. if (!empty($order->bookMobile)) {
  929. $bookName = $order->bookName ?? '';
  930. $bookMobile = $order->bookMobile;
  931. $content .= '订花人:<BR>';
  932. if (!empty($bookName)) {
  933. $content .= '<B>' . $bookName . '</B><BR>';
  934. }
  935. $content .= '<B>' . $bookMobile . '</B><BR>';
  936. }
  937. }
  938. }
  939. if (!empty($respond['remark'])) {
  940. $content .= '<BR>';
  941. $content .= '备注:<BR>';
  942. $content .= '<B>' . $respond['remark'] . '</B><BR>';
  943. }
  944. $content .= '<BR>';
  945. $content .= '商品 数量/单价 金额 <BR>';
  946. $content .= '--------------------------------<BR>';
  947. if (!empty($respond['product'])) {
  948. foreach ($respond['product'] as $current) {
  949. $content = self::printGroup($current, $content);
  950. }
  951. $content .= '--------------------------------<BR>';
  952. }
  953. if (!empty($respond['refund'])) {
  954. $content .= '<B>已退款:</B><BR>';
  955. foreach ($respond['refund'] as $current) {
  956. //58mm的机器,一行打印16个汉字,32个字母
  957. $name = $current['name'] ?? '';
  958. $content .= $name . '<BR>';
  959. $name = str_repeat(' ', 10);
  960. $productNum = $current['num'] ?? '';
  961. $blankNum = bcsub(12, strlen($productNum));
  962. if ($blankNum > 0) {
  963. $productNum = $productNum . str_repeat(' ', $blankNum);
  964. }
  965. $productPrice = $current['price'] ?? '';
  966. $blankNum = bcsub(7, strlen($productPrice));
  967. if ($blankNum > 0) {
  968. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  969. }
  970. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  971. }
  972. $content .= '--------------------------------<BR>';
  973. }
  974. if (isset($order->goodsPrice) && $order->goodsPrice > 0) {
  975. $content .= '商品合计:' . floatval($order->goodsPrice) . '<BR>';
  976. }
  977. if (isset($respond['sendCost']) && $respond['sendCost'] > 0 && $order->sendType == 2) {
  978. $content .= '运费:' . floatval($respond['sendCost']) . '<BR>';
  979. }
  980. if (isset($order->serviceFee) && $order->serviceFee > 0) {
  981. $content .= '手续费:' . floatval($order->serviceFee) . '<BR>';
  982. }
  983. if (isset($order->labourCost) && $order->labourCost > 0) {
  984. $content .= '包装费:' . floatval($order->labourCost) . '<BR>';
  985. }
  986. if (isset($order->prePrice) && $order->prePrice > 0) {
  987. $content .= '总计:' . floatval($order->prePrice) . '<BR>';
  988. }
  989. if (!empty($respond['discountAmount']) && $respond['discountAmount'] > 0) {
  990. $content .= '优惠扣除:-' . floatval($respond['discountAmount']) . '<BR>';
  991. }
  992. $content .= '订单金额:' . floatval($respond['orderPrice']) . '<BR>';
  993. if (isset($respond['refundPrice']) && $respond['refundPrice'] > 0) {
  994. $content .= '退款金额:' . floatval($respond['refundPrice']) . '<BR>';
  995. }
  996. if (isset($respond['debt']) && $respond['debt'] == 1) {
  997. $content .= '实付金额:0<BR>';
  998. } else {
  999. $content .= '实付金额:' . floatval($respond['realPrice']) . '<BR>';
  1000. }
  1001. if (isset($order->remainDebtPrice) && $order->remainDebtPrice > 0) {
  1002. $content .= '<B>待付尾款:' . floatval($order->remainDebtPrice) . '</B><BR>';
  1003. }
  1004. $content .= '--------------------------------<BR>';
  1005. $balance = $custom['balance'] ?? 0;
  1006. $balance = floatval($balance);
  1007. if ($balance > 0) {
  1008. $content .= '当前账户余额:' . $balance . '元<BR>';
  1009. }
  1010. $content .= '订单日期:' . $order->addTime . '<BR>';
  1011. $content .= '订单编号:' . $respond['orderSn'] . '<BR>';
  1012. $content .= '门店名称:' . $currentName . '<BR>';
  1013. $content .= "<BR>";
  1014. //noticeUtil::push($content, '15280215347');
  1015. $p = new printUtil($printSn);
  1016. $orderSn = $order->orderSn ?? '';
  1017. $workList = WorkClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  1018. if (!empty($workList)) {
  1019. $content .= "<BR>";
  1020. $content .= '--------------------------------<BR>';
  1021. foreach ($workList as $work) {
  1022. $goodsSn = $work->goodsSn ?? '';
  1023. $sn = $work->sn ?? '';
  1024. $content .= '<B>制作号:' . $sn . '<B>';
  1025. // 1-523 有-特殊符号,使用飞鹅自带的函数处理
  1026. $content .= $p->bar_code($goodsSn);
  1027. $content .= "<BR>";
  1028. }
  1029. }
  1030. $p->printMsg($content);
  1031. }
  1032. //打印数据 ssh 20210702
  1033. public static function getPrintData($order, $debtAmount)
  1034. {
  1035. $orderSn = $order->orderSn ?? '';
  1036. $printProduct = [];
  1037. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  1038. if (!empty($orderItemList)) {
  1039. foreach ($orderItemList as $itemKey => $itemVal) {
  1040. $unitName = $itemVal['unitName'] ?? '';
  1041. $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
  1042. $printProduct[] = [
  1043. 'name' => $itemVal['name'] ?? '',
  1044. 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
  1045. 'price' => floatval($itemVal['price']),
  1046. 'sn' => '',
  1047. ];
  1048. }
  1049. }
  1050. $orderGoodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  1051. if (!empty($orderGoodsList)) {
  1052. foreach ($orderGoodsList as $itemKey => $itemVal) {
  1053. $unitName = $itemVal['unitName'] ?? '份';
  1054. $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
  1055. $sn = $itemVal['sn'] ?? '';
  1056. $printProduct[] = [
  1057. 'name' => $itemVal['name'] ?? '',
  1058. 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
  1059. 'price' => floatval($itemVal['price']),
  1060. 'sn' => $sn,
  1061. ];
  1062. }
  1063. }
  1064. $sendNum = $order['sendNum'] ?? '';
  1065. //退款记录
  1066. $refundList = [];
  1067. $printData = [
  1068. 'address' => '',
  1069. 'sendCost' => $order->sendCost ?? '0.00',
  1070. 'prePrice' => $order->prePrice ?? '0.00',
  1071. 'orderPrice' => $order->orderPrice ?? '0',
  1072. 'discountAmount' => $order->discountAmount ?? '0.00',
  1073. 'realPrice' => $order->realPrice ?? '0.00',
  1074. 'refundPrice' => $order->refundPrice ?? '0.00',
  1075. 'debt' => $order->debt ?? 0,
  1076. 'orderSn' => $order->orderSn ?? '',
  1077. 'date' => $order->addTime ?? '',
  1078. 'remark' => $order->remark ?? '',
  1079. 'custom' => [
  1080. 'customName' => $order->customName ?? '',
  1081. 'customMobile' => $order->customMobile ?? '',
  1082. 'fullAddress' => $order->fullAddress ?? '',
  1083. ],
  1084. 'product' => $printProduct,
  1085. 'refund' => $refundList,
  1086. 'sendNum' => $sendNum,
  1087. 'debtAmount' => $debtAmount,
  1088. ];
  1089. return $printData;
  1090. }
  1091. public static function printGroup($current, $content)
  1092. {
  1093. //58mm的机器,一行打印16个汉字,32个字母
  1094. $name = $current['name'] ?? '';
  1095. if (!empty($current['sn'])) {
  1096. $name = '#' . $current['sn'] . ' ' . $name;
  1097. }
  1098. $content .= $name . '<BR>';
  1099. $name = str_repeat(' ', 10);
  1100. $productNum = $current['num'] ?? '';
  1101. $blankNum = bcsub(12, strlen($productNum));
  1102. if ($blankNum > 0) {
  1103. $productNum = $productNum . str_repeat(' ', $blankNum);
  1104. }
  1105. $productPrice = $current['price'] ?? '';
  1106. $blankNum = bcsub(7, strlen($productPrice));
  1107. if ($blankNum > 0) {
  1108. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  1109. }
  1110. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR><BR>';
  1111. return $content;
  1112. }
  1113. //扫码收款的,减少库存 ssh 20250822
  1114. public static function reduceMyStock($data, $shop, $custom)
  1115. {
  1116. $mainId = $shop->mainId;
  1117. $customId = $custom->id;
  1118. $orderId = $data['orderId'] ?? '';
  1119. $sjId = $shop->sjId;
  1120. $hdId = $custom->hdId;
  1121. $shopId = $shop->id;
  1122. $order = self::getById($orderId, true);
  1123. if (empty($order)) {
  1124. util::fail('没有找到订单');
  1125. }
  1126. if ($order->mainId != $shop->mainId) {
  1127. util::fail('并不是你的订单');
  1128. }
  1129. if ($order->repeat == 0) {
  1130. util::fail('已经扣过了');
  1131. }
  1132. if ($order->tkPrice > 0) {
  1133. util::fail('已经发生退款,无法操作');
  1134. }
  1135. if ($order->customId != $customId) {
  1136. util::fail('订单对应客户有问题,无法扣库存,编号:' . $customId . ' ' . $order->customId);
  1137. }
  1138. $orderSn = $order->orderSn ?? '';
  1139. $product = $data['product'] ?? [];
  1140. if (empty($product)) {
  1141. util::fail('请选择商品呢');
  1142. }
  1143. $orderItemList = OrderItemClass::getAllbyCondition(['orderSn' => $orderSn], null, '*', null, true);
  1144. if (!empty($orderItemList)) {
  1145. foreach ($orderItemList as $orderItem) {
  1146. $orderItem->unitPrice = 0;
  1147. $orderItem->save();
  1148. }
  1149. }
  1150. $actPrice = $order->actPrice ?? 0;
  1151. if ($actPrice <= 0) {
  1152. util::fail('订单金额为0');
  1153. }
  1154. $ids = array_column($product, 'productId');
  1155. $productInfo = ItemClass::getByIds($ids, null, 'id');
  1156. $goodsPrice = 0;
  1157. foreach ($product as $key => $val) {
  1158. $property = $val['property'] ?? 0;
  1159. if ($property != 1) {
  1160. util::fail('暂时只支持花材');
  1161. }
  1162. $unitPrice = $val['unitPrice'] ?? 0;
  1163. $unitType = $val['unitType'] ?? 0;
  1164. $currentId = $val['productId'] ?? 0;
  1165. $num = $val['num'] ?? 0;
  1166. $currentInfo = $productInfo[$currentId] ?? [];
  1167. if (empty($currentInfo)) {
  1168. util::fail('存在无效花材,编号:' . $currentId);
  1169. }
  1170. $name = $currentInfo['name'] ?? '';
  1171. $cover = $currentInfo['cover'] ?? '';
  1172. $ratio = $currentInfo['ratio'] ?? 20;
  1173. $itemMainId = $currentInfo['mainId'] ?? 0;
  1174. if ($itemMainId != $mainId) {
  1175. util::fail('不是您的花材,请勿使用,编号:' . $mainId . ' ' . $itemMainId);
  1176. }
  1177. if ($unitType == 0) {
  1178. $unitName = $currentInfo['bigUnit'] ?? '';
  1179. $unitId = $currentInfo['bigUnitId'] ?? 0;
  1180. $totalNum = $num;
  1181. } else {
  1182. $unitName = $currentInfo['smallUnit'] ?? '';
  1183. $unitId = $currentInfo['smallUnitId'] ?? 0;
  1184. $totalNum = bcdiv($num, $ratio, 2);
  1185. }
  1186. $ptItemId = $currentInfo['itemId'] ?? 0;
  1187. $classId = $currentInfo['classId'] ?? 0;
  1188. $belongCost = $currentInfo['belongCost'] ?? 0;
  1189. $currentPrice = bcmul($unitPrice, $num, 2);
  1190. $goodsPrice = bcadd($goodsPrice, $currentPrice, 2);
  1191. $currentItemData = [
  1192. 'name' => $name,
  1193. 'cover' => $cover,
  1194. 'ratio' => $ratio,
  1195. 'unitType' => $unitType,
  1196. 'unitName' => $unitName,
  1197. 'unitId' => $unitId,
  1198. 'unitPrice' => $unitPrice,
  1199. 'num' => $num,
  1200. 'orderSn' => $orderSn,
  1201. 'itemId' => $currentId,
  1202. 'ptItemId' => $ptItemId,
  1203. 'totalNum' => $totalNum,
  1204. 'totalPrice' => 0,
  1205. 'customId' => $customId,
  1206. 'hdId' => $hdId,
  1207. 'sjId' => $sjId,
  1208. 'shopId' => $shopId,
  1209. 'mainId' => $mainId,
  1210. 'classId' => $classId,
  1211. 'price' => $currentPrice,
  1212. 'belongCost' => $belongCost,
  1213. ];
  1214. //库存变化在这里
  1215. OrderItemClass::addData($currentItemData, $custom);
  1216. }
  1217. $labourCost = 0;
  1218. $discountType = 0;
  1219. $discountAmount = 0;
  1220. if ($goodsPrice > $actPrice) {
  1221. $prePrice = $goodsPrice;
  1222. $discountAmount = bcsub($goodsPrice, $actPrice, 2);
  1223. if ($discountAmount > 0) {
  1224. $discountType = 3;
  1225. }
  1226. } else {
  1227. $prePrice = $actPrice;
  1228. $labourCost = bcsub($actPrice, $goodsPrice, 2);
  1229. }
  1230. $order->goodsPrice = $goodsPrice;
  1231. $order->prePrice = $prePrice;
  1232. $order->labourCost = $labourCost;
  1233. $order->discountAmount = $discountAmount;
  1234. $order->discountType = $discountType;
  1235. $order->repeat = 0;
  1236. $order->save();
  1237. return $order;
  1238. }
  1239. public static function debtCount($mainId)
  1240. {
  1241. $debtAmount = 0;
  1242. $orders = self::getAllByCondition(['mainId' => $mainId, 'status' => 4, 'debt' => 1], null, 'remainDebtPrice', null, true);
  1243. foreach ($orders as $order) {
  1244. $remainDebtPrice = $order->remainDebtPrice ?? 0;
  1245. $debtAmount = bcadd($debtAmount, $remainDebtPrice, 2);
  1246. }
  1247. return $debtAmount;
  1248. }
  1249. }