PurchaseController.php 48 KB

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