184 lines
4.7 KiB
PHP
184 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Shop;
|
|
|
|
use Carbon\Carbon;
|
|
use App\Mail\Acheminement;
|
|
use App\Mail\Preparation;
|
|
use App\Models\Shop\Order;
|
|
use App\Repositories\Core\DateStats;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class Orders
|
|
{
|
|
use DateStats;
|
|
|
|
public static function getByUUID($uuid)
|
|
{
|
|
return Order::byUUID($uuid)->first();
|
|
}
|
|
|
|
public static function saveOrder($data)
|
|
{
|
|
$data += $data['basket'];
|
|
$basket = $data['basket'];
|
|
unset($data['basket']);
|
|
$order = self::store($data);
|
|
$detail = OrderDetails::saveBasket($order->id, $basket['detail']);
|
|
unset($data['comment']);
|
|
unset($data['agree']);
|
|
unset($data['customer_id']);
|
|
unset($data['delivery_id']);
|
|
unset($data['detail']);
|
|
unset($data['payment_type']);
|
|
unset($data['sale_channel_id']);
|
|
return ($order && $detail) ? Invoices::saveInvoice($order->id, $data) : false;
|
|
}
|
|
|
|
public static function testSend($id)
|
|
{
|
|
$order = self::get($id, ['customer', 'address']);
|
|
dump($order->toArray());
|
|
exit;
|
|
self::sendPreparation($order);
|
|
}
|
|
|
|
public static function sendOrderConfirmed($order_id)
|
|
{
|
|
$order = self::get($order_id, ['customer']);
|
|
$mail = new Acheminement($order);
|
|
return Mail::to($order->email)->send($mail);
|
|
}
|
|
|
|
public static function sendShipping($order_id)
|
|
{
|
|
$order = self::get($order_id, ['customer']);
|
|
$mail = new Acheminement($order);
|
|
return Mail::to($order->email)->send($mail);
|
|
}
|
|
|
|
public static function sendPreparation($order_id)
|
|
{
|
|
$order = self::get($order_id, ['customer']);
|
|
$mail = new Preparation($order);
|
|
return Mail::to($order->email)->send($mail);
|
|
}
|
|
|
|
public static function count()
|
|
{
|
|
return Order::count();
|
|
}
|
|
|
|
public static function countInPreparationLess24H()
|
|
{
|
|
$start = Carbon::now()->subHours(24);
|
|
$end = Carbon::now();
|
|
return Order::preparation()->byPeriod($start, $end, 'updated_at')->count();
|
|
}
|
|
|
|
public static function countInPreparationLess48H()
|
|
{
|
|
$start = Carbon::now()->subHours(48);
|
|
$end = Carbon::now();
|
|
return Order::preparation()->byPeriod($start, $end, 'updated_at')->count();
|
|
}
|
|
|
|
public static function countInPreparationMore48H()
|
|
{
|
|
$start = Carbon::now()->subHours(48);
|
|
return Order::preparation()->where('updated_at', '>', $start)->count();
|
|
}
|
|
|
|
public static function getTotalByPeriod($start, $end)
|
|
{
|
|
return self::sumByPeriod($start, $end, 'total_shipped');
|
|
}
|
|
|
|
public static function edit($id)
|
|
{
|
|
return [
|
|
'order' => self::get($id, ['customer', 'address', 'detail']),
|
|
'statuses' => self::statuses(),
|
|
'payment_types' => self::paymentTypes(),
|
|
'sale_channels' => SaleChannels::getOptions(),
|
|
];
|
|
}
|
|
|
|
public static function get($id, $relations = false)
|
|
{
|
|
return $relations ? Order::with($relations)->findOrFail($id) : Order::findOrFail($id);
|
|
}
|
|
|
|
public static function store($data)
|
|
{
|
|
return ($data['id'] ?? false) ? self::update($data) : self::create($data);
|
|
}
|
|
|
|
public static function create($data)
|
|
{
|
|
OrderStats::increase();
|
|
$data['uuid'] = Str::uuid()->toString();
|
|
$data['ref'] = self::getNewRef();
|
|
return Order::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)
|
|
{
|
|
return Order::destroy($id);
|
|
}
|
|
|
|
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('ym') . '00000';
|
|
$last_ref = Order::orderBy('id', 'desc')->first();
|
|
return $last_ref ? $last_ref->ref + 1 : $ref + 1;
|
|
}
|
|
|
|
public static function getModel()
|
|
{
|
|
return Order::query();
|
|
}
|
|
}
|