Owner.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. 'is_check_asn', //是否校验ASN(收货时检查此项)
  37. 'owner_group_id', // 所属组别
  38. 'is_manual_back', // 是否开启货主订单一键回传
  39. 'interval_time', // 回传间隔时间(分钟为单位)
  40. 'last_manual_back_time', // 上次回传时间
  41. ];
  42. //relevance说明 0:仓储 1:作业 2:快递 3:物流 4:直发 5:系统 存储示例:["0","1"]存在仓储与作业计费
  43. protected $casts = [
  44. "relevance" => "array"
  45. ];
  46. const subjection=[
  47. 0=>'',
  48. 1 => "宝时物流",
  49. 2 => "宝时供应链",
  50. ];
  51. const IS_CHECK_ASN=[
  52. 0 => "否",
  53. 1 => "是"
  54. ];
  55. const IS_MANUAL_BACK=[
  56. 0 => "否",
  57. 1 => "是"
  58. ];
  59. public static function filterAuthorities(){
  60. $user=Auth::user();
  61. $query = (new static)->newQuery();
  62. if(!$user){
  63. return $query->where('id','0');
  64. }
  65. $ownerIds=app('UserService')->getPermittingOwnerIds($user);
  66. if(empty($ownerIds))return $query;
  67. return $query->whereIn('id',$ownerIds);
  68. }
  69. /**
  70. * 退货管理里,客户审核的代码,是拼音+日期+计数,计数的后缀就是checking_count
  71. * @return int|mixed
  72. */
  73. public function getIncreasedCheckingCount(){
  74. $this['checking_count']=$this['checking_count']+1;
  75. $this->update();
  76. return $this['checking_count'];
  77. }
  78. public function paperBoxes()
  79. {
  80. return $this->belongsToMany('\App\PaperBox', 'owner_paper_box', 'owner_id', 'paper_box_id');
  81. }
  82. public function orderTrackingOwner(){
  83. return $this->belongsTo(OrderTrackingOwner::class,'id','owner_id');
  84. }
  85. public function contracts()
  86. { //合同
  87. return $this->hasMany(OwnerContract::class,"owner_id","id");
  88. }
  89. public function order(){
  90. return $this->hasOne(Order::class,'owner_id','id');
  91. }
  92. public function customer()
  93. { //客户
  94. return $this->hasOne(Customer::class,"id","customer_id");
  95. }
  96. public function userOwnerGroup()
  97. { //项目组
  98. return $this->hasOne(UserOwnerGroup::class,"id","user_owner_group_id");
  99. }
  100. public function userWorkGroup()
  101. { //工作组
  102. return $this->belongsTo(UserWorkgroup::class,"user_workgroup_id","id");
  103. }
  104. public function ownerAreaReport()
  105. { //面积报表
  106. return $this->hasOne(OwnerAreaReport::class,"owner_id","id");
  107. }
  108. public function departmentObligationOwner()
  109. { //部门职能关联
  110. return $this->hasMany(DepartmentObligationOwner::class,"owner_id","id")
  111. ->selectRaw('obligation_code,obligation_id,owner_id,department_id,max(valid_time)')
  112. ->groupBy('obligation_id','owner_id','department_id')
  113. ->whereNull('failure_time');
  114. }
  115. public function ownerStoragePriceModels()
  116. { //仓储计费
  117. $query = OwnerStoragePriceModel::query()->select("target_id")
  118. ->whereNotNull("operation")->where("operation","!=","")
  119. ->whereNotNull("target_id")->where("target_id","!=","");
  120. return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner","owner_id","owner_storage_price_model_id")
  121. ->whereNotIn("id",$query)->where(function(Builder $query){
  122. $query->where("operation","!=","D")->orWhereNull("operation");
  123. });
  124. }
  125. public function ownerPriceOperations()
  126. { //作业计费
  127. $query = OwnerPriceOperation::query()->select("target_id")
  128. ->whereNotNull("operation")->where("operation","!=","")
  129. ->whereNotNull("target_id")->where("target_id","!=","");
  130. return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner","owner_id","owner_price_operation_id")
  131. ->whereNotIn("id",$query)->where(function(Builder $query){
  132. $query->where("operation","!=","D")->orWhereNull("operation");
  133. });
  134. }
  135. public function ownerPriceExpresses()
  136. { //快递计费
  137. $query = OwnerPriceExpress::query()->select("target_id")
  138. ->whereNotNull("operation")->where("operation","!=","")
  139. ->whereNotNull("target_id")->where("target_id","!=","");
  140. return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner","owner_id","owner_price_express_id")
  141. ->whereNotIn("id",$query)->where(function(Builder $query){
  142. $query->where("operation","!=","D")->orWhereNull("operation");
  143. });
  144. }
  145. public function ownerPriceLogistics()
  146. { //物流计费
  147. $query = OwnerPriceLogistic::query()->select("target_id")
  148. ->whereNotNull("operation")->where("operation","!=","")
  149. ->whereNotNull("target_id")->where("target_id","!=","");
  150. return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner","owner_id","owner_price_logistic_id")
  151. ->whereNotIn("id",$query)->where(function(Builder $query){
  152. $query->where("operation","!=","D")->orWhereNull("operation");
  153. });
  154. }
  155. public function ownerPriceDirectLogistics()
  156. { //直发车计费
  157. $query = OwnerPriceDirectLogistic::query()->select("target_id")
  158. ->whereNotNull("operation")->where("operation","!=","")
  159. ->whereNotNull("target_id")->where("target_id","!=","");
  160. return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner","owner_id","owner_price_direct_logistic_id")
  161. ->whereNotIn("id",$query)->where(function(Builder $query){
  162. $query->where("operation","!=","D")->orWhereNull("operation");
  163. });
  164. }
  165. public function ownerPriceSystem()
  166. { //系统计费
  167. $query = OwnerPriceSystem::query()->select("target_id")
  168. ->whereNotNull("operation")->where("operation","!=","")
  169. ->whereNotNull("target_id")->where("target_id","!=","");
  170. return $this->hasOne(OwnerPriceSystem::class,"owner_id","id")
  171. ->whereNotIn("id",$query)->where(function(Builder $query){
  172. $query->where("operation","!=","D")->orWhereNull("operation");
  173. });
  174. }
  175. public function warehouse()
  176. { //仓库
  177. return $this->belongsTo(Warehouse::class,"warehouse_id","id");
  178. }
  179. public function ownerMaterials()
  180. { //耗材
  181. return $this->hasMany(OwnerMaterial::class,"owner_id","id");
  182. }
  183. public function taxRate()
  184. { //税率
  185. return $this->belongsTo(TaxRate::class);
  186. }
  187. public function storageAudit()
  188. { //审核的仓储模型
  189. return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner")
  190. ->select(DB::raw(1))->whereNotNull("operation")
  191. ->where("operation","!=","")->groupBy("owner_id");
  192. }
  193. public function operationAudit()
  194. { //审核的作业模型
  195. return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner")
  196. ->select(DB::raw(1))->whereNotNull("operation")
  197. ->where("operation","!=","")->groupBy("owner_id");
  198. }
  199. public function expressAudit()
  200. { //审核的快递模型
  201. return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner")
  202. ->select(DB::raw(1))->whereNotNull("operation")
  203. ->where("operation","!=","")->groupBy("owner_id");
  204. }
  205. public function logisticAudit()
  206. { //审核的物流模型
  207. return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner")
  208. ->select(DB::raw(1))->whereNotNull("operation")
  209. ->where("operation","!=","")->groupBy("owner_id");
  210. }
  211. public function directLogisticAudit()
  212. { //审核的直发模型
  213. return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner")
  214. ->select(DB::raw(1))->whereNotNull("operation")
  215. ->where("operation","!=","")->groupBy("owner_id");
  216. }
  217. public function systemAudit()
  218. { //审核的直发模型
  219. return $this->hasOne(OwnerPriceSystem::class,"owner_id","id")
  220. ->select(DB::raw(1))->whereNotNull("operation")
  221. ->where("operation","!=","");
  222. }
  223. public function ownerSundryFeeDetail(): HasMany
  224. {
  225. return $this->hasMany(OwnerSundryFeeDetail::class);
  226. }
  227. public function roles()
  228. { //角色
  229. return $this->belongsToMany(Role::class);
  230. }
  231. }