Owner.php 8.5 KB

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