Waybill.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App;
  3. use App\Services\UserService;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Model;
  6. use App\Traits\ModelTimeFormat;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. use Illuminate\Support\Facades\Auth;
  9. use App\Traits\ModelLogChanging;
  10. class Waybill extends Model
  11. {
  12. use ModelLogChanging;
  13. use ModelTimeFormat;
  14. use SoftDeletes;
  15. protected $fillable=[
  16. 'status',
  17. 'type',
  18. 'waybill_number',
  19. 'owner_id',
  20. 'wms_bill_number',
  21. 'origination',
  22. 'destination',
  23. 'recipient',
  24. 'recipient_mobile',
  25. 'charge',
  26. 'ordering_remark',
  27. 'logistic_id',
  28. 'carrier_bill',
  29. 'origination_city_id',
  30. 'destination_city_id',
  31. 'warehouse_weight',
  32. 'warehouse_weight_unit_id',
  33. 'carrier_weight',
  34. 'carrier_weight_unit_id',
  35. 'carType_id',
  36. 'car_owner_info',
  37. 'fee',
  38. 'pick_up_fee',
  39. 'other_fee',
  40. 'collect_fee',
  41. 'dispatch_remark',
  42. 'waybill_price_model_id',
  43. 'warehouse_weight_other',
  44. 'warehouse_weight_unit_id_other',
  45. 'carrier_weight_other',
  46. 'carrier_weight_unit_id_other',
  47. 'source_bill',
  48. 'mileage',
  49. 'amount',
  50. 'inquire_tel',
  51. 'amount_unit_id',
  52. 'other_charge',
  53. 'other_charge_remark',
  54. 'deliver_at',
  55. "district_id",
  56. "order_id"
  57. ];
  58. public function district()
  59. { //区
  60. return $this->belongsTo(Region::class,"district_id","id")->where("type",3);
  61. }
  62. public function owner(){
  63. return $this->hasOne('App\Owner','id','owner_id');
  64. }
  65. public function logistic(){
  66. return $this->hasOne('App\Logistic','id','logistic_id');
  67. }
  68. public function originationCity()
  69. { //始发市
  70. return $this->hasOne(Region::class,'id','origination_city_id');
  71. }
  72. public function destinationCity()
  73. { //目的市
  74. return $this->hasOne(Region::class,'id','destination_city_id');
  75. }
  76. public function wmsCommodities(){
  77. return $this->hasMany('App\WMSWaybillOrder','OrderNo','wms_bill_number');
  78. }
  79. public function priceModel(){
  80. return $this->hasOne('App\WaybillPriceModel','id','waybill_price_model_id');
  81. }
  82. public function amountUnit(){
  83. return $this->hasOne('App\Unit','id','amount_unit_id');
  84. }
  85. public function warehouseWeightUnit(){
  86. return $this->hasOne('App\Unit','id','warehouse_weight_unit_id');
  87. }
  88. public function carrierWeightUnit(){
  89. return $this->hasOne('App\Unit','id','carrier_weight_unit_id');
  90. }
  91. public function warehouseWeightUnitOther(){
  92. return $this->hasOne('App\Unit','id','warehouse_weight_unit_id_other');
  93. }
  94. public function carrierWeightUnitOther(){
  95. return $this->hasOne('App\Unit','id','carrier_weight_unit_id_other');
  96. }
  97. public function carType(){
  98. return $this->hasOne('App\CarType','id','carType_id');
  99. }
  100. public function waybillAuditLogs(){
  101. return $this->hasMany('App\WaybillAuditLog','waybill_id','id');
  102. }
  103. public function uploadFile(){
  104. return $this->hasOne('App\UploadFile','table_id','id')->where('table_name','waybills');
  105. }
  106. public function order()
  107. { //订单
  108. return $this->belongsTo(Order::class);
  109. }
  110. static public function setWeightByOrderCode($orderCode,$weight){
  111. if(empty($orderCode))return;
  112. $waybill=Waybill::query()->where('wms_bill_number',$orderCode)->where('status','!=','已完结')
  113. ->where('status','!=','无模型')->first();
  114. if ($waybill){
  115. $waybill->warehouse_weight_other=$weight;
  116. $waybill->warehouse_weight_unit_id_other=1;
  117. $waybill->update();
  118. }
  119. }
  120. /**
  121. * @return Builder
  122. */
  123. public static function filterAuthorities(){
  124. $ids=app('UserService')->getPermittingOwnerIds(auth()->user());
  125. return (new static)->newQuery()->leftJoin("orders","waybills.order_id","orders.id")
  126. ->leftJoin("owners","orders.owner_id","owners.id")
  127. ->selectRaw(<<<column
  128. owners.name as owner_name,
  129. orders.consignee_name as order_consignee_name,
  130. orders.consignee_phone as order_consignee_phone,
  131. orders.province as order_province,
  132. orders.city as order_city,
  133. orders.district as order_district,
  134. orders.address as order_address
  135. column
  136. )
  137. ->where(function ($query)use($ids){
  138. /** @var Builder $query */
  139. $query->whereIn('waybills.owner_id',$ids)->orWhereIn("orders.owner_id",$ids);
  140. });
  141. }
  142. }