PurchaseController.php 18 KB

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