OrderClass.php 55 KB

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