Files
opensem/app/Repositories/Shop/CustomerAddresses.php
2025-02-15 12:12:42 +01:00

105 lines
3.2 KiB
PHP

<?php
namespace App\Repositories\Shop;
use App\Models\Shop\CustomerAddress;
use App\Traits\Model\Basic;
class CustomerAddresses
{
use Basic;
public static function storeByCustomer($customer, $data)
{
$deliveries = $data['deliveries'] ?? false;
if ($deliveries && $deliveries['zipcode'] && $deliveries['city']) {
$deliveries['customer_id'] = $customer->id;
$deliveries['type'] = 1;
self::store($deliveries);
}
$invoices = $data['invoices'] ?? false;
if ($invoices && $invoices['zipcode'] && $invoices['city']) {
$invoices['customer_id'] = $customer->id;
$invoices['type'] = 2;
self::store($invoices);
}
}
public static function createByCustomer($customerId, $data)
{
self::addDeliveryAddress($customerId, $data);
self::addInvoiceAddress($customerId, $data);
}
public static function addDeliveryAddress($customerId, $data)
{
$delivery = $data['use_for_delivery'] ?? false;
$data = array_merge($data, [
'address' => $delivery ? $data['delivery_address'] : $data['address'],
'address2' => $delivery ? $data['delivery_address2'] : $data['address2'],
'zipcode' => $delivery ? $data['delivery_zipcode'] : $data['zipcode'],
'city' => $delivery ? $data['delivery_city'] : $data['city'],
]);
return self::addAddress($customerId, $data, 2);
}
public static function addInvoiceAddress($customerId, $data)
{
return self::addAddress($customerId, $data, 1);
}
public static function addAddress($customerId, $data, $type)
{
$name = $data['company'] ? $data['company'] : $data['first_name'] . ' ' . $data['last_name'];
$data = [
'customer_id' => $customerId,
'type' => $type,
'name' => $name,
'address' => $data['address'],
'address2' => $data['address2'],
'zipcode' => $data['zipcode'],
'city' => $data['city'],
];
return self::store($data);
}
public static function getInvoiceAddress($customerId)
{
$addresses = CustomerAddress::byCustomer($customerId)->byInvoicing()->get();
return count($addresses) ? $addresses->first() : self::getByCustomer($customerId);
}
public static function getDeliveryAddress($customerId)
{
$addresses = CustomerAddress::byCustomer($customerId)->byDelivery()->get();
return count($addresses) ? $addresses->first() : self::getByCustomer($customerId);
}
public static function getByCustomer($customerId = false)
{
$customer = Customers::get($customerId);
return $customer ? $customer->only(['address', 'adress2', 'zipcode', 'city']) : false;
}
public static function getIconByType($type)
{
return ((int) $type === 1) ? '<i class="fa fa-fw fa-truck"></i>' : '<i class="fa fa-fw fa-file-invoice"></i>';
}
public static function toggleActive($id, $active)
{
return self::update(['active' => $active], $id);
}
public static function getModel()
{
return CustomerAddress::query();
}
}