作者 王晓刚

减库存算法

... ... @@ -188,12 +188,53 @@ class Goods extends Model
* @return mixed
*/
public function getDepot($goodsId,$areaId,$number){
$depot = $this->alias('g')
$arr = [];//仓库对应需扣除库存信息
//获取全部仓库库存
$sum_stock_num = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId, 'd.area_id' => $areaId])
->field('d.id,d.stock_num')
->sum('d.stock_num');
if($number > $sum_stock_num){
return '库存不足';
}
//获取当前省仓库库存
$province_depot = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId, 'd.area_id' => $areaId])
->field('d.id,d.stock_num')
->find();
$this->recursion($depot,'',$number,$goodsId);
$province_stock_num = !empty($province_depot['stock_num']) ? $province_depot['stock_num'] : 0;
$residue_number = $number - $province_stock_num;//还需多少库存
if($residue_number > 0){
//需要其它仓库提供库存
$arr1['area_id'] = $areaId;
$arr1['number'] = $province_stock_num;
$arr[] = $arr1;
$depots = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId, 'd.area_id' => ['not in'=>[$areaId]]])
->field('d.id,d.stock_num')->select();
foreach($depots as $key => $depot){
$arr1['area_id'] = $depot['area_id'];
if($depot['stock_num'] < $residue_number){
//还是不够
$arr1['number'] = $depot['stock_num'];
$arr[] = $arr1;
$residue_number = $residue_number - $depot['stock_num'];
}else{
//足够
$arr1['number'] = $residue_number;
$arr[] = $arr1;
}
}
}else{
$arr1['area_id'] = $areaId;
$arr1['number'] = $number;
$arr[] = $arr1;
}
return $arr;
/*$this->recursion($depot,'',$number,$goodsId);
if (!empty($depot) && $number == 0){
... ... @@ -201,7 +242,7 @@ class Goods extends Model
}else{
$depotid = Db::name('depot')->where('goods_id',$goodsId)->order('weigh DESC')->value('id');
return $depotid;
}
}*/
}
public function recursion($depot,$ids,$number,$goodsId){
... ...