RejectedBill.php 4.7 KB

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