PurchaseController.php 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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. //新发地联玉花卉市场,满300元市区免费配送
  414. if ($ghsShopId == 21486) {
  415. if ($respond->actPrice < 300) {
  416. $transaction->rollBack();
  417. util::fail('满300元可送货上门,不满请选其他方式');
  418. }
  419. }
  420. //花悠星 国恋 勇记 易宝暂时不支持免费配送
  421. if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778 || $ghsShopId == 12003) {
  422. util::fail('暂不支持送货上门,请选其它方式');
  423. }
  424. }
  425. }
  426. if ($sendType == 2) {
  427. if (in_array($ghsShopId, [520, 1489, 13628, 3, 7855, 7687])) {
  428. util::fail('暂时不能使用跑腿');
  429. }
  430. }
  431. if (getenv('YII_ENV') == 'production') {
  432. //昆明花落谁家
  433. if ($ghsShopId == 13398) {
  434. if ($respond->bigNum < 15) {
  435. $transaction->rollBack();
  436. util::fail('不足15扎,请联系店长');
  437. }
  438. }
  439. }
  440. $transaction->commit();
  441. util::success($respond);
  442. } catch (Exception $e) {
  443. $transaction->rollBack();
  444. util::fail();
  445. }
  446. }
  447. //赊账 ssh 2021.1.24
  448. public function actionDebtPay()
  449. {
  450. $get = Yii::$app->request->get();
  451. $orderSn = $get['orderSn'] ?? 0;
  452. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  453. PurchaseService::valid($info, $this->shopId);
  454. //解决重复和并发提交
  455. $cacheKey = 'hd_debt_pay_' . $orderSn;
  456. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  457. if (!empty($has)) {
  458. util::fail('请稍等...');
  459. }
  460. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  461. if ($info->status == 5) {
  462. util::fail('订单已取消');
  463. }
  464. if ($info->status != 1) {
  465. util::fail('订单不是待付款状态');
  466. }
  467. if ($info->book == 1) {
  468. util::fail('预售不能记欠款');
  469. }
  470. $ghsId = $info->ghsId;
  471. $ghs = GhsClass::getGhsInfo($ghsId);
  472. $customId = $ghs['customId'] ?? 0;
  473. $custom = CustomClass::getCustom($customId);
  474. if (empty($custom)) {
  475. util::fail('没有找到客户');
  476. }
  477. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  478. util::fail('请先申请赊账权限');
  479. }
  480. $connection = Yii::$app->db;//事务处理
  481. $transaction = $connection->beginTransaction();
  482. try {
  483. //延期支付
  484. PurchaseService::debt($info);
  485. $transaction->commit();
  486. if (isset($info->localOrder) && $info->localOrder == 1) {
  487. if ($info->transType != 5 && $info->transType != 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. } else {
  494. if (isset($info->sendType) && $info->sendType == 4) {
  495. //标记当天已收一次包装费,岭南批发市场用的功能
  496. $current = date("Y_m_d");
  497. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  498. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  499. }
  500. }
  501. //不是预订单,并且采购人没有开批发店,供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
  502. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  503. if (!empty($cg)) {
  504. if (isset($cg->book) && $cg->book == 0) {
  505. $ghsShopId = $ghs['shopId'] ?? 0;
  506. if (getenv('YII_ENV') == 'production') {
  507. $specialList = [10649, 8042, 16454, 21319];
  508. } else {
  509. $specialList = [36523];
  510. }
  511. if (!in_array($ghsShopId, $specialList)) {
  512. $cgShopId = $cg->shopId ?? 0;
  513. $cgShop = ShopClass::getById($cgShopId, true);
  514. if (!empty($cgShop)) {
  515. if (isset($cgShop->pfShopId) && $cgShop->pfShopId == 0) {
  516. PurchaseClass::confirmTake($cg);
  517. }
  518. }
  519. }
  520. }
  521. }
  522. //app新订单通知
  523. $saleId = $info->saleId ?? 0;
  524. $order = OrderClass::getById($saleId, true);
  525. if (!empty($order)) {
  526. $shopId = $order->shopId ?? 0;
  527. $shop = ShopClass::getById($shopId, true);
  528. if (!empty($shop)) {
  529. //微信通知供货商
  530. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  531. $noticeText = json_encode(['orderId' => $saleId]);
  532. $noticeKey = "hdCgNoticeGhs";
  533. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  534. }
  535. //订单生成时唤起在线打印
  536. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  537. //有几个地方要同步去修改
  538. if (getenv('YII_ENV') == 'production') {
  539. //源花汇不自动打开小票
  540. $mallOrderNoPrint = [10536];
  541. } else {
  542. $mallOrderNoPrint = [];
  543. }
  544. if (in_array($order->mainId, $mallOrderNoPrint)) {
  545. } else {
  546. OrderClass::onlinePrint($order);
  547. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  548. if (isset($ext->printSn) && !empty($ext->printSn)) {
  549. $order->printNum += 1;
  550. $order->save();
  551. }
  552. }
  553. }
  554. }
  555. } catch (Exception $e) {
  556. $transaction->rollBack();
  557. util::fail('支付失败');
  558. }
  559. util::success($info->attributes);
  560. }
  561. //余额支付 ssh 2021.1.24
  562. public function actionBalancePay()
  563. {
  564. $shopAdmin = $this->shopAdmin;
  565. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  566. util::fail('超管才能操作');
  567. }
  568. $get = Yii::$app->request->get();
  569. $orderSn = $get['orderSn'] ?? 0;
  570. $password = $get['password'] ?? '';
  571. // 重新获取,不加密的用户数据
  572. $admin = AdminClass::getById($this->adminId, true);
  573. if (password_verify($password, $admin->payPassword) == false) {
  574. util::fail('密码错误');
  575. }
  576. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  577. if (empty($info)) {
  578. util::fail('没有找到采购单');
  579. }
  580. if ($info->status == 5) {
  581. util::fail('订单已取消');
  582. }
  583. if ($info->status != 1) {
  584. util::fail('订单不是待付款状态');
  585. }
  586. PurchaseService::valid($info, $this->shopId);
  587. $connection = Yii::$app->db;//事务处理
  588. $transaction = $connection->beginTransaction();
  589. try {
  590. //余额支付
  591. purchaseService::balancePay($info);
  592. $transaction->commit();
  593. } catch (Exception $e) {
  594. $transaction->rollBack();
  595. util::fail('支付失败');
  596. }
  597. util::success($info->attributes);
  598. }
  599. //采购记录 ssh 2021.1.24
  600. public function actionList()
  601. {
  602. $get = Yii::$app->request->get();
  603. $where = ['shopId' => $this->shopId];
  604. $status = $get['status'] ?? 0;
  605. if (!empty($status)) {
  606. $where['status'] = $status;
  607. }
  608. $respond = PurchaseService::getPurchaseList($where);
  609. $respond['shop'] = $this->shop->attributes;
  610. util::success($respond);
  611. }
  612. //采购详情 ssh 2021.01.25
  613. public function actionDetail()
  614. {
  615. $get = Yii::$app->request->get();
  616. $id = $get['id'] ?? 0;
  617. $info = PurchaseService::getInfo($id, true, true);
  618. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  619. //不能注释,分享付款功能要用,客户没有登录也能付款
  620. //util::fail('无法访问');
  621. }
  622. $customId = $info['customId'] ?? 0;
  623. $custom = CustomClass::getById($customId);
  624. $customName = $custom['name'] ?? '';
  625. $customMobile = $custom['mobile'] ?? '';
  626. $info['customName'] = $customName;
  627. $info['customMobile'] = $customMobile;
  628. $hasDebtPay = $custom['debt'] ?? 0;
  629. $book = $info['book'] ?? 0;
  630. $presell = $info['presell'] ?? 0;
  631. //预售不能记欠款
  632. if ($book == 1) {
  633. $hasDebtPay = 0;
  634. }
  635. if ($presell == 1) {
  636. $hasDebtPay = 0;
  637. }
  638. $shop = $this->shop;
  639. $info['pfShopId'] = $shop->pfShopId ?? 0;
  640. $info['hasDebtPay'] = $hasDebtPay;
  641. $info['balance'] = $shop->balance ?? 0;
  642. $getPayType = dict::getDict('getPayType');
  643. $info['getPayType'] = $getPayType;
  644. //获取售后条件
  645. $ghsId = $info['ghsId'] ?? 0;
  646. $ghsInfo = GhsClass::getById($ghsId, true);
  647. if (empty($ghsInfo)) {
  648. util::fail('没有找到供货商');
  649. }
  650. $info['afterSale'] = $ghsInfo->afterSale ?? 1;
  651. //重要,客户看到的电话修改成门店的联系电话
  652. $ghsShopId = $ghsInfo->shopId ?? 0;
  653. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  654. $info['ghsMobile'] = $ghsShopInfo->telephone ?? '';
  655. util::success($info);
  656. }
  657. //可用的支付方式 ssh 2021.1.25
  658. public function actionPayWay()
  659. {
  660. $button = [
  661. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  662. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  663. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  664. ];
  665. util::success(['button' => $button]);
  666. }
  667. //待结款明细 ssh 2021.1.26
  668. public function actionDebtList()
  669. {
  670. $get = Yii::$app->request->get();
  671. $id = $get['id'] ?? 0;
  672. $info = GhsClass::getGhsInfo($id);
  673. GhsClass::valid($info, $this->shopId);
  674. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  675. $searchTime = $get['searchTime'] ?? '';
  676. if (!empty($searchTime)) {
  677. $startTime = $get['startTime'] ?? '';
  678. $endTime = $get['endTime'] ?? '';
  679. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  680. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  681. }
  682. $respond = PurchaseService::getDebtList($where);
  683. util::success($respond);
  684. }
  685. //微信支付 ssh 2021.4.11
  686. public function actionAliPay()
  687. {
  688. ini_set('date.timezone', 'Asia/Shanghai');
  689. $post = Yii::$app->request->post();
  690. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  691. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  692. if (empty($order)) {
  693. util::fail('订单号无效');
  694. }
  695. if ($order->status == 5) {
  696. util::fail('订单已取消');
  697. }
  698. if ($order->status != 1) {
  699. util::fail('订单不是待付款状态');
  700. }
  701. $deadline = $order->deadline;
  702. $current = time();
  703. if (($current + 50) > strtotime($deadline)) {
  704. util::fail('订单已失效,请重新下单');
  705. }
  706. $ghsId = $order->ghsId ?? 0;
  707. $ghs = GhsClass::getById($ghsId, true);
  708. if (empty($ghs)) {
  709. util::fail('没有供货商信息');
  710. }
  711. $ghsShopId = $ghs->shopId ?? 0;
  712. $ghsShop = ShopClass::getById($ghsShopId, true);
  713. if (empty($ghsShop)) {
  714. util::fail('没有供货商门店信息');
  715. }
  716. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  717. util::fail('商家支付功能未开通');
  718. }
  719. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  720. if ($order->changePrice == 2) {
  721. $oldOrderSn = $orderSn;
  722. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  723. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  724. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  725. $params = [
  726. 'appid' => 'OP00002119',
  727. 'serial_no' => '018b08cfddbd',
  728. 'merchant_no' => $ghsShop->lklSjNo,
  729. 'term_no' => $ghsShop->lklScanTermNo,
  730. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  731. 'lklCertificatePath' => $lklCertificatePath,
  732. ];
  733. $laResource = new Lakala($params);
  734. $closeParams = [
  735. 'orderSn' => $oldOrderSn,
  736. ];
  737. $response = $laResource->cashClose($closeParams);
  738. if (isset($response['code']) == false || $response['code'] != '000000') {
  739. $msg = $response['msg'] ?? '';
  740. noticeUtil::push('注意花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功(收银台-支付宝),' . $msg, '15280215347');
  741. }
  742. $newOrderSn = orderSn::getPurchaseSn();
  743. $newGhsOrderSn = orderSn::getGhsOrderSn();
  744. $orderSn = $newOrderSn;
  745. $order->orderSn = $newOrderSn;
  746. $order->save();
  747. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  748. $saleId = $order->saleId ?? 0;
  749. $sale = OrderClass::getById($saleId, true);
  750. if (!empty($sale)) {
  751. $oldSaleOrderSn = $sale->orderSn ?? '';
  752. $sale->orderSn = $newGhsOrderSn;
  753. $sale->save();
  754. OrderItemClass::updateByCondition(['orderSn' => $oldSaleOrderSn], ['orderSn' => $newGhsOrderSn]);
  755. }
  756. }
  757. $order->changePrice = 2;
  758. $order->save();
  759. $name = $orderSn;
  760. $totalFee = $order->realPrice;
  761. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  762. $ghsId = $order->ghsId ?? 0;
  763. $ghs = GhsClass::getById($ghsId, true);
  764. if (empty($ghs)) {
  765. util::fail('没有供货商信息');
  766. }
  767. $ghsShopId = $ghs->shopId ?? 0;
  768. $ghsShop = ShopClass::getById($ghsShopId, true);
  769. if (empty($ghsShop)) {
  770. util::fail('没有供货商门店信息');
  771. }
  772. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  773. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  774. $params = [
  775. 'appid' => 'OP00002119',
  776. 'serial_no' => '018b08cfddbd',
  777. 'merchant_no' => $ghsShop->lklSjNo,
  778. 'term_no' => $ghsShop->lklB2BTermNo,
  779. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  780. 'lklCertificatePath' => $lklCertificatePath,
  781. ];
  782. $laResource = new Lakala($params);
  783. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  784. $wxParams = [
  785. 'orderSn' => $orderSn,
  786. 'amount' => $totalFee,
  787. 'capitalType' => $purchaseCapital,
  788. 'notifyUrl' => $notifyUrl,
  789. 'subject' => $name,
  790. ];
  791. $response = $laResource->cashierPay($wxParams);
  792. if (isset($response['code']) == false || $response['code'] != '000000') {
  793. $errMsg = $response['msg'] ?? '';
  794. noticeUtil::push('编号AA90:' . $errMsg, '15280215347');
  795. util::fail('发起支付失败');
  796. }
  797. $counter_url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  798. $new = [
  799. 'payUrl' => $counter_url,
  800. 'orderSn' => $orderSn,
  801. ];
  802. util::success($new);
  803. }
  804. //微信支付 ssh 2021.4.11
  805. public function actionWxPay()
  806. {
  807. ini_set('date.timezone', 'Asia/Shanghai');
  808. $post = Yii::$app->request->post();
  809. $couponId = $post['couponId'] ?? 0;
  810. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  811. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  812. if (empty($order)) {
  813. util::fail('订单号无效');
  814. }
  815. if ($order->status == 5) {
  816. util::fail('订单已取消');
  817. }
  818. if ($order->status != 1) {
  819. util::fail('订单不是待付款状态');
  820. }
  821. $deadline = $order->deadline;
  822. $current = time();
  823. if (($current + 50) > strtotime($deadline)) {
  824. util::fail('订单已失效,请重新下单');
  825. }
  826. $ghsId = $order->ghsId ?? 0;
  827. $ghs = GhsClass::getById($ghsId, true);
  828. if (empty($ghs)) {
  829. util::fail('没有供货商信息');
  830. }
  831. $ghsShopId = $ghs->shopId ?? 0;
  832. $ghsShop = ShopClass::getById($ghsShopId, true);
  833. if (empty($ghsShop)) {
  834. util::fail('没有供货商门店信息');
  835. }
  836. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  837. util::fail('商家支付功能未开通');
  838. }
  839. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  840. if ($order->changePrice == 2) {
  841. $oldOrderSn = $orderSn;
  842. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  843. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  844. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  845. $params = [
  846. 'appid' => 'OP00002119',
  847. 'serial_no' => '018b08cfddbd',
  848. 'merchant_no' => $ghsShop->lklSjNo,
  849. 'term_no' => $ghsShop->lklScanTermNo,
  850. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  851. 'lklCertificatePath' => $lklCertificatePath,
  852. ];
  853. $laResource = new Lakala($params);
  854. $closeParams = [
  855. 'orderSn' => $oldOrderSn,
  856. ];
  857. $response = $laResource->close($closeParams);
  858. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  859. $msg = $response['msg'] ?? '';
  860. noticeUtil::push('重点注意,花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg, '15280215347');
  861. util::fail('出错了,请重新下一单');
  862. }
  863. $newOrderSn = orderSn::getPurchaseSn();
  864. $newGhsOrderSn = orderSn::getGhsOrderSn();
  865. $orderSn = $newOrderSn;
  866. $order->orderSn = $newOrderSn;
  867. $order->save();
  868. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  869. $saleId = $order->saleId ?? 0;
  870. $sale = OrderClass::getById($saleId, true);
  871. if (!empty($sale)) {
  872. $oldSaleOrderSn = $sale->orderSn ?? '';
  873. $sale->orderSn = $newGhsOrderSn;
  874. $sale->save();
  875. OrderItemClass::updateByCondition(['orderSn' => $oldSaleOrderSn], ['orderSn' => $newGhsOrderSn]);
  876. }
  877. }
  878. $id = $order->id;
  879. $order->changePrice = 2;
  880. $order->save();
  881. $name = $orderSn;
  882. $totalFee = $order->realPrice;
  883. $openId = '';
  884. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  885. $openId = $post['currentMiniOpenId'];
  886. //noticeUtil::push('花店采购下单时,通过前端获取了openId:' . $openId, '15280215347');
  887. }
  888. if (empty($openId)) {
  889. if (isset($this->admin) && !empty($this->admin)) {
  890. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  891. $openId = $this->admin->miniOpenId;
  892. noticeUtil::push('花店采购下单时,通过数据库获取了openId:' . $openId, '15280215347');
  893. }
  894. }
  895. }
  896. if (empty($openId)) {
  897. $currentShopId = $order->shopId ?? 0;
  898. $currentShop = ShopClass::getById($currentShopId, true);
  899. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  900. $currentMobile = $currentShop->mobile ?? '';
  901. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  902. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  903. }
  904. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  905. //花卉宝代为申请的微信支付
  906. $merchantExtend = WxOpenClass::getWxInfo();
  907. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  908. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  909. $params = [
  910. 'appid' => 'OP00002119',
  911. 'serial_no' => '018b08cfddbd',
  912. 'merchant_no' => $ghsShop->lklSjNo,
  913. 'term_no' => $ghsShop->lklScanTermNo,
  914. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  915. 'lklCertificatePath' => $lklCertificatePath,
  916. ];
  917. $laResource = new Lakala($params);
  918. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  919. $wxParams = [
  920. 'orderSn' => $orderSn,
  921. 'amount' => $totalFee,
  922. 'capitalType' => $purchaseCapital,
  923. 'notifyUrl' => $notifyUrl,
  924. 'wxAppId' => $merchantExtend['miniAppId'],
  925. 'openId' => $openId,
  926. 'subject' => $name,
  927. 'couponId' => $couponId,
  928. ];
  929. $response = $laResource->driveWxPay($wxParams);
  930. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  931. $errMsg = $response['msg'] ?? '';
  932. noticeUtil::push('编号99891:' . $errMsg, '15280215347');
  933. util::fail('支付失败');
  934. }
  935. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  936. $appId = $newParams['app_id'] ?? '';
  937. $nonceStr = $newParams['nonce_str'] ?? '';
  938. $paySign = $newParams['pay_sign'] ?? '';
  939. $package = $newParams['package'] ?? '';
  940. $signType = $newParams['sign_type'] ?? '';
  941. $timeStamp = $newParams['time_stamp'] ?? '';
  942. $new = [
  943. 'id' => $id,
  944. 'appId' => $appId,
  945. 'nonceStr' => $nonceStr,
  946. 'paySign' => $paySign,
  947. 'package' => $package,
  948. 'signType' => $signType,
  949. 'timeStamp' => $timeStamp,
  950. 'orderSn' => $orderSn,
  951. ];
  952. util::success($new);
  953. }
  954. //计算运费 ssh 20221003
  955. public function actionFreight()
  956. {
  957. $get = Yii::$app->request->get();
  958. $shop = $this->shop;
  959. $shopLat = isset($shop->lat) ? $shop->lat : '';
  960. $shopLong = isset($shop->long) ? $shop->long : '';
  961. if (empty($shopLat) || empty($shopLong)) {
  962. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  963. }
  964. $ghsId = $get['ghsId'] ?? 0;
  965. $ghs = GhsClass::getById($ghsId, true);
  966. if (empty($ghs)) {
  967. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  968. }
  969. $ghsShopId = $ghs->shopId ?? 0;
  970. $ghsShop = ShopClass::getById($ghsShopId, true);
  971. if (empty($ghsShop)) {
  972. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  973. }
  974. $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : '';
  975. $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : '';
  976. if (empty($ghsShopLat) || empty($ghsShopLong)) {
  977. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  978. }
  979. $weight = $get['weight'] ?? 0;
  980. if (empty($weight)) {
  981. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  982. }
  983. $respond = PurchaseClass::getDistanceFee($this->shop, $ghsShop, $weight);
  984. util::success($respond);
  985. }
  986. //运费计算 lqh 2021.4.12 暂时返回固定值
  987. public function actionFreight2()
  988. {
  989. //lqh 2021.6.25 运费固定返回0
  990. util::success(['sedCost' => 0]);
  991. $post = Yii::$app->request->post();
  992. $ghsId = $post['ghsId'] ?? 0;
  993. //供货商
  994. $ghsInfo = GhsClass::getById($ghsId);
  995. if (empty($ghsInfo)) {
  996. util::fail('没有找到供货商');
  997. }
  998. //花材信息
  999. $productJson = $post['product'] ?? '';
  1000. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  1001. if (empty($productJson)) {
  1002. util::fail('请选择花材');
  1003. }
  1004. $productList = json_decode($productJson, true);
  1005. if (empty($productList)) {
  1006. util::fail('请选择花材');
  1007. }
  1008. $productList = PurchaseClass::mergeItemInfo($productList);
  1009. $weight = 0;
  1010. $price = 0;
  1011. $productData = ProductClass::getProductMapData($productList);
  1012. $bigNum = 0;
  1013. foreach ($productList as $key => $val) {
  1014. $productId = $val['productId'];
  1015. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  1016. $bigNum += $val['bigNum'];
  1017. $weight = bcadd($w, $weight, 2);
  1018. $ratio = $productData[$productId]['ratio'] ?? 0;
  1019. //大小数量合并多少扎
  1020. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  1021. //售卖价格
  1022. $itemPrice = $productData[$productId]['price'] ?? 0;
  1023. //合计价格
  1024. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  1025. }
  1026. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  1027. $ghsSjId = $ghsInfo['sjId'] ?? 0;
  1028. $sendTime = $post['sendTime'] ?? '';
  1029. if (!empty($sendTime)) {
  1030. //配送时间不为空
  1031. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  1032. $sendTimeInt = strtotime($sendTime);
  1033. $now = time();
  1034. $diff = $sendTimeInt - $now;
  1035. //因服务器写入时间,改为至少提前6分钟
  1036. if ($diff <= 360) {
  1037. util::fail('配送时间至少提前6分钟');
  1038. }
  1039. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  1040. } else {
  1041. $sendTime = date('Y-m-d H:i', strtotime("+30 minute"));
  1042. }
  1043. //找供货商的客户信息,用里面的配送相关地址信息
  1044. $customId = $ghsInfo['customId'] ?? 0;
  1045. $customInfo = CustomClass::getById($customId);
  1046. if (empty($customInfo)) {
  1047. util::fail('没有找到客户');
  1048. }
  1049. //判断商家是否开启达达,若未开启,则使用自定义运费计算
  1050. $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
  1051. if (!$shopNo) {
  1052. //使用自定义运费
  1053. //util::fail('供货商暂未开通配送');
  1054. $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight);
  1055. util::success(['sedCost' => $cost]);
  1056. }
  1057. //组装订单信息
  1058. $info = [];
  1059. //发送放的商家信息,即供货商的相关信息
  1060. $info['sjId'] = $ghsSjId;
  1061. $info['shopId'] = $ghsShopId;
  1062. $info['actPrice'] = $price;//价格
  1063. $info['weight'] = $weight;
  1064. $info['orderSn'] = \common\components\orderSn::getGhsOrderSn();
  1065. $info['bigNum'] = $bigNum;
  1066. //配送地址,当前零售端的门店地址
  1067. $info['customName'] = $customInfo['name'] ?? '';
  1068. $info['customMobile'] = $customInfo['mobile'] ?? '';
  1069. $info['province'] = $customInfo['province'] ?? '';
  1070. $info['city'] = $customInfo['city'] ?? '';;
  1071. $info['address'] = $customInfo['address'] ?? '';;
  1072. $info['floor'] = $customInfo['floor'] ?? '';;
  1073. $info['fullAddress'] = $customInfo['fullAddress'] ?? '';;
  1074. $info['lat'] = $customInfo['lat'] ?? '';;
  1075. $info['long'] = $customInfo['long'] ?? '';;
  1076. $info['sendTime'] = $sendTime;
  1077. $res = DadaExpressServices::queryDeliverFee($info);
  1078. util::success(['sedCost' => $res['fee']]);
  1079. }
  1080. }