| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712 |
- <?php
- namespace hd\controllers;
- use biz\admin\classes\AdminClass;
- use biz\common\classes\GhsNotifyClass;
- use biz\ghs\classes\GhsClass;
- use biz\shop\classes\ShopClass;
- use biz\shop\classes\ShopExtClass;
- use bizGhs\custom\classes\AccountMoneyClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\express\classes\GhsDeliveryOrderClass;
- use bizGhs\order\classes\OrderClass;
- use bizHd\cg\classes\CgMergeSnClass;
- 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\delivery\util\DeliveryQuoteUtil;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use Yii;
- use common\components\util;
- use bizGhs\order\services\OrderService;
- use common\components\lakala\Lakala;
- use common\components\qrCodeUtil;
- use common\components\push;
- class PurchaseController extends BaseController
- {
- public $guestAccess = ['detail', 'ali-pay', 'get-ali-pay-code', 'wx-pay', 'merge-order-list', 'debt-list'];
- //取消采购单 ssh 20250710
- public function actionCancel()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? '';
- $info = PurchaseClass::getById($id, true);
- if (empty($info)) {
- util::fail('没有找到采购单');
- }
- if (!isset($info->mainId) || $info->mainId != $this->mainId) {
- util::fail('访问错误');
- }
- if ($info->status == 5) {
- util::fail('已取消');
- }
- if ($info->status != 1) {
- util::fail('不能取消');
- }
- PurchaseService::expire($info);
- util::complete('已取消');
- }
- //获取支付宝付款码 ssh 20240713
- public function actionGetAliPayCode()
- {
- $get = Yii::$app->request->get();
- $orderSn = $get['orderSn'] ?? '';
- $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
- if (empty($info)) {
- util::fail('没有找到采购单');
- }
- if (!isset($info->mainId) || $info->mainId != $this->mainId) {
- //重要,这个不能限制,因为开单分享给客户付款的,要显示支付宝二维码,限制了会不显示!!!!!! shish 20260326
- //util::fail('无法访问!');
- }
- if (getenv('YII_ENV') == 'production') {
- $url = "https://mall.huahb.cn/#/admin/cg/alipay?orderSn={$orderSn}";
- } else {
- $url = "https://mall.huaml.com/#/admin/cg/alipay?orderSn={$orderSn}";
- }
- $unique = "order_ali_pay_" . $orderSn;
- $imgUrl = qrCodeUtil::generateRechargeQrCode($url, $unique);
- $imageUrl = imgUtil::groupImg($imgUrl);
- util::success(['imgUrl' => $imageUrl]);
- }
- //确认收货 ssh 20231119
- public function actionConfirmTake()
- {
- $get = Yii::$app->request->get();
- $id = $get['id'] ?? 0;
- $info = PurchaseClass::getById($id, true);
- if (empty($info)) {
- util::fail('没有找到采购单');
- }
- if (isset($info->mainId) == false || $info->mainId != $this->mainId) {
- util::fail('无法访问!!');
- }
- PurchaseClass::confirmTake($info);
- util::complete('确认成功');
- }
- //这个接口只能用于
- 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']) || $info['mainId'] != $this->mainId) {
- util::fail('无法访问。。。');
- }
- $orderSn = $info['orderSn'] ?? '';
- $ghsShopId = $info['ghsShopId'] ?? 0;
- if (empty($ghsShopId)) {
- $ghsId = $info['ghsId'] ?? 0;
- if (!empty($ghsId)) {
- $ghs = GhsClass::getById($ghsId, true);
- $ghsShopId = $ghs->shopId ?? 0;
- PurchaseClass::updateById($id, ['ghsShopId' => $ghsShopId]);
- }
- }
- //申请售后 0禁止 1允许
- $afterSale = 1;
- //下单后多少小时内可以售后,0不限制
- $saleHour = 12;
- //是否可以仅退款 1支持 0不支持
- $refundOnlyPrice = 1;
- //售后是否要求上传照片 0不要求 1必须
- $refundMustImg = 0;
- if (!empty($ghsShopId)) {
- $shop = ShopClass::getByCondition(['id' => $ghsShopId], false, false, 'id,afterSale,saleHour,refundOnlyPrice,refundMustImg');
- $afterSale = $shop['afterSale'] ?? 1;
- $saleHour = $shop['saleHour'] ?? 12;
- $refundOnlyPrice = $shop['refundOnlyPrice'] ?? 1;
- $refundMustImg = $shop['refundMustImg'] ?? 0;
- }
- //售后规则
- $info['afterSaleRule'] = ['afterSale' => $afterSale, 'saleHour' => $saleHour, 'refundOnlyPrice' => $refundOnlyPrice, 'refundMustImg' => $refundMustImg];
- $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;
- }
- }
- $info['product'] = $list;
- util::success($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();
- $transactionFinished = false;
- register_shutdown_function(function () use (&$transactionFinished, $transaction) {
- if ($transactionFinished) {
- return;
- }
- ProductClass::rollbackLimitBuySnapshot();
- if ($transaction->isActive) {
- $transaction->rollBack();
- }
- });
- 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();
- $transactionFinished = true;
- ProductClass::clearLimitBuyRollbackSnapshot();
- util::success($respond);
- } catch (\Throwable $e) {
- if ($transaction->isActive) {
- $transaction->rollBack();
- }
- ProductClass::rollbackLimitBuySnapshot();
- $transactionFinished = true;
- util::fail();
- }
- }
- // 批发商的限购花材信息
- public function actionLimitBuyInfo()
- {
- $post = Yii::$app->request->post();
- $ghsId = $post['ghsId'] ?? 0;
- $list = $post['list'] ?? [];
- if (empty($ghsId)) {
- util::fail('缺少供货商');
- }
- if (empty($list) || !is_array($list)) {
- util::fail('请选择花材');
- }
- $ghsInfo = GhsClass::getById($ghsId);
- if (empty($ghsInfo)) {
- util::fail('没有找到供货商');
- }
- $customId = $ghsInfo['customId'] ?? 0;
- if (empty($customId)) {
- util::fail('没有找到客户');
- }
- $respond = ProductClass::getLimitBuyInfoByList($list, $customId);
- util::success($respond);
- }
- //生成采购单 ssh 2021.1.17
- public function actionCreateOrder()
- {
- $post = Yii::$app->request->post();
- $version = $post['version'] ?? 0;
- $direct = $post['direct'] ?? 0;
- //避免重复提交
- $adminId = $this->adminId;
- util::checkRepeatCommit($adminId, 3);
- //多颜色数据整理
- $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 (!empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
- util::fail('不能跟自己的店买花');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- $transactionFinished = false;
- register_shutdown_function(function () use (&$transactionFinished, $transaction) {
- if ($transactionFinished) {
- return;
- }
- ProductClass::rollbackLimitBuySnapshot();
- if ($transaction->isActive) {
- $transaction->rollBack();
- }
- });
- 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;
- $custom = CustomClass::getById($customId, true);
- if (empty($custom)) {
- util::fail('用户信息缺失');
- }
- $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 (isset($post['transType']) && $post['transType'] == 4) {
- if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
- $notSameCity = PurchaseClass::notSameCity($this->shop, $ghsShopInfo);
- if (!$notSameCity) {
- 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('不能选择过去时间');
- }
- }
- //收集客户要下单的花材数量
- $orderNum = [];
- $productCount = 0;
- foreach ($productList as $key => $product) {
- $bigNum = $product['bigNum'] ?? 0;
- $smallNum = $product['smallNum'] ?? 0;
- $pName = $product['name'] ?? '';
- if ($smallNum > 0) {
- util::fail($pName . '不能选小单位');
- }
- $productId = $product['productId'] ?? 0;
- $orderNum[$productId] = ['num' => $bigNum];
- $productCount += $bigNum;
- }
- $lackList = [];
- $changeList = [];
- $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']) || $currentInfo['mainId'] != $ghsMainId) {
- util::fail('只能选一家的商品,请清空重新选。');
- }
- $currentName = $currentInfo['name'] ?? '';
- if (isset($currentInfo['frontHide']) && $currentInfo['frontHide'] == 1) {
- //珑松珠海店,隐藏的商品,分享出去了也要能下单。644 是测试账号的mainId
- if (isset($currentInfo['mainId']) && in_array($currentInfo['mainId'], [14116, 644])) {
- //无操作
- } else {
- util::fail($currentName . ' 没有库存,请删除');
- }
- }
- $presell = $currentInfo['presell'] ?? 0;
- $presellData[] = $presell;
- if ($presell == 1) {
- if ($book == 1) {
- util::fail('预订时不能选择预售花材');
- } else {
- if (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)) {
- util::fail("有预售花材在{$sendTimeWant}没到货");
- }
- $post['presell'] = 1;
- }
- }
- if ($version >= 10) {
- $stock = $currentInfo['stock'] ?? 0;
- $stock = floatval($stock);
- //会有0.5扎和2.5扎问题,所以要转成整数,不然会出严重问题 ssh 20250503
- $stock = intval($stock);
- $productId = $currentInfo['id'] ?? 0;
- $productName = $currentInfo['name'] ?? '';
- $num = $orderNum[$productId] && $orderNum[$productId]['num'] ? $orderNum[$productId]['num'] : 0;
- $num = floatval($num);
- if ($num > $stock) {
- $lackList[] = ['name' => $productName, 'stock' => $stock];
- $changeList[$productId] = $stock;
- }
- }
- }
- if (count($productInfoList) != count($ids)) {
- util::fail('存在无效商品');
- }
- $presellData = array_unique($presellData);
- if (count($presellData) > 1) {
- util::fail('预售和非预售花材不能一起下单');
- }
- if ($version >= 10) {
- if ($direct == 1) {
- //直接更换库存并提交
- $allNoStock = true;
- foreach ($productList as $pKey => $pVal) {
- $productId = $pVal['productId'] ?? 0;
- if (isset($changeList[$productId])) {
- $changeStock = $changeList[$productId];
- $productList[$pKey]['bigNum'] = $changeStock;
- }
- if ($productList[$pKey]['bigNum'] > 0) {
- $allNoStock = false;
- }
- if ($productList[$pKey]['bigNum'] <= 0) {
- unset($productList[$pKey]);
- }
- }
- if ($allNoStock) {
- util::fail('全部没库存了');
- }
- } else {
- //前台提示库存不足
- if (!empty($lackList)) {
- util::success(['lackList' => $lackList, 'hasLackError' => 1]);
- }
- }
- }
- // 已经有了 -- 在 replaceItem()
- // foreach ($productList as $itemData) {
- // $productId = $itemData['productId'] ?? 0;
- // $product = $productInfoList[$productId] ?? [];
- // if (empty($product)) {
- // util::fail('有商品信息没有找到');
- // }
- // $bigNum = $itemData['bigNum'] ?? 0;
- // if (($product['limitBuy'] ?? 0) > 0) {
- // $product['productId'] = $productId;
- // ProductClass::handleLimitBuy($product, $customId, floatval($bigNum));
- // }
- // }
- } 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 ($sendType == dict::getDict('sendType', 'thirdSend')) {
- $shopExt = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,thirdSend,hcMap,hcFreeKm');
- if ($shopExt->thirdSend == 1) {
- util::fail('本店暂不能使用跑腿哦');
- }
- }
- 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 = PurchaseClass::getKmPackCost($ghsMainId);
- $packCost = 0;
- if (!empty($kmPackCost)) {
- foreach ($kmPackCost as $itemPack) {
- $kiloNum = $itemPack['num'] ?? 0;
- if ($kiloNum >= $totalWeight) {
- $packCost = $itemPack['amount'] ?? 0;
- break;
- }
- }
- if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
- noticeUtil::push("总重量" . $totalWeight, '15280215347');
- }
- }
- if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
- noticeUtil::push("走的pfLevel=1", '15280215347');
- }
- //同城配送和到店自取免打包费
- if ($transType == 4 || $transType == 5) {
- $packCost = 0;
- if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
- noticeUtil::push("没有计算打包费原因:transType = 4 =5", '15280215347');
- }
- }
- //如果已经算过一次打包费和运费了,则不再费了
- $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
- if (!$needAdd) {
- $packCost = 0;
- if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
- noticeUtil::push("没有计算打包费原因:已经算过一次了", '15280215347');
- }
- }
- $post['packCost'] = $packCost;
- $transCost = dict::getDict('transCost', null, null, $ghsShopInfo->mainId);
- $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);
- }
- }
- }
- }
- //如果已经算过一次打包费和运费了,则不再费了
- $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
- if ($needAdd == false) {
- $sendCost = 0;
- }
- $post['sendCost'] = $sendCost;
- //标记为昆明到地方订单
- $post['localOrder'] = 1;
- } else {
- //同城发货包装费和运费的计算
- $sendCost = 0;
- $sendDistance = 0;
- //特别注意,这边取供货商的shop信息
- $mapSet = ShopClass::hasIntraCity($ghsShopInfo);
- $openIntraCity = $mapSet['openIntraCity'] ?? 0;
- if ($sendType == dict::getDict('sendType', 'thirdSend')) {
- if ($openIntraCity == 2) {
- util::fail('不能使用跑腿');
- }
- if ($openIntraCity == 1) {
- // 生成随机订单号,不要跟原来的混起来,跑腿-销售单-
- $prefix = 'PT-XSD-' . $this->mainId . '-';
- $orderSn = $prefix . round(microtime(true) * 1000);
- // 使用 DeliveryQuoteUtil 获取配送报价
- try {
- if (empty($post['deliveryPlatform'])) {
- util::fail('请选择跑腿');
- }
- $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
- 'productList' => $productList,
- 'deliveryPlatform' => $post['deliveryPlatform'],
- 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
- 'ghsInfo' => $ghsInfo,
- 'custom' => $custom,
- 'order' => [
- 'orderSn' => $orderSn,
- 'goodsType' => 1, //商品类型:0花束 1花材
- 'itemTotalAmount' => $post['itemTotalAmount'] ?? 0,
- 'remark' => $post['remark'] ?? '',
- ],
- 'mainId' => $this->mainId,
- 'productCount' => $productCount,
- ]);
- $sendCost = $quoteResult['sendCost'];
- $sendDistance = $quoteResult['sendDistance'];
- } catch (\Exception $e) {
- util::fail($e->getMessage());
- }
- }
- }
- $post['sendCost'] = $sendCost;
- $post['sendDistance'] = $sendDistance;
- //出车、发快递、发物流可以算包装费
- $packCost = 0;
- if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
- noticeUtil::push("走的pfLevel=0", '15280215347');
- }
- $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;
- }
- if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
- noticeUtil::push("包装费:" . $packCost, '15280215347');
- }
- $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 || $ghsShopId == 12003 || $ghsShopId == 38128) {
- util::fail('暂不支持送货上门,请选其它配送方式!');
- }
- //广州鑫源花卉,选择送货上门必须选必选商品
- if ($ghsShopId == 12439) {
- $hasMust = array_intersect($ids, [3446945, 3446970, 3446973, 3446979, 3446995, 3447010, 3447023, 3459983, 4333184, 4317745, 1585738]);
- if (empty($hasMust)) {
- util::fail('请返回选择 必选商品');
- }
- }
- if ($ghsShopId == 100883) {
- $hasMust = array_intersect($ids, [5017957,5036067,5036089,5036099,5017958,5036063,5017948,5035980,5036073,5036075,5036076,5036117,5036118,5017949,5035979,5035976,5035928,5035930,5036061,5035879]);
- if (empty($hasMust)) {
- util::fail('请返回选择 必选商品');
- }
- }
- //三明易批花
- if ($ghsShopId == 82200) {
- if ($respond->actPrice < 100 || $respond->bigNum < 15) {
- $hasMust = array_intersect($ids, [4283478]);
- if (empty($hasMust)) {
- util::fail('没满100元且15扎,请返回选运费');
- }
- }
- // 按当天计时,把 sendTimeWant 要限制在两天的时间范围之内
- $future = date("Y-m-d", strtotime('+2 day'));
- if (strtotime($respond->sendTimeWant) > strtotime($future)) {
- util::fail('配送日期只能选最近二天');
- }
- }
- if ($ghsShopId == 77703) {
- //天天鲜花思明店,暂时关闭送货上门
- if ($respond->sendType == 0) {
- //util::fail('节日暂停送货上门,请选到店自取');
- }
- }
- if ($ghsShopId == 86726) {
- //藏花林
- if ($respond->transType != 4 && $respond->transType != 5) {
- util::fail('请选同城配送或到店自取');
- }
- }
- } else {
- //好运鲜花批发,选择送货上门必须选必选商品
- // if ($ghsShopId == 36523) {
- // if ($respond->transType != 4 && $respond->transType != 5) {
- // util::fail('请选同城配送或到店自取');
- // }
- // }
- // if ($ghsShopId == 36523) {
- // if ($respond->actPrice < 50 || $respond->bigNum < 10) {
- // $hasMust = array_intersect($ids, [85295]);
- // if (empty($hasMust)) {
- // util::fail('没满50元且满10扎,请运费必选商品');
- // }
- // }
- // // 按当天计时,把 sendTimeWant 要限制在两天的时间范围之内
- // $future = date("Y-m-d", strtotime('+2 day'));
- // if (strtotime($respond->sendTimeWant) > strtotime($future)) {
- // util::fail('配送日期只能选最近二天');
- // }
- // }
- }
- if (isset($ghsInfo['home'])) {
- if ($ghsInfo['home'] == 0) {
- util::fail('暂不支持送货上门,请选其它配送方式');
- }
- $homeAmount = $ghsInfo['homeAmount'] ? floatval($ghsInfo['homeAmount']) : 0;
- $homeNum = $ghsInfo['homeNum'] ? floatval($ghsInfo['homeNum']) : 0;
- if ($homeAmount > 0 && $homeAmount > $respond->actPrice) {
- util::fail("满{$homeAmount}元 可送货上门");
- }
- if ($homeNum > 0 && $homeNum > $respond->bigNum) {
- util::fail("满{$homeNum}扎 可送货上门");
- }
- }
- }
- if ($sendType == 2) {
- //温州易批花,跑腿,不满100,要选10元的补运费商品
- if ($ghsShopId == 91115) {
- if ($respond->actPrice < 100) {
- $transaction->rollBack();
- $hasMust = array_intersect($ids, [4449812, 4449811]);
- if (empty($hasMust)) {
- util::fail('不满100,必选商品 补运费10元');
- }
- }
- }
- }
- if ($sendType == 4) {
- //温州易批花,快递,不满100,要选10元的补运费商品
- if ($ghsShopId == 91115) {
- if ($respond->actPrice < 100) {
- $transaction->rollBack();
- $hasMust = array_intersect($ids, [4449812, 4449811]);
- if (empty($hasMust)) {
- util::fail('不满100,必选商品 补运费10元');
- }
- }
- }
- }
- if ($sendType == 3) {
- //温州易批花,物流,不满200,要选10元的补运费商品
- if ($ghsShopId == 91115) {
- if ($respond->actPrice < 200) {
- $transaction->rollBack();
- $hasMust = array_intersect($ids, [4449812, 4449811]);
- if (empty($hasMust)) {
- util::fail('不满200,必选商品 补运费10元');
- }
- }
- }
- }
- $transaction->commit();
- $transactionFinished = true;
- ProductClass::clearLimitBuyRollbackSnapshot();
- util::success($respond);
- } catch (\Exception $e) {
- if ($transaction->isActive) {
- $transaction->rollBack();
- }
- ProductClass::rollbackLimitBuySnapshot();
- $transactionFinished = true;
- Yii::error($e->getMessage());
- util::fail();
- }
- }
- /**
- * 赊账支付
- * 职责:处理采购单延期支付(记欠款/赊账),更新采购单状态,并触发后续的自动收货、通知、在线打印、跑腿呼叫等业务流
- * 入参:GET 传参 orderSn (采购单号)
- * 返回:JSON 格式的采购单属性
- * 副作用:更新采购单、订单状态,增减花材库存并记录库存流水,更新客户及供货商的最后下单时间,发送外部通知和打印请求
- * 关键边界:
- * 1. 校验采购单及客户的赊账权限
- * 2. 优化:复用采购单实例,避免重复查询
- * 3. 优化:将自动确认收货(confirmTake)包裹在独立事务中,合并写操作,减少磁盘 I/O
- * 4. 优化:合并打印逻辑中的 ShopExt 查询及订单保存,减少数据库交互
- * 5. 优化:使用 updateByCondition 直接更新最后下单时间,避免先 SELECT 再 UPDATE
- */
- public function actionDebtPay()
- {
- $get = Yii::$app->request->get();
- $orderSn = $get['orderSn'] ?? 0;
- $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
- PurchaseService::valid($info, $this->shopId);
- //解决重复和并发提交
- $cacheKey = 'hd_debt_pay_' . $orderSn;
- $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- if (!empty($has)) {
- util::fail('请稍等...');
- }
- Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
- 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('请先申请赊账权限');
- }
- if (getenv('YII_ENV') == 'production') {
- //源花汇不允许客户自己下欠款单
- if ($ghs['mainId'] == 10536) {
- util::fail('暂未开通');
- }
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- //延期支付
- PurchaseService::debt($info);
- $transaction->commit();
- if (isset($info->localOrder) && $info->localOrder == 1) {
- if ($info->transType != 5 && $info->transType != 4) {
- //昆明发货,客户算了一次打包费和运费,第二次就不再算了
- $current = date("Y_m_d");
- $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
- Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
- }
- } else {
- 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']);
- }
- }
- // 复杂分支/关键逻辑:不是预订单,且供货商配置为直接完成流程,则自动确认收货。
- // 优化:复用已有的 $info 实例,避免重复查询;同时将自动确认收货包裹在独立事务中,合并批量写操作,减少磁盘 I/O。
- if (!empty($info)) {
- if (isset($info->book) && $info->book == 0) {
- $ghsShopId = $ghs['shopId'] ?? 0;
- $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
- if (!empty($ext) && intval($ext->orderFlow ?? 0) === 0) {
- $takeTransaction = Yii::$app->db->beginTransaction();
- try {
- PurchaseClass::confirmTake($info);
- $takeTransaction->commit();
- } catch (\Exception $takeEx) {
- $takeTransaction->rollBack();
- Yii::error("自动确认收货失败: " . $takeEx->getMessage());
- throw $takeEx;
- }
- }
- }
- }
- //app新订单通知
- $saleId = $info->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- if (!empty($order)) {
- $shopId = $order->shopId ?? 0;
- $shop = ShopClass::getById($shopId, true);
- if (!empty($shop)) {
- //通知供货商
- GhsNotifyClass::newOrderNotify($saleId);
- }
- //订单生成时唤起在线打印
- if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
- //有几个地方要同步去修改
- if (getenv('YII_ENV') == 'production') {
- //源花汇、盛丰不自动打小票,关键词mall_order_no_auto_print
- $mallOrderNoPrint = [10536, 44282, 26374];
- } else {
- $mallOrderNoPrint = [];
- }
- if (!in_array($order->mainId, $mallOrderNoPrint)) {
- // 复杂分支/关键逻辑:优化:只查询一次 $ext,合并 $order->save(),减少数据库交互
- $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
- $hasPrintSn = isset($ext->printSn) && !empty($ext->printSn);
- OrderClass::onlinePrint($order);
- if ($hasPrintSn) {
- $order->printNum += 1;
- }
- //花镜 打二次小票,有多处要修改搜索关键词tow_print
- if (in_array($order->mainId, [76796])) {
- OrderClass::onlinePrint($order);
- if ($hasPrintSn) {
- $order->printNum += 1;
- }
- }
- if ($hasPrintSn) {
- $order->save();
- }
- }
- }
- //付款成功之后呼叫跑腿,关键词 pay_after_call_pt,多处要同步修改
- GhsDeliveryOrderClass::payAfter($order);
- // 复杂分支/关键逻辑:优化:使用 updateByCondition 直接更新最后下单时间,避免先 SELECT 再 UPDATE 的性能开销
- $customId = $order->customId ?? 0;
- $ghsId = $order->ghsId ?? 0;
- $date = date("Y-m-d H:i:s");
- if ($customId > 0) {
- CustomClass::updateByCondition(['id' => $customId], ['recentExpend' => $date]);
- }
- if ($ghsId > 0) {
- GhsClass::updateByCondition(['id' => $ghsId], ['recentExpend' => $date]);
- }
- }
- } catch (\Exception $e) {
- $transaction->rollBack();
- Yii::error("赊账支付失败原因:" . $e->getMessage());
- util::fail('支付失败');
- }
- util::success($info->attributes);
- }
- /**
- * 采购待支付页:客户 xhGhsCustom 净余额支付(并发重试 + 行锁)。
- */
- public function actionCustomBalancePay()
- {
- $post = Yii::$app->request->post();
- $orderSn = $post['orderSn'] ?? '';
- if (empty($orderSn)) {
- util::fail('订单号无效');
- }
- $cacheKey = 'hd_custom_balance_pay_' . $orderSn;
- $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- if (!empty($has)) {
- util::fail('请稍等...');
- }
- Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 8, 'has']);
- $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
- if (empty($info)) {
- util::fail('没有找到采购单');
- }
- PurchaseService::valid($info, $this->shopId);
- $deadline = $info->deadline ?? '';
- if (!empty($deadline) && time() > strtotime($deadline)) {
- util::fail('订单已失效了哦');
- }
- $ghsId = intval($info->ghsId ?? 0);
- $ghs = GhsClass::getGhsInfo($ghsId);
- try {
- $result = util::runWithDbConcurrencyRetry(function () use ($orderSn) {
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $purchase = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
- $respond = PurchaseService::payByGhsCustomBalance($purchase);
- $transaction->commit();
- return $respond;
- } catch (\Exception $exception) {
- if ($transaction->isActive) {
- $transaction->rollBack();
- }
- throw $exception;
- }
- });
- $info = $result['purchase'];
- $order = $result['saleOrder'] ?? null;
- if (!empty($order)) {
- if (isset($info->book) && $info->book == 0 && !empty($ghs)) {
- $ghsShopId = $ghs['shopId'] ?? 0;
- $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
- if (!empty($ext) && intval($ext->orderFlow ?? 0) === 0) {
- $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
- if (!empty($cg)) {
- PurchaseClass::confirmTake($cg);
- }
- }
- }
- $shopId = $order->shopId ?? 0;
- $shop = ShopClass::getById($shopId, true);
- if (!empty($shop)) {
- GhsNotifyClass::newOrderNotify($order->id ?? 0);
- }
- if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
- OrderClass::onlinePrint($order);
- }
- GhsDeliveryOrderClass::payAfter($order);
- $customId = $order->customId ?? 0;
- $custom = CustomClass::getById($customId, true);
- $date = date('Y-m-d H:i:s');
- if (!empty($custom)) {
- $custom->recentExpend = $date;
- $custom->save();
- }
- $orderGhsId = $order->ghsId ?? 0;
- $orderGhs = GhsClass::getById($orderGhsId, true);
- if (!empty($orderGhs)) {
- $orderGhs->recentExpend = $date;
- $orderGhs->save();
- }
- }
- util::success($info->attributes);
- } catch (\Exception $e) {
- Yii::info('客户余额支付失败:' . $e->getMessage());
- util::fail($e->getMessage() ?: '支付失败');
- }
- }
- //余额支付 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];
- $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']]];
- }
- $ghsId = $get['ghsId'] ?? 0;
- if (!empty($ghsId)) {
- $where['ghsId'] = (int)$ghsId;
- }
- $status = $get['status'] ?? 0;
- if (!empty($status)) {
- $where['status'] = $status;
- }
- $respond = PurchaseService::getPurchaseList($where);
- $respond['shop'] = $this->shop->attributes;
- util::success($respond);
- }
- //获取合并采购单列表 ssh 20260519
- public function actionMergeOrderList()
- {
- Yii::$app->params['pageSize'] = 9999;
- $get = Yii::$app->request->get();
- $mergeId = $get['mergeId'] ?? 0;
- if (empty($mergeId)) {
- util::fail('请选择采购单');
- }
- $mergeSn = CgMergeSnClass::getById($mergeId, true);
- if (empty($mergeSn)) {
- util::fail('请选择合并采购单');
- }
- $salt = $get['salt'] ?? '';
- if (!empty($salt)) {
- if (empty($mergeSn->salt) || $mergeSn->salt != $salt) {
- util::fail('此单无法查看哦');
- }
- } else {
- if (empty($this->shopId)) {
- util::fail('暂时不能查看');
- }
- if ($mergeSn->mainId != $this->mainId) {
- util::fail('此单无法查看哈');
- }
- }
- $ghsId = $mergeSn->ghsId ?? 0;
- $where = [
- 'ghsId' => $ghsId,
- 'mergeId' => $mergeId,
- ];
- $respond = PurchaseService::getPurchaseList($where);
- $totalPrice = 0;
- if (!empty($respond['list'])) {
- foreach ($respond['list'] as $one) {
- $actPrice = $one['actPrice'] ?? 0;
- $totalPrice = bcadd($totalPrice, $actPrice, 2);
- }
- }
- $totalPrice = floatval($totalPrice);
- $respond['totalPrice'] = $totalPrice;
- $respond['shop'] = !empty($this->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 (empty($info)) {
- util::fail('没有找到采购单');
- }
- $salt = $get['salt'] ?? '';
- if (!empty($salt)) {
- if (!isset($info['salt']) || $info['salt'] != $salt) {
- util::fail('无效采购单呢!');
- }
- } else {
- if (!isset($info['mainId']) || $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;
- }
- $shop = $this->shop;
- $info['pfShopId'] = $shop->pfShopId ?? 0;
- $info['hasDebtPay'] = $hasDebtPay;
- // 待支付页展示 xhGhsCustom 净余额(正数=可支付余额),不再使用 main.balance
- $info['balance'] = 0;
- $info['customNetBalance'] = '0.00';
- $info['showBalancePay'] = 0;
- $info['ghsSalt'] = '';
- $ghsId = intval($info['ghsId'] ?? 0);
- if ($ghsId > 0) {
- $ghsRow = GhsClass::getById($ghsId, true);
- if (!empty($ghsRow)) {
- $info['ghsSalt'] = $ghsRow->salt ?? '';
- $ghsCustomId = intval($ghsRow->customId ?? 0);
- if ($ghsCustomId > 0) {
- $ghsCustom = CustomClass::getById($ghsCustomId, true);
- if (!empty($ghsCustom)) {
- $net = AccountMoneyClass::getNetBalanceFromRow($ghsCustom);
- $info['customNetBalance'] = $net;
- if (bccomp($net, '0', 2) > 0) {
- $info['balance'] = floatval($net);
- $info['showBalancePay'] = 1;
- }
- }
- }
- }
- }
- $getPayType = dict::getDict('getPayType');
- $info['getPayType'] = $getPayType;
- //获取售后条件
- $ghsId = $info['ghsId'] ?? 0;
- $ghsInfo = GhsClass::getById($ghsId, true);
- if (empty($ghsInfo)) {
- util::fail('没有找到供货商');
- }
- $info['afterSale'] = $ghsInfo->afterSale ?? 1;
- //重要,客户看到的电话修改成门店的联系电话
- $ghsShopId = $ghsInfo->shopId ?? 0;
- $ghsShopInfo = ShopClass::getById($ghsShopId, true);
- $info['ghsMobile'] = $ghsShopInfo->telephone ?? '';
- $info['ghsMobile2'] = $ghsShopInfo->telephone2 ?? '';
- $info['hasRenew'] = 1;
- //如果是快捷开单,则不需要显示累计待结,因为商家经常把快捷开单的订单发给客户结账
- $showForPay = 1;
- if ($ghsShopInfo->skCustomId == $info['customId']) {
- $showForPay = 0;
- }
- $info['showForPay'] = $showForPay;
- 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;
- $salt = $get['salt'] ?? '';
- $info = GhsClass::getGhsInfo($id);
- if (!empty($salt)) {
- if (!isset($info['salt']) || $info['salt'] != $salt) {
- util::fail('供货商出错了');
- }
- } else {
- if (!isset($info['ownShopId']) || $info['ownShopId'] != $this->shopId) {
- util::fail('错误的供货商.');
- }
- }
- //0搜索的addTime 1搜索的payTime
- $searchTimeType = $get['searchTimeType'] ?? 0;
- $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);
- if ($searchTimeType == 0) {
- $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
- }else{
- $where['payTime'] = ['between', [$period['startTime'], $period['endTime']]];
- }
- }
- $respond = PurchaseService::getDebtList($where);
- util::success($respond);
- }
- //支付宝支付 ssh 2021.4.11
- public function actionAliPay()
- {
- ini_set('date.timezone', 'Asia/Shanghai');
- $post = Yii::$app->request->post();
- //默认是支付宝支付
- $payWay = $post['payWay'] ?? 1;
- $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('订单不是待付款状态');
- }
- $deadline = $order->deadline;
- $current = time();
- if (($current + 50) > strtotime($deadline)) {
- util::fail('订单已失效,请重新下单');
- }
- $ghsId = $order->ghsId ?? 0;
- $ghs = GhsClass::getById($ghsId, true);
- if (empty($ghs)) {
- util::fail('没有供货商信息');
- }
- $ghsShopId = $ghs->shopId ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (empty($ghsShop)) {
- util::fail('没有供货商门店信息');
- }
- if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
- util::fail('商家支付功能未开通');
- }
- //避免重复提交
- util::checkRepeatCommit($orderSn, 8);
- //已经唤起过支付了,根据拉卡拉的规则,需要生成新的订单号
- if ($order->changePrice == 2) {
- $oldOrderSn = $orderSn;
- //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $closeParams = [
- 'orderSn' => $oldOrderSn,
- ];
- $response = $laResource->cashClose($closeParams);
- if (isset($response['code']) == false || $response['code'] != '000000') {
- $msg = $response['msg'] ?? '';
- $code = $response['code'] ?? 0;
- if (!in_array($code, ['000091'])) {
- noticeUtil::push('注意花店买花更换单号:' . $oldOrderSn . ',关单没有成功(收银台-支付宝),' . $msg, '15280215347');
- }
- }
- $shopId = $order->shopId ?? 0;
- $mainId = $order->mainId ?? 0;
- $customId = $order->customId ?? 0;
- $ghsId = $order->ghsId ?? 0;
- $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
- $newOrderSn = orderSn::getPurchaseSn($snData);
- $orderSn = $newOrderSn;
- $order->orderSn = $newOrderSn;
- $order->save();
- PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
- }
- $order->changePrice = 2;
- $order->save();
- $ghsShopName = $ghsShop->shopName ?? '';
- //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
- $name = $ghsShopName . " 花材 " . $orderSn;
- $totalFee = $order->realPrice;
- $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
- $ghsId = $order->ghsId ?? 0;
- $ghs = GhsClass::getById($ghsId, true);
- if (empty($ghs)) {
- util::fail('没有供货商信息');
- }
- $ghsShopId = $ghs->shopId ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (empty($ghsShop)) {
- util::fail('没有供货商门店信息');
- }
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklB2BTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
- $wxParams = [
- 'orderSn' => $orderSn,
- 'amount' => $totalFee,
- 'capitalType' => $purchaseCapital,
- 'notifyUrl' => $notifyUrl,
- 'subject' => $name,
- 'payWay' => $payWay,
- ];
- $response = $laResource->cashierPay($wxParams);
- if (isset($response['code']) == false || $response['code'] != '000000') {
- $errMsg = $response['msg'] ?? '';
- noticeUtil::push('编号AA90:' . $errMsg, '15280215347');
- util::fail('发起支付失败');
- }
- $counter_url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
- $hasRenew = $ghsShop->hasRenew ?? 0;
- $new = [
- 'payUrl' => $counter_url,
- 'orderSn' => $orderSn,
- 'hasRenew' => 1,
- ];
- util::success($new);
- }
- //微信支付 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('订单不是待付款状态');
- }
- $deadline = $order->deadline;
- $current = time();
- if (($current + 50) > strtotime($deadline)) {
- util::fail('订单已失效,请重新下单');
- }
- //避免重复提交
- util::checkRepeatCommit($orderSn, 8);
- $ghsId = $order->ghsId ?? 0;
- $ghs = GhsClass::getById($ghsId, true);
- if (empty($ghs)) {
- util::fail('没有供货商信息');
- }
- $ghsShopId = $ghs->shopId ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (empty($ghsShop)) {
- util::fail('没有供货商门店信息');
- }
- if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
- util::fail('商家支付功能未开通');
- }
- //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
- if ($order->changePrice == 2) {
- $oldOrderSn = $orderSn;
- //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $closeParams = [
- 'orderSn' => $oldOrderSn,
- ];
- $response = $laResource->close($closeParams);
- if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
- $msg = $response['msg'] ?? '';
- $code = $response['code'] ?? 0;
- if (!in_array($code, ['BBS11114'])) {
- noticeUtil::push('重点注意,花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . $code, '15280215347');
- util::fail('出错了,请重新下单');
- }
- }
- $shopId = $order->shopId ?? 0;
- $mainId = $order->mainId ?? 0;
- $customId = $order->customId ?? 0;
- $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
- $newOrderSn = orderSn::getPurchaseSn($snData);
- $orderSn = $newOrderSn;
- $order->orderSn = $newOrderSn;
- $order->save();
- PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
- }
- $id = $order->id;
- $order->changePrice = 2;
- $order->save();
- $ghsShopName = $ghsShop->shopName ?? '';
- //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
- $name = $ghsShopName . " 花材 " . $orderSn;
- $totalFee = $order->realPrice;
- $openId = '';
- if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
- $openId = $post['currentMiniOpenId'];
- //noticeUtil::push('花店采购下单时,通过前端获取了openId:' . $openId, '15280215347');
- }
- if (empty($openId)) {
- if (isset($this->admin) && !empty($this->admin)) {
- if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
- $openId = $this->admin->miniOpenId;
- noticeUtil::push('花店采购下单时,通过数据库获取了openId:' . $openId, '15280215347');
- }
- }
- }
- 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');
- //花卉宝代为申请的微信支付
- $merchantExtend = WxOpenClass::getWxInfo();
- if (empty($ghsShop->lklSjNo)) {
- noticeUtil::push('编号31505:单号:' . $orderSn . ' 商家没有开通收款功能:' . $ghsShopName, '15280215347');
- util::fail('商家未开通支付功能');
- }
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $ghsShop->lklSjNo,
- 'term_no' => $ghsShop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
- $wxParams = [
- 'orderSn' => $orderSn,
- 'amount' => $totalFee,
- 'capitalType' => $purchaseCapital,
- 'notifyUrl' => $notifyUrl,
- 'wxAppId' => $merchantExtend['miniAppId'],
- 'openId' => $openId,
- 'subject' => $name,
- 'couponId' => $couponId,
- ];
- $response = $laResource->driveWxPay($wxParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '';
- noticeUtil::push('编号129680931:' . $errMsg . ' 单号:' . $orderSn . ' ' . $ghsShopName, '15280215347');
- util::fail('支付失败');
- }
- $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
- $appId = $newParams['app_id'] ?? '';
- $nonceStr = $newParams['nonce_str'] ?? '';
- $paySign = $newParams['pay_sign'] ?? '';
- $package = $newParams['package'] ?? '';
- $signType = $newParams['sign_type'] ?? '';
- $timeStamp = $newParams['time_stamp'] ?? '';
- $new = [
- 'id' => $id,
- 'appId' => $appId,
- 'nonceStr' => $nonceStr,
- 'paySign' => $paySign,
- 'package' => $package,
- 'signType' => $signType,
- 'timeStamp' => $timeStamp,
- 'orderSn' => $orderSn,
- ];
- util::success($new);
- }
- //计算运费 ssh 20221003
- public function actionFreight()
- {
- $get = Yii::$app->request->get();
- $shop = $this->shop;
- $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]);
- }
- $customId = $ghs->customId ?? 0;
- $custom = CustomClass::getById($customId, true);
- if (empty($custom)) {
- 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($shop, $ghsShop, $weight, $custom);
- util::success($respond);
- }
- //运费计算 lqh 2021.4.12 暂时返回固定值
- public function actionFreight2()
- {
- //lqh 2021.6.25 运费固定返回0
- util::success(['sedCost' => 0]);
- }
- }
|