OrderClass.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. <?php
  2. namespace bizGhs\order\classes;
  3. use biz\shop\classes\ShopAdminClass;
  4. use biz\shop\classes\ShopClass;
  5. use biz\shop\classes\ShopExtClass;
  6. use biz\shop\models\Shop;
  7. use biz\sj\classes\SjClass;
  8. use biz\stat\classes\StatItemClass;
  9. use biz\stat\classes\StatOrderCountClass;
  10. use bizGhs\custom\models\Custom;
  11. use bizGhs\express\services\DadaExpressServices;
  12. use bizGhs\order\models\Order;
  13. use bizGhs\order\traits\OrderTrait;
  14. use bizGhs\product\classes\ProductClass;
  15. use bizGhs\admin\classes\AdminClass;
  16. use bizGhs\custom\classes\CustomClass;
  17. use bizGhs\stat\classes\StatYjClass;
  18. use bizGhs\stock\classes\StockRecordClass;
  19. use bizGhs\stock\services\StockRecordService;
  20. use bizHd\purchase\classes\PurchaseClass;
  21. use bizHd\stat\classes\StatIncomeClass;
  22. use bizHd\stat\classes\StatOrderClass;
  23. use common\components\dict;
  24. use common\components\httpUtil;
  25. use common\components\imgUtil;
  26. use common\components\orderSn;
  27. use common\components\printUtil;
  28. use common\components\stringUtil;
  29. use common\components\util;
  30. use bizGhs\base\classes\BaseClass;
  31. use Yii;
  32. class OrderClass extends BaseClass
  33. {
  34. use OrderTrait;
  35. public static $baseFile = '\bizGhs\order\models\Order';
  36. //已退款
  37. const ORDER_STATUS_REFUND = 6;
  38. //已取消
  39. const ORDER_STATUS_CANCEL = 5;
  40. //已完成
  41. const ORDER_STATUS_COMPLETE = 4;
  42. //配送中
  43. const ORDER_STATUS_SENDING = 3;
  44. //待配送
  45. const ORDER_STATUS_UN_SEND = 2;
  46. //待付款,待确认
  47. const ORDER_STATUS_UN_PAY = 1;
  48. //未确认
  49. const SEND_TYPE_UNKNOWN = 1;
  50. //自取
  51. const SEND_TYPE_NO = 2;
  52. //自己送
  53. const SEND_TYPE_MYSELF = 3;
  54. //快递送
  55. const SEND_TYPE_THIRD = 4;
  56. public static $sendTypeMap = [
  57. self::SEND_TYPE_UNKNOWN => '未确认',
  58. self::SEND_TYPE_NO => '自取',
  59. self::SEND_TYPE_MYSELF => '商家配送',
  60. self::SEND_TYPE_THIRD => '快递配送',
  61. ];
  62. //待付款
  63. const PAY_STATUS_UN_PAY = 1;
  64. //已付款
  65. const PAY_STATUS_HAS_PAY = 2;
  66. //待配送
  67. const SEND_STATUS_UN_SEND = 1;
  68. //配送中
  69. const SEND_STATUS_SENDING = 2;
  70. //配送完成
  71. const SEND_STATUS_COMPLETE = 3;
  72. //订单总流程数
  73. const TOTAL_FLOW = 3;
  74. //清算方式
  75. //欠款
  76. const CLEAR_DEBT = 1;
  77. //已付款
  78. const CLEAR_HAS_GATHERING = 2;
  79. //没有欠款
  80. const DEBT_NO = 0;
  81. //欠款
  82. const DEBT_YES = 1;
  83. //正常订单
  84. const REFUND_NO = 1;
  85. //已退款订单
  86. const REFUND_YES = 2;
  87. public static $refundMap = [
  88. self::REFUND_NO => '',
  89. self::REFUND_YES => '有退款',
  90. ];
  91. //添加订单 ssh 2019.12.5
  92. public static function addOrder($data)
  93. {
  94. $sjId = $data['sjId'] ?? 0;
  95. $orderSn = orderSn::getGhsOrderSn();
  96. $data['orderSn'] = $orderSn;
  97. $data['payStatus'] = self::PAY_STATUS_UN_PAY;
  98. $data['customId'] = isset($data['customId']) && !empty($data['customId']) ? $data['customId'] : 0;
  99. $product = $data['product'];
  100. //添加花材
  101. $respond = OrderItemClass::replaceItem($orderSn, $product, $data);
  102. $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
  103. $data['sendCost'] = $sendCost;
  104. $data['bigNum'] = $respond['bigNum'] ?? 0;
  105. $data['smallNum'] = $respond['smallNum'] ?? 0;
  106. $data['itemNum'] = $respond['totalItemNum'] ?? 0;
  107. //默认订单要打单
  108. $data['needPrint'] = $data['needPrint'] ?? dict::getDict('needPrint', 'need');
  109. //价格
  110. $price = $respond['price'] ?? 0;
  111. $currentPrice = bcadd($price, $sendCost, 2);
  112. $data['prePrice'] = $currentPrice;
  113. $actPrice = $currentPrice;
  114. $realPrice = $currentPrice;
  115. //商家自己开单时
  116. if (isset($data['modifyPrice'])) {
  117. $modifyPrice = $data['modifyPrice'] ?? 0;
  118. if ($modifyPrice <= 0) {
  119. util::fail('收款金额必须大于0');
  120. }
  121. if ($modifyPrice > $currentPrice) {
  122. //人工和资材费用
  123. $data['labourCost'] = bcsub($modifyPrice, $currentPrice, 2);
  124. }
  125. if ($modifyPrice < $currentPrice) {
  126. //优惠金额
  127. $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
  128. $data['discountType'] = dict::getDict('discountType', 'discount');
  129. }
  130. $actPrice = $modifyPrice;
  131. $realPrice = $modifyPrice;
  132. }
  133. //客户采购时使用红包时
  134. if (isset($data['discountType']) && $data['discountType'] == dict::getDict('discountType', 'hb')) {
  135. $discountAmount = $data['discountAmount'] ?? 0;
  136. $currentDiscountPrice = bcsub($currentPrice, $discountAmount, 2);
  137. $actPrice = $currentDiscountPrice;
  138. $realPrice = $currentDiscountPrice;
  139. }
  140. $data['actPrice'] = $actPrice;
  141. $data['realPrice'] = $realPrice;
  142. $data['sendTimeWant'] = isset($data['sendTimeWant']) && !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
  143. //订单有效期
  144. $validTime = dict::getDict('sale_purchase_order_valid');
  145. $current = time() + $validTime;
  146. $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
  147. $data['status'] = self::ORDER_STATUS_UN_PAY;
  148. $currentShopId = $data['shopId'] ?? 0;
  149. //计算总重量(kg)
  150. $weight = 0;
  151. $productMapData = ProductClass::getProductMapData($product);
  152. foreach ($product as $key => $val) {
  153. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  154. $productId = $val['productId'];
  155. $weight = bcadd($w, $weight, 2);
  156. $checkStock = true;
  157. $stockInfo = ProductClass::decreaseStock($val['productId'], $val['bigNum'], $val['smallNum'], $checkStock);
  158. //记录库存流水
  159. $recordData = [];
  160. $recordData['sjId'] = $sjId;
  161. $recordData['shopId'] = $currentShopId;
  162. $recordData['itemId'] = $productMapData[$productId]['itemId'] ?? 0;
  163. $ratio = $productMapData[$productId]['ratio'] ?? 0;
  164. $recordData['itemNum'] = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  165. $recordData['oldStock'] = $stockInfo['oldStock'];
  166. $recordData['newStock'] = $stockInfo['newStock'];
  167. $recordData['productId'] = $productId;
  168. $recordData['orderSn'] = $orderSn;
  169. $recordData['relateName'] = $data['customName'] ?? '';
  170. StockRecordService::addSellStockOrderRecord($recordData);
  171. }
  172. $data['weight'] = $weight;
  173. //门店总订单数+1
  174. $shop = ShopClass::getLockById($currentShopId);
  175. if (empty($shop)) {
  176. util::fail('没有找到门店');
  177. }
  178. $shop->totalOrder += 1;
  179. $shop->unPayOrder += 1;
  180. $shop->save();
  181. //将当天日期+花店每天的总订单数作为编号
  182. $data['sendNum'] = date("j") . StatOrderCountClass::addOrder($shop);
  183. //订单有效期
  184. $currentTime = time();
  185. $validTime = dict::getDict('sale_purchase_order_valid');
  186. $current = $currentTime + $validTime;
  187. $data['deadline'] = isset($data['deadline']) ? $data['deadline'] : date("Y-m-d H:i:s", $current);
  188. //订单自动记为欠款单时间
  189. $localConfirmTime = dict::getDict('local_gys_kd_auto_set_debt_time');
  190. $currentSet = $currentTime + $localConfirmTime;
  191. $data['autoSetTime'] = $data['autoSetTime'] ?? date("Y-m-d H:i:s", $currentSet);
  192. $return = self::add($data, true);
  193. return $return;
  194. }
  195. //添加订单 ssh 2019.12.5
  196. public static function updateOrder($order, $data)
  197. {
  198. $sjId = $order->sjId ?? 0;
  199. $orderSn = $order->orderSn ?? 0;
  200. $currentShopId = $order->shopId ?? 0;
  201. $data['payStatus'] = self::PAY_STATUS_UN_PAY;
  202. //花材列表
  203. $product = $data['product'];
  204. //删除花材再添加
  205. OrderItemClass::deleteByCondition(['orderSn' => $orderSn]);
  206. $respond = OrderItemClass::replaceItem($orderSn, $product, $data);
  207. $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
  208. $data['sendCost'] = $sendCost;
  209. $data['bigNum'] = $respond['bigNum'] ?? 0;
  210. $data['smallNum'] = $respond['smallNum'] ?? 0;
  211. $price = $respond['price'] ?? 0;
  212. $currentPrice = bcadd($price, $sendCost, 2);
  213. $data['prePrice'] = $currentPrice;
  214. if (isset($data['modifyPrice'])) {
  215. //商家打折,修改金额
  216. $modifyPrice = $data['modifyPrice'] ?? 0;
  217. if ($modifyPrice > $currentPrice) {
  218. util::fail('实收金额大于订单总金额');
  219. }
  220. if ($modifyPrice < $currentPrice) {
  221. $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
  222. $data['discountType'] = dict::getDict('discountType', 'discount');
  223. $currentPrice = $modifyPrice;
  224. }
  225. }
  226. $data['actPrice'] = $currentPrice;
  227. $data['realPrice'] = $currentPrice;
  228. //如果有平台补贴
  229. if (isset($data['discountType']) && $data['discountType'] == dict::getDict('discountType', 'usePtCoupon')) {
  230. $discountAmount = $data['discountAmount'] ?? 0;
  231. $data['realPrice'] = bcsub($currentPrice, $discountAmount, 2);
  232. }
  233. $data['sendTime'] = isset($data['sendTime']) && !empty($data['sendTime']) ? $data['sendTime'] : '0000-00-00 00:00:00';
  234. //订单有效期
  235. $validTime = dict::getDict('sale_purchase_order_valid');
  236. $current = time() + $validTime;
  237. $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
  238. $data['status'] = self::ORDER_STATUS_UN_PAY;
  239. //计算总重量(kg)
  240. $weight = 0;
  241. $productMapData = ProductClass::getProductMapData($product);
  242. foreach ($product as $key => $val) {
  243. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  244. $productId = $val['productId'];
  245. $weight = bcadd($w, $weight, 2);
  246. $checkStock = true;
  247. $stockInfo = ProductClass::decreaseStock($val['productId'], $val['bigNum'], $val['smallNum'], $checkStock);
  248. //记录库存流水
  249. $recordData = [];
  250. $recordData['sjId'] = $sjId;
  251. $recordData['shopId'] = $currentShopId;
  252. $recordData['itemId'] = $productMapData[$productId]['itemId'] ?? 0;
  253. $ratio = $productMapData[$productId]['ratio'] ?? 0;
  254. $recordData['itemNum'] = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  255. $recordData['oldStock'] = $stockInfo['oldStock'];
  256. $recordData['newStock'] = $stockInfo['newStock'];
  257. $recordData['productId'] = $productId;
  258. $recordData['orderSn'] = $orderSn;
  259. StockRecordService::addSellStockOrderRecord($recordData);
  260. }
  261. $data['weight'] = $weight;
  262. $current = time() + 300;
  263. $data['deadline'] = isset($data['deadline']) ? $data['deadline'] : date("Y-m-d H:i:s", $current);
  264. //修改订单暂时不打印
  265. $data['needPrint'] = $data['needPrint'] ?? dict::getDict('needPrint', 'need');
  266. $orderId = $order->id ?? 0;
  267. self::updateById($orderId, $data);
  268. $return = self::getLockById($orderId);
  269. //门店总订单数+1
  270. $shop = ShopClass::getLockById($currentShopId);
  271. if (empty($shop)) {
  272. util::fail('没有找到门店');
  273. }
  274. $shop->unPayOrder += 1;
  275. $shop->cancelOrder -= 1;
  276. $shop->save();
  277. return $return;
  278. }
  279. //根据订单编号查询 ssh 2021.19
  280. public static function getByOrderSn($orderSn, $returnObj = false)
  281. {
  282. return self::getByCondition(['orderSn' => $orderSn], $returnObj);
  283. }
  284. //根据编号列表查订单
  285. public static function getOrderInfoBySnList($snList)
  286. {
  287. $list = self::getAllByCondition(['orderSn' => ['in', $snList]], null, '*', 'orderSn');
  288. $list = self::groupOrder($list, true, false, false, true);
  289. return $list;
  290. }
  291. //订单列表 ssh 2021.1.19
  292. public static function getOrderList($where)
  293. {
  294. $data = self::getList('*', $where, 'addTime DESC');
  295. $data['list'] = self::groupOrder($data['list'], true, false, false, true);
  296. return $data;
  297. }
  298. //显示操作按钮
  299. public static function getShowButton($showButton, $isList, $order)
  300. {
  301. if ($showButton == false) {
  302. return [];
  303. }
  304. $print = ['type' => 'print', 'show' => 1, 'disable' => 0, 'name' => '打印'];
  305. $modifyProduct = ['type' => 'modifyProduct', 'show' => 1, 'disable' => 0, 'name' => '修改花材'];
  306. $confirm = ['type' => 'confirm', 'show' => 0, 'disable' => 1, 'name' => '确认'];
  307. $cancel = ['type' => 'cancel', 'show' => 0, 'disable' => 0, 'name' => '取消'];
  308. if ($isList) {
  309. //订单列表页显示的按钮
  310. $send = ['type' => 'send', 'show' => 0, 'disable' => 0, 'name' => '发货'];
  311. $selfGet = ['type' => 'selfGet', 'show' => 0, 'disable' => 1, 'name' => '自取'];
  312. if ($order['status'] == self::ORDER_STATUS_UN_PAY) {
  313. //待确认订单,打印和修改花材按钮可以使用
  314. $send['disable'] = 1;
  315. $selfGet['disable'] = 1;
  316. $print['disable'] = 0;
  317. $confirm['disable'] = 0;
  318. }
  319. if ($order['status'] == self::ORDER_STATUS_UN_SEND) {
  320. //待配送订单,改花材按钮不可点,自取可点
  321. $selfGet['disable'] = 0;
  322. }
  323. //自取订单发货按钮置灰
  324. if ($order['sendType'] == self::SEND_TYPE_NO) {
  325. $send['disable'] = 1;
  326. }
  327. //取消订单置灰
  328. if ($order['status'] == self::ORDER_STATUS_CANCEL) {
  329. $send['disable'] = 1;
  330. $selfGet['disable'] = 1;
  331. $print['disable'] = 1;
  332. }
  333. if ($order['status'] == self::ORDER_STATUS_COMPLETE) {
  334. $send['name'] = '发货记录';
  335. }
  336. return [$send, $selfGet, $print, $confirm];
  337. } else {
  338. //订单详情页显示的按钮
  339. //待确认订单显示延期付和已付款
  340. if ($order['status'] == self::ORDER_STATUS_UN_PAY) {
  341. $debtPay = ['type' => 'debtPay', 'show' => 0, 'disable' => 0, 'name' => '延期收款'];
  342. $hasPay = ['type' => 'hasPay', 'show' => 0, 'disable' => 0, 'name' => '欠款'];
  343. $confirm['disable'] = 0;
  344. return [$print, $debtPay, $hasPay, $confirm, $cancel];
  345. }
  346. //待配送订单显示打印、发货和自取
  347. if ($order['status'] == self::ORDER_STATUS_UN_SEND) {
  348. $selfGet = ['type' => 'selfGet', 'show' => 0, 'disable' => 1, 'name' => '自取'];
  349. $send = ['type' => 'send', 'show' => 0, 'disable' => 1, 'name' => '发货'];
  350. if ($order['hasItem'] == 2) {
  351. return [$print, $selfGet, $send];
  352. }
  353. $selfGet['disable'] = 0;
  354. $send['disable'] = 0;
  355. return [$print, $selfGet, $send];
  356. }
  357. //配送中显示按钮
  358. if ($order['status'] == self::ORDER_STATUS_SENDING) {
  359. $send = ['type' => 'send', 'show' => 0, 'disable' => 0, 'name' => '发货'];
  360. return [$print, $send];
  361. }
  362. //完成
  363. if ($order['status'] == self::ORDER_STATUS_COMPLETE) {
  364. $send = ['type' => 'send', 'show' => 0, 'disable' => 0, 'name' => '发货记录'];
  365. //自取订单不显示发货按钮
  366. if ($order['sendType'] == self::SEND_TYPE_NO) {
  367. $send['show'] = 0;
  368. }
  369. return [$print, $send];
  370. }
  371. //取消订单置灰
  372. if ($order['status'] == self::ORDER_STATUS_CANCEL) {
  373. $selfGet['disable'] = 1;
  374. $send['disable'] = 1;
  375. $print['disable'] = 1;
  376. return [$modifyProduct];
  377. }
  378. return [];
  379. }
  380. }
  381. //显示进度 ssh 2021.3.1
  382. public static function getShowProgress($showProgress, $order)
  383. {
  384. if ($showProgress == false) {
  385. return [];
  386. }
  387. $create = ['name' => '创建', 'complete' => 1];
  388. $unConfirm = ['name' => '待确认', 'complete' => 0];
  389. if ($order['status'] > 0) {
  390. $unConfirm['complete'] = 1;
  391. }
  392. $out = ['name' => '出库', 'complete' => 0];
  393. if ($order['status'] > 1) {
  394. $out['complete'] = 1;
  395. }
  396. return [$create, $unConfirm, $out];
  397. }
  398. //退款等需要使用原来的订单花材结构
  399. public static function getOriginalShowItem($showItem, $order)
  400. {
  401. if ($showItem == false) {
  402. return ['list' => [], 'itemStat' => []];
  403. }
  404. $orderSn = $order['orderSn'];
  405. $list = OrderItemClass::getOrderItem($orderSn);
  406. $totalPrice = 0;
  407. $totalBigNum = 0;
  408. $totalSmallNum = 0;
  409. $kind = 0;
  410. $itemStat = [];
  411. if (!empty($list)) {
  412. foreach ($list as $key => $val) {
  413. $list[$key]['property'] = $val['rank'] . '级';
  414. $price = $val['price'];
  415. $totalPrice = stringUtil::calcAdd($totalPrice, $price);
  416. $kind++;
  417. $currentBigNum = $val['bigNum'];
  418. $currentSmallNum = $val['smallNum'];
  419. $totalBigNum = stringUtil::calcAdd($totalBigNum, $currentBigNum);
  420. $totalSmallNum = stringUtil::calcAdd($totalSmallNum, $currentSmallNum);
  421. }
  422. $itemStat = ['kind' => $kind, 'bigNum' => $totalBigNum, 'smallNun' => $totalSmallNum, 'totalPrice' => $totalPrice];
  423. }
  424. return ['list' => $list, 'itemStat' => $itemStat];
  425. }
  426. public static function getShowItem($showItem, $order)
  427. {
  428. if ($showItem == false) {
  429. return ['list' => [], 'itemStat' => []];
  430. }
  431. $orderSn = $order['orderSn'];
  432. $list = OrderItemClass::getOrderItem($orderSn);
  433. $totalPrice = 0;
  434. $totalBigNum = 0;
  435. $totalSmallNum = 0;
  436. $kind = 0;
  437. $itemStat = [];
  438. //小菊增加部分
  439. $xjAddList = [];
  440. if (!empty($list)) {
  441. foreach ($list as $key => $val) {
  442. $price = $val['price'];
  443. $totalPrice = stringUtil::calcAdd($totalPrice, $price);
  444. $kind++;
  445. $currentBigNum = $val['bigNum'];
  446. $currentSmallNum = $val['smallNum'];
  447. $totalBigNum = stringUtil::calcAdd($totalBigNum, $currentBigNum);
  448. $totalSmallNum = stringUtil::calcAdd($totalSmallNum, $currentSmallNum);
  449. $list[$key]['property'] = '';
  450. //小菊
  451. if (isset($val['variety']) && $val['variety'] == 1) {
  452. $currentItemId = $val['id'] ?? 0;
  453. $xjList = OrderXjClass::getAllByCondition(['itemId' => $currentItemId], null, '*');
  454. if (!empty($xjList)) {
  455. //选了有颜色的小菊才把原小菊删除不显示出来
  456. unset($list[$key]);
  457. foreach ($xjList as $xjKey => $xj) {
  458. $xjBase = $val;
  459. $num = $xj['num'] ?? 0;
  460. $price = $xj['price'] ?? 0;
  461. $name = $xj['name'] ?? '';
  462. $cover = $xj['cover'] ?? '';
  463. if ($num > 0) {
  464. $bigTotalPrice = bcmul($price, $num, 2);
  465. $xjBase['cover'] = imgUtil::groupImg($cover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  466. $xjBase['unitPrice'] = $price;
  467. $xjBase['price'] = floatval($bigTotalPrice);
  468. $xjBase['num'] = floatval($num);
  469. $xjBase['bigNum'] = floatval($num);
  470. $xjBase['name'] = $name;
  471. $xjBase['property'] = '';
  472. $xjBase['userPrice'] = $bigTotalPrice;
  473. $xjAddList[] = $xjBase;
  474. }
  475. }
  476. }
  477. }
  478. }
  479. if (!empty($xjAddList)) {
  480. $list = array_merge($list, $xjAddList);
  481. }
  482. $itemStat = ['kind' => $kind, 'bigNum' => $totalBigNum, 'smallNun' => $totalSmallNum, 'totalPrice' => $totalPrice];
  483. }
  484. return ['list' => $list, 'itemStat' => $itemStat];
  485. }
  486. //整合订单员工和供应商等信息 ssh 2021.1.19
  487. //$isList 是否列表页
  488. public static function groupOrder($list, $showButton = false, $showItem = false, $showExpress = false, $isList = false)
  489. {
  490. if (empty($list)) {
  491. return $list;
  492. }
  493. $adminIds = array_unique(array_filter(array_column($list, 'adminId')));
  494. $adminInfo = AdminClass::getAdminByIds($adminIds, null, 'id');
  495. $customIds = array_column($list, 'customId');
  496. $customIds = array_unique(array_filter($customIds));
  497. $customInfoList = CustomClass::getByIds($customIds, null, 'id');
  498. foreach ($list as $key => $val) {
  499. $adminId = $val['adminId'] ?? 0;
  500. $list[$key]['adminName'] = $adminInfo[$adminId]['name'] ?? '';
  501. $list[$key]['customAddress'] = $val['fullAddress'] ?? '';
  502. //显示列表页的操作按钮
  503. $list[$key]['buttonList'] = self::getShowButton($showButton, $isList, $val);
  504. //为了列表页的打印按钮能用,列表也强制显示花材
  505. $showItem = true;
  506. //退款需要显示原有的花材结构,不显示小菊的多颜色
  507. $getOriginalItem = Yii::$app->request->get('getOriginalItem', 0);
  508. $getOriginalItem2 = Yii::$app->request->post('getOriginalItem', 0);
  509. if ($getOriginalItem == 1 || $getOriginalItem2 == 1) {
  510. $showItemData = self::getOriginalShowItem($showItem, $val);
  511. } else {
  512. $showItemData = self::getShowItem($showItem, $val);
  513. }
  514. $list[$key]['product'] = $showItemData['list'];
  515. $list[$key]['productStat'] = $showItemData['itemStat'];
  516. $showResetItem = 0;
  517. //待付款订单显示改花材按钮
  518. if ($val['status'] == self::PAY_STATUS_UN_PAY) {
  519. $showResetItem = 1;
  520. }
  521. $list[$key]['showResetItem'] = $showResetItem;
  522. //获取配送进展
  523. $expressProgress = '';
  524. if ($val['sendType'] == 4) {
  525. $expressInfo = OrderExpressClass::getByCondition(['orderSn' => $val['orderSn']]);
  526. if ($expressInfo) {
  527. $expressProgress = ['progressName' => OrderExpressClass::$statusMap[$expressInfo['status']] ?? ''];
  528. }
  529. }
  530. $list[$key]['sendTypeName'] = self::$sendTypeMap[$val['sendType']] ?? '未知';
  531. $list[$key]['expressProgress'] = $expressProgress;
  532. $list[$key]['customAvatarUrl'] = imgUtil::groupImg($val['customAvatar']);
  533. $list[$key]['customName'] = isset($val['customId']) && !empty($val['customId']) ? $val['customName'] : '散客';
  534. $customId = $val['customId'] ?? '';
  535. $debtAmount = $customInfoList[$customId]['debtAmount'] ?? 0.00;
  536. //打印数据组合
  537. $printData = self::getPrintData($val, $debtAmount, $showItemData);
  538. $list[$key]['printData'] = json_encode($printData);
  539. $list[$key]['debtAmount'] = $debtAmount;
  540. $list[$key]['customShopId'] = $customInfoList[$customId]['shopId'] ?? 0;
  541. }
  542. return $list;
  543. }
  544. public static function printGroup($current, $content)
  545. {
  546. //58mm的机器,一行打印16个汉字,32个字母
  547. $name = $current['name'] ?? '';
  548. $content .= $name . '<BR>';
  549. $name = str_repeat(' ', 10);
  550. $productNum = $current['num'] ?? '';
  551. $blankNum = bcsub(12, strlen($productNum));
  552. if ($blankNum > 0) {
  553. $productNum = $productNum . str_repeat(' ', $blankNum);
  554. }
  555. $productPrice = $current['price'] ?? '';
  556. $blankNum = bcsub(7, strlen($productPrice));
  557. if ($blankNum > 0) {
  558. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  559. }
  560. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  561. return $content;
  562. }
  563. //云打印 shish 20210709
  564. public static function onlinePrint($orderInfo)
  565. {
  566. $showItem = OrderClass::getShowItem(true, $orderInfo);
  567. $customId = $orderInfo['customId'] ?? 0;
  568. $c = CustomClass::getById($customId);
  569. $debtAmount = $c['debtAmount'] ?? 0.00;
  570. $respond = self::getPrintData($orderInfo, $debtAmount, $showItem);
  571. $content = '<B>' . $respond['sendNum'] . '</B><BR>';
  572. $content .= '--------------------------------<BR>';
  573. $content .= '<B>' . $respond['custom']['fullAddress'] . '</B><BR>';
  574. $content .= '<B>' . $respond['custom']['customName'] . '</B><BR>';
  575. $content .= '<B>' . $respond['custom']['customMobile'] . '</B><BR>';
  576. $payDate = date("Y-m-d", strtotime($orderInfo['payTime']));
  577. $sendTimWant = $orderInfo['sendTimeWant'] ?? '';
  578. if ($payDate != $sendTimWant) {
  579. $content .= '<BR>';
  580. $content .= '送货时间:<BR>';
  581. $content .= '<B>' . $orderInfo['sendTimeWant'] . '</B><BR>';
  582. }
  583. if (isset($respond['remark']) && !empty($respond['remark'])) {
  584. $content .= '<BR>';
  585. $content .= '备注:<BR>';
  586. $content .= '<B>' . $respond['remark'] . '</B><BR>';
  587. }
  588. $content .= '<BR>';
  589. $content .= '花材 数量/单价 金额 <BR>';
  590. $content .= '--------------------------------<BR>';
  591. if (isset($respond['product']) && !empty($respond['product'])) {
  592. foreach ($respond['product'] as $current) {
  593. $content = self::printGroup($current, $content);
  594. }
  595. $content .= '--------------------------------<BR>';
  596. }
  597. if (isset($respond['refund']) && !empty($respond['refund'])) {
  598. $content .= '<B>已退款:</B><BR>';
  599. foreach ($respond['refund'] as $current) {
  600. //58mm的机器,一行打印16个汉字,32个字母
  601. $name = $current['name'] ?? '';
  602. $content .= $name . '<BR>';
  603. $name = str_repeat(' ', 10);
  604. $productNum = $current['num'] ?? '';
  605. $blankNum = bcsub(12, strlen($productNum));
  606. if ($blankNum > 0) {
  607. $productNum = $productNum . str_repeat(' ', $blankNum);
  608. }
  609. $productPrice = $current['price'] ?? '';
  610. $blankNum = bcsub(7, strlen($productPrice));
  611. if ($blankNum > 0) {
  612. $productPrice = $productPrice . str_repeat(' ', $blankNum);
  613. }
  614. $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
  615. }
  616. $content .= '--------------------------------<BR>';
  617. }
  618. if (isset($respond['sendCost']) && $respond['sendCost'] > 0) {
  619. $content .= ' 运费:' . $respond['sendCost'] . '<BR>';
  620. }
  621. $content .= '订单金额:' . $respond['prePrice'] . '<BR>';
  622. if (isset($respond['discountAmount']) && !empty($respond['discountAmount']) && $respond['discountAmount'] > 0) {
  623. $content .= '优惠扣除:' . $respond['discountAmount'] . '<BR>';
  624. }
  625. if (isset($respond['refundPrice']) && $respond['refundPrice'] > 0) {
  626. $content .= '退款金额:' . $respond['refundPrice'] . '<BR>';
  627. }
  628. $content .= '应付金额:' . $respond['realPrice'] . '<BR>';
  629. if (isset($respond['debt']) && $respond['debt'] == 1) {
  630. $content .= '实付金额:0.00<BR>';
  631. } else {
  632. $content .= '实付金额:' . $respond['realPrice'] . '<BR>';
  633. }
  634. if ($debtAmount > 0) {
  635. $content .= '累计欠款:' . $debtAmount . '<BR>';
  636. }
  637. $shopId = $orderInfo['shopId'] ?? 0;
  638. $shop = ShopClass::getById($shopId, true);
  639. $sjId = $shop->sjId ?? 0;
  640. $sj = SjClass::getById($sjId, true);
  641. $sjName = $sj->name ?? '';
  642. $currentName = $sjName;
  643. if (isset($shop->default) == false || $shop->default != 1) {
  644. $shopName = $shop->shopName ?? '';
  645. if (!empty($shopName)) {
  646. $currentName = $currentName . ' ' . $shopName;
  647. }
  648. }
  649. $getInfo = self::getById($orderInfo['id']);
  650. $content .= '--------------------------------<BR>';
  651. $content .= '订单日期:' . $getInfo['addTime'] . '<BR>';
  652. $content .= '订单编号:' . $respond['orderSn'] . '<BR>';
  653. $content .= '门店名称:' . $currentName . '<BR>';
  654. $content .= "<BR>";
  655. $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
  656. $printSn = $ext['printSn'] ?? '';
  657. if (!empty($printSn)) {
  658. $p = new printUtil($printSn);
  659. $p->printMsg($content);
  660. }
  661. }
  662. //打印数据 shish 20210702
  663. public static function getPrintData($val, $debtAmount, $showItem)
  664. {
  665. $printProduct = [];
  666. if (isset($showItem['list']) && !empty($showItem['list'])) {
  667. foreach ($showItem['list'] as $itemKey => $itemVal) {
  668. $bigNum = $itemVal['bigNum'] ?? 0;
  669. $bigUnitPrice = $itemVal['unitPrice'] ?? 0;
  670. $smallUnitPrice = $itemVal['smallUnitPrice'] ?? 0;
  671. $smallNum = $itemVal['smallNum'] ?? 0;
  672. if ($bigNum > 0) {
  673. $bigTotalPrice = bcmul($bigUnitPrice, $bigNum, 2);
  674. $printProduct[] = [
  675. 'name' => $itemVal['name'] ?? '',
  676. 'num' => floatval($bigNum) . '扎*' . floatval($bigUnitPrice),
  677. 'price' => floatval($bigTotalPrice),
  678. ];
  679. }
  680. if ($smallNum > 0) {
  681. $smallTotalPrice = bcmul($smallUnitPrice, $smallNum, 2);
  682. $printProduct[] = [
  683. 'name' => $itemVal['name'] ?? '',
  684. 'num' => floatval($smallNum) . '支*' . floatval($smallUnitPrice),
  685. 'price' => floatval($smallTotalPrice),
  686. ];
  687. }
  688. }
  689. }
  690. $sendNum = $val['sendNum'] ?? '';
  691. $relateOrderSn = $val['orderSn'] ?? '';
  692. $refundList = [];
  693. $refund = RefundOrderClass::getByCondition(['relateOrderSn' => $relateOrderSn], true);
  694. if (!empty($refund)) {
  695. $refundOrderSn = $refund->orderSn ?? '';
  696. $refundProductList = RefundOrderItemClass::getAllByCondition(['orderSn' => $refundOrderSn], null, '*');
  697. if (!empty($refundProductList)) {
  698. foreach ($refundProductList as $currentRefund) {
  699. $num = $currentRefund['itemNum'] ?? 0;
  700. $unitPrice = $currentRefund['itemPrice'] ?? 0;
  701. $currentPrice = bcmul($unitPrice, $num, 2);
  702. $refundList[] = [
  703. 'name' => $currentRefund['name'] ?? '',
  704. 'num' => floatval($num) . '扎*' . floatval($unitPrice),
  705. 'price' => floatval($currentPrice),
  706. ];
  707. }
  708. }
  709. }
  710. $printData = [
  711. 'address' => '',
  712. 'sendCost' => $val['sendCost'] ?? '0.00',
  713. 'prePrice' => $val['prePrice'] ?? '0.00',
  714. 'discountAmount' => $val['discountAmount'] ?? '0.00',
  715. 'realPrice' => $val['realPrice'] ?? '0.00',
  716. 'refundPrice' => $val['refundPrice'] ?? '0.00',
  717. 'debt' => $val['debt'] ?? 0,
  718. 'orderSn' => $val['orderSn'] ?? '',
  719. 'date' => $val['addTime'] ?? '',
  720. 'remark' => $val['remark'] ?? '',
  721. 'custom' => [
  722. 'customName' => $val['customName'] ?? '',
  723. 'customMobile' => $val['customMobile'] ?? '',
  724. 'fullAddress' => $val['fullAddress'] ?? '',
  725. ],
  726. 'product' => $printProduct,
  727. 'refund' => $refundList,
  728. 'sendNum' => $sendNum,
  729. 'debtAmount' => $debtAmount,
  730. ];
  731. return $printData;
  732. }
  733. //订单的验证 ssh 2021.3.2
  734. public static function valid($info, $shopId)
  735. {
  736. if (empty($info)) {
  737. util::fail('没有找到订单');
  738. }
  739. if (isset($info['shopId']) && $info['shopId'] == $shopId) {
  740. return true;
  741. }
  742. util::fail('没有权限操作的订单');
  743. }
  744. //快递送 ssh 2021.3.16
  745. public static function thirdSend($order, $expressInfo)
  746. {
  747. if ($order->status == self::ORDER_STATUS_UN_PAY) {
  748. util::fail('订单未付款');
  749. }
  750. if ($order->status != self::ORDER_STATUS_UN_SEND) {
  751. util::fail('不是待配送状态');
  752. }
  753. $id = $order->id;
  754. $expressId = $expressInfo['expressId'] ?? 1;
  755. $expressName = $expressInfo['deliveryName'] ?? '达达';
  756. $expressRemark = $expressInfo['remark'] ?? '';
  757. $order->status = self::ORDER_STATUS_SENDING;
  758. $order->sendType = self::SEND_TYPE_THIRD;
  759. $order->expressId = $expressId;
  760. $order->save();
  761. $shopId = $order->shopId;
  762. $shop = Shop::findBySql('select * from ' . Shop::tableName() . ' where id = :id for update', ['id' => $shopId])->one();
  763. if (empty($shop)) {
  764. util::fail('没有找到门店');
  765. }
  766. $shop->unSendOrder -= 1;
  767. $shop->sendingOrder += 1;
  768. $shop->save();
  769. $express = OrderExpressClass::getByCondition(['orderId' => $id]);
  770. if (!empty($express)) {
  771. util::fail('已经发过了');
  772. }
  773. //达达
  774. $dadaRes = DadaExpressServices::addOrder($order, $expressRemark);
  775. $orderSn = $order->orderSn;
  776. $sjId = $order->sjId ?? 0;
  777. /*
  778. $getInfo = ExpressClass::getExpressInfo($expressId);
  779. $hour = $expressInfo['sendTime'] ?? '12:00';
  780. $sendCost = 12;
  781. $distance = 5;
  782. $sendTime = strtotime(date("Y-m-d ") . $hour);
  783. OrderSendClass::thirdSend($order);
  784. */
  785. $sendTime = $order->sendTime;
  786. if ($sendTime == '0000-00-00 00:00:00') {
  787. $sendTime = time();
  788. } else {
  789. $sendTime = strtotime($order->sendTime);
  790. }
  791. $expData = [
  792. 'orderId' => $id,
  793. 'orderSn' => $orderSn,
  794. 'sjId' => $sjId,
  795. 'expressId' => $expressId,
  796. 'sendTime' => $sendTime,
  797. 'cost' => $dadaRes['fee'],
  798. 'distance' => $dadaRes['distance'],
  799. 'status' => 1,
  800. 'expressName' => $expressName,
  801. 'remark' => $expressRemark,
  802. ];
  803. OrderExpressClass::addExpress($expData);
  804. OrderSendClass::thirdSend($order);
  805. $respond = [
  806. 'expressName' => $expressName,
  807. 'expressId' => $expressId,
  808. 'distance' => $dadaRes['distance'],
  809. 'cost' => $dadaRes['fee'],
  810. 'status' => 2,
  811. 'tip' => 0,
  812. ];
  813. //采购单变成已发货
  814. $purchaseId = $order->purchaseId ?? 0;
  815. PurchaseClass::sending($purchaseId);
  816. return $respond;
  817. }
  818. //本店送 ssh 2021.1.24
  819. public static function selfSend($info)
  820. {
  821. if ($info->status == self::ORDER_STATUS_UN_PAY) {
  822. util::fail('订单未付款');
  823. }
  824. if ($info->status != self::ORDER_STATUS_UN_SEND) {
  825. util::fail('不是待配送状态');
  826. }
  827. $info->status = self::ORDER_STATUS_SENDING;
  828. $info->sendType = self::SEND_TYPE_MYSELF;
  829. $info->save();
  830. $shopId = $info->shopId;
  831. $shop = Shop::findBySql('select * from ' . Shop::tableName() . ' where id = :id for update', ['id' => $shopId])->one();
  832. if (empty($shop)) {
  833. util::fail('没有找到门店');
  834. }
  835. $shop->unSendOrder -= 1;
  836. $shop->sendingOrder += 1;
  837. $shop->save();
  838. OrderSendClass::selfSend($info);
  839. //采购单变成已发货
  840. $purchaseId = $info->purchaseId ?? 0;
  841. PurchaseClass::sending($purchaseId);
  842. }
  843. //订单完成 ssh 2020.3.12
  844. public static function complete($order, $shop)
  845. {
  846. $order->status = OrderClass::ORDER_STATUS_COMPLETE;
  847. $order->currentFlow = self::TOTAL_FLOW;
  848. $order->totalFlow = self::TOTAL_FLOW;
  849. $order->save();
  850. $shop->finishOrder += 1;
  851. $shop->sendingOrder -= 1;
  852. $shop->save();
  853. return $order;
  854. }
  855. //修改订单表的配送信息 linqh 2021.3.28
  856. public static function updateCustomExpressInfo($id, $expressAddInfo)
  857. {
  858. $data = [
  859. 'province' => $expressAddInfo['province'],
  860. 'city' => $expressAddInfo['city'],
  861. 'address' => $expressAddInfo['address'],
  862. 'floor' => $expressAddInfo['floor'],
  863. 'fullAddress' => $expressAddInfo['city'] . $expressAddInfo['address'] . $expressAddInfo['floor'],
  864. 'lat' => $expressAddInfo['lat'],
  865. 'long' => $expressAddInfo['long'],
  866. 'sendTime' => $expressAddInfo['sendTime'],
  867. ];
  868. return self::updateById($id, $data);
  869. }
  870. //最客户最近的订单 shish 2021.5.4
  871. public static function getCustomOrder($id, $limit = 10, $order = null, $with = null)
  872. {
  873. $where = ['customId' => $id, 'payStatus' => 1];
  874. return self::getLimitList('*', $where, $limit, $order, $with);
  875. }
  876. //销售单取消 shish 2021.5.14
  877. public static function cancel($id)
  878. {
  879. $order = self::getLockById($id);
  880. if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
  881. util::fail("销售单{$id}不是待付款状态,无法取消");
  882. }
  883. $order->status = self::ORDER_STATUS_CANCEL;
  884. $order->save();
  885. $shopId = $order->shopId;
  886. $shop = ShopClass::getLockById($shopId);
  887. if (empty($shop)) {
  888. util::fail('没有找到门店');
  889. }
  890. $shop->unPayOrder -= 1;
  891. $shop->cancelOrder += 1;
  892. $shop->save();
  893. //加回库存
  894. $orderItem = OrderItemClass::getOrderItem($order->orderSn);
  895. foreach ($orderItem as $v) {
  896. $stockInfo = ProductClass::addStockByItemNum($v['productId'], $v['num']);
  897. $recordData = [];
  898. $recordData['sjId'] = $order->sjId;
  899. $recordData['shopId'] = $shopId;
  900. $recordData['itemId'] = $v['itemId'] ?? 0;
  901. $recordData['itemNum'] = $v['num'];
  902. $recordData['oldStock'] = $stockInfo['oldStock'];
  903. $recordData['newStock'] = $stockInfo['newStock'];
  904. $recordData['productId'] = $v['productId'];
  905. $recordData['orderSn'] = $order->orderSn;
  906. $recordData['relateName'] = $order->customName ?? '';
  907. StockRecordService::addSellStockOrderCancelRecord($recordData);
  908. }
  909. return $order;
  910. }
  911. //新订单入队列 shish 20211012
  912. public static function newOrderAddQueue($order)
  913. {
  914. $shopId = $order->shopId ?? 0;
  915. if (empty($shopId)) {
  916. return false;
  917. }
  918. $id = $order->id ?? 0;
  919. if (empty($id)) {
  920. return false;
  921. }
  922. $newOrderKey = 'ghsNewOrder_' . $shopId;
  923. Yii::$app->redis->executeCommand('LPUSH', [$newOrderKey, $id]);
  924. }
  925. }