PurchaseController.php 6.4 KB

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