PurchaseController.php 19 KB

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