Waybill.php 5.1 KB

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