From f3a2ff35d7c528ab43e8c70f16fa09aaaeecbd82 Mon Sep 17 00:00:00 2001 From: hafidhmuhammadakbar Date: Wed, 14 Jun 2023 22:48:37 +0700 Subject: [PATCH 1/2] edit model --- app/Models/DetailSale.php | 33 +++++++++++++++++++++++++++++++++ app/Models/Medicine.php | 6 ++++++ app/Models/Sale.php | 33 +++++++++++++++++++++++++++++++++ app/Models/User.php | 6 ++++++ 4 files changed, 78 insertions(+) diff --git a/app/Models/DetailSale.php b/app/Models/DetailSale.php index 1235795..32611db 100644 --- a/app/Models/DetailSale.php +++ b/app/Models/DetailSale.php @@ -9,6 +9,27 @@ class DetailSale extends Model { use HasFactory; + protected $primaryKey = 'id'; + public $incrementing = false; + + protected static function boot() + { + parent::boot(); + + static::creating(function ($detailSale) { + $detailSale->id = 'DS' . str_pad(static::maxIdNumber() + 1, 4, '0', STR_PAD_LEFT); + }); + } + + protected static function maxIdNumber() + { + $maxId = static::max('id'); + if ($maxId) { + return intval(substr($maxId, 2)); + } + return 0; + } + protected $fillable = [ 'sale_id', 'medicine_id', @@ -17,4 +38,16 @@ class DetailSale extends Model 'discount', 'subtotal', ]; + + // eloquent relationship with sale + public function sale() + { + return $this->belongsTo(Sale::class, 'sale_id', 'id'); + } + + // eloquent relationship with medicine + public function medicine() + { + return $this->belongsTo(Medicine::class, 'medicine_id', 'id'); + } } diff --git a/app/Models/Medicine.php b/app/Models/Medicine.php index ea3114a..3af163f 100644 --- a/app/Models/Medicine.php +++ b/app/Models/Medicine.php @@ -38,4 +38,10 @@ protected static function maxIdNumber() 'discount', 'price', ]; + + // eloquent relationship with detail_sale + public function detailSales() + { + return $this->hasMany(DetailSale::class, 'medicine_id', 'id'); + } } diff --git a/app/Models/Sale.php b/app/Models/Sale.php index d677661..11317bf 100644 --- a/app/Models/Sale.php +++ b/app/Models/Sale.php @@ -9,9 +9,42 @@ class Sale extends Model { use HasFactory; + protected $primaryKey = 'id'; + public $incrementing = false; + + protected static function boot() + { + parent::boot(); + + static::creating(function ($sale) { + $sale->id = 'S' . str_pad(static::maxIdNumber() + 1, 4, '0', STR_PAD_LEFT); + }); + } + + protected static function maxIdNumber() + { + $maxId = static::max('id'); + if ($maxId) { + return intval(substr($maxId, 1)); + } + return 0; + } + protected $fillable = [ 'cashier_id', 'date', 'time', ]; + + // eloquent relationship with user + public function cashier() + { + return $this->belongsTo(User::class, 'cashier_id', 'id'); + } + + // eloquent relationship with sale_detail + public function detailSales() + { + return $this->hasMany(DetailSale::class, 'sale_id', 'id'); + } } diff --git a/app/Models/User.php b/app/Models/User.php index bbee82f..3b7b37e 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -71,4 +71,10 @@ public function getJoiningDateAttribute($value) 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; + + // eloquent relationship with sale + public function sales() + { + return $this->hasMany(Sale::class, 'cashier_id', 'id'); + } } From 3b2321a0c6d8b2384bbafe52d669ec78ce42d5c3 Mon Sep 17 00:00:00 2001 From: hafidhmuhammadakbar Date: Wed, 14 Jun 2023 22:56:08 +0700 Subject: [PATCH 2/2] edit model --- app/Models/DetailSale.php | 6 ++++-- app/Models/Sale.php | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/Models/DetailSale.php b/app/Models/DetailSale.php index 32611db..2df0f91 100644 --- a/app/Models/DetailSale.php +++ b/app/Models/DetailSale.php @@ -12,12 +12,14 @@ class DetailSale extends Model protected $primaryKey = 'id'; public $incrementing = false; - protected static function boot() + protected static function boot(): void { parent::boot(); static::creating(function ($detailSale) { - $detailSale->id = 'DS' . str_pad(static::maxIdNumber() + 1, 4, '0', STR_PAD_LEFT); + if (!$detailSale->id) { + $detailSale->id = 'DS' . str_pad(static::maxIdNumber() + 1, 4, '0', STR_PAD_LEFT); + } }); } diff --git a/app/Models/Sale.php b/app/Models/Sale.php index 11317bf..994b0cc 100644 --- a/app/Models/Sale.php +++ b/app/Models/Sale.php @@ -12,12 +12,14 @@ class Sale extends Model protected $primaryKey = 'id'; public $incrementing = false; - protected static function boot() + protected static function boot(): void { parent::boot(); static::creating(function ($sale) { - $sale->id = 'S' . str_pad(static::maxIdNumber() + 1, 4, '0', STR_PAD_LEFT); + if(!$sale->id) { + $sale->id = 'S' . str_pad(static::maxIdNumber() + 1, 4, '0', STR_PAD_LEFT); + } }); }