PurchaseController.php 27 KB

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