ProcurementTotalBill.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. // ->where("created_at","like",$this->counting_month."%");
  39. $procurementDeliveryQuery= ProcurementDeliverie::query()->select('id')
  40. ->whereIn("procurement_id",$ProcurementQuery)
  41. ->where("created_at","like",$this->counting_month."%");
  42. $this->relations["procurementCheckSheets"]=ProcurementCheckSheet::query()
  43. ->with(['procurementDelivery.procurement.ownerMaterial.material'])
  44. ->whereIn('procurement_delivery_id',$procurementDeliveryQuery)->get();
  45. }
  46. protected static function booted()
  47. {
  48. /** @var User $user */
  49. $user = Auth::user();
  50. if ($user && !$user->isSuperAdmin()) {
  51. /** @var \stdClass $user */
  52. $ids = array_column(DB::select(DB::raw("SELECT supplier_id FROM supplier_user WHERE user_id = ?"),[$user->id]),"supplier_id");
  53. if (count($ids)>0){
  54. static::addGlobalScope('supplier', function (Builder $builder)use ($ids) {
  55. $builder->whereIn('supplier_id', $ids);
  56. });
  57. }
  58. }
  59. }
  60. }