ProcurementTotalBill.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(): \Illuminate\Database\Eloquent\Relations\BelongsTo
  22. {
  23. return $this->belongsTo('App\Supplier','supplier_id','id');
  24. }
  25. public function procurement()
  26. {
  27. return $this->hasMany('App\Procurement','supplier_id','supplier_id')
  28. ->whereNotIn('status',[3,10]);
  29. }
  30. //截取账单日期为月
  31. public function getCountingMonthAttribute($value)
  32. {
  33. return substr($value,0,7);
  34. }
  35. public function setCurrentMothProcurements(){
  36. $ProcurementQuery = Procurement::query()->select("id")
  37. ->where('supplier_id',$this->supplier_id);
  38. $procurementDeliveryQuery= ProcurementDeliverie::query()->select('id')
  39. ->whereIn("procurement_id",$ProcurementQuery)
  40. ->where("created_at","like",$this->counting_month."%");
  41. $this->relations["procurementCheckSheets"]=ProcurementCheckSheet::query()
  42. ->with(['procurementDelivery.procurement.ownerMaterial.material','procurementDelivery.procurement.supplier'])
  43. ->whereIn('procurement_delivery_id',$procurementDeliveryQuery)->get();
  44. }
  45. protected static function booted()
  46. {
  47. /** @var User $user */
  48. $user = Auth::user();
  49. if ($user && !$user->isSuperAdmin()) {
  50. /** @var \stdClass $user */
  51. $ids = array_column(DB::select(DB::raw("SELECT supplier_id FROM supplier_user WHERE user_id = ?"),[$user->id]),"supplier_id");
  52. if (count($ids)>0){
  53. static::addGlobalScope('supplier', function (Builder $builder)use ($ids) {
  54. $builder->whereIn('supplier_id', $ids);
  55. });
  56. }
  57. }
  58. }
  59. }