PurchaseController.php 20 KB

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