PurchaseController.php 7.4 KB

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