PurchaseController.php 14 KB

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