PurchaseController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. noticeUtil::push('订单号无效。。。 ' , '15280215347');
  246. util::fail('订单号无效');
  247. }
  248. if ($order->status == 5) {
  249. util::fail('订单已取消');
  250. }
  251. if ($order->status != 1) {
  252. util::fail('订单不是待付款状态');
  253. }
  254. $id = $order->id;
  255. $order->changePrice = 2;
  256. $order->save();
  257. $deadline = $order->deadline;
  258. $current = time();
  259. if (($current + 50) > strtotime($deadline)) {
  260. util::fail('订单已失效,请重新下单');
  261. }
  262. $name = $orderSn;
  263. $totalFee = $order->realPrice;
  264. //强制使用小程序的miniOpenId
  265. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  266. if (empty($openId)) {
  267. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  268. }
  269. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  270. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  271. $wxPayType = 0;
  272. if (httpUtil::isMiniProgram()) {
  273. $wxPayType = 1;
  274. }
  275. $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  276. //订单30分钟后过期
  277. $now = time();
  278. $expireTime = $now + 1800;
  279. $wx = Yii::getAlias("@vendor/weixin");
  280. require_once($wx . '/lib/WxPay.Api.php');
  281. require_once($wx . '/example/WxPay.JsApiPay.php');
  282. $input = new \WxPayUnifiedOrder();
  283. $input->SetBody($name);
  284. $input->SetOut_trade_no($orderSn);
  285. $input->SetTotal_fee($totalFee * 100);
  286. $input->SetTime_start(date("YmdHis", $now));
  287. //将流水类型、代金劵等传给微信再回传
  288. $input->SetAttach($attach);
  289. $input->SetTime_expire(date("YmdHis", $expireTime));
  290. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  291. $input->SetTrade_type("JSAPI");
  292. //花卉宝代为申请的微信支付
  293. $merchantExtend = WxOpenClass::getWxInfo();
  294. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  295. $input->SetSub_openid($openId);
  296. } else {
  297. $input->SetOpenid($openId);
  298. }
  299. //强制使用小程序的miniAppId
  300. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  301. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  302. $tools = new \JsApiPay();
  303. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  304. $newParams = json_decode($jsApiParameters, true);
  305. $newParams['id'] = $id;
  306. util::success($newParams);
  307. }
  308. //运费计算 lqh 2021.4.12 暂时返回固定值
  309. public function actionFreight()
  310. {
  311. //lqh 2021.6.25 运费固定返回0
  312. util::success(['sedCost' => 0]);
  313. $post = Yii::$app->request->post();
  314. $ghsId = $post['ghsId'] ?? 0;
  315. //供货商
  316. $ghsInfo = GhsClass::getById($ghsId);
  317. if (empty($ghsInfo)) {
  318. util::fail('没有找到供货商');
  319. }
  320. //花材信息
  321. $productJson = $post['product'] ?? '';
  322. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  323. if (empty($productJson)) {
  324. util::fail('请选择花材');
  325. }
  326. $productList = json_decode($productJson, true);
  327. if (empty($productList)) {
  328. util::fail('请选择花材');
  329. }
  330. $productList = PurchaseClass::mergeItemInfo($productList);
  331. $weight = 0;
  332. $price = 0;
  333. $productData = ProductClass::getProductMapData($productList);
  334. $bigNum = 0;
  335. foreach ($productList as $key => $val) {
  336. $productId = $val['productId'];
  337. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  338. $bigNum += $val['bigNum'];
  339. $weight = bcadd($w, $weight, 2);
  340. $ratio = $productData[$productId]['ratio'] ?? 0;
  341. //大小数量合并多少扎
  342. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  343. //售卖价格
  344. $itemPrice = $productData[$productId]['price'] ?? 0;
  345. //合计价格
  346. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  347. }
  348. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  349. $ghsSjId = $ghsInfo['sjId'] ?? 0;
  350. $sendTime = $post['sendTime'] ?? '';
  351. if (!empty($sendTime)) {
  352. //配送时间不为空
  353. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  354. $sendTimeInt = strtotime($sendTime);
  355. $now = time();
  356. $diff = $sendTimeInt - $now;
  357. //因服务器写入时间,改为至少提前6分钟
  358. if ($diff <= 360) {
  359. util::fail('配送时间至少提前6分钟');
  360. }
  361. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  362. } else {
  363. $sendTime = date('Y-m-d H:i', strtotime("+30 minute"));
  364. }
  365. //找供货商的客户信息,用里面的配送相关地址信息
  366. $customId = $ghsInfo['customId'] ?? 0;
  367. $customInfo = CustomClass::getById($customId);
  368. if (empty($customInfo)) {
  369. util::fail('没有找到客户');
  370. }
  371. //判断商家是否开启达达,若未开启,则使用自定义运费计算
  372. $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
  373. if (!$shopNo) {
  374. //使用自定义运费
  375. //util::fail('供货商暂未开通配送');
  376. $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight);
  377. util::success(['sedCost' => $cost]);
  378. }
  379. //组装订单信息
  380. $info = [];
  381. //发送放的商家信息,即供货商的相关信息
  382. $info['sjId'] = $ghsSjId;
  383. $info['shopId'] = $ghsShopId;
  384. $info['actPrice'] = $price;//价格
  385. $info['weight'] = $weight;
  386. $info['orderSn'] = \common\components\orderSn::getGhsOrderSn();
  387. $info['bigNum'] = $bigNum;
  388. //配送地址,当前零售端的门店地址
  389. $info['customName'] = $customInfo['name'] ?? '';
  390. $info['customMobile'] = $customInfo['mobile'] ?? '';
  391. $info['province'] = $customInfo['province'] ?? '';
  392. $info['city'] = $customInfo['city'] ?? '';;
  393. $info['address'] = $customInfo['address'] ?? '';;
  394. $info['floor'] = $customInfo['floor'] ?? '';;
  395. $info['fullAddress'] = $customInfo['fullAddress'] ?? '';;
  396. $info['lat'] = $customInfo['lat'] ?? '';;
  397. $info['long'] = $customInfo['long'] ?? '';;
  398. $info['sendTime'] = $sendTime;
  399. $res = DadaExpressServices::queryDeliverFee($info);
  400. util::success(['sedCost' => $res['fee']]);
  401. }
  402. }