InventoryAccount.php 2.5 KB

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