| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?php
- namespace hd\controllers;
- use biz\admin\classes\AdminClass;
- use biz\ghs\classes\GhsClass;
- use biz\notice\classes\NoticeClass;
- use biz\shop\classes\ShopExpressClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\express\services\DadaExpressServices;
- use bizGhs\order\classes\OrderClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\purchase\services\PurchaseService;
- use bizGhs\product\classes\ProductClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dateUtil;
- use common\components\dict;
- use common\components\freight;
- 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;
- //供应商
- $ghsInfo = GhsClass::getById($ghsId);
- if (empty($ghsInfo)) {
- util::fail('没有找到供应商');
- }
- //增加判定,若是快递配置,则判定供应商是否开通达达,若未开通,则不能现在配送
- $getType = $post['getType'] ?? 0;
- if ($getType == 1) {
- //配送
- $ghsShopId = $ghsInfo['shopId'] ?? 0;
- $ghsSjId = $ghsInfo['sjId'] ?? 0;
- // $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
- // if (!$shopNo) {
- // util::fail("商家还未开通配送服务");
- // }
- //计算运费, 先粗略限制,判定sendCost不能为空, 后续应该重新再算运费
- // if (!$post['sendCost']) {
- // util::fail("请确认运费");
- // }
- }
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
- $currentShopId = $ghsInfo['shopId'] ?? 0;
- $post['sjId'] = $this->sjId;
- $post['shopId'] = $this->shopId;
- $post['shopAdminId'] = $this->shopAdminId;
- $post['ghsId'] = $ghsId;
- $post['ghsName'] = $ghsInfo['name'] ?? '';
- $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
- $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
- $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
- $customId = $ghsInfo['customId'] ?? 0;
- $post['customId'] = $customId;
- $shopAdmin = $this->shopAdmin->attributes;
- $name = $shopAdmin['name'] ?? '';
- $post['shopAdminName'] = $name;
- $product = $post['product'] ?? '';
- $productList = json_decode($product, true);
- //判断product数据是不是同个供应商的
- ProductClass::valid($productList, $this->mainId);
- $post['product'] = $productList;
- $post['sendCost'] = isset($post['sendCost']) && is_numeric($post['sendCost']) ? $post['sendCost'] : 0;
- $respond = PurchaseService::createPurchase($post, $ghsInfo);
- $transaction->commit();
- util::success($respond);
- } catch (Exception $e) {
- $transaction->rollBack();
- util::fail();
- }
- }
- //延期支付 ssh 2021.1.24
- public function actionDebtPay()
- {
- $get = Yii::$app->request->get();
- $orderSn = $get['orderSn'] ?? 0;
- $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
- PurchaseService::valid($info, $this->shopId);
- $ghsId = $info->ghsId;
- $ghs = GhsClass::getGhsInfo($ghsId);
- $customId = $ghs['customId'] ?? 0;
- $custom = CustomClass::getCustom($customId);
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
- util::fail('您没有延期付款权限');
- }
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
- //延期支付
- PurchaseService::debt($info);
- $transaction->commit();
- //app新订单通知
- $saleId = $info->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- NoticeClass::ghsNewOrderNotice($order);
- } catch (Exception $e) {
- $transaction->rollBack();
- util::fail('支付失败');
- }
- util::success($info->attributes);
- }
- //余额支付 ssh 2021.1.24
- public function actionBalancePay()
- {
- $shopAdmin = $this->shopAdmin;
- if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
- util::fail('超管才能操作');
- }
- $get = Yii::$app->request->get();
- $orderSn = $get['orderSn'] ?? 0;
- $password = $get['password'] ?? '';
- // 重新获取,不加密的用户数据
- $admin = AdminClass::getById($this->adminId, true);
- if (password_verify($password, $admin->payPassword) == false) {
- util::fail('密码错误');
- }
- $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
- if (empty($info)) {
- util::fail('没有找到采购单');
- }
- PurchaseService::valid($info, $this->shopId);
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
- //余额支付
- purchaseService::balancePay($info);
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- util::fail('支付失败');
- }
- util::success($info->attributes);
- }
- //采购记录 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;
- }
- $respond = PurchaseService::getPurchaseList($where);
- $respond['shop'] = $this->shop->attributes;
- util::success($respond);
- }
- //采购详情 ssh 2021.01.25
- public function actionDetail()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $info = PurchaseService::getInfo($id, true, true);
- if (isset($info['shopId']) == false || $info['shopId'] != $this->shopId) {
- util::fail('没有权限访问');
- }
- $customId = $info['customId'] ?? 0;
- $custom = CustomClass::getById($customId);
- $hasDebtPay = $custom['debt'] ?? 0;
- $info['hasDebtPay'] = $hasDebtPay;
- $info['balance'] = $this->shop->balance ?? 0;
- 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()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $info = GhsClass::getGhsInfo($id);
- GhsClass::valid($info, $this->shopId);
- $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
- $searchTime = $get['searchTime'] ?? '';
- if (!empty($searchTime)) {
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
- $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
- }
- $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], true);
- if (empty($order)) {
- util::fail('订单号无效');
- }
- $id = $order->id;
- $order->changePrice = 2;
- $order->save();
- $deadline = $order->deadline;
- $current = date("Y-m-d H:i:s");
- if ((strtotime($deadline) + 20) < strtotime($current)) {
- util::fail('订单已经失效');
- }
- $name = $orderSn;
- $totalFee = $order->realPrice;
- //强制使用小程序的miniOpenId
- $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
- if (empty($openId)) {
- util::fail('没有openId');
- }
- $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);
- $newParams['id'] = $id;
- util::success($newParams);
- }
- //运费计算 linqh 2021.4.12 暂时返回固定值
- public function actionFreight()
- {
- //linqh 2021.6.25 运费固定返回0
- util::success(['sedCost' => 0]);
- $post = Yii::$app->request->post();
- $ghsId = $post['ghsId'] ?? 0;
- //供应商
- $ghsInfo = GhsClass::getById($ghsId);
- if (empty($ghsInfo)) {
- util::fail('没有找到供应商');
- }
- //花材信息
- $productJson = $post['product'] ?? '';
- // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
- if (empty($productJson)) {
- util::fail('请选择花材');
- }
- $productList = json_decode($productJson, true);
- if (empty($productList)) {
- util::fail('请选择花材');
- }
- $productList = PurchaseClass::mergeItemInfo($productList);
- $weight = 0;
- $price = 0;
- $productData = ProductClass::getProductMapData($productList);
- $bigNum = 0;
- foreach ($productList as $key => $val) {
- $productId = $val['productId'];
- $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
- $bigNum += $val['bigNum'];
- $weight = bcadd($w, $weight, 2);
- $ratio = $productData[$productId]['ratio'] ?? 0;
- //大小数量合并多少扎
- $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
- //售卖价格
- $itemPrice = $productData[$productId]['price'] ?? 0;
- //合计价格
- $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
- }
- $ghsShopId = $ghsInfo['shopId'] ?? 0;
- $ghsSjId = $ghsInfo['sjId'] ?? 0;
- $sendTime = $post['sendTime'] ?? '';
- if (!empty($sendTime)) {
- //配送时间不为空
- // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
- $sendTimeInt = strtotime($sendTime);
- $now = time();
- $diff = $sendTimeInt - $now;
- //因服务器写入时间,改为至少提前6分钟
- if ($diff <= 360) {
- util::fail('配送时间至少提前6分钟');
- }
- $sendTime = date('Y-m-d H:i', strtotime($sendTime));
- } else {
- $sendTime = date('Y-m-d H:i', strtotime("+30 minute"));
- }
- //找供应商的客户信息,用里面的配送相关地址信息
- $customId = $ghsInfo['customId'] ?? 0;
- $customInfo = CustomClass::getById($customId);
- if (empty($customInfo)) {
- util::fail('没有找到客户');
- }
- //判断商家是否开启达达,若未开启,则使用自定义运费计算
- $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
- if (!$shopNo) {
- //使用自定义运费
- //util::fail('供应商暂未开通配送');
- $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight);
- util::success(['sedCost' => $cost]);
- }
- //组装订单信息
- $info = [];
- //发送放的商家信息,即供应商的相关信息
- $info['sjId'] = $ghsSjId;
- $info['shopId'] = $ghsShopId;
- $info['actPrice'] = $price;//价格
- $info['weight'] = $weight;
- $info['orderSn'] = \common\components\orderSn::getGhsOrderSn();
- $info['bigNum'] = $bigNum;
- //配送地址,当前零售端的门店地址
- $info['customName'] = $customInfo['name'] ?? '';
- $info['customMobile'] = $customInfo['mobile'] ?? '';
- $info['province'] = $customInfo['province'] ?? '';
- $info['city'] = $customInfo['city'] ?? '';;
- $info['address'] = $customInfo['address'] ?? '';;
- $info['floor'] = $customInfo['floor'] ?? '';;
- $info['fullAddress'] = $customInfo['fullAddress'] ?? '';;
- $info['lat'] = $customInfo['lat'] ?? '';;
- $info['long'] = $customInfo['long'] ?? '';;
- $info['sendTime'] = $sendTime;
- $res = DadaExpressServices::queryDeliverFee($info);
- util::success(['sedCost' => $res['fee']]);
- }
- }
|