Manage address and deliveries
This commit is contained in:
53
app/Repositories/Shop/CustomerAddresses.php
Normal file
53
app/Repositories/Shop/CustomerAddresses.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Shop;
|
||||
|
||||
use App\Models\Shop\CustomerAddress;
|
||||
|
||||
class CustomerAddresses
|
||||
{
|
||||
public static function getOptions()
|
||||
{
|
||||
return CustomerAddress::orderBy('name', 'asc')->get()->pluck('name', 'id')->toArray();
|
||||
}
|
||||
|
||||
public static function getAll()
|
||||
{
|
||||
return CustomerAddress::orderBy('name', 'asc')->get();
|
||||
}
|
||||
|
||||
public static function get($id)
|
||||
{
|
||||
return CustomerAddress::find($id);
|
||||
}
|
||||
|
||||
public static function store($data)
|
||||
{
|
||||
$id = $data['id'] ?? false;
|
||||
$item = $id ? self::update($data, $id) : self::create($data);
|
||||
return $item->id;
|
||||
}
|
||||
|
||||
public static function create($data)
|
||||
{
|
||||
return CustomerAddress::create($data);
|
||||
}
|
||||
|
||||
public static function update($data, $id = false)
|
||||
{
|
||||
$id = $id ? $id : $data['id'];
|
||||
$delivery = self::get($id);
|
||||
$delivery->update($data);
|
||||
return $delivery;
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
return CustomerAddress::destroy($id);
|
||||
}
|
||||
|
||||
public static function toggle_active($id, $active)
|
||||
{
|
||||
return self::update(['active' => $active], $id);
|
||||
}
|
||||
}
|
||||
@@ -26,13 +26,51 @@ class Customers
|
||||
return Customer::find($id);
|
||||
}
|
||||
|
||||
public static function storeFull($data)
|
||||
{
|
||||
dump($data);
|
||||
exit;
|
||||
|
||||
$deliveries = $data['deliveries'];
|
||||
$addresses = $data['addresses'];
|
||||
unset($data['deliveries']);
|
||||
unset($data['addresses']);
|
||||
dump($data);
|
||||
exit;
|
||||
$customer = self::store($data);
|
||||
self::storeDeliveries($customer, $deliveries);
|
||||
self::storeAddresses($customer, $addresses);
|
||||
return $customer->id;
|
||||
}
|
||||
|
||||
public static function store($data)
|
||||
{
|
||||
$id = isset($data['id']) ? $data['id'] : false;
|
||||
$id = $data['id'] ?? false;
|
||||
$item = $id ? self::update($data, $id) : self::create($data);
|
||||
return $item->id;
|
||||
}
|
||||
|
||||
public static function storeDeliveries($customer, $deliveries)
|
||||
{
|
||||
if ($deliveries) {
|
||||
$deliveries = collect($deliveries)->transform(
|
||||
function ($item, $key) {
|
||||
return (int) $item;
|
||||
}
|
||||
)->toArray();
|
||||
return $customer->deliveries()->sync($deliveries);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function storeAddresses($customer, $addresses)
|
||||
{
|
||||
foreach ($addresses as $address) {
|
||||
CustomerAddresses::store($address);
|
||||
}
|
||||
}
|
||||
|
||||
public static function create($data)
|
||||
{
|
||||
return Customer::create($data);
|
||||
@@ -40,8 +78,10 @@ class Customers
|
||||
|
||||
public static function update($data, $id = false)
|
||||
{
|
||||
$id = isset($data['id']) ? $data['id'] : false;
|
||||
return Customer::find($id)->update($data);
|
||||
$id = $id ? $id : $data['id'];
|
||||
$customer = self::get($id);
|
||||
$customer->update($data);
|
||||
return $customer;
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
|
||||
Reference in New Issue
Block a user