Owner.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. return $query->whereIn('id',app("OwnerService")->getQuery());
  66. }
  67. /**
  68. * 退货管理里,客户审核的代码,是拼音+日期+计数,计数的后缀就是checking_count
  69. * @return int|mixed
  70. */
  71. public function getIncreasedCheckingCount(){
  72. $this['checking_count']=$this['checking_count']+1;
  73. $this->update();
  74. return $this['checking_count'];
  75. }
  76. public function paperBoxes(): BelongsToMany
  77. {
  78. return $this->belongsToMany('\App\PaperBox', 'owner_paper_box', 'owner_id', 'paper_box_id');
  79. }
  80. public function users(): BelongsToMany
  81. {
  82. return $this->belongsToMany('\App\User', 'owner_user', 'owner_id', 'user_id');
  83. }
  84. public function orderTrackingOwner(){
  85. return $this->belongsTo(OrderTrackingOwner::class,'id','owner_id');
  86. }
  87. public function contracts()
  88. { //合同
  89. return $this->hasMany(OwnerContract::class,"owner_id","id");
  90. }
  91. public function order(){
  92. return $this->hasOne(Order::class,'owner_id','id');
  93. }
  94. public function customer()
  95. { //客户
  96. return $this->hasOne(Customer::class,"id","customer_id");
  97. }
  98. public function userOwnerGroup()
  99. { //项目组
  100. return $this->hasOne(UserOwnerGroup::class,"id","user_owner_group_id");
  101. }
  102. public function userWorkGroup()
  103. { //工作组
  104. return $this->belongsTo(UserWorkgroup::class,"user_workgroup_id","id");
  105. }
  106. public function ownerAreaReport()
  107. { //面积报表
  108. return $this->hasOne(OwnerAreaReport::class,"owner_id","id");
  109. }
  110. public function departmentObligationOwner()
  111. { //部门职能关联
  112. return $this->hasMany(DepartmentObligationOwner::class,"owner_id","id")
  113. ->selectRaw('obligation_code,obligation_id,owner_id,department_id,max(valid_time)')
  114. ->groupBy('obligation_id','owner_id','department_id')
  115. ->whereNull('failure_time');
  116. }
  117. public function ownerStoragePriceModels()
  118. { //仓储计费
  119. $query = OwnerStoragePriceModel::query()->select("target_id")
  120. ->whereNotNull("operation")->where("operation","!=","")
  121. ->whereNotNull("target_id")->where("target_id","!=","");
  122. return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner","owner_id","owner_storage_price_model_id")
  123. ->whereNotIn("id",$query)->where(function(Builder $query){
  124. $query->where("operation","!=","D")->orWhereNull("operation");
  125. });
  126. }
  127. public function ownerPriceOperations()
  128. { //作业计费
  129. $query = OwnerPriceOperation::query()->select("target_id")
  130. ->whereNotNull("operation")->where("operation","!=","")
  131. ->whereNotNull("target_id")->where("target_id","!=","");
  132. return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner","owner_id","owner_price_operation_id")
  133. ->whereNotIn("id",$query)->where(function(Builder $query){
  134. $query->where("operation","!=","D")->orWhereNull("operation");
  135. });
  136. }
  137. public function ownerPriceExpresses()
  138. { //快递计费
  139. $query = OwnerPriceExpress::query()->select("target_id")
  140. ->whereNotNull("operation")->where("operation","!=","")
  141. ->whereNotNull("target_id")->where("target_id","!=","");
  142. return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner","owner_id","owner_price_express_id")
  143. ->whereNotIn("id",$query)->where(function(Builder $query){
  144. $query->where("operation","!=","D")->orWhereNull("operation");
  145. });
  146. }
  147. public function ownerPriceLogistics()
  148. { //物流计费
  149. $query = OwnerPriceLogistic::query()->select("target_id")
  150. ->whereNotNull("operation")->where("operation","!=","")
  151. ->whereNotNull("target_id")->where("target_id","!=","");
  152. return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner","owner_id","owner_price_logistic_id")
  153. ->whereNotIn("id",$query)->where(function(Builder $query){
  154. $query->where("operation","!=","D")->orWhereNull("operation");
  155. });
  156. }
  157. public function ownerPriceDirectLogistics()
  158. { //直发车计费
  159. $query = OwnerPriceDirectLogistic::query()->select("target_id")
  160. ->whereNotNull("operation")->where("operation","!=","")
  161. ->whereNotNull("target_id")->where("target_id","!=","");
  162. return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner","owner_id","owner_price_direct_logistic_id")
  163. ->whereNotIn("id",$query)->where(function(Builder $query){
  164. $query->where("operation","!=","D")->orWhereNull("operation");
  165. });
  166. }
  167. public function ownerPriceSystem()
  168. { //系统计费
  169. $query = OwnerPriceSystem::query()->select("target_id")
  170. ->whereNotNull("operation")->where("operation","!=","")
  171. ->whereNotNull("target_id")->where("target_id","!=","");
  172. return $this->hasOne(OwnerPriceSystem::class,"owner_id","id")
  173. ->whereNotIn("id",$query)->where(function(Builder $query){
  174. $query->where("operation","!=","D")->orWhereNull("operation");
  175. });
  176. }
  177. public function warehouse()
  178. { //仓库
  179. return $this->belongsTo(Warehouse::class,"warehouse_id","id");
  180. }
  181. public function ownerMaterials()
  182. { //耗材
  183. return $this->hasMany(OwnerMaterial::class,"owner_id","id");
  184. }
  185. public function taxRate()
  186. { //税率
  187. return $this->belongsTo(TaxRate::class);
  188. }
  189. public function storageAudit()
  190. { //审核的仓储模型
  191. return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner")
  192. ->select(DB::raw(1))->whereNotNull("operation")
  193. ->where("operation","!=","")->groupBy("owner_id");
  194. }
  195. public function operationAudit()
  196. { //审核的作业模型
  197. return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner")
  198. ->select(DB::raw(1))->whereNotNull("operation")
  199. ->where("operation","!=","")->groupBy("owner_id");
  200. }
  201. public function expressAudit()
  202. { //审核的快递模型
  203. return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner")
  204. ->select(DB::raw(1))->whereNotNull("operation")
  205. ->where("operation","!=","")->groupBy("owner_id");
  206. }
  207. public function logisticAudit()
  208. { //审核的物流模型
  209. return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner")
  210. ->select(DB::raw(1))->whereNotNull("operation")
  211. ->where("operation","!=","")->groupBy("owner_id");
  212. }
  213. public function directLogisticAudit()
  214. { //审核的直发模型
  215. return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner")
  216. ->select(DB::raw(1))->whereNotNull("operation")
  217. ->where("operation","!=","")->groupBy("owner_id");
  218. }
  219. public function systemAudit()
  220. { //审核的直发模型
  221. return $this->hasOne(OwnerPriceSystem::class,"owner_id","id")
  222. ->select(DB::raw(1))->whereNotNull("operation")
  223. ->where("operation","!=","");
  224. }
  225. public function ownerSundryFeeDetail(): HasMany
  226. {
  227. return $this->hasMany(OwnerSundryFeeDetail::class);
  228. }
  229. public function roles()
  230. { //角色
  231. return $this->belongsToMany(Role::class);
  232. }
  233. }