Inventory.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App;
  3. use App\Traits\ModelTimeFormat;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. class Inventory extends Model
  7. {
  8. use ModelTimeFormat;
  9. use SoftDeletes;
  10. protected $fillable=[
  11. 'id','owner_id','type', 'start_at', 'end_at','total','processed','difference','returned','deleted_at','created_at',
  12. ];
  13. protected $appends = [
  14. 'surplus','check_surplus'
  15. ];
  16. public function owner(){
  17. return $this->belongsTo('App\Owner','owner_id','id');
  18. }
  19. public function inventoryMissions(){
  20. return $this->belongsTo('App\InventoryMission','id','inventory_id');
  21. }
  22. public function getSurplusAttribute()
  23. {
  24. return $this['total'] ? $this['total']-$this['processed']:0;
  25. }
  26. public function getProcessedAmount(){
  27. return $this->inventoryMissions()->where('checked','是')->where('inventory_id',$this['id'])->count();
  28. }
  29. //复盘剩余数
  30. public function getCheckSurplusAttribute()
  31. {
  32. $re_checked_amount=$this->inventoryMissions()->where('inventory_id',$this['id'])->whereNotNull('re_checked_amount')->count();
  33. return $this['total'] ? $this['total']-$re_checked_amount:null;
  34. }
  35. public function getDifferenceAmount(){
  36. return $this->inventoryMissions()->where('inventory_id',$this['id'])->where('difference_amount','>',0)->count();
  37. }
  38. public function getReturnedAmount(){
  39. return $this->inventoryMissions()->where('inventory_id',$this['id'])->where('returned','是')->count();
  40. }
  41. }