PurchaseController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. $respond = PurchaseClass::addPurchase($post);
  37. $orderSn = $respond['orderSn'] ?? '';
  38. $actPrice = $respond['actPrice'] ?? '';
  39. $data = [
  40. 'orderSn' => $orderSn,
  41. 'actPrice' => $actPrice,
  42. 'hasBalancePay' => 1,
  43. 'hasPayPassword' => 0,
  44. 'hasDebtPay' => 1,
  45. ];
  46. util::success($data);
  47. }
  48. //延期支付 ssh 2021.1.24
  49. public function actionDebtPay()
  50. {
  51. $get = Yii::$app->request->get();
  52. $orderSn = $get['orderSn'] ?? 0;
  53. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn]);
  54. PurchaseClass::valid($info, $this->shopId);
  55. PurchaseClass::debt($info);
  56. util::complete();
  57. }
  58. //余额支付 ssh 2021.1.24
  59. public function actionBalancePay()
  60. {
  61. $get = Yii::$app->request->get();
  62. $orderSn = $get['orderSn'] ?? 0;
  63. $password = $get['password'] ?? '';
  64. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn]);
  65. if (empty($info)) {
  66. util::fail('没有找到采购单');
  67. }
  68. PurchaseClass::valid($info, $this->shopId);
  69. purchaseClass::balancePay($info);
  70. util::complete();
  71. }
  72. //采购记录 ssh 2021.1.24
  73. public function actionList()
  74. {
  75. $get = Yii::$app->request->get();
  76. $where = ['shopId' => $this->shopId];
  77. $status = $get['status'] ?? 0;
  78. if (!empty($status)) {
  79. $where['status'] = $status;
  80. }
  81. $list = PurchaseService::getPurchaseList($where);
  82. util::success($list);
  83. }
  84. //采购详情 ssh 2021.01.25
  85. public function actionDetail()
  86. {
  87. $get = Yii::$app->request->get();
  88. $id = $get['id'] ?? 0;
  89. $info = PurchaseClass::getInfo($id, true, true);
  90. PurchaseClass::valid($info, $this->shopId);
  91. util::success($info);
  92. }
  93. //可用的支付方式 ssh 2021.1.25
  94. public function actionPayWay()
  95. {
  96. $button = [
  97. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  98. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  99. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  100. ];
  101. util::success(['button' => $button]);
  102. }
  103. //待结款明细 ssh 2021.1.26
  104. public function actionDebtList()
  105. {
  106. $where = [
  107. 'merchantId' => $this->sjId,
  108. 'debt' => PurchaseClass::DEBT_YES
  109. ];
  110. $respond = PurchaseService::getDebtList($where);
  111. util::success($respond);
  112. }
  113. //微信支付 shish 2021.4.11
  114. public function actionWxPay()
  115. {
  116. ini_set('date.timezone', 'Asia/Shanghai');
  117. $post = Yii::$app->request->post();
  118. $couponId = $post['couponId'] ?? 0;
  119. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  120. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn]);
  121. if (empty($order)) {
  122. util::fail('订单号无效');
  123. }
  124. $id = $order['id'];
  125. $name = $orderSn;
  126. $totalFee = $order['actPrice'];
  127. //强制使用小程序的miniOpenId
  128. $openId = $this->admin['miniOpenId'];
  129. $typeList = dict::getConfig('capitalType');
  130. $capitalType = $typeList['xhPurchase']['id'];
  131. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  132. $wxPayType = 0;
  133. if (httpUtil::isMiniProgram()) {
  134. $wxPayType = 1;
  135. }
  136. $attach = "couponId=" . $couponId . "&capitalType=" . $capitalType . '&wxPayType=' . $wxPayType;
  137. //订单30分钟后过期
  138. $now = time();
  139. $expireTime = $now + 1800;
  140. $wx = Yii::getAlias("@vendor/weixin");
  141. require_once($wx . '/lib/WxPay.Api.php');
  142. require_once($wx . '/example/WxPay.JsApiPay.php');
  143. $input = new \WxPayUnifiedOrder();
  144. $input->SetBody($name);
  145. $input->SetOut_trade_no($orderSn);
  146. $input->SetTotal_fee($totalFee * 100);
  147. $input->SetTime_start(date("YmdHis", $now));
  148. $input->SetAttach($attach);//将流水类型、代金劵等传给微信再回传
  149. $input->SetTime_expire(date("YmdHis", $expireTime));
  150. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  151. $input->SetTrade_type("JSAPI");
  152. //花卉宝代为申请的微信支付
  153. $merchantExtend = WxOpenClass::getWxInfo();
  154. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  155. $input->SetSub_openid($openId);
  156. } else {
  157. $input->SetOpenid($openId);
  158. }
  159. //强制使用小程序的miniAppId
  160. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  161. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  162. $tools = new \JsApiPay();
  163. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  164. $newParams = json_decode($jsApiParameters, true);
  165. $current = date("Y-m-d H:i:s");
  166. $updateData = ['deadline' => $current, 'changePrice' => 2];
  167. PurchaseClass::updateById($id, $updateData);
  168. util::success($newParams);
  169. }
  170. //运费计算 linqh 2021.4.12 暂反回固定
  171. public function actionFreight()
  172. {
  173. util::success(['sedCost' => 10]);
  174. }
  175. }