Files
hyperf_service/app/Service/Admin/Depot/DepotService.php
LAPTOP-7SGDREK0\shiweijun b139ea8db4 fix:depot
2025-02-28 09:59:04 +08:00

872 lines
33 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Service\Admin\Depot;
use App\Constants\Admin\DepotCode;
use App\Constants\Common\MaterialCode;
use App\Constants\Common\RoleCode;
use App\Exception\ErrException;
use App\Extend\DateUtil;
use App\Model\Depot;
use App\Model\DepotPurchase;
use App\Model\DepotRecycle;
use App\Model\DepotSale;
use App\Model\Material;
use App\Model\MaterialApplication;
use App\Model\MaterialStock;
use App\Model\Supplier;
use App\Service\Admin\BaseService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
class DepotService extends BaseService{
/**
* @var Depot
*/
#[Inject]
protected Depot $DepotModel;
#[Inject]
protected Material $MaterialModel;
#[Inject]
protected Supplier $SupplierModel;
#[Inject]
protected MaterialStock $MaterialStockModel;
#[Inject]
protected DepotPurchase $DepotPurchaseModel;
#[Inject]
protected DepotSale $DepotSaleModel;
#[Inject]
protected DepotRecycle $DepotRecycleModel;
#[inject]
protected MaterialApplication $MaterialApplicationModel;
public function handle()
{
}
/**
* @return array
*/
public function depotList():array
{
$limit = (int)$this->request->input('limit', 10);
$id = (int)$this->request->input('query_id');
$kitchenId = (int)$this->request->input('query_kitchen_id');
$list = $this->DepotModel
->where('is_del',DepotCode::IS_NO_DEL)
->when($id,function ($query) use ($id) {
$query->where('id',$id);
})
->when($kitchenId,function ($query) use ($kitchenId) {
$query->where('kitchen_id',$kitchenId);
})
->paginate($limit)->toArray();
return $this->return->success('success',$list);
}
/**
* @return array
*/
public function add():array
{
$name = $this->request->input('name');
$kitchen_id = (int)$this->request->input('kitchen_id');
$info = $this->DepotModel->getInfoByName($name,$kitchen_id);
if (!empty($info)) throw new ErrException('仓库已存在');
$depot = new Depot();
$depot->name = $name;
$depot->city_id = $this->request->input('city_id');
$depot->kitchen_id = $kitchen_id;
if (!$depot->save()) throw new ErrException('仓库添加失败');
return $this->return->success();
}
/**
* @return array
*/
public function edit(): array
{
$id = (int)$this->request->input('id');
$depotName = $this->request->input('name');
$kitchen_id = (int)$this->request->input('kitchen_id');
$info = $this->DepotModel->getInfoById($id);
if (empty($info)) throw new ErrException('数据不存在');
$name = $this->DepotModel->getInfoByName($depotName,$kitchen_id);
if (!empty($name)){
if ($name->id != $info->id && $info->kitchen_id == $kitchen_id)
throw new ErrException('仓库已存在');
}
$info->name = $depotName;
if (!$info->save()) throw new ErrException('仓库修改失败');
return $this->return->success();
}
/**
* @return array
*/
public function delete(): array
{
$id = (int)$this->request->input('id');
$info = $this->DepotModel->getInfoById($id);
if (empty($info)) throw new ErrException('仓库不存在');
$info->is_del = DepotCode::IS_DEL;
if (!$info->save()) throw new ErrException('删除失败');
return $this->return->success();
}
public function purchase():array
{
Db::beginTransaction();
try {
$depotId = (int)$this->request->input('depot_id');
$materialId = (int)$this->request->input('material_id');
$supplierId = (int)$this->request->input('supplier_id');
$purchase_price = $this->request->input('purchase_price');
$number = $this->request->input('number');
if (!empty($purchase_price) && !empty($number)){
$sum_price = bcmul($purchase_price,$number,2);
}
$cityId = (int)$this->request->input('city_id');
$kitchenId = (int)$this->request->input('kitchen_id');
$depotPurchase = new DepotPurchase();
$depotPurchase->depot_id = $depotId;
$depotPurchase->material_id = $materialId;
$depotPurchase->supplier_id = $supplierId;
$depotPurchase->type = 1;
$depotPurchase->purchase_price = $purchase_price;
$depotPurchase->number = $number;
$depotPurchase->sum_price = $sum_price;
$depotPurchase->status = DepotCode::INPUT;
$depotPurchase->city_id = $cityId;
$depotPurchase->kitchen_id = $kitchenId;
$depotPurchase->operator_id = $this->adminId;
$materialStock = $this->MaterialStockModel
->where('depot_id',$depotId)
->where('material_id',$materialId)
->where('supplier_id',$supplierId)
->first();
if (empty($materialStock)){
$materialStock = new MaterialStock();
$materialStock->depot_id = $depotId;
$materialStock->material_id = $materialId;
$materialStock->supplier_id = $supplierId;
$materialStock->current_stock = $number;
$materialStock->unit_price = $purchase_price;
}
else{
//库存增加
// $totalValue = bcadd(bcmul($purchase_price,$number),bcmul($materialStock->unit_price,$materialStock->current_stock),2);
$materialStock->current_stock = bcadd($materialStock->current_stock,$number,2);
// $materialStock->unit_price = bcdiv($totalValue,$materialStock->current_stock,2);
$materialStock->unit_price = $purchase_price;
}
if (!$depotPurchase->save() || !$materialStock->save()) throw new ErrException('采购添加失败');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}
public function purchaseUpdate():array
{
Db::beginTransaction();
try {
$id = (int)$this->request->input('id');
$info = $this->DepotPurchaseModel->getDepotPurchase($id);
$old_number = $info->number;
$number = $this->request->input('number');
$sum_price = bcmul($info->purchase_price,$number,2);
$info->number = $number;
$info->sum_price = $sum_price;
$materialStock = $this->MaterialStockModel
->where('depot_id',$info->depot_id)
->where('material_id',$info->material_id)
->where('supplier_id',$info->supplier_id)
->first();
$materialStock->current_stock = bcsub(bcadd($materialStock->current_stock,$number,2),$old_number,2);
if (!$info->save() || !$materialStock->save()) throw new ErrException('采购数量修改失败');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}
public function purchaseBack():array
{
Db::beginTransaction();
try {
$id = (int)$this->request->input('id');
// $number = (double)$this->request->input('number');
$info = $this->DepotPurchaseModel->getDepotPurchase($id);
$info->status = DepotCode::OUTPUT;
if (!empty($info)){
$depotPurchase = new DepotPurchase();
$depotPurchase->depot_id = $info->depot_id;
$depotPurchase->material_id = $info->material_id;
$depotPurchase->supplier_id = $info->supplier_id;
$depotPurchase->type = 2;
$depotPurchase->purchase_price = $info->purchase_price;
// if (empty($number)){
$depotPurchase->number = $info->number;
// }
// else{
// if ($info->number >= $number)
// $depotPurchase->number = $number;
// else
// throw new ErrException('采购退货数量不能大于进货数量');
// }
$depotPurchase->sum_price = bcmul($depotPurchase->purchase_price,$depotPurchase->number,2);
$depotPurchase->city_id = $info->city_id;
$depotPurchase->kitchen_id = $info->kitchen_id;
$depotPurchase->operator_id = $this->adminId;
}
$materialStock = $this->MaterialStockModel
->where('depot_id',$depotPurchase->depot_id)
->where('material_id',$depotPurchase->material_id)
->where('supplier_id',$depotPurchase->supplier_id)
->first();
if (empty($materialStock))
throw new ErrException('库存更改异常');
else{
if ($materialStock->current_stock < $depotPurchase->number)
throw new ErrException('库存数量小于退货数量');
//库存减少
$materialStock->current_stock = bcsub($materialStock->current_stock,$depotPurchase->number,2);
}
if (!$depotPurchase->save() || !$materialStock->save() || !$info->save()) throw new ErrException('采购退货异常');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}
public function purchaseList():array
{
$limit = (int)$this->request->input('limit', 10);
$id = (int)$this->request->input('query_id');
$type = (int)$this->request->input('type');
$kitchenId = (int)$this->request->input('query_kitchen_id');
$list = $this->DepotPurchaseModel
->leftJoin('material','depot_purchase.material_id','=','material.id')
->leftJoin('supplier','depot_purchase.supplier_id','=','supplier.id')
->leftJoin('depot','depot_purchase.depot_id','=','depot.id')
->where('depot_purchase.is_del',DepotCode::IS_NO_DEL)
->when($id,function ($query) use ($id) {
$query->where('depot_purchase.id',$id);
})
->when(!empty($type), function ($query) use ($type) {
$query->where('type', $type);
})
->when($kitchenId,function ($query) use ($kitchenId) {
$query->where('depot_purchase.kitchen_id',$kitchenId);
})
->orderBy('depot_purchase.create_time','desc')
->paginate($limit,['depot_purchase.*','material.name as material_name','supplier.name as supplier_name','depot.name as depot_name'])
->toArray();
return $this->return->success('success',$list);
}
public function purchaseStatistics():array
{
$cityId = (int)$this->request->input('city_id');
//今日采购额
$todayStartDate = DateUtil::getTodayStartDate();
$todayEndDate = DateUtil::getTodayEndDate();
$todayPurchase = $this->DepotPurchaseModel
->where('is_del',DepotCode::IS_NO_DEL)
->where('type',1)
->where('status',DepotCode::INPUT)
->where('city_id',$cityId)
->whereBetween('create_time',[$todayStartDate,$todayEndDate])
->sum('sum_price');
//昨天采购额
$yesterdayStartDate = DateUtil::getStartDate();
$yesterdayEndDate = DateUtil::getEndDate();
$yesterdayPurchase = $this->DepotPurchaseModel
->where('is_del',DepotCode::IS_NO_DEL)
->where('type',1)
->where('status',DepotCode::INPUT)
->where('city_id',$cityId)
->whereBetween('create_time',[$yesterdayStartDate,$yesterdayEndDate])
->sum('sum_price');
//本月采购额
$currentMonthStartDate = date("Y-m-01 00:00:00");
$monthPurchase = $this->DepotPurchaseModel
->where('is_del',DepotCode::IS_NO_DEL)
->where('type',1)
->where('status',DepotCode::INPUT)
->where('city_id',$cityId)
->whereBetween('create_time',[$currentMonthStartDate,$todayEndDate])
->sum('sum_price');
//今年采购额
$currentYearStartDate = date("Y-01-01 00:00:00");
$yearPurchase = $this->DepotPurchaseModel
->where('is_del',DepotCode::IS_NO_DEL)
->where('type',1)
->where('status',DepotCode::INPUT)
->where('city_id',$cityId)
->whereBetween('create_time',[$currentYearStartDate,$todayEndDate])
->sum('sum_price');
return $this->return->success('success',["todayPurchase"=>$todayPurchase,"yesterdayPurchase"=>$yesterdayPurchase,
"monthPurchase"=>$monthPurchase,"yearPurchase"=>$yearPurchase]);
}
public function depotSale():array
{
Db::beginTransaction();
try {
$depotId = (int)$this->request->input('depot_id');
$materialId = (int)$this->request->input('material_id');
$supplierId = (int)$this->request->input('supplier_id');
$number = $this->request->input('number');
$applicationId = (int)$this->request->input('application_id');
$cityId = (int)$this->request->input('city_id');
$kitchenId = (int)$this->request->input('kitchen_id');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
$applicationInfo = $this->MaterialApplicationModel
->where('id',$applicationId)
->first();
$materialStock = $this->MaterialStockModel
->where('depot_id',$depotId)
->where('material_id',$materialId)
->where('supplier_id',$supplierId)
->first();
$depotSale = new DepotSale();
$depotSale->depot_id = $depotId;
$depotSale->dish_id = $applicationInfo->dish_id;
$depotSale->material_id = $materialId;
$depotSale->supplier_id = $supplierId;
$depotSale->sale_price = $materialStock->unit_price;
$depotSale->number = $number;
$depotSale->sum_price = bcmul($depotSale->sale_price,$number,2);
$depotSale->application_id = $applicationId;
$depotSale->city_id = $cityId;
$depotSale->kitchen_id = $kitchenId;
$depotSale->operator_id = $this->adminId;
//库存减少
$materialStock->current_stock = bcsub($materialStock->current_stock,$number,2);
$applicationInfo->al_number = bcadd($number,$applicationInfo->al_number,2);
if ($applicationInfo->al_number < $applicationInfo->number)
$applicationInfo->status = MaterialCode::PART_OUT;
else
$applicationInfo->status = MaterialCode::ALL_OUT;
if (!$depotSale->save() || !$materialStock->save() ||!$applicationInfo->save()) throw new ErrException('商品出库异常');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}
public function saleUpdate():array
{
Db::beginTransaction();
try {
$id = (int)$this->request->input('id');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
$info = $this->DepotSaleModel
->where('id',$id)
->first();
$applicationInfo = $this->MaterialApplicationModel
->where('id',$info->application_id)
->first();
$old_number = $info->number;
$number = $this->request->input('number');
$sum_price = bcmul($info->sale_price,$number,2);
$info->number = $number;
$info->sum_price = $sum_price;
$materialStock = $this->MaterialStockModel
->where('depot_id',$info->depot_id)
->where('material_id',$info->material_id)
->where('supplier_id',$info->supplier_id)
->first();
$materialStock->current_stock = bcadd($materialStock->current_stock,bcsub($old_number,$number,2),2);
$applicationInfo->al_number = bcadd($applicationInfo->al_number,bcsub($number,$old_number,2),2);
if ($applicationInfo->al_number < $applicationInfo->number)
$applicationInfo->status = MaterialCode::PART_OUT;
else
$applicationInfo->status = MaterialCode::ALL_OUT;
if (!$info->save() || !$materialStock->save() || !$applicationInfo->save()) throw new ErrException('商品出库数量修改失败');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}
public function saleDelete():array
{
Db::beginTransaction();
try {
$id = (int)$this->request->input('id');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
$info = $this->DepotSaleModel
->where('is_del',MaterialCode::IS_NO_DEL)
->where('id',$id)
->first();
$applicationInfo = $this->MaterialApplicationModel
->where('id',$info->application_id)
->first();
$applicationInfo->al_number = bcsub($applicationInfo->al_number,$info->number,2);
if (bccomp($applicationInfo->al_number,'0') <= 0)
$applicationInfo->status = MaterialCode::AUDITED;
else if ($applicationInfo->al_number < $applicationInfo->number)
$applicationInfo->status = MaterialCode::PART_OUT;
$materialStock = $this->MaterialStockModel
->where('depot_id',$info->depot_id)
->where('material_id',$info->material_id)
->where('supplier_id',$info->supplier_id)
->first();
$materialStock->current_stock = bcadd($materialStock->current_stock,$info->number,2);
$info->is_del = MaterialCode::IS_DEL;
if (!$applicationInfo->save() || !$materialStock->save() || !$info->save()) throw new ErrException('商品出库数量删除失败');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}
public function saleList():array
{
$limit = (int)$this->request->input('limit', 10);
$id = (int)$this->request->input('query_id');
$kitchenId = (int)$this->request->input('query_kitchen_id');
$list = $this->DepotSaleModel
->leftJoin('material','depot_sale.material_id','=','material.id')
->leftJoin('supplier','depot_sale.supplier_id','=','supplier.id')
->leftJoin('dish','depot_sale.dish_id','=','dish.id')
->leftJoin('depot','depot_sale.depot_id','=','depot.id')
->leftJoin('admin_user','depot_sale.operator_id','=','admin_user.id')
->where('depot_sale.is_del',MaterialCode::IS_NO_DEL)
->when($id,function ($query) use ($id) {
$query->where('depot_sale.id',$id);
})
->when($kitchenId,function ($query) use ($kitchenId) {
$query->where('depot_sale.kitchen_id',$kitchenId);
})
->paginate($limit,['depot_sale.*','material.name as material_name','supplier.name as supplier_name','dish.dish as dish_name','admin_user.chinese_name as operator_name','depot.name as depot_name'])
->toArray();
return $this->return->success('success',$list);
}
public function saleStatistics():array
{
$cityId = (int)$this->request->input('city_id');
//今日销售额
$todayStartDate = DateUtil::getTodayStartDate();
$todayEndDate = DateUtil::getTodayEndDate();
$todaySale = $this->DepotSaleModel
->where('is_del',DepotCode::IS_NO_DEL)
->where('city_id',$cityId)
->whereBetween('create_time',[$todayStartDate,$todayEndDate])
->sum('sum_price');
//昨天销售额
$yesterdayStartDate = DateUtil::getStartDate();
$yesterdayEndDate = DateUtil::getEndDate();
$yesterdaySale = $this->DepotPurchaseModel
->where('is_del',DepotCode::IS_NO_DEL)
->where('city_id',$cityId)
->whereBetween('create_time',[$yesterdayStartDate,$yesterdayEndDate])
->sum('sum_price');
//本月销售额
$currentMonthStartDate = date("Y-m-01 00:00:00");
$monthSale = $this->DepotSaleModel
->where('is_del',DepotCode::IS_NO_DEL)
->where('city_id',$cityId)
->whereBetween('create_time',[$currentMonthStartDate,$todayEndDate])
->sum('sum_price');
//今年销售额
$currentYearStartDate = date("Y-01-01 00:00:00");
$yearSale = $this->DepotSaleModel
->where('is_del',DepotCode::IS_NO_DEL)
->where('city_id',$cityId)
->whereBetween('create_time',[$currentYearStartDate,$todayEndDate])
->sum('sum_price');
return $this->return->success('success',["todaySale"=>$todaySale,"yesterdaySale"=>$yesterdaySale,
"monthSale"=>$monthSale,"yearSale"=>$yearSale]);
}
public function recycle():array
{
Db::beginTransaction();
try {
$materialId = (int)$this->request->input('material_id');
$supplierId = (int)$this->request->input('supplier_id');
$number = (double)$this->request->input('number');
$saleId = (int)$this->request->input('sale_id');
$cityId = (int)$this->request->input('city_id');
$kitchenId = (int)$this->request->input('kitchen_id');
$depotInfo = $this->DepotModel
->getInfoByName('回收仓库',$kitchenId);
if (empty($depotInfo))
throw new ErrException('未创建回收仓库');
else
$depotId = $depotInfo->id;
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
$saleInfo = $this->DepotSaleModel
->where('is_del',MaterialCode::IS_NO_DEL)
->where('id',$saleId)
->first();
$saleInfo->back_number = $saleInfo->back_number + $number;
$saleInfo->sum_price = $saleInfo->sale_price * ($saleInfo->number - $saleInfo->back_number);
if ($saleInfo->back_number > $saleInfo->number)
throw new ErrException('回收数量不能大于出库数量');
$materialStock = $this->MaterialStockModel
->where('material_id',$materialId)
->where('depot_id',$depotId)
->where('supplier_id',$supplierId)
->first();
$depotRecycle = new DepotRecycle();
$depotRecycle->depot_id = $depotId;
$depotRecycle->material_id = $materialId;
$depotRecycle->supplier_id = $supplierId;
$depotRecycle->recycle_price = $saleInfo->sale_price;
$depotRecycle->number = $number;
$depotRecycle->sum_price = $saleInfo->sale_price * $number;
$depotRecycle->sale_id = $saleId;
$depotRecycle->status = MaterialCode::AUDITED;
$depotRecycle->city_id = $cityId;
$depotRecycle->kitchen_id = $kitchenId;
$depotRecycle->operator_id = $this->adminId;
if (empty($materialStock)){
$materialStock = new MaterialStock();
$materialStock->depot_id = $depotId;
$materialStock->material_id = $materialId;
$materialStock->supplier_id = $supplierId;
$materialStock->current_stock = $number;
$materialStock->unit_price = $depotRecycle->recycle_price;
}
else
$materialStock->current_stock = $materialStock->current_stock + $number;
if (!$saleInfo->save() || !$depotRecycle->save() || !$materialStock->save()) throw new ErrException('回收发生异常');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}
public function recycleUpdate():array
{
Db::beginTransaction();
try {
$id = (int)$this->request->input('id');
$number = (double)$this->request->input('number');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
$info = $this->DepotRecycleModel
->where('id',$id)
->first();
$old_number = $info->number;
$sum_price = $info->recycle_price * $number;
$info->number = $number;
$info->sum_price = $sum_price;
$materialStock = $this->MaterialStockModel
->where('material_id',$info->material_id)
->where('depot_id',$info->depot_id)
->where('supplier_id',$info->supplier_id)
->first();
$materialStock->current_stock = $materialStock->current_stock + $number - $old_number;
$saleInfo = $this->DepotSaleModel
->where('id',$info->sale_id)
->first();
$saleInfo->back_number = $saleInfo->back_number + $number - $old_number;
$saleInfo->sum_price = $saleInfo->sale_price * ($saleInfo->number - $saleInfo->back_number);
if ($saleInfo->back_number > $saleInfo->number)
throw new ErrException('回收数量不能大于出库数量');
if (!$info->save() || !$materialStock->save() || !$saleInfo->save()) throw new ErrException('商品回收数量修改失败');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}
public function recycleDelete():array
{
Db::beginTransaction();
try {
$id = (int)$this->request->input('id');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
$info = $this->DepotRecycleModel
->where('id',$id)
->first();
if ($info->status == MaterialCode::AUDITED){
throw new ErrException('该回收已审核通过');
}
$info->is_del = MaterialCode::IS_DEL;
$materialStock = $this->MaterialStockModel
->where('material_id',$info->material_id)
->where('depot_id',$info->depot_id)
->where('supplier_id',$info->supplier_id)
->first();
$materialStock->current_stock = $materialStock->current_stock - $info->number;
$saleInfo = $this->DepotSaleModel
->where('id',$info->sale_id)
->first();
$saleInfo->back_number = $saleInfo->back_number - $info->number;
$saleInfo->sum_price = $saleInfo->sale_price * ($saleInfo->number - $saleInfo->back_number);
if (!$info->save() || !$materialStock->save() || !$saleInfo->save())
throw new ErrException('商品回收删除失败');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}
public function recycleList():array{
$limit = (int)$this->request->input('limit', 10);
$id = (int)$this->request->input('query_id');
$kitchenId = (int)$this->request->input('query_kitchen_id');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
$list = $this->DepotRecycleModel
->leftJoin('depot','depot_recycle.depot_id','=','depot.id')
->leftJoin('material','depot_recycle.material_id','=','material.id')
->leftJoin('supplier','depot_recycle.supplier_id','=','supplier.id')
->leftJoin('admin_user','depot_recycle.operator_id','=','admin_user.id')
->where('depot_recycle.is_del',MaterialCode::IS_NO_DEL)
->when($id,function ($query) use ($id) {
$query->where('depot_recycle.id',$id);
})
->when($kitchenId > 0,function($query) use($kitchenId){
$query->where('depot_recycle.kitchen_id',$kitchenId);
})
->paginate($limit,['depot_recycle.*','material.name as material_name','supplier.name as supplier_name','admin_user.chinese_name as operator_name','depot.name as depot_name'])
->toArray();
return $this->return->success('success',$list);
}
public function recycleAudit():array
{
Db::beginTransaction();
try {
$id = (int)$this->request->input('id');
$status = (int)$this->request->input('status');
if (RoleCode::WAREHOUSE != $this->roleId && RoleCode::SUPER_ADMIN != $this->roleId && RoleCode::ADMIN != $this->roleId)
throw new ErrException('该角色没有权限');
$info = $this->DepotRecycleModel
->where('id',$id)
->first();
if ($info->status != MaterialCode::AUDITED){
if ($status == MaterialCode::AUDITED){
$saleInfo = $this->DepotSaleModel
->where('is_del',MaterialCode::IS_NO_DEL)
->where('id',$info->sale_id)
->first();
if (empty($saleInfo)){
throw new ErrException('无出库记录');
}
$saleInfo->back_number = $saleInfo->back_number + $info->number;
$saleInfo->sum_price = $saleInfo->sale_price * ($saleInfo->number - $saleInfo->back_number);
$materialStock = $this->MaterialStockModel
->where('material_id',$info->material_id)
->where('depot_id',$info->depot_id)
->where('supplier_id',$info->supplier_id)
->first();
if (empty($materialStock)){
$materialStock = new MaterialStock();
$materialStock->depot_id = $info->depotId;
$materialStock->material_id = $info->materialId;
$materialStock->supplier_id = $info->supplierId;
$materialStock->current_stock = $info->number;
$materialStock->unit_price = $info->recycle_price;
}
else
$materialStock->current_stock = $materialStock->current_stock + $info->number;
if (!$materialStock->save() || !$saleInfo->save()) throw new ErrException('回收审核异常');
}
}
if ($info->status == MaterialCode::AUDITED){
if ($status == MaterialCode::UN_AUDIT || $status == MaterialCode::AUDIT_REFUSE){
$materialStock = $this->MaterialStockModel
->where('is_del',MaterialCode::IS_NO_DEL)
->where('material_id',$info->material_id)
->where('depot_id',$info->depot_id)
->where('supplier_id',$info->supplier_id)
->first();
$materialStock->current_stock = $materialStock->current_stock - $info->number;
$saleInfo = $this->DepotSaleModel
->where('id',$info->sale_id)
->first();
$saleInfo->back_number = $saleInfo->back_number - $info->number;
$saleInfo->sum_price = $saleInfo->sale_price * ($saleInfo->number - $saleInfo->back_number);
if (!$materialStock->save() || !$saleInfo->save()) throw new ErrException('回收审核异常');
}
}
$info->status = $status;
if (!$info->save()) throw new ErrException('回收审核异常');
DB::commit();
} catch (ErrException $error) {
Db::rollBack();
throw new ErrException($error->getMessage());
}
return $this->return->success();
}
}