PurchaseController.php 17 KB

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