117 lines
2.7 KiB
PHP
117 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Shop;
|
|
|
|
use App\Models\Shop\Invoice;
|
|
use App\Traits\Model\Basic;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Invoices
|
|
{
|
|
use Basic;
|
|
|
|
public static function getInvoiceHtml($id)
|
|
{
|
|
$invoice = self::getFull($id);
|
|
$order = $invoice->order;
|
|
$customer = $order->customer;
|
|
unset($invoice->order, $order->customer);
|
|
$data = [
|
|
'invoice' => $invoice->toArray(),
|
|
'order' => $order->toArray(),
|
|
'customer' => $customer->toArray(),
|
|
];
|
|
|
|
return view('Shop.Invoices.mail', $data)->render();
|
|
}
|
|
|
|
public static function init()
|
|
{
|
|
return [
|
|
'statuses' => self::statuses(),
|
|
'payment_types' => InvoicePayments::paymentTypes(),
|
|
];
|
|
}
|
|
|
|
public static function view($uuid)
|
|
{
|
|
$data = self::getFullByUUID($uuid)->toArray();
|
|
$data['payment_type'] = InvoicePayments::getPaymentType($data['payment_type']);
|
|
$data['status'] = self::getStatus($data['status']);
|
|
|
|
return $data;
|
|
}
|
|
|
|
public static function getFullByUUID($id)
|
|
{
|
|
return self::getByUUID($id, self::full());
|
|
}
|
|
|
|
public static function getFull($id)
|
|
{
|
|
return self::get($id, self::full());
|
|
}
|
|
|
|
public static function full()
|
|
{
|
|
return [
|
|
'address',
|
|
'customer',
|
|
'order.delivery_address',
|
|
'order.detail',
|
|
'order.sale_channel',
|
|
'payments',
|
|
];
|
|
}
|
|
|
|
public static function saveInvoice($orderId, $data)
|
|
{
|
|
$data['order_id'] = $orderId;
|
|
$data['date_invoice'] = date('Y-m-d');
|
|
$data['date_due'] = Carbon::now()->addMonth()->format('Y-m-d');
|
|
|
|
return self::store($data);
|
|
}
|
|
|
|
public static function create($data)
|
|
{
|
|
InvoiceStats::increase($data['total']);
|
|
$data['uuid'] = Str::uuid()->toString();
|
|
$data['ref'] = self::getNewRef();
|
|
|
|
return Invoice::create($data);
|
|
}
|
|
|
|
public static function delete($id)
|
|
{
|
|
$invoice = self::get($id);
|
|
InvoiceStats::decrease($invoice->total);
|
|
|
|
return Invoice::destroy($id);
|
|
}
|
|
|
|
public static function getNewRef()
|
|
{
|
|
$ref = date('ymd').'00000';
|
|
$lastRef = Invoice::where('ref', '>', $ref)->orderBy('id', 'desc')->first();
|
|
|
|
return $lastRef ? $lastRef->ref + 1 : $ref + 1;
|
|
}
|
|
|
|
public static function getStatus($id)
|
|
{
|
|
return self::statuses()[$id] ?? false;
|
|
}
|
|
|
|
public static function statuses()
|
|
{
|
|
return ['En attente', 'Paiement partiel', 'Soldée', 'Paiement rejeté'];
|
|
}
|
|
|
|
public static function getModel()
|
|
{
|
|
return Invoice::query();
|
|
}
|
|
}
|