Owner.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 ownerStoragePriceModels()
  109. { //仓储计费
  110. $query = OwnerStoragePriceModel::query()->select("target_id")
  111. ->whereNotNull("operation")->where("operation","!=","")
  112. ->whereNotNull("target_id")->where("target_id","!=","");
  113. return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner","owner_id","owner_storage_price_model_id")
  114. ->whereNotIn("id",$query)->where(function(Builder $query){
  115. $query->where("operation","!=","D")->orWhereNull("operation");
  116. });
  117. }
  118. public function ownerPriceOperations()
  119. { //作业计费
  120. $query = OwnerPriceOperation::query()->select("target_id")
  121. ->whereNotNull("operation")->where("operation","!=","")
  122. ->whereNotNull("target_id")->where("target_id","!=","");
  123. return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner","owner_id","owner_price_operation_id")
  124. ->whereNotIn("id",$query)->where(function(Builder $query){
  125. $query->where("operation","!=","D")->orWhereNull("operation");
  126. });
  127. }
  128. public function ownerPriceExpresses()
  129. { //快递计费
  130. $query = OwnerPriceExpress::query()->select("target_id")
  131. ->whereNotNull("operation")->where("operation","!=","")
  132. ->whereNotNull("target_id")->where("target_id","!=","");
  133. return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner","owner_id","owner_price_express_id")
  134. ->whereNotIn("id",$query)->where(function(Builder $query){
  135. $query->where("operation","!=","D")->orWhereNull("operation");
  136. });
  137. }
  138. public function ownerPriceLogistics()
  139. { //物流计费
  140. $query = OwnerPriceLogistic::query()->select("target_id")
  141. ->whereNotNull("operation")->where("operation","!=","")
  142. ->whereNotNull("target_id")->where("target_id","!=","");
  143. return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner","owner_id","owner_price_logistic_id")
  144. ->whereNotIn("id",$query)->where(function(Builder $query){
  145. $query->where("operation","!=","D")->orWhereNull("operation");
  146. });
  147. }
  148. public function ownerPriceDirectLogistics()
  149. { //直发车计费
  150. $query = OwnerPriceDirectLogistic::query()->select("target_id")
  151. ->whereNotNull("operation")->where("operation","!=","")
  152. ->whereNotNull("target_id")->where("target_id","!=","");
  153. return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner","owner_id","owner_price_direct_logistic_id")
  154. ->whereNotIn("id",$query)->where(function(Builder $query){
  155. $query->where("operation","!=","D")->orWhereNull("operation");
  156. });
  157. }
  158. public function ownerPriceSystem()
  159. { //系统计费
  160. $query = OwnerPriceSystem::query()->select("target_id")
  161. ->whereNotNull("operation")->where("operation","!=","")
  162. ->whereNotNull("target_id")->where("target_id","!=","");
  163. return $this->hasOne(OwnerPriceSystem::class,"owner_id","id")
  164. ->whereNotIn("id",$query)->where(function(Builder $query){
  165. $query->where("operation","!=","D")->orWhereNull("operation");
  166. });
  167. }
  168. public function warehouse()
  169. { //仓库
  170. return $this->belongsTo(Warehouse::class,"warehouse_id","id");
  171. }
  172. public function ownerMaterials()
  173. { //耗材
  174. return $this->hasMany(OwnerMaterial::class,"owner_id","id");
  175. }
  176. public function taxRate()
  177. { //税率
  178. return $this->belongsTo(TaxRate::class);
  179. }
  180. public function storageAudit()
  181. { //审核的仓储模型
  182. return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner")
  183. ->select(DB::raw(1))->whereNotNull("operation")
  184. ->where("operation","!=","")->groupBy("owner_id");
  185. }
  186. public function operationAudit()
  187. { //审核的作业模型
  188. return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner")
  189. ->select(DB::raw(1))->whereNotNull("operation")
  190. ->where("operation","!=","")->groupBy("owner_id");
  191. }
  192. public function expressAudit()
  193. { //审核的快递模型
  194. return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner")
  195. ->select(DB::raw(1))->whereNotNull("operation")
  196. ->where("operation","!=","")->groupBy("owner_id");
  197. }
  198. public function logisticAudit()
  199. { //审核的物流模型
  200. return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner")
  201. ->select(DB::raw(1))->whereNotNull("operation")
  202. ->where("operation","!=","")->groupBy("owner_id");
  203. }
  204. public function directLogisticAudit()
  205. { //审核的直发模型
  206. return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner")
  207. ->select(DB::raw(1))->whereNotNull("operation")
  208. ->where("operation","!=","")->groupBy("owner_id");
  209. }
  210. public function systemAudit()
  211. { //审核的直发模型
  212. return $this->hasOne(OwnerPriceSystem::class,"owner_id","id")
  213. ->select(DB::raw(1))->whereNotNull("operation")
  214. ->where("operation","!=","");
  215. }
  216. public function ownerSundryFeeDetail(): HasMany
  217. {
  218. return $this->hasMany(OwnerSundryFeeDetail::class);
  219. }
  220. public function roles()
  221. { //角色
  222. return $this->belongsToMany(Role::class);
  223. }
  224. }