InventoryAccount.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App;
  3. use App\Traits\ModelTimeFormat;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. use Illuminate\Support\Facades\Auth;
  7. class InventoryAccount extends Model
  8. {
  9. use ModelTimeFormat;
  10. use SoftDeletes;
  11. protected $fillable=[
  12. 'id','owner_id','type', 'start_at', 'end_at','total','processed','difference','returned','deleted_at','created_at','status','remark','auditor','ignored'
  13. ];
  14. protected $appends = [
  15. 'surplus','check_surplus'
  16. ];
  17. public function creator(){
  18. return $this->hasOne('App\Sign','signable_id','id')
  19. ->where('signable_type','inventory_accounts')->where('field','创建人');
  20. }
  21. public function owner(){
  22. return $this->belongsTo('App\Owner','owner_id','id');
  23. }
  24. public function userAuditor(){
  25. return $this->belongsTo('App\User','auditor','id');
  26. }
  27. public function inventoryMissions(){
  28. return $this->belongsTo('App\InventoryAccountMission','id','inventory_account_id');
  29. }
  30. public function getSurplusAttribute()
  31. {
  32. return $this['total'] ? $this['total']-($this['processed']+$this['ignored']):0;
  33. }
  34. public function getProcessedAmount(){
  35. return $this->inventoryMissions()->whereIn('checked',['是','确认差异','已复核'])->where('inventory_account_id',$this['id'])->count();
  36. }
  37. public function getIgnoredAmount(){
  38. return $this->inventoryMissions()->where('checked','跳过')->where('inventory_account_id',$this['id'])->count();
  39. }
  40. //复盘剩余数
  41. public function getCheckSurplusAttribute()
  42. {
  43. $re_checked_amount=$this->inventoryMissions()->where('inventory_account_id',$this['id'])->whereNotNull('re_checked_amount')->count();
  44. return $this['total'] ? $this['total']-$re_checked_amount:null;
  45. }
  46. public function getDifferenceAmount(){
  47. return $this->inventoryMissions()->where('inventory_account_id',$this['id'])->where('difference_amount','!=',0)->count();
  48. }
  49. //复盘归位
  50. public function getReturnedAmount(){
  51. return $this->inventoryMissions()->where('inventory_account_id',$this['id'])->where('returned','是')->count();
  52. }
  53. public function createSignCreator(){
  54. return Sign::query()->create([
  55. 'signable_type'=>'inventory_accounts',
  56. 'signable_id'=>$this['id'],
  57. 'field'=>'创建人',
  58. 'mark'=>Auth::user()['name'],
  59. ]);
  60. }
  61. }