PurchaseController.php 7.3 KB

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