OrderClass.php 60 KB

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