PurchaseController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 bizGhs\custom\classes\CustomClass;
  9. use bizGhs\express\services\DadaExpressServices;
  10. use bizGhs\order\classes\OrderClass;
  11. use bizHd\purchase\classes\PurchaseClass;
  12. use bizHd\purchase\services\PurchaseService;
  13. use bizGhs\product\classes\ProductClass;
  14. use bizHd\wx\classes\WxOpenClass;
  15. use common\components\dateUtil;
  16. use common\components\dict;
  17. use common\components\freight;
  18. use common\components\httpUtil;
  19. use common\components\noticeUtil;
  20. use Yii;
  21. use common\components\util;
  22. use biz\wx\classes\WxMessageClass;
  23. class PurchaseController extends BaseController
  24. {
  25. public $guestAccess = ['detail'];
  26. //虚拟供货商的采购 ssh 20230308
  27. public function actionAddVirtualCg()
  28. {
  29. $post = Yii::$app->request->post();
  30. $ghsId = $post['ghsId'] ?? 0;
  31. $ghsInfo = GhsClass::getById($ghsId);
  32. if (empty($ghsInfo)) {
  33. util::fail('没有找到供货商');
  34. }
  35. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  36. util::fail('这个是你自己的批发店');
  37. }
  38. $connection = Yii::$app->db;//事务处理
  39. $transaction = $connection->beginTransaction();
  40. try {
  41. $post['book'] = $post['book'] ?? 0;
  42. $post['sjId'] = $this->sjId;
  43. $post['shopId'] = $this->shopId;
  44. $post['shopAdminId'] = $this->shopAdminId;
  45. $post['ghsId'] = $ghsId;
  46. $post['ghsName'] = $ghsInfo['name'] ?? '';
  47. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  48. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  49. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  50. $customId = $ghsInfo['customId'] ?? 0;
  51. $post['customId'] = $customId;
  52. $shopAdmin = $this->shopAdmin ?? null;
  53. $name = $shopAdmin->name ?? '';
  54. $post['shopAdminName'] = $name;
  55. $post['mainId'] = $this->mainId;
  56. $product = $post['product'] ?? '';
  57. $productList = json_decode($product, true);
  58. //判断product数据是不是同个供货商的
  59. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  60. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  61. if (!empty($productInfoList)) {
  62. foreach ($productInfoList as $currentInfo) {
  63. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $this->mainId) {
  64. util::fail('只能选择同一家的商品哦');
  65. }
  66. }
  67. if (count($productInfoList) != count($ids)) {
  68. util::fail('存在无效商品');
  69. }
  70. } else {
  71. util::fail('花材不存在');
  72. }
  73. //供货商预订单最低公斤数要求和每公斤服务费
  74. $ghsInfo['kiloFee'] = 0;
  75. $ghsInfo['miniKilo'] = 0;
  76. $post['product'] = $productList;
  77. $sendCost = 0;
  78. $post['sendCost'] = $sendCost;
  79. $packCost = 0;
  80. $post['packCost'] = $packCost;
  81. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  82. $transaction->commit();
  83. util::success($respond);
  84. } catch (Exception $e) {
  85. $transaction->rollBack();
  86. util::fail();
  87. }
  88. }
  89. //生成采购单 ssh 2021.1.17
  90. public function actionCreateOrder()
  91. {
  92. $post = Yii::$app->request->post();
  93. //多颜色花材数据结转换
  94. $colorItem = $post['xj'] ?? [];
  95. $newXj = [];
  96. if (!empty($colorItem)) {
  97. $colorData = json_decode($colorItem, true);
  98. if (!empty($colorData) && is_array($colorData)) {
  99. foreach ($colorData as $colorList) {
  100. if (!empty($colorList)) {
  101. foreach ($colorList as $color) {
  102. $ptItemId = $color['ptItemId'];
  103. $newXj[$ptItemId][] = $color;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. $post['xj'] = $newXj;
  110. $ghsId = $post['ghsId'] ?? 0;
  111. //供货商
  112. $ghsInfo = GhsClass::getById($ghsId);
  113. if (empty($ghsInfo)) {
  114. util::fail('没有找到供货商');
  115. }
  116. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  117. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  118. util::fail('这个是你自己的批发店');
  119. }
  120. $connection = Yii::$app->db;//事务处理
  121. $transaction = $connection->beginTransaction();
  122. try {
  123. $post['book'] = $post['book'] ?? 0;
  124. $post['sjId'] = $this->sjId;
  125. $post['shopId'] = $this->shopId;
  126. $post['shopAdminId'] = $this->shopAdminId;
  127. $post['ghsId'] = $ghsId;
  128. $post['ghsName'] = $ghsInfo['name'] ?? '';
  129. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  130. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  131. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  132. $customId = $ghsInfo['customId'] ?? 0;
  133. $post['customId'] = $customId;
  134. $shopAdmin = $this->shopAdmin ?? null;
  135. $name = $shopAdmin->name ?? '';
  136. $post['shopAdminName'] = $name;
  137. $post['mainId'] = $this->mainId;
  138. $product = $post['product'] ?? '';
  139. $productList = json_decode($product, true);
  140. //判断product数据是不是同个供货商的
  141. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  142. $ghsMainId = $ghsShopInfo->mainId ?? 0;
  143. $isOpen = ShopClass::isOpen($ghsShopInfo);
  144. $book = $post['book'] ?? 0;
  145. if ($isOpen == 0 && $book == 0) {
  146. util::fail('本店已休息,请选择其它门店');
  147. }
  148. if ($book == 1) {
  149. $sendTimeWant = isset($post['sendTimeWant']) && !empty($post['sendTimeWant']) ? $post['sendTimeWant'] : date("Y-m-d");
  150. $future = date("Y-m-d", strtotime('+2 day'));
  151. if (strtotime($sendTimeWant) < strtotime($future)) {
  152. util::fail('请选择未来二天或更后时间');
  153. }
  154. $nowTime = strtotime(date("Y-m-d"));
  155. if (strtotime($sendTimeWant) < $nowTime) {
  156. util::fail('不能选择过去时间');
  157. }
  158. }
  159. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  160. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  161. if (!empty($productInfoList)) {
  162. $presellData = [];
  163. $nowTime = strtotime(date("Y-m-d"));
  164. foreach ($productInfoList as $currentInfo) {
  165. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $ghsMainId) {
  166. util::fail('只能选择同一家的商品哦');
  167. }
  168. $currentName = $currentInfo['name'] ?? '';
  169. $presell = $currentInfo['presell'] ?? 0;
  170. $presellData[] = $presell;
  171. if ($presell == 1) {
  172. if ($book == 1) {
  173. util::fail('预订时不能选择预售花材');
  174. } else {
  175. if (isset($post['sendTimeWant']) == false || empty($post['sendTimeWant'])) {
  176. util::fail('请选择配送日期');
  177. }
  178. $sendTimeWant = $post['sendTimeWant'];
  179. if (strtotime($sendTimeWant) < $nowTime) {
  180. util::fail('不能选择过去时间');
  181. }
  182. $hasDateStr = $currentInfo['presellDate'] ?? [];
  183. $hasDate = explode(',', trim($hasDateStr));
  184. if (empty($hasDate)) {
  185. util::fail('预售日期有问题,请提醒门店');
  186. }
  187. if (in_array(strtotime($sendTimeWant), $hasDate) == false) {
  188. util::fail("有预售花材在{$sendTimeWant}没到货");
  189. }
  190. $post['presell'] = 1;
  191. }
  192. }
  193. }
  194. if (count($productInfoList) != count($ids)) {
  195. util::fail('存在无效商品');
  196. }
  197. $presellData = array_unique($presellData);
  198. if (count($presellData) > 1) {
  199. util::fail('预售和非预售花材不能一起下单');
  200. }
  201. } else {
  202. util::fail('花材不存在');
  203. }
  204. //供货商预订单最低公斤数要求和每公斤服务费
  205. $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0;
  206. $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0;
  207. $post['product'] = $productList;
  208. $sendType = $post['sendType'] ?? 0;
  209. $sendCost = 0;
  210. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  211. $weight = 0;
  212. foreach ($productList as $itemData) {
  213. $currentWeight = $itemData['weight'] ?? 0;
  214. $weight = bcadd($currentWeight, $weight, 2);
  215. }
  216. $result = PurchaseClass::getDistanceFee($this->shop, $ghsShopInfo, $weight);
  217. $sendCost = $result['fee'] ?? 0;
  218. }
  219. $post['sendCost'] = $sendCost;
  220. //出车和发快递可以算打包费
  221. $packCost = 0;
  222. if ($sendType == dict::getDict('sendType', 'thirdSend') || $sendType == dict::getDict('sendType', 'carGet')) {
  223. $packCost = $ghsShopInfo->packCost ?? 0;
  224. }
  225. $post['packCost'] = $packCost;
  226. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  227. $transaction->commit();
  228. util::success($respond);
  229. } catch (Exception $e) {
  230. $transaction->rollBack();
  231. util::fail();
  232. }
  233. }
  234. //延期支付 ssh 2021.1.24
  235. public function actionDebtPay()
  236. {
  237. $get = Yii::$app->request->get();
  238. $orderSn = $get['orderSn'] ?? 0;
  239. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  240. PurchaseService::valid($info, $this->shopId);
  241. if ($info->status == 5) {
  242. util::fail('订单已取消');
  243. }
  244. if ($info->status != 1) {
  245. util::fail('订单不是待付款状态');
  246. }
  247. if ($info->book == 1) {
  248. util::fail('预售不能记欠款');
  249. }
  250. $ghsId = $info->ghsId;
  251. $ghs = GhsClass::getGhsInfo($ghsId);
  252. $customId = $ghs['customId'] ?? 0;
  253. $custom = CustomClass::getCustom($customId);
  254. if (empty($custom)) {
  255. util::fail('没有找到客户');
  256. }
  257. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  258. util::fail('您没有延期付款权限');
  259. }
  260. $connection = Yii::$app->db;//事务处理
  261. $transaction = $connection->beginTransaction();
  262. try {
  263. //延期支付
  264. PurchaseService::debt($info);
  265. $transaction->commit();
  266. //app新订单通知
  267. $saleId = $info->saleId ?? 0;
  268. $order = OrderClass::getById($saleId, true);
  269. if (!empty($order)) {
  270. $shopId = $order->shopId ?? 0;
  271. $shop = ShopClass::getById($shopId, true);
  272. if (!empty($shop)) {
  273. WxMessageClass::ghsHasNewOrderInform($shop, $order);
  274. }
  275. //订单生成时唤起在线打印
  276. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  277. OrderClass::onlinePrint($order);
  278. $order->printNum += 1;
  279. $order->save();
  280. }
  281. //花店采购供货商端收到通知
  282. NoticeClass::ghsNewOrderNotice($order);
  283. }
  284. } catch (Exception $e) {
  285. $transaction->rollBack();
  286. util::fail('支付失败');
  287. }
  288. util::success($info->attributes);
  289. }
  290. //余额支付 ssh 2021.1.24
  291. public function actionBalancePay()
  292. {
  293. $shopAdmin = $this->shopAdmin;
  294. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  295. util::fail('超管才能操作');
  296. }
  297. $get = Yii::$app->request->get();
  298. $orderSn = $get['orderSn'] ?? 0;
  299. $password = $get['password'] ?? '';
  300. // 重新获取,不加密的用户数据
  301. $admin = AdminClass::getById($this->adminId, true);
  302. if (password_verify($password, $admin->payPassword) == false) {
  303. util::fail('密码错误');
  304. }
  305. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  306. if (empty($info)) {
  307. util::fail('没有找到采购单');
  308. }
  309. if ($info->status == 5) {
  310. util::fail('订单已取消');
  311. }
  312. if ($info->status != 1) {
  313. util::fail('订单不是待付款状态');
  314. }
  315. PurchaseService::valid($info, $this->shopId);
  316. $connection = Yii::$app->db;//事务处理
  317. $transaction = $connection->beginTransaction();
  318. try {
  319. //余额支付
  320. purchaseService::balancePay($info);
  321. $transaction->commit();
  322. } catch (Exception $e) {
  323. $transaction->rollBack();
  324. util::fail('支付失败');
  325. }
  326. util::success($info->attributes);
  327. }
  328. //采购记录 ssh 2021.1.24
  329. public function actionList()
  330. {
  331. $get = Yii::$app->request->get();
  332. $where = ['shopId' => $this->shopId];
  333. $status = $get['status'] ?? 0;
  334. if (!empty($status)) {
  335. $where['status'] = $status;
  336. }
  337. $respond = PurchaseService::getPurchaseList($where);
  338. $respond['shop'] = $this->shop->attributes;
  339. util::success($respond);
  340. }
  341. //采购详情 ssh 2021.01.25
  342. public function actionDetail()
  343. {
  344. $get = Yii::$app->request->get();
  345. $id = $get['id'] ?? 0;
  346. $info = PurchaseService::getInfo($id, true, true);
  347. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  348. //不能注释,好像分享付款功能要用
  349. //util::fail('没有权限访问');
  350. }
  351. $customId = $info['customId'] ?? 0;
  352. $custom = CustomClass::getById($customId);
  353. $hasDebtPay = $custom['debt'] ?? 0;
  354. $book = $info['book'] ?? 0;
  355. $presell = $info['presell'] ?? 0;
  356. //预售不能记欠款
  357. if ($book == 1) {
  358. $hasDebtPay = 0;
  359. }
  360. if ($presell == 1) {
  361. $hasDebtPay = 0;
  362. }
  363. $info['hasDebtPay'] = $hasDebtPay;
  364. $info['balance'] = $this->shop->balance ?? 0;
  365. util::success($info);
  366. }
  367. //可用的支付方式 ssh 2021.1.25
  368. public function actionPayWay()
  369. {
  370. $button = [
  371. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  372. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  373. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  374. ];
  375. util::success(['button' => $button]);
  376. }
  377. //待结款明细 ssh 2021.1.26
  378. public function actionDebtList()
  379. {
  380. $get = Yii::$app->request->get();
  381. $id = $get['id'] ?? 0;
  382. $info = GhsClass::getGhsInfo($id);
  383. GhsClass::valid($info, $this->shopId);
  384. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  385. $searchTime = $get['searchTime'] ?? '';
  386. if (!empty($searchTime)) {
  387. $startTime = $get['startTime'] ?? '';
  388. $endTime = $get['endTime'] ?? '';
  389. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  390. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  391. }
  392. $respond = PurchaseService::getDebtList($where);
  393. util::success($respond);
  394. }
  395. //微信支付 ssh 2021.4.11
  396. public function actionWxPay()
  397. {
  398. ini_set('date.timezone', 'Asia/Shanghai');
  399. $post = Yii::$app->request->post();
  400. $couponId = $post['couponId'] ?? 0;
  401. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  402. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  403. if (empty($order)) {
  404. util::fail('订单号无效');
  405. }
  406. if ($order->status == 5) {
  407. util::fail('订单已取消');
  408. }
  409. if ($order->status != 1) {
  410. util::fail('订单不是待付款状态');
  411. }
  412. $id = $order->id;
  413. $order->changePrice = 2;
  414. $order->save();
  415. $deadline = $order->deadline;
  416. $current = time();
  417. if (($current + 50) > strtotime($deadline)) {
  418. util::fail('订单已失效,请重新下单');
  419. }
  420. $name = $orderSn;
  421. $totalFee = $order->realPrice;
  422. //强制使用小程序的miniOpenId
  423. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  424. if (empty($openId)) {
  425. $currentShopId = $order->shopId ?? 0;
  426. $currentShop = ShopClass::getById($currentShopId, true);
  427. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  428. $currentMobile = $currentShop->mobile ?? '';
  429. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  430. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  431. }
  432. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  433. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  434. $wxPayType = 0;
  435. if (httpUtil::isMiniProgram()) {
  436. $wxPayType = 1;
  437. }
  438. $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  439. //订单30分钟后过期
  440. $now = time();
  441. $expireTime = $now + 1800;
  442. $wx = Yii::getAlias("@vendor/weixin");
  443. require_once($wx . '/lib/WxPay.Api.php');
  444. require_once($wx . '/example/WxPay.JsApiPay.php');
  445. $input = new \WxPayUnifiedOrder();
  446. $input->SetBody($name);
  447. $input->SetOut_trade_no($orderSn);
  448. $input->SetTotal_fee($totalFee * 100);
  449. $input->SetTime_start(date("YmdHis", $now));
  450. //将流水类型、代金劵等传给微信再回传
  451. $input->SetAttach($attach);
  452. $input->SetTime_expire(date("YmdHis", $expireTime));
  453. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  454. $input->SetTrade_type("JSAPI");
  455. //花卉宝代为申请的微信支付
  456. $merchantExtend = WxOpenClass::getWxInfo();
  457. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  458. $input->SetSub_openid($openId);
  459. } else {
  460. $input->SetOpenid($openId);
  461. }
  462. //强制使用小程序的miniAppId
  463. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  464. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  465. $tools = new \JsApiPay();
  466. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  467. $newParams = json_decode($jsApiParameters, true);
  468. $newParams['id'] = $id;
  469. util::success($newParams);
  470. }
  471. //计算运费 ssh 20221003
  472. public function actionFreight()
  473. {
  474. $get = Yii::$app->request->get();
  475. $shop = $this->shop;
  476. $shopLat = isset($shop->lat) ? $shop->lat : '';
  477. $shopLong = isset($shop->long) ? $shop->long : '';
  478. if (empty($shopLat) || empty($shopLong)) {
  479. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  480. }
  481. $ghsId = $get['ghsId'] ?? 0;
  482. $ghs = GhsClass::getById($ghsId, true);
  483. if (empty($ghs)) {
  484. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  485. }
  486. $ghsShopId = $ghs->shopId ?? 0;
  487. $ghsShop = ShopClass::getById($ghsShopId, true);
  488. if (empty($ghsShop)) {
  489. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  490. }
  491. $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : '';
  492. $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : '';
  493. if (empty($ghsShopLat) || empty($ghsShopLong)) {
  494. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  495. }
  496. $weight = $get['weight'] ?? 0;
  497. if (empty($weight)) {
  498. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  499. }
  500. $respond = PurchaseClass::getDistanceFee($this->shop, $ghsShop, $weight);
  501. util::success($respond);
  502. }
  503. //运费计算 lqh 2021.4.12 暂时返回固定值
  504. public function actionFreight2()
  505. {
  506. //lqh 2021.6.25 运费固定返回0
  507. util::success(['sedCost' => 0]);
  508. $post = Yii::$app->request->post();
  509. $ghsId = $post['ghsId'] ?? 0;
  510. //供货商
  511. $ghsInfo = GhsClass::getById($ghsId);
  512. if (empty($ghsInfo)) {
  513. util::fail('没有找到供货商');
  514. }
  515. //花材信息
  516. $productJson = $post['product'] ?? '';
  517. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  518. if (empty($productJson)) {
  519. util::fail('请选择花材');
  520. }
  521. $productList = json_decode($productJson, true);
  522. if (empty($productList)) {
  523. util::fail('请选择花材');
  524. }
  525. $productList = PurchaseClass::mergeItemInfo($productList);
  526. $weight = 0;
  527. $price = 0;
  528. $productData = ProductClass::getProductMapData($productList);
  529. $bigNum = 0;
  530. foreach ($productList as $key => $val) {
  531. $productId = $val['productId'];
  532. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  533. $bigNum += $val['bigNum'];
  534. $weight = bcadd($w, $weight, 2);
  535. $ratio = $productData[$productId]['ratio'] ?? 0;
  536. //大小数量合并多少扎
  537. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  538. //售卖价格
  539. $itemPrice = $productData[$productId]['price'] ?? 0;
  540. //合计价格
  541. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  542. }
  543. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  544. $ghsSjId = $ghsInfo['sjId'] ?? 0;
  545. $sendTime = $post['sendTime'] ?? '';
  546. if (!empty($sendTime)) {
  547. //配送时间不为空
  548. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  549. $sendTimeInt = strtotime($sendTime);
  550. $now = time();
  551. $diff = $sendTimeInt - $now;
  552. //因服务器写入时间,改为至少提前6分钟
  553. if ($diff <= 360) {
  554. util::fail('配送时间至少提前6分钟');
  555. }
  556. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  557. } else {
  558. $sendTime = date('Y-m-d H:i', strtotime("+30 minute"));
  559. }
  560. //找供货商的客户信息,用里面的配送相关地址信息
  561. $customId = $ghsInfo['customId'] ?? 0;
  562. $customInfo = CustomClass::getById($customId);
  563. if (empty($customInfo)) {
  564. util::fail('没有找到客户');
  565. }
  566. //判断商家是否开启达达,若未开启,则使用自定义运费计算
  567. $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
  568. if (!$shopNo) {
  569. //使用自定义运费
  570. //util::fail('供货商暂未开通配送');
  571. $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight);
  572. util::success(['sedCost' => $cost]);
  573. }
  574. //组装订单信息
  575. $info = [];
  576. //发送放的商家信息,即供货商的相关信息
  577. $info['sjId'] = $ghsSjId;
  578. $info['shopId'] = $ghsShopId;
  579. $info['actPrice'] = $price;//价格
  580. $info['weight'] = $weight;
  581. $info['orderSn'] = \common\components\orderSn::getGhsOrderSn();
  582. $info['bigNum'] = $bigNum;
  583. //配送地址,当前零售端的门店地址
  584. $info['customName'] = $customInfo['name'] ?? '';
  585. $info['customMobile'] = $customInfo['mobile'] ?? '';
  586. $info['province'] = $customInfo['province'] ?? '';
  587. $info['city'] = $customInfo['city'] ?? '';;
  588. $info['address'] = $customInfo['address'] ?? '';;
  589. $info['floor'] = $customInfo['floor'] ?? '';;
  590. $info['fullAddress'] = $customInfo['fullAddress'] ?? '';;
  591. $info['lat'] = $customInfo['lat'] ?? '';;
  592. $info['long'] = $customInfo['long'] ?? '';;
  593. $info['sendTime'] = $sendTime;
  594. $res = DadaExpressServices::queryDeliverFee($info);
  595. util::success(['sedCost' => $res['fee']]);
  596. }
  597. }