| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- namespace App;
- use App\Traits\ModelTimeFormat;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- class Inventory extends Model
- {
- use ModelTimeFormat;
- use SoftDeletes;
- protected $fillable=[
- 'id','owner_id','type', 'start_at', 'end_at','total','processed','difference','returned','deleted_at','created_at',
- ];
- protected $appends = [
- 'surplus','check_surplus'
- ];
- public function owner(){
- return $this->belongsTo('App\Owner','owner_id','id');
- }
- public function inventoryMissions(){
- return $this->belongsTo('App\InventoryMission','id','inventory_id');
- }
- public function getSurplusAttribute()
- {
- return $this['total'] ? $this['total']-$this['processed']:0;
- }
- public function getProcessedAmount(){
- return $this->inventoryMissions()->where('checked','是')->where('inventory_id',$this['id'])->count();
- }
- //复盘剩余数
- public function getCheckSurplusAttribute()
- {
- $re_checked_amount=$this->inventoryMissions()->where('inventory_id',$this['id'])->whereNotNull('re_checked_amount')->count();
- return $this['total'] ? $this['total']-$re_checked_amount:null;
- }
- public function getDifferenceAmount(){
- return $this->inventoryMissions()->where('inventory_id',$this['id'])->where('difference_amount','>',0)->count();
- }
- public function getReturnedAmount(){
- return $this->inventoryMissions()->where('inventory_id',$this['id'])->where('returned','是')->count();
- }
- }
|