admin/addon/stock/shop/controller/Stock.php

578 lines
20 KiB
PHP

<?php
/**
* ThinkShop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.cdcloudshop.com
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
* =========================================================
*/
namespace addon\stock\shop\controller;
use addon\stock\model\stock\Allot;
use addon\stock\model\stock\Document;
use addon\stock\model\stock\Inventory;
use addon\stock\model\stock\Stock as StockModel;
use addon\stock\model\Store;
use app\model\goods\GoodsCategory as GoodsCategoryModel;
use app\shop\controller\BaseShop;
/**
* 库存管理
* Class Goods
*/
class Stock extends BaseShop
{
/**
* 库存列表
* @return mixed
*/
public function manage()
{
if (request()->isAjax()) {
$page = input('page', 1);
$page_size = input('page_size', PAGE_LIST_ROWS);
$search = input('search_text', '');//名称或编码
$category_id = input('category_id', '');//todo 也可以多选
$min_stock = input('min_stock', 0);
$max_stock = input('max_stock', 0);
$store_id = input('store_id', 0);
$field = 'gs.stock,gs.*,g.unit';
$join = array (
[ 'goods g', 'g.goods_id = gs.goods_id', 'left' ],
);
$condition = [];
$condition[] = [ 'gs.site_id', '=', $this->site_id ];
$condition[] = [ 'g.is_delete', '=', 0 ];
$condition[] = [ 'g.goods_class', '=', 1 ];
if (!empty($search)) {
$condition[] = [ 'gs.sku_name|sku_no', 'like', '%' . $search . '%' ];
}
if (!empty($category_id)) {
$condition[] = [ 'g.category_id', 'like', '%,' . $category_id . ',%' ];
}
if ($store_id > 0) {
$stock_alias = 'sgs.real_stock';
$join[] = [
'store_goods_sku sgs',
'sgs.sku_id = gs.sku_id and (sgs.store_id is null or sgs.store_id = ' . $store_id . ')',
'left'
];
$field .= ',sgs.stock, sgs.real_stock';
}else{
$stock_alias = 'gs.real_stock';
$join[] = [
'store_goods_sku sgs',
'sgs.sku_id = gs.sku_id or sgs.sku_id is null',
'left'
];
$field .= ',gs.stock, sum(sgs.real_stock) as real_stock';
$group = 'gs.sku_id';
}
if ($min_stock > 0 && $max_stock > 0) {
$condition[] = [ $stock_alias, 'between', [ $min_stock, $max_stock ] ];
} else if ($min_stock > 0 && $max_stock == 0) {
$condition[] = [ $stock_alias, '>=', $min_stock ];
} else if ($min_stock == 0 && $max_stock > 0) {
$condition[] = [ $stock_alias, '<=', $max_stock ];
}
$goods_model = new \app\model\goods\Goods();
$list = $goods_model->getGoodsSkuPageList($condition, $page, $page_size, 'g.create_time desc', $field, 'gs', $join, $group ?? null);
return $list;
} else {
//获取一级商品分类
$goods_category_model = new GoodsCategoryModel();
$condition = [
[ 'pid', '=', 0 ],
[ 'site_id', '=', $this->site_id ]
];
$goods_category_list = $goods_category_model->getCategoryList($condition, 'category_id,category_name,level,commission_rate')[ 'data' ];
$this->assign('goods_category_list', $goods_category_list);
$this->assign('store_list', (new Store)->getStoreList($this->site_id)['data'] ?? []);
return $this->fetch('stock/manage');
}
}
/**
* 入库管理(应该豁免盘点)
* @return mixed
*/
public function storage()
{
if (request()->isAjax()) {
$document_model = new Document();
$page = input('page', 1);
$page_size = input('page_size', PAGE_LIST_ROWS);
$store_id = !empty(input('store_id', 0)) ? input('store_id', 0) : $this->store_id;
$condition = array (
[ 'site_id', '=', $this->site_id ],
[ 'type', '=', 'input' ]
);
if ($store_id > 0) {
$condition[] = [ 'store_id', 'in', $store_id ];
}
$result = $document_model->getDocumentPageList($condition, $page, $page_size, 'create_time desc');
return $result;
} else {
$this->assign('store_list', (new Store)->getStoreList($this->site_id)['data'] ?? []);
return $this->fetch('stock/storage');
}
}
/**
* 创建入库单
*/
public function stockin()
{
if (request()->isAjax()) {
$stock_json = input('stock_json', '');
$stock_array = json_decode($stock_json, true);
$store_id = input('store_id', 0);
$document_model = new Document();
$result = $document_model->addPurchase([
'site_id' => $this->site_id,
'store_id' => $store_id,
'user_info' => $this->user_info,
'goods_sku_list' => $stock_array,
]);
return $result;
} else {
$this->assign('store_list', (new Store)->getStoreList($this->site_id)['data'] ?? []);
return $this->fetch('stock/stockin');
}
}
/**
* 入库单详情
*/
public function inputDetail()
{
$document_id = intval(input('document_id', 0));
$document_model = new Document();
$condition = array (
[ 'site_id', '=', $this->site_id ],
[ 'document_id', '=', $document_id ],
[ 'type', '=', 'input' ]
);
$document_detail = $document_model->getDocumentInfo($condition)[ 'data' ] ?? [];
$this->assign('document_detail', $document_detail);
return $this->fetch('stock/input_detail');
}
/**
* 出库管理(应该豁免盘点)
* @return mixed
*/
public function wastage()
{
if (request()->isAjax()) {
$document_model = new Document();
$page = input('page', 1);
$page_size = input('page_size', PAGE_LIST_ROWS);
$store_id = !empty(input('store_id', 0)) ? input('store_id', 0) : $this->store_id;
$condition = array (
[ 'site_id', '=', $this->site_id ],
[ 'type', '=', 'output' ]
);
if ($store_id > 0) {
$condition[] = [ 'store_id', 'in', $store_id ];
}
$result = $document_model->getDocumentPageList($condition, $page, $page_size, 'create_time desc');
return $result;
} else {
$this->assign('store_list', (new Store)->getStoreList($this->site_id)['data'] ?? []);
return $this->fetch('stock/wastage');
}
}
/**
* 创建出库单
*/
public function stockout()
{
if (request()->isAjax()) {
$stock_json = input('stock_json', '');
$store_id = input('store_id', 0);
$stock_array = json_decode($stock_json, true);
$document_model = new Document();
$document_params = array(
'site_id' => $this->site_id,
'store_id' => $store_id,
'user_info' => $this->user_info,
'goods_sku_list' => $stock_array,
'is_out_stock' => 1
);
$result = $document_model->addOtherOutput($document_params);
return $result;
} else {
$this->assign('store_list', (new Store)->getStoreList($this->site_id)['data'] ?? []);
return $this->fetch('stock/stockout');
}
}
/**
* 出库单详情
*/
public function outputDetail()
{
$document_id = input('document_id', 0);
$document_model = new Document();
$condition = array (
[ 'site_id', '=', $this->site_id ],
[ 'document_id', '=', $document_id ],
[ 'type', '=', 'output' ]
);
$document_detail = $document_model->getDocumentInfo($condition)[ 'data' ] ?? [];
$this->assign('document_detail', $document_detail);
return $this->fetch('stock/output_detail');
}
/**
* 库存盘点
* @return mixed
*/
public function check()
{
if (request()->isAjax()) {
$page = input('page', 1);
$page_size = input('page_size', PAGE_LIST_ROWS);
$store_id = !empty(input('store_id', 0)) ? input('store_id', 0) : $this->store_id;
$inventory_no = input('inventory_no', '');
$condition = array (
[ 'site_id', '=', $this->site_id ],
);
if ($store_id > 0) {
$condition[] = [ 'store_id', 'in', $store_id ];
}
if ($inventory_no) {
$condition[] = [ 'inventory_no', '=', $inventory_no ];
}
$inventory_model = new Inventory();
$list = $inventory_model->getInventoryPageList($condition, $page, $page_size, 'create_time desc');
return $list;
} else {
$this->assign('store_list', (new Store)->getStoreList($this->site_id)['data'] ?? []);
return $this->fetch('stock/check');
}
}
/**
* 库存盘点详情
*/
public function inventoryDetail()
{
$inventory_no = input('inventory_no', 0);
$inventory_model = new Inventory();
$condition = array (
[ 'site_id', '=', $this->site_id ],
[ 'inventory_no', '=', $inventory_no ]
);
$inventory_detail = $inventory_model->getInventoryInfo($condition)[ 'data' ] ?? [];
if (empty($inventory_detail)) {
$this->error("盘点单不存在");
}
$this->assign('inventory_detail', $inventory_detail);
return $this->fetch('stock/inventory_detail');
}
/**
*新增盘点记录
* @return mixed
*/
public function addCheck()
{
if (request()->isAjax()) {
$inventory_model = new Inventory();
$store_id = input('store_id', 0);
$stock_list = input('stock_json', '');//商品库存映照json {{'sku_id':1, 'stock' : 10, 'cost_price':10,'source':'来源'}}
$stock_list = json_decode($stock_list, true);
$params = array(
'site_id' => $this->site_id,
'store_id' => $store_id,
'sku_list' => $stock_list,
'user_info' => $this->user_info,
);
$result = $inventory_model->addInventory($params);
return $result;
} else {
$this->assign('store_list', (new Store)->getStoreList($this->site_id)['data'] ?? []);
return $this->fetch('stock/add_check');
}
}
/**
* 库存流水
*/
public function records()
{
$sku_id = input('sku_id', 0);
$document_model = new Document();
if (request()->isAjax()) {
$page = input('page', 1);
$page_size = input('page_size', PAGE_LIST_ROWS);
$type = input('type', '');
$start_time = input('start_time', 0);
$end_time = input('end_time', 0);
$store_id = input('store_id', 0);
$condition = array (
[ 'dg.site_id', '=', $this->site_id ],
);
if ($sku_id > 0) {
$condition[] = [ 'dg.goods_sku_id', '=', $sku_id ];
}
if (!empty($type)) {
$condition[] = [ 'dt.key', '=', $type ];
}
if($store_id > 0){
$condition[] = [ 'd.store_id', '=', $store_id ];
}
//注册时间
if ($start_time != '' && $end_time != '') {
$condition[] = [ 'dg.create_time', 'between', [ strtotime($start_time), strtotime($end_time) ] ];
} else if ($start_time != '' && $end_time == '') {
$condition[] = [ 'dg.create_time', '>=', strtotime($start_time) ];
} else if ($start_time == '' && $end_time != '') {
$condition[] = [ 'dg.create_time', '<=', strtotime($end_time) ];
}
$result = $document_model->getDocumentGoodsPageList($condition, $page, $page_size, 'dg.create_time desc');
return $result;
} else {
$type_list = $document_model->getDocumentTypeList()[ 'data' ] ?? [];
$this->assign('store_list', (new Store)->getStoreList($this->site_id)['data'] ?? []);
$this->assign('type_list', $type_list);
$this->assign('sku_id', $sku_id);
return $this->fetch('stock/records');
}
}
/**
* 商品规格列表(仅作临时用)
*/
public function getSkuList()
{
$search = input('search', '');
$goods_id = input('goods_id', '');
$store_id = input('store_id', 0);
$stock_model = new StockModel();
$condition = array (
[ 'gs.site_id', '=', $this->site_id ],
[ 'g.goods_class', '=', 1 ],
[ 'g.is_delete', '=', 0 ]
);
if (!empty($search)) {
$condition[] = [ 'gs.sku_name|gs.sku_no', 'like', '%' . $search . '%' ];
}
if (!empty($goods_id)) {
$condition[] = [ 'g.goods_id', '=', $goods_id ];
}
if ($store_id > 0) {
//查询商品支持门店(支持当前门店或全部)
$condition[] = ['g.sale_store', 'like', ["%,".$store_id.",%", "%all%"], 'or'];
// $condition[] = ['sgs.store_id', '=', $store_id];
}
$sku_list = $stock_model->getStoreGoodsSkuList($condition, 'gs.*,sgs.stock,sgs.real_stock,sgs.price,sgs.cost_price', 'gs.create_time desc', $store_id);
return $sku_list;
}
/**
* 商品规格详情
*/
public function getSkuInfo()
{
$search = input('search', '');
$goods_id = input('goods_id', '');
$store_id = input('store_id', 0);
$sku_id = input('sku_id', 0);
$goods_model = new StoreGoods();
$condition = array (
[ 'gs.site_id', '=', $this->site_id ],
[ 'g.goods_type', '=', '' ],
[ 'g.is_delete', '=', 0 ],
);
if (!empty($search)) {
$condition[] = [ 'gs.sku_name|gs.sku_no', 'like', '%' . $search . '%' ];
}
if (!empty($goods_id)) {
$condition[] = [ 'g.goods_id', '=', $goods_id ];
}
if (!empty($sku_id)) {
$condition[] = [ 'gs.sku_id', '=', $sku_id ];
}
$sku_list = $goods_model->getStoreGoodsSkuInfo($condition, 'gs.*,sgs.stock,sgs.real_stock', $store_id);
return $sku_list;
}
/**
* 商品详情
*/
public function getGoodsInfo()
{
$goods_id = input('goods_id', '');
$store_id = input('store_id', 0);
$goods_model = new StoreGoods();
$condition = array (
[ 'site_id', '=', $this->site_id ],
[ 'goods_id', '=', $goods_id ],
[ 'store_id', '=', $store_id ],
);
$info = $goods_model->getGoodsInfo($condition);
return $info;
}
/**
* 更新库存
*/
public function skuInput()
{
$params = array (
'site_id' => $this->site_id,
'sku_id' => input('sku_id', 0),
'goods_id' => input('goods_id', 0),
'store_id' => input('store_id', 0),
'key' => input('key', 0),
'remark' => input('remark', 0),
'goods_num' => input('goods_num', 0),
'goods_price' => input('goods_price', 0),
'user_info' => $this->user_info,
'time' => input('time', 0),
);
$stock_model = new StockModel();
$result = $stock_model->changeStock($params);
return $result;
}
/**
* 调拨单
* @return mixed
*/
public function allocate()
{
if (request()->isAjax()) {
$allot_model = new Allot();
$page = input('page', 1);
$page_size = input('page_size', PAGE_LIST_ROWS);
$output_store_id = input('output_store_id', 0);
$input_store_id = input('input_store_id', 0);
$allot_no = input('allot_no', '');
$condition = array (
[ 'site_id', '=', $this->site_id ],
);
if ($output_store_id > 0) {
$condition[] = [ 'output_store_id', '=', $output_store_id ];
}
if ($input_store_id > 0) {
$condition[] = [ 'input_store_id', '=', $input_store_id ];
}
if ($allot_no) {
$condition[] = [ 'allot_no', '=', $allot_no ];
}
$result = $allot_model->getStockAllotPageList($condition, $page, $page_size, 'create_time desc');
return $result;
} else {
$this->assign('store_list', (new Store)->getStoreList($this->site_id)['data'] ?? []);
return $this->fetch('stock/allocate');
}
}
/**
* 调拨记录
* @return mixed
*/
public function allotrecords()
{
$allot_id = input('allot_id', 0);
$allot_model = new Allot();
if (request()->isAjax()) {
$page = input('page', 1);
$page_size = input('page_size', PAGE_LIST_ROWS);
$output_store_id = input('output_store_id', 0);
$input_store_id = input('input_store_id', 0);
$allot_no = input('allot_no', '');
$condition = array (
[ 'site_id', '=', $this->site_id ],
);
if ($output_store_id > 0) {
$condition[] = [ 'output_store_id', '=', $output_store_id ];
}
if ($input_store_id > 0) {
$condition[] = [ 'input_store_id', '=', $input_store_id ];
}
if ($allot_no > 0) {
$condition[] = [ 'allot_no', '=', $allot_no ];
}
$result = $allot_model->getStockAllotPageList($condition, $page, $page_size, 'create_time desc');
return $result;
} else {
$condition = array (
[ 'site_id', '=', $this->site_id ],
[ 'allot_id', '=', $allot_id ],
);
$detail = $allot_model->getAllotInfo($condition)[ 'data' ] ?? [];
$this->assign('detail', $detail);
return $this->fetch('stock/allot_records');
}
}
/**
* 创建调拨单
*/
public function addAllocate()
{
if (request()->isAjax()) {
$allot_model = new Allot();
$data = [
'output_store_id' => input('output_store_id', 0),
'input_store_id' => input('input_store_id', 0),
'remark' => input('remark', ''),
'goods_sku_list' => input('goods_sku_list', '') ? json_decode(input('goods_sku_list'), true) : [],
'allot_time' => input('allot_time', '') ? date_to_time(input('allot_time'), true) : 0,
'operater' => $this->uid,
'site_id' => $this->site_id
];
$result = $allot_model->addAllot($data);
return $result;
} else {
$this->assign('store_list', (new Store)->getStoreList($this->site_id)['data'] ?? []);
$this->assign('start_time', date('Y-m-d'));
$this->assign('default_time', date('Y-m-d H:i:s'));
return $this->fetch('stock/add_allocate');
}
}
}