| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079 |
- <?php
- namespace bizHd\order\classes;
- use biz\goods\classes\GoodsClass;
- use biz\goods\models\Goods;
- 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\stat\classes\StatKdClass;
- use biz\stat\classes\StatOrderCountClass;
- use biz\stat\classes\StatSaleClass;
- use biz\stock\classes\GoodsStockRecordClass;
- use biz\wx\classes\WxMessageClass;
- use bizGhs\order\classes\CheckOrderClass;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\stat\classes\StatYjClass;
- use bizGhs\stock\classes\StockRecordClass;
- use bizGhs\ws\services\WsService;
- use bizHd\custom\classes\CustomClass;
- use bizHd\merchant\classes\SjClass;
- use bizHd\order\classes\OrderItemClass;
- use bizHd\refund\classes\HdRefundClass;
- use bizHd\shop\classes\MainClass;
- use bizHd\shop\classes\ShopMoneyChangeClass;
- use bizHd\stat\classes\StatIncomeClass;
- use bizHd\stat\classes\StatOrderClass;
- use bizHd\user\classes\UserClass;
- use bizHd\work\classes\WorkClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\miniUtil;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\printUtil;
- use common\components\util;
- use Yii;
- use bizHd\base\classes\BaseClass;
- use common\components\lakala\Lakala;
- class OrderClass extends BaseClass
- {
- public static $baseFile = '\bizHd\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 ORDER_TOTAL_FLOW = 4;
- //未确认配送方式
- const SEND_TYPE_UNKNOWN = 0;
- //自取
- const SEND_TYPE_NO = 1;
- //自己送
- const SEND_TYPE_MYSELF = 2;
- //快递送
- const SEND_TYPE_THIRD = 3;
- //正常订单
- const REFUND_NO = 0;
- //已退款订单
- const REFUND_YES = 1;
- //送达时间段
- public static $reachPeriod = [0 => '上午', 1 => '下午', 2 => '晚上'];
- //优惠金额
- public static $randDiscount = [
- 50 => [1, 3],
- 100 => [5, 9],
- 300 => [10, 15],
- 500 => [16, 18],
- 1000 => [18, 30],
- 20000 => [90, 100],
- ];
- //因为开负数订单时引导库存变化 ssh 20250731
- public static function shPayChange($order, $shop, $staff, $originOrder = null)
- {
- $forward = $order['forward'] ?? 0;
- if ($forward == 0) {
- return false;
- }
- $forwardStock = $order['forwardStock'] ?? 0;
- $orderSn = $order['orderSn'] ?? '';
- $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- $orderGoodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- if (empty($orderItemList) && empty($orderGoodsList)) {
- return false;
- }
- $shopId = $shop->id ?? 0;
- $sjId = $shop->sjId ?? 0;
- $adminId = $staff->adminId;
- $staffName = $staff->name ?? '';
- $mainId = $shop->mainId;
- if (!empty($orderItemList)) {
- $itemInfo = [];
- $ids = array_column($orderItemList, 'itemId');
- $infoList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id');
- foreach ($orderItemList as $orderItem) {
- $unitType = $orderItem['unitType'] ?? 0;
- $num = $orderItem['num'] ?? 0;
- $productId = $orderItem['itemId'] ?? 0;
- $info = $infoList[$productId] ?? [];
- $stock = $info['stock'] ?? 0;
- $ratio = $info['ratio'] ?? 20;
- $map = ProductClass::formatStock($stock, $ratio);
- $bigNum = $map['bigNum'] ?? 0;
- $smallNum = $map['smallNum'] ?? 0;
- $num = $forwardStock == 0 ? $num * 2 : $num;
- if ($unitType == 0) {
- $bigNum = $bigNum + $num;
- } else {
- $smallNum = $smallNum + $num;
- }
- $itemInfo[] = ['productId' => $productId, 'bigNum' => $bigNum, 'smallNum' => $smallNum];
- }
- $remark = $forwardStock == 0 ? '开售后付款单,退货并退款' : '开售后付款单,只退款';
- $itemData = [
- 'remark' => $remark,
- 'itemInfo' => $itemInfo,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'adminId' => $adminId,
- 'staffName' => $staffName,
- ];
- CheckOrderClass::opOrder($itemData);
- }
- if (!empty($orderGoodsList)) {
- foreach ($orderGoodsList as $orderGoods) {
- $goodsId = $orderGoods['goodsId'] ?? 0;
- $num = $orderGoods['num'] ?? 0;
- $num = $forwardStock == 0 ? $num * 2 : $num;
- $ret = \bizHd\goods\classes\GoodsClass::addStock($goodsId, $mainId, $num);
- $oldStock = $ret['oldStock'] ?? 0;
- $newStock = $ret['newStock'] ?? 0;
- $recordData = [];
- $recordData['sjId'] = $sjId;
- $recordData['shopId'] = $shopId;
- $recordData['mainId'] = $mainId;
- $recordData['orderSn'] = '';
- $recordData['goodsId'] = $goodsId;
- $recordData['goodsNum'] = $num;
- $recordData['oldStock'] = $oldStock;
- $recordData['newStock'] = $newStock;
- $recordData['relateName'] = $staffName;
- GoodsStockRecordClass::pdGoods($recordData);
- }
- }
- if (is_object($order) && !empty($originOrder)) {
- //建立关系
- $originOrder->hasForward = 1;
- $originOrder->save();
- $orderId = $originOrder->id ?? 0;
- $orderSn = $originOrder->orderSn ?? '';
- $shOrderId = $order->id ?? 0;
- $shOrderSn = $order->orderSn ?? '';
- $map = ['orderId' => $orderId, 'orderSn' => $orderSn, 'shOrderId' => $shOrderId, 'shOrderSn' => $shOrderSn];
- OrderForwardClass::add($map, true);
- }
- return true;
- }
- /**
- * @param $order
- * @param $mainId
- * @return array
- * $shAddPay 0走正常流程售后,1走售后付款单
- * $shAddPayReason 走售后付款单的原因:0、售后金额大于剩余欠款(剩余欠款可能没有了,可能还有);1、可售后时间到了
- */
- public static function ifShPay($order, $main, $refundAmount = 0)
- {
- $shAddPay = 0;
- $shAddPayReason = 0;
- $payStatus = $order['payStatus'] ?? 0;
- if ($payStatus == 1) {
- if ($order['payWay'] == 3) {
- if ($order['remainDebtPrice'] <= 0) {
- //不管是否要售后,没有剩余欠款了,只能走开负数订单模式
- $shAddPay = 1;
- } else {
- if ($refundAmount > 0 && $refundAmount > $order['remainDebtPrice']) {
- //如果需要售后,并且售后金额大于剩余欠款,走开负数订单模式
- $shAddPay = 1;
- }
- }
- }
- $cRet = OrderClass::couldRefund($order, $main);
- $could = $cRet['could'];
- if (!$could) {
- //可售后时间到了
- $shAddPay = 1;
- $shAddPayReason = 1;
- }
- }
- return ['shAddPay' => $shAddPay, 'shAddPayReason' => $shAddPayReason];
- }
- //订单是否过了可以售后的时间 ssh 20250730
- // $could false 过了售后时间,不能售后,true可以
- public static function couldRefund($order, $main)
- {
- $refundLimit = $main->refundLimit ?? 0;
- $payTime = $order['payTime'];
- $currentPayTime = strtotime($payTime);
- $addDateTime = $refundLimit * 86400;
- $couldRefundTime = $currentPayTime + $addDateTime;
- $now = time();
- $could = $couldRefundTime > $now;
- return ['could' => $could];
- }
- public static function valid($order, $mainId)
- {
- if (!isset($order->mainId) || $order->mainId != $mainId) {
- Yii::info(json_encode($order));
- Yii::info('mainId: ' . $mainId);
- util::fail('无法访问的订单');
- }
- return true;
- }
- //添加订单 ssh 2019.12.5
- public static function addOrder($data)
- {
- $data['orderSn'] = $data['orderSn'] ?? orderSn::getOrderSn();
- $data['payStatus'] = $data['payStatus'] ?? 0;
- $shopId = $data['shopId'] ?? 0;
- //累计订单增加
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店信息54');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有找到main信息');
- }
- $main->unPayOrder += 1;
- $main->totalOrder += 1;
- $main->save();
- $address = $data['address'] ?? '';
- $floor = $data['floor'] ?? '';
- if (isset($data['fullAddress']) == false || empty($data['fullAddress'])) {
- $data['fullAddress'] = $address . $floor;
- }
- //非自取订单考虑配送时间
- $data['reachDate'] = isset($data['reachDate']) && !empty($data['reachDate']) ? $data['reachDate'] : date("Y-m-d");
- $sendType = $data['sendType'] ?? 0;
- if ($sendType != 1) {
- if (isset($data['reachPeriod']) == false || empty($data['reachPeriod'])) {
- util::fail('请选择配送时间');
- }
- $data['reachTime'] = strtotime($data['reachDate'] . ' ' . $data['reachPeriod']);
- }
- //将花店每月的总订单数作为编号
- $riseNum = StatOrderCountClass::addOrder($shop, $main);
- $data['sendNum'] = date("d") . $riseNum;
- $data['totalFlow'] = OrderClass::ORDER_TOTAL_FLOW;
- return self::add($data, true);
- }
- public static function getByOrderSn($orderSn)
- {
- return self::getByCondition(['orderSn' => $orderSn]);
- }
- //评价 ssh 2019.12.16
- public static function comment($data)
- {
- $id = $data['id'];
- unset($data['id']);
- return self::updateById($id, $data);
- }
- public static function getFullInfo($id)
- {
- $order = self::getById($id);
- if (empty($order)) {
- return [];
- }
- $sn = $order['orderSn'] ?? '';
- $order['goodsInfoList'] = OrderGoodsClass::getListBySn($sn);
- $order['itemInfoList'] = OrderItemClass::getListBySn($sn);
- $workList = WorkClass::getAllByCondition(['orderId' => $id], null, '*');
- if (!empty($workList)) {
- foreach ($workList as $key => $work) {
- $shortCover = $work['cover'] ?? '';
- $workList[$key]['shortCover'] = $shortCover;
- $workList[$key]['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- }
- }
- $order['workList'] = $workList;
- $debtPrice = $order['debtPrice'] ?? 0;
- $remainDebtPrice = $order['remainDebtPrice'] ?? 0;
- $orderId = $order['id'] ?? 0;
- $osList = [];
- if ($debtPrice > $remainDebtPrice) {
- $osList = OrderSettleClass::getAllByCondition(['orderId' => $orderId, 'status' => 1], null, '*', null, true);
- }
- //结账记录关系
- $order['orderSettleList'] = $osList;
- $order['today'] = date("Y-m-d");
- $order['tomorrow'] = date("Y-m-d", strtotime("+1 day"));
- return $order;
- }
- //订单
- public static function getOrderById($id)
- {
- $order = self::getById($id);
- if (empty($order)) {
- return [];
- }
- $sn = $order['orderSn'] ?? '';
- $order['goodsInfoList'] = OrderGoodsClass::getListBySn($sn);
- return $order;
- }
- //根据订单查询 ssh 2020.1.4
- public static function getOrderBySn($orderSn)
- {
- $order = self::getByCondition(['orderSn' => $orderSn]);
- if (empty($order)) {
- return [];
- }
- $id = $order['id'];
- $order['goodsInfoList'] = OrderGoodsClass::getGoodsListById($id);
- return $order;
- }
- //返回随机优惠金额 ssh 2019.9.12
- public static function getRandDiscount($amount)
- {
- if ($amount <= 0) {
- return 0;
- }
- $randDiscount = self::$randDiscount;
- krsort($randDiscount);
- $rand = 0;
- foreach ($randDiscount as $currentAmount => $val) {
- if ($amount >= $currentAmount) {
- $rand = rand($val[0], $val[1]);
- break;
- }
- }
- return $rand / 10;
- }
- //第三方支付后回调处理流程 ssh 2021.4.30
- public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId)
- {
- $order = self::getByCondition(['orderSn' => $orderSn], true);
- if (empty($order)) {
- noticeUtil::push('散客下单,付款成功,支付回调,单号:' . $orderSn . ' 金额:' . $totalFee . ' 回调id:' . $transactionId . ' 附件:' . $attach . ',没有找到订单', '15280215347');
- $msg = "没有找到零售订单 orderSn:{$orderSn}";
- Yii::info($msg);
- util::fail($msg);
- }
- if ($order->mainPay != $totalFee) {
- noticeUtil::push("散客下单,付款成功,支付回调错误,零售订单金额与回调通知金额不一致 orderSn:{$orderSn} {$order->mainPay} {$totalFee} 附件:{$attach}", '15280215347');
- $msg = "零售订单金额与回调通知金额不一致 orderSn:{$orderSn} {$order->mainPay} {$totalFee}";
- Yii::info($msg);
- util::fail($msg);
- }
- $id = $order->id;
- $order = self::getLockById($id);
- $date = date("Y-m-d H:i:s");
- $order->payTime = $date;
- $order->thirdNo = $transactionId;
- $order->onlinePay = dict::getDict('onlinePay', 'yes');
- $order->save();
- self::payAfter($order, $payWay);
- }
- //订单完成 ssh 2021.4.20
- public static function payAfter($order, $payWay)
- {
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- if (!isset($order->status)) {
- util::fail('没有找到订单的状态');
- }
- $orderSn = $order->orderSn ?? '';
- if ($order->status == self::ORDER_STATUS_CANCEL) {
- $shopId = $order->shopId;
- $shop = ShopClass::getById($shopId, true);
- if (empty($shop)) {
- noticeUtil::push('注意,零售订单:' . $orderSn . ',付款成功但订单已取消,没有找到门店,关键词 cancel_get_money', '15280215347');
- util::stop("SUCCESS");
- }
- Yii::$app->params['errorReport'] = 1;
- try {
- //恢复取消的订单
- self::recoverOrderToPay($order);
- noticeUtil::push('注意,零售订单:' . $orderSn . ',付款成功,取消的订单,已恢复 SUCCESS', '15280215347');
- } catch (\Exception $exception) {
- $ret = HdRefundClass::onlyRefundAmount($shop, $order);
- if ($ret['status'] == 0) {
- $errMsg = $exception->getMessage();
- noticeUtil::push('注意,零售订单:' . $orderSn . ',付款成功但订单取消。订单没有恢复成功,原因' . $errMsg . ',也没有退款成功。请手动处理。关键词 cancel_get_money', '15280215347');
- }
- util::stop("SUCCESS");
- }
- }
- if ($order->payStatus == 1) {
- noticeUtil::push('注意,零售订单:' . $orderSn . ',已经付过款了,无需重复请求和操作', '15280215347');
- util::fail('订单已经付过款了');
- }
- $orderId = $order->id ?? 0;
- $customId = $order->customId ?? 0;
- if ($order->fromType == 3) {
- //客服下的单要配送流程
- $order->status = self::ORDER_STATUS_UN_SEND;
- } else {
- $order->status = self::ORDER_STATUS_COMPLETE;
- }
- $order->payWay = $payWay;
- $order->payStatus = 1;
- $order->payTime = date("Y-m-d H:i:s");
- $order->stockChange = 1;
- $order->save();
- $myCustom = CustomClass::getLockById($customId);
- if (!empty($myCustom)) {
- if ($order->forward == 0) {
- $myCustom->recentExpend = date("Y-m-d H:i:s");
- $myCustom->visitTime = date("Y-m-d H:i:s");
- $myCustom->save();
- }
- }
- \bizMall\order\classes\OrderGoodsClass::stockChange($order);
- $realPrice = $order->realPrice ?? 0;
- $shopId = $order->shopId;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店,编号5115');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息,编号3096');
- }
- $main->unSendOrder += 1;
- $main->unPayOrder -= 1;
- $currentTotalIncome = bcadd($main->totalIncome, $realPrice, 2);
- $main->totalIncome = $currentTotalIncome;
- $main->save();
- $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
- $shopAdminId = $order->shopAdminId ?? 0;
- $shopAdminName = $order->shopAdminName ?? '';
- $sjId = $order->sjId;
- $fromType = $order->fromType ?? 1;
- $event = !empty($shopAdminId) ? '员工开单' : '客户下单';
- $capital = [
- 'capitalType' => $capitalType,
- 'relateId' => $orderId,
- 'io' => 1,
- 'totalIncome' => $currentTotalIncome,
- 'payWay' => $payWay,
- 'event' => $event,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'shopAdminId' => $shopAdminId,
- 'shopAdminName' => $shopAdminName,
- 'amount' => $realPrice,
- 'fromType' => $fromType,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capital);
- //每天和每月收入增加
- //StatIncomeClass::updateOrInsert($main, $shop, $realPrice);
- //今日订单+1
- //StatOrderClass::updateOrInsert($main, $shop);
- //销售收入增加
- //StatSaleClass::replace($main, $shop, $realPrice);
- //各渠道收入金额统计
- //StatKdClass::replace($shop, $realPrice, $payWay);
- //当天同个人同地址的订单汇总
- $sameDate = date("Y_m_d");
- $sameAddress = $order->showAddress ?? '';
- $addressMd5 = md5($sameAddress);
- $sameKey = $customId . '_' . $addressMd5 . '_' . $sameDate . '_retail';
- $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');
- }
- //客户在线支付下单增加商家可提现余额
- if (isset($order->onlinePay) && $order->onlinePay == dict::getDict('onlinePay', 'yes')) {
- $amount = $order->mainPay;
- ShopClass::customKdAddBalance($main, $shop, $amount, $order, $capitalType);
- }
- //客户使用余额付款
- if ($payWay == dict::getDict('payWay', 'balancePay')) {
- CustomClass::payToChangeBalance($order);
- }
- //有欠款时
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- if (isset($order->remainDebtPrice) && $order->remainDebtPrice > 0) {
- CustomClass::cgDebtAmountAdd($myCustom, $order);
- }
- }
- //全部现金付款余额增加
- if ($payWay == dict::getDict('payWay', 'cash')) {
- $moneyBalance = bcadd($main->money, $order->mainPay, 2);
- $main->money = $moneyBalance;
- $main->save();
- $moneyEvent = "客户现金下单" . floatval($order->mainPay) . "元(单号 {$order->orderSn})";
- $moneyRemark = '';
- $change = [
- 'relateId' => $orderId,
- 'amount' => $order->mainPay,
- 'balance' => $moneyBalance,
- 'io' => 1,
- 'mainId' => $mainId,
- 'capitalType' => $capitalType,
- 'ptStyle' => dict::getDict('ptStyle', 'hd'),
- 'event' => $moneyEvent,
- 'remark' => $moneyRemark,
- ];
- ShopMoneyChangeClass::addData($change);
- } else {
- //部分现金付款
- $orderCash = $order->cash ?? 0;
- $orderCash = floatval($orderCash);
- if ($orderCash > 0) {
- $moneyBalance = bcadd($main->money, $orderCash, 2);
- $main->money = $moneyBalance;
- $main->save();
- $moneyEvent = "客户部分现金付款{$orderCash}元(单号 {$order->orderSn})";
- $moneyRemark = '';
- $change = [
- 'relateId' => $orderId,
- 'amount' => $orderCash,
- 'balance' => $moneyBalance,
- 'io' => 1,
- 'mainId' => $mainId,
- 'capitalType' => $capitalType,
- 'ptStyle' => dict::getDict('ptStyle', 'hd'),
- 'event' => $moneyEvent,
- 'remark' => $moneyRemark,
- ];
- ShopMoneyChangeClass::addData($change);
- }
- }
- }
- //转化出送到时间 ssh 2020.3.15
- public static function getReachTime($order)
- {
- $reachTime = '';
- if (isset($order['reachDate']) && !empty($order['reachDate'])) {
- $prev = date("n-j", strtotime($order['reachDate']));
- $periodData = self::$reachPeriod;
- $periodKey = $order['reachPeriod'];
- $period = isset($periodData[$periodKey]) ? $periodData[$periodKey] : '上午';
- $reachTime = $prev . ' ' . $period;
- }
- return $reachTime;
- }
- //支付成功获取unionId ssh 2020.5.12
- public static function payToUpdateUnionId($merchant, $merchantExtend, $orderSn, $user)
- {
- $mcId = $merchantExtend['wxPayMerchantId'];
- $miniOpenId = isset($user['miniOpenId']) ? $user['miniOpenId'] : '';
- $unionId = isset($user['unionId']) ? $user['unionId'] : '';
- $userId = $user['id'];
- if (empty($unionId)) {
- $unionId = miniUtil::getPaidUnionId($merchant, $miniOpenId, $mcId, (string)$orderSn, 0);
- if (!empty($unionId)) {
- $findUser = UserClass::getByUnionId($unionId);
- if (empty($findUser)) {
- UserClass::updateByCondition(['id' => $userId], ['unionId' => $unionId]);
- } else {
- UserClass::mergeUser($findUser, $user);
- }
- }
- }
- }
- //订单取消 ssh 20220516
- public static function setExpire($order, $force = false)
- {
- $now = time();
- if ($order->deadline > $now && $force == false) {
- return false;
- }
- if ($order->status != 1) {
- return false;
- }
- $order->status = 5;
- $order->cancelTime = date("Y-m-d H:i:s");
- $order->save();
- //暂时只考虑花材库存的回滚。商品只有下单成功了才会占用库存。
- $orderSn = $order->orderSn ?? '';
- $itemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($itemList)) {
- foreach ($itemList as $item) {
- $ptItemId = $item->ptItemId ?? 0;
- $sjId = $item->sjId ?? 0;
- $shopId = $item->shopId ?? 0;
- $mainId = $item->mainId ?? 0;
- $ratio = $item->ratio ?? 20;
- $unitType = $item->unitType ?? 0;
- if ($unitType == 0) {
- $bigNum = $item->num;
- $smallNum = 0;
- } else {
- $bigNum = 0;
- $smallNum = $item->num;
- }
- $itemId = $item->itemId ?? 0;
- $stockInfo = ProductClass::addStock($itemId, $bigNum, $smallNum);
- $recordData = [];
- $recordData['sjId'] = $sjId;
- $recordData['shopId'] = $shopId;
- $recordData['mainId'] = $mainId;
- $recordData['itemId'] = $ptItemId;
- $recordData['itemNum'] = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
- $recordData['oldStock'] = $stockInfo['oldStock'];
- $recordData['newStock'] = $stockInfo['newStock'];
- $recordData['productId'] = $itemId;
- $recordData['orderSn'] = $orderSn;
- $recordData['relateName'] = '系统';
- StockRecordClass::addSellStockOrderCancelRecord($recordData);
- }
- }
- $shopId = $order->shopId ?? 0;
- $shop = ShopClass::getById($shopId, true);
- if (!empty($shop)) {
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $shop->lklSjNo,
- 'term_no' => $shop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $closeParams = [
- 'orderSn' => $orderSn,
- ];
- $response = $laResource->close($closeParams);
- if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
- //noticeUtil::push('零售订单:' . $orderSn . ',关单没有成功。', '15280215347');
- } else {
- //noticeUtil::push('零售订单:' . $orderSn . ',关单成功。', '15280215347');
- }
- }
- }
- // 恢复订单 -- 即:把订单的状态从取消状态变成待付款状态
- public static function recoverOrderToPay($order)
- {
- $now = time();
- $order->status = 1;
- $order->deadline = $now + 86400;
- $order->save();
- // 重新占用库存(与取消订单时的库存回滚相反)
- $itemList = OrderItemClass::getAllByCondition(['orderSn' => $order->orderSn], null, '*', null, true);
- if (!empty($itemList)) {
- foreach ($itemList as $item) {
- $ptItemId = $item->ptItemId ?? 0;
- $sjId = $item->sjId ?? 0;
- $shopId = $item->shopId ?? 0;
- $mainId = $item->mainId ?? 0;
- $ratio = $item->ratio ?? 20;
- $unitType = $item->unitType ?? 0;
- if ($unitType == 0) {
- $bigNum = $item->num;
- $smallNum = 0;
- } else {
- $bigNum = 0;
- $smallNum = $item->num;
- }
- $itemId = $item->itemId ?? 0;
- // 重新扣减库存(占用库存)
- $stockInfo = ProductClass::decreaseStock($itemId, $bigNum, $smallNum, true);
- $recordData = [];
- $recordData['sjId'] = $sjId;
- $recordData['shopId'] = $shopId;
- $recordData['mainId'] = $mainId;
- $recordData['itemId'] = $ptItemId;
- $recordData['itemNum'] = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
- $recordData['oldStock'] = $stockInfo['oldStock'];
- $recordData['newStock'] = $stockInfo['newStock'];
- $recordData['productId'] = $itemId;
- $recordData['orderSn'] = $order->orderSn;
- $recordData['relateName'] = $order->customName ?? '';
- StockRecordClass::hdKdAddRecord($recordData);
- }
- }
- return true;
- }
- // 扣库存
- protected static function decGoodsStock($orderGoods)
- {
- foreach ($orderGoods as $goods) {
- GoodsClass::counters([
- 'stock' => -$goods['num']
- ], [
- 'and',
- ['id' => $goods['goodsId']],
- ['<>', 'stock', Goods::FULL_STOCK]
- ]);
- }
- }
- //云打印 ssh 20210709
- public static function onlinePrint($order, $must = false, $port = 'front')
- {
- if (empty($order)) {
- return false;
- }
- if (!$must) {
- if ($order->needPrint != dict::getDict('needPrint', 'need')) {
- return false;
- }
- if ($order->payStatus != 1) {
- return false;
- }
- }
- if ($order->forward == 1) {
- //售后付款单暂时不能打印
- return false;
- }
- $shopId = $order->shopId ?? 0;
- $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
- $printSn = $ext['printSn'] ?? '';
- if ($port == 'make') {
- $printSn = $ext['makePrintSn'] ?? '';
- if ($order->fromType == 4) {
- $printSn = $ext['mtPrintSn'] ?? '';
- }
- $workList = WorkClass::getAllByCondition(['orderId' => $order->id], null, '*', null, true);
- $hasSh = 0;
- if (!empty($workList)) {
- foreach ($workList as $work) {
- $sh = $work->sh ?? 0;
- if ($sh == 1) {
- $hasSh = 1;
- }
- }
- }
- if ($hasSh == 1) {
- $printSn = $ext['printSn'] ?? '';
- }
- }
- if (empty($printSn)) {
- return false;
- }
- $order->printNum += 1;
- $order->save();
- $customId = $order->customId ?? 0;
- $custom = CustomClass::getById($customId);
- $debtAmount = $custom['debtAmount'] ?? 0;
- $respond = self::getPrintData($order, $debtAmount);
- $fromType = $order->fromType ?? dict::getDict('fromType', 'shop');
- $fromTypeMap = dict::getDict('fromTypeMap');
- $fromTypeName = $fromTypeMap[$fromType] ?? '门店';
- //客服下的单则显示客服号和客户名称
- if ($fromType == dict::getDict('fromType', 'friend')) {
- $staffName = $order->shopAdminName ?? '';
- $bookName = $order->bookName ?? '';
- $fromTypeName = $staffName . ' ' . $bookName;
- }
- $content = '';
- //$content .= "<CB>【零售】</CB><BR>";
- if ($fromType == dict::getDict('fromType', 'mt')) {
- $content .= '<B>美团 ' . $order->thirdSn . '</B><BR>';
- if (isset($order->goodsNum) && $order->goodsNum > 1) {
- $content .= '<B>美团 ' . $order->thirdSn . ' (' . $order->goodsNum . '份)</B><BR>';
- }
- $content .= '--------------------------------<BR>';
- $content .= date("Y-m-d", strtotime($order->addTime)) . '<BR>';
- } else {
- $content .= '<CB>' . $fromTypeName . ' ' . $respond['sendNum'] . '</CB><BR>';
- $content .= '--------------------------------<BR>';
- }
- $readPeriod = $order->reachPeriod ?? 0;
- if ($order->sendType == 1) {
- if (isset($order->reachDate) && !empty($order->reachDate) && $order->reachDate != '0000-00-00') {
- $currentReachDate = date("m-d", strtotime($order->reachDate));
- $content .= '<B>' . $currentReachDate . ' ' . $readPeriod . '前</B><BR>';
- $content .= '<B>到店自取</B><BR><BR>';
- } else {
- $content .= '<B>到店自取</B><BR><BR>';
- }
- } else {
- if ($order->sendType == 0) {
- $content .= '<B>送货上门</B><BR><BR>';
- } else if ($order->sendType == 2) {
- $content .= '<B>跑腿到付</B><BR><BR>';
- } else if ($order->sendType == 3) {
- $content .= '<B>物流到付</B><BR><BR>';
- } else if ($order->sendType == 4) {
- $content .= '<B>快递到付</B><BR><BR>';
- } else {
- $content .= '';
- }
- if (isset($order->reachDate) && !empty($order->reachDate) && $order->reachDate != '0000-00-00') {
- $currentReachDate = date("m-d", strtotime($order->reachDate));
- $content .= '<B>' . $currentReachDate . ' ' . $readPeriod . '前</B><BR><BR>';
- }
- }
- if (isset($order->receiveUserName) && !empty($order->receiveUserName)) {
- $content .= '收花人:<BR>';
- $content .= '<B>' . $order->receiveUserName . '</B><BR>';
- }
- if (isset($order->receiveMobile) && !empty($order->receiveMobile)) {
- $content .= '<B>' . $order->receiveMobile . '</B><BR><BR>';
- }
- if (isset($order->fullAddress) && !empty($order->fullAddress)) {
- $content .= '<B>' . $order->fullAddress . '(' . $order->showAddress . ')' . '</B><BR><BR>';
- }
- if (isset($order->cardInfo) && !empty($order->cardInfo)) {
- $content .= '<B>贺卡内容:' . $order->cardInfo . '</B><BR><BR>';
- }
- if ($order->anonymity == 1) {
- $content .= '<B>匿名派送!!</B><BR><BR>';
- } else {
- if (isset($order->bookMobile) && !empty($order->bookMobile)) {
- $bookName = $order->bookName ?? '';
- $bookMobile = $order->bookMobile ?? '';
- $content .= '订花人:<BR>';
- if (!empty($bookName)) {
- $content .= '<B>' . $bookName . '</B><BR>';
- }
- if (!empty($bookMobile)) {
- $content .= '<B>' . $bookMobile . '</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($order->goodsPrice) && $order->goodsPrice > 0) {
- $content .= '商品合计:' . floatval($order->goodsPrice) . '<BR>';
- }
- if (isset($respond['sendCost']) && $respond['sendCost'] > 0 && $order->sendType == 2) {
- $content .= '运费:' . floatval($respond['sendCost']) . '<BR>';
- }
- if (isset($order->serviceFee) && $order->serviceFee > 0) {
- $content .= '手续费:' . floatval($order->serviceFee) . '<BR>';
- }
- if (isset($order->labourCost) && $order->labourCost > 0) {
- $content .= '包装费:' . floatval($order->labourCost) . '<BR>';
- }
- if (isset($order->prePrice) && $order->prePrice > 0) {
- $content .= '总计:' . floatval($order->prePrice) . '<BR>';
- }
- if (isset($respond['discountAmount']) && !empty($respond['discountAmount']) && $respond['discountAmount'] > 0) {
- $content .= '优惠扣除:-' . floatval($respond['discountAmount']) . '<BR>';
- }
- $content .= '订单金额:' . floatval($respond['orderPrice']) . '<BR>';
- if (isset($respond['refundPrice']) && $respond['refundPrice'] > 0) {
- $content .= '退款金额:' . floatval($respond['refundPrice']) . '<BR>';
- }
- if (isset($respond['debt']) && $respond['debt'] == 1) {
- $content .= '实付金额:0<BR>';
- } else {
- $content .= '实付金额:' . floatval($respond['realPrice']) . '<BR>';
- }
- if (isset($order->remainDebtPrice) && $order->remainDebtPrice > 0) {
- $content .= '<B>待付尾款:' . floatval($order->remainDebtPrice) . '</B><BR>';
- }
- $shop = ShopClass::getById($shopId, true);
- $sjId = $shop->sjId ?? 0;
- $sj = SjClass::getById($sjId, true);
- $sjName = $sj->name ?? '';
- $shopName = $shop->shopName ?? '';
- $currentName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
- $content .= '--------------------------------<BR>';
- $content .= '订单日期:' . $order->addTime . '<BR>';
- $content .= '订单编号:' . $respond['orderSn'] . '<BR>';
- $content .= '门店名称:' . $currentName . '<BR>';
- $content .= "<BR>";
- $p = new printUtil($printSn);
- $orderSn = $order->orderSn ?? '';
- $workList = WorkClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($workList)) {
- $content .= "<BR>";
- $content .= '--------------------------------<BR>';
- foreach ($workList as $work) {
- $goodsSn = $work->goodsSn ?? '';
- $sn = $work->sn ?? '';
- $content .= '<B>制作号:' . $sn . '<B>';
- // 1-523 有-特殊符号,使用飞鹅自带的函数处理
- $content .= $p->bar_code($goodsSn);
- $content .= "<BR>";
- }
- }
- $p->printMsg($content);
- }
- //打印数据 ssh 20210702
- public static function getPrintData($order, $debtAmount)
- {
- $orderSn = $order->orderSn ?? '';
- $printProduct = [];
- $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $itemKey => $itemVal) {
- $unitName = $itemVal['unitName'] ?? '';
- $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
- $printProduct[] = [
- 'name' => $itemVal['name'] ?? '',
- 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
- 'price' => floatval($itemVal['price']),
- ];
- }
- }
- $orderGoodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- if (!empty($orderGoodsList)) {
- foreach ($orderGoodsList as $itemKey => $itemVal) {
- $unitName = $itemVal['unitName'] ?? '份';
- $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
- $printProduct[] = [
- 'name' => $itemVal['name'] ?? '',
- 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
- 'price' => floatval($itemVal['price']),
- ];
- }
- }
- $sendNum = $order['sendNum'] ?? '';
- //退款记录
- $refundList = [];
- $printData = [
- 'address' => '',
- 'sendCost' => $order->sendCost ?? '0.00',
- 'prePrice' => $order->prePrice ?? '0.00',
- 'orderPrice' => $order->orderPrice ?? '0',
- 'discountAmount' => $order->discountAmount ?? '0.00',
- 'realPrice' => $order->realPrice ?? '0.00',
- 'refundPrice' => $order->refundPrice ?? '0.00',
- 'debt' => $order->debt ?? 0,
- 'orderSn' => $order->orderSn ?? '',
- 'date' => $order->addTime ?? '',
- 'remark' => $order->remark ?? '',
- 'custom' => [
- 'customName' => $order->customName ?? '',
- 'customMobile' => $order->customMobile ?? '',
- 'fullAddress' => $order->fullAddress ?? '',
- ],
- 'product' => $printProduct,
- 'refund' => $refundList,
- 'sendNum' => $sendNum,
- 'debtAmount' => $debtAmount,
- ];
- return $printData;
- }
- 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;
- }
- }
|