PurchaseController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. $ghsId = $info->ghsId;
  74. $ghs = GhsClass::getGhsInfo($ghsId);
  75. $customId = $ghs['customId'] ?? 0;
  76. $custom = CustomClass::getCustom($customId);
  77. if (empty($custom)) {
  78. util::fail('没有找到客户');
  79. }
  80. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  81. util::fail('您没有延期付款权限,请联系此批发商开通');
  82. }
  83. $connection = Yii::$app->db;//事务处理
  84. $transaction = $connection->beginTransaction();
  85. try {
  86. PurchaseClass::debt($info);
  87. $transaction->commit();
  88. } catch (Exception $e) {
  89. $transaction->rollBack();
  90. util::fail('支付失败');
  91. }
  92. util::complete();
  93. }
  94. //余额支付 ssh 2021.1.24
  95. public function actionBalancePay()
  96. {
  97. $get = Yii::$app->request->get();
  98. $orderSn = $get['orderSn'] ?? 0;
  99. $password = $get['password'] ?? '';
  100. // 重新获取,不加密的用户数据
  101. $admin = AdminClass::getById($this->adminId);
  102. if (password_verify($password, $admin['payPassword']) == false) {
  103. util::fail('密码错误');
  104. }
  105. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  106. if (empty($info)) {
  107. util::fail('没有找到采购单');
  108. }
  109. PurchaseClass::valid($info->attributes, $this->shopId);
  110. $connection = Yii::$app->db;//事务处理
  111. $transaction = $connection->beginTransaction();
  112. try {
  113. purchaseClass::balancePay($info);
  114. $transaction->commit();
  115. } catch (Exception $e) {
  116. $transaction->rollBack();
  117. util::fail('支付失败');
  118. }
  119. util::complete();
  120. }
  121. //采购记录 ssh 2021.1.24
  122. public function actionList()
  123. {
  124. $get = Yii::$app->request->get();
  125. $where = ['shopId' => $this->shopId];
  126. $status = $get['status'] ?? 0;
  127. if (!empty($status)) {
  128. $where['status'] = $status;
  129. }
  130. $list = PurchaseService::getPurchaseList($where);
  131. util::success($list);
  132. }
  133. //采购详情 ssh 2021.01.25
  134. public function actionDetail()
  135. {
  136. $get = Yii::$app->request->get();
  137. $id = $get['id'] ?? 0;
  138. $info = PurchaseClass::getInfo($id, true, true);
  139. PurchaseClass::valid($info, $this->shopId);
  140. util::success($info);
  141. }
  142. //可用的支付方式 ssh 2021.1.25
  143. public function actionPayWay()
  144. {
  145. $button = [
  146. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  147. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  148. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  149. ];
  150. util::success(['button' => $button]);
  151. }
  152. //待结款明细 ssh 2021.1.26
  153. public function actionDebtList()
  154. {
  155. $id = Yii::$app->request->get('id', 0);
  156. $info = GhsClass::getGhsInfo($id);
  157. GhsClass::valid($info, $this->shopId);
  158. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  159. $respond = PurchaseService::getDebtList($where);
  160. util::success($respond);
  161. }
  162. //微信支付 shish 2021.4.11
  163. public function actionWxPay()
  164. {
  165. ini_set('date.timezone', 'Asia/Shanghai');
  166. $post = Yii::$app->request->post();
  167. $couponId = $post['couponId'] ?? 0;
  168. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  169. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn]);
  170. if (empty($order)) {
  171. util::fail('订单号无效');
  172. }
  173. $id = $order['id'];
  174. $name = $orderSn;
  175. $totalFee = $order['actPrice'];
  176. //强制使用小程序的miniOpenId
  177. $openId = $this->admin['miniOpenId'];
  178. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  179. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  180. $wxPayType = 0;
  181. if (httpUtil::isMiniProgram()) {
  182. $wxPayType = 1;
  183. }
  184. $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  185. //订单30分钟后过期
  186. $now = time();
  187. $expireTime = $now + 1800;
  188. $wx = Yii::getAlias("@vendor/weixin");
  189. require_once($wx . '/lib/WxPay.Api.php');
  190. require_once($wx . '/example/WxPay.JsApiPay.php');
  191. $input = new \WxPayUnifiedOrder();
  192. $input->SetBody($name);
  193. $input->SetOut_trade_no($orderSn);
  194. $input->SetTotal_fee($totalFee * 100);
  195. $input->SetTime_start(date("YmdHis", $now));
  196. //将流水类型、代金劵等传给微信再回传
  197. $input->SetAttach($attach);
  198. $input->SetTime_expire(date("YmdHis", $expireTime));
  199. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  200. $input->SetTrade_type("JSAPI");
  201. //花卉宝代为申请的微信支付
  202. $merchantExtend = WxOpenClass::getWxInfo();
  203. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  204. $input->SetSub_openid($openId);
  205. } else {
  206. $input->SetOpenid($openId);
  207. }
  208. //强制使用小程序的miniAppId
  209. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  210. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  211. $tools = new \JsApiPay();
  212. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  213. $newParams = json_decode($jsApiParameters, true);
  214. $current = date("Y-m-d H:i:s");
  215. $updateData = ['deadline' => $current, 'changePrice' => 2];
  216. PurchaseClass::updateById($id, $updateData);
  217. util::success($newParams);
  218. }
  219. //运费计算 linqh 2021.4.12 暂反回固定
  220. public function actionFreight()
  221. {
  222. util::success(['sedCost' => 10]);
  223. }
  224. }