| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace hd\controllers;
- use biz\ghs\classes\GhsClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\purchase\services\PurchaseService;
- use bizGhs\product\classes\ProductClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\httpUtil;
- use Yii;
- use common\components\util;
- class PurchaseController extends BaseController
- {
- //生成采购单 ssh 2021.1.17
- public function actionCreateOrder()
- {
- $post = Yii::$app->request->post();
- $ghsId = $post['ghsId'] ?? 0;
- //供货商
- $supplier = GhsClass::getById($ghsId);
- if (empty($supplier)) {
- util::fail('没有找到供货商');
- }
- $currentShopId = $supplier['shopId'] ?? 0;
- $post['merchantId'] = $this->sjId;
- $post['shopId'] = $this->shopId;
- $post['shopAdminId'] = $this->shopAdminId;
- $shopAdmin = $this->shopAdmin;
- $name = $shopAdmin['name'] ?? '';
- $post['shopAdminName'] = $name;
- $product = $post['product'] ?? '';
- $productList = json_decode($product, true);
- //判断product数据是不是同个供货商的
- ProductClass::valid($productList, $currentShopId);
- $post['product'] = $productList;
- $post['sendCost'] = isset($post['sendCost']) && is_numeric($post['sendCost']) ? $post['sendCost'] : 0;
- $respond = PurchaseService::addPurchase($post);
- $orderSn = $respond['orderSn'] ?? '';
- $actPrice = $respond['actPrice'] ?? '';
- $data = [
- 'orderSn' => $orderSn,
- 'actPrice' => $actPrice,
- 'hasBalancePay' => 1,
- 'hasPayPassword' => 0,
- 'hasDebtPay' => 1,
- ];
- util::success($data);
- }
- //延期支付 ssh 2021.1.24
- public function actionDebtPay()
- {
- $get = Yii::$app->request->get();
- $orderSn = $get['orderSn'] ?? 0;
- $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
- PurchaseClass::valid($info->attributes, $this->shopId);
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
- PurchaseClass::debt($info);
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- util::fail('支付失败');
- }
- util::complete();
- }
- //余额支付 ssh 2021.1.24
- public function actionBalancePay()
- {
- $get = Yii::$app->request->get();
- $orderSn = $get['orderSn'] ?? 0;
- $password = $get['password'] ?? '';
- $admin = $this->admin;
- if (password_verify($password, $admin['payPassword']) == false) {
- util::fail('密码错误');
- }
- $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
- if (empty($info)) {
- util::fail('没有找到采购单');
- }
- PurchaseClass::valid($info->attributes, $this->shopId);
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
- purchaseClass::balancePay($info);
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- util::fail('支付失败');
- }
- util::complete();
- }
- //采购记录 ssh 2021.1.24
- public function actionList()
- {
- $get = Yii::$app->request->get();
- $where = ['shopId' => $this->shopId];
- $status = $get['status'] ?? 0;
- if (!empty($status)) {
- $where['status'] = $status;
- }
- $list = PurchaseService::getPurchaseList($where);
- util::success($list);
- }
- //采购详情 ssh 2021.01.25
- public function actionDetail()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $info = PurchaseClass::getInfo($id, true, true);
- PurchaseClass::valid($info, $this->shopId);
- util::success($info);
- }
- //可用的支付方式 ssh 2021.1.25
- public function actionPayWay()
- {
- $button = [
- ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
- ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
- ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
- ];
- util::success(['button' => $button]);
- }
- //待结款明细 ssh 2021.1.26
- public function actionDebtList()
- {
- $id = Yii::$app->request->get('id', 0);
- $info = GhsClass::getGhsInfo($id);
- GhsClass::valid($info, $this->sjId);
- $where = ['ghsId' => $id];
- $respond = PurchaseService::getDebtList($where);
- util::success($respond);
- }
- //微信支付 shish 2021.4.11
- public function actionWxPay()
- {
- ini_set('date.timezone', 'Asia/Shanghai');
- $post = Yii::$app->request->post();
- $couponId = $post['couponId'] ?? 0;
- $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
- $order = PurchaseClass::getByCondition(['orderSn' => $orderSn]);
- if (empty($order)) {
- util::fail('订单号无效');
- }
- $id = $order['id'];
- $name = $orderSn;
- $totalFee = $order['actPrice'];
- //强制使用小程序的miniOpenId
- $openId = $this->admin['miniOpenId'];
- $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
- //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
- $wxPayType = 0;
- if (httpUtil::isMiniProgram()) {
- $wxPayType = 1;
- }
- $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
- //订单30分钟后过期
- $now = time();
- $expireTime = $now + 1800;
- $wx = Yii::getAlias("@vendor/weixin");
- require_once($wx . '/lib/WxPay.Api.php');
- require_once($wx . '/example/WxPay.JsApiPay.php');
- $input = new \WxPayUnifiedOrder();
- $input->SetBody($name);
- $input->SetOut_trade_no($orderSn);
- $input->SetTotal_fee($totalFee * 100);
- $input->SetTime_start(date("YmdHis", $now));
- //将流水类型、代金劵等传给微信再回传
- $input->SetAttach($attach);
- $input->SetTime_expire(date("YmdHis", $expireTime));
- $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
- $input->SetTrade_type("JSAPI");
- //花卉宝代为申请的微信支付
- $merchantExtend = WxOpenClass::getWxInfo();
- if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
- $input->SetSub_openid($openId);
- } else {
- $input->SetOpenid($openId);
- }
- //强制使用小程序的miniAppId
- $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
- $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
- $tools = new \JsApiPay();
- $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
- $newParams = json_decode($jsApiParameters, true);
- $current = date("Y-m-d H:i:s");
- $updateData = ['deadline' => $current, 'changePrice' => 2];
- PurchaseClass::updateById($id, $updateData);
- util::success($newParams);
- }
- //运费计算 linqh 2021.4.12 暂反回固定
- public function actionFreight()
- {
- util::success(['sedCost' => 10]);
- }
- }
|