PurchaseController.php 8.7 KB

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