Owner.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. 'subjection' //主体公司
  32. ];
  33. //relevance说明 0:仓储 1:作业 2:快递 3:物流 4:直发 存储示例:["0","1"]存在仓储与作业计费
  34. protected $casts = [
  35. "relevance" => "array"
  36. ];
  37. const subjection=[
  38. 0=>'',
  39. 1 => "宝时物流",
  40. 2 => "宝时供应链",
  41. ];
  42. public static function filterAuthorities(){
  43. $user=Auth::user();
  44. $query = (new static)->newQuery();
  45. if(!$user){
  46. return $query->where('id','0');
  47. }
  48. $ownerIds=app('UserService')->getPermittingOwnerIds($user);
  49. if(empty($ownerIds))return $query;
  50. return $query->whereIn('id',$ownerIds);
  51. }
  52. /**
  53. * 退货管理里,客户审核的代码,是拼音+日期+计数,计数的后缀就是checking_count
  54. * @return int|mixed
  55. */
  56. public function getIncreasedCheckingCount(){
  57. $this['checking_count']=$this['checking_count']+1;
  58. $this->update();
  59. return $this['checking_count'];
  60. }
  61. public function paperBoxes()
  62. {
  63. return $this->belongsToMany('\App\PaperBox', 'owner_paper_box', 'owner_id', 'paper_box_id');
  64. }
  65. public function orderTrackingOwner(){
  66. return $this->belongsTo(OrderTrackingOwner::class,'id','owner_id');
  67. }
  68. public function contracts()
  69. { //合同
  70. return $this->hasMany(OwnerContract::class,"owner_id","id");
  71. }
  72. public function order(){
  73. return $this->hasOne(Order::class,'owner_id','id');
  74. }
  75. public function customer()
  76. { //客户
  77. return $this->hasOne(Customer::class,"id","customer_id");
  78. }
  79. public function userOwnerGroup()
  80. { //项目组
  81. return $this->hasOne(UserOwnerGroup::class,"id","user_owner_group_id");
  82. }
  83. public function userWorkGroup()
  84. { //工作组
  85. return $this->belongsTo(UserWorkgroup::class,"user_workgroup_id","id");
  86. }
  87. public function ownerAreaReport()
  88. { //面积报表
  89. return $this->hasOne(OwnerAreaReport::class,"owner_id","id");
  90. }
  91. public function ownerStoragePriceModels()
  92. { //仓储计费
  93. $query = OwnerStoragePriceModel::query()->select("target_id")
  94. ->whereNotNull("operation")->where("operation","!=","")
  95. ->whereNotNull("target_id")->where("target_id","!=","");
  96. return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner","owner_id","owner_storage_price_model_id")
  97. ->whereNotIn("id",$query)->where(function(Builder $query){
  98. $query->where("operation","!=","D")->orWhereNull("operation");
  99. });
  100. }
  101. public function ownerPriceOperations()
  102. { //作业计费
  103. $query = OwnerPriceOperation::query()->select("target_id")
  104. ->whereNotNull("operation")->where("operation","!=","")
  105. ->whereNotNull("target_id")->where("target_id","!=","");
  106. return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner","owner_id","owner_price_operation_id")
  107. ->whereNotIn("id",$query)->where(function(Builder $query){
  108. $query->where("operation","!=","D")->orWhereNull("operation");
  109. });
  110. }
  111. public function ownerPriceExpresses()
  112. { //快递计费
  113. $query = OwnerPriceExpress::query()->select("target_id")
  114. ->whereNotNull("operation")->where("operation","!=","")
  115. ->whereNotNull("target_id")->where("target_id","!=","");
  116. return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner","owner_id","owner_price_express_id")
  117. ->whereNotIn("id",$query)->where(function(Builder $query){
  118. $query->where("operation","!=","D")->orWhereNull("operation");
  119. });
  120. }
  121. public function ownerPriceLogistics()
  122. { //物流计费
  123. $query = OwnerPriceLogistic::query()->select("target_id")
  124. ->whereNotNull("operation")->where("operation","!=","")
  125. ->whereNotNull("target_id")->where("target_id","!=","");
  126. return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner","owner_id","owner_price_logistic_id")
  127. ->whereNotIn("id",$query)->where(function(Builder $query){
  128. $query->where("operation","!=","D")->orWhereNull("operation");
  129. });
  130. }
  131. public function ownerPriceDirectLogistics()
  132. { //直发车计费
  133. $query = OwnerPriceDirectLogistic::query()->select("target_id")
  134. ->whereNotNull("operation")->where("operation","!=","")
  135. ->whereNotNull("target_id")->where("target_id","!=","");
  136. return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner","owner_id","owner_price_direct_logistic_id")
  137. ->whereNotIn("id",$query)->where(function(Builder $query){
  138. $query->where("operation","!=","D")->orWhereNull("operation");
  139. });
  140. }
  141. public function warehouse()
  142. { //仓库
  143. return $this->belongsTo(Warehouse::class,"warehouse_id","id");
  144. }
  145. public function ownerMaterials()
  146. { //耗材
  147. return $this->hasMany(OwnerMaterial::class,"owner_id","id");
  148. }
  149. public function taxRate()
  150. { //税率
  151. return $this->belongsTo(TaxRate::class);
  152. }
  153. public function storageAudit()
  154. { //审核的仓储模型
  155. return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner")
  156. ->select(DB::raw(1))->whereNotNull("operation")
  157. ->where("operation","!=","")
  158. ->limit(1);
  159. }
  160. public function operationAudit()
  161. { //审核的作业模型
  162. return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner")
  163. ->select(DB::raw(1))->whereNotNull("operation")
  164. ->where("operation","!=","")
  165. ->limit(1);
  166. }
  167. public function expressAudit()
  168. { //审核的快递模型
  169. return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner")
  170. ->select(DB::raw(1))->whereNotNull("operation")
  171. ->where("operation","!=","")
  172. ->limit(1);
  173. }
  174. public function logisticAudit()
  175. { //审核的物流模型
  176. return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner")
  177. ->select(DB::raw(1))->whereNotNull("operation")
  178. ->where("operation","!=","")
  179. ->limit(1);
  180. }
  181. public function directLogisticAudit()
  182. { //审核的直发模型
  183. return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner")
  184. ->select(DB::raw(1))->whereNotNull("operation")
  185. ->where("operation","!=","")
  186. ->limit(1);
  187. }
  188. }