Waybill.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Traits\ModelTimeFormat;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. use App\Traits\ModelLogChanging;
  9. use Illuminate\Support\Facades\DB;
  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. "is_to_pay", //0否 1是
  58. 'cargo_name',
  59. 'total_number',
  60. 'total_weight',
  61. 'deliveryType_id',
  62. "merge_owner",//合并货主ID集
  63. 'order_type',
  64. 'transport_type',
  65. 'pay_type',
  66. 'back_sign_bill',
  67. 'package_service',
  68. 'express_face_list',
  69. 'arrived_org_simple_name',
  70. 'much_higher_delivery',
  71. 'station_no',
  72. 'subjoin_fee',//附加费
  73. ];
  74. public function district()
  75. { //区
  76. return $this->belongsTo(Region::class,"district_id","id")->where("type",3);
  77. }
  78. public function owner(){
  79. return $this->hasOne('App\Owner','id','owner_id');
  80. }
  81. public function logistic(){
  82. return $this->hasOne('App\Logistic','id','logistic_id');
  83. }
  84. public function originationCity()
  85. { //始发市
  86. return $this->hasOne(Region::class,'id','origination_city_id');
  87. }
  88. public function destinationCity()
  89. { //目的市
  90. return $this->hasOne(Region::class,'id','destination_city_id');
  91. }
  92. public function wmsCommodities(){
  93. return $this->hasMany('App\WMSWaybillOrder','OrderNo','wms_bill_number');
  94. }
  95. public function priceModel(){
  96. return $this->hasOne('App\WaybillPriceModel','id','waybill_price_model_id');
  97. }
  98. public function amountUnit(){
  99. return $this->hasOne('App\Unit','id','amount_unit_id');
  100. }
  101. public function warehouseWeightUnit(){
  102. return $this->hasOne('App\Unit','id','warehouse_weight_unit_id');
  103. }
  104. public function carrierWeightUnit(){
  105. return $this->hasOne('App\Unit','id','carrier_weight_unit_id');
  106. }
  107. public function warehouseWeightUnitOther(){
  108. return $this->hasOne('App\Unit','id','warehouse_weight_unit_id_other');
  109. }
  110. public function carrierWeightUnitOther(){
  111. return $this->hasOne('App\Unit','id','carrier_weight_unit_id_other');
  112. }
  113. public function carType(){
  114. return $this->hasOne('App\CarType','id','carType_id');
  115. }
  116. public function waybillAuditLogs(){
  117. return $this->hasMany('App\WaybillAuditLog','waybill_id','id')->orderByDesc("created_at");
  118. }
  119. public function uploadFiles(){
  120. return $this->hasMany(UploadFile::class,'table_id','id')->where('table_name','waybills');
  121. }
  122. public function order()
  123. { //订单
  124. return $this->belongsTo(Order::class);
  125. }
  126. public function orderByCode()
  127. { //订单
  128. return $this->hasOne(Order::class,'code','wms_bill_number');
  129. }
  130. public function deliveryType()
  131. { //订单
  132. return $this->hasOne(DeliveryType::class,'id','deliveryType_id');
  133. }
  134. static public function setWeightByOrderCode($orderCode,$weight){
  135. if(empty($orderCode))return;
  136. $waybill=Waybill::query()->where('wms_bill_number',$orderCode)->where('status','!=','已完结')
  137. ->where('status','!=','无模型')->first();
  138. if ($waybill){
  139. $waybill->warehouse_weight_other=$weight;
  140. $waybill->warehouse_weight_unit_id_other=1;
  141. $waybill->update();
  142. }
  143. }
  144. /**
  145. * @return Builder
  146. */
  147. public static function filterAuthorities(){
  148. $ids=app('UserService')->getPermittingOwnerIds(auth()->user());
  149. return (new static)->newQuery()->leftJoin("orders","waybills.order_id","orders.id")
  150. ->leftJoin("owners","orders.owner_id","owners.id")
  151. ->selectRaw(<<<column
  152. owners.name as owner_name,
  153. orders.consignee_name as order_consignee_name,
  154. orders.consignee_phone as order_consignee_phone,
  155. orders.province as order_province,
  156. orders.city as order_city,
  157. orders.district as order_district,
  158. orders.address as order_address
  159. column
  160. )
  161. ->where(function ($query)use($ids){
  162. /** @var Builder $query */
  163. $query->whereIn('waybills.owner_id',$ids)->orWhereIn("orders.owner_id",$ids);
  164. });
  165. }
  166. public function ownerWayBillFeeDetail(): HasOne
  167. {
  168. return $this->hasOne(OwnerWayBillFeeDetail::class);
  169. }
  170. public function getDestinationAttribute($value)
  171. {
  172. return $this->relations["order"]["address"] ?? $value;
  173. }
  174. public function getCarrierNameAttribute()
  175. {
  176. return $this->relations["logistic"]["name"] ?? "";
  177. }
  178. public function getAmountUnitNameAttribute()
  179. {
  180. return $this->relations["amountUnit"]["name"] ?? "件";
  181. }
  182. public function getRemoveRelationAttribute()
  183. {
  184. $this->unsetRelations();
  185. $this->offsetUnset("remove_relation");
  186. }
  187. }