RejectedBill.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App;
  3. use App\Http\Controllers\LogisticNumberFeatureController;
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Eloquent\Collection;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. use Overtrue\LaravelPinyin\Facades\Pinyin;
  9. /**
  10. * @method static find(array|string|null $param)
  11. */
  12. class RejectedBill extends Model
  13. {
  14. use SoftDeletes;
  15. protected $fillable=['id_owner','order_number','sender','mobile_sender',
  16. 'logistic_number','logistic_number_return','id_logistic_return',
  17. 'is_loaded','fee_collected','remark','id_operator','is_checked'
  18. ,'is_finished','checked_numbers','remark'];
  19. protected $appends = ['owner_name','logistic_name','created_at_short'
  20. ,'is_loaded_str','goods_amount','is_loaded_null','operator_name'];
  21. function owner(){
  22. return $this->hasOne('App\Owner','id','id_owner');
  23. }
  24. function logistic(){
  25. return $this->hasOne('App\Logistic','id','id_logistic_return');
  26. }
  27. function items(){
  28. return $this->hasMany('App\RejectedBillItem','id_rejected_bill','id');
  29. }
  30. function rejectedBillItems(){
  31. return $this->items();
  32. }
  33. function wmsReflectReceive(){
  34. return $this->belongsTo('App\WMSReflectReceive','id','id_rejected_bill');
  35. }
  36. function logisticName(){
  37. $logistic=$this->hasOne('App\Logistic','id','id_logistic_return')->first();
  38. return $logistic?$logistic['name']:'';
  39. }
  40. function makeCheckedNumbers(){
  41. $owner = $this->owner()->first();
  42. if(!$owner)return '';
  43. $pinyinOwnerName=Pinyin::convert($owner->name);
  44. $pinyinArr=array_map(function($pinyin){
  45. return $pinyin[0];
  46. },$pinyinOwnerName);
  47. $initials=implode("", $pinyinArr);
  48. $this['checked_numbers']=$initials.Carbon::now()->format('Ymd').$owner->getIncreasedCheckingCount();
  49. return $this['checked_numbers'];
  50. }
  51. function update(array $attributes = [], array $options = [])
  52. {
  53. (new LogisticNumberFeatureController())->createFeatures($this['logistic_number_return'],$this['id_logistic_return']);
  54. return parent::update($attributes, $options);
  55. }
  56. function save(array $options = [])
  57. {
  58. (new LogisticNumberFeatureController())->createFeatures($this['logistic_number_return'],$this['id_logistic_return']);
  59. return parent::save($options);
  60. }
  61. public function getOwnerNameAttribute(){
  62. $id_owner=$this['id_owner']??0;
  63. $owner=Owner::find($id_owner);
  64. return $this['owner_name']=$owner['name'];
  65. }
  66. public function getLogisticNameAttribute(){
  67. $id_logistic=$this['id_logistic_return']??0;
  68. $logistic=Logistic::find($id_logistic);
  69. return $this['logistic_name']=$logistic['name'];
  70. }
  71. public function getIsLoadedStrAttribute(){
  72. $val=$this['is_loaded']===null?'null':$this['is_loaded'];
  73. $val=$val===0?'0':$val;
  74. switch ($val){
  75. case 'null':return '无需入库';
  76. case '0':return '否';
  77. case 1:return '是';
  78. case 2:return '待推单';
  79. }
  80. return '';
  81. }
  82. public function getCreatedAtShortAttribute(){
  83. return $this['created_at']?$this['created_at_short']=$this['created_at']->format('m-d H:i:s'):'';
  84. }
  85. public function getGoodsAmountAttribute(){
  86. $amount=0;
  87. $this->items()->get()->each(function (RejectedBillItem $item)use(&$amount){
  88. $amount+=$item['amount'];
  89. });
  90. return $amount;
  91. }
  92. public function getOperatorNameAttribute(){
  93. if(!$this['id_operator']){return '';}
  94. $user=User::find($this['id_operator']);
  95. return $user['name'];
  96. }
  97. public function getIsLoadedNullAttribute(){
  98. if($this['is_loaded']===null) return 'null';
  99. return $this['is_loaded'];
  100. }
  101. public function changeIsLoaded_ifItemsAllLoaded(){
  102. $allItemsAreLoaded=true;
  103. $this->items->each(function (RejectedBillItem $item)use(&$allItemsAreLoaded){
  104. if($item['is_loaded']=='未入库'){
  105. $allItemsAreLoaded=false;
  106. return false;
  107. }
  108. });
  109. if($allItemsAreLoaded){
  110. $this['is_loaded']=1;
  111. }else{
  112. $this['is_loaded']=0;
  113. }
  114. $this->update();
  115. return $this['is_loaded'];
  116. }
  117. }