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