| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347 |
- <?php
- namespace bizHd\order\classes;
- use biz\goods\classes\GoodsClass;
- use biz\goods\models\Goods;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopClass;
- use biz\shop\classes\ShopExtClass;
- use biz\stat\classes\StatOrderCountClass;
- use biz\stock\classes\GoodsStockRecordClass;
- use bizGhs\order\classes\CheckOrderClass;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\stock\classes\StockRecordClass;
- use bizHd\custom\classes\CustomClass;
- use bizHd\custom\classes\HdClass;
- use bizHd\hb\classes\HbClass;
- use bizHd\item\classes\ItemClass;
- use bizHd\merchant\classes\SjClass;
- use bizHd\refund\classes\HdRefundClass;
- use bizHd\shop\classes\MainClass;
- use bizHd\shop\classes\ShopMoneyChangeClass;
- use bizHd\user\classes\UserClass;
- use bizHd\work\classes\WorkClass;
- 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\stringUtil;
- 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 (empty($data['fullAddress'])) {
- $data['fullAddress'] = $address . $floor;
- }
- $sendType = $data['sendType'] ?? 0;
- if ($sendType == 1) {
- //到店自取,客户自己下单,不会有到店自取时间,但是如果是花店后台接单,会填到店自取时间,重要!!!!!!!!阿东那边已经出问题了!!!
- if (empty($data['reachDate'])) {
- $data['reachDate'] = date("Y-m-d");
- }
- if (empty($data['reachPeriod'])) {
- $data['reachPeriod'] = '22:00';
- }
- } else {
- //如果不是到店自取的,则必须要有配送时间,默认当天
- $data['reachDate'] = !empty($data['reachDate']) ? $data['reachDate'] : date("Y-m-d");
- if (empty($data['reachPeriod'])) {
- util::fail('请选择配送时间');
- }
- }
- $data['reachTime'] = strtotime($data['reachDate'] . ' ' . $data['reachPeriod']);
- $book = $data['book'] ?? 0;
- if ($book == 0) {
- $actPrice = $data['actPrice'];
- $meetNum = $shop->meetNum ?? 0;
- $meetAmount = $shop->meetAmount ?? 0;
- $cutOption = $shop->cutAmount ?? 0;
- $cutStyle = $shop->cutStyle ?? 0;
- if ($cutStyle == 0) {
- $cutAmount = $cutOption;
- } else {
- $miniOption = bcsub(1, $cutOption, 2);
- $cutAmount = bcmul($actPrice, $miniOption, 2);
- }
- if ($cutAmount > 0 && $meetNum >= 0 && $meetAmount > 0) {
- if (isset($data['bigNum']) && $data['bigNum'] >= $meetNum && $data['actPrice'] >= $meetAmount) {
- if ($cutAmount < $actPrice) {
- $actPrice = bcsub($actPrice, $cutAmount, 2);
- $data['orderReachDiscountPrice'] = $cutAmount;
- $data['actPrice'] = $actPrice;
- $data['realPrice'] = $actPrice;
- $data['mainPay'] = $actPrice;
- } else {
- noticeUtil::push('订单金额不足满减金额,customId:' . $data['customId'], '15280215347');
- }
- }
- }
- }
- //将花店每月的总订单数作为编号
- $riseNum = StatOrderCountClass::addOrder($shop, $main);
- $data['sendNum'] = date("j") . $riseNum;
- $data['totalFlow'] = OrderClass::ORDER_TOTAL_FLOW;
- $salt = stringUtil::charsShuffleLowerCase(10);
- $data['salt'] = $salt;
- 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();
- $params = [];
- self::payAfter($order, $payWay, $params);
- }
- //订单完成 ssh 2021.4.20
- public static function payAfter($order, $payWay, $params = [])
- {
- 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;
- $order->status = self::ORDER_STATUS_UN_SEND; // 付款后,订单状态设置为:已付款(待配送)
- $order->payWay = $payWay;
- $order->payStatus = 1;
- if (!empty($params['historyDate'])) {
- $order->payTime = $params['historyDate'] . ' ' . date("H:i:s");
- } else {
- $order->payTime = date("Y-m-d H:i:s");
- }
- $order->stockChange = 1;
- $order->save();
- $myCustom = CustomClass::getLockById($customId);
- if (!empty($myCustom)) {
- $customName = $myCustom->name ?? $order->customName ?? '';
- $orderSnText = $order->orderSn ?? '';
- $shopAdminId = $order->shopAdminId ?? 0;
- $shopAdminName = $order->shopAdminName ?? '';
- $eventPrefix = $customName;
- if ($eventPrefix === '' && $orderSnText !== '') {
- $eventPrefix = '客户';
- }
- if ($order->forward == 0) { //0 常规订单
- $myCustom->recentExpend = date("Y-m-d H:i:s");
- $myCustom->visitTime = date("Y-m-d H:i:s");
- if ($payWay == dict::getDict('payWay', 'balancePay')) {
- $myCustom->recordGrowthAndIntegral = false;
- }
- $myCustom->buyAmount = bcadd($myCustom->buyAmount, $order->realPrice, 2);
- $myCustom->eventRemark = trim($eventPrefix . '下单' . $orderSnText);
- } elseif ($order->forward == 1) { //1 售后付款单(红冲)
- $myCustom->buyAmount = bcsub($myCustom->buyAmount, $order->realPrice, 2);
- $myCustom->eventRemark = trim($eventPrefix . '红冲' . $orderSnText);
- } else {
- util::fail('订单类型不存在');
- }
- $myCustom->relateId = $orderId;
- $myCustom->relateType = 1; //关联的表或类别:0.无关联 1.零售订单
- $myCustom->staffId = $shopAdminId;
- $myCustom->staffName = $shopAdminName;
- $myCustom->mainId = $order->mainId;
- $myCustom->save();
- $hd = HdClass::getLockById($myCustom->hdId);
- //$hd = HdClass::getByCondition(['shopId'=>$order->shopId, 'userId'=>$order->userId], true, false, 'id,customId,expendAmount');
- if (!empty($hd)) {
- if ($order->forward == 0) { //0 常规订单
- $hd->expendAmount = bcadd($hd->expendAmount, $order->realPrice, 2);
- } elseif ($order->forward == 1) { //1 售后付款单(红冲)
- $hd->expendAmount = bcsub($hd->expendAmount, $order->realPrice, 2);
- } else {
- util::fail('订单类型不存在');
- }
- $hd->save();
- }
- }
- $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::skCgDebtAmountAdd($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, $staff = null)
- {
- $now = time();
- if ($order->deadline > $now && $force == false) {
- return false;
- }
- if ($order->status != 1) {
- return false;
- }
- // 红包退回
- HbClass::hbBack($order);
- $order->status = 5;
- $order->cancelTime = date("Y-m-d H:i:s");
- $order->save();
- $orderSn = $order->orderSn ?? '';
- $relateName = $staff->name ?? '系统';
- $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'] = $relateName;
- StockRecordClass::addSellStockOrderCancelRecord($recordData);
- }
- }
- $goodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($goodsList)) {
- foreach ($goodsList as $goods) {
- $goodsId = $goods->goodsId ?? 0;
- $num = $goods->num ?? 0;
- $mainId = $goods->mainId ?? 0;
- $orderSn = $goods->orderSn ?? '';
- $shopId = $goods->shopId ?? 0;
- $sjId = $goods->sjId ?? 0;
- $params = ['reduceSale' => 1];
- $respond = \bizHd\goods\classes\GoodsClass::addStock($goodsId, $mainId, $num, $params);
- $oldStock = $respond['oldStock'] ?? 0;
- $newStock = $respond['newStock'] ?? 0;
- $data = [];
- $data['sjId'] = $sjId;
- $data['shopId'] = $shopId;
- $data['mainId'] = $mainId;
- $data['orderSn'] = $orderSn;
- $data['goodsId'] = $goodsId;
- $data['goodsNum'] = $num;
- $data['oldStock'] = $oldStock;
- $data['newStock'] = $newStock;
- $data['relateName'] = $relateName;
- GoodsStockRecordClass::cancelOrder($data);
- }
- }
- $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');
- }
- }
- //清理限购缓存
- $customId = $order->customId ?? 0;
- if (!empty($customId) && !empty($itemList)) {
- foreach ($itemList as $item) {
- $productId = $item['itemId'];
- $num = floor($item['num']);
- if (!empty($productId) && $num > 0) {
- \bizHd\product\classes\ProductClass::baseClearLimitBuy($productId, $customId, $num);
- }
- }
- }
- }
- // 恢复订单 -- 即:把订单的状态从取消状态变成待付款状态
- 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;
- $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;
- $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>' . $currentName . '</CB><BR>';
- $content .= '<CB>销售单</CB><BR><BR>';
- $content .= '<CB>' . $fromTypeName . ' ' . $respond['sendNum'] . '</CB><BR>';
- $content .= '--------------------------------<BR>';
- }
- $readPeriod = $order->reachPeriod ?? 0;
- if ($order->sendType == 1) {
- if (!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 (!empty($order->reachDate) && $order->reachDate != '0000-00-00') {
- $currentReachDate = date("m-d", strtotime($order->reachDate));
- $content .= '<B>' . $currentReachDate . ' ' . $readPeriod . '前</B><BR><BR>';
- }
- }
- $defaultCustomId = $shop->defaultCustomId ?? 0;
- $fastOrder = $defaultCustomId == $customId ? 1 : 0;
- //快捷开单不需要显示这些东西
- if ($fastOrder == 0) {
- if (!empty($order->receiveUserName)) {
- $content .= '收花人:<BR>';
- $content .= '<B>' . $order->receiveUserName . '</B><BR>';
- }
- if (!empty($order->receiveMobile)) {
- $content .= '<B>' . $order->receiveMobile . '</B><BR><BR>';
- }
- if (!empty($order->fullAddress)) {
- $content .= '<B>' . $order->fullAddress . '(' . $order->showAddress . ')' . '</B><BR><BR>';
- }
- if (!empty($order->cardInfo)) {
- $content .= '<B>贺卡内容:' . $order->cardInfo . '</B><BR><BR>';
- }
- if ($order->anonymity == 1) {
- $content .= '<B>匿名派送!!</B><BR><BR>';
- } else {
- if (!empty($order->bookMobile)) {
- $bookName = $order->bookName ?? '';
- $bookMobile = $order->bookMobile;
- $content .= '订花人:<BR>';
- if (!empty($bookName)) {
- $content .= '<B>' . $bookName . '</B><BR>';
- }
- $content .= '<B>' . $bookMobile . '</B><BR>';
- }
- }
- }
- if (!empty($respond['remark'])) {
- $content .= '<BR>';
- $content .= '备注:<BR>';
- $content .= '<B>' . $respond['remark'] . '</B><BR>';
- }
- $content .= '<BR>';
- $content .= '商品 数量/单价 金额 <BR>';
- $content .= '--------------------------------<BR>';
- if (!empty($respond['product'])) {
- foreach ($respond['product'] as $current) {
- $content = self::printGroup($current, $content);
- }
- $content .= '--------------------------------<BR>';
- }
- if (!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 (!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>';
- }
- $content .= '--------------------------------<BR>';
- $balance = $custom['balance'] ?? 0;
- $balance = floatval($balance);
- if ($balance > 0) {
- $content .= '当前账户余额:' . $balance . '元<BR>';
- }
- $content .= '订单日期:' . $order->addTime . '<BR>';
- $content .= '订单编号:' . $respond['orderSn'] . '<BR>';
- $content .= '门店名称:' . $currentName . '<BR>';
- $content .= "<BR>";
- //noticeUtil::push($content, '15280215347');
- $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']),
- 'sn' => '',
- ];
- }
- }
- $orderGoodsList = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- if (!empty($orderGoodsList)) {
- foreach ($orderGoodsList as $itemKey => $itemVal) {
- $unitName = $itemVal['unitName'] ?? '份';
- $unitPrice = $itemVal['unitPrice'] ? floatval($itemVal['unitPrice']) : 0;
- $sn = $itemVal['sn'] ?? '';
- $printProduct[] = [
- 'name' => $itemVal['name'] ?? '',
- 'num' => $itemVal['num'] . $unitName . '*' . $unitPrice,
- 'price' => floatval($itemVal['price']),
- 'sn' => $sn,
- ];
- }
- }
- $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'] ?? '';
- if (!empty($current['sn'])) {
- $name = '#' . $current['sn'] . ' ' . $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><BR>';
- return $content;
- }
- //扫码收款的,减少库存 ssh 20250822
- public static function reduceMyStock($data, $shop, $custom)
- {
- $mainId = $shop->mainId;
- $customId = $custom->id;
- $orderId = $data['orderId'] ?? '';
- $sjId = $shop->sjId;
- $hdId = $custom->hdId;
- $shopId = $shop->id;
- $order = self::getById($orderId, true);
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- if ($order->mainId != $shop->mainId) {
- util::fail('并不是你的订单');
- }
- if ($order->repeat == 0) {
- util::fail('已经扣过了');
- }
- if ($order->tkPrice > 0) {
- util::fail('已经发生退款,无法操作');
- }
- if ($order->customId != $customId) {
- util::fail('订单对应客户有问题,无法扣库存,编号:' . $customId . ' ' . $order->customId);
- }
- $orderSn = $order->orderSn ?? '';
- $product = $data['product'] ?? [];
- if (empty($product)) {
- util::fail('请选择商品呢');
- }
- $orderItemList = OrderItemClass::getAllbyCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $orderItem->unitPrice = 0;
- $orderItem->save();
- }
- }
- $actPrice = $order->actPrice ?? 0;
- if ($actPrice <= 0) {
- util::fail('订单金额为0');
- }
- $ids = array_column($product, 'productId');
- $productInfo = ItemClass::getByIds($ids, null, 'id');
- $goodsPrice = 0;
- foreach ($product as $key => $val) {
- $property = $val['property'] ?? 0;
- if ($property != 1) {
- util::fail('暂时只支持花材');
- }
- $unitPrice = $val['unitPrice'] ?? 0;
- $unitType = $val['unitType'] ?? 0;
- $currentId = $val['productId'] ?? 0;
- $num = $val['num'] ?? 0;
- $currentInfo = $productInfo[$currentId] ?? [];
- if (empty($currentInfo)) {
- util::fail('存在无效花材,编号:' . $currentId);
- }
- $name = $currentInfo['name'] ?? '';
- $cover = $currentInfo['cover'] ?? '';
- $ratio = $currentInfo['ratio'] ?? 20;
- $itemMainId = $currentInfo['mainId'] ?? 0;
- if ($itemMainId != $mainId) {
- util::fail('不是您的花材,请勿使用,编号:' . $mainId . ' ' . $itemMainId);
- }
- if ($unitType == 0) {
- $unitName = $currentInfo['bigUnit'] ?? '';
- $unitId = $currentInfo['bigUnitId'] ?? 0;
- $totalNum = $num;
- } else {
- $unitName = $currentInfo['smallUnit'] ?? '';
- $unitId = $currentInfo['smallUnitId'] ?? 0;
- $totalNum = bcdiv($num, $ratio, 2);
- }
- $ptItemId = $currentInfo['itemId'] ?? 0;
- $classId = $currentInfo['classId'] ?? 0;
- $belongCost = $currentInfo['belongCost'] ?? 0;
- $currentPrice = bcmul($unitPrice, $num, 2);
- $goodsPrice = bcadd($goodsPrice, $currentPrice, 2);
- $currentItemData = [
- 'name' => $name,
- 'cover' => $cover,
- 'ratio' => $ratio,
- 'unitType' => $unitType,
- 'unitName' => $unitName,
- 'unitId' => $unitId,
- 'unitPrice' => $unitPrice,
- 'num' => $num,
- 'orderSn' => $orderSn,
- 'itemId' => $currentId,
- 'ptItemId' => $ptItemId,
- 'totalNum' => $totalNum,
- 'totalPrice' => 0,
- 'customId' => $customId,
- 'hdId' => $hdId,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'mainId' => $mainId,
- 'classId' => $classId,
- 'price' => $currentPrice,
- 'belongCost' => $belongCost,
- ];
- //库存变化在这里
- OrderItemClass::addData($currentItemData, $custom);
- }
- $labourCost = 0;
- $discountType = 0;
- $discountAmount = 0;
- if ($goodsPrice > $actPrice) {
- $prePrice = $goodsPrice;
- $discountAmount = bcsub($goodsPrice, $actPrice, 2);
- if ($discountAmount > 0) {
- $discountType = 3;
- }
- } else {
- $prePrice = $actPrice;
- $labourCost = bcsub($actPrice, $goodsPrice, 2);
- }
- $order->goodsPrice = $goodsPrice;
- $order->prePrice = $prePrice;
- $order->labourCost = $labourCost;
- $order->discountAmount = $discountAmount;
- $order->discountType = $discountType;
- $order->repeat = 0;
- $order->save();
- return $order;
- }
- public static function debtCount($mainId)
- {
- $debtAmount = 0;
- $orders = self::getAllByCondition(['mainId' => $mainId, 'status' => 4, 'debt' => 1], null, 'remainDebtPrice', null, true);
- foreach ($orders as $order) {
- $remainDebtPrice = $order->remainDebtPrice ?? 0;
- $debtAmount = bcadd($debtAmount, $remainDebtPrice, 2);
- }
- return $debtAmount;
- }
- }
|