Skip to content

Commit

Permalink
Merge pull request #13 from alfikiafan/hafidh
Browse files Browse the repository at this point in the history
Hafidh
  • Loading branch information
hafidhmuhammadakbar authored Jun 14, 2023
2 parents 8e20961 + 3b2321a commit a17b150
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/Models/DetailSale.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ class DetailSale extends Model
{
use HasFactory;

protected $primaryKey = 'id';
public $incrementing = false;

protected static function boot(): void
{
parent::boot();

static::creating(function ($detailSale) {
if (!$detailSale->id) {
$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',
Expand All @@ -17,4 +40,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');
}
}
6 changes: 6 additions & 0 deletions app/Models/Medicine.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ protected static function maxIdNumber(): int
'discount',
'price',
];

// eloquent relationship with detail_sale
public function detailSales()
{
return $this->hasMany(DetailSale::class, 'medicine_id', 'id');
}
}
35 changes: 35 additions & 0 deletions app/Models/Sale.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,44 @@ class Sale extends Model
{
use HasFactory;

protected $primaryKey = 'id';
public $incrementing = false;

protected static function boot(): void
{
parent::boot();

static::creating(function ($sale) {
if(!$sale->id) {
$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');
}
}
6 changes: 6 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,10 @@ public function getJoiningDateAttribute($value): string
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

// eloquent relationship with sale
public function sales()
{
return $this->hasMany(Sale::class, 'cashier_id', 'id');
}
}

0 comments on commit a17b150

Please sign in to comment.