PurchaseController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. namespace hd\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\ghs\classes\GhsClass;
  5. use biz\shop\classes\ShopExpressClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizGhs\express\services\DadaExpressServices;
  8. use bizHd\purchase\classes\PurchaseClass;
  9. use bizHd\purchase\services\PurchaseService;
  10. use bizGhs\product\classes\ProductClass;
  11. use bizHd\wx\classes\WxOpenClass;
  12. use common\components\dict;
  13. use common\components\freight;
  14. use common\components\httpUtil;
  15. use Yii;
  16. use common\components\util;
  17. class PurchaseController extends BaseController
  18. {
  19. //生成采购单 ssh 2021.1.17
  20. public function actionCreateOrder()
  21. {
  22. $post = Yii::$app->request->post();
  23. $ghsId = $post['ghsId'] ?? 0;
  24. //供应商
  25. $ghsInfo = GhsClass::getById($ghsId);
  26. if (empty($ghsInfo)) {
  27. util::fail('没有找到供应商');
  28. }
  29. //增加判定,若是快递配置,则判定供应商是否开通达达,若未开通,则不能现在配送
  30. $getType = $post['getType'] ?? 0;
  31. if ($getType == 1) {
  32. //配送
  33. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  34. $ghsSjId = $ghsInfo['sjId'] ?? 0;
  35. // $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
  36. // if (!$shopNo) {
  37. // util::fail("商家还未开通配送服务");
  38. // }
  39. //计算运费, 先粗略限制,判定sendCost不能为空, 后续应该重新再算运费
  40. // if (!$post['sendCost']) {
  41. // util::fail("请确认运费");
  42. // }
  43. }
  44. $connection = Yii::$app->db;//事务处理
  45. $transaction = $connection->beginTransaction();
  46. try {
  47. $currentShopId = $ghsInfo['shopId'] ?? 0;
  48. $post['sjId'] = $this->sjId;
  49. $post['shopId'] = $this->shopId;
  50. $post['shopAdminId'] = $this->shopAdminId;
  51. $post['ghsId'] = $ghsId;
  52. $post['ghsName'] = $ghsInfo['name'] ?? '';
  53. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  54. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  55. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  56. $customId = $ghsInfo['customId'] ?? 0;
  57. $post['customId'] = $customId;
  58. $shopAdmin = $this->shopAdmin->attributes;
  59. $name = $shopAdmin['name'] ?? '';
  60. $post['shopAdminName'] = $name;
  61. $product = $post['product'] ?? '';
  62. $productList = json_decode($product, true);
  63. //判断product数据是不是同个供应商的
  64. ProductClass::valid($productList, $currentShopId);
  65. $post['product'] = $productList;
  66. $post['sendCost'] = isset($post['sendCost']) && is_numeric($post['sendCost']) ? $post['sendCost'] : 0;
  67. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  68. $orderSn = $respond->orderSn ?? '';
  69. $actPrice = $respond->actPrice ?? '';
  70. $realPrice = $respond->realPrice ?? '';
  71. $transaction->commit();
  72. //可以付款的倒计时,实际少120秒,避免支付回调时订单已经自动过期
  73. $validTime = dict::getDict('sale_purchase_order_valid');
  74. $realTime = $validTime - 120;
  75. $count = $realTime > 0 ? $realTime : 100;
  76. $custom = CustomClass::getById($customId);
  77. $hasDebtPay = $custom['debt'] ?? 0;
  78. $data = [
  79. 'orderSn' => $orderSn,
  80. 'actPrice' => $actPrice,
  81. 'realPrice' => $realPrice,
  82. 'hasBalancePay' => 1,
  83. 'hasPayPassword' => 0,
  84. 'hasDebtPay' => $hasDebtPay,
  85. 'count' => $count,
  86. ];
  87. util::success($data);
  88. } catch (Exception $e) {
  89. $transaction->rollBack();
  90. util::fail();
  91. }
  92. }
  93. //延期支付 ssh 2021.1.24
  94. public function actionDebtPay()
  95. {
  96. $get = Yii::$app->request->get();
  97. $orderSn = $get['orderSn'] ?? 0;
  98. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  99. PurchaseService::valid($info, $this->shopId);
  100. $ghsId = $info->ghsId;
  101. $ghs = GhsClass::getGhsInfo($ghsId);
  102. $customId = $ghs['customId'] ?? 0;
  103. $custom = CustomClass::getCustom($customId);
  104. if (empty($custom)) {
  105. util::fail('没有找到客户');
  106. }
  107. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  108. util::fail('您没有延期付款权限');
  109. }
  110. $connection = Yii::$app->db;//事务处理
  111. $transaction = $connection->beginTransaction();
  112. try {
  113. //延期支付
  114. PurchaseService::debt($info);
  115. $transaction->commit();
  116. } catch (Exception $e) {
  117. $transaction->rollBack();
  118. util::fail('支付失败');
  119. }
  120. util::success($info->attributes);
  121. }
  122. //余额支付 ssh 2021.1.24
  123. public function actionBalancePay()
  124. {
  125. $get = Yii::$app->request->get();
  126. $orderSn = $get['orderSn'] ?? 0;
  127. $password = $get['password'] ?? '';
  128. // 重新获取,不加密的用户数据
  129. $admin = AdminClass::getById($this->adminId, true);
  130. if (password_verify($password, $admin->payPassword) == false) {
  131. util::fail('密码错误');
  132. }
  133. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  134. if (empty($info)) {
  135. util::fail('没有找到采购单');
  136. }
  137. PurchaseService::valid($info, $this->shopId);
  138. $connection = Yii::$app->db;//事务处理
  139. $transaction = $connection->beginTransaction();
  140. try {
  141. //余额支付
  142. purchaseService::balancePay($info);
  143. $transaction->commit();
  144. } catch (Exception $e) {
  145. $transaction->rollBack();
  146. util::fail('支付失败');
  147. }
  148. util::success($info->attributes);
  149. }
  150. //采购记录 ssh 2021.1.24
  151. public function actionList()
  152. {
  153. $get = Yii::$app->request->get();
  154. $where = ['shopId' => $this->shopId];
  155. $status = $get['status'] ?? 0;
  156. if (!empty($status)) {
  157. $where['status'] = $status;
  158. }
  159. $respond = PurchaseService::getPurchaseList($where);
  160. $respond['shop'] = $this->shop->attributes;
  161. util::success($respond);
  162. }
  163. //采购详情 ssh 2021.01.25
  164. public function actionDetail()
  165. {
  166. $get = Yii::$app->request->get();
  167. $id = $get['id'] ?? 0;
  168. $info = PurchaseService::getInfo($id, true, true);
  169. if (isset($info['shopId']) == false || $info['shopId'] != $this->shopId) {
  170. util::fail('没有权限访问');
  171. }
  172. util::success($info);
  173. }
  174. //可用的支付方式 ssh 2021.1.25
  175. public function actionPayWay()
  176. {
  177. $button = [
  178. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  179. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  180. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  181. ];
  182. util::success(['button' => $button]);
  183. }
  184. //待结款明细 ssh 2021.1.26
  185. public function actionDebtList()
  186. {
  187. $id = Yii::$app->request->get('id', 0);
  188. $info = GhsClass::getGhsInfo($id);
  189. GhsClass::valid($info, $this->shopId);
  190. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  191. $respond = PurchaseService::getDebtList($where);
  192. util::success($respond);
  193. }
  194. //微信支付 shish 2021.4.11
  195. public function actionWxPay()
  196. {
  197. ini_set('date.timezone', 'Asia/Shanghai');
  198. $post = Yii::$app->request->post();
  199. $couponId = $post['couponId'] ?? 0;
  200. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  201. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  202. if (empty($order)) {
  203. util::fail('订单号无效');
  204. }
  205. $id = $order->id;
  206. $order->changePrice = 2;
  207. $order->save();
  208. $deadline = $order->deadline;
  209. $current = date("Y-m-d H:i:s");
  210. if ((strtotime($deadline) + 20) < strtotime($current)) {
  211. util::fail('订单已经失效');
  212. }
  213. $name = $orderSn;
  214. $totalFee = $order->realPrice;
  215. //强制使用小程序的miniOpenId
  216. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  217. if (empty($openId)) {
  218. util::fail('没有openId');
  219. }
  220. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  221. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  222. $wxPayType = 0;
  223. if (httpUtil::isMiniProgram()) {
  224. $wxPayType = 1;
  225. }
  226. $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  227. //订单30分钟后过期
  228. $now = time();
  229. $expireTime = $now + 1800;
  230. $wx = Yii::getAlias("@vendor/weixin");
  231. require_once($wx . '/lib/WxPay.Api.php');
  232. require_once($wx . '/example/WxPay.JsApiPay.php');
  233. $input = new \WxPayUnifiedOrder();
  234. $input->SetBody($name);
  235. $input->SetOut_trade_no($orderSn);
  236. $input->SetTotal_fee($totalFee * 100);
  237. $input->SetTime_start(date("YmdHis", $now));
  238. //将流水类型、代金劵等传给微信再回传
  239. $input->SetAttach($attach);
  240. $input->SetTime_expire(date("YmdHis", $expireTime));
  241. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  242. $input->SetTrade_type("JSAPI");
  243. //花卉宝代为申请的微信支付
  244. $merchantExtend = WxOpenClass::getWxInfo();
  245. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  246. $input->SetSub_openid($openId);
  247. } else {
  248. $input->SetOpenid($openId);
  249. }
  250. //强制使用小程序的miniAppId
  251. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  252. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  253. $tools = new \JsApiPay();
  254. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  255. $newParams = json_decode($jsApiParameters, true);
  256. $newParams['id'] = $id;
  257. util::success($newParams);
  258. }
  259. //运费计算 linqh 2021.4.12 暂时返回固定值
  260. public function actionFreight()
  261. {
  262. //linqh 2021.6.25 运费固定返回0
  263. util::success(['sedCost' => 0]);
  264. $post = Yii::$app->request->post();
  265. $ghsId = $post['ghsId'] ?? 0;
  266. //供应商
  267. $ghsInfo = GhsClass::getById($ghsId);
  268. if (empty($ghsInfo)) {
  269. util::fail('没有找到供应商');
  270. }
  271. //花材信息
  272. $productJson = $post['product'] ?? '';
  273. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  274. if (empty($productJson)) {
  275. util::fail('请选择花材');
  276. }
  277. $productList = json_decode($productJson, true);
  278. if (empty($productList)) {
  279. util::fail('请选择花材');
  280. }
  281. $productList = PurchaseClass::mergeItemInfo($productList);
  282. $weight = 0;
  283. $price = 0;
  284. $productData = ProductClass::getProductMapData($productList);
  285. $bigNum = 0;
  286. foreach ($productList as $key => $val) {
  287. $productId = $val['productId'];
  288. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  289. $bigNum += $val['bigNum'];
  290. $weight = bcadd($w, $weight, 2);
  291. $ratio = $productData[$productId]['ratio'] ?? 0;
  292. //大小数量合并多少扎
  293. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  294. //售卖价格
  295. $itemPrice = $productData[$productId]['price'] ?? 0;
  296. //合计价格
  297. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  298. }
  299. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  300. $ghsSjId = $ghsInfo['sjId'] ?? 0;
  301. $sendTime = $post['sendTime'] ?? '';
  302. if (!empty($sendTime)) {
  303. //配送时间不为空
  304. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  305. $sendTimeInt = strtotime($sendTime);
  306. $now = time();
  307. $diff = $sendTimeInt - $now;
  308. //因服务器写入时间,改为至少提前6分钟
  309. if ($diff <= 360) {
  310. util::fail('配送时间至少提前6分钟');
  311. }
  312. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  313. } else {
  314. $sendTime = date('Y-m-d H:i', strtotime("+30 minute"));
  315. }
  316. //找供应商的客户信息,用里面的配送相关地址信息
  317. $customId = $ghsInfo['customId'] ?? 0;
  318. $customInfo = CustomClass::getById($customId);
  319. if (empty($customInfo)) {
  320. util::fail('没有找到客户');
  321. }
  322. //判断商家是否开启达达,若未开启,则使用自定义运费计算
  323. $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
  324. if (!$shopNo) {
  325. //使用自定义运费
  326. //util::fail('供应商暂未开通配送');
  327. $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight);
  328. util::success(['sedCost' => $cost]);
  329. }
  330. //组装订单信息
  331. $info = [];
  332. //发送放的商家信息,即供应商的相关信息
  333. $info['sjId'] = $ghsSjId;
  334. $info['shopId'] = $ghsShopId;
  335. $info['actPrice'] = $price;//价格
  336. $info['weight'] = $weight;
  337. $info['orderSn'] = \common\components\orderSn::getGhsOrderSn();
  338. $info['bigNum'] = $bigNum;
  339. //配送地址,当前零售端的门店地址
  340. $info['customName'] = $customInfo['name'] ?? '';
  341. $info['customMobile'] = $customInfo['mobile'] ?? '';
  342. $info['province'] = $customInfo['province'] ?? '';
  343. $info['city'] = $customInfo['city'] ?? '';;
  344. $info['address'] = $customInfo['address'] ?? '';;
  345. $info['floor'] = $customInfo['floor'] ?? '';;
  346. $info['fullAddress'] = $customInfo['fullAddress'] ?? '';;
  347. $info['lat'] = $customInfo['lat'] ?? '';;
  348. $info['long'] = $customInfo['long'] ?? '';;
  349. $info['sendTime'] = $sendTime;
  350. $res = DadaExpressServices::queryDeliverFee($info);
  351. util::success(['sedCost' => $res['fee']]);
  352. }
  353. }