feat: 修改了WorkbenchController和相关的模型类,优化了仓库统计功能,增加了门店仓库总库存的统计方法。

This commit is contained in:
mkm 2024-08-23 13:46:25 +08:00
parent cf1d6db00a
commit a0814641c8
7 changed files with 123 additions and 69 deletions

View File

@ -52,13 +52,13 @@ class WorkbenchController extends BaseAdminController
public function store_index()
{
$params = $this->request->get();
if(!isset($params['store_id']) ||$params['store_id']==''){
$params['store_id'] =0;
if (!isset($params['store_id']) || $params['store_id'] == '') {
$params['store_id'] = 0;
}
if(!isset($params['start_time']) ||$params['start_time']==''){
$time=explode('-', $this->getDay(''));
$params['start_time'] =$time[0];
$params['end_time'] =$time[1];
if (!isset($params['start_time']) || $params['start_time'] == '') {
$time = explode('-', $this->getDay(''));
$params['start_time'] = $time[0];
$params['end_time'] = $time[1];
}
$result = LogicWorkbenchLogic::index($params);
@ -102,14 +102,14 @@ class WorkbenchController extends BaseAdminController
public function get_trend()
{
$dates = [];
$date=$this->request->get('date','');
$days=31;
if($date){
$arr=explode('-', $date);
if($arr[0]==$arr[1]){
$date = $this->request->get('date', '');
$days = 31;
if ($date) {
$arr = explode('-', $date);
if ($arr[0] == $arr[1]) {
$date = new DateTime($arr[0]);
$dates[] = $date->format("Y-m-d");
}else{
} else {
$datetime_start = new DateTime($arr[0]);
$datetime_end = new DateTime($arr[1]);
$days = $datetime_start->diff($datetime_end)->days;
@ -119,7 +119,7 @@ class WorkbenchController extends BaseAdminController
$dates[] = $date->format('Y-m-d');
}
}
}else{
} else {
$today = new DateTime();
$thirtyDaysAgo = new DateTime($today->format('Y-m-d'));
$thirtyDaysAgo->modify('-30 days');
@ -166,10 +166,10 @@ class WorkbenchController extends BaseAdminController
*/
public function get_product_ranking()
{
$date=$this->request->get('date','');
$date = $this->request->get('date', '');
$where['create_time'] = $this->getDay($date);
$data=(new ProductStatisticLogic())->get_product_ranking($where);
return $this->success('',$data);
$data = (new ProductStatisticLogic())->get_product_ranking($where);
return $this->success('', $data);
}
//-------------------------------用户统计---------------------------------------//
@ -179,13 +179,13 @@ class WorkbenchController extends BaseAdminController
*/
public function get_user_basic()
{
$date=$this->request->get('date','');
$store_id=$this->request->get('store_id');
$date = $this->request->get('date', '');
$store_id = $this->request->get('store_id');
$where['create_time'] = $this->getDay($date);
if($store_id){
if ($store_id) {
$where['store_id'] = $store_id;
}
$data=(new UserStatisticLogic())->getBasic($where);
$data = (new UserStatisticLogic())->getBasic($where);
return $this->data($data);
}
@ -194,13 +194,13 @@ class WorkbenchController extends BaseAdminController
*/
public function get_user_trend()
{
$date=$this->request->get('date','');
$store_id=$this->request->get('store_id');
$date = $this->request->get('date', '');
$store_id = $this->request->get('store_id');
$where['create_time'] = $this->getDay($date);
if($store_id){
if ($store_id) {
$where['store_id'] = $store_id;
}
$data=(new UserStatisticLogic())->getTrend($where);
$data = (new UserStatisticLogic())->getTrend($where);
return $this->data($data);
}
@ -208,7 +208,7 @@ class WorkbenchController extends BaseAdminController
//当日订单金额
public function top_trade()
{
$logic=(new TradeStatisticLogic());
$logic = (new TradeStatisticLogic());
$leftToday = $logic->getTopLeftTrade(['create_time' => 'today']);
$leftyestoday = $logic->getTopLeftTrade(['create_time' => 'yestoday']);
$rightOne = $logic->getTopRightOneTrade();
@ -231,22 +231,24 @@ class WorkbenchController extends BaseAdminController
//交易趋势
public function bottom_trade()
{
$date=$this->request->get('date','');
$data=(new TradeStatisticLogic())->getBottomTrade(['data'=>$this->getDay($date)]);
$date = $this->request->get('date', '');
$data = (new TradeStatisticLogic())->getBottomTrade(['data' => $this->getDay($date)]);
return $this->data($data);
}
/**
* 实时商品统计
*/
public function product_order(){
public function product_order()
{
return $this->dataLists(new StoreOrderCartInfoGroupLists());
}
/**
* 月销售商品统计
*/
public function product_month_order(){
public function product_month_order()
{
return $this->dataLists(new StoreOrderCartInfoGroupMonthLists());
}
/**
@ -261,9 +263,9 @@ class WorkbenchController extends BaseAdminController
if (!$startTime && !$endTime) {
return date("Y/m/d", strtotime("-30 days", time())) . '-' . date("Y/m/d", time());
} else {
if($startTime==$endTime){
return $startTime . '-' . $endTime.' 23:59:59';
}else{
if ($startTime == $endTime) {
return $startTime . '-' . $endTime . ' 23:59:59';
} else {
return $startTime . '-' . $endTime;
}
}
@ -271,24 +273,35 @@ class WorkbenchController extends BaseAdminController
return date("Y/m/d", strtotime("-30 days", time())) . '-' . date("Y/m/d", time());
}
}
//-------------------------------统计仓库---------------------------------------//
/**
* 统计仓库
* 统计仓库
* @return array
*/
public function total_warehouse()
{
$data=WarehouseLogic::total_warehouse();
$data = WarehouseLogic::total_warehouse();
return $this->data($data);
}
/**
* 统计仓库
* 统计仓库列表
* @return array
*/
public function warehouse_list()
{
return $this->dataLists(new StoreProductLists());
}
/**
* 统计门店仓库总库存
* @return array
*/
public function total_warehouse_list()
{
$parmas=$this->request->get();
$data =WarehouseLogic::total_warehouse_list($parmas,$parmas['type']??1);
return $this->data($data);
}
}

View File

@ -66,12 +66,12 @@ class StoreProductLists extends BaseAdminDataLists implements ListsSearchInterfa
->limit($this->limitOffset, $this->limitLength)
->field('p.id, p.store_name, p.image,
(SELECT SUM(c.cart_num) FROM `la_store_order_cart_info` c WHERE c.product_id=p.id AND c.is_pay=1) AS sales,
(SELECT SUM(b.stock) FROM `la_store_branch_product` b WHERE b.product_id=p.id) AS store_stock,
(SELECT SUM(w.nums) FROM `la_warehouse_product_storege` w WHERE w.product_id=p.id) AS warehouse_stock,
(SELECT SUM(wp.total_price) FROM `la_warehouse_product` wp WHERE wp.product_id=p.id) AS total_purchase,
(SELECT SUM(wp.total_price) FROM `la_warehouse_product` wp WHERE wp.product_id=p.id AND wp.is_pay=1) AS total_completed_amount,
(SELECT SUM(wp.total_price) FROM `la_warehouse_product` wp WHERE wp.product_id=p.id AND wp.is_pay=0) AS total_outstanding_amount')
(SELECT SUM(c.cart_num) FROM `la_store_order_cart_info` c WHERE c.product_id=p.id AND c.is_pay=1 AND c.delete_time IS NULL) AS sales,
(SELECT SUM(b.stock) FROM `la_store_branch_product` b WHERE b.product_id=p.id AND b.delete_time IS NULL) AS store_stock,
(SELECT SUM(w.nums) FROM `la_warehouse_product_storege` w WHERE w.product_id=p.id AND w.delete_time IS NULL) AS warehouse_stock,
(SELECT SUM(wp.total_price) FROM `la_warehouse_product` wp WHERE wp.product_id=p.id AND wp.delete_time IS NULL) AS total_purchase,
(SELECT SUM(wp.total_price) FROM `la_warehouse_product` wp WHERE wp.product_id=p.id AND wp.is_pay=1 AND wp.delete_time IS NULL) AS total_completed_amount,
(SELECT SUM(wp.total_price) FROM `la_warehouse_product` wp WHERE wp.product_id=p.id AND wp.is_pay=0 AND wp.delete_time IS NULL) AS total_outstanding_amount')
->select()
->each(function ($item) {
// 计算总库存

View File

@ -7,9 +7,11 @@ use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_order\StoreOrder;
use app\common\model\store_order_cart_info\StoreOrderCartInfo;
use app\common\model\store_product\StoreProduct;
use app\common\model\system_store\SystemStore;
use app\common\model\user\User;
use app\common\model\user\UserVisit;
use app\common\model\user_recharge\UserRecharge;
use app\common\model\warehouse\Warehouse;
use app\common\model\warehouse_order\WarehouseOrder;
use app\common\model\warehouse_product\WarehouseProduct;
use app\common\model\warehouse_product_storege\WarehouseProductStorege;
@ -103,4 +105,34 @@ class WarehouseLogic extends BaseLogic
return $data;
}
/**
* 统计门店仓库总库存
* @return array
*/
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{
$item->total_price = 0;
}
});
$count=WarehouseProductStorege::where('nums','>',0)->where('product_id',$parmas['product_id'])->count();
}
return ['lists'=>$list,'count'=>$count];
}
}

View File

@ -42,7 +42,7 @@ class StoreOrderLogic extends BaseLogic
$v['uid'] = $params['user_id'];
$v['store_id'] = $params['store_id'];
$v['cart_num'] = $v['stock'];
StoreBranchProduct::where('id',$v['id'])->update(['price'=>$v['price'],'vip_price'=>$v['price'],'cost'=>$v['price'],'purchase'=>$v['purchase']]);
StoreBranchProduct::where('id',$v['id'])->update(['price'=>$v['price'],'vip_price'=>$v['price'],'cost'=>$v['price'],'purchase'=>$v['price']]);
unset($v['id']);
$res = CartLogic::add($v);
$cartId[] = $res['id'];
@ -56,7 +56,7 @@ class StoreOrderLogic extends BaseLogic
return false;
} else {
$arr = [
'warehouse_id' => 1,
'warehouse_id' => $params['warehouse_id']??-1,
'store_id' => $params['store_id'],
'supplier_id' => 0,
'code' => getNewOrderId('PS'),
@ -69,23 +69,21 @@ class StoreOrderLogic extends BaseLogic
$res = WarehouseOrder::create($arr);
foreach ($params['product_arr'] as $k => $v) {
$data = [
'warehouse_id' => 1,
'warehouse_id' => $params['warehouse_id']??-1,
'product_id' => $v['product_id'],
'store_id' => $params['store_id'],
'financial_pm' => 0,
'batch' => 1,
'nums' => $arr['stock'],
'nums' => $v['stock'],
'status' => 1,
'admin_id' => $params['admin_id'],
];
$storeProduct = StoreProduct::where('id', $arr['id'])->findOrEmpty()->toArray();
if ($arr['stock'] == 0) {
StoreProductLogic::ordinary($arr, $params['store_id'], $params['admin_id'], $storeProduct);
} else {
$data['total_price'] = bcmul($arr['stock'], $storeProduct['purchase'], 2);
$storeProduct = StoreBranchProduct::where('id', $v['id'])->findOrEmpty()->toArray();
if ($v['stock']>0) {
$data['total_price'] = bcmul($v['stock'], $storeProduct['purchase'], 2);
$data['purchase'] = $storeProduct['purchase'];
$data['oid'] = $res['id'];
WarehouseProductLogic::add($data);
WarehouseProductLogic::add($data,0);
$finds = WarehouseProduct::where('oid', $res['id'])->field('sum(nums) as nums,sum(total_price) as total_price')->find();
WarehouseOrder::where('id', $res['id'])->update(['total_price' => $finds['total_price'], 'nums' => $finds['nums']]);
}

View File

@ -32,7 +32,7 @@ class WarehouseProductLogic extends BaseLogic
* @author admin
* @date 2024/07/31 16:55
*/
public static function add(array $params)
public static function add(array $params,$type=1)
{
// Db::startTrans();
try {
@ -55,13 +55,15 @@ class WarehouseProductLogic extends BaseLogic
}
$storeBranchProduct=StoreProductLogic::ordinary(['id'=>$params['product_id']],$params['store_id'], $params['admin_id'], $storeProduct);
}
if ($params['nums'] > 0) {
if ($params['nums'] > 0&&$type==1) {
StoreBranchProduct::where('id', $storeBranchProduct['id'])->inc('stock',$params['nums'])->update();
}
} else {
$after_nums = $storege['nums'] + $params['nums'];
if($type==1){
WarehouseProductStorege::where('id', $storege['id'])->inc('nums', $params['nums'])->update();
}
}
$before_nums = $storege['nums'];
} else {
$after_nums=$params['nums'];

View File

@ -23,6 +23,8 @@ use support\Log;
use think\facade\Db;
use Webman\RedisQueue\Redis;
use function DI\get;
class OrderController extends BaseApiController
{
public $notNeedLogin = ['refund_reason'];
@ -134,7 +136,7 @@ class OrderController extends BaseApiController
public function createOrder()
{
$cartId = (array)$this->request->post('cart_id', []);
$store_id = (array)$this->request->post('store_id', 0);
$store_id =$this->request->post('store_id', 0);
$pay_type = (int)$this->request->post('pay_type');
$addressId = (int)$this->request->post('address_id');
$auth_code = $this->request->post('auth_code'); //微信支付条码
@ -145,7 +147,13 @@ class OrderController extends BaseApiController
if (count($cartId) > 100) {
return $this->fail('购物车商品不能超过100个');
}
$store_arr=getenv('NO_STORE_ID');
if($store_arr){
$store_arr=explode(',',$store_arr);
if (in_array($store_id,$store_arr)){
return $this->fail('该门店不支持下单');
}
}
$user = User::where('id', $this->userId)->find();
if ($pay_type == PayEnum::PURCHASE_FUNDS || $pay_type == PayEnum::BALANCE_PAY) {
if (!isset($params['password'])) {

View File

@ -257,6 +257,7 @@ class OrderLogic extends BaseLogic
$alert = '当前时间超过配送截止时间16:00,若下单,物品送达时间为' . date('Y-m-d', strtotime($currentDate . '+2 days'));
}
} catch (\Exception $e) {
d($e);
self::setError($e->getMessage());
return false;
}