| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- <?php
- namespace bizGhs\order\classes;
- use biz\ghs\classes\GhsClass;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopClass;
- use biz\stat\classes\StatCgClass;
- use biz\stat\classes\StatOutClass;
- use bizGhs\base\classes\BaseClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\order\traits\OrderTrait;
- use bizGhs\stock\services\StockRecordService;
- use bizGhs\product\classes\ProductClass;
- use common\components\dict;
- 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;
- //添加采购单 lqh 2021.1.19
- public static function addOrder($data)
- {
- $sjId = $data['sjId'];
- $shopId = $data['shopId'];
- $data['status'] = self::PURCHASE_ORDER_STATUS_COMPLETE;
- $orderSn = orderSn::getGhsPurchaseSn();
- $data['orderSn'] = $orderSn;
- $ghsItemInfo = $data['itemInfo']; // [{itemId:0,bigNum:0,smallNum:0,productId:1.0,itemPrice:1,weight:1}] //增加重量kg
- $ghsItemInfo = self::mergeItemInfo($ghsItemInfo);
- //计算出总共多少扎,多少支
- $bigNum = 0; //总共多少扎
- $smallNum = 0; //总共多少支
- $totalItemPrice = 0;
- //花材中总重量
- $totalItemWeight = 0;
- $ghsItemInfoMap = [];
- if (!empty($ghsItemInfo)) {
- foreach ($ghsItemInfo as $v) {
- $bigNum += $v['bigNum'] ?? 0;
- $smallNum += $v['smallNum'] ?? 0;
- $totalItemPrice = bcadd($totalItemPrice, $v['itemPrice'], 2);
- $ghsItemInfoMap[$v['productId']] = $v;
- if (getenv('YII_ENV', 'local') == 'production') {
- $weight = $v['weight'] ?? 0;
- } else {
- $weight = 0.6;
- }
- $totalItemWeight = bcadd($totalItemWeight, $weight, 2);
- }
- }
- $data['bigNum'] = $bigNum;
- $data['smallNum'] = $smallNum;
- $data['price'] = $totalItemPrice;
- //计算订单总价
- $data['packingCharge'] = empty($data['packingCharge']) ? 0 : $data['packingCharge'];
- $data['shortCharge'] = empty($data['shortCharge']) ? 0 : $data['shortCharge'];
- $data['longCharge'] = empty($data['longCharge']) ? 0 : $data['longCharge'];
- $data['pickCharge'] = empty($data['pickCharge']) ? 0 : $data['pickCharge'];
- $data['localCharge'] = empty($data['localCharge']) ? 0 : $data['localCharge'];
- 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;
- $orderPrice = bcadd($totalItemPrice, $packingCharge, 2);
- $orderPrice = bcadd($orderPrice, $shortCharge, 2);
- $orderPrice = bcadd($orderPrice, $longCharge, 2);
- $orderPrice = bcadd($orderPrice, $pickCharge, 2);
- $orderPrice = bcadd($orderPrice, $localCharge, 2);
- $data['orderPrice'] = $orderPrice;
- $data['prePrice'] = $orderPrice;
- $data['actPrice'] = $orderPrice;
- $data['realPrice'] = $orderPrice;
- //总运费
- $totalFreight = bcadd($packingCharge, $shortCharge, 2);
- $totalFreight = bcadd($totalFreight, $longCharge, 2);
- $totalFreight = bcadd($totalFreight, $pickCharge, 2);
- $totalFreight = bcadd($totalFreight, $localCharge, 2);
- if ($totalFreight <= 0) {
- util::fail('请填写运费');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $order = self::add($data);
- //采购支出
- //组装详情表数据
- $batchData = self::groupOrderItem($ghsItemInfo, $orderSn);
- //写入详情表
- PurchaseOrderItemClass::batchAddOrderItem($batchData);
- //总共多少扎(总扎数)
- $totalItemNum = 0;
- foreach ($batchData as $v) {
- $totalItemNum = bcadd($totalItemNum, $v['itemNum'], 2);
- }
- //平均计算出每扎得运费:总运费/总扎数
- //$avgFreight = bcdiv($totalFreight, $totalItemNum, 2);
- //计算每公斤的运费 总运费/总重量
- $avgKgWeight = bcdiv($totalFreight, $totalItemWeight, 2);
- //现在暂时点击确定,完成 加库存
- foreach ($batchData as $v) {
- $itemId = $v['itemId'];
- $productId = $v['productId'];
- //流水记录
- $record = [];
- $record['sjId'] = $sjId;
- $record['shopId'] = $shopId;
- $record['orderSn'] = $orderSn;
- $record['itemId'] = $itemId;
- $record['itemNum'] = $v['itemNum'];
- $record['oldStock'] = $v['itemStock'];
- $record['newStock'] = bcadd($v['itemStock'], $v['itemNum'], 2);
- StockRecordService::addPurchaseOrderRecord($record);
- //加库存
- ProductClass::addStockByItemNum($productId, $v['itemNum']);
- //
- //每扎的采购价
- //总的采购价/扎数
- $avgItemCost = 0;
- if ($v['itemNum'] > 0) {
- $avgItemCost = bcdiv($v['itemPrice'], $v['itemNum'], 2);
- }
- // 每扎采购价+每扎运费 = 每扎成本价
- //$avgCost = bcadd($avgItemCost, $avgFreight, 2);
- //2021-06-11 修改每扎运费计算
- $itemInfoTmp = json_decode($v['itemInfo'], true);
- //花材的重量
- $productWeight = $itemInfoTmp['itemWeight'] ?? 0;
- $productFreight = bcmul($avgKgWeight, $productWeight, 2);
- $avgCost = bcadd($avgItemCost, $productFreight, 2);
- //修改 ghsItem 中的成本价
- // ItemClass::changeCost($itemId,$sjId,$avgCost);
- //2021.4.6修改product 成本价
- ProductClass::changeCost($productId, $avgCost);
- //触发自动调价 2021.4.26 linqh
- ProductClass::autoPriceById($productId);
- }
- //门店支出增加
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $currentTotalExpend = bcadd($shop->totalExpend, $order['orderPrice'], 2);
- $shop->totalExpend = $currentTotalExpend;
- $shop->cgFinish += 1;
- $shop->totalPurchaseOrder += 1;
- $currentTotalPurchase = bcadd($shop->totalPurchase, $order['orderPrice'], 2);
- $shop->totalPurchase = $currentTotalPurchase;
- $shop->save();
- //采购统计
- StatCgClass::replace($shop, $order['orderPrice']);
- //当天和当月支出统计
- StatOutClass::updateOrInsert($shop, $order['orderPrice']);
- //供应商资产增加
- $ghsId = $data['ghsId'] ?? 0;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供应商');
- }
- $ghs->debt = GhsClass::DEBT_YES;
- $ghs->debtAmount = bcadd($ghs->debtAmount, $order['orderPrice'], 2);
- $ghs->debtNum += 1;
- $ghs->expendAmount = bcadd($ghs->expendAmount, $order['orderPrice'], 2);
- $ghs->expendNum += 1;
- $ghs->save();
- //客户资产增加
- $customId = $ghs->customId;
- $custom = CustomClass::getLockById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户信息');
- }
- $custom->isDebt = CustomClass::IS_DEBT_YES;
- $custom->debtAmount = bcadd($custom->debtAmount, $order['orderPrice'], 2);
- $custom->debtNum += 1;
- $custom->buyNum += 1;
- $custom->buyAmount = bcadd($custom->buyAmount, $order['orderPrice'], 2);
- $custom->save();
- //支出流水
- $payWay = dict::getDict('payWay', 'unknown');
- $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
- $sjId = $order['sjId'] ?? 0;
- $shopId = $order['shopId'] ?? 0;
- $event = '采购';
- $capitalData = [
- 'capitalType' => $capitalType,
- 'io' => 0,
- 'totalExpend' => $currentTotalExpend,
- 'payWay' => $payWay,
- 'amount' => $order['orderPrice'],
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- ];
- 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,
- 'shopId' => $shopId,
- ];
- $orderData = self::getByCondition($where);
- $orderData['ghsInfo'] = json_decode($orderData['ghsInfo']);
- if (!$orderData) {
- util::fail('订单不存在');
- }
- $orderInfo = PurchaseOrderItemClass::getOrderItemDetail($orderSn);
- $itemData = [];
- if ($orderInfo) {
- foreach ($orderInfo as $v) {
- $info = json_decode($v['itemInfo'], true);
- $tmp = $info;
- $tmp['itemCover'] = self::groupImg($info['itemCover']);
- unset($v['itemInfo']);
- //兼容 旧数据,没有 rank (花材等级 默认给B)
- if (!isset($tmp['rank'])) {
- $tmp['rank'] = 'B';
- }
- $tmp = array_merge($tmp, $v);
- $itemData[] = $tmp;
- }
- }
- $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['itemInfo'] = $itemData;
- $orderData['statusName'] = self::getStatusName($orderData['status']);
- return $orderData;
- }
- // 组装 orderItem linqh 2021.1.25
- public static function groupOrderItem($ghsItemInfo, $orderSn)
- {
- $productData = self::getProductMapData($ghsItemInfo);
- //$itemIds = array_column($productData,'itemId');
- //$itemData = xhItemService::getByIds($itemIds);
- //$itemData = array_column($itemData, NULL, 'id');
- //写入详情表
- $batchData = [];
- foreach ($ghsItemInfo as $v) {
- $tmp = [];
- $productId = $v['productId'];
- $itemId = $productData[$productId]['itemId'];
- $rank = $productData[$productId]['rank'] ?? 'B';// 花材级别
- $tmp['orderSn'] = $orderSn;
- $tmp['itemId'] = $itemId;
- $tmp['productId'] = $v['productId'];
- $ratio = $productData[$productId]['ratio'] ?? 0;
- $tmp['itemStock'] = $productData[$productId]['stock'] ?? 0;//当时库存
- $tmp['itemNum'] = ProductClass::mergeItemNum($v['bigNum'], $v['smallNum'], $ratio);// 采购数量
- $tmp['itemPrice'] = $v['itemPrice']; //采购价格
- $stockFormat = ProductClass::formatStock($tmp['itemStock'], $ratio); // 库存总单位数量转大小单位的数量
- $itemInfo = [
- 'itemName' => $productData[$productId]['name'] ?? '',//花材名称
- 'itemCover' => $productData[$productId]['cover'] ?? '',//花材图片
- 'bigNum' => $v['bigNum'] ?? 0,//采购数量(大单位)
- 'smallNum' => $v['smallNum'] ?? 0,//采购数量(小单位)
- 'bigNumStock' => $stockFormat['bigNum'],// 当时库存大单位数
- 'smallNumStock' => $stockFormat['smallNum'],//当时库存 小单位数
- 'itemUnit' => $ratio ? 2 : 1,//单位
- 'ratio' => $ratio,// 比例
- 'rank' => $rank,// 花材级别
- 'bigUnit' => $productData[$productId]['bigUnit'],//大单位名称 扎
- 'smallUnit' => $productData[$productId]['smallUnit'],//小单位名称 支
- 'purchaseWeight' => $v['weight'] ?? 0, //采购重量
- 'itemWeight' => $productData[$productId]['weight'] ?? 0,
- ];
- $tmp['itemInfo'] = json_encode($itemInfo);
- $batchData[] = $tmp;
- }
- return $batchData;
- }
- // 组装状态码 linqh 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,orderPrice");
- $totalAmount = 0;
- $totalNum = count($res);
- foreach ($res as $v) {
- $totalAmount = bcadd($totalAmount, $v['orderPrice'], 2);
- }
- return [
- 'totalAmount' => $totalAmount,
- 'totalNum' => $totalNum,
- ];
- }
- }
|