| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980 |
- <?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\order\traits\OrderTrait;
- use bizGhs\product\classes\ProductClass;
- 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\orderSn;
- use common\components\util;
- use bizHd\base\classes\BaseClass;
- use Yii;
- 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;
- //配送
- const GET_TYPE_SEND = 1;
- //自取
- const GET_TYPE_SELF_GET = 2;
- const REFUND_NO = 1;
- const REFUND_YES = 2;
- //计算距离和运费 ssh 20221003
- public static function getDistanceFee($shop, $ghsShop, $weight)
- {
- $shopLat = isset($shop->lat) ? $shop->lat : '';
- $shopLong = isset($shop->long) ? $shop->long : '';
- $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : '';
- $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : '';
- //跑腿费用设置,基础费用8—10元,3—100公里每公里增加2元,超过5公斤,5—21公斤每公斤增加2元
- //起步价
- $baseFee = 9;
- $distance = mapUtil::calculateDistance($shopLong, $shopLat, $ghsShopLong, $ghsShopLat);
- //每公里增加多少元
- $perKmFee = 2;
- //每公斤增加多少元
- $perKiloFee = 2;
- //免费距离
- $freeDistance = 2;
- $freeKm = 5;
- $addDistanceFee = 0;
- if ($distance > $freeDistance * 1000) {
- $addDistanceFee = ceil(($distance - $freeDistance * 1000) / 1000) * $perKmFee;
- }
- $addWeightFee = 0;
- if ($weight > $freeKm) {
- $addWeightFee = ceil(bcsub($weight, $freeKm, 2)) * $perKiloFee;
- }
- $addFee = bcadd($addDistanceFee, $addWeightFee, 2);
- $totalFee = bcadd($baseFee, $addFee, 2);
- $showDistance = sprintf("%.1f", ($distance / 1000));
- return ['distance' => $distance, 'showDistance' => $showDistance, 'fee' => $totalFee];
- }
- //到货之后 ssh 20220321
- public static function arrival($cg, $data)
- {
- if ($cg->stockChange == 1) {
- util::fail('您已操作过了');
- }
- $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 = 0;
- $currentPrice = bcadd($itemPrice, $sendCost, 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->status = self::STATUS_COMPLETE;
- $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信息');
- }
- $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_UN_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);
- //微信收款的原路退回
- ini_set('date.timezone', 'Asia/Shanghai');
- $wx = Yii::getAlias("@vendor/wxPayApi_v3.0.10");
- require_once($wx . '/lib/WxPay.Api.php');
- require_once($wx . '/example/WxPay.Config.php');
- $totalFee = $cg->bookPrice * 100;
- $refundFee = $refundPrice * 100;
- $transaction_id = $cg->thirdNo ?? '';
- $merchantExt = WxOpenClass::getWxInfo();
- $mid = $merchantExt['wxPayMerchantId'] ?? '';
- $appId = $merchantExt['miniAppId'] ?? '';
- $key = $merchantExt['wxPayKey'] ?? '';
- $input = new \WxPayRefund();
- $input->SetTransaction_id($transaction_id);
- $input->SetTotal_fee($totalFee);
- $input->SetRefund_fee($refundFee);
- $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-refund-callback/');
- $config = new \WxPayConfig();
- $config->SetAppId($appId);
- $config->SetMerchantId($mid);
- $config->SetKey($key);
- $input->SetOut_refund_no($refundOrderSn);
- $input->SetOp_user_id($mid);
- $return = \WxPayApi::refund($config, $input);
- if (isset($return['return_code']) == false || $return['return_code'] != 'SUCCESS') {
- $msg = $return['return_msg'] ?? '';
- Yii::info("退款失败 cgId:{$cgId} msg:{$msg}");
- util::fail('退款失败');
- }
- $wxRefundId = $return['refund_id'] ?? '';
- $cgRefund->thirdRefundNo = $wxRefundId;
- $cgRefund->save();
- $cg->refundPrice = $refundPrice;
- $cg->tkPrice = bcadd($cg->tkPrice, $refundPrice, 2);
- $cg->refund = self::REFUND_YES;
- $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)
- {
- $orderSn = orderSn::getPurchaseSn();
- $data['orderSn'] = $orderSn;
- $prePrice = 0;
- $sendTimeWant = isset($data['sendTimeWant']) && !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
- if ($sendTimeWant < date("Y-m-d")) {
- util::fail('您选了过去时间');
- }
- $data['sendTimeWant'] = $sendTimeWant;
- if (isset($data['product']) == false || empty($data['product'])) {
- util::fail('请选择花材');
- }
- $productList = $data['product'];
- $productList = self::mergeItemInfo($productList);
- $data['kindNum'] = count($productList); //多少种花材
- //传过来的是供货商的productId,需要转换为零售端的productId
- //数据格式 [{"productId":"113","bigNum":1,"smallNum":0,"classId":"-2","hdProductId":1}]
- $shopId = $data['shopId'] ?? 0;
- $sjId = $data['sjId'] ?? 0;
- $mainId = $data['mainId'] ?? 0;
- $productList = ProductClass::toggleProductList($productList, $shopId, $sjId, $mainId);
- $productData = self::getProductMapData($productList, "productId");
- $hdProductData = self::getProductMapData($productList, "hdProductId");
- $giveLevel = $ghs['giveLevel'] ?? 0;
- $priceMap = CustomClass::$levelPriceKeyMap;
- $addPriceMap = CustomClass::$levelAddPriceKeyMap;
- //计算价格= 各个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;
- $productId = $product['productId'];
- $hdProductId = $product['hdProductId'];
- $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);
- //供货商开单改价,这个价格优先级最高
- $changePrice = isset($product['userPrice']) && $product['userPrice'] > 0 ? $product['userPrice'] : 0;
- $currentProduct = $productData[$productId] ?? [];
- $price = ProductClass::getFinalPrice($currentProduct, $giveLevel, $priceMap, $addPriceMap, $changePrice);
- $bigPrice = $price;
- if ($currentSmallNum > 0) {
- //如果是小单位数量,则传过来的就是小单位价格,无需转换
- if (empty($changePrice)) {
- $ghsRatio = $currentProduct['ratio'] ?? 0;
- $ghsSmallRatio = $currentProduct['smallRatio'] ?? 1;
- $price = bcdiv($price, $ghsRatio, 2);
- $price = bcmul($price, $ghsSmallRatio, 2);
- }
- $productTotalPrice = bcmul($currentSmallNum, $price, 2);
- } else {
- $productTotalPrice = bcmul($currentBigNum, $price, 2);
- }
- $itemInfo = [
- 'itemName' => $hdProductData[$hdProductId]['name'] ?? '',//花材名称
- 'itemCover' => $hdProductData[$hdProductId]['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'],//小单位名称 支
- ];
- $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'] = $bigPrice;
- $productList[$key]['ratio'] = $ratio;
- $productList[$key]['unitWeight'] = $unitWeight;
- $productList[$key]['totalPrice'] = $productTotalPrice;
- $productList[$key]['preTotalPrice'] = $productTotalPrice;
- $productList[$key]['productId'] = $hdProductId;
- $productList[$key]['ghsProductId'] = $productId;
- $productList[$key]['smallPrice'] = $price;
- $productList[$key]['mainId'] = $mainId;
- if ($currentBigNum > 0) {
- $xhNum = $currentBigNum;
- $xhUnitName = $hdProductData[$hdProductId]['bigUnit'] ?? '';
- $xhUnitType = dict::getDict('unitType', 'big');
- } else {
- $xhNum = $currentSmallNum;
- $xhUnitName = $hdProductData[$hdProductId]['smallUnit'] ?? '';
- $xhUnitType = dict::getDict('unitType', 'small');
- }
- $itemPrice = $price;
- $price = bcmul($itemPrice, $xhNum, 2);
- $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);
- $data['weight'] = $totalWeight;
- //预订单需要增加服务费
- $book = $data['book'] ?? 0;
- 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;
- $realPrice = $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;
- $realPrice = $modifyPrice;
- }
- $data['prePrice'] = $prePrice;
- $data['bookPrice'] = $actPrice;
- $data['orderPrice'] = $actPrice;
- $data['actPrice'] = $actPrice;
- $data['realPrice'] = $realPrice;
- $data['bigNum'] = $bigNum;
- $data['smallNum'] = $smallNum;
- $data['discount'] = 1;
- //采购单有效期
- $currentTime = time();
- $validTime = dict::getDict('order_pay_has_time');
- $current = $currentTime + $validTime;
- $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
- //采购单自动记为欠款单时间
- $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('没有找到门店');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息');
- }
- $main->totalPurchaseOrder += 1;
- $main->cgUnPay += 1;
- $main->save();
- return $respond;
- }
- //添加采购 ssh 2021.1.18
- public static function updatePurchase($purchase, $data)
- {
- $orderSn = $purchase->orderSn ?? '';
- $shopId = $purchase->shopId ?? 0;
- $sjId = $purchase->sjId ?? 0;
- $prePrice = 0;
- $data['sendTimeWant'] = date("Y-m-d") . ' ' . $data['sendTime'];
- unset($data['sendTime']);
- if (isset($data['product']) == false || empty($data['product'])) {
- util::fail('请选择花材');
- }
- $productList = $data['product'];
- $productList = self::mergeItemInfo($productList);
- $data['kindNum'] = count($productList); //多少种花材
- //供货商productId转成零售的productId
- $mainId = $purchase->mainId ?? 0;
- $productList = ProductClass::toggleProductList($productList, $shopId, $sjId, $mainId);
- $productData = self::getProductMapData($productList, "productId");
- $hdProductData = self::getProductMapData($productList, "hdProductId");
- //计算价格= 各个productId的price*num(bigNum,smallNum) + sendCost(配送费)
- $bigNum = 0; //总共多少扎
- $smallNum = 0; //总共多少支
- foreach ($productList as $key => $product) {
- $bigNum += $product['bigNum'] ?? 0;
- $smallNum += $product['smallNum'] ?? 0;
- $productList[$key]['orderSn'] = $orderSn;
- $productId = $product['productId'];
- $hdProductId = $product['hdProductId'];
- $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);//采购数量
- $price = $productData[$productId]['price']; //卖的单价
- //每个花材总价(原价)
- $productPrePrice = bcmul($itemNum, $price, 2); //采购总价
- //供货商开单的每个花材总价,可能会改价
- $productTotalPrice = isset($product['userPrice']) && $product['userPrice'] > 0 ? $product['userPrice'] : $productPrePrice;
- $itemInfo = [
- 'itemName' => $hdProductData[$hdProductId]['name'] ?? '',//花材名称
- 'itemCover' => $hdProductData[$hdProductId]['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'],//小单位名称 支
- ];
- $productList[$key]['itemInfo'] = json_encode($itemInfo);
- $productList[$key]['itemId'] = $itemId;
- $productList[$key]['itemNum'] = $itemNum;
- $productList[$key]['price'] = $price;
- $productList[$key]['ratio'] = $ratio;
- $productList[$key]['totalPrice'] = $productTotalPrice;
- $productList[$key]['preTotalPrice'] = $productPrePrice;
- $productList[$key]['productId'] = $hdProductId;
- $productList[$key]['ghsProductId'] = $productId;
- //小单位单价
- $smallPrice = bcdiv($price, $ratio, 2);
- $productList[$key]['smallPrice'] = $smallPrice;
- $prePrice = bcadd($prePrice, $productTotalPrice, 2);
- }
- //删除花材再添加
- PurchaseItemClass::deleteByCondition(['orderSn' => $orderSn]);
- PurchaseItemClass::batchAdd($productList);
- $data['itemPrice'] = $prePrice;
- $sendCost = $data['sendCost'] ?? 0.00;
- $prePrice = bcadd($prePrice, $sendCost, 2);
- $actPrice = $prePrice;
- $realPrice = $prePrice;
- if (isset($data['modifyPrice'])) {
- //商家打折,修改金额
- $modifyPrice = $data['modifyPrice'] ?? 0;
- if ($modifyPrice > $prePrice) {
- util::fail('实收金额大于订单总金额');
- }
- if ($modifyPrice < $prePrice) {
- $data['discountAmount'] = bcsub($prePrice, $modifyPrice, 2);
- $data['discountType'] = dict::getDict('discountType', 'discount');
- $actPrice = $modifyPrice;
- $realPrice = $modifyPrice;
- }
- } else {
- //验证优惠券
- $hbId = isset($data['hbId']) ? $data['hbId'] : 0;
- if (!empty($hbId)) {
- $coupon = ShopCouponClass::getLockById($hbId);
- ShopCouponClass::valid($coupon, $data['shopId']);
- if ($coupon->miniCost != 0 && $coupon->miniCost > $prePrice) {
- util::fail('达到最低消费才能使用优惠券');
- }
- if ($prePrice < $coupon->amount) {
- util::fail('总价应大于优惠金额');
- }
- $coupon->take = ShopCouponClass::TAKE_HAS;
- $coupon->save();
- $realPrice = bcsub($actPrice, $coupon->amount, 2);
- $data['discountType'] = dict::getDict('discountType', 'usePtCoupon');
- $data['discountAmount'] = $coupon->amount;
- $data['hbId'] = $hbId;
- }
- }
- $data['prePrice'] = $prePrice;
- $data['actPrice'] = $actPrice;
- $data['realPrice'] = $realPrice;
- $data['bigNum'] = $bigNum;
- $data['smallNum'] = $smallNum;
- //采购单有效期
- $validTime = dict::getDict('order_pay_has_time');
- $current = time() + $validTime;
- $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
- $purchaseId = $purchase->id ?? 0;
- self::updateById($purchaseId, $data);
- $respond = self::getLockById($purchaseId);
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $shop->cancelOrder -= 1;
- $shop->cgUnPay += 1;
- $shop->save();
- return $respond;
- }
- //组合数据 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'] ?? '';
- $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;
- $tmp['cover'] = self::groupImg($info['itemCover']);
- unset($v['itemInfo']);
- if (!isset($tmp['rank'])) {
- $tmp['rank'] = 'B';
- }
- $tmp['name'] = $tmp['itemName'];
- $tmp = array_merge($tmp, $v);
- 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->save();
- $shopId = $purchase->shopId;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息');
- }
- $main->cgUnPay -= 1;
- $main->cgCancel += 1;
- $main->save();
- 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('没有找到门店');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息');
- }
- $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('没有找到门店');
- }
- $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('没有权限访问');
- }
- }
- }
|