RejectedBill.php 4.8 KB

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