Owner.php 9.2 KB

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