109 lines
2.7 KiB
PHP
109 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories\Shop;
|
|
|
|
use Carbon\Carbon;
|
|
use App\Models\Shop\Order;
|
|
|
|
class Orders
|
|
{
|
|
|
|
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 count()
|
|
{
|
|
return Order::count();
|
|
}
|
|
|
|
public static function countByMonth()
|
|
{
|
|
$start = Carbon::now()->startOfMonth();
|
|
return Order::where('created_at', '>', $start)->count();
|
|
}
|
|
|
|
public static function sumByMonth()
|
|
{
|
|
$start = Carbon::now()->startOfMonth();
|
|
return Order::where('created_at', '>', $start)->sum('total_shipped');
|
|
}
|
|
|
|
public static function avgByMonth()
|
|
{
|
|
return self::countByMonth() ? round(self::sumByMonth() / self::countByMonth(), 2) : 0;
|
|
}
|
|
|
|
public static function edit($id)
|
|
{
|
|
return [
|
|
'order' => self::get($id, ['customer', 'address', 'detail']),
|
|
'statuses' => self::statuses(),
|
|
'payment_types' => self::payment_types(),
|
|
'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();
|
|
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 getStatus($id)
|
|
{
|
|
return self::statuses()[$id] ?? false;
|
|
}
|
|
|
|
public static function statuses()
|
|
{
|
|
return ['En attente', 'Expédié', 'Livré'];
|
|
}
|
|
|
|
public static function getPaymentType($id)
|
|
{
|
|
return self::payment_types()[$id] ?? false;
|
|
}
|
|
|
|
public static function payment_types()
|
|
{
|
|
return ['', 'CARTE BANCAIRE', 'CHEQUE', 'VIREMENT BANCAIRE'];
|
|
}
|
|
}
|