PurchaseController.php 9.0 KB

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