InventoryAccount.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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',
  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 inventoryMissions(){
  25. return $this->belongsTo('App\InventoryAccountMission','id','inventory_account_id');
  26. }
  27. public function getSurplusAttribute()
  28. {
  29. return $this['total'] ? $this['total']-$this['processed']:0;
  30. }
  31. public function getProcessedAmount(){
  32. return $this->inventoryMissions()->whereIn('checked',['是','跳过','确认差异','已复核'])->where('inventory_account_id',$this['id'])->count();
  33. }
  34. //复盘剩余数
  35. public function getCheckSurplusAttribute()
  36. {
  37. $re_checked_amount=$this->inventoryMissions()->where('inventory_account_id',$this['id'])->whereNotNull('re_checked_amount')->count();
  38. return $this['total'] ? $this['total']-$re_checked_amount:null;
  39. }
  40. public function getDifferenceAmount(){
  41. return $this->inventoryMissions()->where('inventory_account_id',$this['id'])->where('difference_amount','!=',0)->count();
  42. }
  43. //复盘归位
  44. public function getReturnedAmount(){
  45. return $this->inventoryMissions()->where('inventory_account_id',$this['id'])->where('returned','是')->count();
  46. }
  47. public function createSignCreator(){
  48. return Sign::query()->create([
  49. 'signable_type'=>'inventory_accounts',
  50. 'signable_id'=>$this['id'],
  51. 'field'=>'创建人',
  52. 'mark'=>Auth::user()['name'],
  53. ]);
  54. }
  55. }