| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830 |
- <?php
- namespace hd\controllers;
- use biz\admin\classes\AdminClass;
- use biz\ghs\classes\GhsClass;
- use biz\notice\classes\NoticeClass;
- use biz\shop\classes\ShopClass;
- 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\classes\PurchaseItemClass;
- 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 common\components\imgUtil;
- use common\components\noticeUtil;
- use Yii;
- use common\components\util;
- use biz\wx\classes\WxMessageClass;
- use bizGhs\order\services\OrderService;
- class PurchaseController extends BaseController
- {
- public $guestAccess = ['detail'];
- public function actionGetFullInfo()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $info = PurchaseClass::getById($id);
- if (empty($info)) {
- util::fail('没有找到采购单');
- }
- if (isset($info->mainId) == false || $info->mainId != $this->mainId) {
- util::fail('没有权限访问');
- }
- $orderSn = $info->orderSn ?? '';
- $list = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- if (!empty($list)) {
- foreach ($list as $key => $item) {
- $shortCover = $item['cover'] ?? '';
- $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
- $list[$key]['cover'] = $cover;
- $list[$key]['bigCover'] = $bigCover;
- $list[$key]['shortCover'] = $shortCover;
- }
- }
- util::success(['list' => $list, 'info' => $info]);
- }
- //虚拟供货商的采购 ssh 20230308
- public function actionAddVirtualCg()
- {
- $post = Yii::$app->request->post();
- $ghsId = $post['ghsId'] ?? 0;
- $payMode = $post['payMode'] ?? 0;
- $payWay = $post['payWay'] ?? 0;
- $ghsInfo = GhsClass::getById($ghsId);
- if (empty($ghsInfo)) {
- util::fail('没有找到供货商');
- }
- if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
- util::fail('这个是你自己的批发店');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $post['book'] = $post['book'] ?? 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 ?? null;
- $name = $shopAdmin->name ?? '';
- $post['shopAdminName'] = $name;
- $post['mainId'] = $this->mainId;
- $product = $post['product'] ?? '';
- $productList = json_decode($product, true);
- //判断product数据是不是同个供货商的
- $ids = array_unique(array_filter(array_column($productList, 'productId')));
- $productInfoList = ProductClass::getByIds($ids, null, 'id');
- if (!empty($productInfoList)) {
- foreach ($productInfoList as $currentInfo) {
- if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $this->mainId) {
- util::fail('只能选择同一家的商品哦');
- }
- }
- if (count($productInfoList) != count($ids)) {
- util::fail('存在无效商品');
- }
- } else {
- util::fail('花材不存在');
- }
- //供货商预订单最低公斤数要求和每公斤服务费
- $ghsInfo['kiloFee'] = 0;
- $ghsInfo['miniKilo'] = 0;
- $post['product'] = $productList;
- $packCost = 0;
- $post['packCost'] = $packCost;
- $post['sendCost'] = isset($post['sendCost']) && $post['sendCost'] > 0 ? $post['sendCost'] : 0;
- $respond = PurchaseService::createPurchase($post, $ghsInfo);
- if ($payMode == 0) {
- //欠款
- PurchaseService::debt($respond);
- } else {
- //线下支付
- PurchaseService::payAfter($respond, $payWay);
- $saleId = $respond->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- OrderService::payAfter($order, $payWay, true);
- }
- $transaction->commit();
- util::success($respond);
- } catch (Exception $e) {
- $transaction->rollBack();
- util::fail();
- }
- }
- //生成采购单 ssh 2021.1.17
- public function actionCreateOrder()
- {
- $post = Yii::$app->request->post();
- $shop = $this->shop;
- $pfShopId = $shop->pfShopId ?? 0;
- $mainId = $shop->mainId ?? 0;
- if ($mainId != 1553 && $mainId != 52) {
- //除了花样年华、惠雅鲜花、天天鲜花和美天鲜花,其他人有开批发,暂不支持直接向别人采购
- if (!empty($pfShopId)) {
- util::fail('你有开批发店,暂不支持直接向别人采购');
- }
- }
- //多颜色数据整理
- $colorItem = $post['xj'] ?? [];
- $newXj = [];
- if (!empty($colorItem)) {
- $colorData = json_decode($colorItem, true);
- if (!empty($colorData) && is_array($colorData)) {
- foreach ($colorData as $colorInfo) {
- $ptItemId = $colorInfo['ptItemId'] ?? 0;
- $colorList = $colorInfo['list'] ?? [];
- if (!empty($colorList)) {
- foreach ($colorList as $colorItemKey => $colorItem) {
- $newXj[$ptItemId][] = $colorItem;
- }
- }
- }
- }
- }
- $post['xj'] = $newXj;
- $ghsId = $post['ghsId'] ?? 0;
- //供货商
- $ghsInfo = GhsClass::getById($ghsId);
- if (empty($ghsInfo)) {
- util::fail('没有找到供货商');
- }
- $ghsShopId = $ghsInfo['shopId'] ?? 0;
- if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
- util::fail('这个是你自己的批发店');
- }
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
- $post['book'] = $post['book'] ?? 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 ?? null;
- $name = $shopAdmin->name ?? '';
- $post['shopAdminName'] = $name;
- $post['mainId'] = $this->mainId;
- $product = $post['product'] ?? '';
- $productList = json_decode($product, true);
- //判断product数据是不是同个供货商的
- $ghsShopInfo = ShopClass::getById($ghsShopId, true);
- $ghsMainId = $ghsShopInfo->mainId ?? 0;
- $isOpen = ShopClass::isOpen($ghsShopInfo);
- $book = $post['book'] ?? 0;
- if ($isOpen == 0 && $book == 0) {
- util::fail('本店已休息,请选择其它门店');
- }
- if ($book == 1) {
- $sendTimeWant = isset($post['sendTimeWant']) && !empty($post['sendTimeWant']) ? $post['sendTimeWant'] : date("Y-m-d");
- $future = date("Y-m-d", strtotime('+2 day'));
- if (strtotime($sendTimeWant) < strtotime($future)) {
- //util::fail('请选择时间');
- }
- $nowTime = strtotime(date("Y-m-d"));
- if (strtotime($sendTimeWant) < $nowTime) {
- util::fail('不能选择过去时间');
- }
- }
- $ids = array_unique(array_filter(array_column($productList, 'productId')));
- $productInfoList = ProductClass::getByIds($ids, null, 'id');
- if (!empty($productInfoList)) {
- $presellData = [];
- $nowTime = strtotime(date("Y-m-d"));
- foreach ($productInfoList as $currentInfo) {
- if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $ghsMainId) {
- util::fail('只能选择同一家的商品哦');
- }
- $currentName = $currentInfo['name'] ?? '';
- $presell = $currentInfo['presell'] ?? 0;
- $presellData[] = $presell;
- if ($presell == 1) {
- if ($book == 1) {
- util::fail('预订时不能选择预售花材');
- } else {
- if (isset($post['sendTimeWant']) == false || empty($post['sendTimeWant'])) {
- util::fail('请选择配送日期');
- }
- $sendTimeWant = $post['sendTimeWant'];
- if (strtotime($sendTimeWant) < $nowTime) {
- util::fail('不能选择过去时间');
- }
- $hasDateStr = $currentInfo['presellDate'] ?? [];
- $hasDate = explode(',', trim($hasDateStr));
- if (empty($hasDate)) {
- util::fail('预售日期有问题,请提醒门店');
- }
- if (in_array(strtotime($sendTimeWant), $hasDate) == false) {
- util::fail("有预售花材在{$sendTimeWant}没到货");
- }
- $post['presell'] = 1;
- }
- }
- }
- if (count($productInfoList) != count($ids)) {
- util::fail('存在无效商品');
- }
- $presellData = array_unique($presellData);
- if (count($presellData) > 1) {
- util::fail('预售和非预售花材不能一起下单');
- }
- } else {
- util::fail('花材不存在');
- }
- //供货商预订单最低公斤数要求和每公斤服务费
- $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0;
- $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0;
- $post['product'] = $productList;
- $sendType = $post['sendType'] ?? 0;
- $transType = $post['transType'] ?? 0;
- if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
- //昆明发货包装费和运费的计算
- $totalWeight = 0;
- $totalItemNum = 0;
- foreach ($productList as $itemData) {
- $bigNum = $itemData['bigNum'] ?? 0;
- $thisWeight = $itemData['weight'] ?? 0;
- $currentWeight = bcmul($thisWeight, $bigNum, 2);
- $totalWeight = bcadd($currentWeight, $totalWeight, 2);
- $totalItemNum = bcadd($totalItemNum, $bigNum, 2);
- }
- $totalItemNum = floatval($totalItemNum);
- $kmPackCost = dict::getDict('kmPackCost');
- $packCost = 0;
- if (!empty($kmPackCost)) {
- foreach ($kmPackCost as $itemPack) {
- $kiloNum = $itemPack['num'] ?? 0;
- if ($kiloNum >= $totalWeight) {
- $packCost = $itemPack['amount'] ?? 0;
- break;
- }
- }
- }
- $post['packCost'] = $packCost;
- $transCost = dict::getDict('transCost');
- $sendCost = 0;
- if (!empty($transCost)) {
- foreach ($transCost as $trans) {
- $sign = $trans['sign'] ?? 0;
- if ($sign == $transType) {
- if ($sign == 2) {
- //冷链物流
- $llOption = $trans['option'] ?? [];
- if (!empty($llOption)) {
- $largeInfo = end($llOption);
- $largeNum = $largeInfo['num'] ?? 0;
- $largeAmount = $largeInfo['amount'] ?? 0;
- $bs = intval($totalItemNum / $largeNum);
- if ($bs > 0) {
- $addSendCost = bcmul($bs, $largeAmount, 2);
- $sendCost = bcadd($sendCost, $addSendCost, 2);
- $subNum = bcmul($bs, $largeNum, 2);
- $subNum = floatval($subNum);
- $totalItemNum = bcsub($totalItemNum, $subNum, 2);
- $totalItemNum = floatval($totalItemNum);
- }
- foreach ($llOption as $miniItem) {
- if ($miniItem['num'] >= $totalItemNum) {
- $sendCost = bcadd($sendCost, $miniItem['amount'], 2);
- break;
- }
- }
- }
- } else {
- //其它物流
- $perKiloCost = $trans['perKiloCost'] ?? 0;
- $sendCost = bcmul($perKiloCost, $totalWeight, 3);
- $sendCost = round($sendCost, 2);
- }
- }
- }
- }
- $post['sendCost'] = $sendCost;
- //标记为昆明到地方订单
- $post['localOrder'] = 1;
- } else {
- //同城发货包装费和运费的计算
- $sendCost = 0;
- if ($sendType == dict::getDict('sendType', 'thirdSend')) {
- $weight = 0;
- foreach ($productList as $itemData) {
- $bigNum = $itemData['bigNum'] ?? 0;
- $thisWeight = $itemData['weight'] ?? 0;
- $currentWeight = bcmul($thisWeight, $bigNum, 2);
- $weight = bcadd($currentWeight, $weight, 2);
- }
- $result = PurchaseClass::getDistanceFee($this->shop, $ghsShopInfo, $weight);
- $sendCost = $result['fee'] ?? 0;
- }
- $post['sendCost'] = $sendCost;
- //出车、发快递、发物流可以算包装费
- $packCost = 0;
- $itemTotalAmount = $post['itemTotalAmount'] ?? 0;
- unset($post['itemTotalAmount']);
- if ($sendType == dict::getDict('sendType', 'express')) {
- $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $itemTotalAmount);
- if ($needAdd) {
- $packCost = $ghsShopInfo->packCost ?? 0;
- }
- }
- $post['packCost'] = $packCost;
- }
- $respond = PurchaseService::createPurchase($post, $ghsInfo);
- if ($sendType == 0) {
- if (getenv('YII_ENV') == 'production') {
- //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送
- if ($ghsShopId == 4608) {
- if ($respond->actPrice < 500) {
- $transaction->rollBack();
- util::fail('满500元支持免费配送');
- }
- }
- //叶上花,满500元市区免费配送
- if ($ghsShopId == 4804) {
- if ($respond->actPrice < 500) {
- $transaction->rollBack();
- util::fail('满500元支持免费配送');
- }
- }
- //花悠星 国恋 勇记暂时不支持免费配送
- if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778) {
- util::fail('暂不支持送货上门,请选其它方式');
- }
- }
- }
- $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);
- if ($info->status == 5) {
- util::fail('订单已取消');
- }
- if ($info->status != 1) {
- util::fail('订单不是待付款状态');
- }
- if ($info->book == 1) {
- util::fail('预售不能记欠款');
- }
- $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();
- if (isset($info->sendType) && $info->sendType == 4) {
- //标记当天已收一次包装费,岭南批发市场用的功能
- $current = date("Y_m_d");
- $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
- Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
- }
- //app新订单通知
- $saleId = $info->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- if (!empty($order)) {
- $cgShopId = $info->shopId ?? 0;
- $cgShop = ShopClass::getById($cgShopId, true);
- if (!empty($cgShop)) {
- //采购成功小程序通知
- \bizHd\notice\classes\NoticeClass::cgSuccessMiniNotice($cgShop, $info);
- }
- $shopId = $order->shopId ?? 0;
- $shop = ShopClass::getById($shopId, true);
- if (!empty($shop)) {
- //微信通知供货商
- WxMessageClass::ghsHasNewOrderInform($shop, $order);
- }
- //订单生成时唤起在线打印
- if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
- OrderClass::onlinePrint($order);
- $order->printNum += 1;
- $order->save();
- }
- //花店采购供货商端收到APP通知
- //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('没有找到采购单');
- }
- if ($info->status == 5) {
- util::fail('订单已取消');
- }
- if ($info->status != 1) {
- 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['mainId']) == false || $info['mainId'] != $this->mainId) {
- //不能注释,好像分享付款功能要用,客户没有登录也能付款
- //util::fail('没有权限访问');
- }
- $customId = $info['customId'] ?? 0;
- $custom = CustomClass::getById($customId);
- $customName = $custom['name'] ?? '';
- $customMobile = $custom['mobile'] ?? '';
- $info['customName'] = $customName;
- $info['customMobile'] = $customMobile;
- $hasDebtPay = $custom['debt'] ?? 0;
- $book = $info['book'] ?? 0;
- $presell = $info['presell'] ?? 0;
- //预售不能记欠款
- if ($book == 1) {
- $hasDebtPay = 0;
- }
- if ($presell == 1) {
- $hasDebtPay = 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);
- }
- //微信支付 ssh 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('订单号无效');
- }
- if ($order->status == 5) {
- util::fail('订单已取消');
- }
- if ($order->status != 1) {
- util::fail('订单不是待付款状态');
- }
- $id = $order->id;
- $order->changePrice = 2;
- $order->save();
- $deadline = $order->deadline;
- $current = time();
- if (($current + 50) > strtotime($deadline)) {
- util::fail('订单已失效,请重新下单');
- }
- $name = $orderSn;
- $totalFee = $order->realPrice;
- //强制使用小程序的miniOpenId
- $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
- if (empty($openId)) {
- $currentShopId = $order->shopId ?? 0;
- $currentShop = ShopClass::getById($currentShopId, true);
- $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
- $currentMobile = $currentShop->mobile ?? '';
- noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
- util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
- }
- $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);
- }
- //计算运费 ssh 20221003
- public function actionFreight()
- {
- $get = Yii::$app->request->get();
- $shop = $this->shop;
- $shopLat = isset($shop->lat) ? $shop->lat : '';
- $shopLong = isset($shop->long) ? $shop->long : '';
- if (empty($shopLat) || empty($shopLong)) {
- util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
- }
- $ghsId = $get['ghsId'] ?? 0;
- $ghs = GhsClass::getById($ghsId, true);
- if (empty($ghs)) {
- util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
- }
- $ghsShopId = $ghs->shopId ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (empty($ghsShop)) {
- util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
- }
- $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : '';
- $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : '';
- if (empty($ghsShopLat) || empty($ghsShopLong)) {
- util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
- }
- $weight = $get['weight'] ?? 0;
- if (empty($weight)) {
- util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
- }
- $respond = PurchaseClass::getDistanceFee($this->shop, $ghsShop, $weight);
- util::success($respond);
- }
- //运费计算 lqh 2021.4.12 暂时返回固定值
- public function actionFreight2()
- {
- //lqh 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']]);
- }
- }
|