Changeset View
Changeset View
Standalone View
Standalone View
src/app/PackageSku.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use Illuminate\Database\Eloquent\Relations\Pivot; | use Illuminate\Database\Eloquent\Relations\Pivot; | ||||
/** | /** | ||||
* Link SKUs to Packages. | * Link SKUs to Packages. | ||||
*/ | */ | ||||
class PackageSku extends Pivot | class PackageSku extends Pivot | ||||
{ | { | ||||
protected $fillable = [ | protected $fillable = [ | ||||
'package_id', | 'package_id', | ||||
'sku_id', | 'sku_id', | ||||
'cost', | |||||
'qty' | 'qty' | ||||
]; | ]; | ||||
protected $casts = [ | protected $casts = [ | ||||
'cost' => 'integer', | |||||
'qty' => 'integer' | 'qty' => 'integer' | ||||
]; | ]; | ||||
/** | |||||
* Under this package, how much does this SKU cost? | |||||
* | |||||
* @return int The costs of this SKU under this package in cents. | |||||
*/ | |||||
public function cost() | |||||
{ | |||||
$costs = 0; | |||||
$units = $this->qty - $this->sku->units_free; | |||||
if ($units < 0) { | |||||
\Log::debug( | |||||
"Package {$this->package_id} is misconfigured for more free units than qty." | |||||
); | |||||
$units = 0; | |||||
} | |||||
$ppu = $this->sku->cost * ((100 - $this->package->discount_rate) / 100); | |||||
$costs += $units * $ppu; | |||||
return $costs; | |||||
} | |||||
public function package() | |||||
{ | |||||
return $this->belongsTo('App\Package'); | |||||
} | |||||
public function sku() | |||||
{ | |||||
return $this->belongsTo('App\Sku'); | |||||
} | |||||
} | } |