PurchaseController.php 32 KB

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