PurchaseController.php 15 KB

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