File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Shop/ShipmentGroups/ShipmentGroupService.php
<?php
namespace App\Komma\Shop\ShipmentGroups;
use App\Komma\Kms\Core\ModelService;
use App\Komma\Shop\Shipments\Shipment;
use App\Komma\Shop\Shipments\ShipmentService;
use App\Komma\Shop\Shipments\ShipmentStatus;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Collection;
class ShipmentGroupService extends ModelService
{
public function __construct()
{
parent::__construct();
$this->setModelClassName(ShipmentGroup::class);
}
/**
* Makes a shipmentGroup using
*
* @param Shipment $shipment
* @return ShipmentGroup
*/
public function createForShipment(Shipment $shipment): ShipmentGroup
{
return $this->createForShipments(collect([$shipment]));
}
/**
* Give this method an array, collection or any other iterable that contains Shipments, and it will
* return you a ShipmentGroup for those shipments. Shipments that are already a part of a shipmentGroup wil be ignored.
* If all the shipments are assigned to another ShipmentGroup, it will return null instead of a ShipmentGroup.
*
* @param Collection $shipments
* @return ShipmentGroup|null
*/
public function createForShipments(Collection $shipments): ? ShipmentGroup
{
if (! ShipmentService::iterableContainsShipments($shipments)) {
throw new \InvalidArgumentException('The iterable does not exclusively contain shipments.');
}
//Filter out the shipments that already belong to a shipmentGroup
$shipments = $shipments->filter(function (Shipment $shipment) {
return ! $shipment->shipmentGroup_id;
});
if ($shipments->count() == 0) {
return null;
}
$shipmentGroup = new ShipmentGroup();
$shipmentGroup->status = ShipmentGroupStatus::PENDING;
$shipmentGroup->save();
$shipmentGroup->shipments()->saveMany($shipments);
return $shipmentGroup;
}
/**
* Updates all shipments in a group to a certain status
*
* @param ShipmentGroup $shipmentGroup
* @param string $status
* @return ShipmentGroup
*/
public function updateShipmentStatusesTo(ShipmentGroup $shipmentGroup, $status)
{
if (! ShipmentStatus::isValidItem($status)) {
throw new \InvalidArgumentException('The status of "'.$status.'" is not a valid status');
}
$shipmentGroup->shipments()->update(['status' => $status]);
return $shipmentGroup;
}
/**
* Return a query builder to retrieve shipment groups by a certain status.
*
* @param string $status
* @return mixed
*/
public function shipmentGroupsByStatus(string $status)
{
if (! ShipmentGroupStatus::isValidItem($status)) {
throw new \InvalidArgumentException('The shipment group status "'.$status.'" is invalid.');
}
return $this->modelClassName::where('status', '=', $status);
}
/**
* Returns the latest x shipment groups. Where x is a random number.
*
* @param $int
* @return Builder
*/
public function getLatest($int): Builder
{
/** @var Builder $ordersQueryBuilder */
$ordersQueryBuilder = $this->modelClassName::query();
return $ordersQueryBuilder->latest('created_at')->limit($int);
}
/**
* @param Collection $shipments
*/
public function removeShipmentsFromGroup(Collection $shipments)
{
$shipmentIds = $shipments->pluck('id');
if (! ShipmentService::iterableContainsShipments($shipments)) {
throw new \InvalidArgumentException('The iterable does not exclusively contain shipments.');
}
Shipment::whereIn('id', $shipmentIds)->update(['shipment_group_id' => null]);
}
/**
* Remove the shipment group from a shipment
* @param Shipment $shipment
*/
public function removeShipmentFromGroup(Shipment $shipment)
{
$shipment->shipmentGroup()->dissociate();
$shipment->save();
}
/**
* @param ShipmentGroup $shipmentGroup
* @param Collection $shipments
*/
public function addShipmentsToGroup(ShipmentGroup $shipmentGroup, Collection $shipments)
{
if (! ShipmentService::iterableContainsShipments($shipments)) {
throw new \InvalidArgumentException('The iterable does not exclusively contain shipments.');
}
$shipments->each(function (Shipment $shipment) use ($shipmentGroup) {
if ($shipment->shipment_group_id) {
return;
} //continue to the next iteration
$shipment->shipmentGroup()->associate($shipmentGroup);
$shipment->save();
});
}
/**
* Ship the shipmentgroup. Marking it as shipped.
*
* @param ShipmentGroup $shipmentGroup
* @return ShipmentGroup
*/
public function ship(ShipmentGroup $shipmentGroup)
{
$this->updateShipmentStatusesTo($shipmentGroup, ShipmentStatus::IN_TRANSIT);
$shipmentGroup->status = ShipmentGroupStatus::SHIPPED;
$shipmentGroup->save();
return $shipmentGroup;
}
/**
* Retrieves an array where the key names are field names, and value are the search values.
* And uses that array to search for orders. The found orders are returned as a QueryBuilder
* that holds the query to return all the found orders.
*
* @param array $input $input
* @return Builder
*/
public function search(array $input): Builder
{
/** @var Builder $ordersQueryBuilder */
$ordersQueryBuilder = $this->modelClassName::query();
foreach ($input as $fieldName => $searchValue) {
if ($searchValue == '') {
continue;
}
switch ($fieldName) {
case 'id':
$ordersQueryBuilder->where('id', '=', $searchValue);
break;
}
}
return $ordersQueryBuilder;
}
}