2024-08-21 22:55:18 +08:00
< ? php
namespace app\admin\logic\statistic ;
use app\common\logic\BaseLogic ;
2024-08-22 14:14:21 +08:00
use app\common\model\store_branch_product\StoreBranchProduct ;
2024-09-01 22:23:15 +08:00
use app\common\model\store_finance_flow\StoreFinanceFlow ;
2024-08-21 22:55:18 +08:00
use app\common\model\store_order\StoreOrder ;
2024-08-22 14:14:21 +08:00
use app\common\model\store_order_cart_info\StoreOrderCartInfo ;
2024-08-22 12:04:58 +08:00
use app\common\model\store_product\StoreProduct ;
2024-08-23 14:41:07 +08:00
use app\common\model\supplier\Supplier ;
2024-08-23 13:46:25 +08:00
use app\common\model\system_store\SystemStore ;
2024-08-21 22:55:18 +08:00
use app\common\model\user\User ;
use app\common\model\user\UserVisit ;
use app\common\model\user_recharge\UserRecharge ;
2024-08-23 13:46:25 +08:00
use app\common\model\warehouse\Warehouse ;
2024-08-22 14:14:21 +08:00
use app\common\model\warehouse_order\WarehouseOrder ;
use app\common\model\warehouse_product\WarehouseProduct ;
use app\common\model\warehouse_product_storege\WarehouseProductStorege ;
2024-08-21 22:55:18 +08:00
use Exception ;
2024-08-22 12:04:58 +08:00
2024-08-21 22:55:18 +08:00
/**
* Class 仓库统计
* @ package app\services\statistic
*/
class WarehouseLogic extends BaseLogic
{
2024-08-22 12:04:58 +08:00
public static function total_warehouse ()
{
2024-08-21 22:55:18 +08:00
$topData [] = [
'title' => '总采购金额' ,
'desc' => '平台采购商品总支付金额' ,
2024-08-22 14:14:21 +08:00
'total_money' => WarehouseOrder :: sum ( 'total_price' ),
2024-08-21 22:55:18 +08:00
'value' => [],
'type' => 1 ,
];
$topData [] = [
'title' => '已结算金额' ,
'desc' => '平台支付给供应商的金额' ,
2024-08-22 14:14:21 +08:00
'total_money' => WarehouseOrder :: sum ( 'completed_amount' ),
2024-08-21 22:55:18 +08:00
'value' => [],
'type' => 1 ,
];
$topData [] = [
'title' => '未结算金额' ,
'desc' => '平台未支付给供应商的金额' ,
2024-08-22 14:14:21 +08:00
'total_money' => WarehouseOrder :: sum ( 'outstanding_amount' ),
2024-08-21 22:55:18 +08:00
'value' => [],
'type' => 1 ,
];
2024-09-01 22:23:15 +08:00
$pay_price = StoreOrder :: where ( 'paid' , 1 ) -> where ( 'refund_status' , 0 ) -> sum ( 'pay_price' );
// $refund_price=StoreOrder::where('paid',1)->sum('refund_price');
$topData [] = [
'title' => '交易金额' ,
'desc' => '平台发生交易的金额' ,
'total_money' => $pay_price ,
'value' => [],
'type' => 1 ,
];
$number = StoreFinanceFlow :: where ( 'financial_type' , 3 ) -> where ( 'financial_pm' , 1 ) -> sum ( 'number' );
$topData [] = [
'title' => '获得利润' ,
'desc' => '平台订单产生的手续费' ,
'total_money' => $number ,
'value' => [],
'type' => 1 ,
];
2024-09-01 16:58:21 +08:00
// $toreProduct = StoreProduct::where('stock', '>', 0)->field('sum(stock) as stock,sum(total_price) as total_price')->find();
$warehouseProductStorege = WarehouseProductStorege :: where ( 'nums' , '>' , 0 ) -> field ( 'sum(nums) as nums,sum(total_price) as total_price' ) -> find ();
$storeBranchProduct = StoreBranchProduct :: where ( 'stock' , '>' , 0 ) -> field ( 'sum(stock) as stock,sum(total_price) as total_price' ) -> find ();
2024-08-21 22:55:18 +08:00
$topData [] = [
'title' => '总商品库存' ,
'desc' => '平台统计商品总库存、含门店仓库' ,
2024-09-01 18:04:07 +08:00
'total_money' => bcadd ( $warehouseProductStorege [ 'nums' ], $storeBranchProduct [ 'stock' ], 2 ),
'cash_title' => bcadd ( $warehouseProductStorege [ 'total_price' ], $storeBranchProduct [ 'total_price' ], 2 ),
2024-08-21 22:55:18 +08:00
'value' => [],
'type' => 1 ,
];
2024-08-24 13:40:52 +08:00
$warehouseProductStorege = WarehouseProductStorege :: where ( 'nums' , '>' , 0 ) -> field ( 'sum(nums) as nums,sum(total_price) as total_price' ) -> find ();
2024-08-21 22:55:18 +08:00
$topData [] = [
'title' => '总仓库库存' ,
'desc' => '平台统计仓库库存' ,
2024-08-24 11:30:25 +08:00
'total_money' => $warehouseProductStorege [ 'nums' ],
'cash_title' => $warehouseProductStorege [ 'total_price' ],
2024-08-21 22:55:18 +08:00
'value' => [],
'type' => 1 ,
];
2024-08-24 15:12:53 +08:00
$warehouseProductStorege_1 = WarehouseProductStorege :: where ( 'nums' , '>' , 0 ) -> where ( 'warehouse_id' , 1 ) -> field ( 'sum(nums) as nums,sum(total_price) as total_price' ) -> find ();
2024-08-21 22:55:18 +08:00
$topData [] = [
'title' => '海吉星仓库库存' ,
'desc' => '平台统计海吉星仓库库存' ,
2024-08-24 11:30:25 +08:00
'total_money' => $warehouseProductStorege_1 [ 'nums' ],
'cash_title' => $warehouseProductStorege_1 [ 'total_price' ],
2024-08-21 22:55:18 +08:00
'value' => [],
'type' => 1 ,
];
2024-08-24 15:12:53 +08:00
$warehouseProductStorege_2 = WarehouseProductStorege :: where ( 'nums' , '>' , 0 ) -> where ( 'warehouse_id' , 2 ) -> field ( 'sum(nums) as nums,sum(total_price) as total_price' ) -> find ();
2024-08-21 22:55:18 +08:00
$topData [] = [
'title' => '泸县集采集配库存' ,
'desc' => '平台统计泸县集采集配库存' ,
2024-08-24 11:30:25 +08:00
'total_money' => $warehouseProductStorege_2 [ 'nums' ],
'cash_title' => $warehouseProductStorege_2 [ 'total_price' ],
2024-08-21 22:55:18 +08:00
'value' => [],
'type' => 1 ,
];
$topData [] = [
'title' => '总门店库存' ,
'desc' => '平台统计门店库存' ,
2024-08-24 15:12:53 +08:00
'total_money' => $storeBranchProduct [ 'stock' ],
2024-08-24 11:30:25 +08:00
'cash_title' => $storeBranchProduct [ 'total_price' ],
2024-08-21 22:55:18 +08:00
'value' => [],
'type' => 1 ,
];
$data [ 'series' ] = [];
foreach ( $topData as $k => $v ) {
// $data['x'] = $Chain['out']['x'];
$data [ 'series' ][ $k ][ 'name' ] = $v [ 'title' ];
$data [ 'series' ][ $k ][ 'desc' ] = $v [ 'desc' ];
$data [ 'series' ][ $k ][ 'total_value' ] = $v [ 'total_money' ];
2024-08-22 12:04:58 +08:00
$data [ 'series' ][ $k ][ 'total_money' ] = $v [ 'cash_title' ] ? ? '' ;
2024-08-21 22:55:18 +08:00
$data [ 'series' ][ $k ][ 'type' ] = $v [ 'type' ];
}
return $data ;
}
2024-08-22 12:04:58 +08:00
2024-08-23 13:46:25 +08:00
/**
* 统计门店仓库总库存
* @ return array
*/
2024-08-24 15:12:53 +08:00
public static function total_warehouse_list ( $parmas , $type = 1 )
{
if ( $type == 1 ) {
$list = StoreBranchProduct :: where ( 'stock' , '>' , 0 ) -> where ( 'product_id' , $parmas [ 'product_id' ])
-> select () -> each ( function ( $item ) {
$item -> system_store = SystemStore :: where ( 'id' , $item [ 'store_id' ]) -> value ( 'name' );
$item -> total_price = bcmul ( $item -> purchase , $item -> stock , 2 );
});
$count = StoreBranchProduct :: where ( 'stock' , '>' , 0 ) -> where ( 'product_id' , $parmas [ 'product_id' ])
-> count ();
} else {
$list = WarehouseProductStorege :: where ( 'nums' , '>' , 0 ) -> where ( 'product_id' , $parmas [ 'product_id' ]) -> select () -> each ( function ( $item ) {
$item -> warehouse_name = Warehouse :: where ( 'id' , $item [ 'warehouse_id' ]) -> value ( 'name' );
$find = StoreProduct :: where ( 'id' , $item [ 'product_id' ]) -> find ();
$item -> store_name = $find [ 'store_name' ];
$item -> image = $find [ 'image' ];
$item -> purchase = $find [ 'purchase' ];
if ( $find [ 'purchase' ] > 0 && $item -> nums > 0 ) {
$item -> total_price = bcmul ( $find [ 'purchase' ], $item -> nums , 2 );
} else {
2024-08-23 13:46:25 +08:00
$item -> total_price = 0 ;
}
});
2024-08-24 15:12:53 +08:00
$count = WarehouseProductStorege :: where ( 'nums' , '>' , 0 ) -> where ( 'product_id' , $parmas [ 'product_id' ]) -> count ();
2024-08-23 13:46:25 +08:00
}
2024-08-24 15:12:53 +08:00
return [ 'lists' => $list , 'count' => $count ];
2024-08-23 13:46:25 +08:00
}
2024-08-23 14:41:07 +08:00
/**
* 统计商品采购总价 已结未结
* @ return array
*/
2024-08-24 15:12:53 +08:00
public static function total_warehouse_product_list ( $parmas , $type = 1 )
{
$list = [];
$count = 0 ;
if ( $type == 1 ) {
2024-08-23 14:41:07 +08:00
//总采购价
2024-08-24 15:12:53 +08:00
$where = [[ 'product_id' , '=' , $parmas [ 'product_id' ]], [ 'nums' , '>' , 0 ], [ 'financial_pm' , '=' , 1 ]];
$list = WarehouseProduct :: where ( $where ) -> select () -> each ( function ( $item ) {
$item -> warehouse_name = Warehouse :: where ( 'id' , $item [ 'warehouse_id' ]) -> value ( 'name' );
$item -> supplier_name = Supplier :: where ( 'id' , $item [ 'supplier_id' ]) -> value ( 'mer_name' );
$find = StoreProduct :: where ( 'id' , $item [ 'product_id' ]) -> find ();
$item -> store_name = $find [ 'store_name' ];
$item -> image = $find [ 'image' ];
2024-08-23 14:41:07 +08:00
});
2024-08-24 15:12:53 +08:00
$count = WarehouseProduct :: where ( $where ) -> count ();
} elseif ( $type == 2 ) {
//已结算采购价
$where = [[ 'product_id' , '=' , $parmas [ 'product_id' ]], [ 'nums' , '>' , 0 ], [ 'financial_pm' , '=' , 1 ], [ 'is_pay' , '=' , 1 ]];
$list = WarehouseProduct :: where ( $where ) -> select () -> each ( function ( $item ) {
$item -> warehouse_name = Warehouse :: where ( 'id' , $item [ 'warehouse_id' ]) -> value ( 'name' );
$item -> supplier_name = Supplier :: where ( 'id' , $item [ 'supplier_id' ]) -> value ( 'mer_name' );
$find = StoreProduct :: where ( 'id' , $item [ 'product_id' ]) -> find ();
$item -> store_name = $find [ 'store_name' ];
$item -> image = $find [ 'image' ];
2024-08-23 14:41:07 +08:00
});
2024-08-24 15:12:53 +08:00
$count = WarehouseProduct :: where ( $where ) -> count ();
} elseif ( $type == 3 ) {
//未结算采购价
$where = [[ 'product_id' , '=' , $parmas [ 'product_id' ]], [ 'nums' , '>' , 0 ], [ 'financial_pm' , '=' , 1 ], [ 'is_pay' , '=' , 0 ]];
$list = WarehouseProduct :: where ( $where ) -> select () -> each ( function ( $item ) {
$item -> warehouse_name = Warehouse :: where ( 'id' , $item [ 'warehouse_id' ]) -> value ( 'name' );
$item -> supplier_name = Supplier :: where ( 'id' , $item [ 'supplier_id' ]) -> value ( 'mer_name' );
$find = StoreProduct :: where ( 'id' , $item [ 'product_id' ]) -> find ();
$item -> store_name = $find [ 'store_name' ];
$item -> image = $find [ 'image' ];
2024-08-23 14:41:07 +08:00
});
2024-08-24 15:12:53 +08:00
$count = WarehouseProduct :: where ( $where ) -> count ();
}
return [ 'lists' => $list , 'count' => $count ];
}
/**
* 负库存检测
*/
public static function negativeInventory ( $parmas )
{
if ( $parmas [ 'type' ] == 1 ) {
$list = StoreProduct :: where ( 'stock' , '<' , 0 ) -> page ( $parmas [ 'page_no' ], 15 ) -> select () -> toArray ();
$count = StoreProduct :: where ( 'stock' , '<' , 0 ) -> count ();
} elseif ( $parmas [ 'type' ] == 2 ) {
2024-09-01 18:04:07 +08:00
$where [] = [ 'stock' , '<' , 0 ];
if ( isset ( $parmas [ 'store_id' ]) && $parmas [ 'store_id' ] > 0 ) {
$where [] = [ 'store_id' , '=' , $parmas [ 'store_id' ]];
2024-08-25 13:56:55 +08:00
}
2024-09-01 18:04:07 +08:00
$store_arr = getenv ( 'NO_STORE_STATISTICS' );
if ( $store_arr ) {
$store_arr = explode ( ',' , $store_arr );
$where [] = [ 'store_id' , 'not in' , $store_arr ];
2024-08-25 16:34:56 +08:00
}
2024-08-25 13:56:55 +08:00
$list = StoreBranchProduct :: where ( $where ) -> page ( $parmas [ 'page_no' ], 15 ) -> select ()
2024-08-24 15:12:53 +08:00
-> each ( function ( $item ) {
$item -> remark = SystemStore :: where ( 'id' , $item [ 'store_id' ]) -> value ( 'name' );
})
-> toArray ();
2024-08-25 13:56:55 +08:00
$count = StoreBranchProduct :: where ( $where ) -> count ();
2024-08-24 15:12:53 +08:00
} elseif ( $parmas [ 'type' ] == 3 ) {
$list = WarehouseProductStorege :: where ( 'nums' , '<' , 0 ) -> page ( $parmas [ 'page_no' ], 15 ) -> select ()
-> each ( function ( $item ) {
$find = StoreProduct :: where ( 'id' , $item [ 'product_id' ]) -> find ();
$item -> store_name = $find [ 'store_name' ];
$item -> image = $find [ 'image' ];
$item -> stock = $item [ 'nums' ];
$item -> remark = Warehouse :: where ( 'id' , $item [ 'warehouse_id' ]) -> value ( 'name' );
}) -> toArray ();
$count = WarehouseProductStorege :: where ( 'nums' , '<' , 0 ) -> count ();
2024-08-23 14:41:07 +08:00
}
2024-08-24 15:12:53 +08:00
return [ 'lists' => $list , 'count' => $count ];
2024-09-01 18:04:07 +08:00
}
2024-08-25 17:57:48 +08:00
/**
* 负库存更新归0
*/
public static function updateNegativeZero ( $parmas )
{
if ( $parmas [ 'type' ] == 1 ) {
2024-09-01 18:04:07 +08:00
$res = StoreProduct :: where ( 'id' , $parmas [ 'id' ]) -> update ([ 'stock' => 0 ]);
2024-08-25 17:57:48 +08:00
} elseif ( $parmas [ 'type' ] == 2 ) {
2024-09-01 18:04:07 +08:00
$res = StoreBranchProduct :: where ( 'id' , $parmas [ 'id' ]) -> update ([ 'stock' => 0 ]);
2024-08-25 17:57:48 +08:00
} elseif ( $parmas [ 'type' ] == 3 ) {
2024-09-01 18:04:07 +08:00
$res = WarehouseProductStorege :: where ( 'id' , $parmas [ 'id' ]) -> update ([ 'nums' => 0 ]);
2024-08-25 17:57:48 +08:00
}
return $res ;
2024-08-23 14:41:07 +08:00
}
2024-09-01 15:51:23 +08:00
2024-09-01 18:04:07 +08:00
public static function stockProductPrice ( $parmas )
{
$arr1 = WarehouseProductStorege :: where ( 'nums' , '>' , 0 ) -> select ();
foreach ( $arr1 as $k => $v ) {
$find = StoreProduct :: where ( 'id' , $v [ 'product_id' ]) -> find ();
if ( $find && $find [ 'price' ] > 0 ) {
$total_price = bcmul ( $find [ 'price' ], $v [ 'nums' ], 2 );
$price = $find [ 'price' ];
} else {
$total_price = 0 ;
$price = 0 ;
2024-09-01 15:51:23 +08:00
}
2024-09-01 18:04:07 +08:00
WarehouseProductStorege :: where ( 'id' , $v [ 'id' ]) -> update ([ 'price' => $price , 'total_price' => $total_price ]);
2024-09-01 15:51:23 +08:00
}
2024-09-01 18:04:07 +08:00
$arr2 = StoreBranchProduct :: where ( 'stock' , '>' , 0 ) -> select ();
foreach ( $arr2 as $k => $v ) {
if ( $v [ 'price' ] > 0 ) {
$total_price = bcmul ( $v [ 'price' ], $v [ 'stock' ], 2 );
} else {
$total_price = 0 ;
2024-09-01 15:51:23 +08:00
}
2024-09-01 18:04:07 +08:00
StoreBranchProduct :: where ( 'id' , $v [ 'id' ]) -> update ([ 'total_price' => $total_price ]);
2024-09-01 15:51:23 +08:00
}
2024-09-01 18:04:07 +08:00
$arr3 = StoreProduct :: where ( 'stock' , '>=' , 0 ) -> select ();
foreach ( $arr3 as $k => $v ) {
$stock = StoreBranchProduct :: where ( 'product_id' , $v [ 'id' ]) -> where ( 'stock' , '>' , 0 ) -> sum ( 'stock' );
$nums = WarehouseProductStorege :: where ( 'nums' , '>' , 0 ) -> where ( 'product_id' , $v [ 'id' ]) -> sum ( 'nums' );
$stock2 = bcadd ( $stock , $nums , 2 );
bcmul ( $v [ 'purchase' ], $stock2 , 2 );
StoreProduct :: where ( 'id' , $v [ 'id' ]) -> update ([ 'stock' => $stock2 , 'total_price' => $v ]);
2024-09-01 15:51:23 +08:00
}
2024-09-01 18:04:07 +08:00
2024-09-01 15:51:23 +08:00
return true ;
}
2024-08-22 12:04:58 +08:00
}