<?php
namespace app\warehouse\admin;
use app\system\admin\Admin;
use app\warehouse\model\Warehouse as WarehouseModel;
use app\shop\model\Shop as ShopModel;
use app\warehouse\model\Stock as StockModel;
use app\product\model\Product as ProductModel;
use app\user\model\User as UserModel;
use think\Db;
class Stock extends Admin
{
protected $hisiModel = 'Stock' protected $hisiTable = '' protected $hisiAddScene = '' protected $hisiEditScene = ''
protected function initialize()
{
parent::initialize();
$users = UserModel::field('id,nick')->all();
$this->assign('users', $users);
$products = ProductModel::field('id,name')->all();
$this->assign('products', $products);
$StockModel = new StockModel;
$detailed_types = $StockModel->detailedTypes();
$this->assign('detailed_types', $detailed_types);
$wares = Db::name('warehouse')->field('id,name')->select();
$this->assign('wares',$wares);
}
public function index()
{
if ($this->request->isAjax()) {
$where = [];
$page = $this->request->param('page/d', 1);
$limit = $this->request->param('limit/d', 15);
$where['status'] = 1;
$where['shop_id'] = session('shop_id');
$user_id = $this->request->param('user_id/d', '');
$product_id = $this->request->param('product_id/d', '');
$detailed_type = $this->request->param('detailed_type/d', '');
$warehouse_id = $this->request->param('warehouse_id/d', '');
$start = $this->request->param('start', '2000-01-01');
$end = $this->request->param('end', date('Y-m-d H:i:s',time()));
if(!empty($warehouse_id)){
$where['warehouse_id'] = $warehouse_id;
}
if(!empty($user_id)){
$where['user_id'] = $user_id;
}
if(!empty($product_id)){
$where['product_id'] = $product_id;
}
if(!empty($detailed_type)){
$where['detailed_type'] = $detailed_type;
}
if(empty($start)){
$start = '2000-01-01';
}
if(empty($end)){
$end = date('Y-m-d H:i:s',time());
}
$data['data'] = StockModel::where($where)
->field('*, detailed_type as detailed_type_text,user_id as user_name,warehouse_id as warehouse_text')
->whereTime('createtime', [strtotime($start), strtotime($end)])
->page($page)
->limit($limit)
->order('id desc')
->select();
$data['count'] = StockModel::where($where)->count('id');
$data['code'] = 0;
return json($data);
}
return $this->fetch();
}
public function real()
{
if ($this->request->isAjax()) {
$where = [];
$page = $this->request->param('page/d', 1);
$limit = $this->request->param('limit/d', 15);
$product_id = $this->request->param('product_id/d', '');
if(!empty($product_id)){
$where['id'] = $product_id;
}
$data['data'] = ProductModel::where($where)
->field('*, unit_id as unit_text, decomposition_id as decomposition_text,id as warehousing')
->page($page)
->limit($limit)
->order('id desc')
->select();
$out_of = [];
foreach ($data['data'] as $key => $value) {
$data['data'][$key]['zong'] = Db::name('stock')->alias('s')
->join('hisi_warehouse w', 'w.id=s.warehouse_id')
->where('s.shop_id',session('shop_id'))
->where('w.type',0)
->where('s.product_id',$value['id'])
->where('s.type','出库')
->where('s.status',1)
->sum('s.product_num');
$data['data'][$key]['pei'] = Db::name('stock')->alias('s')
->join('hisi_warehouse w', 'w.id=s.warehouse_id')
->where('s.shop_id',session('shop_id'))
->where('w.type',1)
->where('s.product_id',$value['id'])
->where('s.type','出库')
->where('s.status',1)
->sum('s.product_num');
}
$data['count'] = ProductModel::where($where)->count('id');
$data['code'] = 0;
return json($data);
}
return $this->fetch();
}
public function decompose()
{
if ($this->request->isAjax()) {
$where = [];
$page = $this->request->param('page/d', 1);
$limit = $this->request->param('limit/d', 15);
$user_id = $this->request->param('user_id/d', '');
$product_id = $this->request->param('product_id/d', '');
$detailed_type = $this->request->param('detailed_type/d', '');
$start = $this->request->param('start', '2000-01-01');
$end = $this->request->param('end', date('Y-m-d H:i:s',time()));
if(!empty($user_id)){
$where['user_id'] = $user_id;
}
if(!empty($product_id)){
$where['product_id'] = $product_id;
}
if(!empty($detailed_type)){
$where['detailed_type'] = $detailed_type;
}
if(empty($start)){
$start = '2000-01-01';
}
if(empty($end)){
$end = date('Y-m-d H:i:s',time());
}
$where['status'] = 1;
$where['shop_id'] = session('shop_id');
$warehouse = WarehouseModel::where('shop_id',session('shop_id'))->where('type', 1)->find();
$where['warehouse_id'] = $warehouse['id'];
$data['data'] = StockModel::where($where)
->field('*, detailed_type as detailed_type_text,user_id as user_name,warehouse_id as warehouse_text')
->whereTime('createtime', [strtotime($start), strtotime($end)])
->page($page)
->limit($limit)
->order('id desc')
->select();
$data['count'] = StockModel::where($where)->whereTime('createtime', [strtotime($start), strtotime($end)])->count('id');
$data['code'] = 0;
return json($data);
}
return $this->fetch('index');
}
public function always()
{
if ($this->request->isAjax()) {
$where = [];
$page = $this->request->param('page/d', 1);
$limit = $this->request->param('limit/d', 15);
$user_id = $this->request->param('user_id/d', '');
$product_id = $this->request->param('product_id/d', '');
$warehouse_id = $this->request->param('warehouse_id/d', '');
$detailed_type = $this->request->param('detailed_type/d', '');
$start = $this->request->param('start', '2000-01-01');
$end = $this->request->param('end', date('Y-m-d H:i:s',time()));
if(!empty($user_id)){
$where['user_id'] = $user_id;
}
if(!empty($product_id)){
$where['product_id'] = $product_id;
}
if(!empty($detailed_type)){
$where['detailed_type'] = $detailed_type;
}
if(empty($start)){
$start = '2000-01-01';
}
if(empty($end)){
$end = date('Y-m-d H:i:s',time());
}
if(!empty($warehouse_id)){
$where['warehouse_id'] = $warehouse_id;
}else{
$warehouses = WarehouseModel::where('shop_id',session('shop_id'))->where('type', 0)->column('id');
$where['warehouse_id'] = $warehouses;
}
$where['status'] = 1;
$where['shop_id'] = session('shop_id');
$data['data'] = StockModel::where($where)
->field('*, detailed_type as detailed_type_text,user_id as user_name')
->whereTime('createtime', [strtotime($start), strtotime($end)])
->page($page)
->limit($limit)
->order('id desc')
->select();
foreach ($data['data'] as $key => $value) {
$data['data'][$key]['warehouse_name'] = Db::name('warehouse')->where('id',$value['warehouse_id'])->value('name');
}
$data['count'] = StockModel::where($where)->whereTime('createtime', [strtotime($start), strtotime($end)])->count('id');
$data['code'] = 0;
return json($data);
}
return $this->fetch('index');
}
public function realinventory()
{
if ($this->request->isAjax()) {
$where = [];
$page = $this->request->param('page/d', 1);
$limit = $this->request->param('limit/d', 15);
$product_id = $this->request->param('product_id/d', '');
if(!empty($product_id)){
$where['id'] = $product_id;
}
$data['data'] = ProductModel::where($where)
->field('*, unit_id as unit_text, decomposition_id as decomposition_text')
->page($page)
->limit($limit)
->order('id desc')
->select();
foreach ($data['data'] as $key => $value) {
$data['data'][$key]['in'] = Db::name('stock')->alias('s')
->join('hisi_warehouse w', 'w.id=s.warehouse_id')
->where('s.shop_id',session('shop_id'))
->where('w.type',1)
->where('s.product_id',$value['id'])
->where('s.type','入库')
->where('s.status',1)
->sum('s.product_num');
$data['data'][$key]['in_price'] = Db::name('stock')->alias('s')
->join('hisi_warehouse w', 'w.id=s.warehouse_id')
->where('s.shop_id',session('shop_id'))
->where('w.type',1)
->where('s.product_id',$value['id'])
->where('s.type','入库')
->where('s.status',1)
->sum('s.price');
$data['data'][$key]['out'] = Db::name('stock')->alias('s')
->join('hisi_warehouse w', 'w.id=s.warehouse_id')
->where('s.shop_id',session('shop_id'))
->where('w.type',1)
->where('s.product_id',$value['id'])
->where('s.type','出库')
->where('s.status',1)
->sum('s.product_num');
$data['data'][$key]['out_price'] = Db::name('stock')->alias('s')
->join('hisi_warehouse w', 'w.id=s.warehouse_id')
->where('s.shop_id',session('shop_id'))
->where('w.type',1)
->where('s.product_id',$value['id'])
->where('s.type','出库')
->where('s.status',1)
->sum('s.price');
}
$data['count'] = ProductModel::where($where)->count('id');
$data['code'] = 0;
return json($data);
}
return $this->fetch();
}
public function realzong1()
{
if ($this->request->isAjax()) {
$where = [];
$page = $this->request->param('page/d', 1);
$limit = $this->request->param('limit/d', 15);
$product_id = $this->request->param('product_id/d', '');
$warehouse_id = $this->request->param('warehouse_id/d', '');
if(!empty($product_id)){
$where['id'] = $product_id;
}
if(!empty($warehouse_id)){
$where['warehouse_id'] = $warehouse_id;
}
$data['data'] = Db::name('stock')->alias('s')->join('warehouse w','w.id=s.warehouse_id')
->field('s.id,s.product_id,SUM(s.product_num) as product_num,s.product_name,s.warehouse_id,s.type as stype,w.name as wname')
->where($where)
->where('s.status',1)
->where('s.shop_id',session('shop_id'))
->group('s.product_id,s.warehouse_id,s.type')
->select();
halt($data['data']);
$data['count'] = ProductModel::where($where)->count('id');
$data['code'] = 0;
return json($data);
}
return $this->fetch();
}
public function realzong()
{
if ($this->request->isAjax()) {
$where = [];
$where2 = [];
$page = $this->request->param('page/d', 1);
$limit = $this->request->param('limit/d', 15);
$product_id = $this->request->param('product_id/d', '');
$warehouse_id = $this->request->param('warehouse_id/d', '');
if(!empty($product_id)){
$where['id'] = $product_id;
}
$warehouses = Db::name('warehouse')->where('shop_id',session('shop_id'))->column('id');
$data['data'] = ProductModel::where($where)
->field('*, unit_id as unit_text, decomposition_id as decomposition_text')
->page($page)
->limit($limit)
->order('id desc')
->select();
foreach ($data['data'] as $key => $value) {
$data['data'][$key]['in'] = Db::name('stock')->alias('s')
->join('hisi_warehouse w', 'w.id=s.warehouse_id')
->where('s.shop_id',session('shop_id'))
->where('s.warehouse_id',$warehouse_id)
->where('s.product_id',$value['id'])
->where('s.type','入库')
->where('s.status',1)
->sum('s.product_num');
$data['data'][$key]['in_price'] = Db::name('stock')->alias('s')
->join('hisi_warehouse w', 'w.id=s.warehouse_id')
->where('s.shop_id',session('shop_id'))
->where('w.type',0)
->where('s.product_id',$value['id'])
->where('s.type','入库')
->where('s.status',1)
->sum('s.price');
$data['data'][$key]['out'] = Db::name('stock')->alias('s')
->join('hisi_warehouse w', 'w.id=s.warehouse_id')
->where('s.shop_id',session('shop_id'))
->where('w.type',0)
->where('s.product_id',$value['id'])
->where('s.type','出库')
->where('s.status',1)
->sum('s.product_num');
$data['data'][$key]['out_price'] = Db::name('stock')->alias('s')
->join('hisi_warehouse w', 'w.id=s.warehouse_id')
->where('s.shop_id',session('shop_id'))
->where('w.type',0)
->where('s.product_id',$value['id'])
->where('s.type','出库')
->where('s.status',1)
->sum('s.price');
}
$data['count'] = ProductModel::where($where)->count('id');
$data['code'] = 0;
return json($data);
}
return $this->fetch();
}
}