Owner.php 9.0 KB

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