sarga/packages/Webkul/Sales/src/Repositories/ShipmentRepository.php

176 lines
5.7 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\Sales\Repositories;
use Illuminate\Container\Container as App;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\DB;
use Webkul\Core\Eloquent\Repository;
2019-04-03 13:21:05 +00:00
use Webkul\Sales\Contracts\Shipment;
2019-07-01 11:33:36 +00:00
use Webkul\Sales\Repositories\OrderRepository;
use Webkul\Sales\Repositories\OrderItemRepository;
use Webkul\Sales\Repositories\ShipmentItemRepository;
/**
* Shipment Reposotory
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class ShipmentRepository extends Repository
{
/**
* OrderRepository object
*
* @var Object
*/
2019-07-01 11:33:36 +00:00
protected $orderRepository;
/**
* OrderItemRepository object
*
* @var Object
*/
2019-07-01 11:33:36 +00:00
protected $orderItemRepository;
/**
* ShipmentItemRepository object
*
* @var Object
*/
2019-07-01 11:33:36 +00:00
protected $shipmentItemRepository;
/**
* Create a new repository instance.
*
2019-07-01 11:33:36 +00:00
* @param Webkul\Sales\Repositories\OrderRepository $orderRepository
* @param Webkul\Sales\Repositories\OrderItemRepository $orderItemRepository
* @param Webkul\Sales\Repositories\ShipmentItemRepository $orderItemRepository
* @return void
*/
public function __construct(
2019-07-01 11:33:36 +00:00
OrderRepository $orderRepository,
OrderItemRepository $orderItemRepository,
ShipmentItemRepository $shipmentItemRepository,
App $app
)
{
2019-07-01 11:33:36 +00:00
$this->orderRepository = $orderRepository;
2019-07-01 11:33:36 +00:00
$this->orderItemRepository = $orderItemRepository;
2019-07-01 11:33:36 +00:00
$this->shipmentItemRepository = $shipmentItemRepository;
parent::__construct($app);
}
2019-12-24 14:01:13 +00:00
/**
* Specify Model class name
*
* @return Mixed
*/
function model()
{
2019-04-03 13:21:05 +00:00
return Shipment::class;
}
/**
* @param array $data
* @return mixed
*/
public function create(array $data)
{
DB::beginTransaction();
2019-12-24 14:01:13 +00:00
try {
2019-12-24 14:01:13 +00:00
Event::dispatch('sales.shipment.save.before', $data);
2019-07-01 11:33:36 +00:00
$order = $this->orderRepository->find($data['order_id']);
$shipment = $this->model->create([
'order_id' => $order->id,
'total_qty' => 0,
'carrier_title' => $data['shipment']['carrier_title'],
'track_number' => $data['shipment']['track_number'],
'customer_id' => $order->customer_id,
'customer_type' => $order->customer_type,
'order_address_id' => $order->shipping_address->id,
'inventory_source_id' => $data['shipment']['source'],
]);
$totalQty = 0;
foreach ($data['shipment']['items'] as $itemId => $inventorySource) {
$qty = $inventorySource[$data['shipment']['source']];
2019-07-01 11:33:36 +00:00
$orderItem = $this->orderItemRepository->find($itemId);
2019-01-18 11:19:03 +00:00
if ($qty > $orderItem->qty_to_ship)
$qty = $orderItem->qty_to_ship;
$totalQty += $qty;
2019-07-01 11:33:36 +00:00
$shipmentItem = $this->shipmentItemRepository->create([
'shipment_id' => $shipment->id,
'order_item_id' => $orderItem->id,
'name' => $orderItem->name,
'sku' => $orderItem->getTypeInstance()->getOrderedItem($orderItem)->sku,
'qty' => $qty,
'weight' => $orderItem->weight * $qty,
'price' => $orderItem->price,
'base_price' => $orderItem->base_price,
'total' => $orderItem->price * $qty,
'base_total' => $orderItem->base_price * $qty,
'product_id' => $orderItem->product_id,
'product_type' => $orderItem->product_type,
'additional' => $orderItem->additional,
]);
if ($orderItem->getTypeInstance()->isComposite()) {
foreach ($orderItem->children as $child) {
2019-09-05 12:31:06 +00:00
if (! $child->qty_ordered) {
$finalQty = $qty;
} else {
$finalQty = ($child->qty_ordered / $orderItem->qty_ordered) * $qty;
}
$this->shipmentItemRepository->updateProductInventory([
'shipment' => $shipment,
'product' => $child->product,
'qty' => $finalQty,
'vendor_id' => isset($data['vendor_id']) ? $data['vendor_id'] : 0
]);
2019-12-24 14:01:13 +00:00
$this->orderItemRepository->update(['qty_shipped' => $child->qty_shipped + $finalQty], $child->id);
}
} else {
$this->shipmentItemRepository->updateProductInventory([
'shipment' => $shipment,
'product' => $orderItem->product,
'qty' => $qty,
'vendor_id' => isset($data['vendor_id']) ? $data['vendor_id'] : 0
]);
}
2019-07-01 11:33:36 +00:00
$this->orderItemRepository->update(['qty_shipped' => $orderItem->qty_shipped + $qty], $orderItem->id);
}
$shipment->update([
'total_qty' => $totalQty
]);
2019-07-01 11:33:36 +00:00
$this->orderRepository->updateOrderStatus($order);
2018-10-26 13:43:01 +00:00
2019-12-24 14:01:13 +00:00
Event::dispatch('sales.shipment.save.after', $shipment);
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
2019-12-24 14:01:13 +00:00
DB::commit();
return $shipment;
}
}