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;
|
2019-04-03 13:21:05 +00:00
|
|
|
use Webkul\Sales\Contracts\Order;
|
2018-10-05 06:18:58 +00:00
|
|
|
use Webkul\Sales\Repositories\OrderItemRepository;
|
2019-08-22 15:24:30 +00:00
|
|
|
use Webkul\Core\Models\CoreConfig;
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new repository instance.
|
|
|
|
|
*
|
2018-12-26 10:00:23 +00:00
|
|
|
* @param Webkul\Sales\Repositories\OrderItemRepository $orderItem
|
2018-09-28 12:54:26 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
OrderItemRepository $orderItem,
|
|
|
|
|
App $app
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
$this->orderItem = $orderItem;
|
|
|
|
|
|
|
|
|
|
parent::__construct($app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Specify Model class name
|
|
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function model()
|
|
|
|
|
{
|
2019-04-03 13:21:05 +00:00
|
|
|
return Order::class;
|
2018-09-28 12:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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);
|
|
|
|
|
|
2019-01-15 11:54:41 +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
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (isset($data['channel']) && $data['channel']) {
|
2018-10-09 12:02:22 +00:00
|
|
|
$data['channel_id'] = $data['channel']->id;
|
|
|
|
|
$data['channel_type'] = get_class($data['channel']);
|
|
|
|
|
$data['channel_name'] = $data['channel']->name;
|
|
|
|
|
} else {
|
|
|
|
|
unset($data['channel']);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-25 14:14:07 +00:00
|
|
|
$data['status'] = 'pending';
|
2018-10-05 11:59:43 +00:00
|
|
|
|
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']);
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($data['items'] as $item) {
|
2018-10-05 06:18:58 +00:00
|
|
|
$orderItem = $this->orderItem->create(array_merge($item, ['order_id' => $order->id]));
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
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
|
|
|
|
2018-12-26 13:04:49 +00:00
|
|
|
$this->orderItem->manageInventory($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);
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $order->canCancel())
|
2018-10-15 10:39:09 +00:00
|
|
|
return false;
|
|
|
|
|
|
2019-01-18 05:18:25 +00:00
|
|
|
Event::fire('sales.order.cancel.before', $order);
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($order->items as $item) {
|
|
|
|
|
if ($item->qty_to_cancel) {
|
2018-12-26 13:04:49 +00:00
|
|
|
$this->orderItem->returnQtyToProductInventory($item);
|
|
|
|
|
|
2018-10-15 10:39:09 +00:00
|
|
|
$item->qty_canceled += $item->qty_to_cancel;
|
|
|
|
|
|
|
|
|
|
$item->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->updateOrderStatus($order);
|
|
|
|
|
|
2019-01-17 11:59:07 +00:00
|
|
|
Event::fire('sales.order.cancel.after', $order);
|
2019-01-17 11:47:53 +00:00
|
|
|
|
2018-10-15 10:39:09 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 06:18:58 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
2019-08-22 15:24:30 +00:00
|
|
|
* @return int|string
|
2018-10-05 06:18:58 +00:00
|
|
|
*/
|
|
|
|
|
public function generateIncrementId()
|
|
|
|
|
{
|
2019-08-22 15:24:30 +00:00
|
|
|
$config = new CoreConfig();
|
2019-08-23 06:34:12 +00:00
|
|
|
$invoiceNumberPrefix = $config->where('code','=',"sales.invoiceSettings.invoice_number.invoice_number_prefix")->first()
|
|
|
|
|
? $config->where('code','=',"sales.invoiceSettings.invoice_number.invoice_number_prefix")->first()->value : false;
|
|
|
|
|
$invoiceNumberLength = $config->where('code','=',"sales.invoiceSettings.invoice_number.invoice_number_length")->first()
|
|
|
|
|
? $config->where('code','=',"sales.invoiceSettings.invoice_number.invoice_number_length")->first()->value : false;
|
|
|
|
|
$invoiceNumberSuffix = $config->where('code','=',"sales.invoiceSettings.invoice_number.invoice_number_suffix")->first()
|
2019-08-23 07:10:40 +00:00
|
|
|
? $config->where('code','=',"sales.invoiceSettings.invoice_number.invoice_number_suffix")->first()->value: false;
|
2018-10-05 06:18:58 +00:00
|
|
|
|
2019-08-22 15:24:30 +00:00
|
|
|
$lastOrder = $this->model->orderBy('id', 'desc')->limit(1)->first();
|
2018-10-05 06:18:58 +00:00
|
|
|
$lastId = $lastOrder ? $lastOrder->id : 0;
|
|
|
|
|
|
2019-08-22 15:24:30 +00:00
|
|
|
if($invoiceNumberLength && ( $invoiceNumberPrefix || $invoiceNumberSuffix) ){
|
|
|
|
|
|
|
|
|
|
$invoiceNumber = $invoiceNumberPrefix . sprintf("%0{$invoiceNumberLength}d", $lastId + 1) . $invoiceNumberSuffix;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
$invoiceNumber = $lastId + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $invoiceNumber;
|
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;
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($order->items as $item) {
|
2018-10-15 10:39:09 +00:00
|
|
|
$totalQtyOrdered += $item->qty_ordered;
|
|
|
|
|
$totalQtyInvoiced += $item->qty_invoiced;
|
|
|
|
|
$totalQtyShipped += $item->qty_shipped;
|
|
|
|
|
$totalQtyRefunded += $item->qty_refunded;
|
|
|
|
|
$totalQtyCanceled += $item->qty_canceled;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-09 10:03:05 +00:00
|
|
|
if ($totalQtyOrdered != ($totalQtyRefunded + $totalQtyCanceled) &&
|
2018-10-15 10:39:09 +00:00
|
|
|
$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;
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($order->items as $item) {
|
2018-10-15 10:39:09 +00:00
|
|
|
$totalQtyOrdered += $item->qty_ordered;
|
|
|
|
|
$totalQtyCanceled += $item->qty_canceled;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($totalQtyOrdered == $totalQtyCanceled)
|
2018-10-15 10:39:09 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $order
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function isInClosedState($order)
|
|
|
|
|
{
|
|
|
|
|
$totalQtyOrdered = 0;
|
|
|
|
|
$totalQtyRefunded = 0;
|
|
|
|
|
$totalQtyCanceled = 0;
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($order->items as $item) {
|
2018-10-15 10:39:09 +00:00
|
|
|
$totalQtyOrdered += $item->qty_ordered;
|
|
|
|
|
$totalQtyRefunded += $item->qty_refunded;
|
|
|
|
|
$totalQtyCanceled += $item->qty_canceled;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($totalQtyOrdered == $totalQtyRefunded + $totalQtyCanceled)
|
2018-10-15 10:39:09 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $order
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function updateOrderStatus($order)
|
|
|
|
|
{
|
|
|
|
|
$status = 'processing';
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($this->isInCompletedState($order))
|
2018-10-15 10:39:09 +00:00
|
|
|
$status = 'completed';
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($this->isInCanceledState($order))
|
2018-10-15 10:39:09 +00:00
|
|
|
$status = 'canceled';
|
2019-01-15 11:54:41 +00:00
|
|
|
else if ($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)
|
|
|
|
|
{
|
2019-01-09 07:57:43 +00:00
|
|
|
$order->sub_total_invoiced = $order->base_sub_total_invoiced = 0;
|
|
|
|
|
$order->shipping_invoiced = $order->base_shipping_invoiced = 0;
|
|
|
|
|
$order->tax_amount_invoiced = $order->base_tax_amount_invoiced = 0;
|
2018-10-09 12:02:22 +00:00
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($order->invoices as $invoice) {
|
2019-01-09 07:57:43 +00:00
|
|
|
$order->sub_total_invoiced += $invoice->sub_total;
|
|
|
|
|
$order->base_sub_total_invoiced += $invoice->base_sub_total;
|
2018-10-09 12:02:22 +00:00
|
|
|
|
2019-01-09 07:57:43 +00:00
|
|
|
$order->shipping_invoiced += $invoice->shipping_amount;
|
|
|
|
|
$order->base_shipping_invoiced += $invoice->base_shipping_amount;
|
2018-10-09 12:02:22 +00:00
|
|
|
|
2019-01-09 07:57:43 +00:00
|
|
|
$order->tax_amount_invoiced += $invoice->tax_amount;
|
|
|
|
|
$order->base_tax_amount_invoiced += $invoice->base_tax_amount;
|
2019-05-23 11:30:11 +00:00
|
|
|
|
|
|
|
|
$order->discount_invoiced += $invoice->discount_amount;
|
|
|
|
|
$order->base_discount_invoiced += $invoice->base_discount_amount;
|
2018-10-09 12:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-23 11:30:11 +00:00
|
|
|
$order->grand_total_invoiced = $order->sub_total_invoiced + $order->shipping_invoiced + $order->tax_amount_invoiced - $order->discount_invoiced;
|
|
|
|
|
$order->base_grand_total_invoiced = $order->base_sub_total_invoiced + $order->base_shipping_invoiced + $order->base_tax_amount_invoiced - $order->base_discount_invoiced;
|
2018-10-09 12:02:22 +00:00
|
|
|
|
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
|
|
return $order;
|
|
|
|
|
}
|
2018-09-28 12:54:26 +00:00
|
|
|
}
|