| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017 |
- <?php
- namespace bizGhs\order\services;
- use biz\ghs\classes\GhsClass;
- use biz\shop\classes\ShopAdminClass;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopClass;
- use biz\shop\classes\ShopExtClass;
- use biz\stat\classes\StatKdClass;
- use biz\stat\classes\StatKhCgClass;
- use biz\stat\classes\StatSaleClass;
- use biz\wx\classes\WxMessageClass;
- use bizGhs\base\services\BaseService;
- use bizGhs\book\classes\BookItemClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\custom\classes\CustomDebtChangeClass;
- use bizGhs\custom\services\GhsRechargeSettleService;
- use bizGhs\clear\classes\OrderCgClearClass;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\classes\OrderExpressClass;
- use bizGhs\order\classes\OrderItemClass;
- use bizGhs\order\classes\OrderSendClass;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\shop\classes\MainClass;
- use bizGhs\shop\classes\ShopMoneyChangeClass;
- use bizGhs\shop\classes\ShopMoneyClass;
- use bizGhs\stat\classes\StatYjClass;
- use bizGhs\ws\services\WsService;
- use bizHd\cg\services\CgRefundService;
- use bizHd\purchase\services\PurchaseService;
- use bizHd\stat\classes\StatIncomeClass;
- use bizHd\stat\classes\StatOrderClass;
- use common\components\dict;
- use common\components\noticeUtil;
- use common\components\util;
- use bizHd\purchase\classes\PurchaseClass;
- use common\components\stringUtil;
- use Yii;
- class OrderService extends BaseService
- {
- public static $baseFile = '\bizGhs\order\classes\OrderClass';
- public static function arrival($order, $data)
- {
- $params = ['fhType' => 0, 'fhWl' => '', 'fhWlNo' => '',];
- OrderClass::orderSend($order, $params);
- //product [{"productId":"3674","num":0,"price":1}]
- $product = $data['product'] ?? '';
- if (empty($product)) {
- util::fail('请填写成本价和到货数量');
- }
- $product = json_decode($product, true);
- if (empty($product)) {
- util::fail('没有填写成本价和到货数量');
- }
- $data['product'] = $product;
- //供货商到货
- $respond = OrderClass::arrival($order, $data);
- $cgId = $order->purchaseId ?? 0;
- $cg = PurchaseClass::getById($cgId, true);
- if (empty($cg)) {
- util::fail('没有找到采购单');
- }
- //供货商花材转换为零售店花材
- $ids = array_column($product, 'productId');
- $ghsProductInfo = ProductClass::getByIds($ids);
- if (empty($ghsProductInfo)) {
- util::fail('没有花材??');
- }
- $ghsPtIdRelation = [];
- foreach ($ghsProductInfo as $ghsProduct) {
- $id = $ghsProduct['id'] ?? 0;
- $ptItemId = $ghsProduct['itemId'] ?? 0;
- $ghsPtIdRelation[$ptItemId] = $id;
- }
- $ptIds = array_keys($ghsPtIdRelation);
- $cgMainId = $cg->mainId ?? 0;
- $cgSjId = $cg->sjdId ?? 0;
- $cgShopId = $cg->shopId ?? 0;
- $cgItem = ProductClass::getAllByCondition(['mainId' => $cgMainId, 'itemId' => ['in', $ptIds]], null, '*');
- $ghsHdIdRelate = [];
- foreach ($cgItem as $it) {
- $ptItemId = $it['itemId'] ?? 0;
- $ghsItemId = $ghsPtIdRelation[$ptItemId] ?? 0;
- $cgItemId = $it['id'] ?? 0;
- $ghsHdIdRelate[$ghsItemId] = $cgItemId;
- }
- $cgProduct = [];
- foreach ($product as $pro) {
- $price = $pro['price'] ?? 0;
- $num = $pro['num'] ?? 0;
- $productId = $pro['productId'] ?? 0;
- $currentId = $ghsHdIdRelate[$productId] ?? 0;
- $cgProduct[$currentId] = ['num' => $num, 'price' => $price, 'productId' => $currentId];
- }
- //零售到货
- $saleRefund = $respond['refund'] ?? null;
- $saleRefundId = 0;
- if (!empty($saleRefund)) {
- $saleRefundId = $saleRefund->id ?? 0;
- }
- $kiloFee = $respond['kiloFee'] ?? 0;
- $miniKilo = $respond['miniKilo'] ?? 0;
- $saleRefundId = $saleRefundId;
- $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
- $packCost = isset($data['packCost']) && is_numeric($data['packCost']) ? $data['packCost'] : 0;
- $cgData = [
- 'product' => $cgProduct,
- 'kiloFee' => $kiloFee,
- 'miniKilo' => $miniKilo,
- 'saleRefundId' => $saleRefundId,
- 'sjId' => $cgSjId,
- 'shopId' => $cgShopId,
- 'mainId' => $cgMainId,
- 'sendCost' => $sendCost,
- 'packCost' => $packCost,
- ];
- $result = PurchaseClass::arrival($cg, $cgData);
- $cgRefund = $result['refund'] ?? null;
- if (!empty($cgRefund)) {
- $cgRefundId = $cgRefund->id ?? 0;
- if (!empty($saleRefund)) {
- $saleRefund->cgRefundId = $cgRefundId;
- $saleRefund->save();
- }
- }
- //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
- if (!empty($cg) && !empty($order)) {
- $ghsShopId = $order->shopId ?? 0;
- $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
- if ($ext->orderFlow == 0) {
- $newCg = PurchaseClass::getById($cgId, true);
- PurchaseClass::confirmTake($newCg);
- }
- }
- }
- //添加订单
- public static function createNewOrder($data, $custom, $hasPay = 1)
- {
- //有unitType数据,说明是收银台传过来的数据,转换成和手机端结构一致
- $hasUnitType = array_column($data['product'], 'unitType');
- if (!empty($hasUnitType)) {
- $newProductData = [];
- foreach ($data['product'] as $productItem) {
- $unitType = $productItem['unitType'] ?? 0;
- $num = $productItem['num'] ?? 0;
- $productId = $productItem['productId'] ?? 0;
- $classId = $productItem['classId'] ?? 0;
- $itemId = $productItem['itemId'] ?? 0;
- $price = $productItem['price'] ?? 0;
- if ($unitType == 0) {
- $bigNum = $num;
- $smallNum = '';
- } else {
- $bigNum = '';
- $smallNum = $num;
- }
- $newProductData[] = [
- 'productId' => (int)$productId,
- 'bigNum' => (int)$bigNum,
- 'smallNum' => (int)$smallNum,
- 'classId' => (int)$classId,
- 'itemId' => (int)$itemId,
- 'price' => floatval($price),
- ];
- }
- $data['product'] = $newProductData;
- }
- //供货商销售单
- $returnOrder = OrderClass::addOrder($data, $custom);
- $saleId = $returnOrder->id ?? 0;
- $customSjId = $custom['sjId'] ?? 0;
- $customShopId = $custom['shopId'] ?? 0;
- $localOrder = $data['localOrder'] ?? 0;
- $transType = $data['transType'] ?? 0;
- $ghsShopAdminId = $data['shopAdminId'] ?? 0;
- $ghsShopAdminName = $data['shopAdminName'] ?? '';
- $customId = $data['customId'] ?? 0;
- $ghsId = $data['ghsId'] ?? 0;
- $ghs = GhsClass::getById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- $ghsName = $ghs['name'] ?? '';
- $ghsAvatar = $ghs['avatar'] ?? '';
- $ghsMobile = $ghs['mobile'] ?? '';
- $ghsFullAddress = $ghs['fullAddress'] ?? '';
- $payWay = $data['payWay'] ?? dict::getDict('payWay', 'wxPay');
- //销售单欠款字段与采购单欠款字段转换
- $debt = $data['debt'] ?? 0;
- $purchaseDebt = $debt == 1 ? PurchaseClass::DEBT_YES : PurchaseClass::DEBT_NO;
- if ($hasPay == dict::getDict('hasPay', 'balance')) {
- $payWay = dict::getDict('payWay', 'balancePay');
- $purchaseDebt = PurchaseClass::DEBT_NO;
- } elseif ($debt == 1) {
- $payWay = dict::getDict('payWay', 'debtPay');
- }
- //销售单的自取还是配送字段 与 采购单的转换
- $sendType = $data['sendType'] ?? OrderClass::SEND_TYPE_CAR_GET;
- $getType = $sendType == OrderClass::SEND_TYPE_THIRD_SEND ? PurchaseClass::GET_TYPE_SEND : PurchaseClass::GET_TYPE_SELF_GET;
- //零售采购单时没有传price字段,供货商可以改价会传price字段,这里转成userPrice以便采购单识别使用改的价还是花材原价!
- $currentProduct = $data['product'];
- foreach ($currentProduct as $currentKey => $currentItem) {
- if (isset($currentItem['price'])) {
- $currentProduct[$currentKey]['userPrice'] = $currentItem['price'];
- unset($currentProduct[$currentKey]['price']);
- }
- }
- $sendTimeWant = !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
- $remark = $data['remark'] ?? '';
- $xj = $data['xj'] ?? '';
- $treeData = $data['treeData'] ?? [];
- $book = $data['book'] ?? 0;
- //后端开单,涉及花材满减,关键词 back_order_reach_discount_price
- $reachDiscountPrice = $data['reachDiscountPrice'] ?? 0;
- $wlName = $data['wlName'] ?? '';
- $cgMainId = $ghs['ownMainId'] ?? 0;
- $purchaseData = [
- 'book' => $book,
- 'product' => $currentProduct,
- 'sendTimeWant' => $sendTimeWant,
- 'sendCost' => $data['sendCost'] ?? '0',
- 'packCost' => $data['packCost'] ?? '0',
- 'ghsShopAdminId' => $ghsShopAdminId,
- 'ghsShopAdminName' => $ghsShopAdminName,
- 'sjId' => $customSjId,
- 'shopId' => $customShopId,
- 'saleId' => $saleId,
- 'ghsId' => $ghsId,
- 'ghsName' => $ghsName,
- 'ghsMobile' => $ghsMobile,
- 'ghsFullAddress' => $ghsFullAddress,
- 'ghsAvatar' => $ghsAvatar,
- 'customId' => $customId,
- 'payWay' => $payWay,
- 'status' => PurchaseClass::STATUS_UN_PAY,
- 'cgStyle' => dict::getDict('cgStyle', 'ghs'),
- 'debt' => $purchaseDebt,
- 'getType' => $getType,
- 'sendType' => $sendType,
- 'remark' => $remark,
- 'xj' => $xj,
- 'treeData' => $treeData,
- 'discount' => 1,
- 'mainId' => $cgMainId,
- 'wlName' => $wlName,
- 'localOrder' => $localOrder,
- 'transType' => $transType,
- 'reachDiscountPrice' => $reachDiscountPrice,
- ];
- if (!empty($data['deadline'])) {
- $purchaseData['deadline'] = $data['deadline'];
- }
- if (isset($data['modifyPrice']) && is_numeric($data['modifyPrice'])) {
- $purchaseData['modifyPrice'] = $data['modifyPrice'];
- }
- $purchase = PurchaseClass::addPurchase($purchaseData, $ghs);
- //开历史订单
- $historyDate = $data['historyDate'] ?? '';
- if (!empty($historyDate)) {
- $currentPayTime = $historyDate . ' 12:00:00';
- $returnOrder->payTime = $currentPayTime;
- $purchase->payTime = $currentPayTime;
- $purchase->save();
- CustomDebtChangeClass::updateByCondition(['capitalType' => 10, 'relateId' => $returnOrder->id], ['payTime' => $currentPayTime]);
- }
- $purchaseId = $purchase->id ?? 0;
- $returnOrder->purchaseId = $purchaseId;
- $returnOrder->save();
- //欠款、已付款、余额支付的走付款后流程
- if (in_array($hasPay, [dict::getDict('hasPay', 'payed'), dict::getDict('hasPay', 'debt'), dict::getDict('hasPay', 'balance')])) {
- // 挂账/余额:先处理销售单(含余额抵挂账),再同步采购单待结
- if (in_array($hasPay, [dict::getDict('hasPay', 'debt'), dict::getDict('hasPay', 'balance')])) {
- self::payAfter($returnOrder, $payWay);
- $purchase = PurchaseClass::getLockById($purchaseId);
- if (!empty($purchase)) {
- $purchase->remainDebtPrice = $returnOrder->remainDebtPrice ?? '0.00';
- $purchase->debtPrice = $returnOrder->debtPrice ?? '0.00';
- $purchase->debt = ($returnOrder->debt ?? 0) == OrderClass::DEBT_YES
- ? PurchaseClass::DEBT_YES : PurchaseClass::DEBT_NO;
- $purchase->save(false, ['remainDebtPrice', 'debtPrice', 'debt']);
- }
- PurchaseService::payAfter($purchase, $payWay);
- } else {
- PurchaseService::payAfter($purchase, $payWay);
- self::payAfter($returnOrder, $payWay);
- }
- //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
- if (isset($returnOrder->book) && $returnOrder->book == 0) {
- $ghsShopId = $ghs['shopId'] ?? 0;
- $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
- if ($ext->orderFlow == 0) {
- $returnId = $returnOrder->id;
- $order = OrderClass::getById($returnId, true);
- if (!empty($order)) {
- OrderClass::confirmReach($order);
- }
- }
- }
- }
- return $returnOrder;
- }
- //修改订单
- public static function updateOrder($order, $data)
- {
- $purchaseId = $order->purchaseId ?? 0;
- $purchase = PurchaseClass::getLockById($purchaseId);
- //修改供货商销售单
- $returnOrder = OrderClass::updateOrder($order, $data);
- //供货商销售单的 0未欠款 1欠款 转成零售采购单的 1欠款 2未欠款
- $debt = $data['debt'] ?? 0;
- $purchaseDebt = $debt == 1 ? PurchaseClass::DEBT_YES : PurchaseClass::DEBT_NO;
- $payWay = $data['payWay'] ?? dict::getDict('payWay', 'wxPay');
- if ($debt == 1) {
- $payWay = dict::getDict('payWay', 'debtPay');
- }
- //自取还是配送订单,销售单与采购单之间转换
- $sendType = $data['sendType'] ?? OrderClass::SEND_TYPE_MYSELF;
- $getType = $sendType == OrderClass::SEND_TYPE_NO ? PurchaseClass::GET_TYPE_SELF_GET : PurchaseClass::GET_TYPE_SEND;
- //供货商价格是可以修改的,修改的价格一起传给采购单保存
- $currentProduct = $data['product'];
- foreach ($currentProduct as $currentKey => $currentItem) {
- if (isset($currentProduct[$currentKey]['price'])) {
- $currentProduct[$currentKey]['userPrice'] = $currentProduct[$currentKey]['price'];
- unset($currentProduct[$currentKey]['price']);
- }
- }
- $remark = $data['remark'] ?? '';
- $purchaseData = [
- 'product' => $currentProduct,
- 'sendTime' => date("H:i:s"),
- 'sendCost' => $data['sendCost'] ?: '0.00',
- 'payWay' => $payWay,
- 'status' => PurchaseClass::STATUS_UN_PAY,
- 'cgStyle' => dict::getDict('cgStyle', 'ghs'),
- 'debt' => $purchaseDebt,
- 'getType' => $getType,
- 'status' => PurchaseClass::STATUS_UN_PAY,
- 'remark' => $remark,
- ];
- if (isset($data['deadline']) && !empty($data['deadline'])) {
- $purchaseData['deadline'] = $data['deadline'];
- }
- if (isset($data['modifyPrice']) && is_numeric($data['modifyPrice'])) {
- //优惠之后的金额
- if (is_numeric($data['modifyPrice']) == false || $data['modifyPrice'] <= 0) {
- util::fail('金额填错了哦');
- }
- $purchaseData['modifyPrice'] = $data['modifyPrice'];
- }
- //修改采购单
- $purchase = PurchaseClass::updatePurchase($purchase, $purchaseData);
- //除了自取订单直接变成已确认,其它都进入待确认流程。订单的确认以欠款或已收款方式确认。 ssh 20210809
- if (isset($data['sendType']) && $data['sendType'] == OrderClass::SEND_TYPE_NO) {
- PurchaseService::payAfter($purchase, $payWay);
- self::payAfter($returnOrder, $payWay);
- } else {
- //没有进入确认流程的订单也有打印需求
- if ($data['needPrint'] == dict::getDict('needPrint', 'need')) {
- OrderClass::onlinePrint($returnOrder);
- $returnOrder->printNum += 1;
- $returnOrder->save();
- }
- }
- return $returnOrder;
- }
- //自取免配送流程处理 ssh 2021.1.24
- public static function withoutSend($order)
- {
- if (isset($order->status) == false) {
- util::fail('没有订单状态');
- }
- if ($order->status == OrderClass::ORDER_STATUS_COMPLETE) {
- util::fail('订单已完成');
- }
- //自取订单,订单直接完成。【现在有三个地方有订单完成操作】
- $shopId = $order->shopId ?? 0;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店3');
- }
- $shop->unSendOrder -= 1;
- $shop->sendingOrder += 1;
- $shop->save();
- //自取免配送
- $order->sendType = OrderClass::SEND_TYPE_NO;
- $order->save();
- //这个是采购模板的withoutSend,应另外封装方法
- $purchaseId = $order->purchaseId ?? 0;
- $purchase = PurchaseClass::getById($purchaseId, true);
- $pShopId = $purchase->shopId;
- $pShop = ShopClass::getLockById($pShopId);
- $pShop->cgUnSend -= 1;
- $pShop->cgSending += 1;
- $pShop->save();
- //订单完成
- self::finish($order, $shop);
- OrderSendClass::withoutSend($order);
- return OrderClass::TOTAL_FLOW;
- }
- //确认送达 ssh 2021.3.4
- public static function reach($order)
- {
- if ($order->status != OrderClass::ORDER_STATUS_SENDING) {
- util::fail('订单没有在配送状态');
- }
- //第三方配送的变更第三方订单状态
- if (isset($order->sendType) && $order->sendType == OrderClass::SEND_TYPE_THIRD) {
- $express = OrderExpressClass::getByCondition(['orderSn' => $order->orderSn], true);
- if (!empty($express)) {
- $express->status = OrderExpressClass::STATUS_COMPLETE;
- $express->save();
- }
- }
- $shopId = $order->shopId ?? 0;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店4');
- }
- //订单完成
- self::finish($order, $shop);
- OrderSendClass::reach($order);
- //是否微信通知
- $noNeedWxNotice = isset(Yii::$app->params['noNeedWxNotice']) ? Yii::$app->params['noNeedWxNotice'] : 0;
- if ($noNeedWxNotice == 0) {
- //微信通知
- WxMessageClass::reachInform($shop, $order);
- }
- }
- //订单完成
- public static function finish($order, $shop)
- {
- //销售单完成
- $order = OrderClass::complete($order, $shop);
- //采购单完成
- $purchaseId = $order->purchaseId ?? 0;
- $purchase = PurchaseClass::getById($purchaseId, true);
- PurchaseClass::complete($purchase);
- }
- //获取订单信息 ssh 2019.11.28
- public static function getOrderList($where)
- {
- $data = self::getList('*', $where, 'addTime DESC');
- if (isset($data['list']) == false || empty($data['list'])) {
- return $data;
- }
- $orderSns = array_column($data['list'], 'orderSn');
- //获取订单的商品信息
- $goodsList = OrderGoodsService::getGoodsListByOrderSns($orderSns);
- //获取订花人的用户信息
- $orderUserIds = array_column($data['list'], 'userId');
- $orderUserIds = array_unique($orderUserIds);
- $userInfoList = UserService::getUserByIds($orderUserIds);
- $sjIdList = array_column($data['list'], 'sjId');
- $sjIdList = array_filter(array_unique($sjIdList));
- $merchantList = MerchantService::getByIds($sjIdList, null, 'id');
- $periodData = [0 => '上午', 1 => '下午', 2 => '晚上'];
- foreach ($data['list'] as $key => $val) {
- $id = $val['id'];
- $userId = $val['userId'];
- $data['list'][$key]['goodsInfoList'] = isset($goodsList[$id]) ? $goodsList[$id] : [];
- $data['list'][$key]['bookAvatar'] = isset($userInfoList[$userId]['smallAvatarUrl']) ? $userInfoList[$userId]['smallAvatarUrl'] : '';
- $sjId = $val['sjId'];
- $data['list'][$key]['name'] = isset($merchantList[$sjId]['name']) ? $merchantList[$sjId]['name'] : '';
- $data['list'][$key]['reachTime'] = OrderClass::getReachTime($val);
- $requireReachTime = '';
- if ($val['reachDate'] != '0000-00-00') {
- $reachPeriodId = $val['reachPeriod'];
- $reachPeriod = isset($periodData[$reachPeriodId]) ? $periodData[$reachPeriodId] : '上午';
- $requireReachTime = date("m-d", strtotime($val['reachDate'])) . ' ' . $reachPeriod;
- }
- $data['list'][$key]['requireReachTime'] = $requireReachTime;
- }
- return $data;
- }
- //取客户最近若干条订单 ssh 2019.11.30
- public static function getUserOrder($userId, $limit = 10, $order = null, $with = null)
- {
- $where = ['userId' => $userId, 'payStatus' => 1];
- return self::getLimitList('*', $where, $limit, $order, $with);
- }
- //支付前验证订单 ssh 2019.12.6
- public static function checkBeforePay($order, $userId)
- {
- $now = time();
- if (empty($order)) {
- util::fail('订单无效');
- }
- if (isset($order['userId']) && $order['userId'] != $userId) {
- util::fail('非法访问');
- }
- if ($order['status'] != 0) {
- util::fail('订单不是待付款状态');
- }
- if (isset($order['deadline']) && $now > $order['deadline']) {
- util::fail('订单已过期');
- }
- if (ceil($order['actPrice']) <= 0) {
- util::fail('订单金额有问题');
- }
- if ($order['payStatus'] == 1) {
- util::fail('您已经付过了');
- }
- }
- //商家验证是否有效 ssh 2019.12.15
- public static function valid($order, $shopId)
- {
- $now = date("Y-m-d H:i:s");
- if (empty($order)) {
- util::fail('订单无效');
- }
- if ($order['shopId'] != $shopId) {
- util::fail('不是您的订单');
- }
- if (isset($order['status']) && $order['status'] == OrderClass::ORDER_STATUS_UN_PAY) {
- if (isset($order['deadline']) && $now > $order['deadline']) {
- util::fail('订单已过期');
- }
- }
- }
- //点评 ssh 2019.12.16
- public static function comment($data)
- {
- return OrderClass::comment($data);
- }
- //订单归类
- public static function classify($id, $categoryId, $usageId)
- {
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $order = OrderClass::getById($id, true);
- if ($order->payStatus != 1) {
- util::fail('订单还未付款');
- }
- if ($order->classify == 1) {
- util::fail('已经归类过了');
- }
- $order->categoryId = $categoryId;
- $order->usageId = $usageId;
- $order->classify = 1;
- $order->save();
- //设置流水的分类和用途并分配资金
- $setData = [
- 'sjId' => $order->sjId,
- 'capitalId' => $order->capitalId,
- 'time' => $order->payTime,
- 'usageId' => $order->usageId,
- 'categoryId' => $order->categoryId,
- 'amount' => $order->actPrice,
- 'payWay' => $order->payWay,
- ];
- SjCapitalService::orderClassify($setData);
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- util::fail('没有归类成功');
- }
- }
- //更新配送单 ssh 2019.12.17
- public static function updateSheet($order, $data)
- {
- $id = $order['id'];
- if ($order['status'] != OrderClass::ORDER_STATUS_UN_SEND) {
- util::fail('订单当前不是待配送状态');
- }
- $data['deliver'] = 1;
- OrderClass::updateById($id, $data);
- }
- //订单详情 ssh 2020.1.2
- public static function getOrderById($id)
- {
- return OrderClass::getOrderById($id);
- }
- //查看订单详情 ssh 2020.1.4
- public static function getOrderBySn($orderSn)
- {
- return OrderClass::getOrderBySn($orderSn);
- }
- //计算优惠 ssh 2020.3.9
- public static function getDiscountPrice($params)
- {
- $couponId = isset($params['couponId']) ? $params['couponId'] : 0;
- $userId = isset($params['userId']) ? $params['userId'] : 0;
- $price = isset($params['price']) ? $params['price'] : 0;
- $sourceType = isset($params['sourceType']) ? $params['sourceType'] : 0;
- $discountAmount = 0;
- $discountType = 0;
- $discountPrice = $price;
- //使用优惠券
- if (!empty($couponId)) {
- $discountAmount = CouponService::checkBeforeUse($couponId, $price, $userId);
- $discountPrice = stringUtil::calcSub($price, $discountAmount);
- $discountType = 1;
- } else {
- //使用会员
- // $asset = UserAssetClass::getByUserId($userId);
- // if (isset($asset['member']) && $asset['member'] > 0) {
- // $currentDiscount = $asset['discount'];
- // $currentPrice = $price * $currentDiscount;
- // $newPrice = substr(sprintf("%.3f", $currentPrice), 0, -1);
- // $discountType = 2;
- // $discountAmount = stringUtil::calcSub($price, $newPrice);
- // $discountPrice = $newPrice;
- // }
- }
- //如果没有优惠券和会员优惠,并且是朋友圈收款,则进行随机优惠 ssh 2019.9.12
- if ($discountPrice == $price && $sourceType == 3) {
- $discountAmount = OrderClass::getRandDiscount($price);
- //开发和测试环境直接随机优惠
- $env = getenv('YII_ENV');
- if ($env == 'local' || $env == 'test') {
- $discountAmount = 0.01;
- }
- $discountPrice = stringUtil::calcSub($price, $discountAmount);
- $discountType = 3;
- }
- $discountType = $discountAmount <= 0 ? 0 : $discountType;
- $discountPrice = $discountPrice <= 0 ? 0.01 : $discountPrice;
- return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount];
- }
- //获取订单详情 ssh 2021.1.21
- public static function getOrderInfo($id, $showButton = false, $showItem = false, $showExpress = false, $isList = false)
- {
- $order = OrderClass::getById($id, true);
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- $info = $order->attributes;
- $respond = OrderClass::groupOrder([$info], $showButton, $showItem, $showExpress, $isList);
- $result = current($respond);
- $orderId = intval($result['id'] ?? 0);
- $result['orderClearList'] = $orderId > 0 ? OrderCgClearClass::listPaidClearRecordsForOrder($orderId) : [];
- return $result;
- }
- //订单失效后的操作 ssh 2021.5.14
- public static function expire($order)
- {
- $purchaseId = $order->purchaseId ?? 0;
- $current = PurchaseClass::getById($purchaseId, true);
- if (empty($current)) {
- util::fail('没有找到采购单');
- }
- PurchaseService::expire($current);
- return true;
- }
- //支付成功后的流程 ssh 2021.5.14
- //$isPurchase 是否零售端采购支付,如果是会引起销售方余额变动
- public static function payAfter($order, $payWay, $isPurchase = false)
- {
- if (isset($order->status) == false) {
- util::fail('没有订单状态');
- }
- if ($order->status != OrderClass::ORDER_STATUS_UN_PAY) {
- util::fail('重复操作');
- }
- if (isset($order->debt) == false) {
- util::fail('订单有问题,没有欠款项');
- }
- $orderId = $order->id;
- $orderSn = $order->orderSn ?? '';
- $customId = $order->customId;
- $ghs = null;
- if (in_array($payWay, [dict::getDict('payWay', 'balancePay'), dict::getDict('payWay', 'debtPay')], true)) {
- $pair = GhsRechargeSettleService::lockAccountPair(['id' => $customId]);
- $custom = $pair['custom'];
- $ghs = $pair['ghs'];
- } else {
- $custom = CustomClass::getLockById($customId);
- }
- if (empty($custom)) {
- util::fail('没有找到客户信息');
- }
- //门店资产变化
- $amount = $order->actPrice ?? 0.00;
- $realPrice = $order->realPrice ?? 0.00;
- $currentShopId = $order->shopId ?? 0;
- $book = $order->book ?? 0;
- $shop = ShopClass::getLockById($currentShopId);
- if (empty($shop)) {
- util::fail('没有找到门店5');
- }
- if ($payWay == dict::getDict('payWay', 'debtPay') && $book != 1) {
- if (isset($shop->overAllowDebt) && $shop->overAllowDebt == 0) {
- $debtLimit = $custom->debtLimit ?? 0;
- $outstanding = \bizGhs\custom\classes\AccountMoneyClass::getOutstandingDebt($custom);
- $newDebt = bcadd((string)$realPrice, (string)$outstanding, 2);
- if (bccomp($newDebt, (string)$debtLimit, 2) > 0) {
- util::fail('待结款已超' . $debtLimit . ',请先结账');
- }
- }
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息3');
- }
- $main->unPayOrder -= 1;
- //预订单的待发货订单增加
- if ($book == 1) {
- $main->unSendOrder += 1;
- //预订单统计数量增加
- $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($itemList)) {
- $bookData = [];
- foreach ($itemList as $currentItem) {
- $itemRemark = $currentItem->remark ?? 0;
- $hasRemark = !empty($itemRemark) ? 1 : 0;
- $bookData[] = [
- 'productId' => $currentItem->productId,
- 'num' => $currentItem->xhNum ?? 0,
- 'lastNum' => $currentItem->xhNum ?? 0,
- 'remark' => $itemRemark,
- 'hasRemark' => $hasRemark,
- ];
- }
- $staffId = $order->shopAdminId ?? 0;
- $staffName = $order->shopAdminName ?? '';
- $params = [
- 'staffId' => $staffId, 'staffName' => $staffName, 'action' => 'add'
- ];
- BookItemClass::addBatchItem($bookData, $order, $params);
- }
- } else {
- $main->finishOrder += 1;
- }
- $main->totalOrderNum += 1;
- $currentTotalIncome = bcadd($main->totalIncome, $amount, 2);
- $main->totalIncome = $currentTotalIncome;
- $totalSale = bcadd($main->totalSale, $amount, 2);
- $main->totalSale = $totalSale;
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- $currentGathering = bcadd($main->mayGathering, $realPrice, 2);
- $main->mayGathering = $currentGathering;
- if ($custom->debtNum == 0) {
- $main->mayGatheringNum += 1;
- }
- }
- $main->save();
- $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
- $shopAdminId = $order->shopAdminId ?? 0;
- $shopAdminName = $order->shopAdminName ?? '';
- $event = '下单';
- if ($isPurchase) {
- $event = '客户采购';
- if (isset($custom->name) && !empty($custom->name)) {
- $event = $custom->name . '采购';
- }
- }
- $sjId = $order->sjId ?? 0;
- $shopId = $order->shopId ?? 0;
- $capital = [
- 'capitalType' => $capitalType,
- 'relateId' => $orderId,
- 'io' => 1,
- 'amount' => $amount,
- 'totalIncome' => $currentTotalIncome,
- 'payWay' => $payWay,
- 'event' => $event,
- 'mainId' => $mainId,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'shopAdminId' => $shopAdminId,
- 'shopAdminName' => $shopAdminName,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capital);
- //客户采购,会增加余额,会增加收入
- if ($isPurchase == true) {
- //余额支付和在线支付(微信支付、支付宝支付)会增加余额
- if ($order->onlinePay == dict::getDict('onlinePay', 'yes') || $payWay == dict::getDict('payWay', 'balancePay')) {
- ShopClass::customKdAddBalance($main, $shop, $realPrice, $order, $capitalType);
- }
- }
- //现金付款,现金余额需要增加
- if ($payWay == dict::getDict('payWay', 'cash')) {
- $moneyBalance = bcadd($main->money, $realPrice, 2);
- $main->money = $moneyBalance;
- $main->save();
- $moneyEvent = $custom->name . "现金下单" . floatval($realPrice) . "元(单号 {$orderSn})";
- $moneyRemark = '';
- $change = [
- 'relateId' => $orderId,
- 'amount' => $realPrice,
- 'balance' => $moneyBalance,
- 'io' => 1,
- 'mainId' => $mainId,
- 'capitalType' => $capitalType,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'event' => $moneyEvent,
- 'remark' => $moneyRemark,
- ];
- ShopMoneyChangeClass::addData($change);
- }
- //更新订单基础状态和支付状态
- $order->payWay = $payWay;
- if ($book == 1) {
- $order->status = OrderClass::ORDER_STATUS_UN_SEND;
- } else {
- $order->status = OrderClass::ORDER_STATUS_UN_SEND;
- }
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- if (isset($order->actPrice) && $order->actPrice == 0) {
- $order->debt = OrderClass::DEBT_NO;
- } else {
- $order->debt = OrderClass::DEBT_YES;
- }
- $order->remainDebtPrice = $order->orderPrice ?? 0;
- $order->debtPrice = $order->orderPrice ?? 0;
- }
- $order->payStatus = 1;
- if (!isset($order->payTime) || $order->payTime == '0000-00-00 00:00:00') {
- $order->payTime = date("Y-m-d H:i:s");
- }
- $order->save();
- //新订单进入队列,用于花材的销量统计
- OrderClass::newOrderAddQueue($order);
- //员工业绩、总业绩增加
- $shopAdminId = $order->shopAdminId ?? 0;
- if (!empty($shopAdminId)) {
- $shopAdmin = ShopAdminClass::getById($shopAdminId, true);
- $shopAdmin->kdAmount = bcadd($shopAdmin->kdAmount, $amount, 2);
- $shopAdmin->save();
- $yjAmount = $order->orderPrice ?? 0;
- StatYjClass::addYj($main, $shop, $shopAdminId, $yjAmount);
- }
- //余额支付
- if ($payWay == dict::getDict('payWay', 'balancePay') && $isPurchase == false) {
- CustomClass::payToChangeBalance($order, $custom, $ghs);
- }
- //欠款
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- CustomClass::cgDebtAmountAdd($custom, $order, false, $ghs);
- }
- $custom->buyNum += 1;
- $custom->buyAmount = bcadd($custom->buyAmount, $amount, 2);
- $custom->visitTime = date("Y-m-d H:i:s");
- $custom->save();
- //收入统计
- StatIncomeClass::updateOrInsert($main, $shop, $amount);
- //各渠道收入金额统计
- StatKdClass::replace($shop, $amount, $payWay);
- //今日订单+1
- StatOrderClass::updateOrInsert($main, $shop);
- //销售统计
- StatSaleClass::replace($main, $shop, $amount);
- //客户采购统计
- StatKhCgClass::replace($order, $amount);
- //当天同个人同地址的订单汇总
- $prefixDate = substr($order->payTime, 0, 10);
- OrderClass::updateSameTimeIds($order, $prefixDate);
- OrderClass::updateMergeOrder($order,0);
- }
- //发起退款
- public static function refund($id, $post)
- {
- $order = OrderClass::getLockById($id);
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- if (isset($order->clearId) && !empty($order->clearId)) {
- util::fail('已经结账,不能再售后');
- }
- if ($order->debt != 1 && $order->addTime < '2023-10-17 00:00:00') {
- util::fail('10月17日前订单无法申请退款');
- }
- //供货商销售单退款
- $saleRefund = RefundOrderService::addRefund($post, $order);
- $saleRefundId = $saleRefund->id;
- $purchaseId = $order->purchaseId;
- $cg = PurchaseClass::getLockById($purchaseId);
- if (empty($cg)) {
- util::fail('没有找到采购订单');
- }
- $mainId = $cg->mainId ?? 0;
- //花材列表结构
- // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
- //供货商的花材信息转换成零售店的花材信息
- $ghsProduct = $post['product'] ?? '';
- $ghsProductIndex = array_column($ghsProduct, null, 'productId');
- $ids = array_column($ghsProduct, 'productId');
- $ghsProductInfo = ProductClass::getByIds($ids, null, 'itemId');
- $ptItemIds = array_keys($ghsProductInfo);
- $ptItemIds = array_unique(array_filter($ptItemIds));
- $hdProductInfo = ProductClass::getAllByCondition(['mainId' => $mainId, 'itemId' => ['in', $ptItemIds]], null, '*', 'itemId');
- $newProduct = [];
- foreach ($hdProductInfo as $ptItemId => $hdInfo) {
- $ghsItemId = $ghsProductInfo[$ptItemId]['id'] ?? 0;
- $current = $ghsProductIndex[$ghsItemId] ?? [];
- $hdItemId = $hdInfo['id'] ?? 0;
- $cost = $hdInfo['cost'] ?? 0;
- $name = $hdInfo['name'] ?? '';
- $cover = $hdInfo['cover'] ?? '';
- $ratio = $hdInfo['ratio'] ?? 20;
- if (empty($current)) {
- util::fail('没有找到花材');
- }
- $current['productId'] = $hdItemId;
- $current['ptItemId'] = $ptItemId;
- $current['cost'] = $cost;
- $current['name'] = $name;
- $current['cover'] = $cover;
- $current['ratio'] = $ratio;
- $newProduct[] = $current;
- }
- $refundType = $post['refundType'] ?? 0;
- $price = $post['price'] ?? 0;
- $remark = $post['remark'] ?? '';
- $cause = $post['cause'] ?? 0;
- $refundSendCost = isset($post['refundSendCost']) && is_numeric($post['refundSendCost']) ? $post['refundSendCost'] : 0;
- $refundPackCost = isset($post['refundPackCost']) && is_numeric($post['refundPackCost']) ? $post['refundPackCost'] : 0;
- $cgData = [
- 'product' => $newProduct,
- 'refundType' => $refundType,
- 'price' => $price,
- 'remark' => $remark,
- 'cause' => $cause,
- 'refundSendCost' => $refundSendCost,
- 'refundPackCost' => $refundPackCost,
- ];
- $cgRefund = CgRefundService::addRefund($cgData, $cg);
- $cgRefundId = $cgRefund->id;
- $saleRefund->cgRefundId = $cgRefundId;
- $saleRefund->save();
- $cgRefund->saleRefundId = $saleRefundId;
- $cgRefund->save();
- return $saleRefund;
- }
- }
|