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::getGhsInfo($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['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  36. $customId = $ghsInfo['customId'] ?? 0;
  37. $post['customId'] = $customId;
  38. $shopAdmin = $this->shopAdmin;
  39. $name = $shopAdmin['name'] ?? '';
  40. $post['shopAdminName'] = $name;
  41. $product = $post['product'] ?? '';
  42. $productList = json_decode($product, true);
  43. //判断product数据是不是同个供货商的
  44. ProductClass::valid($productList, $currentShopId);
  45. $post['product'] = $productList;
  46. $post['sendCost'] = isset($post['sendCost']) && is_numeric($post['sendCost']) ? $post['sendCost'] : 0;
  47. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  48. $orderSn = $respond->orderSn ?? '';
  49. $actPrice = $respond->actPrice ?? '';
  50. $transaction->commit();
  51. $data = [
  52. 'orderSn' => $orderSn,
  53. 'actPrice' => $actPrice,
  54. 'hasBalancePay' => 1,
  55. 'hasPayPassword' => 0,
  56. 'hasDebtPay' => 1,
  57. ];
  58. util::success($data);
  59. } catch (Exception $e) {
  60. $transaction->rollBack();
  61. util::fail();
  62. }
  63. }
  64. //延期支付 ssh 2021.1.24
  65. public function actionDebtPay()
  66. {
  67. $get = Yii::$app->request->get();
  68. $orderSn = $get['orderSn'] ?? 0;
  69. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  70. PurchaseClass::valid($info->attributes, $this->shopId);
  71. $deadline = $info->deadline;
  72. if ($deadline <= date("Y-m-d H:i:s")) {
  73. PurchaseClass::cancel($info);
  74. util::fail('订单已失效,请重新采购');
  75. }
  76. $ghsId = $info->ghsId;
  77. $ghs = GhsClass::getGhsInfo($ghsId);
  78. $customId = $ghs['customId'] ?? 0;
  79. $custom = CustomClass::getCustom($customId);
  80. if (empty($custom)) {
  81. util::fail('没有找到客户');
  82. }
  83. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  84. util::fail('您没有延期付款权限,请联系此批发商开通');
  85. }
  86. $connection = Yii::$app->db;//事务处理
  87. $transaction = $connection->beginTransaction();
  88. try {
  89. PurchaseClass::debt($info);
  90. $transaction->commit();
  91. } catch (Exception $e) {
  92. $transaction->rollBack();
  93. util::fail('支付失败');
  94. }
  95. util::complete();
  96. }
  97. //余额支付 ssh 2021.1.24
  98. public function actionBalancePay()
  99. {
  100. $get = Yii::$app->request->get();
  101. $orderSn = $get['orderSn'] ?? 0;
  102. $password = $get['password'] ?? '';
  103. // 重新获取,不加密的用户数据
  104. $admin = AdminClass::getById($this->adminId);
  105. if (password_verify($password, $admin['payPassword']) == false) {
  106. util::fail('密码错误');
  107. }
  108. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  109. if (empty($info)) {
  110. util::fail('没有找到采购单');
  111. }
  112. PurchaseClass::valid($info->attributes, $this->shopId);
  113. $connection = Yii::$app->db;//事务处理
  114. $transaction = $connection->beginTransaction();
  115. try {
  116. purchaseClass::balancePay($info);
  117. $transaction->commit();
  118. } catch (Exception $e) {
  119. $transaction->rollBack();
  120. util::fail('支付失败');
  121. }
  122. util::complete();
  123. }
  124. //采购记录 ssh 2021.1.24
  125. public function actionList()
  126. {
  127. $get = Yii::$app->request->get();
  128. $where = ['shopId' => $this->shopId];
  129. $status = $get['status'] ?? 0;
  130. if (!empty($status)) {
  131. $where['status'] = $status;
  132. }
  133. $list = PurchaseService::getPurchaseList($where);
  134. util::success($list);
  135. }
  136. //采购详情 ssh 2021.01.25
  137. public function actionDetail()
  138. {
  139. $get = Yii::$app->request->get();
  140. $id = $get['id'] ?? 0;
  141. $info = PurchaseClass::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];
  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. }