PurchaseController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace hd\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\ghs\classes\GhsClass;
  5. use bizGhs\custom\classes\CustomClass;
  6. use bizHd\purchase\classes\PurchaseClass;
  7. use bizHd\purchase\services\PurchaseService;
  8. use bizGhs\product\classes\ProductClass;
  9. use bizHd\wx\classes\WxOpenClass;
  10. use common\components\dict;
  11. use common\components\httpUtil;
  12. use Yii;
  13. use common\components\util;
  14. class PurchaseController extends BaseController
  15. {
  16. //生成采购单 ssh 2021.1.17
  17. public function actionCreateOrder()
  18. {
  19. $post = Yii::$app->request->post();
  20. $ghsId = $post['ghsId'] ?? 0;
  21. //供货商
  22. $ghsInfo = GhsClass::getById($ghsId);
  23. if (empty($ghsInfo)) {
  24. util::fail('没有找到供货商');
  25. }
  26. $connection = Yii::$app->db;//事务处理
  27. $transaction = $connection->beginTransaction();
  28. try {
  29. $currentShopId = $ghsInfo['shopId'] ?? 0;
  30. $post['merchantId'] = $this->sjId;
  31. $post['shopId'] = $this->shopId;
  32. $post['shopAdminId'] = $this->shopAdminId;
  33. $post['ghsId'] = $ghsId;
  34. $post['ghsName'] = $ghsInfo['name'] ?? '';
  35. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  36. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  37. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  38. $customId = $ghsInfo['customId'] ?? 0;
  39. $post['customId'] = $customId;
  40. $shopAdmin = $this->shopAdmin;
  41. $name = $shopAdmin['name'] ?? '';
  42. $post['shopAdminName'] = $name;
  43. $product = $post['product'] ?? '';
  44. $productList = json_decode($product, true);
  45. //判断product数据是不是同个供货商的
  46. ProductClass::valid($productList, $currentShopId);
  47. $post['product'] = $productList;
  48. $post['sendCost'] = isset($post['sendCost']) && is_numeric($post['sendCost']) ? $post['sendCost'] : 0;
  49. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  50. $orderSn = $respond->orderSn ?? '';
  51. $actPrice = $respond->actPrice ?? '';
  52. $transaction->commit();
  53. $data = [
  54. 'orderSn' => $orderSn,
  55. 'actPrice' => $actPrice,
  56. 'hasBalancePay' => 1,
  57. 'hasPayPassword' => 0,
  58. 'hasDebtPay' => 1,
  59. ];
  60. util::success($data);
  61. } catch (Exception $e) {
  62. $transaction->rollBack();
  63. util::fail();
  64. }
  65. }
  66. //延期支付 ssh 2021.1.24
  67. public function actionDebtPay()
  68. {
  69. $get = Yii::$app->request->get();
  70. $orderSn = $get['orderSn'] ?? 0;
  71. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  72. PurchaseClass::valid($info->attributes, $this->shopId);
  73. $deadline = $info->deadline;
  74. if ($deadline <= date("Y-m-d H:i:s")) {
  75. PurchaseClass::cancel($info);
  76. util::fail('订单已失效,请重新采购');
  77. }
  78. $ghsId = $info->ghsId;
  79. $ghs = GhsClass::getGhsInfo($ghsId);
  80. $customId = $ghs['customId'] ?? 0;
  81. $custom = CustomClass::getCustom($customId);
  82. if (empty($custom)) {
  83. util::fail('没有找到客户');
  84. }
  85. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  86. util::fail('您没有延期付款权限,请联系此批发商开通');
  87. }
  88. $connection = Yii::$app->db;//事务处理
  89. $transaction = $connection->beginTransaction();
  90. try {
  91. PurchaseClass::debt($info);
  92. $transaction->commit();
  93. } catch (Exception $e) {
  94. $transaction->rollBack();
  95. util::fail('支付失败');
  96. }
  97. util::complete();
  98. }
  99. //余额支付 ssh 2021.1.24
  100. public function actionBalancePay()
  101. {
  102. $get = Yii::$app->request->get();
  103. $orderSn = $get['orderSn'] ?? 0;
  104. $password = $get['password'] ?? '';
  105. // 重新获取,不加密的用户数据
  106. $admin = AdminClass::getById($this->adminId);
  107. if (password_verify($password, $admin['payPassword']) == false) {
  108. util::fail('密码错误');
  109. }
  110. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  111. if (empty($info)) {
  112. util::fail('没有找到采购单');
  113. }
  114. PurchaseClass::valid($info->attributes, $this->shopId);
  115. $connection = Yii::$app->db;//事务处理
  116. $transaction = $connection->beginTransaction();
  117. try {
  118. purchaseClass::balancePay($info);
  119. $transaction->commit();
  120. } catch (Exception $e) {
  121. $transaction->rollBack();
  122. util::fail('支付失败');
  123. }
  124. util::complete();
  125. }
  126. //采购记录 ssh 2021.1.24
  127. public function actionList()
  128. {
  129. $get = Yii::$app->request->get();
  130. $where = ['shopId' => $this->shopId];
  131. $status = $get['status'] ?? 0;
  132. if (!empty($status)) {
  133. $where['status'] = $status;
  134. }
  135. $list = PurchaseService::getPurchaseList($where);
  136. util::success($list);
  137. }
  138. //采购详情 ssh 2021.01.25
  139. public function actionDetail()
  140. {
  141. $get = Yii::$app->request->get();
  142. $id = $get['id'] ?? 0;
  143. $info = PurchaseClass::getInfo($id, true, true);
  144. PurchaseClass::valid($info, $this->shopId);
  145. util::success($info);
  146. }
  147. //可用的支付方式 ssh 2021.1.25
  148. public function actionPayWay()
  149. {
  150. $button = [
  151. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  152. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  153. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  154. ];
  155. util::success(['button' => $button]);
  156. }
  157. //待结款明细 ssh 2021.1.26
  158. public function actionDebtList()
  159. {
  160. $id = Yii::$app->request->get('id', 0);
  161. $info = GhsClass::getGhsInfo($id);
  162. GhsClass::valid($info, $this->shopId);
  163. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  164. $respond = PurchaseService::getDebtList($where);
  165. util::success($respond);
  166. }
  167. //微信支付 shish 2021.4.11
  168. public function actionWxPay()
  169. {
  170. ini_set('date.timezone', 'Asia/Shanghai');
  171. $post = Yii::$app->request->post();
  172. $couponId = $post['couponId'] ?? 0;
  173. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  174. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn]);
  175. if (empty($order)) {
  176. util::fail('订单号无效');
  177. }
  178. $id = $order['id'];
  179. $name = $orderSn;
  180. $totalFee = $order['actPrice'];
  181. //强制使用小程序的miniOpenId
  182. $openId = $this->admin['miniOpenId'];
  183. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  184. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  185. $wxPayType = 0;
  186. if (httpUtil::isMiniProgram()) {
  187. $wxPayType = 1;
  188. }
  189. $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  190. //订单30分钟后过期
  191. $now = time();
  192. $expireTime = $now + 1800;
  193. $wx = Yii::getAlias("@vendor/weixin");
  194. require_once($wx . '/lib/WxPay.Api.php');
  195. require_once($wx . '/example/WxPay.JsApiPay.php');
  196. $input = new \WxPayUnifiedOrder();
  197. $input->SetBody($name);
  198. $input->SetOut_trade_no($orderSn);
  199. $input->SetTotal_fee($totalFee * 100);
  200. $input->SetTime_start(date("YmdHis", $now));
  201. //将流水类型、代金劵等传给微信再回传
  202. $input->SetAttach($attach);
  203. $input->SetTime_expire(date("YmdHis", $expireTime));
  204. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  205. $input->SetTrade_type("JSAPI");
  206. //花卉宝代为申请的微信支付
  207. $merchantExtend = WxOpenClass::getWxInfo();
  208. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  209. $input->SetSub_openid($openId);
  210. } else {
  211. $input->SetOpenid($openId);
  212. }
  213. //强制使用小程序的miniAppId
  214. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  215. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  216. $tools = new \JsApiPay();
  217. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  218. $newParams = json_decode($jsApiParameters, true);
  219. $current = date("Y-m-d H:i:s");
  220. $updateData = ['deadline' => $current, 'changePrice' => 2];
  221. PurchaseClass::updateById($id, $updateData);
  222. util::success($newParams);
  223. }
  224. //运费计算 linqh 2021.4.12 暂反回固定
  225. public function actionFreight()
  226. {
  227. util::success(['sedCost' => 10]);
  228. }
  229. }