Files
opensem/app/Repositories/Shop/Customers.php
Ludovic CANDELLIER 719e4481d7 [WIP] Order process
2022-07-03 22:38:08 +02:00

176 lines
4.5 KiB
PHP

<?php
namespace App\Repositories\Shop;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Laravolt\Avatar\Avatar;
use App\Repositories\Core\File;
use App\Models\Shop\Customer;
class Customers
{
public static function getAvatar($id = false)
{
$customer = $id ? self::get($id) : self::getAuth();
$file = self::makeAvatarFilename($customer);
if (!File::checkFile($file)) {
self::createAvatar($customer);
}
return self::getPublic(self::getAvatarFilename($customer));
}
public static function createAvatar($customer)
{
$filename = self::makeAvatarFilename($customer);
$name = $customer->first_name . ' ' . $customer->last_name;
$avatar = new Avatar();
$ret = $avatar->create($name)
->setBackground($bgColor)
->setBorder(1, '#29292e')
->setFontFamily('Roboto Condensed')
->setDimension(40)
->setFontSize(16)
->save($filename);
return $ret;
}
public static function makeAvatarFilename($customer)
{
$path = storage_path(self::getStorage());
if (File::checkDirOrCreate($path)) {
$filename = $path . 'user-' . $customer->uuid . '.png';
}
return $filename ?? false;
}
public static function getAvatarFilename($customer)
{
return 'user-' . $customer->uuid . '.png';
}
public static function getName()
{
$user = self::getAuth();
return $user ? $user->first_name : '';
}
public static function getAuth()
{
return Auth::guard('customer')->user();
}
public static function getId()
{
return Auth::guard('customer')->id();
}
public static function isConnected()
{
return Auth::guard('customer')->check();
}
public static function getOptions()
{
return Customer::orderBy('value', 'asc')->get()->pluck('value', 'id')->toArray();
}
public static function getOptionsByPackage($package_id)
{
return Customer::byPackage($package_id)->orderBy('value', 'asc')->get()->pluck('value', 'id')->toArray();
}
public static function getAll()
{
return Customer::orderBy('value', 'asc')->get();
}
public static function get($id)
{
return Customer::find($id);
}
public static function edit($id)
{
$customer = Customer::with(['addresses'])->find($id);
$data = $customer->toArray();
$data['deliveries'] = $customer->deliveries->pluck('id')->toArray();
return $data;
}
public static function storeFull($data)
{
$deliveries = $data['deliveries'];
$addresses = $data['addresses'];
unset($data['deliveries']);
unset($data['addresses']);
$customer = self::store($data);
self::storeDeliveries($customer, $deliveries);
self::storeAddresses($customer, $addresses);
return $customer->id;
}
public static function store($data)
{
$id = $data['id'] ?? false;
$item = $id ? self::update($data, $id) : self::create($data);
return $item;
}
public static function storeDeliveries($customer, $deliveries)
{
if (!$deliveries) {
return false;
}
$deliveries = collect($deliveries)->transform(
function ($item, $key) {
return (int) $item;
}
)->toArray();
return $customer->deliveries()->sync($deliveries);
}
public static function storeAddresses($customer, $addresses)
{
foreach ($addresses as $address) {
$address['customer_id'] = $customer->id;
CustomerAddresses::store($address);
}
}
public static function create($data)
{
$data['uuid'] = Str::uuid()->toString();
return Customer::create($data);
}
public static function update($data, $id = false)
{
$id = $id ? $id : $data['id'];
$customer = self::get($id);
$customer->update($data);
return $customer;
}
public static function destroy($id)
{
return Customer::destroy($id);
}
public static function getStorage($filename = false)
{
$path = '/app/public/Customers/';
return $filename ? $path . $filename : $path;
}
public static function getPublic($filename = false)
{
$path = '/storage/Customers/';
return $filename ? $path . $filename : $path;
}
}