Owner.php 8.8 KB

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