Owner.php 8.9 KB

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