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