| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369 |
- <?php
- namespace bizHd\purchase\classes;
- use biz\ghs\classes\GhsClass;
- use biz\product\classes\XjClass;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopClass;
- use biz\shop\classes\ShopCouponClass;
- use biz\sj\classes\SjClass;
- use biz\stat\classes\StatCgClass;
- use biz\stat\classes\StatCgGhsClass;
- use biz\stat\classes\StatOutClass;
- use biz\stock\classes\StockClass;
- use biz\wx\classes\WxMessageClass;
- use bizGhs\admin\classes\AdminClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\item\classes\CostChangeClass;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\traits\OrderTrait;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\stock\classes\OnStockRecordClass;
- use bizGhs\stock\classes\StockRecordClass;
- use bizGhs\stock\services\StockRecordService;
- use bizHd\cg\classes\CgRefundClass;
- use bizHd\cg\classes\CgXjClass;
- use bizHd\shop\classes\MainClass;
- use bizHd\stat\classes\StatIncomeClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\mapUtil;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\util;
- use bizHd\base\classes\BaseClass;
- use common\components\lakala\Lakala;
- use Yii;
- use yii\helpers\ArrayHelper;
- class PurchaseClass extends BaseClass
- {
- use OrderTrait;
- public static $baseFile = '\bizHd\purchase\models\Purchase';
- //订单状态 1待付款,待确认,2待配送,3配送中,4已完成,5取消,失败
- const STATUS_UN_PAY = 1;
- const STATUS_UN_SEND = 2;
- const STATUS_SENDING = 3;
- const STATUS_COMPLETE = 4;
- const STATUS_CANCEL = 5;
- //未知
- const DEBT_UNKNOWN = 0;
- //欠款
- const DEBT_YES = 1;
- //没有欠款
- const DEBT_NO = 2;
- //配送,这里的数值是错的,请参考dict.php的数值
- const GET_TYPE_SEND = 1;
- //自取,这里的数值是错的,请参考dict.php的数值
- const GET_TYPE_SELF_GET = 2;
- const REFUND_NO = 1;
- const REFUND_YES = 2;
- //获取打包费
- public static function getKmPackCost($mainId)
- {
- $cost = dict::getDict('kmPackCost');
- if (getenv('YII_ENV') == 'production') {
- //云漫梦金鹏 徐记花卉 康多鲜 旭海 打包费0元
- if (in_array($mainId, [10728, 20528, 10866, 38657, 41121])) {
- $cost = dict::getDict('kmPackCost2');
- }
- //花瀚鲜花供应链
- if (in_array($mainId, [50, 14499])) {
- $cost = dict::getDict('kmPackCost3');
- }
- } else {
- }
- return $cost;
- }
- //订单发货主方法 ssh 20231119
- public static function orderSend($cg, $params, $allowRepeat)
- {
- $respond = self::orderFh($cg, $params, $allowRepeat);
- $orderId = $cg->saleId ?? 0;
- $orderInfo = OrderClass::getById($orderId, true);
- if (empty($orderInfo)) {
- util::fail('没有找到销售单信息');
- }
- OrderClass::orderFh($orderInfo, $params, $allowRepeat);
- return $respond;
- }
- //订单发货子方法 ssh 20231119
- public static function orderFh($cg, $params, $allowRepeat = false, $notice = false)
- {
- if ($notice == true) {
- $noticeText = json_encode(['id' => $cg->id]);
- $noticeKey = "ghsKdNoticeHd";
- Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
- }
- if ($cg->status == 3) {
- if ($allowRepeat == false) {
- util::fail('订单已发货');
- } else {
- return true;
- }
- }
- $fhType = $params['fhType'] ?? 0;
- if (is_numeric($fhType) == false) {
- util::fail('请选择发货类型');
- }
- if ($fhType == 1) {
- $fhWl = $params['fhWl'] ?? '';
- $fhWlNo = $params['fhWlNo'] ?? '';
- if (empty($fhWl)) {
- util::fail('请填写物流名称');
- }
- if (empty($fhWlNo)) {
- util::fail('请填写物流编号');
- }
- $cg->fhWl = $fhWl;
- $cg->fhWlNo = $fhWlNo;
- }
- $cg->fhType = $fhType;
- $cg->status = 3;
- $cg->fhTime = date("Y-m-d H:i:s");
- $cg->save();
- $orderSn = $cg->orderSn ?? '';
- $itemList = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($itemList)) {
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $mainId = $cg->mainId ?? 0;
- $ghsName = $cg->ghsName ?? '';
- foreach ($itemList as $key => $val) {
- $productId = $val->productId ?? 0;
- $itemId = $val->itemId ?? 0;
- $num = $val->xhNum ?? 0;
- $product = ProductClass::getLockById($productId);
- if (empty($product)) {
- util::fail('没有找到花材呢');
- }
- //增加路上库存
- $onRecord = [];
- $onRecord['sjId'] = $sjId;
- $onRecord['shopId'] = $shopId;
- $onRecord['mainId'] = $mainId;
- $onRecord['orderSn'] = $orderSn;
- $onRecord['productId'] = $productId;
- $onRecord['itemId'] = $itemId;
- $onRecord['relateName'] = $ghsName;
- $onRecord['itemNum'] = $num;
- $oldOnStock = $product->onStock ?? 0;
- $onRecord['oldStock'] = $oldOnStock;
- $newOnStock = bcadd($oldOnStock, $num);
- $onRecord['newStock'] = $newOnStock;
- $onRecord['ptStyle'] = 1;
- OnStockRecordClass::addPurchaseOrderRecord($onRecord);
- ProductClass::addOnStockByItemNum($productId, $num);
- }
- }
- return $cg;
- }
- //确认收货 ssh 20231119
- public static function confirmTake($cg)
- {
- //订单如果没有发货,则先发货
- if ($cg->status == self::STATUS_UN_SEND) {
- $cg = self::orderSend($cg, [], true);
- }
- // 构建“冲销”函数的参数 -- 变动前的花材存库数据
- $purchaseItems = PurchaseItemClass::getAllByCondition(['orderSn' => $cg->orderSn], null, "productId");
- $productIds = ArrayHelper::getColumn($purchaseItems, 'productId'); //$productIds 是本次入库涉及的花材ID数组
- $oldStocks = [];
- foreach ($productIds as $productId) {
- $product = ProductClass::getById($productId, true);
- $oldStocks[$productId] = $product;
- }
- self::takeToPutIn($cg);
- $shop = ShopClass::getLockById($cg->shopId);
- if (empty($shop)) {
- util::fail('没有找到门店14');
- }
- $adminId = $shop->adminId;
- $mainId = $shop->mainId;
- PurchaseClass::rollbackProductStock($oldStocks, $shop, $adminId, $mainId, '系统');
- }
- //确认收货并入库 ssh 20231119
- public static function takeToPutIn($cg)
- {
- if ($cg->status != 3) {
- util::fail('订单不是待收货状态哦');
- }
- $saleId = $cg->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- if ($order->status != 3) {
- util::fail('订单不是待收货状态,订单状态:' . $order->status . ',订单ID:' . $saleId);
- }
- $order->status = 4;
- $order->save();
- //加库存 lqh 2021-05-10
- $orderSn = $cg->orderSn;
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $mainId = $cg->mainId ?? 0;
- $ghsName = $cg->ghsName ?? '';
- $purchaseItem = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, "*");
- foreach ($purchaseItem as $v) {
- //小单位库存不考虑入库 ssh 202310
- $itemNum = $v['itemNum'];
- if ($itemNum > 0) {
- $productId = $v['productId'];
- $stockInfo = ProductClass::addStockByItemNum($v['productId'], $itemNum);
- //记录库存流水
- $recordData = [];
- $recordData['sjId'] = $cg->sjId;
- $recordData['shopId'] = $cg->shopId;
- $recordData['mainId'] = $cg->mainId;
- $recordData['itemId'] = $v['itemId'] ?? 0;
- $recordData['itemNum'] = $itemNum;
- $recordData['oldStock'] = $stockInfo['oldStock'];
- $recordData['newStock'] = $stockInfo['newStock'];
- $recordData['productId'] = $v['productId'];
- $recordData['orderSn'] = $orderSn;
- $recordData['relateName'] = $cg->ghsName ?? '';
- StockRecordClass::addPurchaseOrderRecord($recordData);
- $product = ProductClass::getLockById($productId);
- if (empty($product)) {
- util::fail('没有找到花材哈');
- }
- //减少路上库存
- $onRecord = [];
- $onRecord['sjId'] = $sjId;
- $onRecord['shopId'] = $shopId;
- $onRecord['mainId'] = $mainId;
- $onRecord['orderSn'] = $orderSn;
- $onRecord['productId'] = $productId;
- $onRecord['itemId'] = $v['itemId'] ?? 0;
- $onRecord['relateName'] = $ghsName;
- $onRecord['itemNum'] = $itemNum;
- $oldOnStock = $product->onStock ?? 0;
- $onRecord['oldStock'] = $oldOnStock;
- $onRecord['newStock'] = bcsub($oldOnStock, $itemNum);
- $onRecord['ptStyle'] = 1;
- OnStockRecordClass::addInStockOrderRecord($onRecord);
- ProductClass::decreaseOnStockByItemNum($productId, $itemNum);
- $unitPrice = $v['xhUnitPrice'] ?? 0;
- $product->cost = $unitPrice;
- $product->save();
- $ptItemId = $product->itemId ?? 0;
- $mainId = $product->mainId ?? 0;
- $staffId = $cg->shopAdminId ?? 0;
- $staffName = $cg->shopAdminName ?? '';
- //成本变动记录
- $changeData = [
- 'ptStyle' => 2,
- 'sjId' => 0,
- 'mainId' => $mainId,
- 'itemId' => $productId,
- 'ptItemId' => $ptItemId,
- 'cost' => $unitPrice,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'name' => $product->name ?? '',
- 'cover' => $product->cover ?? '',
- 'targetId' => $cg->id ?? 0,
- 'event' => '采购',
- ];
- CostChangeClass::addData($changeData);
- }
- }
- $cg->status = 4;
- $cg->stockChange = 1;
- $cg->save();
- }
- // 库存回滚为入库前的状态
- public static function rollbackProductStock($oldStocks, $shop, $adminId, $mainId, $staffName)
- {
- $itemInfo = [];
- foreach ($oldStocks as $productId => $product) {
- $ratio = $product->ratio;
- $formNum = ProductClass::formatStock($product->stock, $ratio); //花材的库存转换
- $itemInfo[] = [
- 'productId' => $productId,
- 'itemNum' => $product->stock, // 回滚为入库前的库存
- 'itemId' => $product->itemId,
- 'bigNum' => $formNum['bigNum'],
- 'smallNum' => $formNum['smallNum'],
- 'price' => $product->price,
- ];
- }
- $data = [
- 'sjId' => $shop->sjId,
- 'shopId' => $shop->id,
- 'adminId' => $adminId,
- 'mainId' => $mainId,
- 'staffName' => $staffName,
- 'itemInfo' => $itemInfo,
- 'pdType' => 'rollbackStock',
- 'remark' => '',
- ];
- \bizGhs\order\classes\CheckOrderClass::opOrder($data, false, false, false);
- }
- // 指定花材新增库存
- public static function increaseProductStock($itemDatas, $shop, $adminId, $mainId, $staffName)
- {
- $itemInfo = [];
- foreach ($itemDatas as $productId => $arr) {
- $addNum = $arr['addNum']; //新增的数值
- $product = $arr['product'];
- $ratio = $product->ratio;
- $newStock = bcadd($product->stock, $addNum, 2);
- $formNum = ProductClass::formatStock($newStock, $ratio); //花材的库存转换
- $itemInfo[] = [
- 'productId' => $productId,
- 'itemNum' => $newStock, // 新库存数
- 'itemId' => $product->itemId,
- 'bigNum' => $formNum['bigNum'],
- 'smallNum' => $formNum['smallNum'],
- 'price' => $product->price,
- ];
- }
- $data = [
- 'sjId' => $shop->sjId,
- 'shopId' => $shop->id,
- 'adminId' => $adminId,
- 'mainId' => $mainId,
- 'staffName' => $staffName,
- 'itemInfo' => $itemInfo,
- 'pdType' => 'rollbackStock',
- 'remark' => '',
- ];
- \bizGhs\order\classes\CheckOrderClass::opOrder($data, false, false, false);
- }
- public static function notSameCity($shop, $ghsShop)
- {
- return true;
- $shopLat = $shop->lat ?? '';
- $shopLong = $shop->long ?? '';
- if (empty($shopLat) || empty($shopLong)) {
- util::fail('请完善门店地址');
- }
- $ghsShopLat = $ghsShop->lat ?? '';
- $ghsShopLong = $ghsShop->long ?? '';
- $distance = mapUtil::calculateDistance($shopLong, $shopLat, $ghsShopLong, $ghsShopLat);
- $formatDistance = ceil($distance / 1000);
- if ($formatDistance > 150) {
- return false;
- }
- return true;
- }
- //计算距离和运费【另外还有个地方要同步修改,关键词calc_freight_distance】 ssh 20221003
- public static function getDistanceFee($shop, $ghsShop, $weight)
- {
- return ['distance' => 0, 'showDistance' => 0, 'fee' => 0];
- $shopLat = isset($shop->lat) ? $shop->lat : '';
- $shopLong = isset($shop->long) ? $shop->long : '';
- if (empty($shopLat) || empty($shopLong)) {
- util::fail('请完善门店地址哦');
- }
- $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : '';
- $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : '';
- if (empty($ghsShopLat) || empty($ghsShopLong)) {
- util::fail('商家地址有问题');
- }
- //基础配送费8元,0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
- //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
- //00:00 - 06:00 +6元,22:00-24:00 +4元
- //恶劣天气,下单高峰期会临时调整价格
- //起步价
- $baseFee = 10;
- $distance = mapUtil::calculateDistance($shopLong, $shopLat, $ghsShopLong, $ghsShopLat);
- //0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
- $formatDistance = ceil($distance / 1000);
- $startKiloFee = 6;
- if ($formatDistance < 5) {
- $addDistanceFee = bcmul($formatDistance, 2);
- } elseif ($formatDistance == 5) {
- $addDistanceFee = $startKiloFee;
- } else {
- $overKilo = bcsub($formatDistance, 5);
- $overFee = bcmul($overKilo, 3);
- $addDistanceFee = bcadd($startKiloFee, $overFee);
- }
- //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
- $formatWeight = ceil($weight);
- if ($formatWeight > 15) {
- $addWeightFee = 20;
- } elseif ($formatWeight >= 10 && $formatWeight <= 15) {
- $addWeightFee = 12;
- } elseif ($formatWeight >= 5 && $formatWeight < 10) {
- $overWeight = bcsub($formatWeight, 4);
- $addWeightFee = bcmul($overWeight, 2);
- } else {
- $addWeightFee = 0;
- }
- $addFee = bcadd($addDistanceFee, $addWeightFee, 2);
- $totalFee = bcadd($baseFee, $addFee, 2);
- //特殊时段费
- $timeFee = 0;
- $hour = date("G");
- //22:00 - 24:00 +4元
- if (in_array($hour, [22, 23])) {
- $timeFee = 4;
- }
- //00:00 - 06:00 +6元
- if (in_array($hour, [0, 1, 2, 3, 4, 5])) {
- $timeFee = 6;
- }
- $totalFee = bcadd($totalFee, $timeFee, 2);
- //恶劣天气和下单高峰费用
- $weatherFee = 0;
- $totalFee = bcadd($totalFee, $weatherFee, 2);
- $showDistance = sprintf("%.1f", ($distance / 1000));
- //noticeUtil::push("距离:{$distance} {$showDistance} 重量:{$weight} 费用:{$totalFee}", '15280215347');
- //万丽花行 跑腿不算运费
- if ($ghsShop->id == 12003) {
- return ['distance' => 0, 'showDistance' => 0, 'fee' => 0];
- }
- return ['distance' => $distance, 'showDistance' => $showDistance, 'fee' => $totalFee];
- }
- //到货之后 ssh 20220321
- public static function arrival($cg, $data)
- {
- if ($cg->stockChange == 1) {
- util::fail('您已操作过了');
- }
- $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
- $packCost = isset($data['packCost']) && is_numeric($data['packCost']) ? $data['packCost'] : 0;
- $cg->sendCost = $sendCost;
- $cg->packCost = $packCost;
- $cg->save();
- $sjId = $data['sjId'] ?? 0;
- $shopId = $data['shopId'] ?? 0;
- //product [{"productId":"3674","num":0,"price":1}]
- $product = $data['product'] ?? '';
- if (empty($product)) {
- util::fail('请填写成本价和到货数量');
- }
- $ghsName = $cg->ghsName ?? '';
- $orderSn = $cg->orderSn ?? '';
- $list = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (empty($list)) {
- util::fail('没有找到花材哦');
- }
- $weight = 0;
- $itemPrice = 0;
- foreach ($list as $key => $val) {
- $productId = $val->productId ?? 0;
- if (isset($product[$productId]) == false) {
- util::fail('没有找到对应采购花材');
- }
- $num = $product[$productId]['num'] ?? 0;
- $price = $product[$productId]['price'] ?? 0;
- if ($price <= 0) {
- util::fail('花材金额需要大于0');
- }
- $totalPrice = bcmul($num, $price, 2);
- $itemPrice = bcadd($itemPrice, $totalPrice, 2);
- $val->price = $price;
- $val->itemNum = $num;
- $val->preTotalPrice = $totalPrice;
- $val->totalPrice = $totalPrice;
- $val->xhNum = $num;
- $val->xhUnitPrice = $price;
- $val->xhPrice = $totalPrice;
- $val->save();
- $unitWeight = $val->unitWeight ?? 0;
- $currentWeight = bcmul($num, $unitWeight, 2);
- $weight = bcadd($weight, $currentWeight, 2);
- $unitType = $val->xhUnitType ?? 0;
- $bigNum = 0;
- $smallNum = 0;
- if ($unitType == dict::getDict('unitType', 'big')) {
- $bigNum = $num;
- } else {
- $smallNum = $num;
- }
- //增加库存
- $option = ['relateName' => $ghsName];
- StockClass::purchaseItem($orderSn, $sjId, $shopId, $productId, $bigNum, $smallNum, $option);
- }
- $cg->itemPrice = $itemPrice;
- $sendCost = $cg->sendCost ?? 0;
- $currentPrice = bcadd($itemPrice, $sendCost, 2);
- $packCost = $cg->packCost ?? 0;
- $currentPrice = bcadd($currentPrice, $packCost, 2);
- $kiloFee = $data['kiloFee'] ?? 0;
- $miniKilo = $data['miniKilo'] ?? 0;
- $calcWeight = $weight > $miniKilo ? $weight : $miniKilo;
- $allKiloFee = bcmul($calcWeight, $kiloFee, 2);
- $currentPrice = bcadd($currentPrice, $allKiloFee, 2);
- $cg->prePrice = $currentPrice;
- $cg->orderPrice = $currentPrice;
- $cg->actPrice = $currentPrice;
- $cg->realPrice = $currentPrice;
- $cg->stockChange = 1;
- $cg->save();
- $mainId = $cg->mainId ?? 0;
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $cgId = $cg->id ?? 0;
- $saleRefundId = $data['saleRefundId'] ?? 0;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到零售店的门店');
- }
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息12');
- }
- $main->cgUnSend -= 1;
- $main->cgFinish += 1;
- $main->save();
- $cgRefund = null;
- $bookPrice = $cg->bookPrice ?? 0;
- if ($bookPrice == $currentPrice) {
- return ['refund' => $cgRefund];
- } elseif ($bookPrice > $currentPrice) {
- //退款
- $refundPrice = bcsub($bookPrice, $currentPrice, 2);
- $book = 1;
- $refundOrderSn = orderSn::getCgRefundSn();
- $refundType = CgRefundClass::REFUND_TYPE_MONEY;
- $refundData = [
- 'book' => $book,
- 'mainId' => $mainId,
- 'orderSn' => $refundOrderSn,
- 'cgId' => $cgId,
- 'status' => CgRefundClass::STATUS_COMPLETE,
- 'shopAdminId' => 0,
- 'shopAdminName' => '',
- 'refundType' => $refundType,
- 'refundPrice' => $refundPrice,
- 'saleRefundId' => $saleRefundId,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- ];
- $cgRefund = CgRefundClass::add($refundData, true);
- //门店收入增加
- $currentTotalIncome = bcadd($main->totalIncome, $refundPrice, 2);
- $main->totalIncome = $currentTotalIncome;
- $main->save();
- //供货商退款,向供货商采购总金额减少
- StatCgClass::replace($main, $shop, $refundPrice, 0, false);
- //供货商退款,按供货商分别采购的金额减少
- StatCgGhsClass::hdCgGhsReplace(false, $cg, $refundPrice, 0);
- $currentType = dict::getDict('capitalType', 'hdBookRefund', 'id');
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $event = "预订单{$orderSn}多退少补({$ghsName})";
- $capitalData = [
- 'capitalType' => $currentType,
- 'io' => 1,
- 'totalIncome' => $currentTotalIncome,
- 'payWay' => 0,
- 'amount' => $refundPrice,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capitalData);
- //收入统计
- StatIncomeClass::updateOrInsert($main, $shop, $refundPrice);
- $ghsId = $cg->ghsId ?? 0;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- $ghsShopId = $ghs->shopId ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (empty($ghsShop)) {
- util::fail('没有找到供货商的门店');
- }
- $refundFee = bcmul($refundPrice, 100);
- $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);
- $refundReason = '';
- $aliParams = [
- 'refundSn' => $refundOrderSn,
- 'orderSn' => $orderSn,
- 'refundAmount' => $refundFee,
- 'refundReason' => $refundReason,
- ];
- $response = $laResource->refund($aliParams);
- if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '';
- noticeUtil::push('退款失败了哦!请注意:' . $errMsg . ' 金额:' . $refundFee, '15280215347');
- util::fail('退款失败');
- }
- $refundTradeNo = isset($response['resp_data']['trade_no']) ? $response['resp_data']['trade_no'] : '';
- $cgRefund->thirdRefundNo = $refundTradeNo;
- $cgRefund->save();
- $cg->refundPrice = $refundPrice;
- $cg->tkPrice = bcadd($cg->tkPrice, $refundPrice, 2);
- $cg->refund = self::REFUND_YES;
- $cg->refundLog = 1;
- $cg->save();
- } else {
- //欠款
- $currentDebt = bcsub($currentPrice, $bookPrice, 2);
- $cg->debtPrice = $currentDebt;
- $cg->remainDebtPrice = $currentDebt;
- $cg->debt = PurchaseClass::DEBT_YES;
- $cg->save();
- $payWay = dict::getDict('payWay', 'debtPay');
- $ghsId = $cg->ghsId ?? 0;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- \bizHd\ghs\classes\GhsClass::cgDebtAmountAdd($ghs, $cg, true);
- //支出统计
- StatOutClass::updateOrInsert($main, $shop, $currentDebt);
- //支出流水
- $capitalType = dict::getDict('capitalType', 'hdBookFinalPay', 'id');
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $currentExpend = bcadd($main->totalExpend, $currentDebt, 2);
- $main->totalExpend = $currentExpend;
- $main->save();
- $event = "预订单{$orderSn}补尾款欠款";
- $capitalData = [
- 'capitalType' => $capitalType,
- 'io' => 0,
- 'totalExpend' => $currentExpend,
- 'payWay' => $payWay,
- 'amount' => $currentDebt,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capitalData);
- //补款给供货商,采购总金额增加
- StatCgClass::replace($main, $shop, $currentDebt, 0, true);
- //补款给供货商,按供货商分别统计采购量增加
- StatCgGhsClass::hdCgGhsReplace(true, $cg, $currentDebt, 0);
- }
- return ['refund' => $cgRefund];
- }
- //添加采购 ssh 2021.1.18
- public static function addPurchase($data, $ghs)
- {
- $shopId = $data['shopId'] ?? 0;
- $sjId = $data['sjId'] ?? 0;
- $mainId = $data['mainId'] ?? 0;
- $ghsId = $data['ghsId'] ?? 0;
- $book = $data['book'] ?? 0;
- $shopAdminId = $data['shopAdminId'] ?? 0;
- $customId = $data['customId'] ?? 0;
- $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'ghsId' => $ghsId, 'customId' => $customId];
- $orderSn = orderSn::getPurchaseSn($snData);
- $data['orderSn'] = $orderSn;
- $prePrice = 0;
- $live = $ghs['live'] ?? 1;
- $sendTimeWant = !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
- if ($sendTimeWant < date("Y-m-d")) {
- util::fail('您选了过去时间');
- }
- $data['sendTimeWant'] = $sendTimeWant;
- if (empty($data['product'])) {
- util::fail('请选择花材17');
- }
- $productList = $data['product'];
- $productList = self::mergeItemInfo($productList);
- $data['kindNum'] = count($productList); //多少种花材
- //传过来的是供货商的productId,需要转换为零售端的productId
- //数据格式 [{"productId":"113","bigNum":1,"smallNum":0,"classId":"-2","hdProductId":1}]
- if ($live == 1) {
- //花店真实向批发店采购花材,因为拿的供货商的ID,所以需要转化花店的id
- $productList = ProductClass::toggleProductList($productList, $shopId, $sjId, $mainId, $ghs);
- $ghsProductData = self::getProductMapData($productList, "ghsProductId");
- } else {
- //虚拟采购用的自己的ID所以不需要转化
- $ghsProductData = [];
- }
- $hdProductData = self::getProductMapData($productList, "productId");
- $giveLevel = $ghs['giveLevel'] ?? 0;
- $priceMap = CustomClass::$levelPriceKeyMap;
- $addPriceMap = CustomClass::$levelAddPriceKeyMap;
- //累计满减
- $totalReachDiscount = 0;
- //计算价格= 各个productId的price*num(bigNum,smallNum) + sendCost(配送费)
- $bigNum = 0; //总共多少扎
- $smallNum = 0; //总共多少支
- $totalWeight = 0;
- foreach ($productList as $key => $product) {
- $currentBigNum = $product['bigNum'] ?? 0;
- $currentSmallNum = $product['smallNum'] ?? 0;
- $bigNum += $product['bigNum'] ?? 0;
- $smallNum += $product['smallNum'] ?? 0;
- $productList[$key]['orderSn'] = $orderSn;
- if ($live == 1) {
- $ghsProductId = $product['ghsProductId'];
- $hdProductId = $product['productId'];
- } else {
- $hdProductId = $product['productId'] ?? 0;
- $ghsProductId = 0;
- }
- $unitWeight = $hdProductData[$hdProductId]['weight'] ?? 0;
- $variety = $hdProductData[$hdProductId]['variety'] ?? 0;
- $ratio = $hdProductData[$hdProductId]['ratio'] ?? 0;
- $itemId = $hdProductData[$hdProductId]['itemId'] ?? 0;
- $rank = $hdProductData[$hdProductId]['rank'] ?? 'B';
- $stock = $hdProductData[$hdProductId]['stock'] ?? 0;
- $stockFormat = ProductClass::formatStock($stock, $ratio);
- $itemNum = ProductClass::mergeItemNum($product['bigNum'], $product['smallNum'], $ratio);
- $weight = $hdProductData[$hdProductId]['weight'] ?? 0;
- $currentWeight = bcmul($weight, $itemNum, 2);
- $totalWeight = bcadd($currentWeight, $totalWeight, 2);
- //1、供货商开单改价 2、花店向虚拟供货商采购录入价格
- $userPrice = isset($product['userPrice']) && $product['userPrice'] > 0 ? $product['userPrice'] : 0;
- $currentGhsProduct = $ghsProductData[$ghsProductId] ?? [];
- if (!empty($currentGhsProduct)) {
- //用批发店的花材名称
- $itemName = $currentGhsProduct['name'] ?? '';
- $cover = $currentGhsProduct['cover'] ?? '';
- //如果零售店自己下预订单,预付0.1元,有二个地方要同步修改,搜索关键词 pre0.1
- if ($book == 1 && $shopAdminId > 0) {
- $price = 0.1;
- } else {
- //用批发店花材的价格
- $price = ProductClass::getFinalPrice($currentGhsProduct, $giveLevel, $priceMap, $addPriceMap, $userPrice, $itemNum);
- }
- $thisPrice = $price;
- $reachNum = $currentGhsProduct['reachNum'] ?? 0;
- if ($reachNum > 0) {
- if ($itemNum >= $reachNum) {
- $reachNumDiscount = $currentGhsProduct['reachNumDiscount'] ?? 0;
- $currentReachDiscount = bcmul($reachNumDiscount, $itemNum, 2);
- $totalReachDiscount = bcadd($totalReachDiscount, $currentReachDiscount, 2);
- }
- }
- } else {
- $currentHdProduct = $hdProductData[$hdProductId] ?? [];
- if (empty($currentHdProduct)) {
- util::fail('花材有问题哦');
- }
- $itemName = $currentHdProduct['name'] ?? '';
- $cover = $currentHdProduct['cover'] ?? '';
- $price = $userPrice;
- $thisPrice = $userPrice;
- }
- if ($currentSmallNum > 0) {
- $productTotalPrice = bcmul($currentSmallNum, $price, 2);
- } else {
- $productTotalPrice = bcmul($currentBigNum, $price, 2);
- }
- $itemInfo = [
- 'itemName' => $itemName,
- 'itemCover' => $cover,
- 'bigNum' => $product['bigNum'] ?? 0,
- 'smallNum' => $product['smallNum'] ?? 0,
- 'bigNumStock' => $stockFormat['bigNum'],
- 'smallNumStock' => $stockFormat['smallNum'],
- 'itemUnit' => $ratio ? 2 : 1,
- 'ratio' => $ratio,
- 'rank' => $rank,
- 'bigUnit' => $hdProductData[$hdProductId]['bigUnit'],
- 'smallUnit' => $hdProductData[$hdProductId]['smallUnit'],
- ];
- if ($itemNum <= 0) {
- util::fail($itemName . ' 数量要大于零');
- }
- $productList[$key]['itemInfo'] = json_encode($itemInfo);
- $productList[$key]['variety'] = $variety;
- $productList[$key]['itemId'] = $itemId;
- $productList[$key]['preItemNum'] = $itemNum;
- $productList[$key]['itemNum'] = $itemNum;
- $productList[$key]['bigNum'] = $product['bigNum'] ?? 0;
- $productList[$key]['smallNum'] = $product['smallNum'] ?? 0;
- $productList[$key]['price'] = $thisPrice;
- $productList[$key]['ratio'] = $ratio;
- $productList[$key]['unitWeight'] = $unitWeight;
- $productList[$key]['totalPrice'] = $productTotalPrice;
- $productList[$key]['preTotalPrice'] = $productTotalPrice;
- $productList[$key]['productId'] = $hdProductId;
- $productList[$key]['ghsProductId'] = $ghsProductId;
- $productList[$key]['smallPrice'] = $price;
- $productList[$key]['mainId'] = $mainId;
- $productList[$key]['ghsId'] = $ghsId;
- $productList[$key]['name'] = $itemName;
- $productList[$key]['cover'] = $cover;
- $currentBigUnit = $hdProductData[$hdProductId]['bigUnit'] ?? '';
- $currentSmallUnit = $hdProductData[$hdProductId]['smallUnit'] ?? '';
- if ($currentBigNum > 0) {
- $xhNum = $currentBigNum;
- $xhUnitName = $currentBigUnit;
- $xhUnitType = dict::getDict('unitType', 'big');
- } else {
- $xhNum = $currentSmallNum;
- $xhUnitName = $currentSmallUnit;
- $xhUnitType = dict::getDict('unitType', 'small');
- }
- $itemPrice = $price;
- $price = bcmul($itemPrice, $xhNum, 2);
- //客户下单时会出现同时选了扎和支,很奇怪,前端没有找到原因,后端限制
- if ($currentBigNum > 0 && $currentSmallNum > 0) {
- util::fail($itemName . " {$currentBigUnit}和{$currentSmallUnit}只能选一种");
- }
- $productList[$key]['xhNum'] = $xhNum;
- $productList[$key]['xhUnitName'] = $xhUnitName;
- $productList[$key]['xhUnitType'] = $xhUnitType;
- $productList[$key]['xhUnitPrice'] = $itemPrice;
- $productList[$key]['xhPrice'] = $price;
- $productList[$key]['xhPreNum'] = $xhNum;
- $productList[$key]['xhPreUnitName'] = $xhUnitName;
- $productList[$key]['xhPreUnitType'] = $xhUnitType;
- $productList[$key]['xhPreUnitPrice'] = $itemPrice;
- $productList[$key]['xhPrePrice'] = $price;
- $prePrice = bcadd($prePrice, $productTotalPrice, 2);
- }
- $data['itemPrice'] = $prePrice;
- $sendCost = $data['sendCost'] ?? 0.00;
- $prePrice = bcadd($prePrice, $sendCost, 2);
- $packCost = $data['packCost'] ?? 0;
- $prePrice = bcadd($prePrice, $packCost, 2);
- $data['weight'] = $totalWeight;
- //预订单需要增加服务费
- if ($book == 1) {
- $kiloFee = $ghs['kiloFee'] ?? 0;
- $miniKilo = $ghs['miniKilo'] ?? 0;
- $calcWeight = $totalWeight > $miniKilo ? $totalWeight : $miniKilo;
- $allKiloFee = bcmul($calcWeight, $kiloFee, 2);
- $prePrice = bcadd($prePrice, $allKiloFee, 2);
- $data['allKiloFee'] = $allKiloFee;
- $data['kiloFee'] = $kiloFee;
- $data['miniKilo'] = $miniKilo;
- $data['stockChange'] = 0;
- }
- $actPrice = $prePrice;
- if (isset($data['modifyPrice'])) {
- $modifyPrice = $data['modifyPrice'] ?? 0;
- if ($modifyPrice < $prePrice) {
- //优惠金额
- $data['discountAmount'] = bcsub($prePrice, $modifyPrice, 2);
- $data['discountType'] = dict::getDict('discountType', 'discount');
- }
- if ($modifyPrice > $prePrice) {
- //人工和资材费用
- $data['labourCost'] = bcsub($modifyPrice, $prePrice, 2);
- }
- $actPrice = $modifyPrice;
- if ($book == 1) {
- $actPrice = 0;
- }
- } else {
- //整单满减
- if ($book == 0) {
- $ghsShopId = $ghs['shopId'] ?? 0;
- $ghsShop = ShopClass::getById($ghsShopId, true);
- if (!empty($ghsShop)) {
- $meetNum = $ghsShop->meetNum ?? 0;
- $meetAmount = $ghsShop->meetAmount ?? 0;
- $cutStyle = $ghsShop->cutStyle ?? 0;
- $cutOption = $ghsShop->cutAmount ?? 0;
- if ($cutStyle == 0) {
- $cutAmount = $cutOption;
- } else {
- $miniOption = bcsub(1, $cutOption, 2);
- $cutAmount = bcmul($actPrice, $miniOption, 2);
- }
- if ($cutOption > 0) {
- if ($meetNum > 0 && $bigNum >= $meetNum) {
- if ($cutAmount > $actPrice) {
- util::fail('整单满减为负');
- }
- $actPrice = bcsub($actPrice, $cutAmount, 2);
- $data['orderReachDiscountPrice'] = $cutAmount;
- } else {
- if ($meetAmount > 0 && isset($data['itemPrice']) && $data['itemPrice'] >= $meetAmount) {
- if ($cutAmount > $actPrice) {
- util::fail('整单满减为负');
- }
- $actPrice = bcsub($actPrice, $cutAmount, 2);
- $data['orderReachDiscountPrice'] = $cutAmount;
- }
- }
- }
- }
- }
- }
- $data['reachDiscountPrice'] = $totalReachDiscount;
- $data['prePrice'] = $prePrice;
- $data['bookPrice'] = $actPrice;
- $data['orderPrice'] = $actPrice;
- $data['actPrice'] = $actPrice;
- $data['realPrice'] = $actPrice;
- $data['bigNum'] = $bigNum;
- $data['smallNum'] = $smallNum;
- $data['discount'] = 1;
- $data['status'] = self::STATUS_UN_PAY;
- //采购单有效期
- $currentTime = time();
- $validTime = dict::getDict('order_pay_has_time');
- $current = $currentTime + $validTime;
- $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
- if ($book == 1) {
- $fiveTime = bcmul(86400, 5);
- $bookDeadline = bcadd($currentTime, $fiveTime);
- $data['deadline'] = date("Y-m-d H:i:s", $bookDeadline);
- }
- //采购单自动记为欠款单时间
- $localConfirmTime = dict::getDict('local_gys_kd_auto_set_debt_time');
- $currentSet = $currentTime + $localConfirmTime;
- $data['autoSetTime'] = $data['autoSetTime'] ?? date("Y-m-d H:i:s", $currentSet);
- //保存订单
- $respond = self::add($data, true);
- foreach ($productList as $productKey => $productVal) {
- //增加采购单的花材
- $returnItem = PurchaseItemClass::add($productVal, true);
- $unitPrice = $returnItem->price ?? 0;
- $currentId = $returnItem->id ?? 0;
- $currentPtItemId = $returnItem->itemId ?? 0;
- if (isset($data['xj'][$currentPtItemId]) && !empty($data['xj'][$currentPtItemId])) {
- $xjArr = $data['xj'][$currentPtItemId];
- if (is_array($xjArr)) {
- $ids = array_column($xjArr, 'id');
- $sjInfo = XjClass::getByIds($ids, null, 'id');
- $xjData = [];
- foreach ($xjArr as $xjItem) {
- $id = $xjItem['id'] ?? 0;
- $num = $xjItem['num'] ?? 0;
- $name = $sjInfo[$id]['name'] ?? '';
- $cover = $sjInfo[$id]['cover'] ?? '';
- $xjData[] = ['xjId' => $id, 'name' => $name, 'ptItemId' => $currentPtItemId,
- 'num' => $num, 'price' => $unitPrice, 'itemId' => $currentId, 'cover' => $cover];
- }
- CgXjClass::batchAdd($xjData);
- $returnItem->variety = 1;
- $returnItem->save();
- }
- }
- }
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店59 ' . $shopId);
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息13');
- }
- $main->totalPurchaseOrder += 1;
- $main->cgUnPay += 1;
- $main->save();
- return $respond;
- }
- //添加采购 ssh 2021.1.18
- public static function updatePurchase($purchase, $data)
- {
- util::fail('已删除');
- }
- //组合数据 ssh 2021.1.25
- public static function groupPurchase($list, $showButton = false, $showProduct = false)
- {
- $ghsIds = array_unique(array_filter(array_column($list, 'ghsId')));
- $ghsInfo = GhsClass::getGhsByIds($ghsIds);
- $adminIds = array_unique(array_filter(array_column($list, 'adminId')));
- $adminInfo = AdminClass::getAdminByIds($adminIds, null, 'id');
- $time = time();
- foreach ($list as $key => $val) {
- //剩余支付时间
- $remainPayTime = 0;
- $deadline = $val['deadline'] ?? '';
- if (!empty($deadline)) {
- $deadTime = strtotime($deadline);
- if (($deadTime - $time - 30) > 0) {
- $remainPayTime = $deadTime - $time;
- $remainPayTime = $remainPayTime - 30;
- }
- }
- $list[$key]['remainPayTime'] = $remainPayTime;
- $adminId = $val['adminId'] ?? 0;
- $list[$key]['adminName'] = $adminInfo[$adminId]['name'] ?? '';
- $ghsId = $val['ghsId'] ?? 0;
- $list[$key]['supplierName'] = $ghsInfo[$ghsId]['name'] ?? '';
- $list[$key]['supplierAvatar'] = $ghsInfo[$ghsId]['smallLogoUrl'] ?? '';
- $list[$key]['supplierMobile'] = $ghsInfo[$ghsId]['mobile'] ?? '';
- $list[$key]['supplierAddress'] = $ghsInfo[$ghsId]['address'] ?? '';
- $list[$key]['debtAmount'] = $ghsInfo[$ghsId]['debtAmount'] ?? '';
- //剩余欠款
- $balance = $ghsInfo[$ghsId]['balance'] ?? 0;
- $debtAmount = $ghsInfo[$ghsId]['debtAmount'] ?? 0;
- $ghsSalt = $ghsInfo[$ghsId]['salt'] ?? '';
- $remainDebtAmount = bcsub($debtAmount, $balance, 2);
- $list[$key]['remainDebtAmount'] = floatval($remainDebtAmount);
- $list[$key]['inMyGhsBalance'] = floatval($balance);
- $list[$key]['ghsSalt'] = $ghsSalt;
- $list[$key]['lat'] = $ghsInfo[$ghsId]['lat'] ?? '';
- $list[$key]['long'] = $ghsInfo[$ghsId]['long'] ?? '';
- $list[$key]['supplierId'] = $ghsInfo[$ghsId]['id'] ?? '';
- if ($showButton) {
- $list[$key]['buttonList'] = [
- ['type' => 'pay', 'show' => 1, 'disable' => 1],
- ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
- ];
- }
- $list[$key]['sendProgress'] = [];
- if ($showProduct) {
- $orderSn = $val['orderSn'];
- $orderItem = PurchaseItemClass::getOrderItemDetail($orderSn);
- $itemData = [];
- $xjData = [];
- if ($orderItem) {
- foreach ($orderItem as $v) {
- $info = json_decode($v['itemInfo'], true);
- $tmp = $info;
- unset($v['itemInfo']);
- if (!isset($tmp['rank'])) {
- $tmp['rank'] = 'B';
- }
- $tmp['name'] = $tmp['itemName'] ?? '';
- $tmp = array_merge($tmp, $v);
- $shortCover = $info['itemCover'] ?? '';
- $tmp['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- if (isset($tmp['variety']) && $tmp['variety'] == 1) {
- $currentItemId = $tmp['id'] ?? 0;
- $xjList = CgXjClass::getAllByCondition(['itemId' => $currentItemId], null, '*');
- if (!empty($xjList)) {
- foreach ($xjList as $xjKey => $xj) {
- $xjBase = $tmp;
- $num = $xj['num'] ?? 0;
- $name = $xj['name'] ?? '';
- $cover = $xj['cover'] ?? '';
- $price = $xj['price'] ?? '';
- $totalPrice = bcmul($num, $price, 2);
- if ($num > 0) {
- $xjBase['cover'] = imgUtil::groupImg($cover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $xjBase['name'] = $name;
- $xjBase['itemNum'] = floatval($num);
- $xjBase['bigNum'] = floatval($num);
- $xjBase['smallNum'] = 0;
- $xjBase['price'] = $price;
- $xjBase['totalPrice'] = $totalPrice;
- $xjBase['xhNum'] = floatval($num);
- $xjAddList[] = $xjBase;
- }
- $xjData[] = $xjBase;
- }
- } else {
- $itemData[] = $tmp;
- }
- } else {
- $itemData[] = $tmp;
- }
- }
- if (!empty($xjData)) {
- $itemData = array_merge($itemData, $xjData);
- }
- }
- $list[$key]['product'] = $itemData;
- }
- }
- return $list;
- }
- //订单取消,变更状态,占用库存释放 ssh 2021.5.8
- public static function cancel($id)
- {
- $purchase = PurchaseClass::getLockById($id);
- $purchase->status = self::STATUS_CANCEL;
- $purchase->cancelTime = date("Y-m-d H:i:s");
- $purchase->save();
- $shopId = $purchase->shopId;
- $orderSn = $purchase->orderSn ?? '';
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店60');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息14');
- }
- $main->cgUnPay -= 1;
- $main->cgCancel += 1;
- $main->save();
- $ghsId = $purchase->ghsId;
- $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->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $closeParams = ['orderSn' => $orderSn];
- $response = $laResource->close($closeParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $msg = $response['msg'] ?? '';
- $code = $response['code'] ?? 0;
- //主扫(手机上微信下单拉取微信支付)调用这个方法有效,如果收银机上扫客户收款码的,调用这个方法无效,具体说明看 https://o.lakala.com/#/home/document/detail?id=115 所以在这个取消方法,暂时不考虑关单成功与否 ssh 20250517
- //noticeUtil::push('花店采购 ' . $orderSn . ' 关单没有成功哦' . $msg . ' ' . $code, '15280215347');
- }
- //收银台关单
- $closeParams = ['orderSn' => $orderSn];
- $response = $laResource->cashClose($closeParams);
- if (!isset($response['code']) || $response['code'] != '000000') {
- $msg = $response['msg'] ?? '';
- $code = $response['code'] ?? 0;
- //聚合收银台(APP发起的微信和支付支付)调用这个方法有效,说明 https://o.lakala.com/#/home/document/detail?id=722,其他情况调用无法,所以在这个取消方法,暂时不考虑关单成功与否 ssh 20250517
- //noticeUtil::push('花店采购 ' . $orderSn . ' 收银台关单的提示:' . $msg . ' ' . $code, '15280215347');
- }
- return $purchase;
- }
- //采购统计2021-05-08
- //采购数,采购金额统计(根据时间,今日,昨天,7天,30天)
- public static function statOrderInfo($shopId, $statType)
- {
- $start = '';
- $end = '';
- switch ($statType) {
- case "today":
- $start = date('Y-m-d');
- $end = $start;
- break;
- case "yesterday" :
- $start = date('Y-m-d', strtotime("-1 day"));
- $end = $start;
- break;
- case "seven":
- $start = date('Y-m-d', strtotime("-6 day"));
- $end = date('Y-m-d');
- break;
- case "thirty":
- $start = date('Y-m-d', strtotime("-29 day"));
- $end = date('Y-m-d');
- break;
- default:
- }
- $where = [];
- $where['shopId'] = $shopId;
- $where['addTime'] = ['between', [$start, $end]];
- $res = self::getAllByCondition($where, null, "id,actPrice");
- $totalAmount = 0;
- $totalNum = count($res);
- foreach ($res as $v) {
- $totalAmount = bcadd($totalAmount, $v['actPrice'], 2);
- }
- return [
- 'totalAmount' => $totalAmount,
- 'totalNum' => $totalNum,
- ];
- }
- //采购单完成 ssh 2021.5.15
- public static function complete($purchase)
- {
- $purchase->status = self::STATUS_COMPLETE;
- $purchase->save();
- $shopId = $purchase->shopId;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店61');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息15');
- }
- $main->cgSending -= 1;
- $main->cgFinish += 1;
- $main->save();
- /********订单完成后加库存 lqh 2021-05-10*********/
- $orderSn = $purchase->orderSn;
- $purchaseItem = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, "*");
- foreach ($purchaseItem as $v) {
- $stockInfo = ProductClass::addStockByItemNum($v['productId'], $v['itemNum']);
- //记录库存流水
- $recordData = [];
- $recordData['sjId'] = $purchase->sjId;
- $recordData['shopId'] = $purchase->shopId;
- $recordData['mainId'] = $purchase->mainId;
- $recordData['itemId'] = $v['itemId'] ?? 0;
- $recordData['itemNum'] = $v['itemNum'];
- $recordData['oldStock'] = $stockInfo['oldStock'];
- $recordData['newStock'] = $stockInfo['newStock'];
- $recordData['productId'] = $v['productId'];
- $recordData['orderSn'] = $orderSn;
- StockRecordClass::addPurchaseOrderRecord($recordData);
- }
- }
- //订单信息变更为配送中 ssh 2021.5.15
- public static function sending($purchaseId)
- {
- $purchase = PurchaseClass::getById($purchaseId, true);
- if (empty($purchase)) {
- util::fail('没有采购单信息');
- }
- $purchase->status = PurchaseClass::STATUS_SENDING;
- $purchase->save();
- $shopId = $purchase->shopId;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店62');
- }
- $shop->cgUnSend -= 1;
- $shop->cgSending += 1;
- $shop->save();
- //自动完成订单时不需要通知
- $noNeedWxNotice = isset(Yii::$app->params['noNeedWxNotice']) ? Yii::$app->params['noNeedWxNotice'] : 0;
- if ($noNeedWxNotice == 0) {
- WxMessageClass::cgOrderHasSend($shop, $purchase);
- }
- }
- public static function valid($cg, $shopId)
- {
- if (isset($cg->shopId) == false || $cg->shopId != $shopId) {
- util::fail('无法访问');
- }
- }
- }
|