PurchaseController.php 26 KB

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