ProcurementTotalBill.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App;
  3. use App\Traits\ModelTimeFormat;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Model;
  6. use App\Traits\ModelLogChanging;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\DB;
  9. class ProcurementTotalBill extends Model
  10. {
  11. use ModelLogChanging;
  12. use ModelTimeFormat;
  13. const status=[
  14. 0 => "未出账",
  15. 1 => "已出账",
  16. 2 => "已完结",
  17. ];
  18. protected $fillable=[
  19. 'counting_month','supplier_id','status','total_payable'
  20. ];
  21. public function supplier(){
  22. return $this->belongsTo('App\Supplier','supplier_id','id');
  23. }
  24. //截取账单日期为月
  25. public function getCountingMonthAttribute($value)
  26. {
  27. return substr($value,0,7);
  28. }
  29. public function setCurrentMothProcurements(){
  30. $ProcurementQuery = Procurement::query()->select("id")
  31. ->where('supplier_id',$this->supplier_id)
  32. ->where("created_at","like",$this->counting_month."%");
  33. $procurementDeliveryQuery= ProcurementDeliverie::query()->select('id')
  34. ->whereIn("procurement_id",$ProcurementQuery);
  35. $this->relations["procurementCheckSheets"]=ProcurementCheckSheet::query()
  36. ->with(['procurementDelivery.procurement.ownerMaterial.material'])
  37. ->whereIn('procurement_delivery_id',$procurementDeliveryQuery)->get();
  38. }
  39. protected static function booted()
  40. {
  41. /** @var User $user */
  42. $user = Auth::user();
  43. if ($user && !$user->isSuperAdmin()) {
  44. /** @var \stdClass $user */
  45. $ids = array_column(DB::select(DB::raw("SELECT supplier_id FROM supplier_user WHERE user_id = ?"),[$user->id]),"supplier_id");
  46. if (count($ids)>0){
  47. static::addGlobalScope('supplier', function (Builder $builder)use ($ids) {
  48. $builder->whereIn('supplier_id', $ids);
  49. });
  50. }
  51. }
  52. }
  53. }