Owner.php 9.1 KB

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