2018-09-28 12:54:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Sales\Repositories;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Container\Container as App;
|
|
|
|
|
use Webkul\Core\Eloquent\Repository;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* OrderItem Reposotory
|
|
|
|
|
*
|
|
|
|
|
* @author Jitendra Singh <jitendra@webkul.com>
|
|
|
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class OrderItemRepository extends Repository
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Specify Model class name
|
|
|
|
|
*
|
|
|
|
|
* @return Mixed
|
|
|
|
|
*/
|
|
|
|
|
function model()
|
|
|
|
|
{
|
|
|
|
|
return 'Webkul\Sales\Contracts\OrderItem';
|
|
|
|
|
}
|
2018-10-05 06:18:58 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function create(array $data)
|
|
|
|
|
{
|
2019-01-15 11:54:41 +00:00
|
|
|
if (isset($data['product']) && $data['product']) {
|
2018-10-05 06:18:58 +00:00
|
|
|
$data['product_id'] = $data['product']->id;
|
|
|
|
|
$data['product_type'] = get_class($data['product']);
|
2018-10-05 11:59:43 +00:00
|
|
|
|
|
|
|
|
unset($data['product']);
|
2018-10-05 06:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->model->create($data);
|
|
|
|
|
}
|
2018-10-09 12:02:22 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $orderItem
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function collectTotals($orderItem)
|
|
|
|
|
{
|
|
|
|
|
$qtyShipped = $qtyInvoiced = 0;
|
|
|
|
|
$totalInvoiced = $baseTotalInvoiced = 0;
|
|
|
|
|
$taxInvoiced = $baseTaxInvoiced = 0;
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($orderItem->invoice_items as $invoiceItem) {
|
2018-10-09 12:02:22 +00:00
|
|
|
$qtyInvoiced += $invoiceItem->qty;
|
|
|
|
|
|
|
|
|
|
$totalInvoiced += $invoiceItem->total;
|
|
|
|
|
$baseTotalInvoiced += $invoiceItem->base_total;
|
|
|
|
|
|
|
|
|
|
$taxInvoiced += $invoiceItem->tax_amount;
|
|
|
|
|
$baseTaxInvoiced += $invoiceItem->base_tax_amount;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
foreach ($orderItem->shipment_items as $shipmentItem) {
|
2018-10-09 12:02:22 +00:00
|
|
|
$qtyShipped += $shipmentItem->qty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$orderItem->qty_shipped = $qtyShipped;
|
|
|
|
|
$orderItem->qty_invoiced = $qtyInvoiced;
|
|
|
|
|
|
|
|
|
|
$orderItem->total_invoiced = $totalInvoiced;
|
|
|
|
|
$orderItem->base_total_invoiced = $baseTotalInvoiced;
|
|
|
|
|
|
|
|
|
|
$orderItem->tax_amount_invoiced = $taxInvoiced;
|
|
|
|
|
$orderItem->base_tax_amount_invoiced = $baseTaxInvoiced;
|
|
|
|
|
|
|
|
|
|
$orderItem->save();
|
|
|
|
|
|
|
|
|
|
return $orderItem;
|
|
|
|
|
}
|
2018-12-26 10:00:23 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $orderItem
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-12-26 13:04:49 +00:00
|
|
|
public function manageInventory($orderItem)
|
2018-12-26 10:00:23 +00:00
|
|
|
{
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $orderedQuantity = $orderItem->qty_ordered)
|
2018-12-26 10:00:23 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
$product = $orderItem->type == 'configurable' ? $orderItem->child->product : $orderItem->product;
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $product) {
|
2018-12-26 10:00:23 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-26 13:04:49 +00:00
|
|
|
$orderedInventory = $product->ordered_inventories()
|
2018-12-26 10:00:23 +00:00
|
|
|
->where('channel_id', $orderItem->order->channel->id)
|
|
|
|
|
->first();
|
|
|
|
|
|
2019-01-15 11:54:41 +00:00
|
|
|
if ($orderedInventory) {
|
2018-12-26 13:04:49 +00:00
|
|
|
$orderedInventory->update([
|
|
|
|
|
'qty' => $orderedInventory->qty + $orderItem->qty_ordered
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
$product->ordered_inventories()->create([
|
|
|
|
|
'qty' => $orderItem->qty_ordered,
|
|
|
|
|
'product_id' => $product->id,
|
|
|
|
|
'channel_id' => $orderItem->order->channel->id,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns qty to product inventory after order cancelation
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $orderItem
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function returnQtyToProductInventory($orderItem)
|
|
|
|
|
{
|
2019-01-15 11:54:41 +00:00
|
|
|
if (! $product = $orderItem->product)
|
2018-12-26 13:04:49 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
$orderedInventory = $product->ordered_inventories()
|
|
|
|
|
->where('channel_id', $orderItem->order->channel->id)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if ($orderedInventory) {
|
|
|
|
|
if (($qty = $orderedInventory->qty - $orderItem->qty_to_cancel) < 0) {
|
|
|
|
|
$qty = 0;
|
|
|
|
|
}
|
2018-12-26 10:00:23 +00:00
|
|
|
|
2018-12-26 13:04:49 +00:00
|
|
|
$orderedInventory->update([
|
|
|
|
|
'qty' => $qty
|
2018-12-26 10:00:23 +00:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-28 12:54:26 +00:00
|
|
|
}
|