OrderClass.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. <?php
  2. namespace bizGhs\order\classes;
  3. use biz\shop\classes\ShopAdminClass;
  4. use biz\shop\classes\ShopCapitalClass;
  5. use biz\shop\classes\ShopClass;
  6. use biz\shop\classes\ShopExtClass;
  7. use biz\shop\models\Shop;
  8. use biz\sj\classes\SjClass;
  9. use biz\stat\classes\StatItemClass;
  10. use biz\stat\classes\StatKdClass;
  11. use biz\stat\classes\StatKhCgClass;
  12. use biz\stat\classes\StatOrderCountClass;
  13. use biz\stat\classes\StatOutClass;
  14. use biz\stat\classes\StatRefundClass;
  15. use biz\stat\classes\StatSaleClass;
  16. use biz\stock\classes\StockClass;
  17. use bizGhs\custom\models\Custom;
  18. use bizGhs\express\services\DadaExpressServices;
  19. use bizGhs\order\models\Order;
  20. use bizGhs\order\traits\OrderTrait;
  21. use bizGhs\product\classes\ProductClass;
  22. use bizGhs\admin\classes\AdminClass;
  23. use bizGhs\custom\classes\CustomClass;
  24. use bizGhs\shop\classes\MainClass;
  25. use bizGhs\stat\classes\StatYjClass;
  26. use bizGhs\stock\classes\StockRecordClass;
  27. use bizGhs\stock\services\StockRecordService;
  28. use bizHd\purchase\classes\PurchaseClass;
  29. use bizHd\stat\classes\StatIncomeClass;
  30. use bizHd\stat\classes\StatOrderClass;
  31. use common\components\dict;
  32. use common\components\httpUtil;
  33. use common\components\imgUtil;
  34. use common\components\orderSn;
  35. use common\components\printUtil;
  36. use common\components\stringUtil;
  37. use common\components\util;
  38. use bizGhs\base\classes\BaseClass;
  39. use Yii;
  40. class OrderClass extends BaseClass
  41. {
  42. use OrderTrait;
  43. public static $baseFile = '\bizGhs\order\models\Order';
  44. //已取消
  45. const ORDER_STATUS_CANCEL = 5;
  46. //已完成
  47. const ORDER_STATUS_COMPLETE = 4;
  48. //配送中
  49. const ORDER_STATUS_SENDING = 3;
  50. //待配送
  51. const ORDER_STATUS_UN_SEND = 2;
  52. //待付款,待确认
  53. const ORDER_STATUS_UN_PAY = 1;
  54. //未确认
  55. const SEND_TYPE_UNKNOWN = 1;
  56. //自取
  57. const SEND_TYPE_NO = 2;
  58. //自己送
  59. const SEND_TYPE_MYSELF = 3;
  60. //快递送
  61. const SEND_TYPE_THIRD = 4;
  62. public static $sendTypeMap = [
  63. self::SEND_TYPE_UNKNOWN => '未确认',
  64. self::SEND_TYPE_NO => '自取',
  65. self::SEND_TYPE_MYSELF => '商家配送',
  66. self::SEND_TYPE_THIRD => '快递配送',
  67. ];
  68. //待付款
  69. const PAY_STATUS_UN_PAY = 1;
  70. //已付款
  71. const PAY_STATUS_HAS_PAY = 2;
  72. //待配送
  73. const SEND_STATUS_UN_SEND = 1;
  74. //配送中
  75. const SEND_STATUS_SENDING = 2;
  76. //配送完成
  77. const SEND_STATUS_COMPLETE = 3;
  78. //订单总流程数
  79. const TOTAL_FLOW = 3;
  80. //清算方式
  81. //欠款
  82. const CLEAR_DEBT = 1;
  83. //已付款
  84. const CLEAR_HAS_GATHERING = 2;
  85. //没有欠款
  86. const DEBT_NO = 0;
  87. //欠款
  88. const DEBT_YES = 1;
  89. //正常订单
  90. const REFUND_NO = 1;
  91. //已退款订单
  92. const REFUND_YES = 2;
  93. public static $refundMap = [
  94. self::REFUND_NO => '',
  95. self::REFUND_YES => '有退款',
  96. ];
  97. public static function arrival($order, $data)
  98. {
  99. if ($order->stockChange == 1) {
  100. util::fail('您已操作过了');
  101. }
  102. $sjId = $order->sjId ?? 0;
  103. $shopId = $order->shopId ?? 0;
  104. $mainId = $order->mainId ?? 0;
  105. //product [{"productId":"3674","num":0,"price":1}]
  106. $product = $data['product'] ?? '';
  107. if (empty($product)) {
  108. util::fail('没有花材?');
  109. }
  110. $ids = [];
  111. $format = [];
  112. foreach ($product as $val) {
  113. $productId = $val['productId'] ?? 0;
  114. $ids[] = $productId;
  115. $format[$productId] = $val;
  116. }
  117. $info = ProductClass::getByIds($ids, null, 'id');
  118. $orderSn = $order->orderSn ?? '';
  119. $customName = $order->customName ?? '';
  120. $list = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  121. if (empty($list)) {
  122. util::fail('没有找到订单的花材');
  123. }
  124. $itemPrice = 0;
  125. $weight = 0;
  126. foreach ($list as $key => $val) {
  127. $productId = $val->productId ?? 0;
  128. $num = $format[$productId]['num'] ?? 0;
  129. $unitPrice = $format[$productId]['price'] ?? 0;
  130. $price = bcmul($unitPrice, $num, 2);
  131. $name = $val->name ?? '';
  132. $xhUnitName = $val->xhUnitName ?? '';
  133. $xhPreNum = $val->xhPreNum ?? 0;
  134. if ($unitPrice <= 0) {
  135. util::fail('花材金额需要大于0');
  136. }
  137. if ($num > $xhPreNum) {
  138. util::fail($name . "数量不能超过{$xhPreNum}{$xhUnitName}");
  139. }
  140. $val->cost = $unitPrice;
  141. $val->unitPrice = $unitPrice;
  142. $val->price = $price;
  143. $val->bigNum = $num;
  144. $val->num = $num;
  145. $val->xhNum = $num;
  146. $val->xhUnitPrice = $unitPrice;
  147. $val->xhPrice = $price;
  148. $val->save();
  149. if ($val->xhPreNum > $num) {
  150. //客户下单退货数增加
  151. $subNum = bcsub($val->xhPreNum, $num);
  152. StatKhCgClass::ghsBookOrderRefundNum($order, $subNum);
  153. //花材销量榜减少
  154. StatItemClass::ghsBookOrderRefundNum($order, $val, $subNum);
  155. }
  156. $itemPrice = bcadd($itemPrice, $price, 2);
  157. $productWeight = $info[$productId]['weight'] ?? 0;
  158. $currentWeight = bcmul($productWeight, $num, 2);
  159. $weight = bcadd($currentWeight, $weight, 2);
  160. //减少库存
  161. $unitType = $val->xhUnitType ?? 0;
  162. $bigNum = 0;
  163. $smallNum = 0;
  164. if ($unitType == dict::getDict('unitType', 'big')) {
  165. $bigNum = $num;
  166. $itemNum = $num;
  167. } else {
  168. $smallNum = $num;
  169. $itemNum = bcdiv($num, $val->ratio, 2);
  170. }
  171. $checkStock = true;
  172. $stockInfo = ProductClass::decreaseStock($productId, $bigNum, $smallNum, $checkStock);
  173. $recordData = [];
  174. $recordData['sjId'] = $sjId;
  175. $recordData['shopId'] = $shopId;
  176. $recordData['mainId'] = $mainId;
  177. $recordData['itemId'] = $val->itemId;
  178. $recordData['itemNum'] = $itemNum;
  179. $recordData['oldStock'] = $stockInfo['oldStock'];
  180. $recordData['newStock'] = $stockInfo['newStock'];
  181. $recordData['productId'] = $productId;
  182. $recordData['orderSn'] = $orderSn;
  183. $recordData['relateName'] = $customName;
  184. $recordData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  185. StockRecordClass::addSellStockOrderRecord($recordData);
  186. }
  187. $order->itemPrice = $itemPrice;
  188. $sendCost = 0;
  189. $currentPrice = bcadd($itemPrice, $sendCost, 2);
  190. $shopId = $order->shopId ?? 0;
  191. $shop = ShopClass::getLockById($shopId);
  192. if (empty($shop)) {
  193. util::fail('没有找到门店');
  194. }
  195. $mainId = $shop->mainId ?? 0;
  196. $main = MainClass::getLockById($mainId);
  197. if (empty($main)) {
  198. util::fail('没有main信息');
  199. }
  200. $main->unSendOrder -= 1;
  201. $main->finishOrder += 1;
  202. $main->save();
  203. $kiloFee = $shop->kiloFee ?? 0;
  204. $miniKilo = $shop->miniKilo ?? 0;
  205. $calcWeight = $weight > $miniKilo ? $weight : $miniKilo;
  206. $allKiloFee = bcmul($calcWeight, $kiloFee, 2);
  207. $currentPrice = bcadd($currentPrice, $allKiloFee, 2);
  208. $order->prePrice = $currentPrice;
  209. $order->orderPrice = $currentPrice;
  210. $order->actPrice = $currentPrice;
  211. $order->realPrice = $currentPrice;
  212. $order->stockChange = 1;
  213. $order->status = self::ORDER_STATUS_COMPLETE;
  214. $order->save();
  215. $bookPrice = $order->bookPrice ?? 0;
  216. $mainId = $shop->mainId ?? 0;
  217. $sjId = $shop->sjId ?? 0;
  218. $customId = $order->customId ?? 0;
  219. $relateOrderSn = $order->orderSn ?? '';
  220. $shopAdminId = $data['shopAdminId'] ?? 0;
  221. $shopAdminName = $data['shopAdminName'] ?? '';
  222. $refund = null;
  223. if ($bookPrice != $currentPrice) {
  224. if ($bookPrice > $currentPrice) {
  225. $refundPrice = bcsub($bookPrice, $currentPrice, 2);
  226. $order->refundPrice = $refundPrice;
  227. $order->tkPrice = bcadd($order->tkPrice, $refundPrice, 2);
  228. $order->refund = self::REFUND_YES;
  229. $order->save();
  230. $orderSn = orderSn::getGhsRefundSn();
  231. $book = 1;
  232. $refundData = [
  233. 'mainId' => $mainId,
  234. 'shopId' => $shopId,
  235. 'sjId' => $sjId,
  236. 'customId' => $customId,
  237. 'orderSn' => $orderSn,
  238. 'relateOrderSn' => $relateOrderSn,
  239. 'book' => $book,
  240. 'status' => RefundOrderClass::STATUS_UN_COMPLETE,
  241. 'shopAdminId' => $shopAdminId,
  242. 'shopAdminName' => $shopAdminName,
  243. 'refundPrice' => $refundPrice,
  244. 'refundType' => RefundOrderClass::REFUND_TYPE_MONEY,
  245. ];
  246. //退款
  247. $refund = RefundOrderClass::add($refundData, true);
  248. //余额减少
  249. ShopClass::ghsBookOrderRefundReduceBalance($main, $shop, $refund, $refundPrice);
  250. //总支出增加
  251. $currentTotalExpend = bcadd($main->totalExpend, $refundPrice, 2);
  252. $main->totalExpend = $currentTotalExpend;
  253. $totalRefund = bcadd($main->totalRefund, $refundPrice, 2);
  254. $main->totalRefund = $totalRefund;
  255. $main->save();
  256. //支出流水
  257. $thisType = dict::getDict('capitalType', 'ghsBookRefund', 'id');
  258. $sjId = $order->sjId ?? 0;
  259. $event = "预订单多付退给{$customName}";
  260. $capitalData = [
  261. 'capitalType' => $thisType,
  262. 'io' => 0,
  263. 'totalExpend' => $currentTotalExpend,
  264. 'payWay' => 0,
  265. 'amount' => $refundPrice,
  266. 'sjId' => $sjId,
  267. 'shopId' => $shopId,
  268. 'event' => $event,
  269. 'mainId' => $mainId,
  270. ];
  271. ShopCapitalClass::addCapital($capitalData);
  272. //支出统计
  273. StatOutClass::updateOrInsert($main, $shop, $refundPrice);
  274. //退款统计
  275. $payWay = $order->payWay ?? 0;
  276. StatRefundClass::replace($main, $shop, $payWay, $refundPrice);
  277. //供应商预订单退款给客户,客户采购金额减少
  278. StatKhCgClass::ghsBookOrderChange(false, $order, $refundPrice);
  279. } else {
  280. //欠款
  281. $currentDebt = bcsub($currentPrice, $bookPrice, 2);
  282. $order->debtPrice = $currentDebt;
  283. $order->remainDebtPrice = $currentDebt;
  284. $order->debt = OrderClass::DEBT_YES;
  285. $order->save();
  286. $currentTotalIncome = bcadd($main->totalIncome, $currentDebt, 2);
  287. $main->totalIncome = $currentTotalIncome;
  288. $main->save();
  289. $payWay = dict::getDict('payWay', 'debtPay');
  290. $custom = CustomClass::getLockById($customId);
  291. if (empty($custom)) {
  292. util::fail('没有找到客户');
  293. }
  294. CustomClass::cgDebtAmountAdd($custom, $order, true);
  295. //收入增加
  296. StatIncomeClass::updateOrInsert($main, $shop, $currentDebt);
  297. //收入流水增加
  298. $orderId = $order->id;
  299. $capitalType = dict::getDict('capitalType', 'ghsBookOrderGetBack', 'id');
  300. $shopAdminId = $order->shopAdminId ?? 0;
  301. $shopAdminName = $order->shopAdminName ?? '';
  302. $event = "预订单 {$orderSn} 差价补款";
  303. $mainId = $shop->mainId ?? 0;
  304. $sjId = $order->sjId ?? 0;
  305. $shopId = $order->shopId ?? 0;
  306. $capital = [
  307. 'capitalType' => $capitalType,
  308. 'relateId' => $orderId,
  309. 'io' => 1,
  310. 'amount' => $currentDebt,
  311. 'totalIncome' => $currentTotalIncome,
  312. 'payWay' => $payWay,
  313. 'event' => $event,
  314. 'mainId' => $mainId,
  315. 'sjId' => $sjId,
  316. 'shopId' => $shopId,
  317. 'shopAdminId' => $shopAdminId,
  318. 'shopAdminName' => $shopAdminName,
  319. ];
  320. ShopCapitalClass::addCapital($capital);
  321. //各渠道收入金额统计
  322. StatKdClass::replace($shop, $currentDebt, $payWay);
  323. //每天销售收入统计
  324. StatSaleClass::replace($main, $shop, $currentDebt);
  325. //预订单,客户补款给供应商,客户采购金额增加
  326. StatKhCgClass::ghsBookOrderChange(true, $order, $currentDebt);
  327. }
  328. }
  329. return ['kiloFee' => $kiloFee, 'refund' => $refund, 'miniKilo' => $miniKilo];
  330. }
  331. //添加订单 ssh 2019.12.5
  332. public static function addOrder($data)
  333. {
  334. $orderSn = orderSn::getGhsOrderSn();
  335. $data['orderSn'] = $orderSn;
  336. $data['payStatus'] = self::PAY_STATUS_UN_PAY;
  337. $data['customId'] = isset($data['customId']) && !empty($data['customId']) ? $data['customId'] : 0;
  338. $currentShopId = $data['shopId'] ?? 0;
  339. $shop = ShopClass::getLockById($currentShopId);
  340. if (empty($shop)) {
  341. util::fail('没有找到门店');
  342. }
  343. $mainId = $shop->mainId ?? 0;
  344. $main = MainClass::getLockById($mainId);
  345. if (empty($main)) {
  346. util::fail('没有main信息');
  347. }
  348. //花材
  349. $product = $data['product'];
  350. $book = $data['book'] ?? 0;
  351. $stockMayChange = true;
  352. $data['stockChange'] = 1;
  353. if ($book == 1) {
  354. $stockMayChange = false;
  355. $data['stockChange'] = 0;
  356. }
  357. $respond = OrderItemClass::replaceItem($orderSn, $product, $data, $stockMayChange);
  358. $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
  359. $data['sendCost'] = $sendCost;
  360. $data['bigNum'] = $respond['bigNum'] ?? 0;
  361. $data['smallNum'] = $respond['smallNum'] ?? 0;
  362. $data['itemNum'] = $respond['totalItemNum'] ?? 0;
  363. //默认订单要打单
  364. $data['needPrint'] = $data['needPrint'] ?? dict::getDict('needPrint', 'need');
  365. $weight = $respond['weight'] ?? 0;
  366. $data['weight'] = $weight;
  367. $itemPrice = $respond['price'] ?? 0;
  368. $data['itemPrice'] = $itemPrice;
  369. $currentPrice = bcadd($itemPrice, $sendCost, 2);
  370. //预订单需要增加服务费
  371. if ($book == 1) {
  372. $kiloFee = $shop->kiloFee ?? 0;
  373. $miniKilo = $shop->miniKilo ?? 0;
  374. $calcWeight = $weight > $miniKilo ? $weight : $miniKilo;
  375. $allKiloFee = bcmul($calcWeight, $kiloFee, 2);
  376. $currentPrice = bcadd($currentPrice, $allKiloFee, 2);
  377. $data['allKiloFee'] = $allKiloFee;
  378. $data['kiloFee'] = $kiloFee;
  379. $data['miniKilo'] = $miniKilo;
  380. }
  381. $data['prePrice'] = $currentPrice;
  382. $actPrice = $currentPrice;
  383. $realPrice = $currentPrice;
  384. //商家自己开单时
  385. if (isset($data['modifyPrice'])) {
  386. $modifyPrice = $data['modifyPrice'] ?? 0;
  387. if ($modifyPrice <= 0) {
  388. util::fail('收款金额必须大于0');
  389. }
  390. if ($modifyPrice > $currentPrice) {
  391. //人工和资材费用
  392. $data['labourCost'] = bcsub($modifyPrice, $currentPrice, 2);
  393. }
  394. if ($modifyPrice < $currentPrice) {
  395. //优惠金额
  396. $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
  397. $data['discountType'] = dict::getDict('discountType', 'discount');
  398. }
  399. $actPrice = $modifyPrice;
  400. $realPrice = $modifyPrice;
  401. }
  402. //客户采购时使用红包时
  403. if (isset($data['discountType']) && $data['discountType'] == dict::getDict('discountType', 'hb')) {
  404. $discountAmount = $data['discountAmount'] ?? 0;
  405. $currentDiscountPrice = bcsub($currentPrice, $discountAmount, 2);
  406. $actPrice = $currentDiscountPrice;
  407. $realPrice = $currentDiscountPrice;
  408. }
  409. $data['actPrice'] = $actPrice;
  410. $data['bookPrice'] = $actPrice;
  411. $data['orderPrice'] = $actPrice;
  412. $data['realPrice'] = $realPrice;
  413. $data['sendTimeWant'] = isset($data['sendTimeWant']) && !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
  414. //订单有效期
  415. $validTime = dict::getDict('order_pay_has_time');
  416. $current = time() + $validTime;
  417. $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
  418. $data['status'] = self::ORDER_STATUS_UN_PAY;
  419. $main->totalOrder += 1;
  420. $main->unPayOrder += 1;
  421. $main->save();
  422. //将当天日期+花店每天的总订单数作为编号
  423. $data['sendNum'] = date("j") . StatOrderCountClass::addOrder($shop, $main);
  424. //订单有效期
  425. $currentTime = time();
  426. $validTime = dict::getDict('order_pay_has_time');
  427. $current = $currentTime + $validTime;
  428. $data['deadline'] = isset($data['deadline']) ? $data['deadline'] : date("Y-m-d H:i:s", $current);
  429. //订单自动记为欠款单时间
  430. $localConfirmTime = dict::getDict('local_gys_kd_auto_set_debt_time');
  431. $currentSet = $currentTime + $localConfirmTime;
  432. $data['autoSetTime'] = $data['autoSetTime'] ?? date("Y-m-d H:i:s", $currentSet);
  433. $return = self::add($data, true);
  434. return $return;
  435. }
  436. //修改订单 ssh 2019.12.5
  437. public static function updateOrder($order, $data)
  438. {
  439. $sjId = $order->sjId ?? 0;
  440. $orderSn = $order->orderSn ?? 0;
  441. $currentShopId = $order->shopId ?? 0;
  442. $data['payStatus'] = self::PAY_STATUS_UN_PAY;
  443. //花材列表
  444. $product = $data['product'];
  445. //删除花材再添加
  446. OrderItemClass::deleteByCondition(['orderSn' => $orderSn]);
  447. $respond = OrderItemClass::replaceItem($orderSn, $product, $data);
  448. $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
  449. $data['sendCost'] = $sendCost;
  450. $data['bigNum'] = $respond['bigNum'] ?? 0;
  451. $data['smallNum'] = $respond['smallNum'] ?? 0;
  452. $price = $respond['price'] ?? 0;
  453. $currentPrice = bcadd($price, $sendCost, 2);
  454. $data['prePrice'] = $currentPrice;
  455. if (isset($data['modifyPrice'])) {
  456. //商家打折,修改金额
  457. $modifyPrice = $data['modifyPrice'] ?? 0;
  458. if ($modifyPrice > $currentPrice) {
  459. util::fail('实收金额大于订单总金额');
  460. }
  461. if ($modifyPrice < $currentPrice) {
  462. $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
  463. $data['discountType'] = dict::getDict('discountType', 'discount');
  464. $currentPrice = $modifyPrice;
  465. }
  466. }
  467. $data['actPrice'] = $currentPrice;
  468. $data['realPrice'] = $currentPrice;
  469. //如果有平台补贴
  470. if (isset($data['discountType']) && $data['discountType'] == dict::getDict('discountType', 'usePtCoupon')) {
  471. $discountAmount = $data['discountAmount'] ?? 0;
  472. $data['realPrice'] = bcsub($currentPrice, $discountAmount, 2);
  473. }
  474. $data['sendTime'] = isset($data['sendTime']) && !empty($data['sendTime']) ? $data['sendTime'] : '0000-00-00 00:00:00';
  475. //订单有效期
  476. $validTime = dict::getDict('order_pay_has_time');
  477. $current = time() + $validTime;
  478. $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
  479. $data['status'] = self::ORDER_STATUS_UN_PAY;
  480. //计算总重量(kg)
  481. $weight = 0;
  482. $productMapData = ProductClass::getProductMapData($product);
  483. foreach ($product as $key => $val) {
  484. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  485. $productId = $val['productId'];
  486. $weight = bcadd($w, $weight, 2);
  487. $checkStock = true;
  488. $stockInfo = ProductClass::decreaseStock($val['productId'], $val['bigNum'], $val['smallNum'], $checkStock);
  489. //记录库存流水
  490. $recordData = [];
  491. $recordData['sjId'] = $sjId;
  492. $recordData['shopId'] = $currentShopId;
  493. $recordData['itemId'] = $productMapData[$productId]['itemId'] ?? 0;
  494. $ratio = $productMapData[$productId]['ratio'] ?? 0;
  495. $recordData['itemNum'] = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  496. $recordData['oldStock'] = $stockInfo['oldStock'];
  497. $recordData['newStock'] = $stockInfo['newStock'];
  498. $recordData['productId'] = $productId;
  499. $recordData['orderSn'] = $orderSn;
  500. StockRecordClass::addSellStockOrderRecord($recordData);
  501. }
  502. $data['weight'] = $weight;
  503. $current = time() + 300;
  504. $data['deadline'] = isset($data['deadline']) ? $data['deadline'] : date("Y-m-d H:i:s", $current);
  505. //修改订单暂时不打印
  506. $data['needPrint'] = $data['needPrint'] ?? dict::getDict('needPrint', 'need');
  507. $orderId = $order->id ?? 0;
  508. self::updateById($orderId, $data);
  509. $return = self::getLockById($orderId);
  510. //门店总订单数+1
  511. $shop = ShopClass::getLockById($currentShopId);
  512. if (empty($shop)) {
  513. util::fail('没有找到门店');
  514. }
  515. $shop->unPayOrder += 1;
  516. $shop->cancelOrder -= 1;
  517. $shop->save();
  518. return $return;
  519. }
  520. //根据订单编号查询 ssh 2021.19
  521. public static function getByOrderSn($orderSn, $returnObj = false)
  522. {
  523. return self::getByCondition(['orderSn' => $orderSn], $returnObj);
  524. }
  525. //根据编号列表查订单
  526. public static function getOrderInfoBySnList($snList)
  527. {
  528. $list = self::getAllByCondition(['orderSn' => ['in', $snList]], null, '*', 'orderSn');
  529. $list = self::groupOrder($list, true, false, false, true);
  530. return $list;
  531. }
  532. //显示操作按钮
  533. public static function getShowButton($showButton, $isList, $order)
  534. {
  535. if ($showButton == false) {
  536. return [];
  537. }
  538. $print = ['type' => 'print', 'show' => 1, 'disable' => 0, 'name' => '打印'];
  539. $modifyProduct = ['type' => 'modifyProduct', 'show' => 0, 'disable' => 0, 'name' => '修改花材'];
  540. $confirm = ['type' => 'confirm', 'show' => 0, 'disable' => 1, 'name' => '确认'];
  541. $cancel = ['type' => 'cancel', 'show' => 0, 'disable' => 0, 'name' => '取消'];
  542. $arrival = ['type' => 'arrival', 'show' => 0, 'disable' => 0, 'name' => '到货'];
  543. if ($isList) {
  544. //订单列表页显示的按钮
  545. $send = ['type' => 'send', 'show' => 0, 'disable' => 0, 'name' => '发货'];
  546. $selfGet = ['type' => 'selfGet', 'show' => 0, 'disable' => 1, 'name' => '自取'];
  547. if ($order['status'] == self::ORDER_STATUS_UN_PAY) {
  548. //待确认订单,打印和修改花材按钮可以使用
  549. $send['disable'] = 1;
  550. $selfGet['disable'] = 1;
  551. $print['disable'] = 0;
  552. $confirm['disable'] = 0;
  553. }
  554. if ($order['status'] == self::ORDER_STATUS_UN_SEND) {
  555. //待配送订单,改花材按钮不可点,自取可点
  556. $selfGet['disable'] = 0;
  557. }
  558. //自取订单发货按钮置灰
  559. if ($order['sendType'] == self::SEND_TYPE_NO) {
  560. $send['disable'] = 1;
  561. }
  562. //取消订单置灰
  563. if ($order['status'] == self::ORDER_STATUS_CANCEL) {
  564. $send['disable'] = 1;
  565. $selfGet['disable'] = 1;
  566. $print['disable'] = 1;
  567. }
  568. if ($order['status'] == self::ORDER_STATUS_COMPLETE) {
  569. $send['name'] = '发货记录';
  570. }
  571. return [$send, $selfGet, $print, $confirm];
  572. } else {
  573. //订单详情页显示的按钮
  574. if (isset($order['book']) && $order['book'] == 1 && isset($order['stockChange']) && $order['stockChange'] == 0) {
  575. if ($order['status'] == self::ORDER_STATUS_UN_SEND) {
  576. $arrival['show'] = 1;
  577. }
  578. }
  579. //待确认订单显示延期付和已付款
  580. if ($order['status'] == self::ORDER_STATUS_UN_PAY) {
  581. $debtPay = ['type' => 'debtPay', 'show' => 0, 'disable' => 0, 'name' => '延期收款'];
  582. $hasPay = ['type' => 'hasPay', 'show' => 0, 'disable' => 0, 'name' => '欠款'];
  583. $confirm['disable'] = 0;
  584. return [$print, $debtPay, $hasPay, $confirm, $cancel, $arrival];
  585. }
  586. //待配送订单显示打印、发货和自取
  587. if ($order['status'] == self::ORDER_STATUS_UN_SEND) {
  588. $selfGet = ['type' => 'selfGet', 'show' => 0, 'disable' => 1, 'name' => '自取'];
  589. $send = ['type' => 'send', 'show' => 0, 'disable' => 1, 'name' => '发货'];
  590. if ($order['hasItem'] == 2) {
  591. return [$print, $selfGet, $send, $arrival];
  592. }
  593. $selfGet['disable'] = 0;
  594. $send['disable'] = 0;
  595. return [$print, $selfGet, $send, $arrival];
  596. }
  597. //配送中显示按钮
  598. if ($order['status'] == self::ORDER_STATUS_SENDING) {
  599. $send = ['type' => 'send', 'show' => 0, 'disable' => 0, 'name' => '发货'];
  600. return [$print, $send, $arrival];
  601. }
  602. //完成
  603. if ($order['status'] == self::ORDER_STATUS_COMPLETE) {
  604. $send = ['type' => 'send', 'show' => 0, 'disable' => 0, 'name' => '发货记录'];
  605. //自取订单不显示发货按钮
  606. if ($order['sendType'] == self::SEND_TYPE_NO) {
  607. $send['show'] = 0;
  608. }
  609. return [$print, $send, $arrival];
  610. }
  611. //取消订单置灰
  612. if ($order['status'] == self::ORDER_STATUS_CANCEL) {
  613. $selfGet['disable'] = 1;
  614. $send['disable'] = 1;
  615. $print['disable'] = 1;
  616. return [$modifyProduct, $arrival];
  617. }
  618. return [];
  619. }
  620. }
  621. //显示进度 ssh 2021.3.1
  622. public static function getShowProgress($showProgress, $order)
  623. {
  624. if ($showProgress == false) {
  625. return [];
  626. }
  627. $create = ['name' => '创建', 'complete' => 1];
  628. $unConfirm = ['name' => '待确认', 'complete' => 0];
  629. if ($order['status'] > 0) {
  630. $unConfirm['complete'] = 1;
  631. }
  632. $out = ['name' => '出库', 'complete' => 0];
  633. if ($order['status'] > 1) {
  634. $out['complete'] = 1;
  635. }
  636. return [$create, $unConfirm, $out];
  637. }
  638. //退款等需要使用原来的订单花材结构
  639. public static function getOriginalShowItem($showItem, $order)
  640. {
  641. if ($showItem == false) {
  642. return ['list' => [], 'itemStat' => []];
  643. }
  644. $orderSn = $order['orderSn'];
  645. $list = OrderItemClass::getOrderItem($orderSn);
  646. $totalPrice = 0;
  647. $totalBigNum = 0;
  648. $totalSmallNum = 0;
  649. $kind = 0;
  650. $itemStat = [];
  651. if (!empty($list)) {
  652. foreach ($list as $key => $val) {
  653. $list[$key]['property'] = $val['rank'] . '级';
  654. $price = $val['price'];
  655. $totalPrice = stringUtil::calcAdd($totalPrice, $price);
  656. $kind++;
  657. $currentBigNum = $val['bigNum'];
  658. $currentSmallNum = $val['smallNum'];
  659. $totalBigNum = stringUtil::calcAdd($totalBigNum, $currentBigNum);
  660. $totalSmallNum = stringUtil::calcAdd($totalSmallNum, $currentSmallNum);
  661. }
  662. $itemStat = ['kind' => $kind, 'bigNum' => $totalBigNum, 'smallNun' => $totalSmallNum, 'totalPrice' => $totalPrice];
  663. }
  664. return ['list' => $list, 'itemStat' => $itemStat];
  665. }
  666. public static function getShowItem($showItem, $order)
  667. {
  668. if ($showItem == false) {
  669. return ['list' => [], 'itemStat' => []];
  670. }
  671. $orderSn = $order['orderSn'];
  672. $list = OrderItemClass::getOrderItem($orderSn);
  673. $totalPrice = 0;
  674. $totalBigNum = 0;
  675. $totalSmallNum = 0;
  676. $kind = 0;
  677. $itemStat = [];
  678. //小菊增加部分
  679. $xjAddList = [];
  680. if (!empty($list)) {
  681. foreach ($list as $key => $val) {
  682. $price = $val['price'];
  683. $totalPrice = stringUtil::calcAdd($totalPrice, $price);
  684. $kind++;
  685. $currentBigNum = $val['bigNum'];
  686. $currentSmallNum = $val['smallNum'];
  687. $totalBigNum = stringUtil::calcAdd($totalBigNum, $currentBigNum);
  688. $totalSmallNum = stringUtil::calcAdd($totalSmallNum, $currentSmallNum);
  689. $list[$key]['property'] = '';
  690. //小菊
  691. if (isset($val['variety']) && $val['variety'] == 1) {
  692. $currentItemId = $val['id'] ?? 0;
  693. $xjList = OrderXjClass::getAllByCondition(['itemId' => $currentItemId], null, '*');
  694. if (!empty($xjList)) {
  695. //选了有颜色的小菊才把原小菊删除不显示出来
  696. unset($list[$key]);
  697. foreach ($xjList as $xjKey => $xj) {
  698. $xjBase = $val;
  699. $num = $xj['num'] ?? 0;
  700. $price = $xj['price'] ?? 0;
  701. $name = $xj['name'] ?? '';
  702. $cover = $xj['cover'] ?? '';
  703. if ($num > 0) {
  704. $bigTotalPrice = bcmul($price, $num, 2);
  705. $xjBase['cover'] = imgUtil::groupImg($cover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  706. $xjBase['unitPrice'] = $price;
  707. $xjBase['price'] = floatval($bigTotalPrice);
  708. $xjBase['num'] = floatval($num);
  709. $xjBase['bigNum'] = floatval($num);
  710. $xjBase['name'] = $name;
  711. $xjBase['property'] = '';
  712. $xjBase['userPrice'] = $bigTotalPrice;
  713. $xjAddList[] = $xjBase;
  714. }
  715. }
  716. }
  717. }
  718. }
  719. if (!empty($xjAddList)) {
  720. $list = array_merge($list, $xjAddList);
  721. }
  722. $itemStat = ['kind' => $kind, 'bigNum' => $totalBigNum, 'smallNun' => $totalSmallNum, 'totalPrice' => $totalPrice];
  723. }
  724. return ['list' => $list, 'itemStat' => $itemStat];
  725. }
  726. //订单列表 ssh 2021.1.19
  727. public static function getOrderList($where)
  728. {
  729. $data = self::getList('*', $where, 'addTime DESC');
  730. if (isset($data['list']) && !empty($data['list'])) {
  731. foreach ($data['list'] as $key => $val) {
  732. $data['list'][$key]['customAvatarUrl'] = imgUtil::groupImg('');
  733. }
  734. }
  735. return $data;
  736. }
  737. //整合订单员工和供应商等信息 ssh 2021.1.19
  738. //$isList 是否列表页
  739. public static function groupOrder($list, $showButton = false, $showItem = false, $showExpress = false, $isList = false)
  740. {
  741. if (empty($list)) {
  742. return $list;
  743. }
  744. $adminIds = array_unique(array_filter(array_column($list, 'adminId')));
  745. $adminInfo = AdminClass::getAdminByIds($adminIds, null, 'id');
  746. $customIds = array_column($list, 'customId');
  747. $customIds = array_unique(array_filter($customIds));
  748. $customInfoList = CustomClass::getByIds($customIds, null, 'id');
  749. foreach ($list as $key => $val) {
  750. $adminId = $val['adminId'] ?? 0;
  751. $list[$key]['adminName'] = $adminInfo[$adminId]['name'] ?? '';
  752. $list[$key]['customAddress'] = $val['fullAddress'] ?? '';
  753. //显示列表页的操作按钮
  754. $list[$key]['buttonList'] = self::getShowButton($showButton, $isList, $val);
  755. //为了列表页的打印按钮能用,列表也强制显示花材
  756. $showItem = true;
  757. //退款需要显示原有的花材结构,不显示小菊的多颜色
  758. $getOriginalItem = Yii::$app->request->get('getOriginalItem', 0);
  759. $getOriginalItem2 = Yii::$app->request->post('getOriginalItem', 0);
  760. if ($getOriginalItem == 1 || $getOriginalItem2 == 1) {
  761. $showItemData = self::getOriginalShowItem($showItem, $val);
  762. } else {
  763. $showItemData = self::getShowItem($showItem, $val);
  764. }
  765. $list[$key]['product'] = $showItemData['list'];
  766. $list[$key]['productStat'] = $showItemData['itemStat'];
  767. $showResetItem = 0;
  768. //待付款订单显示改花材按钮
  769. if ($val['status'] == self::PAY_STATUS_UN_PAY) {
  770. $showResetItem = 1;
  771. }
  772. $list[$key]['showResetItem'] = $showResetItem;
  773. //获取配送进展
  774. $expressProgress = '';
  775. if ($val['sendType'] == 4) {
  776. $expressInfo = OrderExpressClass::getByCondition(['orderSn' => $val['orderSn']]);
  777. if ($expressInfo) {
  778. $expressProgress = ['progressName' => OrderExpressClass::$statusMap[$expressInfo['status']] ?? ''];
  779. }
  780. }
  781. $list[$key]['sendTypeName'] = self::$sendTypeMap[$val['sendType']] ?? '未知';
  782. $list[$key]['expressProgress'] = $expressProgress;
  783. $list[$key]['customAvatarUrl'] = imgUtil::groupImg($val['customAvatar']);
  784. $list[$key]['customName'] = isset($val['customId']) && !empty($val['customId']) ? $val['customName'] : '散客';
  785. $customId = $val['customId'] ?? '';
  786. $debtAmount = $customInfoList[$customId]['debtAmount'] ?? 0.00;
  787. //打印数据组合
  788. $printData = self::getPrintData($val, $debtAmount, $showItemData);
  789. $list[$key]['printData'] = json_encode($printData);
  790. $list[$key]['debtAmount'] = $debtAmount;
  791. $list[$key]['customShopId'] = $customInfoList[$customId]['shopId'] ?? 0;
  792. }
  793. return $list;
  794. }
  795. public static function printGroup($current, $content)
  796. {
  797. //58mm的机器,一行打印16个汉字,32个字母
  798. $name = $current['name'] ?? '';
  799. $content .= $name . '<BR>';
  800. $name = str_repeat(' ', 10);
  801. $productNum = $current['num'] ?? '';
  802. $blankNum = bcsub(12, strlen($productNum));
  803. if ($blankNum > 0) {
  804. $productNum = $productNum . str_repeat(' ', $blankNum);
  805. }
  806. $productPrice = $current['price'] ?? '';
  807. $blankNum = bcsub(7, strlen($productPrice));
  808. if ($blankNum > 0) {
  809. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  810. }
  811. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  812. return $content;
  813. }
  814. //云打印 ssh 20210709
  815. public static function onlinePrint($orderInfo)
  816. {
  817. $showItem = OrderClass::getShowItem(true, $orderInfo);
  818. $mainId = $orderInfo['mainId'] ?? 0;
  819. $staff = ShopAdminClass::getByCondition(['mainId' => $mainId, 'founder' => 2], true);
  820. $mobile = $staff->mobile ?? '';
  821. $customId = $orderInfo['customId'] ?? 0;
  822. $ghsId = $orderInfo['ghsId'] ?? 0;
  823. $c = CustomClass::getById($customId);
  824. $debtAmount = $c['debtAmount'] ?? 0.00;
  825. $fromType = $orderInfo['fromType'] ?? dict::getDict('fromType', 'shop');
  826. $fromTypeMap = dict::getDict('fromTypeMap');
  827. $fromTypeName = $fromTypeMap[$fromType] ?? '门店';
  828. $respond = self::getPrintData($orderInfo, $debtAmount, $showItem);
  829. $content = '<B>' . $respond['sendNum'] . ' ' . $fromTypeName . '</B><BR>';
  830. $content .= '--------------------------------<BR>';
  831. $content .= '<B>' . $respond['custom']['fullAddress'] . '</B><BR>';
  832. $content .= '<B>' . $respond['custom']['customName'] . '</B><BR>';
  833. $content .= '<B>' . $respond['custom']['customMobile'] . '</B><BR>';
  834. $payDate = date("Y-m-d", strtotime($orderInfo['payTime']));
  835. $sendTimWant = $orderInfo['sendTimeWant'] ?? '';
  836. if ($payDate != $sendTimWant) {
  837. $content .= '<BR>';
  838. $content .= '送货时间:<BR>';
  839. $content .= '<B>' . $orderInfo['sendTimeWant'] . '</B><BR>';
  840. }
  841. if (isset($orderInfo['book']) && $orderInfo['book'] == 1) {
  842. $content .= '<B>预订单</B><BR>';
  843. }
  844. if (isset($respond['remark']) && !empty($respond['remark'])) {
  845. $content .= '<BR>';
  846. $content .= '备注:<BR>';
  847. $content .= '<B>' . $respond['remark'] . '</B><BR>';
  848. }
  849. $content .= '<BR>';
  850. $content .= '花材 数量/单价 金额 <BR>';
  851. $content .= '--------------------------------<BR>';
  852. if (isset($respond['product']) && !empty($respond['product'])) {
  853. foreach ($respond['product'] as $current) {
  854. $content = self::printGroup($current, $content);
  855. }
  856. $content .= '--------------------------------<BR>';
  857. }
  858. if (isset($respond['refund']) && !empty($respond['refund'])) {
  859. $content .= '<B>已退款:</B><BR>';
  860. foreach ($respond['refund'] as $current) {
  861. //58mm的机器,一行打印16个汉字,32个字母
  862. $name = $current['name'] ?? '';
  863. $content .= $name . '<BR>';
  864. $name = str_repeat(' ', 10);
  865. $productNum = $current['num'] ?? '';
  866. $blankNum = bcsub(12, strlen($productNum));
  867. if ($blankNum > 0) {
  868. $productNum = $productNum . str_repeat(' ', $blankNum);
  869. }
  870. $productPrice = $current['price'] ?? '';
  871. $blankNum = bcsub(7, strlen($productPrice));
  872. if ($blankNum > 0) {
  873. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  874. }
  875. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  876. }
  877. $content .= '--------------------------------<BR>';
  878. }
  879. if (isset($respond['sendCost']) && $respond['sendCost'] > 0) {
  880. $content .= ' 运费:' . floatval($respond['sendCost']) . '<BR>';
  881. }
  882. $content .= '订单金额:' . floatval($respond['prePrice']) . '<BR>';
  883. if (isset($respond['discountAmount']) && !empty($respond['discountAmount']) && $respond['discountAmount'] > 0) {
  884. $content .= '优惠扣除:-' . floatval($respond['discountAmount']) . '<BR>';
  885. }
  886. if (isset($respond['refundPrice']) && $respond['refundPrice'] > 0) {
  887. $content .= '退款金额:' . floatval($respond['refundPrice']) . '<BR>';
  888. }
  889. $content .= '应付金额:' . floatval($respond['realPrice']) . '<BR>';
  890. if (isset($respond['debt']) && $respond['debt'] == 1) {
  891. $content .= '实付金额:0<BR>';
  892. } else {
  893. $content .= '实付金额:' . floatval($respond['realPrice']) . '<BR>';
  894. }
  895. if ($debtAmount > 0) {
  896. $content .= '累计欠款:' . floatval($debtAmount) . '<BR>';
  897. }
  898. $shopId = $orderInfo['shopId'] ?? 0;
  899. $shop = ShopClass::getById($shopId, true);
  900. $sjId = $shop->sjId ?? 0;
  901. $sj = SjClass::getById($sjId, true);
  902. $sjName = $sj->name ?? '';
  903. $currentName = $sjName;
  904. if (isset($shop->default) == false || $shop->default != 1) {
  905. $shopName = $shop->shopName ?? '';
  906. if (!empty($shopName)) {
  907. $currentName = $currentName . ' ' . $shopName;
  908. }
  909. }
  910. $getInfo = self::getById($orderInfo['id']);
  911. $content .= '--------------------------------<BR>';
  912. $content .= '订单日期:' . $getInfo['addTime'] . '<BR>';
  913. $content .= '订单编号:' . $respond['orderSn'] . '<BR>';
  914. $content .= '门店名称:' . $currentName . '<BR>';
  915. if (!empty($mobile)) {
  916. $content .= '联系电话:' . $mobile . '<BR>';
  917. }
  918. if ($debtAmount > 0) {
  919. $content .= "<BR>";
  920. $content .= "<C>扫码查看所有欠款单</C>";
  921. $content .= "<BR>";
  922. $host = Yii::$app->params['hdHost'];
  923. $content .= "<QR>" . $host . "/#/pagesPurchase/gathering?id=" . $ghsId . "</QR>";
  924. }
  925. $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
  926. $printSn = $ext['printSn'] ?? '';
  927. if (!empty($printSn)) {
  928. $p = new printUtil($printSn);
  929. $p->printMsg($content);
  930. }
  931. }
  932. //打印数据 ssh 20210702
  933. public static function getPrintData($val, $debtAmount, $showItem)
  934. {
  935. $printProduct = [];
  936. if (isset($showItem['list']) && !empty($showItem['list'])) {
  937. foreach ($showItem['list'] as $itemKey => $itemVal) {
  938. $bigNum = $itemVal['bigNum'] ?? 0;
  939. $bigUnitPrice = $itemVal['unitPrice'] ?? 0;
  940. $smallUnitPrice = $itemVal['smallUnitPrice'] ?? 0;
  941. $smallNum = $itemVal['smallNum'] ?? 0;
  942. if ($bigNum > 0) {
  943. $bigTotalPrice = bcmul($bigUnitPrice, $bigNum, 2);
  944. $printProduct[] = [
  945. 'name' => $itemVal['name'] ?? '',
  946. 'num' => floatval($bigNum) . '扎*' . floatval($bigUnitPrice),
  947. 'price' => floatval($bigTotalPrice),
  948. ];
  949. }
  950. if ($smallNum > 0) {
  951. $smallTotalPrice = bcmul($smallUnitPrice, $smallNum, 2);
  952. $printProduct[] = [
  953. 'name' => $itemVal['name'] ?? '',
  954. 'num' => floatval($smallNum) . '支*' . floatval($smallUnitPrice),
  955. 'price' => floatval($smallTotalPrice),
  956. ];
  957. }
  958. }
  959. }
  960. $sendNum = $val['sendNum'] ?? '';
  961. $relateOrderSn = $val['orderSn'] ?? '';
  962. $refundList = [];
  963. $refundData = RefundOrderClass::getAllByCondition(['relateOrderSn' => $relateOrderSn], null, '*', null, true);
  964. if (!empty($refundData)) {
  965. foreach ($refundData as $refund) {
  966. $refundOrderSn = $refund->orderSn ?? '';
  967. $refundProductList = RefundOrderItemClass::getAllByCondition(['orderSn' => $refundOrderSn], null, '*');
  968. if (!empty($refundProductList)) {
  969. foreach ($refundProductList as $currentRefund) {
  970. $num = $currentRefund['xhNum'] ?? 0;
  971. $unitName = $currentRefund['xhUnitName'] ?? '';
  972. $unitPrice = $currentRefund['xhUnitPrice'] ?? 0;
  973. $currentPrice = bcmul($unitPrice, $num, 2);
  974. $refundList[] = [
  975. 'name' => $currentRefund['name'] ?? '',
  976. 'num' => floatval($num) . $unitName . '*' . floatval($unitPrice),
  977. 'price' => floatval($currentPrice),
  978. ];
  979. }
  980. }
  981. }
  982. }
  983. $printData = [
  984. 'address' => '',
  985. 'sendCost' => $val['sendCost'] ?? '0.00',
  986. 'prePrice' => $val['prePrice'] ?? '0.00',
  987. 'discountAmount' => $val['discountAmount'] ?? '0.00',
  988. 'realPrice' => $val['realPrice'] ?? '0.00',
  989. 'refundPrice' => $val['refundPrice'] ?? '0.00',
  990. 'debt' => $val['debt'] ?? 0,
  991. 'orderSn' => $val['orderSn'] ?? '',
  992. 'date' => $val['addTime'] ?? '',
  993. 'remark' => $val['remark'] ?? '',
  994. 'custom' => [
  995. 'customName' => $val['customName'] ?? '',
  996. 'customMobile' => $val['customMobile'] ?? '',
  997. 'fullAddress' => $val['fullAddress'] ?? '',
  998. ],
  999. 'product' => $printProduct,
  1000. 'refund' => $refundList,
  1001. 'sendNum' => $sendNum,
  1002. 'debtAmount' => $debtAmount,
  1003. ];
  1004. return $printData;
  1005. }
  1006. //订单的验证 ssh 2021.3.2
  1007. public static function valid($info, $shopId)
  1008. {
  1009. if (empty($info)) {
  1010. util::fail('没有找到订单');
  1011. }
  1012. if (isset($info['shopId']) && $info['shopId'] == $shopId) {
  1013. return true;
  1014. }
  1015. util::fail('没有权限操作的订单');
  1016. }
  1017. //快递送 ssh 2021.3.16
  1018. public static function thirdSend($order, $expressInfo)
  1019. {
  1020. if ($order->status == self::ORDER_STATUS_UN_PAY) {
  1021. util::fail('订单未付款');
  1022. }
  1023. if ($order->status != self::ORDER_STATUS_UN_SEND) {
  1024. util::fail('不是待配送状态');
  1025. }
  1026. $id = $order->id;
  1027. $expressId = $expressInfo['expressId'] ?? 1;
  1028. $expressName = $expressInfo['deliveryName'] ?? '达达';
  1029. $expressRemark = $expressInfo['remark'] ?? '';
  1030. $order->status = self::ORDER_STATUS_SENDING;
  1031. $order->sendType = self::SEND_TYPE_THIRD;
  1032. $order->expressId = $expressId;
  1033. $order->save();
  1034. $shopId = $order->shopId;
  1035. $shop = Shop::findBySql('select * from ' . Shop::tableName() . ' where id = :id for update', ['id' => $shopId])->one();
  1036. if (empty($shop)) {
  1037. util::fail('没有找到门店');
  1038. }
  1039. $shop->unSendOrder -= 1;
  1040. $shop->sendingOrder += 1;
  1041. $shop->save();
  1042. $express = OrderExpressClass::getByCondition(['orderId' => $id]);
  1043. if (!empty($express)) {
  1044. util::fail('已经发过了');
  1045. }
  1046. //达达
  1047. $dadaRes = DadaExpressServices::addOrder($order, $expressRemark);
  1048. $orderSn = $order->orderSn;
  1049. $sjId = $order->sjId ?? 0;
  1050. /*
  1051. $getInfo = ExpressClass::getExpressInfo($expressId);
  1052. $hour = $expressInfo['sendTime'] ?? '12:00';
  1053. $sendCost = 12;
  1054. $distance = 5;
  1055. $sendTime = strtotime(date("Y-m-d ") . $hour);
  1056. OrderSendClass::thirdSend($order);
  1057. */
  1058. $sendTime = $order->sendTime;
  1059. if ($sendTime == '0000-00-00 00:00:00') {
  1060. $sendTime = time();
  1061. } else {
  1062. $sendTime = strtotime($order->sendTime);
  1063. }
  1064. $expData = [
  1065. 'orderId' => $id,
  1066. 'orderSn' => $orderSn,
  1067. 'sjId' => $sjId,
  1068. 'expressId' => $expressId,
  1069. 'sendTime' => $sendTime,
  1070. 'cost' => $dadaRes['fee'],
  1071. 'distance' => $dadaRes['distance'],
  1072. 'status' => 1,
  1073. 'expressName' => $expressName,
  1074. 'remark' => $expressRemark,
  1075. ];
  1076. OrderExpressClass::addExpress($expData);
  1077. OrderSendClass::thirdSend($order);
  1078. $respond = [
  1079. 'expressName' => $expressName,
  1080. 'expressId' => $expressId,
  1081. 'distance' => $dadaRes['distance'],
  1082. 'cost' => $dadaRes['fee'],
  1083. 'status' => 2,
  1084. 'tip' => 0,
  1085. ];
  1086. //采购单变成已发货
  1087. $purchaseId = $order->purchaseId ?? 0;
  1088. PurchaseClass::sending($purchaseId);
  1089. return $respond;
  1090. }
  1091. //本店送 ssh 2021.1.24
  1092. public static function selfSend($info)
  1093. {
  1094. if ($info->status == self::ORDER_STATUS_UN_PAY) {
  1095. util::fail('订单未付款');
  1096. }
  1097. if ($info->status != self::ORDER_STATUS_UN_SEND) {
  1098. util::fail('不是待配送状态');
  1099. }
  1100. $info->status = self::ORDER_STATUS_SENDING;
  1101. $info->sendType = self::SEND_TYPE_MYSELF;
  1102. $info->save();
  1103. $shopId = $info->shopId;
  1104. $shop = Shop::findBySql('select * from ' . Shop::tableName() . ' where id = :id for update', ['id' => $shopId])->one();
  1105. if (empty($shop)) {
  1106. util::fail('没有找到门店');
  1107. }
  1108. $shop->unSendOrder -= 1;
  1109. $shop->sendingOrder += 1;
  1110. $shop->save();
  1111. OrderSendClass::selfSend($info);
  1112. //采购单变成已发货
  1113. $purchaseId = $info->purchaseId ?? 0;
  1114. PurchaseClass::sending($purchaseId);
  1115. }
  1116. //订单完成 ssh 2020.3.12
  1117. public static function complete($order, $shop)
  1118. {
  1119. $order->status = OrderClass::ORDER_STATUS_COMPLETE;
  1120. $order->currentFlow = self::TOTAL_FLOW;
  1121. $order->totalFlow = self::TOTAL_FLOW;
  1122. $order->save();
  1123. $shop->finishOrder += 1;
  1124. $shop->sendingOrder -= 1;
  1125. $shop->save();
  1126. return $order;
  1127. }
  1128. //修改订单表的配送信息 lqh 2021.3.28
  1129. public static function updateCustomExpressInfo($id, $expressAddInfo)
  1130. {
  1131. $data = [
  1132. 'province' => $expressAddInfo['province'],
  1133. 'city' => $expressAddInfo['city'],
  1134. 'address' => $expressAddInfo['address'],
  1135. 'floor' => $expressAddInfo['floor'],
  1136. 'fullAddress' => $expressAddInfo['city'] . $expressAddInfo['address'] . $expressAddInfo['floor'],
  1137. 'lat' => $expressAddInfo['lat'],
  1138. 'long' => $expressAddInfo['long'],
  1139. 'sendTime' => $expressAddInfo['sendTime'],
  1140. ];
  1141. return self::updateById($id, $data);
  1142. }
  1143. //最客户最近的订单 ssh 2021.5.4
  1144. public static function getCustomOrder($id, $limit = 10, $order = null, $with = null)
  1145. {
  1146. $where = ['customId' => $id, 'payStatus' => 1];
  1147. return self::getLimitList('*', $where, $limit, $order, $with);
  1148. }
  1149. //销售单取消 ssh 2021.5.14
  1150. public static function cancel($id)
  1151. {
  1152. $order = self::getLockById($id);
  1153. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  1154. util::fail("销售单{$id}不是待付款状态,无法取消");
  1155. }
  1156. $order->status = self::ORDER_STATUS_CANCEL;
  1157. $order->save();
  1158. $shopId = $order->shopId;
  1159. $shop = ShopClass::getLockById($shopId);
  1160. if (empty($shop)) {
  1161. util::fail('没有找到门店');
  1162. }
  1163. $mainId = $shop->mainId ?? 0;
  1164. $main = MainClass::getLockById($mainId);
  1165. if (empty($main)) {
  1166. util::fail('没有main信息');
  1167. }
  1168. $main->unPayOrder -= 1;
  1169. $main->cancelOrder += 1;
  1170. $main->save();
  1171. //加回库存
  1172. $orderItem = OrderItemClass::getOrderItem($order->orderSn);
  1173. foreach ($orderItem as $v) {
  1174. $stockInfo = ProductClass::addStockByItemNum($v['productId'], $v['num']);
  1175. $recordData = [];
  1176. $recordData['sjId'] = $order->sjId;
  1177. $recordData['shopId'] = $shopId;
  1178. //增加中央ID
  1179. $recordData['mainId'] = ProductClass::getMainIdByProductId($v['productId']);
  1180. $recordData['itemId'] = $v['itemId'] ?? 0;
  1181. $recordData['itemNum'] = $v['num'];
  1182. $recordData['oldStock'] = $stockInfo['oldStock'];
  1183. $recordData['newStock'] = $stockInfo['newStock'];
  1184. $recordData['productId'] = $v['productId'];
  1185. $recordData['orderSn'] = $order->orderSn;
  1186. $recordData['relateName'] = $order->customName ?? '';
  1187. $recordData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  1188. StockRecordClass::addSellStockOrderCancelRecord($recordData);
  1189. }
  1190. return $order;
  1191. }
  1192. //新订单入队列 ssh 20211012
  1193. public static function newOrderAddQueue($order)
  1194. {
  1195. $shopId = $order->shopId ?? 0;
  1196. if (empty($shopId)) {
  1197. return false;
  1198. }
  1199. $id = $order->id ?? 0;
  1200. if (empty($id)) {
  1201. return false;
  1202. }
  1203. $newOrderKey = 'ghsNewOrder_' . $shopId;
  1204. Yii::$app->redis->executeCommand('LPUSH', [$newOrderKey, $id]);
  1205. }
  1206. }