PurchaseController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. $respond = PurchaseService::getPurchaseList($where);
  133. $respond['shop'] = $this->shop;
  134. util::success($respond);
  135. }
  136. //采购详情 ssh 2021.01.25
  137. public function actionDetail()
  138. {
  139. $get = Yii::$app->request->get();
  140. $id = $get['id'] ?? 0;
  141. $info = PurchaseService::getInfo($id, true, true);
  142. PurchaseClass::valid($info, $this->shopId);
  143. util::success($info);
  144. }
  145. //可用的支付方式 ssh 2021.1.25
  146. public function actionPayWay()
  147. {
  148. $button = [
  149. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  150. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  151. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  152. ];
  153. util::success(['button' => $button]);
  154. }
  155. //待结款明细 ssh 2021.1.26
  156. public function actionDebtList()
  157. {
  158. $id = Yii::$app->request->get('id', 0);
  159. $info = GhsClass::getGhsInfo($id);
  160. GhsClass::valid($info, $this->shopId);
  161. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  162. $respond = PurchaseService::getDebtList($where);
  163. util::success($respond);
  164. }
  165. //微信支付 shish 2021.4.11
  166. public function actionWxPay()
  167. {
  168. ini_set('date.timezone', 'Asia/Shanghai');
  169. $post = Yii::$app->request->post();
  170. $couponId = $post['couponId'] ?? 0;
  171. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  172. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn]);
  173. if (empty($order)) {
  174. util::fail('订单号无效');
  175. }
  176. $id = $order['id'];
  177. $name = $orderSn;
  178. $totalFee = $order['actPrice'];
  179. //强制使用小程序的miniOpenId
  180. $openId = $this->admin['miniOpenId'];
  181. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  182. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  183. $wxPayType = 0;
  184. if (httpUtil::isMiniProgram()) {
  185. $wxPayType = 1;
  186. }
  187. $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  188. //订单30分钟后过期
  189. $now = time();
  190. $expireTime = $now + 1800;
  191. $wx = Yii::getAlias("@vendor/weixin");
  192. require_once($wx . '/lib/WxPay.Api.php');
  193. require_once($wx . '/example/WxPay.JsApiPay.php');
  194. $input = new \WxPayUnifiedOrder();
  195. $input->SetBody($name);
  196. $input->SetOut_trade_no($orderSn);
  197. $input->SetTotal_fee($totalFee * 100);
  198. $input->SetTime_start(date("YmdHis", $now));
  199. //将流水类型、代金劵等传给微信再回传
  200. $input->SetAttach($attach);
  201. $input->SetTime_expire(date("YmdHis", $expireTime));
  202. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  203. $input->SetTrade_type("JSAPI");
  204. //花卉宝代为申请的微信支付
  205. $merchantExtend = WxOpenClass::getWxInfo();
  206. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  207. $input->SetSub_openid($openId);
  208. } else {
  209. $input->SetOpenid($openId);
  210. }
  211. //强制使用小程序的miniAppId
  212. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  213. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  214. $tools = new \JsApiPay();
  215. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  216. $newParams = json_decode($jsApiParameters, true);
  217. $current = date("Y-m-d H:i:s");
  218. $updateData = ['deadline' => $current, 'changePrice' => 2];
  219. PurchaseClass::updateById($id, $updateData);
  220. util::success($newParams);
  221. }
  222. //运费计算 linqh 2021.4.12 暂反回固定
  223. public function actionFreight()
  224. {
  225. util::success(['sedCost' => 10]);
  226. }
  227. }