PurchaseController.php 48 KB

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