Merge pull request #38 from bagisto/jitendra

Order create
This commit is contained in:
JItendra Singh 2018-10-05 11:50:05 +05:30 committed by GitHub
commit 3666606e14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 339 additions and 91 deletions

View File

@ -238,12 +238,35 @@ class Cart {
*/
public function getCart()
{
if(!$cart = session()->get('cart'))
return false;
if(session()->has('cart')) {
$cart = session()->get('cart');
} else if(auth()->guard('customer')->check()) {
return $cart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id);
}
return $this->cart->find($cart->id);
}
/**
* Returns cart details in array
*
* @return array
*/
public function toArray()
{
$cart = $this->getCart();
$data = $cart->toArray();
$data['shipping_address'] = current($data['shipping_address']);
$data['billing_address'] = current($data['billing_address']);
$data['selected_shipping_rate'] = $cart->selected_shipping_rate->toArray();
return $data;
}
/**
* Method to check if the product is available and its required quantity
* is available or not in the inventory sources.
@ -536,7 +559,7 @@ class Cart {
$shippingAddress = $data['shipping'];
$billingAddress['cart_id'] = $shippingAddress['cart_id'] = $cart->id;
if($billingAddressModel = $cart->biling_address) {
if($billingAddressModel = $cart->billing_address) {
$this->cartAddress->update($billingAddress, $billingAddressModel->id);
if($shippingAddress = $cart->shipping_address) {
@ -791,9 +814,7 @@ class Cart {
}
/**
* Destroys the session
* maintained for cart
* on customer logout.
* Destroys the session maintained for cart on customer logout.
*
* @return response
*/
@ -807,4 +828,89 @@ class Cart {
return redirect()->back();
}
}
/**
* Deactivates current cart
*
* @return void
*/
public function deActivateCart()
{
if($cart = $this->getCart()) {
$this->cart->update($cart->id, ['is_active' => false]);
}
}
/**
* Validate order before creation
*
* @return array
*/
public function prepareDataForOrder()
{
$data = $this->toArray();
$finalData = [
'customer_id' => $data['customer_id'],
'is_guest' => $data['is_guest'],
'customer_email' => $data['customer_email'],
'customer_first_name' => $data['customer_first_name'],
'customer_last_name' => $data['customer_last_name'],
'customer' => auth()->guard('customer')->user,
'shipping_method' => $data['selected_shipping_rate']['method'],
'shipping_description' => $data['selected_shipping_rate']['method_description'],
'shipping_amount' => $data['selected_shipping_rate']['price'],
'base_shipping_amount' => $data['selected_shipping_rate']['base_price'],
'total_item_count' => $data['items_count'],
'total_qty_ordered' => $data['items_qty'],
'base_currency_code' => $data['base_currency_code'],
'channel_currency_code' => $data['channel_currency_code'],
'order_currency_code' => $data['cart_currency_code'],
'grand_total' => $data['grand_total'],
'base_grand_total' => $data['base_grand_total'],
'sub_total' => $data['sub_total'],
'base_sub_total' => $data['base_sub_total'],
'shipping_address' => array_except($data['shipping_address'], ['id', 'cart_id']),
'billing_address' => array_except($data['billing_address'], ['id', 'cart_id']),
'payment' => array_except($data['payment'], ['id', 'cart_id']),
];
foreach($data['items'] as $item) {
$finalData['items'][] = $this->prepareDataForOrderItem($item);
}
return $finalData;
}
/**
* Prepares data for order item
*
* @return array
*/
public function prepareDataForOrderItem($data)
{
$finalData = [
'product' => $this->product->find($data['product_id']),
'sku' => $data['sku'],
'type' => $data['type'],
'name' => $data['name'],
'weight' => $data['weight'],
'total_weight' => $data['total_weight'],
'qty_ordered' => $data['quantity'],
'price' => $data['price'],
'base_price' => $data['base_price'],
'total' => $data['total'],
'base_total' => $data['base_total'],
'additional' => $data['additional'],
];
if(isset($data['child']) && $data['child']) {
$finalData['child'] = $this->prepareDataForOrderItem($data['child']);
}
return $finalData;
}
}

View File

@ -15,10 +15,11 @@ class CreateCartTable extends Migration
{
Schema::create('cart', function (Blueprint $table) {
$table->increments('id');
$table->integer('customer_id')->unsigned()->nullable();
$table->foreign('customer_id')->references('id')->on('customers')->onDelete('cascade');
$table->integer('channel_id')->unsigned();
$table->foreign('channel_id')->references('id')->on('channels')->onDelete('cascade');
$table->string('customer_email')->nullable();
$table->string('customer_first_name')->nullable();
$table->string('customer_last_name')->nullable();
$table->string('shipping_method')->nullable();
$table->string('coupon_code')->nullable();
$table->boolean('is_gift')->default(0);
@ -27,8 +28,8 @@ class CreateCartTable extends Migration
$table->decimal('exchange_rate', 12, 4)->nullable();
$table->string('global_currency_code')->nullable();
$table->string('base_currency_code')->nullable();
$table->string('store_currency_code')->nullable();
$table->string('quote_currency_code')->nullable();
$table->string('channel_currency_code')->nullable();
$table->string('cart_currency_code')->nullable();
$table->decimal('grand_total', 12, 4)->default(0)->nullable();
$table->decimal('base_grand_total', 12, 4)->default(0)->nullable();
$table->decimal('sub_total', 12, 4)->default(0)->nullable();
@ -37,9 +38,14 @@ class CreateCartTable extends Migration
$table->decimal('base_sub_total_with_discount', 12, 4)->default(0)->nullable();
$table->string('checkout_method')->nullable();
$table->boolean('is_guest')->nullable();
$table->boolean('is_active')->nullable()->default(0);
$table->boolean('is_active')->nullable()->default(1);
$table->string('customer_full_name')->nullable();
$table->dateTime('conversion_time')->nullable();
$table->integer('customer_id')->unsigned()->nullable();
$table->foreign('customer_id')->references('id')->on('customers')->onDelete('cascade');
$table->integer('channel_id')->unsigned();
$table->foreign('channel_id')->references('id')->on('channels')->onDelete('cascade');
$table->timestamps();
});
}

View File

@ -44,53 +44,6 @@ class CartComposer
}
public function compose(View $view) {
// session()->forget('cart');
// return redirect()->back();
if(auth()->guard('customer')->check()) {
$cart = $this->cart->findOneByField('customer_id', auth()->guard('customer')->user()->id);
if(isset($cart)) {
$cartItems = $this->cart->items($cart['id']);
$products = array();
foreach($cartItems as $cartItem) {
$image = $this->productImage->getGalleryImages($cartItem->product);
if(isset($image[0]['small_image_url'])) {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, $image[0]['small_image_url'], $cartItem->quantity];
}
else {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, 'null', $cartItem->quantity];
}
}
session()->put('cart', $cart);
$view->with('cart', $products);
}
} else {
if($cart = session()->get('cart')) {
if(isset($cart)) {
$cartItems = $this->cart->items($cart['id']);
$products = array();
foreach($cartItems as $cartItem) {
$image = $this->productImage->getGalleryImages($cartItem->product);
if(isset($image[0]['small_image_url'])) {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, $image[0]['small_image_url'], $cartItem->quantity];
}
else {
$products[$cartItem->product->id] = [$cartItem->product->name, $cartItem->price, 'null', $cartItem->quantity];
}
}
$view->with('cart', $products);
}
}
}
}
}

View File

@ -4,7 +4,7 @@
if (! function_exists('cart')) {
function cart()
{
return new Cart;
return app()->make(Core::class);
}
}
?>

View File

@ -17,6 +17,8 @@ class Cart extends Model
protected $hidden = ['coupon_code'];
protected $with = ['items', 'items.child', 'shipping_address', 'billing_address', 'selected_shipping_rate', 'payment'];
public function items() {
return $this->hasMany(CartItem::class)->whereNull('parent_id');
}
@ -73,12 +75,20 @@ class Cart extends Model
return $this->hasManyThrough(CartShippingRate::class, CartAddress::class, 'cart_id', 'cart_address_id');
}
/**
* Get all of the attributes for the attribute groups.
*/
public function selected_shipping_rate()
{
return $this->shipping_rates()->where('method', $this->shipping_method);
}
/**
* Get all of the attributes for the attribute groups.
*/
public function getSelectedShippingRateAttribute()
{
return $this->shipping_rates()->where('method', $this->shipping_method)->first();
return $this->selected_shipping_rate()->where('method', $this->shipping_method)->first();
}
/**

View File

@ -44,7 +44,6 @@ class CheckoutServiceProvider extends ServiceProvider
*/
protected function registerFacades()
{
//to make the cart facade and bind the
//alias to the class needed to be called.
$loader = AliasLoader::getInstance();

View File

@ -65,16 +65,4 @@ class CartRepository extends Repository
return $this->model->destroy($cart_id);
}
/**
* Used to get items
* for cart explicitly,
* use cart instance
* instead.
*
* @return Mixed
*/
public function items($cartId) {
return $this->model->find($cartId)->items;
}
}

View File

@ -0,0 +1,7 @@
<?php
namespace Webkul\Sales\Contracts;
interface OrderItemInventory
{
}

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOrderItemInventories extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('order_item_inventories', function (Blueprint $table) {
$table->increments('id');
$table->integer('qty')->default(0);
$table->integer('inventory_source_id')->unsigned()->nullable();
$table->integer('order_item_id')->unsigned()->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('order_item_inventories');
}
}

View File

@ -7,7 +7,7 @@ use Webkul\Sales\Contracts\Order as OrderContract;
class Order extends Model implements OrderContract
{
protected $guarded = ['id', 'created_at', 'updated_at'];
protected $guarded = ['id', 'items', 'shipping_address', 'billing_address', 'payment', 'created_at', 'updated_at'];
/**
* Get the order items record associated with the order.

View File

@ -10,6 +10,8 @@ class OrderAddress extends Model implements OrderAddressContract
{
protected $table = 'order_address';
protected $guarded = ['id', 'created_at', 'updated_at'];
/**
* Get of the customer fullname.
*/

View File

@ -7,6 +7,8 @@ use Webkul\Sales\Contracts\OrderItem as OrderItemContract;
class OrderItem extends Model implements OrderItemContract
{
protected $guarded = ['id', 'child', 'created_at', 'updated_at'];
/**
* Get the order record associated with the order item.
*/
@ -30,4 +32,11 @@ class OrderItem extends Model implements OrderItemContract
{
return $this->belongsTo(OrderItemProxy::modelClass(), 'parent_id');
}
/**
* Get the inventories record associated with the order item.
*/
public function inventories() {
return $this->hasMany(CartItemInventoyrProxy::modelClass());
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Webkul\Sales\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Sales\Contracts\OrderItemInventory as OrderItemInventoryContract;
class OrderItemInventory extends Model implements OrderItemInventoryContract
{
protected $guarded = ['id', 'child', 'created_at', 'updated_at'];
/**
* Get the order item record associated with the order item inventory.
*/
public function order_item()
{
return $this->belongsTo(OrderItemProxy::modelClass());
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace Webkul\Sales\Models;
use Konekt\Concord\Proxies\ModelProxy;
class OrderItemInventoryProxy extends ModelProxy
{
}

View File

@ -9,4 +9,6 @@ use Webkul\Sales\Contracts\OrderPayment as OrderPaymentContract;
class OrderPayment extends Model implements OrderPaymentContract
{
protected $table = 'order_payment';
protected $guarded = ['id', 'created_at', 'updated_at'];
}

View File

@ -9,6 +9,7 @@ class ModuleServiceProvider extends BaseModuleServiceProvider
protected $models = [
\Webkul\Sales\Models\Order::class,
\Webkul\Sales\Models\OrderItem::class,
\Webkul\Sales\Models\OrderItemInventory::class,
\Webkul\Sales\Models\OrderAddress::class,
\Webkul\Sales\Models\OrderPayment::class,
\Webkul\Sales\Models\Invoice::class,

View File

@ -0,0 +1,27 @@
<?php
namespace Webkul\Sales\Repositories;
use Illuminate\Container\Container as App;
use Webkul\Core\Eloquent\Repository;
/**
* Order Item Inventory Reposotory
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class OrderItemInventoryRepository extends Repository
{
/**
* Specify Model class name
*
* @return Mixed
*/
function model()
{
return 'Webkul\Sales\Contracts\OrderItemInventory';
}
}

View File

@ -24,4 +24,18 @@ class OrderItemRepository extends Repository
{
return 'Webkul\Sales\Contracts\OrderItem';
}
/**
* @param array $data
* @return mixed
*/
public function create(array $data)
{
if(isset($data['product']) && $data['product'] instanceof Model) {
$data['product_id'] = $data['product']->id;
$data['product_type'] = get_class($data['product']);
}
return $this->model->create($data);
}
}

View File

@ -3,9 +3,12 @@
namespace Webkul\Sales\Repositories;
use Illuminate\Container\Container as App;
use Webkul\Core\Eloquent\Repository;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Webkul\Core\Eloquent\Repository;
use Webkul\Sales\Repositories\OrderItemRepository;
use Webkul\Sales\Repositories\OrderItemInventoryRepository;
/**
* Order Reposotory
@ -23,19 +26,30 @@ class OrderRepository extends Repository
*/
protected $orderItem;
/**
* OrderItemInventoryRepository object
*
* @var Object
*/
protected $orderItemInventory;
/**
* Create a new repository instance.
*
* @param Webkul\Sales\Repositories\OrderItemRepository $orderItem
* @param Webkul\Sales\Repositories\OrderItemRepository $orderItem
* @param Webkul\Sales\Repositories\OrderItemInventoryRepository $orderItemInventory
* @return void
*/
public function __construct(
OrderItemRepository $orderItem,
OrderItemInventoryRepository $orderItemInventory,
App $app
)
{
$this->orderItem = $orderItem;
$this->orderItemInventory = $orderItemInventory;
parent::__construct($app);
}
@ -56,15 +70,33 @@ class OrderRepository extends Repository
*/
public function create(array $data)
{
$this->validateOrder();
DB::beginTransaction();
try {
die;
Event::fire('checkout.order.save.before', $data);
$order = null;
if(isset($data['customer']) && $data['customer'] instanceof Model) {
$data['customer_id'] = $data['customer']->id;
$data['customer_type'] = get_class($data['customer']);
}
$order = $this->model->create(array_merge($data, ['increment_id' => $this->generateIncrementId()]));
$order->payment()->create($data['payment']);
$order->addresses()->create($data['shipping_address']);
$order->addresses()->create($data['billing_address']);
foreach($data['items'] as $item) {
$orderItem = $this->orderItem->create(array_merge($item, ['order_id' => $order->id]));
if(isset($item['child']) && $item['child']) {
$orderItem = $this->orderItem->create(array_merge($item['child'], ['order_id' => $order->id, 'parent_id' => $orderItem->id]));
}
}
} catch (\Exception $e) {
DB::rollBack();
@ -77,4 +109,16 @@ class OrderRepository extends Repository
return $order;
}
/**
* @inheritDoc
*/
public function generateIncrementId()
{
$lastOrder = $this->orderBy('id', 'desc')->limit(1)->first();
$lastId = $lastOrder ? $lastOrder->id : 0;
return 000000000 + $last + 1;
}
}

View File

@ -121,7 +121,9 @@ class OnepageController extends Controller
$this->validateOrder();
$order = $this->orderRepository->create([]);
$order = $this->orderRepository->create(Cart::prepareDataForOrder());
Cart::deActivateCart();
session()->flash('order', $order);
@ -150,5 +152,22 @@ class OnepageController extends Controller
*/
public function validateOrder()
{
$cart = Cart::getCart();
if(!$cart->shipping_address) {
throw new \Exception(trans('Please check shipping address.'));
}
if(!$cart->billing_address) {
throw new \Exception(trans('Please check billing address.'));
}
if(!$cart->selected_shipping_rate) {
throw new \Exception(trans('Please specify shipping method.'));
}
if(!$cart->payment) {
throw new \Exception(trans('Please specify payment method.'));
}
}
}

View File

@ -421,6 +421,7 @@ section.slider-block {
}
}
}
ul.right-responsive {
display: none;

View File

@ -69,7 +69,7 @@ return [
'continue-shopping' => 'Continue Shopping',
'proceed-to-checkout' => 'Proceed To Checkout',
'quantity' => 'Quantity',
'remove' => 'Remove',
'remove-link' => 'Remove',
'move-to-wishlist' => 'Move to Wishlist',
'quantity' => [
'quantity' => 'Quantity',

View File

@ -68,7 +68,7 @@
<span class="remove"><a href="{{ route('shop.checkout.cart.remove', $item->child->id) }}">{{ __('shop::app.checkout.cart.remove') }}</a></span>
@else --}}
<span class="remove"><a href="{{ route('shop.checkout.cart.remove', $item->id) }}">{{ __('shop::app.checkout.cart.remove') }}</a></span>
<span class="remove"><a href="{{ route('shop.checkout.cart.remove', $item->id) }}">{{ __('shop::app.checkout.cart.remove-link') }}</a></span>
{{-- @endif --}}
<span class="towishlist">{{ __('shop::app.checkout.cart.move-to-wishlist') }}</span>

View File

@ -76,7 +76,6 @@
</div>
<div class="reponsive-sorter-limiter mb-20">
<div class="sorter">
@ -115,6 +114,4 @@
<div class="responsive-layred-filter mb-20">
<layered-navigation></layered-navigation>
</div>
</div>

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
{
"/js/shop.js": "/js/shop.js",
"/css/shop.css": "/css/shop.css"
}
}