78 lines
1.8 KiB
PHP
78 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Shop;
|
|
|
|
use Illuminate\Support\Str;
|
|
use Carbon\Carbon;
|
|
use App\Models\Shop\Invoice;
|
|
|
|
class Invoices
|
|
{
|
|
public static function saveInvoice($order_id, $data)
|
|
{
|
|
$data['order_id'] = $order_id;
|
|
return self::store($data);
|
|
}
|
|
|
|
public static function get($id, $relations = false)
|
|
{
|
|
return $relations ? Invoice::with($relations)->findOrFail($id) : Invoice::findOrFail($id);
|
|
}
|
|
|
|
public static function count()
|
|
{
|
|
return Invoice::count();
|
|
}
|
|
|
|
public static function countByMonth()
|
|
{
|
|
$start = Carbon::now()->beginOfMonth();
|
|
return Invoice::where('created_at', '>', $start);
|
|
}
|
|
|
|
public static function store($data)
|
|
{
|
|
return ($data['id'] ?? false) ? self::update($data) : self::create($data);
|
|
}
|
|
|
|
public static function create($data)
|
|
{
|
|
InvoiceStats::increase($data['total_taxed']);
|
|
$data['uuid'] = Str::uuid()->toString();
|
|
$data['ref'] = self::getNewRef();
|
|
return Invoice::create($data);
|
|
}
|
|
|
|
public static function update($data, $id = false)
|
|
{
|
|
$id = $id ? $id : $data['id'];
|
|
$item = self::get($id);
|
|
$item->update($data);
|
|
return $item;
|
|
}
|
|
|
|
public static function delete($id)
|
|
{
|
|
$invoice = self::get($id);
|
|
InvoiceStats::decrease($invoice->total_priced);
|
|
return Invoice::destroy($id);
|
|
}
|
|
|
|
public static function getNewRef()
|
|
{
|
|
$ref = date('ym') . '00000';
|
|
$last_ref = Invoice::orderBy('ref', 'desc')->where('ref', '>', $ref)->first();
|
|
return $last_ref ? $last_ref->ref + 1 : $ref + 1;
|
|
}
|
|
|
|
public static function getStatus($id)
|
|
{
|
|
return self::statuses()[$id] ?? false;
|
|
}
|
|
|
|
public static function statuses()
|
|
{
|
|
return ['En attente', 'Non soldée', 'Soldée'];
|
|
}
|
|
}
|