Owner.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\Auth;
  6. /**
  7. * @method static orderBy(string $string, string $string1)
  8. */
  9. use App\Traits\ModelTimeFormat;
  10. use App\Traits\ModelLogChanging;
  11. use Illuminate\Support\Facades\DB;
  12. class Owner extends Model
  13. {
  14. use ModelLogChanging;
  15. use ModelTimeFormat;
  16. public $fillable = [
  17. 'name', //名称
  18. 'code', //代码
  19. 'checking_count', //审核数量
  20. 'deleted_at', //删除时间
  21. "customer_id", //客户ID
  22. "tax_rate_id", //税率
  23. "linkman", //联系人
  24. "phone_number", //联系电话
  25. "user_owner_group_id", //项目组ID
  26. "waring_line_on", //月单量预警
  27. "description", //描述
  28. "warehouse_id", //仓库ID
  29. "user_workgroup_id", //仓库小组(工作组)
  30. "relevance" //关联模型的JSON数组
  31. ];
  32. //relevance说明 0:仓储 1:作业 2:快递 3:物流 4:直发 存储示例:["0","1"]存在仓储与作业计费
  33. protected $casts = [
  34. "relevance" => "array"
  35. ];
  36. public static function filterAuthorities(){
  37. $user=Auth::user();
  38. $query = (new static)->newQuery();
  39. if(!$user){
  40. return $query->where('id','0');
  41. }
  42. $ownerIds=app('UserService')->getPermittingOwnerIds($user);
  43. if(empty($ownerIds))return $query;
  44. return $query->whereIn('id',$ownerIds);
  45. }
  46. /**
  47. * 退货管理里,客户审核的代码,是拼音+日期+计数,计数的后缀就是checking_count
  48. * @return int|mixed
  49. */
  50. public function getIncreasedCheckingCount(){
  51. $this['checking_count']=$this['checking_count']+1;
  52. $this->update();
  53. return $this['checking_count'];
  54. }
  55. public function paperBoxes()
  56. {
  57. return $this->belongsToMany('\App\PaperBox', 'owner_paper_box', 'owner_id', 'paper_box_id');
  58. }
  59. public function orderTrackingOwner(){
  60. return $this->belongsTo(OrderTrackingOwner::class,'id','owner_id');
  61. }
  62. public function contracts()
  63. { //合同
  64. return $this->hasMany(OwnerContract::class,"owner_id","id");
  65. }
  66. public function order(){
  67. return $this->hasOne(Order::class,'owner_id','id');
  68. }
  69. public function customer()
  70. { //客户
  71. return $this->hasOne(Customer::class,"id","customer_id");
  72. }
  73. public function userOwnerGroup()
  74. { //项目组
  75. return $this->hasOne(UserOwnerGroup::class,"id","user_owner_group_id");
  76. }
  77. public function userWorkGroup()
  78. { //工作组
  79. return $this->belongsTo(UserWorkgroup::class,"user_workgroup_id","id");
  80. }
  81. public function ownerAreaReport()
  82. { //面积报表
  83. return $this->hasOne(OwnerAreaReport::class,"owner_id","id");
  84. }
  85. public function ownerStoragePriceModels()
  86. { //仓储计费
  87. $query = OwnerPriceExpress::query()->select("target_id")
  88. ->whereNotNull("operation")->where("operation","!=","")
  89. ->whereNotNull("target_id")->where("target_id","!=","");
  90. return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner","owner_id","owner_storage_price_model_id")
  91. ->whereNotIn("id",$query)->where(function(Builder $query){
  92. $query->where("operation","!=","D")->orWhereNull("operation");
  93. });
  94. }
  95. public function ownerPriceOperations()
  96. { //作业计费
  97. $query = OwnerPriceOperation::query()->select("target_id")
  98. ->whereNotNull("operation")->where("operation","!=","")
  99. ->whereNotNull("target_id")->where("target_id","!=","");
  100. return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner","owner_id","owner_price_operation_id")
  101. ->whereNotIn("id",$query)->where(function(Builder $query){
  102. $query->where("operation","!=","D")->orWhereNull("operation");
  103. });
  104. }
  105. public function ownerPriceExpresses()
  106. { //快递计费
  107. $query = OwnerPriceExpress::query()->select("target_id")
  108. ->whereNotNull("operation")->where("operation","!=","")
  109. ->whereNotNull("target_id")->where("target_id","!=","");
  110. return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner","owner_id","owner_price_express_id")
  111. ->whereNotIn("id",$query)->where(function(Builder $query){
  112. $query->where("operation","!=","D")->orWhereNull("operation");
  113. });
  114. }
  115. public function ownerPriceLogistics()
  116. { //物流计费
  117. $query = OwnerPriceExpress::query()->select("target_id")
  118. ->whereNotNull("operation")->where("operation","!=","")
  119. ->whereNotNull("target_id")->where("target_id","!=","");
  120. return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner","owner_id","owner_price_logistic_id")
  121. ->whereNotIn("id",$query)->where(function(Builder $query){
  122. $query->where("operation","!=","D")->orWhereNull("operation");
  123. });
  124. }
  125. public function ownerPriceDirectLogistics()
  126. { //直发车计费
  127. $query = OwnerPriceExpress::query()->select("target_id")
  128. ->whereNotNull("operation")->where("operation","!=","")
  129. ->whereNotNull("target_id")->where("target_id","!=","");
  130. return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner","owner_id","owner_price_direct_logistic_id")
  131. ->whereNotIn("id",$query)->where(function(Builder $query){
  132. $query->where("operation","!=","D")->orWhereNull("operation");
  133. });
  134. }
  135. public function warehouse()
  136. { //仓库
  137. return $this->belongsTo(Warehouse::class,"warehouse_id","id");
  138. }
  139. public function taxRate()
  140. { //税率
  141. return $this->belongsTo(TaxRate::class);
  142. }
  143. public function storageAudit()
  144. { //审核的仓储模型
  145. return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner")
  146. ->select(DB::raw(1))->whereNotNull("operation")
  147. ->where("operation","!=","")
  148. ->limit(1);
  149. }
  150. public function operationAudit()
  151. { //审核的作业模型
  152. return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner")
  153. ->select(DB::raw(1))->whereNotNull("operation")
  154. ->where("operation","!=","")
  155. ->limit(1);
  156. }
  157. public function expressAudit()
  158. { //审核的快递模型
  159. return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner")
  160. ->select(DB::raw(1))->whereNotNull("operation")
  161. ->where("operation","!=","")
  162. ->limit(1);
  163. }
  164. public function logisticAudit()
  165. { //审核的物流模型
  166. return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner")
  167. ->select(DB::raw(1))->whereNotNull("operation")
  168. ->where("operation","!=","")
  169. ->limit(1);
  170. }
  171. public function directLogisticAudit()
  172. { //审核的直发模型
  173. return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner")
  174. ->select(DB::raw(1))->whereNotNull("operation")
  175. ->where("operation","!=","")
  176. ->limit(1);
  177. }
  178. }