Procurement.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App;
  3. use App\Services\api\UserService;
  4. use App\Traits\ModelTimeFormat;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Database\Eloquent\Model;
  7. use App\Traits\ModelLogChanging;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\DB;
  10. class Procurement extends Model
  11. {
  12. use ModelLogChanging;
  13. use ModelTimeFormat;
  14. const type=[
  15. 0 => "采购单",
  16. 1 => "询价单",
  17. 2 => "打样单",
  18. ];
  19. const status=[
  20. 0 => "待报价",
  21. 1 => "已报价",
  22. 2 => "待接单",
  23. 3 => "已失效",
  24. 4 => "生成中",
  25. 5 => "待收货",
  26. 6 => "待确定",
  27. 7 => "待出账",
  28. 8 => "已出账",
  29. 9 => "完结",
  30. 10 => "订单取消",
  31. ];
  32. protected $fillable=[
  33. 'code','owner_material_id', 'supplier_id', 'quantity','amount','unit_price','cost_price','status','initiator','type','is_enquiry','deadline'
  34. ];
  35. protected static function booted()
  36. {
  37. /** @var User $user */
  38. $user = Auth::user();
  39. if ($user && !$user->isSuperAdmin()) {
  40. /** @var \stdClass $user */
  41. $ids = array_column(DB::select(DB::raw("SELECT supplier_id FROM supplier_user WHERE user_id = ?"),[$user->id]),"supplier_id");
  42. if (count($ids)>0){
  43. static::addGlobalScope('supplier', function (Builder $builder)use ($ids) {
  44. $builder->whereIn('supplier_id', $ids);
  45. });
  46. }
  47. }
  48. }
  49. public function ownerMaterial(){
  50. return $this->hasOne('App\OwnerMaterial','id','owner_material_id');
  51. }
  52. public function supplier(){
  53. return $this->hasOne('App\Supplier','id','supplier_id');
  54. }
  55. public function initiator()
  56. {
  57. return $this->belongsTo(User::class,'initiator','id');
  58. }
  59. public function procurementDeliveries()
  60. {
  61. return $this->hasMany('App\ProcurementDeliverie','procurement_id','id');
  62. }
  63. public function procurementQuotations()
  64. {
  65. return $this->hasMany('App\ProcurementQuotation','procurement_id','id');
  66. }
  67. public function scopeFilter($query, $filters)
  68. {
  69. return $filters->apply($query);
  70. }
  71. }