| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013 |
- <?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\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\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;
- if (getenv('YII_ENV') == 'production') {
- $specialList = [10649, 8042, 16454, 21319, 33668, 42345];
- } else {
- $specialList = [36523];
- }
- if (!in_array($ghsShopId, $specialList)) {
- $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);
- $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 ($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($currentProduct[$currentKey]['price'])) {
- $currentProduct[$currentKey]['userPrice'] = $currentProduct[$currentKey]['price'];
- unset($currentProduct[$currentKey]['price']);
- }
- }
- $sendTimeWant = isset($data['sendTimeWant']) && !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
- $remark = $data['remark'] ?? '';
- $xj = $data['xj'] ?? '';
- $book = $data['book'] ?? 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,
- 'discount' => 1,
- 'mainId' => $cgMainId,
- 'wlName' => $wlName,
- 'localOrder' => $localOrder,
- 'transType' => $transType,
- ];
- if (isset($data['deadline']) && !empty($data['deadline'])) {
- $purchaseData['deadline'] = $data['deadline'];
- }
- if (isset($data['modifyPrice']) && is_numeric($data['modifyPrice'])) {
- $purchaseData['modifyPrice'] = $data['modifyPrice'];
- }
- $purchase = PurchaseClass::addPurchase($purchaseData, $ghs);
- $purchaseId = $purchase->id ?? 0;
- $returnOrder->purchaseId = $purchaseId;
- $returnOrder->save();
- //欠款和已付款的走付款后流程
- if (in_array($hasPay, [dict::getDict('hasPay', 'payed'), dict::getDict('hasPay', 'debt')])) {
- if ($hasPay == dict::getDict('hasPay', 'debt')) {
- $shopId = $data['shopId'] ?? 0;
- $shop = ShopClass::getById($shopId, true);
- if (empty($shop)) {
- util::fail('没有门店信息');
- }
- //老商家允许设置overAllowDebt,新商家已经不允许设置,默认就是 1,不能超过已经设置的授信额度
- if (isset($shop->overAllowDebt) && $shop->overAllowDebt == 0) {
- $debtLimit = $custom['debtLimit'] ?? 0;
- $debtLimit = floatval($debtLimit);
- $debtAmount = $custom['debtAmount'] ?? 0;
- $currentPrice = $returnOrder->actPrice ?? 0;
- $newDebt = bcadd($currentPrice, $debtAmount, 2);
- if ($newDebt > $debtLimit) {
- util::fail('欠款已超' . $debtLimit . ',请先清账');
- }
- }
- }
- //下面二个顺序不能换
- PurchaseService::payAfter($purchase, $payWay);
- self::payAfter($returnOrder, $payWay);
- //开历史订单
- $historyDate = $data['historyDate'] ?? '';
- if (!empty($historyDate)) {
- $currentPayTime = $historyDate . ' 12:00:00';
- $purchase->payTime = $currentPayTime;
- $purchase->save();
- $returnOrder->payTime = $currentPayTime;
- $returnOrder->save();
- //有修改账单日期需要更新到欠款变动明细,财务好对账,多个地方要同步修改,关键词to_change_custom_debt_pay_time
- CustomDebtChangeClass::updateByCondition(['capitalType' => 10, 'relateId' => $returnOrder->id], ['payTime' => $currentPayTime]);
- }
- //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
- if (isset($returnOrder->book) && $returnOrder->book == 0) {
- $ghsShopId = $ghs['shopId'] ?? 0;
- if (getenv('YII_ENV') == 'production') {
- $specialList = [10649, 8042, 16454, 21319, 33668,42345];
- } else {
- $specialList = [36523];
- }
- if (!in_array($ghsShopId, $specialList)) {
- $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);
- return current($respond);
- }
- //订单失效后的操作 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;
- $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');
- }
- $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;
- $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', 'debtPay')) {
- CustomClass::cgDebtAmountAdd($custom, $order);
- }
- $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);
- //当天同个人同地址的订单汇总
- $sameDate = date("Y_m_d");
- $sameAddress = $order->showAddress ?? '';
- $addressMd5 = md5($sameAddress);
- $sameKey = $customId . '_' . $addressMd5 . '_' . $sameDate;
- $sameString = Yii::$app->redis->executeCommand('GET', [$sameKey]);
- if (!empty($sameString)) {
- $newString = $sameString . ',' . $orderId;
- $newIdsArray = explode(',', $newString);
- if (count($newIdsArray) <= 90) {
- Yii::$app->redis->executeCommand('SETEX', [$sameKey, 186400, $newString]);
- foreach ($newIdsArray as $currentOrderId) {
- OrderClass::updateById($currentOrderId, ['sameTimeIds' => $newString]);
- }
- //noticeUtil::push($sameKey . '' . $sameAddress . " 当天汇总ids:" . $newString, '15280215347');
- }
- } else {
- Yii::$app->redis->executeCommand('SETEX', [$sameKey, 186400, $orderId]);
- $order->sameTimeIds = $orderId;
- $order->save();
- //noticeUtil::push($sameKey . '' . $sameAddress . " 当天汇总ids:" . $orderId, '15280215347');
- }
- }
- //发起退款
- 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;
- }
- }
|