PurchaseController.php 28 KB

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