PurchaseController.php 34 KB

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