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]); } } } } else { util::fail('花材不存在'); } //供货商预订单最低公斤数要求和每公斤服务费 $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0; $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0; $post['product'] = $productList; // xhShMethod 门店下单前保留花材合计,供后续 packCost 计算(pfLevel==0 分支会 unset itemTotalAmount) $shMethodItemAmount = $post['itemTotalAmount'] ?? 0; $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, $sendType); 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, $sendType); if ($needAdd == false) { $sendCost = 0; } $post['sendCost'] = $sendCost; //标记为昆明到地方订单 $post['localOrder'] = 1; } else { // xhShMethod 门店:下单前按配置写入不满最低消费时的附加包装费/运费,关键词 sh_method_area $newShMethod = dict::getNewMethodShop(); if (in_array($ghsShopId, $newShMethod)) { $shCurrentConfig = shMethodClass::getConfig($ghsShopId, $ghsMainId, $sendType); $shCurrentName = $shCurrentConfig['name'] ?? ''; if (isset($shCurrentConfig['status']) && $shCurrentConfig['status'] == 0) { util::fail($shCurrentName . '已停用'); } if ($sendType == dict::getDict('sendType', 'thirdSend')) { //如果是发跑腿 // 生成随机订单号,不要跟原来的订单号混起来,跑腿-销售单- $prefix = 'PT-XSD-' . $this->mainId . '-'; $orderSn = $prefix . round(microtime(true) * 1000); $calcType = $shCurrentConfig['calcType'] ?? 0; if ($calcType == 1) { //使用自定义计算费用 $startKm = $shCurrentConfig['startKm'] ?? 0; $startPrice = $shCurrentConfig['startPrice'] ?? 0; $perKmPrice = $shCurrentConfig['perKmPrice'] ?? 0;//超出起步,每公里加收金额 $changeRate = $shCurrentConfig['changeRate'] ?? 0;//负值时为降价百分比,正值时为涨价百分比 if ($startKm <= 0 || $startPrice <= 0 || $perKmPrice <= 0) { util::fail('算运费失败,请更换配送方式'); } //计算客户到店的距离,骑电动车 $distanceRespond = mapUtil::calcShopDistanceByEleBike($custom, $ghsShopInfo); $sendDistance = $distanceRespond['distanceKm'] ?? 0; if ($sendDistance <= 0) { util::fail('算运费失败,距离是0,请选到店自取'); } // 计算运费逻辑 $sendCost = $startPrice; if ($sendDistance > $startKm) { $extraDistance = bcsub($sendDistance, $startKm, 2); $extraCost = bcmul($extraDistance, $perKmPrice, 2); $sendCost = bcadd($sendCost, $extraCost, 2); } if ($changeRate != 0) { // 临时涨价/降价百分比 $rate = bcadd(1, bcdiv($changeRate, 100, 4), 4); $sendCost = bcmul($sendCost, $rate, 2); } $post['sendCost'] = bcadd($post['sendCost'] ?? 0, $sendCost, 2); $post['sendDistance'] = $sendDistance; } else { // 使用 DeliveryQuoteUtil 获取配送报价 try { $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, //当前配送方式 'shConfig' => $shCurrentConfig, ]); $sendCost = $quoteResult['sendCost']; $sendDistance = $quoteResult['sendDistance']; $post['sendCost'] = bcadd($post['sendCost'] ?? 0, $sendCost, 2); $post['sendDistance'] = $sendDistance; } catch (\Exception $e) { noticeUtil::push("计算跑腿费用出错了:" . $e->getMessage(), '15280215347'); util::fail('下单失败,请选其他配送方式'); } } } else { //如果是不是发跑腿,比如送货上门和发快递,则可能要收运费或打包费任一 $shMethodBigNum = 0; foreach ($productList as $itemData) { $shMethodBigNum += floatval($itemData['bigNum'] ?? 0); } $additionalCosts = ShMethodClass::calcFee( $shCurrentConfig, $sendType, $shMethodItemAmount, $shMethodBigNum, $customId, $ghsInfo ); // 如果产生了附加运费,与前面可能产生的 sendCost 进行累加或覆盖 if ($additionalCosts['sendCost'] > 0) { $post['sendCost'] = bcadd($post['sendCost'] ?? 0, $additionalCosts['sendCost'], 2); } // 如果产生了附加包装费,覆盖前面赋值的 packCost。 if ($additionalCosts['packCost'] > 0) { $post['packCost'] = $additionalCosts['packCost']; } } } 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) { noticeUtil::push("计算跑腿费用出错了:" . $e->getMessage(), '15280215347'); util::fail('下单失败,请选其他配送方式'); } } } $post['sendCost'] = $sendCost; $post['sendDistance'] = $sendDistance; // 出车(0)、发快递(4)、发物流可以算包装费 $packCost = 0; if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) { noticeUtil::push("走的pfLevel=0", '15280215347'); } $itemTotalAmount = $post['itemTotalAmount'] ?? 0; unset($post['itemTotalAmount']); // sendType == 4 要收包装费 if ($sendType == 4) { $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $sendType, $itemTotalAmount); if ($needAdd) { $packCost = $ghsShopInfo->packCost ?? 0; } } $post['packCost'] = $packCost; } } $respond = PurchaseService::createPurchase($post, $ghsInfo); $newShMethod = dict::getNewMethodShop(); if (in_array($ghsShopId, $newShMethod)) { // 使用 xhShMethod 配置校验配送限制,替代旧版门店硬编码规则,关键词 sh_method_area $shMethodError = ShMethodClass::checkLimit( $ghsShopId, $ghsMainId, $sendType, $respond->actPrice ?? 0, $respond->bigNum ?? 0, $post['packCost'] ?? 0, $post['sendCost'] ?? 0, $post['wlName'] ?? '', $customId, $ghsInfo ); if ($shMethodError !== '') { $transaction->rollBack(); util::fail($shMethodError); } } else { 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('请选同城配送或到店自取'); } } } 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 { //标记当配送方式已收一次包装费/运费,只要有成功单就标记,后续不再重复要求门槛或加收费用 $current = date("Y_m_d"); if (isset($info->sendType)) { $key = 'ghs_custom_today_has_get_pack_cost_' . $info->sendType . '_' . $current . '_' . $customId; Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']); // 兼容快递/送货/跑腿收运费或限制下单的情况 $sendKey = 'ghs_custom_today_has_get_send_cost_' . $info->sendType . '_' . $current . '_' . $customId; Yii::$app->redis->executeCommand('SETEX', [$sendKey, 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('错误的供货商.'); } } $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 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]); } }