PurchaseController.php 14 KB

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