PurchaseController.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. <?php
  2. namespace hd\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\ghs\classes\GhsClass;
  5. use biz\notice\classes\NoticeClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopExpressClass;
  8. use biz\shop\classes\ShopExtClass;
  9. use bizGhs\custom\classes\CustomClass;
  10. use bizGhs\express\services\DadaExpressServices;
  11. use bizGhs\order\classes\OrderClass;
  12. use bizGhs\order\classes\OrderItemClass;
  13. use bizHd\purchase\classes\PurchaseClass;
  14. use bizHd\purchase\classes\PurchaseItemClass;
  15. use bizHd\purchase\services\PurchaseService;
  16. use bizGhs\product\classes\ProductClass;
  17. use bizHd\wx\classes\WxOpenClass;
  18. use common\components\dateUtil;
  19. use common\components\dict;
  20. use common\components\freight;
  21. use common\components\httpUtil;
  22. use common\components\imgUtil;
  23. use common\components\noticeUtil;
  24. use common\components\orderSn;
  25. use Yii;
  26. use common\components\util;
  27. use biz\wx\classes\WxMessageClass;
  28. use bizGhs\order\services\OrderService;
  29. use common\components\lakala\Lakala;
  30. class PurchaseController extends BaseController
  31. {
  32. public $guestAccess = ['detail'];
  33. //确认收货 ssh 20231119
  34. public function actionConfirmTake()
  35. {
  36. $get = Yii::$app->request->get();
  37. $id = $get['id'] ?? 0;
  38. $info = PurchaseClass::getById($id, true);
  39. if (empty($info)) {
  40. util::fail('没有找到采购单');
  41. }
  42. if (isset($info->mainId) == false || $info->mainId != $this->mainId) {
  43. util::fail('无法访问');
  44. }
  45. PurchaseClass::confirmTake($info);
  46. util::complete('确认成功');
  47. }
  48. public function actionGetFullInfo()
  49. {
  50. $get = Yii::$app->request->get();
  51. $id = $get['id'] ?? 0;
  52. $info = PurchaseClass::getById($id);
  53. if (empty($info)) {
  54. util::fail('没有找到采购单');
  55. }
  56. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  57. util::fail('无法访问');
  58. }
  59. $orderSn = $info['orderSn'] ?? '';
  60. $list = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  61. if (!empty($list)) {
  62. foreach ($list as $key => $item) {
  63. $shortCover = $item['cover'] ?? '';
  64. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  65. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  66. $list[$key]['cover'] = $cover;
  67. $list[$key]['bigCover'] = $bigCover;
  68. $list[$key]['shortCover'] = $shortCover;
  69. }
  70. }
  71. $info['product'] = $list;
  72. util::success($info);
  73. }
  74. //虚拟供货商的采购 ssh 20230308
  75. public function actionAddVirtualCg()
  76. {
  77. $post = Yii::$app->request->post();
  78. $ghsId = $post['ghsId'] ?? 0;
  79. $payMode = $post['payMode'] ?? 0;
  80. $payWay = $post['payWay'] ?? 0;
  81. $ghsInfo = GhsClass::getById($ghsId);
  82. if (empty($ghsInfo)) {
  83. util::fail('没有找到供货商');
  84. }
  85. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  86. util::fail('不能向自己的店买花');
  87. }
  88. $connection = Yii::$app->db;
  89. $transaction = $connection->beginTransaction();
  90. try {
  91. $post['book'] = $post['book'] ?? 0;
  92. $post['sjId'] = $this->sjId;
  93. $post['shopId'] = $this->shopId;
  94. $post['shopAdminId'] = $this->shopAdminId;
  95. $post['ghsId'] = $ghsId;
  96. $post['ghsName'] = $ghsInfo['name'] ?? '';
  97. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  98. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  99. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  100. $customId = $ghsInfo['customId'] ?? 0;
  101. $post['customId'] = $customId;
  102. $shopAdmin = $this->shopAdmin ?? null;
  103. $name = $shopAdmin->name ?? '';
  104. $post['shopAdminName'] = $name;
  105. $post['mainId'] = $this->mainId;
  106. $product = $post['product'] ?? '';
  107. $productList = json_decode($product, true);
  108. //判断product数据是不是同个供货商的
  109. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  110. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  111. if (!empty($productInfoList)) {
  112. foreach ($productInfoList as $currentInfo) {
  113. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $this->mainId) {
  114. util::fail('只能选择同一家的商品哦');
  115. }
  116. }
  117. if (count($productInfoList) != count($ids)) {
  118. util::fail('存在无效商品');
  119. }
  120. } else {
  121. util::fail('花材不存在');
  122. }
  123. //供货商预订单最低公斤数要求和每公斤服务费
  124. $ghsInfo['kiloFee'] = 0;
  125. $ghsInfo['miniKilo'] = 0;
  126. $post['product'] = $productList;
  127. $packCost = 0;
  128. $post['packCost'] = $packCost;
  129. $post['sendCost'] = isset($post['sendCost']) && $post['sendCost'] > 0 ? $post['sendCost'] : 0;
  130. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  131. if ($payMode == 0) {
  132. //欠款
  133. PurchaseService::debt($respond);
  134. } else {
  135. //线下支付
  136. PurchaseService::payAfter($respond, $payWay);
  137. $saleId = $respond->saleId ?? 0;
  138. $order = OrderClass::getById($saleId, true);
  139. if (empty($order)) {
  140. util::fail('没有找到订单');
  141. }
  142. OrderService::payAfter($order, $payWay, true);
  143. }
  144. $transaction->commit();
  145. util::success($respond);
  146. } catch (Exception $e) {
  147. $transaction->rollBack();
  148. util::fail();
  149. }
  150. }
  151. //生成采购单 ssh 2021.1.17
  152. public function actionCreateOrder()
  153. {
  154. $post = Yii::$app->request->post();
  155. //多颜色数据整理
  156. $colorItem = $post['xj'] ?? [];
  157. $newXj = [];
  158. if (!empty($colorItem)) {
  159. $colorData = json_decode($colorItem, true);
  160. if (!empty($colorData) && is_array($colorData)) {
  161. foreach ($colorData as $colorInfo) {
  162. $ptItemId = $colorInfo['ptItemId'] ?? 0;
  163. $colorList = $colorInfo['list'] ?? [];
  164. if (!empty($colorList)) {
  165. foreach ($colorList as $colorItemKey => $colorItem) {
  166. $newXj[$ptItemId][] = $colorItem;
  167. }
  168. }
  169. }
  170. }
  171. }
  172. $post['xj'] = $newXj;
  173. $ghsId = $post['ghsId'] ?? 0;
  174. //供货商
  175. $ghsInfo = GhsClass::getById($ghsId);
  176. if (empty($ghsInfo)) {
  177. util::fail('没有找到供货商');
  178. }
  179. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  180. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  181. util::fail('不能跟自己的店买花');
  182. }
  183. $connection = Yii::$app->db;//事务处理
  184. $transaction = $connection->beginTransaction();
  185. try {
  186. $post['book'] = $post['book'] ?? 0;
  187. $post['sjId'] = $this->sjId;
  188. $post['shopId'] = $this->shopId;
  189. $post['shopAdminId'] = $this->shopAdminId;
  190. $post['ghsId'] = $ghsId;
  191. $post['ghsName'] = $ghsInfo['name'] ?? '';
  192. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  193. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  194. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  195. $customId = $ghsInfo['customId'] ?? 0;
  196. $post['customId'] = $customId;
  197. $shopAdmin = $this->shopAdmin ?? null;
  198. $name = $shopAdmin->name ?? '';
  199. $post['shopAdminName'] = $name;
  200. $post['mainId'] = $this->mainId;
  201. $product = $post['product'] ?? '';
  202. $productList = json_decode($product, true);
  203. //判断product数据是不是同个供货商的
  204. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  205. $ghsMainId = $ghsShopInfo->mainId ?? 0;
  206. $isOpen = ShopClass::isOpen($ghsShopInfo);
  207. $book = $post['book'] ?? 0;
  208. if ($isOpen == 0 && $book == 0) {
  209. util::fail('本店已休息,请选择其它门店');
  210. }
  211. if (isset($post['transType']) && $post['transType'] == 4) {
  212. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  213. $notSameCity = PurchaseClass::notSameCity($this->shop, $ghsShopInfo);
  214. if ($notSameCity == false) {
  215. util::fail('距离太远,不能选择同城配送');
  216. }
  217. }
  218. }
  219. if ($book == 1) {
  220. $sendTimeWant = isset($post['sendTimeWant']) && !empty($post['sendTimeWant']) ? $post['sendTimeWant'] : date("Y-m-d");
  221. $future = date("Y-m-d", strtotime('+2 day'));
  222. if (strtotime($sendTimeWant) < strtotime($future)) {
  223. //util::fail('请选择时间');
  224. }
  225. $nowTime = strtotime(date("Y-m-d"));
  226. if (strtotime($sendTimeWant) < $nowTime) {
  227. util::fail('不能选择过去时间');
  228. }
  229. }
  230. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  231. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  232. if (!empty($productInfoList)) {
  233. $presellData = [];
  234. $nowTime = strtotime(date("Y-m-d"));
  235. foreach ($productInfoList as $currentInfo) {
  236. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $ghsMainId) {
  237. util::fail('只能选择同一家的商品哦');
  238. }
  239. $currentName = $currentInfo['name'] ?? '';
  240. if (isset($currentInfo['frontHide']) && $currentInfo['frontHide'] == 1) {
  241. util::fail($currentName . '没有库存,请删除');
  242. }
  243. $presell = $currentInfo['presell'] ?? 0;
  244. $presellData[] = $presell;
  245. if ($presell == 1) {
  246. if ($book == 1) {
  247. util::fail('预订时不能选择预售花材');
  248. } else {
  249. if (isset($post['sendTimeWant']) == false || empty($post['sendTimeWant'])) {
  250. util::fail('请选择配送日期');
  251. }
  252. $sendTimeWant = $post['sendTimeWant'];
  253. if (strtotime($sendTimeWant) < $nowTime) {
  254. util::fail('不能选择过去时间');
  255. }
  256. $hasDateStr = $currentInfo['presellDate'] ?? [];
  257. $hasDate = explode(',', trim($hasDateStr));
  258. if (empty($hasDate)) {
  259. util::fail('预售日期有问题,请提醒门店');
  260. }
  261. if (in_array(strtotime($sendTimeWant), $hasDate) == false) {
  262. util::fail("有预售花材在{$sendTimeWant}没到货");
  263. }
  264. $post['presell'] = 1;
  265. }
  266. }
  267. }
  268. if (count($productInfoList) != count($ids)) {
  269. util::fail('存在无效商品');
  270. }
  271. $presellData = array_unique($presellData);
  272. if (count($presellData) > 1) {
  273. util::fail('预售和非预售花材不能一起下单');
  274. }
  275. } else {
  276. util::fail('花材不存在');
  277. }
  278. //供货商预订单最低公斤数要求和每公斤服务费
  279. $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0;
  280. $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0;
  281. $post['product'] = $productList;
  282. $sendType = $post['sendType'] ?? 0;
  283. $transType = $post['transType'] ?? 0;
  284. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  285. //昆明发货包装费和运费的计算
  286. $totalWeight = 0;
  287. $totalItemNum = 0;
  288. foreach ($productList as $itemData) {
  289. $bigNum = $itemData['bigNum'] ?? 0;
  290. $thisWeight = $itemData['weight'] ?? 0;
  291. $currentWeight = bcmul($thisWeight, $bigNum, 2);
  292. $totalWeight = bcadd($currentWeight, $totalWeight, 2);
  293. $totalItemNum = bcadd($totalItemNum, $bigNum, 2);
  294. }
  295. $totalItemNum = floatval($totalItemNum);
  296. $kmPackCost = PurchaseClass::getKmPackCost($ghsMainId);
  297. $packCost = 0;
  298. if (!empty($kmPackCost)) {
  299. foreach ($kmPackCost as $itemPack) {
  300. $kiloNum = $itemPack['num'] ?? 0;
  301. if ($kiloNum >= $totalWeight) {
  302. $packCost = $itemPack['amount'] ?? 0;
  303. break;
  304. }
  305. }
  306. }
  307. //同城配送和到店自取免打包费
  308. if ($transType == 4 || $transType == 5) {
  309. $packCost = 0;
  310. }
  311. //如果已经算过一次打包费和运费了,则不再费了
  312. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
  313. if ($needAdd == false) {
  314. $packCost = 0;
  315. }
  316. $post['packCost'] = $packCost;
  317. $transCost = dict::getDict('transCost', null, null, $ghsShopInfo->mainId);
  318. $sendCost = 0;
  319. if (!empty($transCost)) {
  320. foreach ($transCost as $trans) {
  321. $sign = $trans['sign'] ?? 0;
  322. if ($sign == $transType) {
  323. if ($sign == 2) {
  324. //冷链物流
  325. $llOption = $trans['option'] ?? [];
  326. if (!empty($llOption)) {
  327. $largeInfo = end($llOption);
  328. $largeNum = $largeInfo['num'] ?? 0;
  329. $largeAmount = $largeInfo['amount'] ?? 0;
  330. $bs = intval($totalItemNum / $largeNum);
  331. if ($bs > 0) {
  332. $addSendCost = bcmul($bs, $largeAmount, 2);
  333. $sendCost = bcadd($sendCost, $addSendCost, 2);
  334. $subNum = bcmul($bs, $largeNum, 2);
  335. $subNum = floatval($subNum);
  336. $totalItemNum = bcsub($totalItemNum, $subNum, 2);
  337. $totalItemNum = floatval($totalItemNum);
  338. }
  339. foreach ($llOption as $miniItem) {
  340. if ($miniItem['num'] >= $totalItemNum) {
  341. $sendCost = bcadd($sendCost, $miniItem['amount'], 2);
  342. break;
  343. }
  344. }
  345. }
  346. } else {
  347. //其它物流
  348. $perKiloCost = $trans['perKiloCost'] ?? 0;
  349. $sendCost = bcmul($perKiloCost, $totalWeight, 3);
  350. $sendCost = round($sendCost, 2);
  351. }
  352. }
  353. }
  354. }
  355. //如果已经算过一次打包费和运费了,则不再费了
  356. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
  357. if ($needAdd == false) {
  358. $sendCost = 0;
  359. }
  360. $post['sendCost'] = $sendCost;
  361. //标记为昆明到地方订单
  362. $post['localOrder'] = 1;
  363. } else {
  364. //同城发货包装费和运费的计算
  365. $sendCost = 0;
  366. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  367. $weight = 0;
  368. foreach ($productList as $itemData) {
  369. $bigNum = $itemData['bigNum'] ?? 0;
  370. $thisWeight = $itemData['weight'] ?? 0;
  371. $currentWeight = bcmul($thisWeight, $bigNum, 2);
  372. $weight = bcadd($currentWeight, $weight, 2);
  373. }
  374. $result = PurchaseClass::getDistanceFee($this->shop, $ghsShopInfo, $weight);
  375. $sendCost = $result['fee'] ?? 0;
  376. }
  377. $post['sendCost'] = $sendCost;
  378. //出车、发快递、发物流可以算包装费
  379. $packCost = 0;
  380. $itemTotalAmount = $post['itemTotalAmount'] ?? 0;
  381. unset($post['itemTotalAmount']);
  382. if ($sendType == dict::getDict('sendType', 'express')) {
  383. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $itemTotalAmount);
  384. if ($needAdd) {
  385. $packCost = $ghsShopInfo->packCost ?? 0;
  386. }
  387. }
  388. $post['packCost'] = $packCost;
  389. }
  390. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  391. if ($sendType == 0) {
  392. if (getenv('YII_ENV') == 'production') {
  393. //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送
  394. if ($ghsShopId == 4608) {
  395. if ($respond->actPrice < 500) {
  396. $transaction->rollBack();
  397. util::fail('满500元支持免费配送');
  398. }
  399. }
  400. //叶上花,满500元市区免费配送
  401. if ($ghsShopId == 4804) {
  402. if ($respond->actPrice < 500) {
  403. $transaction->rollBack();
  404. util::fail('满500元支持免费配送');
  405. }
  406. }
  407. //花悠星 国恋 勇记 易宝暂时不支持免费配送
  408. if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778 || $ghsShopId == 12003) {
  409. util::fail('暂不支持送货上门,请选其它方式');
  410. }
  411. }
  412. }
  413. if ($sendType == 2) {
  414. if (in_array($ghsShopId, [520, 1489, 13628, 3, 7855, 7687])) {
  415. util::fail('暂时不能使用跑腿');
  416. }
  417. }
  418. if (getenv('YII_ENV') == 'production') {
  419. //昆明花落谁家
  420. if ($ghsShopId == 13398) {
  421. if ($respond->bigNum < 15) {
  422. $transaction->rollBack();
  423. util::fail('不足15扎,请联系店长');
  424. }
  425. }
  426. }
  427. $transaction->commit();
  428. util::success($respond);
  429. } catch (Exception $e) {
  430. $transaction->rollBack();
  431. util::fail();
  432. }
  433. }
  434. //赊账 ssh 2021.1.24
  435. public function actionDebtPay()
  436. {
  437. $get = Yii::$app->request->get();
  438. $orderSn = $get['orderSn'] ?? 0;
  439. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  440. PurchaseService::valid($info, $this->shopId);
  441. //解决重复和并发提交
  442. $cacheKey = 'hd_debt_pay_' . $orderSn;
  443. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  444. if (!empty($has)) {
  445. util::fail('请稍等...');
  446. }
  447. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  448. if ($info->status == 5) {
  449. util::fail('订单已取消');
  450. }
  451. if ($info->status != 1) {
  452. util::fail('订单不是待付款状态');
  453. }
  454. if ($info->book == 1) {
  455. util::fail('预售不能记欠款');
  456. }
  457. $ghsId = $info->ghsId;
  458. $ghs = GhsClass::getGhsInfo($ghsId);
  459. $customId = $ghs['customId'] ?? 0;
  460. $custom = CustomClass::getCustom($customId);
  461. if (empty($custom)) {
  462. util::fail('没有找到客户');
  463. }
  464. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  465. util::fail('请先申请赊账权限');
  466. }
  467. $connection = Yii::$app->db;//事务处理
  468. $transaction = $connection->beginTransaction();
  469. try {
  470. //延期支付
  471. PurchaseService::debt($info);
  472. $transaction->commit();
  473. if (isset($info->localOrder) && $info->localOrder == 1) {
  474. if ($info->transType != 5 && $info->transType != 4) {
  475. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  476. $current = date("Y_m_d");
  477. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  478. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  479. }
  480. } else {
  481. if (isset($info->sendType) && $info->sendType == 4) {
  482. //标记当天已收一次包装费,岭南批发市场用的功能
  483. $current = date("Y_m_d");
  484. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  485. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  486. }
  487. }
  488. //不是预订单,并且采购人没有开批发店,供货商不是源花汇、小明鲜花、淄博花超、石头花艺,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
  489. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  490. if (!empty($cg)) {
  491. if (isset($cg->book) && $cg->book == 0) {
  492. $ghsShopId = $ghs['shopId'] ?? 0;
  493. if (getenv('YII_ENV') == 'production') {
  494. $specialList = [10649, 8042, 16454, 21319];
  495. } else {
  496. $specialList = [36523];
  497. }
  498. if (!in_array($ghsShopId, $specialList)) {
  499. $cgShopId = $cg->shopId ?? 0;
  500. $cgShop = ShopClass::getById($cgShopId, true);
  501. if (!empty($cgShop)) {
  502. if (isset($cgShop->pfShopId) && $cgShop->pfShopId == 0) {
  503. PurchaseClass::confirmTake($cg);
  504. }
  505. }
  506. }
  507. }
  508. }
  509. //app新订单通知
  510. $saleId = $info->saleId ?? 0;
  511. $order = OrderClass::getById($saleId, true);
  512. if (!empty($order)) {
  513. $shopId = $order->shopId ?? 0;
  514. $shop = ShopClass::getById($shopId, true);
  515. if (!empty($shop)) {
  516. //微信通知供货商
  517. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  518. $noticeText = json_encode(['orderId' => $saleId]);
  519. $noticeKey = "hdCgNoticeGhs";
  520. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  521. }
  522. //订单生成时唤起在线打印
  523. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  524. //有几个地方要同步去修改
  525. if (getenv('YII_ENV') == 'production') {
  526. //源花汇不自动打开小票
  527. $mallOrderNoPrint = [10536];
  528. } else {
  529. $mallOrderNoPrint = [];
  530. }
  531. if (in_array($order->mainId, $mallOrderNoPrint)) {
  532. } else {
  533. OrderClass::onlinePrint($order);
  534. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  535. if (isset($ext->printSn) && !empty($ext->printSn)) {
  536. $order->printNum += 1;
  537. $order->save();
  538. }
  539. }
  540. }
  541. }
  542. } catch (Exception $e) {
  543. $transaction->rollBack();
  544. util::fail('支付失败');
  545. }
  546. util::success($info->attributes);
  547. }
  548. //余额支付 ssh 2021.1.24
  549. public function actionBalancePay()
  550. {
  551. $shopAdmin = $this->shopAdmin;
  552. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  553. util::fail('超管才能操作');
  554. }
  555. $get = Yii::$app->request->get();
  556. $orderSn = $get['orderSn'] ?? 0;
  557. $password = $get['password'] ?? '';
  558. // 重新获取,不加密的用户数据
  559. $admin = AdminClass::getById($this->adminId, true);
  560. if (password_verify($password, $admin->payPassword) == false) {
  561. util::fail('密码错误');
  562. }
  563. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  564. if (empty($info)) {
  565. util::fail('没有找到采购单');
  566. }
  567. if ($info->status == 5) {
  568. util::fail('订单已取消');
  569. }
  570. if ($info->status != 1) {
  571. util::fail('订单不是待付款状态');
  572. }
  573. PurchaseService::valid($info, $this->shopId);
  574. $connection = Yii::$app->db;//事务处理
  575. $transaction = $connection->beginTransaction();
  576. try {
  577. //余额支付
  578. purchaseService::balancePay($info);
  579. $transaction->commit();
  580. } catch (Exception $e) {
  581. $transaction->rollBack();
  582. util::fail('支付失败');
  583. }
  584. util::success($info->attributes);
  585. }
  586. //采购记录 ssh 2021.1.24
  587. public function actionList()
  588. {
  589. $get = Yii::$app->request->get();
  590. $where = ['shopId' => $this->shopId];
  591. $status = $get['status'] ?? 0;
  592. if (!empty($status)) {
  593. $where['status'] = $status;
  594. }
  595. $respond = PurchaseService::getPurchaseList($where);
  596. $respond['shop'] = $this->shop->attributes;
  597. util::success($respond);
  598. }
  599. //采购详情 ssh 2021.01.25
  600. public function actionDetail()
  601. {
  602. $get = Yii::$app->request->get();
  603. $id = $get['id'] ?? 0;
  604. $info = PurchaseService::getInfo($id, true, true);
  605. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  606. //不能注释,分享付款功能要用,客户没有登录也能付款
  607. //util::fail('无法访问');
  608. }
  609. $customId = $info['customId'] ?? 0;
  610. $custom = CustomClass::getById($customId);
  611. $customName = $custom['name'] ?? '';
  612. $customMobile = $custom['mobile'] ?? '';
  613. $info['customName'] = $customName;
  614. $info['customMobile'] = $customMobile;
  615. $hasDebtPay = $custom['debt'] ?? 0;
  616. $book = $info['book'] ?? 0;
  617. $presell = $info['presell'] ?? 0;
  618. //预售不能记欠款
  619. if ($book == 1) {
  620. $hasDebtPay = 0;
  621. }
  622. if ($presell == 1) {
  623. $hasDebtPay = 0;
  624. }
  625. $shop = $this->shop;
  626. $info['pfShopId'] = $shop->pfShopId ?? 0;
  627. $info['hasDebtPay'] = $hasDebtPay;
  628. $info['balance'] = $shop->balance ?? 0;
  629. $getPayType = dict::getDict('getPayType');
  630. $info['getPayType'] = $getPayType;
  631. //获取售后条件
  632. $ghsId = $info['ghsId'] ?? 0;
  633. $ghsInfo = GhsClass::getById($ghsId, true);
  634. if (empty($ghsInfo)) {
  635. util::fail('没有找到供货商');
  636. }
  637. $info['afterSale'] = $ghsInfo->afterSale ?? 1;
  638. //重要,客户看到的电话修改成门店的联系电话
  639. $ghsShopId = $ghsInfo->shopId ?? 0;
  640. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  641. $info['ghsMobile'] = $ghsShopInfo->telephone ?? '';
  642. util::success($info);
  643. }
  644. //可用的支付方式 ssh 2021.1.25
  645. public function actionPayWay()
  646. {
  647. $button = [
  648. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  649. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  650. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  651. ];
  652. util::success(['button' => $button]);
  653. }
  654. //待结款明细 ssh 2021.1.26
  655. public function actionDebtList()
  656. {
  657. $get = Yii::$app->request->get();
  658. $id = $get['id'] ?? 0;
  659. $info = GhsClass::getGhsInfo($id);
  660. GhsClass::valid($info, $this->shopId);
  661. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  662. $searchTime = $get['searchTime'] ?? '';
  663. if (!empty($searchTime)) {
  664. $startTime = $get['startTime'] ?? '';
  665. $endTime = $get['endTime'] ?? '';
  666. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  667. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  668. }
  669. $respond = PurchaseService::getDebtList($where);
  670. util::success($respond);
  671. }
  672. //微信支付 ssh 2021.4.11
  673. public function actionAliPay()
  674. {
  675. ini_set('date.timezone', 'Asia/Shanghai');
  676. $post = Yii::$app->request->post();
  677. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  678. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  679. if (empty($order)) {
  680. util::fail('订单号无效');
  681. }
  682. if ($order->status == 5) {
  683. util::fail('订单已取消');
  684. }
  685. if ($order->status != 1) {
  686. util::fail('订单不是待付款状态');
  687. }
  688. $deadline = $order->deadline;
  689. $current = time();
  690. if (($current + 50) > strtotime($deadline)) {
  691. util::fail('订单已失效,请重新下单');
  692. }
  693. $ghsId = $order->ghsId ?? 0;
  694. $ghs = GhsClass::getById($ghsId, true);
  695. if (empty($ghs)) {
  696. util::fail('没有供货商信息');
  697. }
  698. $ghsShopId = $ghs->shopId ?? 0;
  699. $ghsShop = ShopClass::getById($ghsShopId, true);
  700. if (empty($ghsShop)) {
  701. util::fail('没有供货商门店信息');
  702. }
  703. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  704. util::fail('商家支付功能未开通');
  705. }
  706. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  707. if ($order->changePrice == 2) {
  708. $oldOrderSn = $orderSn;
  709. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  710. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  711. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  712. $params = [
  713. 'appid' => 'OP00002119',
  714. 'serial_no' => '018b08cfddbd',
  715. 'merchant_no' => $ghsShop->lklSjNo,
  716. 'term_no' => $ghsShop->lklScanTermNo,
  717. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  718. 'lklCertificatePath' => $lklCertificatePath,
  719. ];
  720. $laResource = new Lakala($params);
  721. $closeParams = [
  722. 'orderSn' => $oldOrderSn,
  723. ];
  724. $response = $laResource->cashClose($closeParams);
  725. if (isset($response['code']) == false || $response['code'] != '000000') {
  726. $msg = $response['msg'] ?? '';
  727. noticeUtil::push('注意花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功(收银台-支付宝),' . $msg, '15280215347');
  728. }
  729. $newOrderSn = orderSn::getPurchaseSn();
  730. $newGhsOrderSn = orderSn::getGhsOrderSn();
  731. $orderSn = $newOrderSn;
  732. $order->orderSn = $newOrderSn;
  733. $order->save();
  734. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  735. $saleId = $order->saleId ?? 0;
  736. $sale = OrderClass::getById($saleId, true);
  737. if (!empty($sale)) {
  738. $oldSaleOrderSn = $sale->orderSn ?? '';
  739. $sale->orderSn = $newGhsOrderSn;
  740. $sale->save();
  741. OrderItemClass::updateByCondition(['orderSn' => $oldSaleOrderSn], ['orderSn' => $newGhsOrderSn]);
  742. }
  743. }
  744. $order->changePrice = 2;
  745. $order->save();
  746. $name = $orderSn;
  747. $totalFee = $order->realPrice;
  748. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  749. $ghsId = $order->ghsId ?? 0;
  750. $ghs = GhsClass::getById($ghsId, true);
  751. if (empty($ghs)) {
  752. util::fail('没有供货商信息');
  753. }
  754. $ghsShopId = $ghs->shopId ?? 0;
  755. $ghsShop = ShopClass::getById($ghsShopId, true);
  756. if (empty($ghsShop)) {
  757. util::fail('没有供货商门店信息');
  758. }
  759. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  760. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  761. $params = [
  762. 'appid' => 'OP00002119',
  763. 'serial_no' => '018b08cfddbd',
  764. 'merchant_no' => $ghsShop->lklSjNo,
  765. 'term_no' => $ghsShop->lklB2BTermNo,
  766. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  767. 'lklCertificatePath' => $lklCertificatePath,
  768. ];
  769. $laResource = new Lakala($params);
  770. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  771. $wxParams = [
  772. 'orderSn' => $orderSn,
  773. 'amount' => $totalFee,
  774. 'capitalType' => $purchaseCapital,
  775. 'notifyUrl' => $notifyUrl,
  776. 'subject' => $name,
  777. ];
  778. $response = $laResource->cashierPay($wxParams);
  779. if (isset($response['code']) == false || $response['code'] != '000000') {
  780. $errMsg = $response['msg'] ?? '';
  781. noticeUtil::push('编号AA90:' . $errMsg, '15280215347');
  782. util::fail('发起支付失败');
  783. }
  784. $counter_url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  785. $new = [
  786. 'payUrl' => $counter_url,
  787. 'orderSn' => $orderSn,
  788. ];
  789. util::success($new);
  790. }
  791. //微信支付 ssh 2021.4.11
  792. public function actionWxPay()
  793. {
  794. ini_set('date.timezone', 'Asia/Shanghai');
  795. $post = Yii::$app->request->post();
  796. $couponId = $post['couponId'] ?? 0;
  797. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  798. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  799. if (empty($order)) {
  800. util::fail('订单号无效');
  801. }
  802. if ($order->status == 5) {
  803. util::fail('订单已取消');
  804. }
  805. if ($order->status != 1) {
  806. util::fail('订单不是待付款状态');
  807. }
  808. $deadline = $order->deadline;
  809. $current = time();
  810. if (($current + 50) > strtotime($deadline)) {
  811. util::fail('订单已失效,请重新下单');
  812. }
  813. $ghsId = $order->ghsId ?? 0;
  814. $ghs = GhsClass::getById($ghsId, true);
  815. if (empty($ghs)) {
  816. util::fail('没有供货商信息');
  817. }
  818. $ghsShopId = $ghs->shopId ?? 0;
  819. $ghsShop = ShopClass::getById($ghsShopId, true);
  820. if (empty($ghsShop)) {
  821. util::fail('没有供货商门店信息');
  822. }
  823. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  824. util::fail('商家支付功能未开通');
  825. }
  826. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  827. if ($order->changePrice == 2) {
  828. $oldOrderSn = $orderSn;
  829. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  830. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  831. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  832. $params = [
  833. 'appid' => 'OP00002119',
  834. 'serial_no' => '018b08cfddbd',
  835. 'merchant_no' => $ghsShop->lklSjNo,
  836. 'term_no' => $ghsShop->lklScanTermNo,
  837. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  838. 'lklCertificatePath' => $lklCertificatePath,
  839. ];
  840. $laResource = new Lakala($params);
  841. $closeParams = [
  842. 'orderSn' => $oldOrderSn,
  843. ];
  844. $response = $laResource->close($closeParams);
  845. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  846. $msg = $response['msg'] ?? '';
  847. noticeUtil::push('重点注意,花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg, '15280215347');
  848. util::fail('出错了,请重新下一单');
  849. }
  850. $newOrderSn = orderSn::getPurchaseSn();
  851. $newGhsOrderSn = orderSn::getGhsOrderSn();
  852. $orderSn = $newOrderSn;
  853. $order->orderSn = $newOrderSn;
  854. $order->save();
  855. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  856. $saleId = $order->saleId ?? 0;
  857. $sale = OrderClass::getById($saleId, true);
  858. if (!empty($sale)) {
  859. $oldSaleOrderSn = $sale->orderSn ?? '';
  860. $sale->orderSn = $newGhsOrderSn;
  861. $sale->save();
  862. OrderItemClass::updateByCondition(['orderSn' => $oldSaleOrderSn], ['orderSn' => $newGhsOrderSn]);
  863. }
  864. }
  865. $id = $order->id;
  866. $order->changePrice = 2;
  867. $order->save();
  868. $name = $orderSn;
  869. $totalFee = $order->realPrice;
  870. $openId = '';
  871. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  872. $openId = $post['currentMiniOpenId'];
  873. //noticeUtil::push('花店采购下单时,通过前端获取了openId:' . $openId, '15280215347');
  874. }
  875. if (empty($openId)) {
  876. if (isset($this->admin) && !empty($this->admin)) {
  877. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  878. $openId = $this->admin->miniOpenId;
  879. noticeUtil::push('花店采购下单时,通过数据库获取了openId:' . $openId, '15280215347');
  880. }
  881. }
  882. }
  883. if (empty($openId)) {
  884. $currentShopId = $order->shopId ?? 0;
  885. $currentShop = ShopClass::getById($currentShopId, true);
  886. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  887. $currentMobile = $currentShop->mobile ?? '';
  888. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  889. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  890. }
  891. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  892. //花卉宝代为申请的微信支付
  893. $merchantExtend = WxOpenClass::getWxInfo();
  894. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  895. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  896. $params = [
  897. 'appid' => 'OP00002119',
  898. 'serial_no' => '018b08cfddbd',
  899. 'merchant_no' => $ghsShop->lklSjNo,
  900. 'term_no' => $ghsShop->lklScanTermNo,
  901. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  902. 'lklCertificatePath' => $lklCertificatePath,
  903. ];
  904. $laResource = new Lakala($params);
  905. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  906. $wxParams = [
  907. 'orderSn' => $orderSn,
  908. 'amount' => $totalFee,
  909. 'capitalType' => $purchaseCapital,
  910. 'notifyUrl' => $notifyUrl,
  911. 'wxAppId' => $merchantExtend['miniAppId'],
  912. 'openId' => $openId,
  913. 'subject' => $name,
  914. 'couponId' => $couponId,
  915. ];
  916. $response = $laResource->driveWxPay($wxParams);
  917. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  918. $errMsg = $response['msg'] ?? '';
  919. noticeUtil::push('编号99891:' . $errMsg, '15280215347');
  920. util::fail('支付失败');
  921. }
  922. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  923. $appId = $newParams['app_id'] ?? '';
  924. $nonceStr = $newParams['nonce_str'] ?? '';
  925. $paySign = $newParams['pay_sign'] ?? '';
  926. $package = $newParams['package'] ?? '';
  927. $signType = $newParams['sign_type'] ?? '';
  928. $timeStamp = $newParams['time_stamp'] ?? '';
  929. $new = [
  930. 'id' => $id,
  931. 'appId' => $appId,
  932. 'nonceStr' => $nonceStr,
  933. 'paySign' => $paySign,
  934. 'package' => $package,
  935. 'signType' => $signType,
  936. 'timeStamp' => $timeStamp,
  937. 'orderSn' => $orderSn,
  938. ];
  939. util::success($new);
  940. }
  941. //计算运费 ssh 20221003
  942. public function actionFreight()
  943. {
  944. $get = Yii::$app->request->get();
  945. $shop = $this->shop;
  946. $shopLat = isset($shop->lat) ? $shop->lat : '';
  947. $shopLong = isset($shop->long) ? $shop->long : '';
  948. if (empty($shopLat) || empty($shopLong)) {
  949. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  950. }
  951. $ghsId = $get['ghsId'] ?? 0;
  952. $ghs = GhsClass::getById($ghsId, true);
  953. if (empty($ghs)) {
  954. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  955. }
  956. $ghsShopId = $ghs->shopId ?? 0;
  957. $ghsShop = ShopClass::getById($ghsShopId, true);
  958. if (empty($ghsShop)) {
  959. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  960. }
  961. $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : '';
  962. $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : '';
  963. if (empty($ghsShopLat) || empty($ghsShopLong)) {
  964. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  965. }
  966. $weight = $get['weight'] ?? 0;
  967. if (empty($weight)) {
  968. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  969. }
  970. $respond = PurchaseClass::getDistanceFee($this->shop, $ghsShop, $weight);
  971. util::success($respond);
  972. }
  973. //运费计算 lqh 2021.4.12 暂时返回固定值
  974. public function actionFreight2()
  975. {
  976. //lqh 2021.6.25 运费固定返回0
  977. util::success(['sedCost' => 0]);
  978. $post = Yii::$app->request->post();
  979. $ghsId = $post['ghsId'] ?? 0;
  980. //供货商
  981. $ghsInfo = GhsClass::getById($ghsId);
  982. if (empty($ghsInfo)) {
  983. util::fail('没有找到供货商');
  984. }
  985. //花材信息
  986. $productJson = $post['product'] ?? '';
  987. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  988. if (empty($productJson)) {
  989. util::fail('请选择花材');
  990. }
  991. $productList = json_decode($productJson, true);
  992. if (empty($productList)) {
  993. util::fail('请选择花材');
  994. }
  995. $productList = PurchaseClass::mergeItemInfo($productList);
  996. $weight = 0;
  997. $price = 0;
  998. $productData = ProductClass::getProductMapData($productList);
  999. $bigNum = 0;
  1000. foreach ($productList as $key => $val) {
  1001. $productId = $val['productId'];
  1002. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  1003. $bigNum += $val['bigNum'];
  1004. $weight = bcadd($w, $weight, 2);
  1005. $ratio = $productData[$productId]['ratio'] ?? 0;
  1006. //大小数量合并多少扎
  1007. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  1008. //售卖价格
  1009. $itemPrice = $productData[$productId]['price'] ?? 0;
  1010. //合计价格
  1011. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  1012. }
  1013. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  1014. $ghsSjId = $ghsInfo['sjId'] ?? 0;
  1015. $sendTime = $post['sendTime'] ?? '';
  1016. if (!empty($sendTime)) {
  1017. //配送时间不为空
  1018. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  1019. $sendTimeInt = strtotime($sendTime);
  1020. $now = time();
  1021. $diff = $sendTimeInt - $now;
  1022. //因服务器写入时间,改为至少提前6分钟
  1023. if ($diff <= 360) {
  1024. util::fail('配送时间至少提前6分钟');
  1025. }
  1026. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  1027. } else {
  1028. $sendTime = date('Y-m-d H:i', strtotime("+30 minute"));
  1029. }
  1030. //找供货商的客户信息,用里面的配送相关地址信息
  1031. $customId = $ghsInfo['customId'] ?? 0;
  1032. $customInfo = CustomClass::getById($customId);
  1033. if (empty($customInfo)) {
  1034. util::fail('没有找到客户');
  1035. }
  1036. //判断商家是否开启达达,若未开启,则使用自定义运费计算
  1037. $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
  1038. if (!$shopNo) {
  1039. //使用自定义运费
  1040. //util::fail('供货商暂未开通配送');
  1041. $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight);
  1042. util::success(['sedCost' => $cost]);
  1043. }
  1044. //组装订单信息
  1045. $info = [];
  1046. //发送放的商家信息,即供货商的相关信息
  1047. $info['sjId'] = $ghsSjId;
  1048. $info['shopId'] = $ghsShopId;
  1049. $info['actPrice'] = $price;//价格
  1050. $info['weight'] = $weight;
  1051. $info['orderSn'] = \common\components\orderSn::getGhsOrderSn();
  1052. $info['bigNum'] = $bigNum;
  1053. //配送地址,当前零售端的门店地址
  1054. $info['customName'] = $customInfo['name'] ?? '';
  1055. $info['customMobile'] = $customInfo['mobile'] ?? '';
  1056. $info['province'] = $customInfo['province'] ?? '';
  1057. $info['city'] = $customInfo['city'] ?? '';;
  1058. $info['address'] = $customInfo['address'] ?? '';;
  1059. $info['floor'] = $customInfo['floor'] ?? '';;
  1060. $info['fullAddress'] = $customInfo['fullAddress'] ?? '';;
  1061. $info['lat'] = $customInfo['lat'] ?? '';;
  1062. $info['long'] = $customInfo['long'] ?? '';;
  1063. $info['sendTime'] = $sendTime;
  1064. $res = DadaExpressServices::queryDeliverFee($info);
  1065. util::success(['sedCost' => $res['fee']]);
  1066. }
  1067. }