| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363 |
- <?php
- namespace bizGhs\order\classes;
- use biz\shop\classes\ShopAdminClass;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopClass;
- use biz\shop\classes\ShopExtClass;
- use biz\shop\models\Shop;
- use biz\sj\classes\SjClass;
- use biz\stat\classes\StatItemClass;
- use biz\stat\classes\StatKdClass;
- use biz\stat\classes\StatKhCgClass;
- use biz\stat\classes\StatOrderCountClass;
- use biz\stat\classes\StatOutClass;
- use biz\stat\classes\StatRefundClass;
- use biz\stat\classes\StatSaleClass;
- use biz\stock\classes\StockClass;
- use bizGhs\custom\models\Custom;
- use bizGhs\express\services\DadaExpressServices;
- use bizGhs\order\models\Order;
- use bizGhs\order\traits\OrderTrait;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\admin\classes\AdminClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\shop\classes\MainClass;
- use bizGhs\stat\classes\StatYjClass;
- use bizGhs\stock\classes\StockRecordClass;
- use bizGhs\stock\services\StockRecordService;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\stat\classes\StatIncomeClass;
- use bizHd\stat\classes\StatOrderClass;
- use common\components\dict;
- use common\components\httpUtil;
- use common\components\imgUtil;
- use common\components\orderSn;
- use common\components\printUtil;
- use common\components\stringUtil;
- use common\components\util;
- use bizGhs\base\classes\BaseClass;
- use Yii;
- class OrderClass extends BaseClass
- {
- use OrderTrait;
- public static $baseFile = '\bizGhs\order\models\Order';
- //已取消
- const ORDER_STATUS_CANCEL = 5;
- //已完成
- const ORDER_STATUS_COMPLETE = 4;
- //配送中
- const ORDER_STATUS_SENDING = 3;
- //待配送
- const ORDER_STATUS_UN_SEND = 2;
- //待付款,待确认
- const ORDER_STATUS_UN_PAY = 1;
- //未确认
- const SEND_TYPE_UNKNOWN = 1;
- //自取
- const SEND_TYPE_NO = 2;
- //自己送
- const SEND_TYPE_MYSELF = 3;
- //快递送
- const SEND_TYPE_THIRD = 4;
- public static $sendTypeMap = [
- self::SEND_TYPE_UNKNOWN => '未确认',
- self::SEND_TYPE_NO => '自取',
- self::SEND_TYPE_MYSELF => '商家配送',
- self::SEND_TYPE_THIRD => '快递配送',
- ];
- //待付款
- const PAY_STATUS_UN_PAY = 0;
- //已付款
- const PAY_STATUS_HAS_PAY = 1;
- //待配送
- const SEND_STATUS_UN_SEND = 1;
- //配送中
- const SEND_STATUS_SENDING = 2;
- //配送完成
- const SEND_STATUS_COMPLETE = 3;
- //订单总流程数
- const TOTAL_FLOW = 3;
- //清算方式
- //欠款
- const CLEAR_DEBT = 1;
- //已付款
- const CLEAR_HAS_GATHERING = 2;
- //没有欠款
- const DEBT_NO = 0;
- //欠款
- const DEBT_YES = 1;
- //正常订单
- const REFUND_NO = 1;
- //已退款订单
- const REFUND_YES = 2;
- public static $refundMap = [
- self::REFUND_NO => '',
- self::REFUND_YES => '有退款',
- ];
- public static function arrival($order, $data)
- {
- if ($order->stockChange == 1) {
- util::fail('您已操作过了');
- }
- $sjId = $order->sjId ?? 0;
- $shopId = $order->shopId ?? 0;
- $mainId = $order->mainId ?? 0;
- //product [{"productId":"3674","num":0,"price":1}]
- $product = $data['product'] ?? '';
- if (empty($product)) {
- util::fail('没有花材?');
- }
- $ids = [];
- $format = [];
- foreach ($product as $val) {
- $productId = $val['productId'] ?? 0;
- $ids[] = $productId;
- $format[$productId] = $val;
- }
- $info = ProductClass::getByIds($ids, null, 'id');
- $orderSn = $order->orderSn ?? '';
- $customName = $order->customName ?? '';
- $list = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (empty($list)) {
- util::fail('没有找到订单的花材');
- }
- $itemPrice = 0;
- $weight = 0;
- foreach ($list as $key => $val) {
- $productId = $val->productId ?? 0;
- $num = $format[$productId]['num'] ?? 0;
- $unitPrice = $format[$productId]['price'] ?? 0;
- $price = bcmul($unitPrice, $num, 2);
- $name = $val->name ?? '';
- $xhUnitName = $val->xhUnitName ?? '';
- $xhPreNum = $val->xhPreNum ?? 0;
- if ($unitPrice <= 0) {
- util::fail('花材金额需要大于0');
- }
- if ($num > $xhPreNum) {
- util::fail($name . "数量不能超过{$xhPreNum}{$xhUnitName}");
- }
- $val->cost = $unitPrice;
- $val->unitPrice = $unitPrice;
- $val->price = $price;
- $val->bigNum = $num;
- $val->num = $num;
- $val->xhNum = $num;
- $val->xhUnitPrice = $unitPrice;
- $val->xhPrice = $price;
- $val->save();
- if ($val->xhPreNum > $num) {
- //客户下单退货数增加
- $subNum = bcsub($val->xhPreNum, $num);
- StatKhCgClass::ghsBookOrderRefundNum($order, $subNum);
- //花材销量榜减少
- StatItemClass::ghsBookOrderRefundNum($order, $val, $subNum);
- }
- $itemPrice = bcadd($itemPrice, $price, 2);
- $productWeight = $info[$productId]['weight'] ?? 0;
- $currentWeight = bcmul($productWeight, $num, 2);
- $weight = bcadd($currentWeight, $weight, 2);
- //减少库存
- $unitType = $val->xhUnitType ?? 0;
- $bigNum = 0;
- $smallNum = 0;
- if ($unitType == dict::getDict('unitType', 'big')) {
- $bigNum = $num;
- $itemNum = $num;
- } else {
- $smallNum = $num;
- $itemNum = bcdiv($num, $val->ratio, 2);
- }
- $checkStock = true;
- $stockInfo = ProductClass::decreaseStock($productId, $bigNum, $smallNum, $checkStock);
- $recordData = [];
- $recordData['sjId'] = $sjId;
- $recordData['shopId'] = $shopId;
- $recordData['mainId'] = $mainId;
- $recordData['itemId'] = $val->itemId;
- $recordData['itemNum'] = $itemNum;
- $recordData['oldStock'] = $stockInfo['oldStock'];
- $recordData['newStock'] = $stockInfo['newStock'];
- $recordData['productId'] = $productId;
- $recordData['orderSn'] = $orderSn;
- $recordData['relateName'] = $customName;
- $recordData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
- StockRecordClass::addSellStockOrderRecord($recordData);
- }
- $order->itemPrice = $itemPrice;
- $sendCost = 0;
- $currentPrice = bcadd($itemPrice, $sendCost, 2);
- $shopId = $order->shopId ?? 0;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息');
- }
- $main->unSendOrder -= 1;
- $main->finishOrder += 1;
- $main->save();
- $kiloFee = $shop->kiloFee ?? 0;
- $miniKilo = $shop->miniKilo ?? 0;
- $calcWeight = $weight > $miniKilo ? $weight : $miniKilo;
- $allKiloFee = bcmul($calcWeight, $kiloFee, 2);
- $currentPrice = bcadd($currentPrice, $allKiloFee, 2);
- $order->prePrice = $currentPrice;
- $order->orderPrice = $currentPrice;
- $order->actPrice = $currentPrice;
- $order->realPrice = $currentPrice;
- $order->stockChange = 1;
- $order->status = self::ORDER_STATUS_COMPLETE;
- $order->save();
- $bookPrice = $order->bookPrice ?? 0;
- $mainId = $shop->mainId ?? 0;
- $sjId = $shop->sjId ?? 0;
- $customId = $order->customId ?? 0;
- $relateOrderSn = $order->orderSn ?? '';
- $shopAdminId = $data['shopAdminId'] ?? 0;
- $shopAdminName = $data['shopAdminName'] ?? '';
- $refund = null;
- if ($bookPrice != $currentPrice) {
- if ($bookPrice > $currentPrice) {
- $refundPrice = bcsub($bookPrice, $currentPrice, 2);
- $order->refundPrice = $refundPrice;
- $order->tkPrice = bcadd($order->tkPrice, $refundPrice, 2);
- $order->refund = self::REFUND_YES;
- $order->save();
- $orderSn = orderSn::getGhsRefundSn();
- $book = 1;
- $refundData = [
- 'mainId' => $mainId,
- 'shopId' => $shopId,
- 'sjId' => $sjId,
- 'customId' => $customId,
- 'orderSn' => $orderSn,
- 'relateOrderSn' => $relateOrderSn,
- 'book' => $book,
- 'status' => RefundOrderClass::STATUS_UN_COMPLETE,
- 'shopAdminId' => $shopAdminId,
- 'shopAdminName' => $shopAdminName,
- 'refundPrice' => $refundPrice,
- 'refundType' => RefundOrderClass::REFUND_TYPE_MONEY,
- ];
- //退款
- $refund = RefundOrderClass::add($refundData, true);
- //余额减少
- ShopClass::ghsBookOrderRefundReduceBalance($main, $shop, $refund, $refundPrice);
- //总支出增加
- $currentTotalExpend = bcadd($main->totalExpend, $refundPrice, 2);
- $main->totalExpend = $currentTotalExpend;
- $totalRefund = bcadd($main->totalRefund, $refundPrice, 2);
- $main->totalRefund = $totalRefund;
- $main->save();
- //支出流水
- $thisType = dict::getDict('capitalType', 'ghsBookRefund', 'id');
- $sjId = $order->sjId ?? 0;
- $event = "预订单多付退给{$customName}";
- $capitalData = [
- 'capitalType' => $thisType,
- 'io' => 0,
- 'totalExpend' => $currentTotalExpend,
- 'payWay' => 0,
- 'amount' => $refundPrice,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capitalData);
- //支出统计
- StatOutClass::updateOrInsert($main, $shop, $refundPrice);
- //退款统计
- $payWay = $order->payWay ?? 0;
- StatRefundClass::replace($main, $shop, $payWay, $refundPrice);
- //供货商预订单退款给客户,客户采购金额减少
- StatKhCgClass::ghsBookOrderChange(false, $order, $refundPrice);
- } else {
- //欠款
- $currentDebt = bcsub($currentPrice, $bookPrice, 2);
- $order->debtPrice = $currentDebt;
- $order->remainDebtPrice = $currentDebt;
- $order->debt = OrderClass::DEBT_YES;
- $order->save();
- $currentTotalIncome = bcadd($main->totalIncome, $currentDebt, 2);
- $main->totalIncome = $currentTotalIncome;
- $main->save();
- $payWay = dict::getDict('payWay', 'debtPay');
- $custom = CustomClass::getLockById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- CustomClass::cgDebtAmountAdd($custom, $order, true);
- //收入增加
- StatIncomeClass::updateOrInsert($main, $shop, $currentDebt);
- //收入流水增加
- $orderId = $order->id;
- $capitalType = dict::getDict('capitalType', 'ghsBookOrderGetBack', 'id');
- $shopAdminId = $order->shopAdminId ?? 0;
- $shopAdminName = $order->shopAdminName ?? '';
- $event = "预订单 {$orderSn} 差价补款";
- $mainId = $shop->mainId ?? 0;
- $sjId = $order->sjId ?? 0;
- $shopId = $order->shopId ?? 0;
- $capital = [
- 'capitalType' => $capitalType,
- 'relateId' => $orderId,
- 'io' => 1,
- 'amount' => $currentDebt,
- 'totalIncome' => $currentTotalIncome,
- 'payWay' => $payWay,
- 'event' => $event,
- 'mainId' => $mainId,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'shopAdminId' => $shopAdminId,
- 'shopAdminName' => $shopAdminName,
- ];
- ShopCapitalClass::addCapital($capital);
- //各渠道收入金额统计
- StatKdClass::replace($shop, $currentDebt, $payWay);
- //每天销售收入统计
- StatSaleClass::replace($main, $shop, $currentDebt);
- //预订单,客户补款给供货商,客户采购金额增加
- StatKhCgClass::ghsBookOrderChange(true, $order, $currentDebt);
- }
- }
- return ['kiloFee' => $kiloFee, 'refund' => $refund, 'miniKilo' => $miniKilo];
- }
- //添加订单 ssh 2019.12.5
- public static function addOrder($data)
- {
- $orderSn = orderSn::getGhsOrderSn();
- $data['orderSn'] = $orderSn;
- $data['payStatus'] = self::PAY_STATUS_UN_PAY;
- $data['customId'] = isset($data['customId']) && !empty($data['customId']) ? $data['customId'] : 0;
- $currentShopId = $data['shopId'] ?? 0;
- $shop = ShopClass::getLockById($currentShopId);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息');
- }
- //花材
- $product = $data['product'];
- $book = $data['book'] ?? 0;
- $stockMayChange = true;
- $data['stockChange'] = 1;
- if ($book == 1) {
- $stockMayChange = false;
- $data['stockChange'] = 0;
- }
- $respond = OrderItemClass::replaceItem($orderSn, $product, $data, $stockMayChange);
- $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
- $data['sendCost'] = $sendCost;
- $data['bigNum'] = $respond['bigNum'] ?? 0;
- $data['smallNum'] = $respond['smallNum'] ?? 0;
- $data['itemNum'] = $respond['totalItemNum'] ?? 0;
- //默认订单要打单
- $data['needPrint'] = $data['needPrint'] ?? dict::getDict('needPrint', 'need');
- $weight = $respond['weight'] ?? 0;
- $data['weight'] = $weight;
- $itemPrice = $respond['price'] ?? 0;
- $data['itemPrice'] = $itemPrice;
- $currentPrice = bcadd($itemPrice, $sendCost, 2);
- //预订单需要增加服务费
- if ($book == 1) {
- $kiloFee = $shop->kiloFee ?? 0;
- $miniKilo = $shop->miniKilo ?? 0;
- $calcWeight = $weight > $miniKilo ? $weight : $miniKilo;
- $allKiloFee = bcmul($calcWeight, $kiloFee, 2);
- $currentPrice = bcadd($currentPrice, $allKiloFee, 2);
- $data['allKiloFee'] = $allKiloFee;
- $data['kiloFee'] = $kiloFee;
- $data['miniKilo'] = $miniKilo;
- }
- $data['prePrice'] = $currentPrice;
- $actPrice = $currentPrice;
- $realPrice = $currentPrice;
- //商家自己开单时
- if (isset($data['modifyPrice'])) {
- $modifyPrice = $data['modifyPrice'] ?? 0;
- if ($modifyPrice <= 0) {
- util::fail('收款金额必须大于0');
- }
- if ($modifyPrice > $currentPrice) {
- //人工和资材费用
- $data['labourCost'] = bcsub($modifyPrice, $currentPrice, 2);
- }
- if ($modifyPrice < $currentPrice) {
- //优惠金额
- $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
- $data['discountType'] = dict::getDict('discountType', 'discount');
- }
- $actPrice = $modifyPrice;
- $realPrice = $modifyPrice;
- }
- //客户采购时使用红包时
- if (isset($data['discountType']) && $data['discountType'] == dict::getDict('discountType', 'hb')) {
- $discountAmount = $data['discountAmount'] ?? 0;
- $currentDiscountPrice = bcsub($currentPrice, $discountAmount, 2);
- $actPrice = $currentDiscountPrice;
- $realPrice = $currentDiscountPrice;
- }
- $data['actPrice'] = $actPrice;
- $data['bookPrice'] = $actPrice;
- $data['orderPrice'] = $actPrice;
- $data['realPrice'] = $realPrice;
- $data['sendTimeWant'] = isset($data['sendTimeWant']) && !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
- //订单有效期
- $validTime = dict::getDict('order_pay_has_time');
- $current = time() + $validTime;
- $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
- $data['status'] = self::ORDER_STATUS_UN_PAY;
- $main->totalOrder += 1;
- $main->unPayOrder += 1;
- $main->save();
- //将当天日期+花店每天的总订单数作为编号
- $data['sendNum'] = date("j") . StatOrderCountClass::addOrder($shop, $main);
- //订单有效期
- $currentTime = time();
- $validTime = dict::getDict('order_pay_has_time');
- $current = $currentTime + $validTime;
- $data['deadline'] = isset($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);
- $return = self::add($data, true);
- return $return;
- }
- //修改订单 ssh 2019.12.5
- public static function updateOrder($order, $data)
- {
- $sjId = $order->sjId ?? 0;
- $orderSn = $order->orderSn ?? 0;
- $currentShopId = $order->shopId ?? 0;
- $data['payStatus'] = self::PAY_STATUS_UN_PAY;
- //花材列表
- $product = $data['product'];
- //删除花材再添加
- OrderItemClass::deleteByCondition(['orderSn' => $orderSn]);
- $respond = OrderItemClass::replaceItem($orderSn, $product, $data);
- $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
- $data['sendCost'] = $sendCost;
- $data['bigNum'] = $respond['bigNum'] ?? 0;
- $data['smallNum'] = $respond['smallNum'] ?? 0;
- $price = $respond['price'] ?? 0;
- $currentPrice = bcadd($price, $sendCost, 2);
- $data['prePrice'] = $currentPrice;
- if (isset($data['modifyPrice'])) {
- //商家打折,修改金额
- $modifyPrice = $data['modifyPrice'] ?? 0;
- if ($modifyPrice > $currentPrice) {
- util::fail('实收金额大于订单总金额');
- }
- if ($modifyPrice < $currentPrice) {
- $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
- $data['discountType'] = dict::getDict('discountType', 'discount');
- $currentPrice = $modifyPrice;
- }
- }
- $data['actPrice'] = $currentPrice;
- $data['realPrice'] = $currentPrice;
- //如果有平台补贴
- if (isset($data['discountType']) && $data['discountType'] == dict::getDict('discountType', 'usePtCoupon')) {
- $discountAmount = $data['discountAmount'] ?? 0;
- $data['realPrice'] = bcsub($currentPrice, $discountAmount, 2);
- }
- $data['sendTime'] = isset($data['sendTime']) && !empty($data['sendTime']) ? $data['sendTime'] : '0000-00-00 00:00:00';
- //订单有效期
- $validTime = dict::getDict('order_pay_has_time');
- $current = time() + $validTime;
- $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
- $data['status'] = self::ORDER_STATUS_UN_PAY;
- //计算总重量(kg)
- $weight = 0;
- $productMapData = ProductClass::getProductMapData($product);
- foreach ($product as $key => $val) {
- $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
- $productId = $val['productId'];
- $weight = bcadd($w, $weight, 2);
- $checkStock = true;
- $stockInfo = ProductClass::decreaseStock($val['productId'], $val['bigNum'], $val['smallNum'], $checkStock);
- //记录库存流水
- $recordData = [];
- $recordData['sjId'] = $sjId;
- $recordData['shopId'] = $currentShopId;
- $recordData['itemId'] = $productMapData[$productId]['itemId'] ?? 0;
- $ratio = $productMapData[$productId]['ratio'] ?? 0;
- $recordData['itemNum'] = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
- $recordData['oldStock'] = $stockInfo['oldStock'];
- $recordData['newStock'] = $stockInfo['newStock'];
- $recordData['productId'] = $productId;
- $recordData['orderSn'] = $orderSn;
- StockRecordClass::addSellStockOrderRecord($recordData);
- }
- $data['weight'] = $weight;
- $current = time() + 300;
- $data['deadline'] = isset($data['deadline']) ? $data['deadline'] : date("Y-m-d H:i:s", $current);
- //修改订单暂时不打印
- $data['needPrint'] = $data['needPrint'] ?? dict::getDict('needPrint', 'need');
- $orderId = $order->id ?? 0;
- self::updateById($orderId, $data);
- $return = self::getLockById($orderId);
- //门店总订单数+1
- $shop = ShopClass::getLockById($currentShopId);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $shop->unPayOrder += 1;
- $shop->cancelOrder -= 1;
- $shop->save();
- return $return;
- }
- //根据订单编号查询 ssh 2021.19
- public static function getByOrderSn($orderSn, $returnObj = false)
- {
- return self::getByCondition(['orderSn' => $orderSn], $returnObj);
- }
- //根据编号列表查订单
- public static function getOrderInfoBySnList($snList)
- {
- $list = self::getAllByCondition(['orderSn' => ['in', $snList]], null, '*', 'orderSn');
- $list = self::groupOrder($list, true, false, false, true);
- return $list;
- }
- //显示操作按钮
- public static function getShowButton($showButton, $isList, $order)
- {
- if ($showButton == false) {
- return [];
- }
- $print = ['type' => 'print', 'show' => 1, 'disable' => 0, 'name' => '打印'];
- $modifyProduct = ['type' => 'modifyProduct', 'show' => 0, 'disable' => 0, 'name' => '修改花材'];
- $confirm = ['type' => 'confirm', 'show' => 0, 'disable' => 1, 'name' => '确认'];
- $cancel = ['type' => 'cancel', 'show' => 0, 'disable' => 0, 'name' => '取消'];
- $arrival = ['type' => 'arrival', 'show' => 0, 'disable' => 0, 'name' => '到货'];
- if ($isList) {
- //订单列表页显示的按钮
- $send = ['type' => 'send', 'show' => 0, 'disable' => 0, 'name' => '发货'];
- $selfGet = ['type' => 'selfGet', 'show' => 0, 'disable' => 1, 'name' => '自取'];
- if ($order['status'] == self::ORDER_STATUS_UN_PAY) {
- //待确认订单,打印和修改花材按钮可以使用
- $send['disable'] = 1;
- $selfGet['disable'] = 1;
- $print['disable'] = 0;
- $confirm['disable'] = 0;
- }
- if ($order['status'] == self::ORDER_STATUS_UN_SEND) {
- //待配送订单,改花材按钮不可点,自取可点
- $selfGet['disable'] = 0;
- }
- //自取订单发货按钮置灰
- if ($order['sendType'] == self::SEND_TYPE_NO) {
- $send['disable'] = 1;
- }
- //取消订单置灰
- if ($order['status'] == self::ORDER_STATUS_CANCEL) {
- $send['disable'] = 1;
- $selfGet['disable'] = 1;
- $print['disable'] = 1;
- }
- if ($order['status'] == self::ORDER_STATUS_COMPLETE) {
- $send['name'] = '发货记录';
- }
- return [$send, $selfGet, $print, $confirm];
- } else {
- //订单详情页显示的按钮
- if (isset($order['book']) && $order['book'] == 1 && isset($order['stockChange']) && $order['stockChange'] == 0) {
- if ($order['status'] == self::ORDER_STATUS_UN_SEND) {
- $arrival['show'] = 1;
- }
- }
- //待确认订单显示延期付和已付款
- if ($order['status'] == self::ORDER_STATUS_UN_PAY) {
- $debtPay = ['type' => 'debtPay', 'show' => 0, 'disable' => 0, 'name' => '延期收款'];
- $hasPay = ['type' => 'hasPay', 'show' => 0, 'disable' => 0, 'name' => '欠款'];
- $confirm['disable'] = 0;
- return [$print, $debtPay, $hasPay, $confirm, $cancel, $arrival];
- }
- //待配送订单显示打印、发货和自取
- if ($order['status'] == self::ORDER_STATUS_UN_SEND) {
- $selfGet = ['type' => 'selfGet', 'show' => 0, 'disable' => 1, 'name' => '自取'];
- $send = ['type' => 'send', 'show' => 0, 'disable' => 1, 'name' => '发货'];
- if ($order['hasItem'] == 2) {
- return [$print, $selfGet, $send, $arrival];
- }
- $selfGet['disable'] = 0;
- $send['disable'] = 0;
- return [$print, $selfGet, $send, $arrival];
- }
- //配送中显示按钮
- if ($order['status'] == self::ORDER_STATUS_SENDING) {
- $send = ['type' => 'send', 'show' => 0, 'disable' => 0, 'name' => '发货'];
- return [$print, $send, $arrival];
- }
- //完成
- if ($order['status'] == self::ORDER_STATUS_COMPLETE) {
- $send = ['type' => 'send', 'show' => 0, 'disable' => 0, 'name' => '发货记录'];
- //自取订单不显示发货按钮
- if ($order['sendType'] == self::SEND_TYPE_NO) {
- $send['show'] = 0;
- }
- return [$print, $send, $arrival];
- }
- //取消订单置灰
- if ($order['status'] == self::ORDER_STATUS_CANCEL) {
- $selfGet['disable'] = 1;
- $send['disable'] = 1;
- $print['disable'] = 1;
- return [$modifyProduct, $arrival];
- }
- return [];
- }
- }
- //显示进度 ssh 2021.3.1
- public static function getShowProgress($showProgress, $order)
- {
- if ($showProgress == false) {
- return [];
- }
- $create = ['name' => '创建', 'complete' => 1];
- $unConfirm = ['name' => '待确认', 'complete' => 0];
- if ($order['status'] > 0) {
- $unConfirm['complete'] = 1;
- }
- $out = ['name' => '出库', 'complete' => 0];
- if ($order['status'] > 1) {
- $out['complete'] = 1;
- }
- return [$create, $unConfirm, $out];
- }
- //退款等需要使用原来的订单花材结构
- public static function getOriginalShowItem($showItem, $order)
- {
- if ($showItem == false) {
- return ['list' => [], 'itemStat' => []];
- }
- $orderSn = $order['orderSn'];
- $list = OrderItemClass::getOrderItem($orderSn);
- $totalPrice = 0;
- $totalBigNum = 0;
- $totalSmallNum = 0;
- $kind = 0;
- $itemStat = [];
- if (!empty($list)) {
- foreach ($list as $key => $val) {
- $list[$key]['property'] = $val['rank'] . '级';
- $price = $val['price'];
- $totalPrice = stringUtil::calcAdd($totalPrice, $price);
- $kind++;
- $currentBigNum = $val['bigNum'];
- $currentSmallNum = $val['smallNum'];
- $totalBigNum = stringUtil::calcAdd($totalBigNum, $currentBigNum);
- $totalSmallNum = stringUtil::calcAdd($totalSmallNum, $currentSmallNum);
- }
- $itemStat = ['kind' => $kind, 'bigNum' => $totalBigNum, 'smallNun' => $totalSmallNum, 'totalPrice' => $totalPrice];
- }
- return ['list' => $list, 'itemStat' => $itemStat];
- }
- public static function getShowItem($showItem, $order)
- {
- if ($showItem == false) {
- return ['list' => [], 'itemStat' => []];
- }
- $orderSn = $order['orderSn'];
- $list = OrderItemClass::getOrderItem($orderSn);
- $totalPrice = 0;
- $totalBigNum = 0;
- $totalSmallNum = 0;
- $kind = 0;
- $itemStat = [];
- //小菊增加部分
- $xjAddList = [];
- if (!empty($list)) {
- foreach ($list as $key => $val) {
- $price = $val['price'];
- $totalPrice = stringUtil::calcAdd($totalPrice, $price);
- $kind++;
- $currentBigNum = $val['bigNum'];
- $currentSmallNum = $val['smallNum'];
- $totalBigNum = stringUtil::calcAdd($totalBigNum, $currentBigNum);
- $totalSmallNum = stringUtil::calcAdd($totalSmallNum, $currentSmallNum);
- $list[$key]['property'] = '';
- //小菊
- if (isset($val['variety']) && $val['variety'] == 1) {
- $currentItemId = $val['id'] ?? 0;
- $xjList = OrderXjClass::getAllByCondition(['itemId' => $currentItemId], null, '*');
- if (!empty($xjList)) {
- //选了有颜色的小菊才把原小菊删除不显示出来
- unset($list[$key]);
- foreach ($xjList as $xjKey => $xj) {
- $xjBase = $val;
- $num = $xj['num'] ?? 0;
- $price = $xj['price'] ?? 0;
- $name = $xj['name'] ?? '';
- $cover = $xj['cover'] ?? '';
- if ($num > 0) {
- $bigTotalPrice = bcmul($price, $num, 2);
- $xjBase['cover'] = imgUtil::groupImg($cover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $xjBase['unitPrice'] = $price;
- $xjBase['price'] = floatval($bigTotalPrice);
- $xjBase['num'] = floatval($num);
- $xjBase['bigNum'] = floatval($num);
- $xjBase['name'] = $name;
- $xjBase['property'] = '';
- $xjBase['userPrice'] = $bigTotalPrice;
- $xjBase['xhNum'] = floatval($num);
- $xjBase['xhPreNum'] = floatval($num);
- $xjBase['xhPrice'] = floatval($bigTotalPrice);
- $xjAddList[] = $xjBase;
- }
- }
- }
- }
- }
- if (!empty($xjAddList)) {
- $list = array_merge($list, $xjAddList);
- }
- $itemStat = ['kind' => $kind, 'bigNum' => $totalBigNum, 'smallNun' => $totalSmallNum, 'totalPrice' => $totalPrice];
- }
- return ['list' => $list, 'itemStat' => $itemStat];
- }
- //订单列表 ssh 2021.1.19
- public static function getOrderList($where)
- {
- $data = self::getList('*', $where, 'addTime DESC');
- if (isset($data['list']) && !empty($data['list'])) {
- foreach ($data['list'] as $key => $val) {
- $data['list'][$key]['customAvatarUrl'] = imgUtil::groupImg('');
- }
- }
- return $data;
- }
- //整合订单员工和供货商等信息 ssh 2021.1.19
- //$isList 是否列表页
- public static function groupOrder($list, $showButton = false, $showItem = false, $showExpress = false, $isList = false)
- {
- if (empty($list)) {
- return $list;
- }
- $adminIds = array_unique(array_filter(array_column($list, 'adminId')));
- $adminInfo = AdminClass::getAdminByIds($adminIds, null, 'id');
- $customIds = array_column($list, 'customId');
- $customIds = array_unique(array_filter($customIds));
- $customInfoList = CustomClass::getByIds($customIds, null, 'id');
- foreach ($list as $key => $val) {
- $adminId = $val['adminId'] ?? 0;
- $list[$key]['adminName'] = $adminInfo[$adminId]['name'] ?? '';
- $list[$key]['customAddress'] = $val['fullAddress'] ?? '';
- //显示列表页的操作按钮
- $list[$key]['buttonList'] = self::getShowButton($showButton, $isList, $val);
- //为了列表页的打印按钮能用,列表也强制显示花材
- $showItem = true;
- //退款需要显示原有的花材结构,不显示小菊的多颜色
- $getOriginalItem = Yii::$app->request->get('getOriginalItem', 0);
- $getOriginalItem2 = Yii::$app->request->post('getOriginalItem', 0);
- if ($getOriginalItem == 1 || $getOriginalItem2 == 1) {
- $showItemData = self::getOriginalShowItem($showItem, $val);
- } else {
- $showItemData = self::getShowItem($showItem, $val);
- }
- $list[$key]['product'] = $showItemData['list'];
- $list[$key]['productStat'] = $showItemData['itemStat'];
- $showResetItem = 0;
- //待付款订单显示改花材按钮
- if ($val['status'] == self::PAY_STATUS_UN_PAY) {
- $showResetItem = 1;
- }
- $list[$key]['showResetItem'] = $showResetItem;
- //获取配送进展
- $expressProgress = '';
- if ($val['sendType'] == 4) {
- $expressInfo = OrderExpressClass::getByCondition(['orderSn' => $val['orderSn']]);
- if ($expressInfo) {
- $expressProgress = ['progressName' => OrderExpressClass::$statusMap[$expressInfo['status']] ?? ''];
- }
- }
- $list[$key]['sendTypeName'] = self::$sendTypeMap[$val['sendType']] ?? '未知';
- $list[$key]['expressProgress'] = $expressProgress;
- $list[$key]['customAvatarUrl'] = imgUtil::groupImg($val['customAvatar']);
- $list[$key]['customName'] = isset($val['customId']) && !empty($val['customId']) ? $val['customName'] : '散客';
- $customId = $val['customId'] ?? '';
- $debtAmount = $customInfoList[$customId]['debtAmount'] ?? 0.00;
- //打印数据组合
- $printData = self::getPrintData($val, $debtAmount, $showItemData);
- $list[$key]['printData'] = json_encode($printData);
- $list[$key]['debtAmount'] = $debtAmount;
- $list[$key]['customShopId'] = $customInfoList[$customId]['shopId'] ?? 0;
- }
- return $list;
- }
- public static function printGroup($current, $content)
- {
- //58mm的机器,一行打印16个汉字,32个字母
- $name = $current['name'] ?? '';
- $content .= $name . '<BR>';
- $name = str_repeat(' ', 10);
- $productNum = $current['num'] ?? '';
- $blankNum = bcsub(12, strlen($productNum));
- if ($blankNum > 0) {
- $productNum = $productNum . str_repeat(' ', $blankNum);
- }
- $productPrice = $current['price'] ?? '';
- $blankNum = bcsub(7, strlen($productPrice));
- if ($blankNum > 0) {
- $productPrice = $productPrice . str_repeat(' ', $blankNum);
- }
- $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
- return $content;
- }
- //云打印 ssh 20210709
- public static function onlinePrint($orderInfo)
- {
- $showItem = OrderClass::getShowItem(true, $orderInfo);
- $mainId = $orderInfo['mainId'] ?? 0;
- $staff = ShopAdminClass::getByCondition(['mainId' => $mainId, 'founder' => 2], true);
- $mobile = $staff->mobile ?? '';
- $customId = $orderInfo['customId'] ?? 0;
- $ghsId = $orderInfo['ghsId'] ?? 0;
- $c = CustomClass::getById($customId);
- $debtAmount = $c['debtAmount'] ?? 0.00;
- $fromType = $orderInfo['fromType'] ?? dict::getDict('fromType', 'shop');
- $fromTypeMap = dict::getDict('fromTypeMap');
- $fromTypeName = $fromTypeMap[$fromType] ?? '门店';
- $respond = self::getPrintData($orderInfo, $debtAmount, $showItem);
- $content = '<B>' . $respond['sendNum'] . ' ' . $fromTypeName . '</B><BR>';
- $content .= '--------------------------------<BR>';
- $content .= '<B>' . $respond['custom']['fullAddress'] . '</B><BR>';
- $content .= '<B>' . $respond['custom']['customName'] . '</B><BR>';
- $content .= '<B>' . $respond['custom']['customMobile'] . '</B><BR>';
- $payDate = date("Y-m-d", strtotime($orderInfo['payTime']));
- $sendTimWant = $orderInfo['sendTimeWant'] ?? '';
- if ($payDate != $sendTimWant) {
- $content .= '<BR>';
- $content .= '送货时间:<BR>';
- $content .= '<B>' . $orderInfo['sendTimeWant'] . '</B><BR>';
- }
- if (isset($orderInfo['book']) && $orderInfo['book'] == 1) {
- $content .= '<B>预订单</B><BR>';
- }
- if (isset($respond['remark']) && !empty($respond['remark'])) {
- $content .= '<BR>';
- $content .= '备注:<BR>';
- $content .= '<B>' . $respond['remark'] . '</B><BR>';
- }
- $content .= '<BR>';
- $content .= '花材 数量/单价 金额 <BR>';
- $content .= '--------------------------------<BR>';
- if (isset($respond['product']) && !empty($respond['product'])) {
- foreach ($respond['product'] as $current) {
- $content = self::printGroup($current, $content);
- }
- $content .= '--------------------------------<BR>';
- }
- if (isset($respond['refund']) && !empty($respond['refund'])) {
- $content .= '<B>已退款:</B><BR>';
- foreach ($respond['refund'] as $current) {
- //58mm的机器,一行打印16个汉字,32个字母
- $name = $current['name'] ?? '';
- $content .= $name . '<BR>';
- $name = str_repeat(' ', 10);
- $productNum = $current['num'] ?? '';
- $blankNum = bcsub(12, strlen($productNum));
- if ($blankNum > 0) {
- $productNum = $productNum . str_repeat(' ', $blankNum);
- }
- $productPrice = $current['price'] ?? '';
- $blankNum = bcsub(7, strlen($productPrice));
- if ($blankNum > 0) {
- $productPrice = $productPrice . str_repeat(' ', $blankNum);
- }
- $content .= $name . " " . $productNum . ' ' . $productPrice . '<BR>';
- }
- $content .= '--------------------------------<BR>';
- }
- if (isset($respond['sendCost']) && $respond['sendCost'] > 0) {
- $content .= ' 运费:' . floatval($respond['sendCost']) . '<BR>';
- }
- $content .= '订单金额:' . floatval($respond['prePrice']) . '<BR>';
- if (isset($respond['discountAmount']) && !empty($respond['discountAmount']) && $respond['discountAmount'] > 0) {
- $content .= '优惠扣除:-' . floatval($respond['discountAmount']) . '<BR>';
- }
- if (isset($respond['refundPrice']) && $respond['refundPrice'] > 0) {
- $content .= '退款金额:' . floatval($respond['refundPrice']) . '<BR>';
- }
- $content .= '应付金额:' . floatval($respond['realPrice']) . '<BR>';
- if (isset($respond['debt']) && $respond['debt'] == 1) {
- $content .= '实付金额:0<BR>';
- } else {
- $content .= '实付金额:' . floatval($respond['realPrice']) . '<BR>';
- }
- if ($debtAmount > 0) {
- $content .= '累计欠款:' . floatval($debtAmount) . '<BR>';
- }
- $shopId = $orderInfo['shopId'] ?? 0;
- $shop = ShopClass::getById($shopId, true);
- $sjId = $shop->sjId ?? 0;
- $sj = SjClass::getById($sjId, true);
- $sjName = $sj->name ?? '';
- $currentName = $sjName;
- if (isset($shop->default) == false || $shop->default != 1) {
- $shopName = $shop->shopName ?? '';
- if (!empty($shopName)) {
- $currentName = $currentName . ' ' . $shopName;
- }
- }
- $getInfo = self::getById($orderInfo['id']);
- $content .= '--------------------------------<BR>';
- $content .= '订单日期:' . $getInfo['addTime'] . '<BR>';
- $content .= '订单编号:' . $respond['orderSn'] . '<BR>';
- $content .= '门店名称:' . $currentName . '<BR>';
- if (!empty($mobile)) {
- $content .= '联系电话:' . $mobile . '<BR>';
- }
- if ($debtAmount > 0) {
- $content .= "<BR>";
- $content .= "<C>扫码查看所有欠款单</C>";
- $content .= "<BR>";
- $host = Yii::$app->params['hdHost'];
- $content .= "<QR>" . $host . "/#/pagesPurchase/gathering?id=" . $ghsId . "</QR>";
- }
- $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
- $printSn = $ext['printSn'] ?? '';
- if (!empty($printSn)) {
- $p = new printUtil($printSn);
- $p->printMsg($content);
- }
- }
- //打印数据 ssh 20210702
- public static function getPrintData($val, $debtAmount, $showItem)
- {
- $printProduct = [];
- if (isset($showItem['list']) && !empty($showItem['list'])) {
- foreach ($showItem['list'] as $itemKey => $itemVal) {
- $bigNum = $itemVal['bigNum'] ?? 0;
- $bigUnitPrice = $itemVal['unitPrice'] ?? 0;
- $smallUnitPrice = $itemVal['smallUnitPrice'] ?? 0;
- $smallNum = $itemVal['smallNum'] ?? 0;
- if ($bigNum > 0) {
- $bigTotalPrice = bcmul($bigUnitPrice, $bigNum, 2);
- $printProduct[] = [
- 'name' => $itemVal['name'] ?? '',
- 'num' => floatval($bigNum) . '扎*' . floatval($bigUnitPrice),
- 'price' => floatval($bigTotalPrice),
- ];
- }
- if ($smallNum > 0) {
- $smallTotalPrice = bcmul($smallUnitPrice, $smallNum, 2);
- $printProduct[] = [
- 'name' => $itemVal['name'] ?? '',
- 'num' => floatval($smallNum) . '支*' . floatval($smallUnitPrice),
- 'price' => floatval($smallTotalPrice),
- ];
- }
- }
- }
- $sendNum = $val['sendNum'] ?? '';
- $relateOrderSn = $val['orderSn'] ?? '';
- $refundList = [];
- $refundData = RefundOrderClass::getAllByCondition(['relateOrderSn' => $relateOrderSn], null, '*', null, true);
- if (!empty($refundData)) {
- foreach ($refundData as $refund) {
- $refundOrderSn = $refund->orderSn ?? '';
- $refundProductList = RefundOrderItemClass::getAllByCondition(['orderSn' => $refundOrderSn], null, '*');
- if (!empty($refundProductList)) {
- foreach ($refundProductList as $currentRefund) {
- $num = $currentRefund['xhNum'] ?? 0;
- $unitName = $currentRefund['xhUnitName'] ?? '';
- $unitPrice = $currentRefund['xhUnitPrice'] ?? 0;
- $currentPrice = bcmul($unitPrice, $num, 2);
- $refundList[] = [
- 'name' => $currentRefund['name'] ?? '',
- 'num' => floatval($num) . $unitName . '*' . floatval($unitPrice),
- 'price' => floatval($currentPrice),
- ];
- }
- }
- }
- }
- $printData = [
- 'address' => '',
- 'sendCost' => $val['sendCost'] ?? '0.00',
- 'prePrice' => $val['prePrice'] ?? '0.00',
- 'discountAmount' => $val['discountAmount'] ?? '0.00',
- 'realPrice' => $val['realPrice'] ?? '0.00',
- 'refundPrice' => $val['refundPrice'] ?? '0.00',
- 'debt' => $val['debt'] ?? 0,
- 'orderSn' => $val['orderSn'] ?? '',
- 'date' => $val['addTime'] ?? '',
- 'remark' => $val['remark'] ?? '',
- 'custom' => [
- 'customName' => $val['customName'] ?? '',
- 'customMobile' => $val['customMobile'] ?? '',
- 'fullAddress' => $val['fullAddress'] ?? '',
- ],
- 'product' => $printProduct,
- 'refund' => $refundList,
- 'sendNum' => $sendNum,
- 'debtAmount' => $debtAmount,
- ];
- return $printData;
- }
- //订单的验证 ssh 2021.3.2
- public static function valid($info, $shopId)
- {
- if (empty($info)) {
- util::fail('没有找到订单');
- }
- if (isset($info['shopId']) && $info['shopId'] == $shopId) {
- return true;
- }
- util::fail('没有权限操作的订单');
- }
- //快递送 ssh 2021.3.16
- public static function thirdSend($order, $expressInfo)
- {
- if ($order->status == self::ORDER_STATUS_UN_PAY) {
- util::fail('订单未付款');
- }
- if ($order->status != self::ORDER_STATUS_UN_SEND) {
- util::fail('不是待配送状态');
- }
- $id = $order->id;
- $expressId = $expressInfo['expressId'] ?? 1;
- $expressName = $expressInfo['deliveryName'] ?? '达达';
- $expressRemark = $expressInfo['remark'] ?? '';
- $order->status = self::ORDER_STATUS_SENDING;
- $order->sendType = self::SEND_TYPE_THIRD;
- $order->expressId = $expressId;
- $order->save();
- $shopId = $order->shopId;
- $shop = Shop::findBySql('select * from ' . Shop::tableName() . ' where id = :id for update', ['id' => $shopId])->one();
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $shop->unSendOrder -= 1;
- $shop->sendingOrder += 1;
- $shop->save();
- $express = OrderExpressClass::getByCondition(['orderId' => $id]);
- if (!empty($express)) {
- util::fail('已经发过了');
- }
- //达达
- $dadaRes = DadaExpressServices::addOrder($order, $expressRemark);
- $orderSn = $order->orderSn;
- $sjId = $order->sjId ?? 0;
- /*
- $getInfo = ExpressClass::getExpressInfo($expressId);
- $hour = $expressInfo['sendTime'] ?? '12:00';
- $sendCost = 12;
- $distance = 5;
- $sendTime = strtotime(date("Y-m-d ") . $hour);
- OrderSendClass::thirdSend($order);
- */
- $sendTime = $order->sendTime;
- if ($sendTime == '0000-00-00 00:00:00') {
- $sendTime = time();
- } else {
- $sendTime = strtotime($order->sendTime);
- }
- $expData = [
- 'orderId' => $id,
- 'orderSn' => $orderSn,
- 'sjId' => $sjId,
- 'expressId' => $expressId,
- 'sendTime' => $sendTime,
- 'cost' => $dadaRes['fee'],
- 'distance' => $dadaRes['distance'],
- 'status' => 1,
- 'expressName' => $expressName,
- 'remark' => $expressRemark,
- ];
- OrderExpressClass::addExpress($expData);
- OrderSendClass::thirdSend($order);
- $respond = [
- 'expressName' => $expressName,
- 'expressId' => $expressId,
- 'distance' => $dadaRes['distance'],
- 'cost' => $dadaRes['fee'],
- 'status' => 2,
- 'tip' => 0,
- ];
- //采购单变成已发货
- $purchaseId = $order->purchaseId ?? 0;
- PurchaseClass::sending($purchaseId);
- return $respond;
- }
- //本店送 ssh 2021.1.24
- public static function selfSend($info)
- {
- if ($info->status == self::ORDER_STATUS_UN_PAY) {
- util::fail('订单未付款');
- }
- if ($info->status != self::ORDER_STATUS_UN_SEND) {
- util::fail('不是待配送状态');
- }
- $info->status = self::ORDER_STATUS_SENDING;
- $info->sendType = self::SEND_TYPE_MYSELF;
- $info->save();
- $shopId = $info->shopId;
- $shop = Shop::findBySql('select * from ' . Shop::tableName() . ' where id = :id for update', ['id' => $shopId])->one();
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $shop->unSendOrder -= 1;
- $shop->sendingOrder += 1;
- $shop->save();
- OrderSendClass::selfSend($info);
- //采购单变成已发货
- $purchaseId = $info->purchaseId ?? 0;
- PurchaseClass::sending($purchaseId);
- }
- //订单完成 ssh 2020.3.12
- public static function complete($order, $shop)
- {
- $order->status = OrderClass::ORDER_STATUS_COMPLETE;
- $order->currentFlow = self::TOTAL_FLOW;
- $order->totalFlow = self::TOTAL_FLOW;
- $order->save();
- $shop->finishOrder += 1;
- $shop->sendingOrder -= 1;
- $shop->save();
- return $order;
- }
- //修改订单表的配送信息 lqh 2021.3.28
- public static function updateCustomExpressInfo($id, $expressAddInfo)
- {
- $data = [
- 'province' => $expressAddInfo['province'],
- 'city' => $expressAddInfo['city'],
- 'address' => $expressAddInfo['address'],
- 'floor' => $expressAddInfo['floor'],
- 'fullAddress' => $expressAddInfo['city'] . $expressAddInfo['address'] . $expressAddInfo['floor'],
- 'lat' => $expressAddInfo['lat'],
- 'long' => $expressAddInfo['long'],
- 'sendTime' => $expressAddInfo['sendTime'],
- ];
- return self::updateById($id, $data);
- }
- //最客户最近的订单 ssh 2021.5.4
- public static function getCustomOrder($id, $limit = 10, $order = null, $with = null)
- {
- $where = ['customId' => $id, 'payStatus' => 1];
- return self::getLimitList('*', $where, $limit, $order, $with);
- }
- //销售单取消 ssh 2021.5.14
- public static function cancel($id)
- {
- $order = self::getLockById($id);
- if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
- util::fail("销售单{$id}不是待付款状态,无法取消");
- }
- $order->status = self::ORDER_STATUS_CANCEL;
- $order->save();
- $shopId = $order->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->unPayOrder -= 1;
- $main->cancelOrder += 1;
- $main->save();
- //加回库存
- $orderItem = OrderItemClass::getOrderItem($order->orderSn);
- foreach ($orderItem as $v) {
- $stockInfo = ProductClass::addStockByItemNum($v['productId'], $v['num']);
- $recordData = [];
- $recordData['sjId'] = $order->sjId;
- $recordData['shopId'] = $shopId;
- //增加中央ID
- $recordData['mainId'] = ProductClass::getMainIdByProductId($v['productId']);
- $recordData['itemId'] = $v['itemId'] ?? 0;
- $recordData['itemNum'] = $v['num'];
- $recordData['oldStock'] = $stockInfo['oldStock'];
- $recordData['newStock'] = $stockInfo['newStock'];
- $recordData['productId'] = $v['productId'];
- $recordData['orderSn'] = $order->orderSn;
- $recordData['relateName'] = $order->customName ?? '';
- $recordData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
- StockRecordClass::addSellStockOrderCancelRecord($recordData);
- }
- return $order;
- }
- //新订单入队列 ssh 20211012
- public static function newOrderAddQueue($order)
- {
- $shopId = $order->shopId ?? 0;
- if (empty($shopId)) {
- return false;
- }
- $id = $order->id ?? 0;
- if (empty($id)) {
- return false;
- }
- $newOrderKey = 'ghsNewOrder_' . $shopId;
- Yii::$app->redis->executeCommand('LPUSH', [$newOrderKey, $id]);
- }
- }
|