OrderClass.php 58 KB

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