PurchaseController.php 18 KB

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