[WIP] Working on orders & invoices

This commit is contained in:
Ludovic CANDELLIER
2022-08-19 22:04:44 +02:00
parent c22b10dd10
commit f89acd9399
32 changed files with 440 additions and 323 deletions

View File

@@ -0,0 +1,100 @@
<?php
namespace App\Repositories\Shop;
use App\Models\Shop\Offer;
use App\Repositories\Core\User\ShopCart;
class Baskets
{
public static function addBasket($offer_id, $quantity = 1, $update = false)
{
if (ShopCart::has($offer_id) && !$quantity) {
$ret = ShopCart::remove($offer_id);
}
$data = $quantity ? self::getBasketData($offer_id, $quantity) : false;
return $data ? ShopCart::add($data, $update) : false;
}
public static function getBasketSummary()
{
$basket = ShopCart::getContent();
$offers = Offer::with('variation')->whereIn('id', ShopCart::keys())->get();
$total = 0;
$total_taxed = 0;
foreach ($basket as $item) {
$offer = $offers->where('id', $item->id)->first();
$detail[] = [
'id' => (int) $item->id,
'name' => $offer->article->name . ' (' . $offer->variation->name . ')',
'quantity' => (int) $item->quantity,
'price' => $item->price,
'tax' => '',
'price_taxed' => $item->price,
'total' => self::getTotal($item->quantity, $item->price),
'total_taxed' => self::getTotal($item->quantity, $item->price),
];
$total += self::getTotal($item->quantity, $item->price);
$total_taxed += self::getTotal($item->quantity, $item->price);
}
$data = [
'detail' => $detail,
'total' => $total,
'taxes' => $total_taxed - $total,
'total_taxed' => $total_taxed,
];
return $data ?? false;
}
public static function getTotal($quantity, $price)
{
return (int) $quantity * $price;
}
public static function getBasket($sale_channel_id = false)
{
$sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID();
$basket = ShopCart::getContent();
// dump($basket->toArray());
$offers = Offer::with([
'variation',
'article.article_nature',
'article.product.Specie',
'article.image',
'price_lists.price_list_values',
])->withPriceListsBySaleChannel($sale_channel_id)
->whereIn('id', ShopCart::keys())->get();
foreach ($basket as $item) {
$offer = $offers->where('id', $item->id)->first();
$article_nature = strtolower($offer->article->article_nature->name);
$data[$article_nature][] = [
'id' => (int) $item->id,
'name' => $item->name,
'quantity' => (int) $item->quantity,
'price' => $item->price,
'variation' => $offer->variation->name,
'image' => Articles::getPreviewSrc(Articles::getFullImageByArticle($offer->article)),
'latin' => $offer->article->product->specie->latin ?? false,
];
}
return $data ?? false;
}
public static function getBasketData($id, $quantity = 1)
{
$offer = Offers::get($id, ['article', 'variation']);
return [
'id' => $id,
'name' => self::getArticleName($offer),
'price' => Offers::getPrice($id, $quantity)->price_taxed,
'quantity' => $quantity,
'attributes' => [],
];
}
public static function getArticleName(&$offer)
{
return $offer->article->name;
// return $offer->article->name . ' (' . $offer->variation->name . ')';
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Repositories\Shop;
use Spatie\Stats\BaseStats;
class CustomerStats extends BaseStats {}
{
public function getName() : string{
return 'customers';
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Repositories\Shop;
use Illuminate\Support\Str;
use App\Models\Shop\InvoicePayment;
class InvoicePayments
{
public static function get($id, $relations = false)
{
return $relations ? InvoicePayment::with($relations)->findOrFail($id) : InvoicePayment::findOrFail($id);
}
public static function store($data)
{
return ($data['id'] ?? false) ? self::update($data) : self::create($data);
}
public static function create($data)
{
return InvoicePayment::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 InvoicePayment::destroy($id);
}
public static function getPaymentType($id)
{
return self::PaymentTypes()[$id] ?? false;
}
public static function PaymentTypes()
{
return [
'',
'CB',
'CHEQUE',
'VIREMENT',
];
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Repositories\Shop;
use Spatie\Stats\BaseStats;
class InvoiceStats extends BaseStats {}
{
public function getName() : string{
return 'invoices';
}
}

View File

@@ -2,50 +2,55 @@
namespace App\Repositories\Shop;
use Illuminate\Support\Str;
use App\Models\Shop\Invoice;
class Invoices
{
public static function getOptions()
public static function saveInvoice($order_id, $data)
{
return Invoice::orderBy('value', 'asc')->get()->pluck('value', 'id')->toArray();
$data['order_id'] = $order_id;
dump($data);
exit;
return self::store($data);
}
public static function getOptionsByPackage($package_id)
public static function get($id, $relations = false)
{
return Invoice::byPackage($package_id)->orderBy('value', 'asc')->get()->pluck('value', 'id')->toArray();
}
public static function getAll()
{
return Invoice::orderBy('value', 'asc')->get();
}
public static function get($id)
{
return Invoice::find($id);
return $relations ? Invoice::with($relations)->findOrFail($id) : Invoice::findOrFail($id);
}
public static function store($data)
{
$id = isset($data['id']) ? $data['id'] : false;
$item = $id ? self::update($data, $id) : self::create($data);
return $item->id;
return ($data['id'] ?? false) ? self::update($data) : self::create($data);
}
public static function create($data)
{
$data['uuid'] = Str::uuid()->toString();
$data['ref'] = self::getNewRef();
return Invoice::create($data);
}
public static function update($data, $id = false)
{
$id = isset($data['id']) ? $data['id'] : false;
return self::get($id)->update($data);
$id = $id ? $id : $data['id'];
$item = self::get($id);
$item->update($data);
return $item;
}
public static function destroy($id)
public static function delete($id)
{
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 + 1 : $ref + 1;
}
}

View File

@@ -7,15 +7,6 @@ use App\Repositories\Core\User\ShopCart;
class Offers
{
public static function addBasket($offer_id, $quantity = 1, $update = false)
{
if (ShopCart::has($offer_id) && !$quantity) {
$ret = ShopCart::remove($offer_id);
}
$data = $quantity ? Offers::getBasketData($offer_id, $quantity) : false;
return $data ? ShopCart::add($data, $update) : false;
}
public static function getFull($id, $sale_channel_id = false)
{
$sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID();
@@ -43,47 +34,6 @@ class Offers
return $offer->price_lists->first()->price_list_values->first();
}
public static function getBasket($sale_channel_id = false)
{
$sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID();
$basket = ShopCart::getContent();
// dump($basket->toArray());
$offers = Offer::with([
'variation',
'article.article_nature',
'article.product.Specie',
'article.image',
'price_lists.price_list_values',
])->withPriceListsBySaleChannel($sale_channel_id)
->whereIn('id', ShopCart::keys())->get();
foreach ($basket as $item) {
$offer = $offers->where('id', $item->id)->first();
$article_nature = strtolower($offer->article->article_nature->name);
$data[$article_nature][] = [
'id' => (int) $item->id,
'name' => $item->name,
'quantity' => (int) $item->quantity,
'price' => $item->price,
'variation' => $offer->variation->name,
'image' => Articles::getPreviewSrc(Articles::getFullImageByArticle($offer->article)),
'latin' => $offer->article->product->specie->latin ?? false,
];
}
return $data ?? false;
}
public static function getBasketData($id, $quantity = 1)
{
$offer = Offer::with(['article'])->findOrFail($id);
return [
'id' => $id,
'name' => $offer->article->name,
'price' => self::getPrice($id, $quantity)->price_taxed,
'quantity' => $quantity,
'attributes' => [],
];
}
public static function getOffersByArticles($article_ids, $sale_channel_id = false)
{
return self::getOffersBySaleChannelRaw($sale_channel_id)->byArticles($article_ids)->get();
@@ -155,16 +105,14 @@ class Offers
return Offer::get();
}
public static function get($id)
public static function get($id, $relations = false)
{
return Offer::findOrFail($id);
return $relations ? Offer::with($relations)->findOrFail($id) : Offer::findOrFail($id);
}
public static function store($data)
{
$id = isset($data['id']) ? $data['id'] : false;
$item = $id ? self::update($data, $id) : self::create($data);
return $item->id;
return ($data['id'] ?? false) ? self::update($data) : self::create($data);
}
public static function create($data)

View File

@@ -6,12 +6,15 @@ use App\Models\Shop\OrderDetail;
class OrderDetails
{
public static function saveBasket($order_id, $data)
{
foreach ($data as $item) {
$item['order_id'] = $order_id;
$detail = self::store($item);
$detail = self::store([
'order_id' => $order_id,
'offer_id' => $item['id'],
'quantity' => $item['quantity'],
'price_taxed' => $item['price'],
]);
}
return true;
}

View File

@@ -11,7 +11,10 @@ class Orders
{
$basket = $data['basket'];
unset($data['basket']);
dump($data);
exit;
$order = self::store($data);
$invoice = Invoices::saveInvoice($order->id, $data);
return $order ? OrderDetails::saveBasket($order->id, $basket) : false;
}
@@ -42,4 +45,18 @@ class Orders
{
return Order::destroy($id);
}
public static function getStatus($id)
{
return self::statuses()[$id] ?? false;
}
public static function statuses()
{
return [
'En attente',
'Expédié',
'Livré',
];
}
}