Skip to content

Commit

Permalink
Making orders taking into account shipping costs, discounts
Browse files Browse the repository at this point in the history
  • Loading branch information
vilgefortzz committed May 26, 2017
1 parent 39ec66a commit ea5cf01
Show file tree
Hide file tree
Showing 22 changed files with 306 additions and 141 deletions.
15 changes: 15 additions & 0 deletions app/Delivery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Delivery extends Model
{
protected $table = 'deliveries';

public function orders()
{
return $this->hasMany('App\Order');
}
}
27 changes: 23 additions & 4 deletions app/Http/Controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace App\Http\Controllers;

use App\Cart;
use App\Delivery;
use App\Order;
use App\OrderProduct;
use App\Payment;
use App\Product;
use Auth;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -33,7 +35,13 @@ public function showPlaceAnOrder(){
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);

return view('orders.place_an_order', ['products' => $cart->products, 'totalPrice' => $cart->totalPrice]);
// Delivery methods
$deliveries = Delivery::all();

// Payment methods
$payments = Payment::all();

return view('orders.place_an_order', ['products' => $cart->products, 'totalPrice' => $cart->totalPrice, 'deliveries' => $deliveries, 'payments' => $payments]);
}

public function store(Request $request){
Expand Down Expand Up @@ -65,13 +73,24 @@ public function store(Request $request){
// Create order
$order = new Order();

$discount = 0;

if (Auth::check()){
$order->user_id = Auth::user()->id;
$discount = Auth::user()->discount;
}

$order->total_paid = $cart->totalPrice;
$order->delivery_method = $request->delivery_methods;
$order->payment_method = $request->payment_methods;
$order->delivery_id = $request->delivery_methods;
$order->payment_id = $request->payment_methods;

$delivery = Delivery::find($order->delivery_id);

// Total paid for order
$totalPriceForProducts = $cart->totalPrice;
$shipping = $delivery->price;
$totalPaidForOrder = $totalPriceForProducts + $shipping - $discount;

$order->total_paid = $totalPaidForOrder;
$order->status = "Pending";
$order->save();

Expand Down
10 changes: 10 additions & 0 deletions app/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ public function orderProducts()
{
return $this->hasMany('App\OrderProduct');
}

public function delivery()
{
return $this->belongsTo('App\Delivery');
}

public function payment()
{
return $this->belongsTo('App\Payment');
}
}
13 changes: 13 additions & 0 deletions app/Payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Payment extends Model
{
public function orders()
{
return $this->hasMany('App\Order');
}
}
16 changes: 16 additions & 0 deletions database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@
'review' => $faker->text(500),
];
});

$factory->define(App\Delivery::class, function (Faker\Generator $faker) {

return [
'name' => $faker->unique()->word,
'price' => $faker->randomFloat(2, 5, 20)
];
});


$factory->define(App\Payment::class, function (Faker\Generator $faker) {

return [
'name' => $faker->unique()->word
];
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function up()
$table->string('city')->nullable();
$table->string('country')->nullable();
$table->string('phone_number')->nullable();
$table->float('discount')->unsigned()->default('20');
$table->rememberToken();
$table->timestamps();
});
Expand Down
8 changes: 6 additions & 2 deletions database/migrations/2017_03_05_190202_create_orders_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ public function up()
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->float('total_paid')->unsigned();
$table->string('delivery_method');
$table->string('payment_method');
$table->string('status');
$table->timestamps();

$table->integer('user_id')->index()->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade');

$table->integer('delivery_id')->index()->unsigned();
$table->foreign('delivery_id')->references('id')->on('deliveries');

$table->integer('payment_id')->index()->unsigned();
$table->foreign('payment_id')->references('id')->on('payments');
});
}

Expand Down
33 changes: 33 additions & 0 deletions database/migrations/2017_05_23_140629_create_payments_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePaymentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('payments', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('path_to_image')->default('/images/missing.png');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('payments');
}
}
34 changes: 34 additions & 0 deletions database/migrations/2017_05_23_140703_create_deliveries_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateDeliveriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('deliveries', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->float('price')->unsigned();
$table->string('path_to_image')->default('/images/missing.png');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('deliveries');
}
}
2 changes: 2 additions & 0 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public function run()
$this->call(SubcategoriesTableSeeder::class);
$this->call(ProductsTableSeeder::class);
//$this->call(ReviewsTableSeeder::class);
$this->call(DeliveriesTableSeeder::class);
$this->call(PaymentsTableSeeder::class);
}
}
29 changes: 29 additions & 0 deletions database/seeds/DeliveriesTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use App\Delivery;
use Illuminate\Database\Seeder;

class DeliveriesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$deliveryMethodsNames = ['InPost Courier', 'InPost paczkomat', 'UPS', 'Poczta Polska'];
$deliveryMethodsPrices = [8, 7, 12, 6];
$deliveryMethodsImages = ['/images/icons/inPost_courier.png', '/images/icons/inPost_paczkomat.jpg',
'/images/icons/ups.png', '/images/icons/poczta_polska.jpg'];


for ($i = 0; $i < count($deliveryMethodsNames); $i++) {
factory(Delivery::class)->create([
'name' => $deliveryMethodsNames[$i],
'price' => $deliveryMethodsPrices[$i],
'path_to_image' => $deliveryMethodsImages[$i]
]);
}
}
}
25 changes: 25 additions & 0 deletions database/seeds/PaymentsTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use App\Payment;
use Illuminate\Database\Seeder;

class PaymentsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$paymentMethodsNames = ['Credit card', 'Usual transfer'];
$paymentMethodsImages = ['/images/icons/credit_card.png', '/images/icons/usual_transfer.png'];

for ($i = 0; $i < count($paymentMethodsNames); $i++) {
factory(Payment::class)->create([
'name' => $paymentMethodsNames[$i],
'path_to_image' => $paymentMethodsImages[$i]
]);
}
}
}
30 changes: 3 additions & 27 deletions public/css/app.css

Large diffs are not rendered by default.

Loading

0 comments on commit ea5cf01

Please sign in to comment.