Commit 107bc3e6 by Angel MAS

cart complete

parent 92fb5438
......@@ -9,6 +9,11 @@ use Yajra\Datatables\Datatables;
use Onestartup\Shop\Model\ProductShop as Product;
use Onestartup\Shop\Model\ProductCategoryShop as Category;
use Onestartup\Shop\Model\ShippingPrice as Shipping;
use Onestartup\Shop\Model\ClientShop as Client;
use Onestartup\Shop\Model\ShippingAddres as Addres;
use Onestartup\Shop\Model\SaleShop as Sale;
use Onestartup\Shop\Model\DetailShop as Item;
class CartController extends Controller
{
......@@ -24,7 +29,7 @@ class CartController extends Controller
$total = $this->total();
$cart = \Session::get('cart');
return view('shop-public::show-cart')
return view('shop-public::cart.show-cart')
->with('cart', $cart)
->with('total', $total);
}
......@@ -81,17 +86,120 @@ class CartController extends Controller
return redirect()->route('cart.show');
}
public function finish()
{
public function shipping()
{
//\Session::forget('client');
$client = new Client();
$addres = new Addres();
if(count(\Session::get('cart')) <= 0) {return redirect()->route('cart.show');}
if(\Session::has('client')) {
$client = \Session::get('client');
$addres = $client->shipping;
}
$cart = \Session::get('cart');
$total = $this->total();
$shipping = Shipping::select(
\DB::raw("CONCAT(name,' $',cost) AS name"),'id')
->pluck('name', 'id');
return view('shop-public::finish')
return view('shop-public::cart.shipping')
->with('cart', $cart)
->with('total', $total)
->with('client', $client)
->with('addres', $addres)
->with('shipping', $shipping);
}
public function storeClient(Request $request)
{
$client = new Client();
$addres = new Addres();
$cart = \Session::get('cart');
if(\Session::has('client')) {
$client = \Session::get('client');
$addres = $client->shipping;
}
/** Crea cliente ········· */
$client->fill($request->all());
$client->save();
/** Crea forma de envio ····· */
$addres->fill($request->all());
if (!isset($addres->client_id)) {
$addres->client_id = $client->id;
}
$addres->save();
/** Guarda productos en tabla ······ */
$costoEnvio = $addres->shipping_price->cost;
if (!\Session::has('sale')) {
$sale = $client->sales()->save(new Sale(['status' => 1, 'total'=>($this->total() + $costoEnvio)]));
foreach ($cart as $product) {
$item = Item::create([
'product_id'=> $product->id,
'sale_id'=> $sale->id,
'quantity'=> $product->quantity
]);
}
} else {
$sale = Sale::find(\Session::get('sale')->id);
$sale->total = ($this->total() + $costoEnvio);
$sale->save();
}
\Session::put('sale', $sale);
\Session::put('client', $client);
return redirect()->route('cart.finish');
}
public function finish()
{
if (!\Session::has('client')) {
return redirect()->route('cart.shipping');
}
if (!\Session::has('sale')) {
return redirect()->route('cart.shipping');
}
$sale = Sale::find(\Session::get('sale')->id);
$client = $sale->client;
$items = $sale->items;
$addres = $client->shipping;
return view('shop-public::cart.finish')
->with('sale', $sale)
->with('client', $client)
->with('items', $items)
->with('total', $this->total())
->with('addres', $addres);
}
public function cancel()
{
$sale = Sale::find(\Session::get('sale')->id);
$sale->status = 0;
$sale->save();
\Session::forget('sale');
return redirect()->route('cart.show');
}
private function total()
{
......
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateShippingAddresTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('shipping_addres', function (Blueprint $table) {
$table->increments('id');
$table->string('cp');
$table->string('calle');
$table->string('numero');
$table->string('colonia');
$table->string('ciudad');
$table->string('estado');
$table->integer('client_id')->unsigned()->nullable();
$table->foreign('client_id')
->references('id')
->on('client_shops')
->onDelete('cascade');
$table->integer('shipping_price_id')->unsigned()->nullable();
$table->foreign('shipping_price_id')
->references('id')
->on('shipping_prices')
->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('shipping_addres');
}
}
......@@ -13,5 +13,15 @@ class ClientShop extends Model
'email',
'phone'
];
public function shipping()
{
return $this->hasOne('Onestartup\Shop\Model\ShippingAddres', 'client_id');
}
public function sales()
{
return $this->hasMany('Onestartup\Shop\Model\SaleShop', 'client_id');
}
}
......@@ -8,4 +8,14 @@ class DetailShop extends Model
{
protected $table = "detail_shops";
protected $fillable = ['product_id', 'sale_id', 'quantity'];
public function sale()
{
return $this->belongsTo('Onestartup\Shop\Model\SaleShop', 'sale_id');
}
public function product()
{
return $this->belongsTo('Onestartup\Shop\Model\ProductShop', 'product_id');
}
}
......@@ -50,4 +50,9 @@ class ProductShop extends Model
{
return $this->hasOne('Onestartup\Shop\Model\ProductShopInfo', 'product_id');
}
public function items()
{
return $this->hasMany('Onestartup\Shop\Model\DetailShop', 'product_id');
}
}
......@@ -7,5 +7,16 @@ use Illuminate\Database\Eloquent\Model;
class SaleShop extends Model
{
protected $table = 'sale_shops';
protected $fillable = ['state', 'total', 'client_id'];
protected $fillable = ['status', 'total', 'client_id'];
public function client()
{
return $this->belongsTo('Onestartup\Shop\Model\ClientShop', 'client_id');
}
public function items()
{
return $this->hasMany('Onestartup\Shop\Model\DetailShop', 'sale_id');
}
}
<?php
namespace Onestartup\Shop\Model;
use Illuminate\Database\Eloquent\Model;
class ShippingAddres extends Model
{
protected $table = 'shipping_addres';
protected $fillable = [
'cp',
'calle',
'numero',
'colonia',
'ciudad',
'estado',
'client_id',
'shipping_price_id'
];
public function client()
{
return $this->belongsTo('Onestartup\Shop\Model\ClientShop', 'client_id');
}
public function shipping_price()
{
return $this->belongsTo('Onestartup\Shop\Model\ShippingPrice', 'shipping_price_id');
}
}
......@@ -9,4 +9,9 @@ class ShippingPrice extends Model
protected $table = 'shipping_prices';
protected $fillable = ['name', 'cost', 'active'];
public function shippings()
{
return $this->hasMany('Onestartup\Shop\Model\ShippingAddres', 'shipping_price_id');
}
}
......@@ -54,7 +54,11 @@ Route::group(['middleware' => ['web']], function(){
Route::get('cart/add/{product_slug}', 'Onestartup\Shop\Controller\CartController@add')->name('cart.add');
Route::get('cart/remove/{product_slug}', 'Onestartup\Shop\Controller\CartController@remove')->name('cart.remove');
Route::get('cart/trash', 'Onestartup\Shop\Controller\CartController@trash')->name('cart.trash');
Route::get('cart/shipping', 'Onestartup\Shop\Controller\CartController@shipping')->name('cart.shipping');
Route::get('cart/finish', 'Onestartup\Shop\Controller\CartController@finish')->name('cart.finish');
Route::get('cart/cancel', 'Onestartup\Shop\Controller\CartController@cancel')->name('cart.cancel');
Route::post('cart/store/client', 'Onestartup\Shop\Controller\CartController@storeClient')->name('cart.store.client');
Route::get('cart/update/{product_slug}/{quantity}', 'Onestartup\Shop\Controller\CartController@update')
->where('quantity', '[0-9]+')
->name('cart.update');
......
@extends('shop-public::layout')
@section('pageTitle', 'Finalizar compra')
@section('content')
@php
setlocale(LC_MONETARY, 'en_US');
@endphp
<code>
Variables disponibles:
<ul>
<li>$sale</li>
<li>$client</li>
<li>$items</li>
<li>$addres</li>
</ul>
</code>
<section>
<h4>Detalle del pedido <span><a href="{{route('cart.cancel')}}">Modificar mi carrito de compras</a></span></h4>
<table class="table" style="width: 60%;">
<thead>
<tr>
<th>Producto</th>
<th>Precio</th>
<th>Cantidad</th>
<th>Subtotal</th>
<th></th>
</tr>
</thead>
@foreach($items as $item)
<tbody>
<tr align="center">
<td>{{$item->product->name}}</td>
<td>{{money_format('%(#10n', $item->product->infoSale->sale_price)}}</td>
<td>{{$item->quantity}}</td>
<td>
{{money_format('%(#10n', ($item->product->infoSale->sale_price * $item->quantity))}}
</td>
</tr>
</tbody>
@endforeach
<tr>
<td colspan="3" align="right">
Total + Envio:
</td>
<td align="center">
{{ money_format('%(#10n', ($total)) }} +
{{ money_format('%(#10n', $addres->shipping_price->cost) }} =
{{ money_format('%(#10n', ($total + $addres->shipping_price->cost)) }}
</td>
</tr>
</table>
</section>
<section>
<h4>Formulario de pago</h4>
<pre>
form
</pre>
</section>
@endsection
@section('scripts_extra')
@endsection
\ No newline at end of file
@extends('shop-public::layout')
@section('pageTitle', 'Finalizar compra')
@section('content')
@php
setlocale(LC_MONETARY, 'en_US');
@endphp
<code>
Variables disponibles:
<ul>
<li>$shipping</li>
<li>$total</li>
<li>$cart</li>
</ul>
</code>
<section>
<h4>Detalles del pedido</h4>
<table class="table" style="width: 60%;">
<thead>
<tr>
<th>Producto</th>
<th>Precio</th>
<th>Cantidad</th>
<th>Subtotal</th>
<th></th>
</tr>
</thead>
@foreach($cart as $product)
<tbody>
<tr align="center">
<td>{{$product->name}}</td>
<td>{{money_format('%(#10n', $product->infoSale->sale_price)}}</td>
<td>{{$product->quantity}}</td>
<td>
{{money_format('%(#10n', ($product->infoSale->sale_price * $product->quantity))}}
</td>
</tr>
</tbody>
@endforeach
<tr>
<td colspan="3" align="right">
Total:
</td>
<td align="center">
{{money_format('%(#10n', ($total))}}
</td>
</tr>
</table>
</section>
<section>
<h4>Datos del comprador</h4>
{!! Form::model($client, ['route'=>'cart.store.client','method'=>'POST']) !!}
@include('shop-public::forms.fields-client')
</section>
<section>
<h4>Datos de envio</h4>
@include('shop-public::forms.fields-shipping')
<br>
{!! Form::submit('Enviar información', ['class'=>'']) !!}
{!! Form::close() !!}
</section>
@endsection
@section('scripts_extra')
@endsection
\ No newline at end of file
......@@ -71,7 +71,7 @@ setlocale(LC_MONETARY, 'en_US');
<a href="{{route('main.shop')}}">Ver mas productos</a>
</p>
<p>
<a href="#">Finalizar compra</a>
<a href="{{route('cart.shipping')}}">Finalizar compra</a>
</p>
</div>
......
@extends('shop-public::layout')
@section('pageTitle', 'Finalizar compra')
@section('content')
@php
setlocale(LC_MONETARY, 'en_US');
@endphp
<code>
Variables disponibles:
<ul>
<li>$shipping</li>
<li>$total</li>
<li>\Session::get('cart')</li>
</ul>
</code>
{!! Form::open(['method'=>'POST', 'id'=>'finalize']) !!}
<div>
<h3>Informacion general</h3>
<section>
<div class="form-group">
{!! Form::label('name', 'Nombre(s)', ['class'=>'']) !!}
{!! Form::text('name', null, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('lastname', 'Apellidos', ['class'=>'']) !!}
{!! Form::text('lastname', null, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('correo', 'Correo', ['class'=>'']) !!}
{!! Form::text('correo', null, ['class'=>'required email']) !!}
</div>
<div class="form-group">
{!! Form::label('phone', 'Teléfono', ['class'=>'']) !!}
{!! Form::text('phone', null, ['class'=>'required', 'id'=>'phone']) !!}
</div>
</section>
<h3>Información de envio</h3>
<section>
<div class="form-group">
{!! Form::label('cp', 'Codigo postal', ['class'=>'']) !!}
{!! Form::text('cp', null, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('calle', 'Calle', ['class'=>'']) !!}
{!! Form::text('calle', null, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('numero', 'Numero', ['class'=>'']) !!}
{!! Form::text('numero', null, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('colonia', 'Colonia', ['class'=>'']) !!}
{!! Form::text('colonia', null, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('ciudad', 'Ciudad o delegación', ['class'=>'']) !!}
{!! Form::text('ciudad', null, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('estado', 'Estado', ['class'=>'']) !!}
{!! Form::text('estado', null, ['class'=>'required']) !!}
</div>
</section>
<h3>Medios de pago</h3>
<section>
Formulario de pagos
</section>
<h3>Finish</h3>
<section>
<input id="acceptTerms" name="acceptTerms" type="checkbox" class="required"> <label for="acceptTerms">I agree with the Terms and Conditions.</label>
</section>
</div>
{!! Form::close() !!}
@endsection
@section('scripts_extra')
<script type="text/javascript">
var form = $("#finalize");
form.children("div").steps({
headerTag: "h3",
bodyTag: "section",
transitionEffect: "slideLeft",
onStepChanging: function (event, currentIndex, newIndex)
{
form.validate().settings.ignore = ":disabled,:hidden";
return form.valid();
},
onFinishing: function (event, currentIndex)
{
form.validate().settings.ignore = ":disabled";
return form.valid();
},
onFinished: function (event, currentIndex)
{
alert("Submitted!");
}
});
</script>
@endsection
\ No newline at end of file
<div class="form-group">
{!! Form::label('name', 'Nombre(s)', ['class'=>'']) !!}
{!! Form::text('name', null, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('lastname', 'Apellidos', ['class'=>'']) !!}
{!! Form::text('lastname', null, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Correo', ['class'=>'']) !!}
{!! Form::text('email', null, ['class'=>'required email']) !!}
</div>
<div class="form-group">
{!! Form::label('phone', 'Teléfono', ['class'=>'']) !!}
{!! Form::text('phone', null, ['class'=>'required', 'id'=>'phone']) !!}
</div>
\ No newline at end of file
<div class="form-group">
{!! Form::label('cp', 'Codigo postal', ['class'=>'']) !!}
{!! Form::text('cp', $addres->cp, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('calle', 'Calle', ['class'=>'']) !!}
{!! Form::text('calle', $addres->calle, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('numero', 'Numero', ['class'=>'']) !!}
{!! Form::text('numero', $addres->numero, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('colonia', 'Colonia', ['class'=>'']) !!}
{!! Form::text('colonia', $addres->colonia, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('ciudad', 'Ciudad o delegación', ['class'=>'']) !!}
{!! Form::text('ciudad', $addres->ciudad, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('estado', 'Estado', ['class'=>'']) !!}
{!! Form::text('estado', $addres->estado, ['class'=>'required']) !!}
</div>
<div class="form-group">
{!! Form::label('shipping_price_id', 'Tipo de envio', ['class'=>'']) !!}
{!! Form::select('shipping_price_id', $shipping, $addres->shipping_price_id != null ? $addres->shipping_price_id : null,['class'=>'required']) !!}
</div>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment