Owner.php 8.4 KB

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