sarga/packages/Webkul/Sales/src/Repositories/OrderItemInventoryRepositor...

80 lines
1.9 KiB
PHP
Raw Normal View History

2018-10-05 06:18:58 +00:00
<?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';
}
2018-10-05 11:59:43 +00:00
/**
* @param array $data
* @return mixed
*/
public function create(array $data)
{
$orderItem = $data['orderItem'];
$orderedQuantity = $orderItem->qty_ordered;
$product = $orderItem->type == 'configurable' ? $orderItem->child->product : $orderItem->product;
2019-04-03 14:39:13 +00:00
if (!$product) {
return ;
}
$inventories = $product->inventory_sources()->orderBy('priority', 'asc')->get();
2018-10-05 11:59:43 +00:00
2019-04-03 14:39:13 +00:00
foreach ($inventories as $inventorySource) {
if (! $orderedQuantity)
break;
2018-10-05 11:59:43 +00:00
2019-04-03 14:39:13 +00:00
$sourceQuantity = $inventorySource->pivot->qty;
2018-10-05 11:59:43 +00:00
2019-04-03 14:39:13 +00:00
if (! $inventorySource->status || !$sourceQuantity)
continue;
2018-10-05 11:59:43 +00:00
2019-04-03 14:39:13 +00:00
if ($sourceQuantity >= $orderedQuantity) {
$orderItemQuantity = $orderedQuantity;
2018-10-05 11:59:43 +00:00
2019-04-03 14:39:13 +00:00
$sourceQuantity -= $orderItemQuantity;
2018-10-05 11:59:43 +00:00
2019-04-03 14:39:13 +00:00
$orderedQuantity = 0;
} else {
$orderItemQuantity = $sourceQuantity;
2018-10-05 11:59:43 +00:00
2019-04-03 14:39:13 +00:00
$sourceQuantity = 0;
2018-10-05 11:59:43 +00:00
2019-04-03 14:39:13 +00:00
$orderedQuantity -= $orderItemQuantity;
}
2018-10-05 11:59:43 +00:00
2019-04-03 14:39:13 +00:00
$this->model->create([
'qty' => $orderItemQuantity,
'order_item_id' => $orderItem->id,
'inventory_source_id' => $inventorySource->id,
]);
2018-10-05 11:59:43 +00:00
2019-04-03 14:39:13 +00:00
$inventorySource->pivot->update([
'qty' => $sourceQuantity
]);
2018-10-05 11:59:43 +00:00
}
2019-04-03 14:39:13 +00:00
2018-10-05 11:59:43 +00:00
}
2018-10-05 06:18:58 +00:00
}