2018-09-28 12:54:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Sales\Repositories;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Container\Container as App;
|
2018-10-04 06:42:06 +00:00
|
|
|
use Illuminate\Support\Facades\Event;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
2018-10-05 06:18:58 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Webkul\Core\Eloquent\Repository;
|
|
|
|
|
use Webkul\Sales\Repositories\OrderItemRepository;
|
|
|
|
|
use Webkul\Sales\Repositories\OrderItemInventoryRepository;
|
2018-09-28 12:54:26 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Order Reposotory
|
|
|
|
|
*
|
|
|
|
|
* @author Jitendra Singh <jitendra@webkul.com>
|
|
|
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class OrderRepository extends Repository
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* OrderItemRepository object
|
|
|
|
|
*
|
|
|
|
|
* @var Object
|
|
|
|
|
*/
|
|
|
|
|
protected $orderItem;
|
|
|
|
|
|
2018-10-05 06:18:58 +00:00
|
|
|
/**
|
|
|
|
|
* OrderItemInventoryRepository object
|
|
|
|
|
*
|
|
|
|
|
* @var Object
|
|
|
|
|
*/
|
|
|
|
|
protected $orderItemInventory;
|
|
|
|
|
|
2018-09-28 12:54:26 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new repository instance.
|
|
|
|
|
*
|
2018-10-05 06:18:58 +00:00
|
|
|
* @param Webkul\Sales\Repositories\OrderItemRepository $orderItem
|
|
|
|
|
* @param Webkul\Sales\Repositories\OrderItemInventoryRepository $orderItemInventory
|
2018-09-28 12:54:26 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
OrderItemRepository $orderItem,
|
2018-10-05 06:18:58 +00:00
|
|
|
OrderItemInventoryRepository $orderItemInventory,
|
2018-09-28 12:54:26 +00:00
|
|
|
App $app
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
$this->orderItem = $orderItem;
|
|
|
|
|
|
2018-10-05 06:18:58 +00:00
|
|
|
$this->orderItemInventory = $orderItemInventory;
|
|
|
|
|
|
2018-09-28 12:54:26 +00:00
|
|
|
parent::__construct($app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Specify Model class name
|
|
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function model()
|
|
|
|
|
{
|
|
|
|
|
return 'Webkul\Sales\Contracts\Order';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function create(array $data)
|
|
|
|
|
{
|
2018-10-04 06:42:06 +00:00
|
|
|
DB::beginTransaction();
|
2018-10-15 10:45:29 +00:00
|
|
|
|
2018-10-04 06:42:06 +00:00
|
|
|
try {
|
|
|
|
|
Event::fire('checkout.order.save.before', $data);
|
|
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
if(isset($data['customer']) && $data['customer']) {
|
2018-10-05 06:18:58 +00:00
|
|
|
$data['customer_id'] = $data['customer']->id;
|
|
|
|
|
$data['customer_type'] = get_class($data['customer']);
|
2018-10-05 11:59:43 +00:00
|
|
|
} else {
|
|
|
|
|
unset($data['customer']);
|
2018-10-05 06:18:58 +00:00
|
|
|
}
|
2018-10-05 11:59:43 +00:00
|
|
|
|
2018-10-09 12:02:22 +00:00
|
|
|
if(isset($data['channel']) && $data['channel']) {
|
|
|
|
|
$data['channel_id'] = $data['channel']->id;
|
|
|
|
|
$data['channel_type'] = get_class($data['channel']);
|
|
|
|
|
$data['channel_name'] = $data['channel']->name;
|
|
|
|
|
} else {
|
|
|
|
|
unset($data['channel']);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 11:59:43 +00:00
|
|
|
$data['status'] = core()->getConfigData('paymentmethods.' . $data['payment']['method'] . '.status') ?? 'pending';
|
|
|
|
|
|
2018-10-05 06:18:58 +00:00
|
|
|
$order = $this->model->create(array_merge($data, ['increment_id' => $this->generateIncrementId()]));
|
2018-10-04 06:42:06 +00:00
|
|
|
|
2018-10-05 06:18:58 +00:00
|
|
|
$order->payment()->create($data['payment']);
|
|
|
|
|
|
|
|
|
|
$order->addresses()->create($data['shipping_address']);
|
2018-10-15 10:45:29 +00:00
|
|
|
|
2018-10-05 06:18:58 +00:00
|
|
|
$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']) {
|
2018-10-05 11:59:43 +00:00
|
|
|
$orderItem->child = $this->orderItem->create(array_merge($item['child'], ['order_id' => $order->id, 'parent_id' => $orderItem->id]));
|
2018-10-05 06:18:58 +00:00
|
|
|
}
|
2018-10-05 11:59:43 +00:00
|
|
|
|
|
|
|
|
$this->orderItemInventory->create(['orderItem' => $orderItem]);
|
2018-10-05 06:18:58 +00:00
|
|
|
}
|
2018-10-26 13:43:01 +00:00
|
|
|
|
|
|
|
|
Event::fire('checkout.order.save.after', $order);
|
2018-10-04 06:42:06 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
DB::rollBack();
|
|
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
2018-10-15 10:45:29 +00:00
|
|
|
|
2018-10-04 06:42:06 +00:00
|
|
|
DB::commit();
|
|
|
|
|
|
|
|
|
|
return $order;
|
2018-09-28 12:54:26 +00:00
|
|
|
}
|
2018-10-05 06:18:58 +00:00
|
|
|
|
2018-10-15 10:39:09 +00:00
|
|
|
/**
|
|
|
|
|
* @param int $orderId
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function cancel($orderId)
|
|
|
|
|
{
|
|
|
|
|
$order = $this->findOrFail($orderId);
|
|
|
|
|
|
|
|
|
|
if(!$order->canCancel())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
foreach($order->items as $item) {
|
|
|
|
|
if($item->qty_to_cancel) {
|
|
|
|
|
$item->qty_canceled += $item->qty_to_cancel;
|
|
|
|
|
|
|
|
|
|
$item->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->updateOrderStatus($order);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 06:18:58 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function generateIncrementId()
|
|
|
|
|
{
|
2018-10-05 11:59:43 +00:00
|
|
|
$lastOrder = $this->model->orderBy('id', 'desc')->limit(1)->first();
|
2018-10-05 06:18:58 +00:00
|
|
|
|
|
|
|
|
$lastId = $lastOrder ? $lastOrder->id : 0;
|
|
|
|
|
|
2018-10-05 11:59:43 +00:00
|
|
|
return $lastId + 1;
|
2018-10-05 06:18:58 +00:00
|
|
|
}
|
2018-10-09 12:02:22 +00:00
|
|
|
|
|
|
|
|
/**
|
2018-10-15 10:39:09 +00:00
|
|
|
* @param mixed $order
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function isInCompletedState($order)
|
|
|
|
|
{
|
|
|
|
|
$totalQtyOrdered = 0;
|
|
|
|
|
$totalQtyInvoiced = 0;
|
|
|
|
|
$totalQtyShipped = 0;
|
|
|
|
|
$totalQtyRefunded = 0;
|
|
|
|
|
$totalQtyCanceled = 0;
|
|
|
|
|
|
|
|
|
|
foreach($order->items as $item) {
|
|
|
|
|
$totalQtyOrdered += $item->qty_ordered;
|
|
|
|
|
$totalQtyInvoiced += $item->qty_invoiced;
|
|
|
|
|
$totalQtyShipped += $item->qty_shipped;
|
|
|
|
|
$totalQtyRefunded += $item->qty_refunded;
|
|
|
|
|
$totalQtyCanceled += $item->qty_canceled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($totalQtyOrdered != ($totalQtyRefunded + $totalQtyCanceled) &&
|
|
|
|
|
$totalQtyOrdered == $totalQtyInvoiced + $totalQtyRefunded + $totalQtyCanceled &&
|
|
|
|
|
$totalQtyOrdered == $totalQtyShipped + $totalQtyRefunded + $totalQtyCanceled)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $order
|
2018-10-09 12:02:22 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-10-15 10:39:09 +00:00
|
|
|
public function isInCanceledState($order)
|
2018-10-09 12:02:22 +00:00
|
|
|
{
|
2018-10-15 10:39:09 +00:00
|
|
|
$totalQtyOrdered = 0;
|
|
|
|
|
$totalQtyCanceled = 0;
|
|
|
|
|
|
2018-11-01 13:48:59 +00:00
|
|
|
foreach($order->items as $item) {
|
2018-10-15 10:39:09 +00:00
|
|
|
$totalQtyOrdered += $item->qty_ordered;
|
|
|
|
|
$totalQtyCanceled += $item->qty_canceled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($totalQtyOrdered == $totalQtyCanceled)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $order
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function isInClosedState($order)
|
|
|
|
|
{
|
|
|
|
|
$totalQtyOrdered = 0;
|
|
|
|
|
$totalQtyRefunded = 0;
|
|
|
|
|
$totalQtyCanceled = 0;
|
|
|
|
|
|
|
|
|
|
foreach($order->items as $item) {
|
|
|
|
|
$totalQtyOrdered += $item->qty_ordered;
|
|
|
|
|
$totalQtyRefunded += $item->qty_refunded;
|
|
|
|
|
$totalQtyCanceled += $item->qty_canceled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if($totalQtyOrdered == $totalQtyRefunded + $totalQtyCanceled)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $order
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function updateOrderStatus($order)
|
|
|
|
|
{
|
|
|
|
|
$status = 'processing';
|
|
|
|
|
|
|
|
|
|
if($this->isInCompletedState($order))
|
|
|
|
|
$status = 'completed';
|
|
|
|
|
|
|
|
|
|
if($this->isInCanceledState($order))
|
|
|
|
|
$status = 'canceled';
|
2018-11-01 13:48:59 +00:00
|
|
|
elseif($this->isInClosedState($order))
|
2018-10-15 10:39:09 +00:00
|
|
|
$status = 'closed';
|
|
|
|
|
|
|
|
|
|
$order->status = $status;
|
|
|
|
|
$order->save();
|
2018-10-09 12:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $order
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function collectTotals($order)
|
|
|
|
|
{
|
|
|
|
|
$subTotalInvoiced = $baseSubTotalInvoiced = 0;
|
|
|
|
|
$shippingInvoiced = $baseShippingInvoiced = 0;
|
|
|
|
|
$taxInvoiced = $baseTaxInvoiced = 0;
|
|
|
|
|
|
|
|
|
|
foreach($order->invoices as $invoice) {
|
|
|
|
|
$subTotalInvoiced += $invoice->sub_total;
|
|
|
|
|
$baseSubTotalInvoiced += $invoice->base_sub_total;
|
|
|
|
|
|
|
|
|
|
$shippingInvoiced += $invoice->shipping_amount;
|
|
|
|
|
$baseShippingInvoiced += $invoice->base_shipping_amount;
|
|
|
|
|
|
|
|
|
|
$taxInvoiced += $invoice->tax_amount;
|
|
|
|
|
$baseTaxInvoiced += $invoice->base_tax_amount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$order->sub_total_invoiced = $subTotalInvoiced;
|
|
|
|
|
$order->base_sub_total_invoiced = $baseSubTotalInvoiced;
|
|
|
|
|
|
|
|
|
|
$order->shipping_invoiced = $shippingInvoiced;
|
|
|
|
|
$order->base_shipping_invoiced = $baseShippingInvoiced;
|
|
|
|
|
|
|
|
|
|
$order->tax_amount_invoiced = $taxInvoiced;
|
|
|
|
|
$order->base_tax_amount_invoiced = $baseTaxInvoiced;
|
|
|
|
|
|
|
|
|
|
$order->grand_total_invoiced = $subTotalInvoiced + $shippingInvoiced + $taxInvoiced;
|
|
|
|
|
$order->base_grand_total_invoiced = $baseSubTotalInvoiced + $shippingInvoiced + $baseTaxInvoiced;
|
|
|
|
|
|
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
|
|
return $order;
|
|
|
|
|
}
|
2018-09-28 12:54:26 +00:00
|
|
|
}
|