| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- <?php
- namespace bizGhs\order\classes;
- use biz\ghs\classes\GhsClass;
- use biz\shop\classes\MainClass;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopClass;
- use biz\stat\classes\StatCgClass;
- use biz\stat\classes\StatCgGhsClass;
- use biz\stat\classes\StatOutClass;
- use bizGhs\base\classes\BaseClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\order\traits\OrderTrait;
- use bizGhs\stock\classes\StockRecordClass;
- use bizGhs\stock\services\StockRecordService;
- use bizGhs\product\classes\ProductClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- //采购单 lqh 2021.1.20
- class PurchaseOrderClass extends BaseClass
- {
- use OrderTrait;
- public static $baseFile = '\bizGhs\order\models\PurchaseOrder';
- const PURCHASE_ORDER_STATUS_WAIT = 1; //待确认
- const PURCHASE_ORDER_STATUS_UN_SEND = 2; //待配送
- const PURCHASE_ORDER_STATUS_SENDING = 3; //配送中
- const PURCHASE_ORDER_STATUS_COMPLETE = 4; //已完成
- public static $statusMap = [
- self::PURCHASE_ORDER_STATUS_WAIT => '待确认',
- self::PURCHASE_ORDER_STATUS_UN_SEND => '待配送',
- self::PURCHASE_ORDER_STATUS_SENDING => '配送中',
- self::PURCHASE_ORDER_STATUS_COMPLETE => '已完成',
- ];
- const DEBT_UNKNOWN = 0;
- const DEBT_YES = 1;
- const DEBT_NO = 2;
- //寄付与到付
- const YF_PAY_WAY_JF = 1;
- const YF_PAY_WAY_DF = 2;
- //添加采购单 lqh 2021.1.19
- public static function addOrder($data)
- {
- $sjId = $data['sjId'] ?? 0;
- $shopId = $data['shopId'] ?? 0;
- $mainId = $data['mainId'] ?? 0;
- $ghsName = $data['ghsName'] ?? '';
- $data['status'] = self::PURCHASE_ORDER_STATUS_COMPLETE;
- $orderSn = orderSn::getGhsPurchaseSn();
- $data['orderSn'] = $orderSn;
- //数据结构 [{itemId:0,bigNum:0,smallNum:0,productId:12,itemPrice:1,weight:1}]
- $ghsItemInfo = $data['itemInfo'];
- $ghsItemInfo = self::mergeItemInfo($ghsItemInfo);
- //总共多少扎
- $bigNum = 0;
- //总共多少支
- $smallNum = 0;
- //总数量
- $totalNum = 0;
- $totalItemPrice = 0;
- $totalWeight = 0;
- $ghsItemInfoMap = [];
- $productIds = array_column($ghsItemInfo, 'productId');
- $productIds = array_unique(array_filter($productIds));
- $productList = ProductClass::getByIds($productIds, null, 'id');
- foreach ($ghsItemInfo as $v) {
- if (isset($v['bigNum']) == false || $v['bigNum'] <= 0) {
- util::fail('请填写数量');
- }
- if (isset($v['itemPrice']) == false || $v['itemPrice'] <= 0) {
- util::fail('请填写价格');
- }
- $bigNum += $v['bigNum'] ?? 0;
- $smallNum += $v['smallNum'] ?? 0;
- //限制不允许小单位采购,这样大单位就量总数量 ssh 20211021
- if (isset($v['smallNum']) && $v['smallNum'] > 0) {
- util::fail('采购最小单位必须扎');
- }
- $currentBigNum = $v['bigNum'] ?? 0;
- $totalNum = bcadd($totalNum, $currentBigNum, 2);
- $currentTotalPrice = bcmul($v['bigNum'], $v['itemPrice'], 2);
- $totalItemPrice = bcadd($totalItemPrice, $currentTotalPrice, 2);
- $ghsItemInfoMap[$v['productId']] = $v;
- $currentProductId = $v['productId'] ?? 0;
- $currentWeight = $productList[$currentProductId]['weight'] ?? 0;
- $weight = bcmul($currentWeight, $v['bigNum'], 2);
- $totalWeight = bcadd($totalWeight, $weight, 2);
- }
- $data['bigNum'] = $bigNum;
- $data['itemNum'] = $totalNum;
- $data['smallNum'] = $smallNum;
- $data['price'] = $totalItemPrice;
- $data['totalWeight'] = $totalWeight;
- //计算订单总价
- $data['packingCharge'] = isset($data['packingCharge']) && $data['packingCharge'] > 0 ? $data['packingCharge'] : 0;
- $data['shortCharge'] = isset($data['shortCharge']) && $data['shortCharge'] > 0 ? $data['shortCharge'] : 0;
- $data['longCharge'] = isset($data['longCharge']) && $data['longCharge'] > 0 ? $data['longCharge'] : 0;
- $data['pickCharge'] = isset($data['pickCharge']) && $data['pickCharge'] > 0 ? $data['pickCharge'] : 0;
- $data['localCharge'] = isset($data['localCharge']) && $data['localCharge'] > 0 ? $data['localCharge'] : 0;
- $cgModel = $data['cgModel'] ?? 0;
- if ($cgModel == 1) {
- //简约采购模式
- $customAvgKgWeight = $data['avgKgWeight'] ?? 0;
- $data['longCharge'] = bcmul($totalWeight, $customAvgKgWeight, 2);
- unset($data['modifyPrice']);
- } else {
- //正常采购模式不要使用传过来的每公斤运费
- unset($data['avgKgWeight']);
- }
- if (empty($data['entryTime'])) {
- $data['entryTime'] = date('Y-m-d H:i:s');
- }
- $packingCharge = $data['packingCharge'] ?? 0;
- $shortCharge = $data['shortCharge'] ?? 0;
- $longCharge = $data['longCharge'] ?? 0;
- $pickCharge = $data['pickCharge'] ?? 0;
- $localCharge = $data['localCharge'] ?? 0;
- //提货费和本地运费
- $paidPrice = bcadd($localCharge, $pickCharge, 2);
- $yfPayWay = $data['yfPayWay'] ?? self::YF_PAY_WAY_JF;
- //打包费
- $currentPrice = bcadd($totalItemPrice, $packingCharge, 2);
- //寄付才算长途和短途运费
- if ($yfPayWay == self::YF_PAY_WAY_JF) {
- $currentPrice = bcadd($currentPrice, $shortCharge, 2);
- $currentPrice = bcadd($currentPrice, $longCharge, 2);
- } elseif ($yfPayWay == self::YF_PAY_WAY_DF) {
- $paidPrice = bcadd($paidPrice, $shortCharge, 2);
- $paidPrice = bcadd($paidPrice, $longCharge, 2);
- } else {
- util::fail('没有找到这个运费支付方式');
- }
- $data['prePrice'] = $currentPrice;
- if (isset($data['modifyPrice']) && !empty($data['modifyPrice']) && $data['modifyPrice'] > 0) {
- //报损减免后金额
- $modifyPrice = $data['modifyPrice'] ?? 0;
- if ($modifyPrice > $currentPrice) {
- util::fail("应付金额大于总金额,应付金额:{$modifyPrice},总金额:{$currentPrice}");
- }
- if ($modifyPrice < $currentPrice) {
- $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
- $data['discountType'] = dict::getDict('discountType', 'discount');
- $currentPrice = $modifyPrice;
- }
- }
- $data['actPrice'] = $currentPrice;
- $data['realPrice'] = $currentPrice;
- $data['paidPrice'] = $paidPrice;
- //总运费
- $totalFreight = bcadd($packingCharge, $shortCharge, 2);
- $totalFreight = bcadd($totalFreight, $longCharge, 2);
- $totalFreight = bcadd($totalFreight, $pickCharge, 2);
- $totalFreight = bcadd($totalFreight, $localCharge, 2);
- $data['totalFreight'] = $totalFreight;
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- //总运费/总公斤数,计算每公斤的运费
- $avgKgWeight = bcdiv($totalFreight, $totalWeight, 2);
- $data['avgKgWeight'] = $avgKgWeight;
- $data['cgStyle'] = dict::getDict('cgStyle', 'ghs');
- $order = self::add($data, true);
- //组装详情表数据
- $batchData = self::groupOrderItem($ghsItemInfo, $orderSn);
- //加库存
- foreach ($batchData as $currentKey => $v) {
- $itemId = $v['itemId'];
- $productId = $v['productId'];
- //流水记录
- $record = [];
- $record['sjId'] = $sjId;
- $record['shopId'] = $shopId;
- $record['mainId'] = $mainId;
- $record['orderSn'] = $orderSn;
- $record['productId'] = $productId;
- $record['itemId'] = $itemId;
- $record['relateName'] = $ghsName;
- $record['itemNum'] = $v['itemNum'];
- $record['oldStock'] = $v['itemStock'];
- $record['newStock'] = bcadd($v['itemStock'], $v['itemNum'], 2);
- StockRecordClass::addPurchaseOrderRecord($record);
- //加库存
- ProductClass::addStockByItemNum($productId, $v['itemNum']);
- //单位采购价
- $unitCgPrice = $v['bigPrice'] ?? 0;
- $productWeight = $productList[$productId]['weight'] ?? 0;
- $unitFreight = bcmul($avgKgWeight, $productWeight, 2);
- $unitCost = bcadd($unitCgPrice, $unitFreight, 2);
- //四舍五入取整数
- $unitCost = round($unitCost);
- $batchData[$currentKey]['bigFreight'] = $unitFreight;
- $currentCover = $productList[$productId]['cover'] ?? '';
- $currentName = $productList[$productId]['name'] ?? '';
- $batchData[$currentKey]['cover'] = $currentCover;
- $batchData[$currentKey]['name'] = $currentName;
- $currentBigUnit = $productList[$productId]['bigUnit'] ?? '';
- $currentSmallUnit = $productList[$productId]['smallUnit'] ?? '';
- $currentRatio = $productList[$productId]['ratio'] ?? 0;
- $batchData[$currentKey]['bigUnit'] = $currentBigUnit;
- $batchData[$currentKey]['smallUnit'] = $currentSmallUnit;
- $batchData[$currentKey]['ratio'] = $currentRatio;
- $product = ProductClass::getLockById($productId);
- $product->cost = $unitCost;
- $product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
- $product->save();
- //其它门店的成本价也要一起改掉
- $allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
- if (!empty($allShop)) {
- $currentItemId = $product->itemId ?? 0;
- foreach ($allShop as $currentShop) {
- $currentId = $currentShop->id ?? 0;
- if ($currentId == $shopId) {
- continue;
- }
- ProductClass::updateByCondition(['shopId' => $currentId, 'itemId' => $currentItemId], ['cost' => $unitCost]);
- }
- }
- }
- //写入详情表
- PurchaseOrderItemClass::batchAddOrderItem($batchData);
- //门店支出增加
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有资产信息');
- }
- //支出增加
- $currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
- $main->totalExpend = $currentTotalExpend;
- $main->save();
- //采购增加
- $main->cgFinish += 1;
- $main->totalPurchaseOrder += 1;
- $currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
- $main->totalPurchase = $currentTotalPurchase;
- $main->save();
- //供货商资产增加
- $ghsId = $data['ghsId'] ?? 0;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- $ghs->debt = GhsClass::DEBT_YES;
- $ghs->debtAmount = bcadd($ghs->debtAmount, $currentPrice, 2);
- $ghs->debtNum += 1;
- $ghs->expendAmount = bcadd($ghs->expendAmount, $currentPrice, 2);
- $ghs->expendNum += 1;
- $ghs->save();
- //采购统计
- $cgNum = $order->itemNum ?? 0;
- StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
- //采购按供货商统计
- StatCgGhsClass::ghsReplace($order);
- //门店应收客户款增加
- $ghsShopId = $ghs->shopId;
- $ghsShop = ShopClass::getLockById($ghsShopId);
- if (empty($ghsShop)) {
- util::fail('没有找到门店');
- }
- $ghsMainId = $ghsShop->mainId ?? 0;
- $ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
- if (empty($ghsMain)) {
- util::fail('没有main信息');
- }
- $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
- $ghsMain->mayGathering = $currentMayGathering;
- //客户资产增加
- $customId = $ghs->customId;
- $custom = CustomClass::getLockById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户信息');
- }
- $custom->isDebt = CustomClass::IS_DEBT_YES;
- $custom->debtAmount = bcadd($custom->debtAmount, $currentPrice, 2);
- if ($custom->debtNum == 0) {
- $ghsMain->mayGatheringNum += 1;
- }
- $ghsMain->save();
- $custom->debtNum += 1;
- $custom->buyNum += 1;
- $custom->buyAmount = bcadd($custom->buyAmount, $currentPrice, 2);
- $custom->save();
- //当天和当月支出统计
- StatOutClass::updateOrInsert($main, $shop, $currentPrice);
- //支出流水
- $payWay = dict::getDict('payWay', 'unknown');
- $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
- $sjId = $order->sjId ?? 0;
- $shopId = $order->shopId ?? 0;
- $event = '采购';
- $mainId = $shop->mainId ?? 0;
- $capitalData = [
- 'capitalType' => $capitalType,
- 'io' => 0,
- 'totalExpend' => $currentTotalExpend,
- 'payWay' => $payWay,
- 'amount' => $currentPrice,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- 'mainId' => $mainId
- ];
- ShopCapitalClass::addCapital($capitalData);
- $transaction->commit();
- return $order;
- } catch (\Exception $exception) {
- $transaction->rollBack();
- util::fail('保存失败:' . $exception->getMessage());
- }
- return false;
- }
- //采购单列表 lqh 2021.1.19
- public static function getOrderList($where)
- {
- $data = self::getList('*', $where, 'addTime DESC');
- $list = $data['list'] ?? [];
- $data['list'] = self::groupSupplierList($list);
- $data['list'] = self::groupAdminList($data['list']);
- $data['list'] = self::groupStatusName($data['list']);
- return $data;
- }
- //获取订单详情信息 lqh 2021.1.20
- public static function getOrderDetail($orderSn, $shopId)
- {
- $where = ['orderSn' => $orderSn];
- $orderData = self::getByCondition($where);
- if (empty($orderData)) {
- util::fail('订单不存在');
- }
- if (isset($orderData['shopId']) == false || $orderData['shopId'] != $shopId) {
- util::fail('没有权限访问');
- }
- $orderInfo = PurchaseOrderItemClass::getOrderItemDetail($orderSn);
- if ($orderInfo) {
- foreach ($orderInfo as $key => $val) {
- $shortCover = $val['cover'] ?? '';
- $orderInfo[$key]['ShortCover'] = $shortCover;
- $orderInfo[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $orderInfo[$key]['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
- }
- }
- $orderData = self::groupAdmin($orderData);
- //供货商信息
- $ghsId = $orderData['ghsId'] ?? 0;
- $ghsInfo = GhsClass::getGhsInfo($ghsId);
- $orderData['supplierName'] = $ghsInfo['name'] ?? '';
- $orderData['supplierMobile'] = $ghsInfo['mobile'] ?? '';
- $orderData['supplierAddress'] = $ghsInfo['address'] ?? '';
- $orderData['debtAmount'] = $ghsInfo['debtAmount'] ?? 0;
- $orderData['itemInfo'] = $orderInfo;
- $orderData['statusName'] = self::getStatusName($orderData['status']);
- return $orderData;
- }
- // 组装 orderItem lqh 2021.1.25
- public static function groupOrderItem($ghsItemInfo, $orderSn)
- {
- $productData = self::getProductMapData($ghsItemInfo);
- $batchData = [];
- foreach ($ghsItemInfo as $v) {
- $tmp = [];
- $productId = $v['productId'];
- $itemId = $productData[$productId]['itemId'];
- $tmp['orderSn'] = $orderSn;
- $tmp['itemId'] = $itemId;
- $tmp['productId'] = $v['productId'];
- $tmp['itemStock'] = $productData[$productId]['stock'] ?? 0;//当时库存
- $tmp['itemNum'] = $v['bigNum'];
- $tmp['bigPrice'] = $v['itemPrice'];
- $tmp['totalPrice'] = bcmul($v['itemPrice'], $tmp['itemNum'], 2);
- $batchData[] = $tmp;
- }
- return $batchData;
- }
- // 组装状态码 lqh 2021.1.30
- public static function groupStatusName($list)
- {
- foreach ($list as $k => $val) {
- $status = $val['status'] ?? 0;
- $list[$k]['statusName'] = self::getStatusName($status);
- }
- return $list;
- }
- // 获取状态码名称
- public static function getStatusName($status)
- {
- return self::$statusMap[$status] ?? '';
- }
- //获取各个状态下的订单数量 allNum unPayNum unSendNum sendingNum finishNum
- public static function statNum($shopId)
- {
- $data = [
- 'allNum' => 0,
- 'unPayNum' => 0,
- 'unSendNum' => 0,
- 'sendingNum' => 0,
- 'finishNum' => 0,
- ];
- $res = self::getAllList("status", ['shopId' => $shopId]);
- foreach ($res as $v) {
- $data['allNum']++;
- $status = $v['status'];
- switch ($status) {
- case self::PURCHASE_ORDER_STATUS_WAIT:
- //待支付
- $data['unPayNum']++;
- break;
- case self::PURCHASE_ORDER_STATUS_UN_SEND:
- $data['unSendNum']++;
- break;
- case self::PURCHASE_ORDER_STATUS_SENDING:
- $data['sendingNum']++;
- break;
- case self::PURCHASE_ORDER_STATUS_COMPLETE:
- $data['finishNum']++;
- break;
- }
- }
- return $data;
- }
- //采购数,采购金额统计(根据时间,今日,昨天,7天,30天)
- public static function statOrderInfo($shopId, $statType)
- {
- $start = '';
- $end = '';
- switch ($statType) {
- case "today":
- $start = date('Y-m-d');
- $end = $start;
- break;
- case "yesterday" :
- $start = date('Y-m-d', strtotime("-1 day"));
- $end = $start;
- break;
- case "seven":
- $start = date('Y-m-d', strtotime("-6 day"));
- $end = date('Y-m-d');
- break;
- case "thirty":
- $start = date('Y-m-d', strtotime("-29 day"));
- $end = date('Y-m-d');
- break;
- default:
- }
- $where = [];
- $where['shopId'] = $shopId;
- $where['addTime'] = ['between', [$start, $end]];
- $res = self::getAllByCondition($where, null, "id,actPrice");
- $totalAmount = 0;
- $totalNum = count($res);
- foreach ($res as $v) {
- $actPrice = $v['actPrice'] ?? 0;
- $totalAmount = bcadd($totalAmount, $actPrice, 2);
- }
- return [
- 'totalAmount' => $totalAmount,
- 'totalNum' => $totalNum,
- ];
- }
- }
|