Waybill.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. class Waybill extends Model
  10. {
  11. use ModelLogChanging;
  12. use ModelTimeFormat;
  13. use SoftDeletes;
  14. protected $fillable=[
  15. 'status',
  16. 'type',
  17. 'waybill_number',
  18. 'owner_id',
  19. 'wms_bill_number',
  20. 'origination',
  21. 'destination',
  22. 'recipient',
  23. 'recipient_mobile',
  24. 'charge',
  25. 'ordering_remark',
  26. 'logistic_id',
  27. 'carrier_bill',
  28. 'origination_city_id',
  29. 'destination_city_id',
  30. 'warehouse_weight',
  31. 'warehouse_weight_unit_id',
  32. 'carrier_weight',
  33. 'carrier_weight_unit_id',
  34. 'carType_id',
  35. 'car_owner_info',
  36. 'fee',
  37. 'pick_up_fee',
  38. 'other_fee',
  39. 'collect_fee',
  40. 'dispatch_remark',
  41. 'waybill_price_model_id',
  42. 'warehouse_weight_other',
  43. 'warehouse_weight_unit_id_other',
  44. 'carrier_weight_other',
  45. 'carrier_weight_unit_id_other',
  46. 'source_bill',
  47. 'mileage',
  48. 'amount',
  49. 'inquire_tel',
  50. 'amount_unit_id',
  51. 'other_charge',
  52. 'other_charge_remark',
  53. 'deliver_at',
  54. "district_id",
  55. "order_id",
  56. "is_to_pay", //0否 1是
  57. 'cargo_name',
  58. 'total_number',
  59. 'total_weight',
  60. 'deliveryType_id',
  61. "merge_owner",//合并货主ID集
  62. 'order_type',
  63. 'transport_type',
  64. 'pay_type',
  65. 'back_sign_bill',
  66. 'package_service',
  67. 'express_face_list',
  68. 'arrived_org_simple_name',
  69. 'much_higher_delivery',
  70. 'station_no',
  71. 'subjoin_fee',//附加费
  72. ];
  73. const ORDER_TYPE_DEFAULT = 2;
  74. const ORDER_TYPE = [
  75. 1 => "散客模式",
  76. 2 => "大客户模式",
  77. 3 => "同步筛单下单",
  78. ];
  79. const PAY_TYPE_DEFAULT = 2;
  80. const PAY_TYPE = [
  81. 0 => "发货人付款(现付)(大客户模式不支持寄付)",
  82. 1 => "收货人付款(到付)",
  83. 2 => "发货人付款(月结)",
  84. ];
  85. const BACK_SIGN_BILL_DEFAULT = 0;
  86. const BACK_SIGN_BILL = [
  87. 0 => "无需返单",
  88. 1 => "签收单原单返回",
  89. 2 => "电子签收单(签名图需要另外对接查询接口)",
  90. ];
  91. public function district()
  92. { //区
  93. return $this->belongsTo(Region::class,"district_id","id")->where("type",3);
  94. }
  95. public function owner(){
  96. return $this->hasOne('App\Owner','id','owner_id');
  97. }
  98. public function logistic(){
  99. return $this->hasOne('App\Logistic','id','logistic_id');
  100. }
  101. public function originationCity()
  102. { //始发市
  103. return $this->hasOne(Region::class,'id','origination_city_id');
  104. }
  105. public function destinationCity()
  106. { //目的市
  107. return $this->hasOne(Region::class,'id','destination_city_id');
  108. }
  109. public function wmsCommodities(){
  110. return $this->hasMany('App\WMSWaybillOrder','OrderNo','wms_bill_number');
  111. }
  112. public function priceModel(){
  113. return $this->hasOne('App\WaybillPriceModel','id','waybill_price_model_id');
  114. }
  115. public function amountUnit(){
  116. return $this->hasOne('App\Unit','id','amount_unit_id');
  117. }
  118. public function warehouseWeightUnit(){
  119. return $this->hasOne('App\Unit','id','warehouse_weight_unit_id');
  120. }
  121. public function carrierWeightUnit(){
  122. return $this->hasOne('App\Unit','id','carrier_weight_unit_id');
  123. }
  124. public function warehouseWeightUnitOther(){
  125. return $this->hasOne('App\Unit','id','warehouse_weight_unit_id_other');
  126. }
  127. public function carrierWeightUnitOther(){
  128. return $this->hasOne('App\Unit','id','carrier_weight_unit_id_other');
  129. }
  130. public function carType(){
  131. return $this->hasOne('App\CarType','id','carType_id');
  132. }
  133. public function waybillAuditLogs(){
  134. return $this->hasMany('App\WaybillAuditLog','waybill_id','id')->orderByDesc("created_at");
  135. }
  136. public function uploadFiles(){
  137. return $this->hasMany(UploadFile::class,'table_id','id')->where('table_name','waybills');
  138. }
  139. public function order()
  140. { //订单
  141. return $this->belongsTo(Order::class);
  142. }
  143. public function orderByCode()
  144. { //订单
  145. return $this->hasOne(Order::class,'code','wms_bill_number');
  146. }
  147. public function deliveryType()
  148. { //订单
  149. return $this->hasOne(DeliveryType::class,'id','deliveryType_id');
  150. }
  151. static public function setWeightByOrderCode($orderCode,$weight){
  152. if(empty($orderCode))return;
  153. $waybill=Waybill::query()->where('wms_bill_number',$orderCode)->where('status','!=','已完结')
  154. ->where('status','!=','无模型')->first();
  155. if ($waybill){
  156. $waybill->warehouse_weight_other=$weight;
  157. $waybill->warehouse_weight_unit_id_other=1;
  158. $waybill->update();
  159. }
  160. }
  161. /**
  162. * @return Builder
  163. */
  164. public static function filterAuthorities(){
  165. $ownerQuery = app("OwnerService")->getQuery();
  166. return (new static)->newQuery()->leftJoin("orders","waybills.order_id","orders.id")
  167. ->leftJoin("owners","orders.owner_id","owners.id")
  168. ->selectRaw(<<<column
  169. owners.name as owner_name,
  170. orders.consignee_name as order_consignee_name,
  171. orders.consignee_phone as order_consignee_phone,
  172. orders.province as order_province,
  173. orders.city as order_city,
  174. orders.district as order_district,
  175. orders.address as order_address
  176. column
  177. )
  178. ->where(function ($query)use($ownerQuery){
  179. /** @var Builder $query */
  180. $query->whereIn('waybills.owner_id',$ownerQuery)->
  181. orWhereIn("orders.owner_id",$ownerQuery);
  182. });
  183. }
  184. public function ownerWayBillFeeDetail(): HasOne
  185. {
  186. return $this->hasOne(OwnerWayBillFeeDetail::class);
  187. }
  188. public function getDestinationAttribute($value)
  189. {
  190. return $this->relations["order"]["address"] ?? $value;
  191. }
  192. public function getCarrierNameAttribute()
  193. {
  194. return $this->relations["logistic"]["name"] ?? "";
  195. }
  196. public function getAmountUnitNameAttribute()
  197. {
  198. return $this->relations["amountUnit"]["name"] ?? "件";
  199. }
  200. public function getRemoveRelationAttribute()
  201. {
  202. $this->unsetRelations();
  203. $this->offsetUnset("remove_relation");
  204. }
  205. }