diff --git a/src/app/Console/Commands/PlanPackagesCommand.php b/src/app/Console/Commands/PlanPackagesCommand.php index 6125b5cf..73b1d105 100644 --- a/src/app/Console/Commands/PlanPackagesCommand.php +++ b/src/app/Console/Commands/PlanPackagesCommand.php @@ -1,86 +1,88 @@ info(sprintf("Plan: %s", $plan->title)); $plan_costs = 0; foreach ($plan->packages()->get() as $package) { $qtyMin = $package->pivot->qty_min; $qtyMax = $package->pivot->qty_max; $discountQty = $package->pivot->discount_qty; $discountRate = (100 - $package->pivot->discount_rate) / 100; $this->info( sprintf( " Package: %s (min: %d, max: %d, discount %d%% after the first %d, base cost: %d)", $package->title, $package->pivot->qty_min, $package->pivot->qty_max, $package->pivot->discount_rate, $package->pivot->discount_qty, $package->cost() ) ); foreach ($package->skus()->get() as $sku) { $this->info(sprintf(" SKU: %s (%d)", $sku->title, $sku->pivot->qty)); } - if ($qtyMin <= $discountQty) { + if ($qtyMin < $discountQty) { $plan_costs += $qtyMin * $package->cost(); + } elseif ($qtyMin == $discountQty) { + $plan_costs += $package->cost(); } else { // base rate $plan_costs += $discountQty * $package->cost(); // discounted rate $plan_costs += ($qtyMin - $discountQty) * $package->cost() * $discountRate; } } $this->info(sprintf(" Plan costs per month: %d", $plan_costs)); } } } diff --git a/src/app/Package.php b/src/app/Package.php index f7fea74c..3ebd3d7e 100644 --- a/src/app/Package.php +++ b/src/app/Package.php @@ -1,52 +1,57 @@ skus as $sku) { $costs += ($sku->pivot->qty - $sku->units_free) * $sku->cost; } return $costs; } public function skus() { - return $this->belongsToMany('App\Sku', 'package_skus')->using('App\PackageSku')->withPivot(['qty']); + return $this->belongsToMany( + 'App\Sku', + 'package_skus' + )->using('App\PackageSku')->withPivot( + ['qty'] + ); } } diff --git a/src/database/seeds/PackageSeeder.php b/src/database/seeds/PackageSeeder.php index 38129a2b..da9695d7 100644 --- a/src/database/seeds/PackageSeeder.php +++ b/src/database/seeds/PackageSeeder.php @@ -1,76 +1,76 @@ 'kolab', 'description' => 'A fully functional groupware account.', 'discount_rate' => 0 ] ); $skus = [ Sku::firstOrCreate(['title' => 'mailbox']), Sku::firstOrCreate(['title' => 'storage']), Sku::firstOrCreate(['title' => 'groupware']) ]; $package->skus()->saveMany($skus); // This package contains 2 units of the storage SKU, which just so happens to also // be the number of SKU free units. $package->skus()->updateExistingPivot( Sku::firstOrCreate(['title' => 'storage']), - array('qty' => 2), + ['qty' => 2], false ); $package = Package::create( [ 'title' => 'lite', 'description' => 'Just mail and no more.', 'discount_rate' => 0 ] ); $skus = [ Sku::firstOrCreate(['title' => 'mailbox']), Sku::firstOrCreate(['title' => 'storage']) ]; $package->skus()->saveMany($skus); $package->skus()->updateExistingPivot( Sku::firstOrCreate(['title' => 'storage']), ['qty' => 2], false ); $package = Package::create( [ 'title' => 'domain-hosting', - 'description' => 'Use your own domain.', + 'description' => 'Use your own, existing domain.', 'discount_rate' => 0 ] ); $skus = [ Sku::firstOrCreate(['title' => 'domain-hosting']) ]; $package->skus()->saveMany($skus); } } diff --git a/src/database/seeds/PlanSeeder.php b/src/database/seeds/PlanSeeder.php index 1b63ebcb..6ac81b5c 100644 --- a/src/database/seeds/PlanSeeder.php +++ b/src/database/seeds/PlanSeeder.php @@ -1,110 +1,143 @@ 'family', 'description' => 'A group of accounts for 2 or more users.', 'discount_qty' => 0, 'discount_rate' => 0 ] ); $packages = [ Package::firstOrCreate(['title' => 'kolab']), Package::firstOrCreate(['title' => 'domain-hosting']) ]; $plan->packages()->saveMany($packages); $plan->packages()->updateExistingPivot( Package::firstOrCreate(['title' => 'kolab']), [ 'qty_min' => 2, 'qty_max' => -1, 'discount_qty' => 2, 'discount_rate' => 50 ], false ); $plan = Plan::create( [ 'title' => 'small-business', 'description' => 'Accounts for small business owners.', 'discount_qty' => 0, 'discount_rate' => 10 ] ); $packages = [ Package::firstOrCreate(['title' => 'kolab']), Package::firstOrCreate(['title' => 'domain-hosting']) ]; $plan->packages()->saveMany($packages); $plan->packages()->updateExistingPivot( Package::firstOrCreate(['title' => 'kolab']), [ 'qty_min' => 5, 'qty_max' => 25, 'discount_qty' => 5, 'discount_rate' => 30 ], false ); $plan = Plan::create( [ 'title' => 'large-business', 'description' => 'Accounts for large businesses.', 'discount_qty' => 0, 'discount_rate' => 10 ] ); $packages = [ Package::firstOrCreate(['title' => 'kolab']), Package::firstOrCreate(['title' => 'lite']), Package::firstOrCreate(['title' => 'domain-hosting']) ]; $plan->packages()->saveMany($packages); $plan->packages()->updateExistingPivot( Package::firstOrCreate(['title' => 'kolab']), [ 'qty_min' => 20, 'qty_max' => -1, 'discount_qty' => 10, 'discount_rate' => 10 ], false ); $plan->packages()->updateExistingPivot( Package::firstOrCreate(['title' => 'lite']), [ 'qty_min' => 0, 'qty_max' => -1, 'discount_qty' => 10, 'discount_rate' => 10 ], false ); + */ + + $plan = Plan::create( + [ + 'title' => 'individual', + 'description' => "No friends", + 'discount_qty' => 0, + 'discount_rate' => 0 + ] + ); + + $packages = [ + Package::firstOrCreate(['title' => 'kolab']) + ]; + + $plan->packages()->saveMany($packages); + + $plan = Plan::create( + [ + 'title' => 'group', + 'description' => "Some or many friends", + 'discount_qty' => 0, + 'discount_rate' => 0 + ] + ); + + $packages = [ + Package::firstOrCreate(['title' => 'kolab']), + Package::firstOrCreate(['title' => 'domain-hosting']), + ]; + + $plan->packages()->saveMany($packages); } }