134 lines
3.5 KiB
PHP
134 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Shop;
|
|
|
|
use App\Models\Shop\Order;
|
|
use App\Repositories\Core\DateStats;
|
|
use App\Traits\Model\Basic;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Orders
|
|
{
|
|
use Basic, DateStats;
|
|
|
|
public static function getByUUID($uuid)
|
|
{
|
|
return Order::byUUID($uuid)->first();
|
|
}
|
|
|
|
public static function getFullByUUID($uuid)
|
|
{
|
|
return Order::with(['customer', 'delivery', 'delivery_address', 'detail', 'invoice.address', 'sale_channel'])
|
|
->byUUID($uuid)->first();
|
|
}
|
|
|
|
public static function view($uuid)
|
|
{
|
|
$data = [];
|
|
$order = self::getFullByUUID($uuid);
|
|
$data = $order->toArray();
|
|
$data['payment_type'] = InvoicePayments::getPaymentType($order->payment_type);
|
|
$data['status'] = Orders::getStatus($order->status);
|
|
|
|
return $data;
|
|
}
|
|
|
|
public static function saveOrder($data)
|
|
{
|
|
$basket = $data['basket'];
|
|
$invoice = $data['invoice'];
|
|
unset($data['basket'], $data['invoice']);
|
|
$data += self::getSummaryOfBasket($basket);
|
|
$order = self::store($data);
|
|
$detail = $order ? OrderDetails::saveBasket($order->id, $basket['detail']) : false;
|
|
unset($data['comment'], $data['agree'], $data['delivery_address_id'], $data['sale_channel_id']);
|
|
$invoice = $detail ? Invoices::saveInvoice($order->id, $data + $invoice) : false;
|
|
|
|
return $invoice ? $order : false;
|
|
}
|
|
|
|
public static function getSummaryOfBasket($basket)
|
|
{
|
|
return [
|
|
'total' => $basket['total'],
|
|
'taxes' => $basket['taxes'],
|
|
'total_taxed' => $basket['total_taxed'],
|
|
'shipping' => $basket['shipping'],
|
|
'total_shipped' => $basket['total_shipped'],
|
|
];
|
|
}
|
|
|
|
public static function edit($id)
|
|
{
|
|
return [
|
|
'order' => self::get($id, [
|
|
'customer',
|
|
'invoice.address',
|
|
'delivery',
|
|
'delivery_address',
|
|
'detail',
|
|
'sale_channel',
|
|
])->toArray(),
|
|
'statuses' => self::statuses(),
|
|
'delivery_types' => DeliveryTypes::getOptions(),
|
|
'payment_types' => self::paymentTypes(),
|
|
'sale_channels' => SaleChannels::getOptions(),
|
|
];
|
|
}
|
|
|
|
public static function create($data)
|
|
{
|
|
OrderStats::increase();
|
|
$data['uuid'] = Str::uuid()->toString();
|
|
$data['ref'] = self::getNewRef();
|
|
|
|
return Order::create($data);
|
|
}
|
|
|
|
public static function download($id)
|
|
{
|
|
dump($id);
|
|
exit;
|
|
}
|
|
|
|
public static function getStatus($id)
|
|
{
|
|
return self::statuses()[$id] ?? false;
|
|
}
|
|
|
|
public static function getStatusByName($name)
|
|
{
|
|
$data = array_flip(self::statuses());
|
|
|
|
return $data[$name] ?? '';
|
|
}
|
|
|
|
public static function statuses()
|
|
{
|
|
return ['En attente', 'Préparation', 'Expédié', 'Livré'];
|
|
}
|
|
|
|
public static function getPaymentType($id)
|
|
{
|
|
return self::paymentTypes()[$id] ?? false;
|
|
}
|
|
|
|
public static function paymentTypes()
|
|
{
|
|
return ['', 'CARTE BANCAIRE', 'CHEQUE', 'VIREMENT BANCAIRE'];
|
|
}
|
|
|
|
public static function getNewRef()
|
|
{
|
|
$ref = date('ymd').'00000';
|
|
$lastRef = Order::where('ref', '>', $ref)->orderBy('id', 'desc')->first();
|
|
|
|
return $lastRef ? $lastRef->ref + 1 : $ref + 1;
|
|
}
|
|
|
|
public static function getModel()
|
|
{
|
|
return Order::query();
|
|
}
|
|
}
|