Owner.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. use Illuminate\Support\Facades\Auth;
  7. /**
  8. * @method static orderBy(string $string, string $string1)
  9. */
  10. use App\Traits\ModelTimeFormat;
  11. use App\Traits\ModelLogChanging;
  12. use Illuminate\Support\Facades\DB;
  13. class Owner extends Model
  14. {
  15. use ModelLogChanging;
  16. use ModelTimeFormat;
  17. public $fillable = [
  18. 'name', //名称
  19. 'code', //代码
  20. 'checking_count', //审核数量
  21. 'deleted_at', //删除时间
  22. "customer_id", //客户ID
  23. "tax_rate_id", //税率
  24. "linkman", //联系人
  25. "phone_number", //联系电话
  26. "user_owner_group_id", //项目组ID
  27. "waring_line_on", //月单量预警
  28. "description", //描述
  29. "warehouse_id", //仓库ID
  30. "user_workgroup_id", //仓库小组(工作组)
  31. "relevance", //关联模型的JSON数组
  32. 'subjection', //主体公司
  33. 'is_tax_exist' //是否必填税率
  34. ];
  35. //relevance说明 0:仓储 1:作业 2:快递 3:物流 4:直发 5:系统 存储示例:["0","1"]存在仓储与作业计费
  36. protected $casts = [
  37. "relevance" => "array"
  38. ];
  39. const subjection=[
  40. 0=>'',
  41. 1 => "宝时物流",
  42. 2 => "宝时供应链",
  43. ];
  44. public static function filterAuthorities(){
  45. $user=Auth::user();
  46. $query = (new static)->newQuery();
  47. if(!$user){
  48. return $query->where('id','0');
  49. }
  50. $ownerIds=app('UserService')->getPermittingOwnerIds($user);
  51. if(empty($ownerIds))return $query;
  52. return $query->whereIn('id',$ownerIds);
  53. }
  54. /**
  55. * 退货管理里,客户审核的代码,是拼音+日期+计数,计数的后缀就是checking_count
  56. * @return int|mixed
  57. */
  58. public function getIncreasedCheckingCount(){
  59. $this['checking_count']=$this['checking_count']+1;
  60. $this->update();
  61. return $this['checking_count'];
  62. }
  63. public function paperBoxes()
  64. {
  65. return $this->belongsToMany('\App\PaperBox', 'owner_paper_box', 'owner_id', 'paper_box_id');
  66. }
  67. public function orderTrackingOwner(){
  68. return $this->belongsTo(OrderTrackingOwner::class,'id','owner_id');
  69. }
  70. public function contracts()
  71. { //合同
  72. return $this->hasMany(OwnerContract::class,"owner_id","id");
  73. }
  74. public function order(){
  75. return $this->hasOne(Order::class,'owner_id','id');
  76. }
  77. public function customer()
  78. { //客户
  79. return $this->hasOne(Customer::class,"id","customer_id");
  80. }
  81. public function userOwnerGroup()
  82. { //项目组
  83. return $this->hasOne(UserOwnerGroup::class,"id","user_owner_group_id");
  84. }
  85. public function userWorkGroup()
  86. { //工作组
  87. return $this->belongsTo(UserWorkgroup::class,"user_workgroup_id","id");
  88. }
  89. public function ownerAreaReport()
  90. { //面积报表
  91. return $this->hasOne(OwnerAreaReport::class,"owner_id","id");
  92. }
  93. public function ownerStoragePriceModels()
  94. { //仓储计费
  95. $query = OwnerStoragePriceModel::query()->select("target_id")
  96. ->whereNotNull("operation")->where("operation","!=","")
  97. ->whereNotNull("target_id")->where("target_id","!=","");
  98. return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner","owner_id","owner_storage_price_model_id")
  99. ->whereNotIn("id",$query)->where(function(Builder $query){
  100. $query->where("operation","!=","D")->orWhereNull("operation");
  101. });
  102. }
  103. public function ownerPriceOperations()
  104. { //作业计费
  105. $query = OwnerPriceOperation::query()->select("target_id")
  106. ->whereNotNull("operation")->where("operation","!=","")
  107. ->whereNotNull("target_id")->where("target_id","!=","");
  108. return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner","owner_id","owner_price_operation_id")
  109. ->whereNotIn("id",$query)->where(function(Builder $query){
  110. $query->where("operation","!=","D")->orWhereNull("operation");
  111. });
  112. }
  113. public function ownerPriceExpresses()
  114. { //快递计费
  115. $query = OwnerPriceExpress::query()->select("target_id")
  116. ->whereNotNull("operation")->where("operation","!=","")
  117. ->whereNotNull("target_id")->where("target_id","!=","");
  118. return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner","owner_id","owner_price_express_id")
  119. ->whereNotIn("id",$query)->where(function(Builder $query){
  120. $query->where("operation","!=","D")->orWhereNull("operation");
  121. });
  122. }
  123. public function ownerPriceLogistics()
  124. { //物流计费
  125. $query = OwnerPriceLogistic::query()->select("target_id")
  126. ->whereNotNull("operation")->where("operation","!=","")
  127. ->whereNotNull("target_id")->where("target_id","!=","");
  128. return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner","owner_id","owner_price_logistic_id")
  129. ->whereNotIn("id",$query)->where(function(Builder $query){
  130. $query->where("operation","!=","D")->orWhereNull("operation");
  131. });
  132. }
  133. public function ownerPriceDirectLogistics()
  134. { //直发车计费
  135. $query = OwnerPriceDirectLogistic::query()->select("target_id")
  136. ->whereNotNull("operation")->where("operation","!=","")
  137. ->whereNotNull("target_id")->where("target_id","!=","");
  138. return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner","owner_id","owner_price_direct_logistic_id")
  139. ->whereNotIn("id",$query)->where(function(Builder $query){
  140. $query->where("operation","!=","D")->orWhereNull("operation");
  141. });
  142. }
  143. public function ownerPriceSystem()
  144. { //系统计费
  145. $query = OwnerPriceSystem::query()->select("target_id")
  146. ->whereNotNull("operation")->where("operation","!=","")
  147. ->whereNotNull("target_id")->where("target_id","!=","");
  148. return $this->hasOne(OwnerPriceSystem::class,"owner_id","id")
  149. ->whereNotIn("id",$query)->where(function(Builder $query){
  150. $query->where("operation","!=","D")->orWhereNull("operation");
  151. });
  152. }
  153. public function warehouse()
  154. { //仓库
  155. return $this->belongsTo(Warehouse::class,"warehouse_id","id");
  156. }
  157. public function ownerMaterials()
  158. { //耗材
  159. return $this->hasMany(OwnerMaterial::class,"owner_id","id");
  160. }
  161. public function taxRate()
  162. { //税率
  163. return $this->belongsTo(TaxRate::class);
  164. }
  165. public function storageAudit()
  166. { //审核的仓储模型
  167. return $this->belongsToMany(OwnerStoragePriceModel::class,"owner_storage_price_model_owner")
  168. ->select(DB::raw(1))->whereNotNull("operation")
  169. ->where("operation","!=","")->groupBy("owner_id");
  170. }
  171. public function operationAudit()
  172. { //审核的作业模型
  173. return $this->belongsToMany(OwnerPriceOperation::class,"owner_price_operation_owner")
  174. ->select(DB::raw(1))->whereNotNull("operation")
  175. ->where("operation","!=","")->groupBy("owner_id");
  176. }
  177. public function expressAudit()
  178. { //审核的快递模型
  179. return $this->belongsToMany(OwnerPriceExpress::class,"owner_price_express_owner")
  180. ->select(DB::raw(1))->whereNotNull("operation")
  181. ->where("operation","!=","")->groupBy("owner_id");
  182. }
  183. public function logisticAudit()
  184. { //审核的物流模型
  185. return $this->belongsToMany(OwnerPriceLogistic::class,"owner_price_logistic_owner")
  186. ->select(DB::raw(1))->whereNotNull("operation")
  187. ->where("operation","!=","")->groupBy("owner_id");
  188. }
  189. public function directLogisticAudit()
  190. { //审核的直发模型
  191. return $this->belongsToMany(OwnerPriceDirectLogistic::class,"owner_price_direct_logistic_owner")
  192. ->select(DB::raw(1))->whereNotNull("operation")
  193. ->where("operation","!=","")->groupBy("owner_id");
  194. }
  195. public function systemAudit()
  196. { //审核的直发模型
  197. return $this->hasOne(OwnerPriceSystem::class,"owner_id","id")
  198. ->select(DB::raw(1))->whereNotNull("operation")
  199. ->where("operation","!=","");
  200. }
  201. public function ownerSundryFeeDetail(): HasMany
  202. {
  203. return $this->hasMany(OwnerSundryFeeDetail::class);
  204. }
  205. public function roles()
  206. { //角色
  207. return $this->belongsToMany(Role::class);
  208. }
  209. }