fix shipping

This commit is contained in:
Ludovic CANDELLIER
2023-07-16 17:54:44 +02:00
parent c619f540f8
commit ddb4520621
36 changed files with 640 additions and 897 deletions

View File

@@ -18,9 +18,9 @@ class DeliveryPackagesDataTable extends DataTable
protected function getColumns()
{
return [
Column::make('weight')->title('Poids'),
Column::make('weight_flyer')->title('Flyer'),
Column::make('weight_packaging')->title('Packaging'),
Column::make('weight')->title('Poids')->addClass('text-right'),
Column::make('weight_flyer')->title('Flyer')->addClass('text-right'),
Column::make('weight_packaging')->title('Packaging')->addClass('text-right'),
$this->makeColumnButtons(),
];
}

View File

@@ -11,16 +11,12 @@ class DeliveryPackageController extends Controller
{
public function index(DeliveryPackagesDataTable $dataTable)
{
$data = [];
return $dataTable->render('Admin.Shop.DeliveryPackages.list', $data);
return $dataTable->render('Admin.Shop.DeliveryPackages.list');
}
public function create()
{
$data['sale_channels'] = SaleChannels::getOptions();
return view('Admin.Shop.DeliveryPackages.create', $data);
return view('Admin.Shop.DeliveryPackages.create');
}
public function store(Request $request)
@@ -30,17 +26,9 @@ class DeliveryPackageController extends Controller
return redirect()->route('Admin.Shop.DeliveryPackages.index');
}
public function show($id)
{
$data['delivery'] = DeliveryPackages::get($id);
return view('Admin.Shop.DeliveryPackages.view', $data);
}
public function edit($id)
{
$data['delivery'] = DeliveryPackages::get($id)->toArray();
$data['sale_channels'] = SaleChannels::getOptions();
$data['package'] = DeliveryPackages::get($id)->toArray();
return view('Admin.Shop.DeliveryPackages.edit', $data);
}
@@ -49,11 +37,4 @@ class DeliveryPackageController extends Controller
{
return DeliveryPackages::destroy($id);
}
public function toggleActive(Request $request)
{
$data = DeliveryPackages::toggle_active($request->input('id'), ($request->input('active') == 'true') ? 1 : 0);
return response()->json(['error' => 0]);
}
}

View File

@@ -4,21 +4,21 @@ namespace App\Http\Controllers\Admin\Shop;
use App\Datatables\Shop\DeliveryTypeCalculationsDataTable;
use App\Repositories\Shop\DeliveryTypeCalculations;
use App\Repositories\Shop\SaleChannels;
use App\Repositories\Shop\DeliveryTypes;
use Illuminate\Http\Request;
class DeliveryTypeCalculationController extends Controller
{
public function index(DeliveryTypeCalculationsDataTable $dataTable)
{
$data = [];
return $dataTable->render('Admin.Shop.DeliveryTypeCalculations.list', $data);
return $dataTable->render('Admin.Shop.DeliveryTypeCalculations.list');
}
public function create()
{
$data = [];
$data = [
'delivery_types' => DeliveryTypes::getOptions(),
];
return view('Admin.Shop.DeliveryTypeCalculations.create', $data);
}
@@ -30,18 +30,13 @@ class DeliveryTypeCalculationController extends Controller
return redirect()->route('Admin.Shop.DeliveryTypeCalculations.index');
}
public function show($id)
{
$data['delivery'] = DeliveryTypeCalculations::get($id);
return view('Admin.Shop.DeliveryTypeCalculations.view', $data);
}
public function edit($id)
{
$data['delivery'] = DeliveryTypeCalculations::get($id)->toArray();
$data['sale_channels'] = SaleChannels::getOptions();
$data = [
'calcul' => DeliveryTypeCalculations::get($id)->toArray(),
'delivery_types' => DeliveryTypes::getOptions(),
];
return view('Admin.Shop.DeliveryTypeCalculations.edit', $data);
}
@@ -49,11 +44,4 @@ class DeliveryTypeCalculationController extends Controller
{
return DeliveryTypeCalculations::destroy($id);
}
public function toggleActive(Request $request)
{
$data = DeliveryTypeCalculations::toggle_active($request->input('id'), ($request->input('active') == 'true') ? 1 : 0);
return response()->json(['error' => 0]);
}
}

View File

@@ -30,13 +30,6 @@ class DeliveryTypeController extends Controller
return redirect()->route('Admin.Shop.DeliveryTypes.index');
}
public function show($id)
{
$data['delivery'] = DeliveryTypes::get($id);
return view('Admin.Shop.DeliveryTypes.view', $data);
}
public function edit($id)
{
$data['delivery'] = DeliveryTypes::get($id)->toArray();
@@ -49,11 +42,4 @@ class DeliveryTypeController extends Controller
{
return DeliveryTypes::destroy($id);
}
public function toggleActive(Request $request)
{
$data = DeliveryTypes::toggle_active($request->input('id'), ($request->input('active') == 'true') ? 1 : 0);
return response()->json(['error' => 0]);
}
}

View File

@@ -15,24 +15,24 @@ class BasketController extends Controller
{
public function getPrice(Request $request)
{
$offer_id = $request->input('offer_id');
$offerId = $request->input('offer_id');
$quantity = $request->input('quantity') ?? 1;
$price = Offers::getPrice($offer_id, $quantity)->price_taxed;
$price = Offers::getPrice($offerId, $quantity)->price_taxed;
return number_format($quantity * $price, 2);
}
public function addBasket(Request $request)
{
$offer_id = $request->input('offer_id');
$offerId = $request->input('offer_id');
$quantity = $request->input('quantity') ?? 1;
$update = $request->input('update') ?? false;
return Baskets::addBasket($offer_id, $quantity, $update);
return Baskets::addBasket($offerId, $quantity, $update);
}
public function modalBasket($offer_id, $quantity)
public function modalBasket($offerId, $quantity)
{
$data['offer'] = Offers::getFull($offer_id)->toArray();
$data['basket'] = Baskets::addBasket($offer_id, $quantity);
$data['offer'] = Offers::getFull($offerId)->toArray();
$data['basket'] = Baskets::addBasket($offerId, $quantity);
return view('Shop.Baskets.partials.modalBasket', $data);
}
@@ -65,7 +65,7 @@ class BasketController extends Controller
$data = $request->all();
unset($data['_token']);
$data['user_id'] = Users::getId();
Orders::newOrder($data);
Orders::saveOrder($data);
return response()->json(['code' => '200']);
}

View File

@@ -19,72 +19,72 @@ class CategoryController extends Controller
return $dataTable->render('Shop.Categories.list');
}
public function show(Request $request, $category_id, $article_nature_id = false)
public function show(Request $request, $categoryId, $articleNatureId = false)
{
if ($article_nature_id) {
$product_type = ArticleNatures::getProductType($article_nature_id);
dump($product_type);
if ($articleNatureId) {
$productType = ArticleNatures::getProductType($articleNatureId);
dump($productType);
exit;
} else {
$article_nature = $request->input('article_nature');
if (!$article_nature) {
$articleNature = $request->input('article_nature');
if (!$articleNature) {
$articleNatures = Articles::getArticleNaturesWithOffers([
'category_id' => $category_id,
'category_id' => $categoryId,
]);
if (count($articleNatures) === 1) {
$article_nature = $articleNatures[0];
$articleNature = $articleNatures[0];
}
$productTypes = Articles::getProductTypesWithOffers([
'category_id' => $category_id,
'category_id' => $categoryId,
]);
if (count($productTypes) === 1) {
$product_type = $productTypes[0];
$productType = $productTypes[0];
}
}
switch ($article_nature) {
switch ($articleNature) {
case 'semences':
$product_type = 'botanic';
$article_nature_id = 1;
$productType = 'botanic';
$articleNatureId = 1;
break;
case 'plants':
$product_type = 'botanic';
$article_nature_id = 2;
$productType = 'botanic';
$articleNatureId = 2;
break;
case 'legumes':
$product_type = 'botanic';
$article_nature_id = 3;
$productType = 'botanic';
$articleNatureId = 3;
break;
default:
if (!($product_type ?? false)) {
$product_type = 'botanic';
$article_nature_id = 1;
$article_nature = 'semences';
if (!($productType ?? false)) {
$productType = 'botanic';
$articleNatureId = 1;
$articleNature = 'semences';
}
break;
}
// $product_type = ArticleNatures::getProductType($article_nature_id);
// dump($product_type);
// dump($article_nature_id);
// $productType = ArticleNatures::getProductType($articleNatureId);
// dump($productType);
// dump($articleNatureId);
// exit;
}
// exit;
$data = [
'category' => Categories::getFull($category_id),
'breadcrumb' => Categories::getAncestorsByCategory($category_id),
'category' => Categories::getFull($categoryId),
'breadcrumb' => Categories::getAncestorsByCategory($categoryId),
'display_by_rows' => $request->input('display_by_rows') ?? false,
'product_type' => $product_type,
'article_nature' => $article_nature,
'product_type' => $productType,
'article_nature' => $articleNature,
'article_natures' => $articleNatures ?? [],
'product_types' => $productTypes ?? [],
'tags_selected' => $request->input('tags') ?? [],
'articles' => Articles::getArticlesToSell([
'category_id' => $category_id,
'category_id' => $categoryId,
'tags' => $request->input('tags') ?? [],
'product_type' => $product_type ?? false,
'article_nature_id' => $article_nature_id ?? false,
'product_type' => $productType ?? false,
'article_nature_id' => $articleNatureId ?? false,
]),
'tags' => TagGroups::getWithTagsAndCountOffers($category_id),
'tags' => TagGroups::getWithTagsAndCountOffers($categoryId),
];
// dump($data);
// exit;

View File

@@ -13,13 +13,25 @@ class Orders
->activeIfRoute('orders')
->order(1);
$menu->addTo('orders', 'Commandes', [ 'route' => 'Admin.Shop.Orders.index', 'permission' => 'backend_access' ])
->activeIfRoute(['Admin.Shop.Orders.*'])->order(1);
$menu->addTo('orders', 'Factures', [ 'route' => 'Admin.Shop.Invoices.index', 'permission' => 'backend_access' ])
->activeIfRoute(['Admin.Shop.Invoices.*'])->order(2);
$menu->addTo('orders', 'Avoirs', [ 'route' => 'Admin.Shop.Invoices.index', 'permission' => 'backend_access' ])
->activeIfRoute(['Admin.Shop.Invoices.*'])->order(3);
$menu->addTo('orders', 'Bons de livraison', [ 'route' => 'Admin.Shop.Invoices.index', 'permission' => 'backend_access' ])
->activeIfRoute(['Admin.Shop.Invoices.*'])->order(4);
$menu->addTo('orders', 'Commandes', [
'route' => 'Admin.Shop.Orders.index',
'permission' => 'backend_access',
])->activeIfRoute(['Admin.Shop.Orders.*'])->order(1);
$menu->addTo('orders', 'Factures', [
'route' => 'Admin.Shop.Invoices.index',
'permission' => 'backend_access',
])->activeIfRoute(['Admin.Shop.Invoices.*'])->order(2);
/*
$menu->addTo('orders', 'Avoirs', [
'route' => 'Admin.Shop.Invoices.index',
'permission' => 'backend_access',
])->activeIfRoute(['Admin.Shop.Invoices.*'])->order(3);
$menu->addTo('orders', 'Bons de livraison', [
'route' => 'Admin.Shop.Invoices.index',
'permission' => 'backend_access',
])->activeIfRoute(['Admin.Shop.Invoices.*'])->order(4);
*/
}
}

View File

@@ -11,15 +11,21 @@ class Settings
$menu->add('Paramètres', [ 'permission' => 'backend_access', 'icon' => 'cogs' ])
->id('settings')
->activeIfRoute('settings')
->order(1);
->order(7);
$menu->addTo('settings', __('shop.delivery_types.title'), [ 'route' => 'Admin.Shop.DeliveryTypes.index', 'permission' => 'backend_access' ])
->activeIfRoute(['Admin.Shop.DeliveryTypes.*'])->order(1);
$menu->addTo('settings', __('shop.delivery_types.title'), [
'route' => 'Admin.Shop.DeliveryTypes.index',
'permission' => 'backend_access',
])->activeIfRoute(['Admin.Shop.DeliveryTypes.*'])->order(1);
$menu->addTo('settings', __('shop.delivery_type_calculations.title'), [ 'route' => 'Admin.Shop.DeliveryTypeCalculations.index', 'permission' => 'backend_access' ])
->activeIfRoute(['Admin.Shop.DeliveryTypeCalculations.*'])->order(2);
$menu->addTo('settings', __('shop.delivery_type_calculations.title'), [
'route' => 'Admin.Shop.DeliveryTypeCalculations.index',
'permission' => 'backend_access',
])->activeIfRoute(['Admin.Shop.DeliveryTypeCalculations.*'])->order(2);
$menu->addTo('settings', __('shop.delivery_packages.title'), [ 'route' => 'Admin.Shop.DeliveryPackages.index', 'permission' => 'backend_access' ])
->activeIfRoute(['Admin.Shop.DeliveryPackages.*'])->order(3);
$menu->addTo('settings', __('shop.delivery_packages.title'), [
'route' => 'Admin.Shop.DeliveryPackages.index',
'permission' => 'backend_access',
])->activeIfRoute(['Admin.Shop.DeliveryPackages.*'])->order(3);
}
}

View File

@@ -58,9 +58,9 @@ class Category extends parentCategory
return $this->tags()->withCount('Articles');
}
public function scopeByCategory($query, $category_id)
public function scopeByCategory($query, $categoryId)
{
return $query->where('category_id', $category_id);
return $query->where('category_id', $categoryId);
}
public function scopeVisible($query)
@@ -78,28 +78,28 @@ class Category extends parentCategory
return $query->where('id', '<>', 1);
}
public function scopeInRange($query, $_lft, $_rgt)
public function scopeInRange($query, $left, $right)
{
return $query->where('_lft', '>=', $_lft)->where('_rgt', '<=', $_rgt);
return $query->where('_lft', '>=', $left)->where('_rgt', '<=', $right);
}
public function scopeHasAvailableOffers($query, $sale_channel_id = false)
public function scopeHasAvailableOffers($query, $saleChannelId = false)
{
$sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID();
$saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID();
return $query->whereHas('articles', function ($query) use ($sale_channel_id) {
$query->WithAvailableOffers($sale_channel_id);
return $query->whereHas('articles', function ($query) use ($saleChannelId) {
$query->WithAvailableOffers($saleChannelId);
});
}
public function scopeHasAvailableOffersByCategoryParent($query, $sale_channel_id = false)
public function scopeHasAvailableOffersByCategoryParent($query, $saleChannelId = false)
{
$sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID();
$saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID();
return $query->whereHas('articles', function ($query) use ($sale_channel_id) {
$query->WithAvailableOffers($sale_channel_id);
})->orWhereHas('descendants.articles', function ($query) use ($sale_channel_id) {
$query->WithAvailableOffers($sale_channel_id);
return $query->whereHas('articles', function ($query) use ($saleChannelId) {
$query->WithAvailableOffers($saleChannelId);
})->orWhereHas('descendants.articles', function ($query) use ($saleChannelId) {
$query->WithAvailableOffers($saleChannelId);
});
}
}

View File

@@ -58,14 +58,14 @@ class Order extends Model
return $query->where('uuid', $uuid);
}
public function scopeByCustomer($query, $customer_id)
public function scopeByCustomer($query, $customerId)
{
return $query->where('customer_id', $customer_id);
return $query->where('customer_id', $customerId);
}
public function scopeByDelivery($query, $delivery_id)
public function scopeByDelivery($query, $deliveryId)
{
return $query->where('delivery_id', $delivery_id);
return $query->where('delivery_id', $deliveryId);
}
public function scopePreparation($query)
@@ -88,9 +88,9 @@ class Order extends Model
return $query->where('status', $status);
}
public function scopeByPaymentType($query, $payment_type)
public function scopeByPaymentType($query, $paymentType)
{
return $query->where('payment_type', $payment_type);
return $query->where('payment_type', $paymentType);
}
public function scopeByPeriod($query, $start, $end, $field = 'created_at')

View File

@@ -7,13 +7,11 @@ use App\Models\Botanic\Variety;
use App\Models\Shop\Article;
use App\Models\Shop\ArticleNature;
use App\Models\Shop\Merchandise;
use App\Traits\Model\Basic;
class ArticleNatures
{
public static function getOptions()
{
return ArticleNature::get()->pluck('name', 'id')->toArray();
}
use Basic;
public static function getProductType($id)
{
@@ -37,12 +35,16 @@ class ArticleNatures
switch ($model) {
case Specie::class:
case Variety::class:
return 1;
$type = 1;
break;
case Merchandise::class:
return 2;
$type = 2;
break;
default:
$type = false;
}
return false;
return $type;
}
public static function getProductTypes()
@@ -77,14 +79,9 @@ class ArticleNatures
return ArticleNature::byProductType($type)->get();
}
public static function getByCategory($category_id)
public static function getByCategory($categoryId)
{
return Article::byCategory($category_id)->select('article_nature_id')->distinct()->get();
}
public static function getAll()
{
return ArticleNature::orderBy('name', 'asc')->get();
return Article::byCategory($categoryId)->select('article_nature_id')->distinct()->get();
}
public static function getNamesByIds($ids)
@@ -94,34 +91,8 @@ class ArticleNatures
return array_map('strtolower', $names);
}
public static function get($id)
public static function getModel()
{
return ArticleNature::find($id);
}
public static function store($data)
{
$item = ($data['id'] ?? false) ? self::update($data) : self::create($data);
return $item->id;
}
public static function create($data)
{
return ArticleNature::create($data);
}
public static function update($data, $id = false)
{
$id = $id ? $id : $data['id'];
$item = self::get($id);
$ret = $item->update($data);
return $item;
}
public static function destroy($id)
{
return ArticleNature::destroy($id);
return ArticleNature::query();
}
}

View File

@@ -3,30 +3,18 @@
namespace App\Repositories\Shop;
use App\Models\Shop\CustomerAddress;
use App\Traits\Model\Basic;
class CustomerAddresses
{
public static function getOptions()
{
return CustomerAddress::orderBy('name', 'asc')->get()->pluck('name', 'id')->toArray();
}
use Basic;
public static function getAll()
{
return CustomerAddress::orderBy('name', 'asc')->get();
}
public static function get($id)
{
return CustomerAddress::find($id);
}
public static function add($user_id, $data)
public static function add($userId, $data)
{
$name = $data['company'] ? $data['company'] : $data['first_name'].' '.$data['last_name'];
if ($data['use_for_delivery'] ?? false) {
return self::store([
'customer_id' => $user_id,
'customer_id' => $userId,
'name' => $name,
'address' => $data['delivery_address'],
'address2' => $data['delivery_address2'],
@@ -35,7 +23,7 @@ class CustomerAddresses
]);
} else {
return self::store([
'customer_id' => $user_id,
'customer_id' => $userId,
'name' => $name,
'address' => $data['address'],
'address2' => $data['address2'],
@@ -45,35 +33,13 @@ class CustomerAddresses
}
}
public static function store($data)
{
$id = $data['id'] ?? false;
$item = $id ? self::update($data, $id) : self::create($data);
return $item->id;
}
public static function create($data)
{
return CustomerAddress::create($data);
}
public static function update($data, $id = false)
{
$id = $id ? $id : $data['id'];
$delivery = self::get($id);
$delivery->update($data);
return $delivery;
}
public static function destroy($id)
{
return CustomerAddress::destroy($id);
}
public static function toggle_active($id, $active)
public static function toggleActive($id, $active)
{
return self::update(['active' => $active], $id);
}
public static function getModel()
{
return CustomerAddress::query();
}
}

View File

@@ -7,14 +7,14 @@ class Dashboards
public static function getStats($start, $end)
{
return [
'orders_count' => Orders::countByPeriod($start, $end),
'orders_sum' => Orders::sumByPeriod($start, $end, 'total_shipped'),
'orders_avg' => Orders::avgByPeriod($start, $end, 'total_shipped'),
'orders_count' => OrderMetrics::countByPeriod($start, $end),
'orders_sum' => OrderMetrics::sumByPeriod($start, $end, 'total_shipped'),
'orders_avg' => OrderMetrics::avgByPeriod($start, $end, 'total_shipped'),
'offers_count' => Offers::count(),
'clients_count' => Customers::count(),
'preparationLess24H' => Orders::countInPreparationLess24H(),
'preparationLess48H' => Orders::countInPreparationLess48H(),
'preparationMore48H' => Orders::countInPreparationMore48H(),
'preparationLess24H' => OrderMetrics::countInPreparationLess24H(),
'preparationLess48H' => OrderMetrics::countInPreparationLess48H(),
'preparationMore48H' => OrderMetrics::countInPreparationMore48H(),
];
}
}

View File

@@ -3,42 +3,14 @@
namespace App\Repositories\Shop;
use App\Models\Shop\DeliveryPackage;
use App\Traits\Model\Basic;
class DeliveryPackages
{
public static function getOptions()
use Basic;
public static function getModel()
{
return DeliveryPackage::pluck('name', 'id');
}
public static function get($id)
{
return DeliveryPackage::find($id);
}
public static function store($data)
{
$item = ($data['id'] ?? false) ? self::update($data) : self::create($data);
return $item->id;
}
public static function create($data)
{
return DeliveryPackage::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 destroy($id)
{
return DeliveryPackage::destroy($id);
return DeliveryPackage::query();
}
}

View File

@@ -3,52 +3,14 @@
namespace App\Repositories\Shop;
use App\Models\Shop\DeliveryTypeCalculation;
use App\Traits\Model\Basic;
class DeliveryTypeCalculations
{
public static function getByDeliveryType($typeId)
use Basic;
public static function getModel()
{
return DeliveryTypeCalculation::byDeliveryTpe($typeId)->get();
}
public static function getPrice($typeId, $weight)
{
return DeliveryTypeCalculation::byDeliveryType($typeId)->byWeight($weight)->get();
}
public static function getOptions()
{
return DeliveryTypeCalculation::pluck('name', 'id');
}
public static function get($id)
{
return DeliveryTypeCalculation::find($id);
}
public static function store($data)
{
$item = ($data['id'] ?? false) ? self::update($data) : self::create($data);
return $item->id;
}
public static function create($data)
{
return DeliveryTypeCalculation::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 destroy($id)
{
return DeliveryTypeCalculation::destroy($id);
return DeliveryTypeCalculation::query();
}
}

View File

@@ -3,42 +3,14 @@
namespace App\Repositories\Shop;
use App\Models\Shop\DeliveryType;
use App\Traits\Model\Basic;
class DeliveryTypes
{
public static function getOptions()
use Basic;
public static function getModel()
{
return DeliveryType::pluck('name', 'id');
}
public static function get($id)
{
return DeliveryType::find($id);
}
public static function store($data)
{
$item = ($data['id'] ?? false) ? self::update($data) : self::create($data);
return $item->id;
}
public static function create($data)
{
return DeliveryType::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 destroy($id)
{
return DeliveryType::destroy($id);
return DeliveryType::query();
}
}

View File

@@ -28,9 +28,9 @@ class Invoices
return Invoice::byUUID($uuid)->first();
}
public static function saveInvoice($order_id, $data)
public static function saveInvoice($orderId, $data)
{
$data['order_id'] = $order_id;
$data['order_id'] = $orderId;
return self::store($data);
}
@@ -86,9 +86,9 @@ class Invoices
public static function getNewRef()
{
$ref = date('ym').'00000';
$last_ref = Invoice::orderBy('id', 'desc')->first();
$lastRef = Invoice::orderBy('id', 'desc')->first();
return $last_ref ? $last_ref->ref + 1 : $ref + 1;
return $lastRef ? $lastRef->ref + 1 : $ref + 1;
}
public static function getStatus($id)

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Repositories\Shop;
use App\Mail\Acheminement;
use App\Mail\ConfirmationCommande;
use App\Mail\Preparation;
use App\Models\Shop\Order;
use App\Repositories\Core\DateStats;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
class OrderMails
{
public static function testSend($id)
{
self::sendPreparation($id);
}
public static function sendOrderConfirmed($orderId)
{
$order = Orders::get($orderId, ['customer', 'address']);
$mail = new ConfirmationCommande($order);
return Mail::to($order->customer->email)->send($mail);
}
public static function sendPreparation($orderId)
{
$order = Orders::get($orderId, ['customer', 'address']);
$mail = new Preparation($order);
return Mail::to($order->customer->email)->send($mail);
}
public static function sendShipping($orderId)
{
$order = Orders::get($orderId, ['customer', 'address']);
$mail = new Acheminement($order);
return Mail::to($order->customer->email)->send($mail);
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace App\Repositories\Shop;
use App\Models\Shop\Order;
use App\Repositories\Core\DateStats;
use Carbon\Carbon;
class OrderMetrics
{
use DateStats;
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 getModel()
{
return Order::query();
}
}

View File

@@ -2,18 +2,14 @@
namespace App\Repositories\Shop;
use App\Mail\Acheminement;
use App\Mail\ConfirmationCommande;
use App\Mail\Preparation;
use App\Models\Shop\Order;
use App\Repositories\Core\DateStats;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
use App\Traits\Model\Basic;
use Illuminate\Support\Str;
class Orders
{
use DateStats;
use Basic, DateStats;
public static function getByUUID($uuid)
{
@@ -38,68 +34,6 @@ class Orders
return ($order && $detail) ? Invoices::saveInvoice($order->id, $data) : false;
}
public static function testSend($id)
{
self::sendPreparation($id);
}
public static function sendOrderConfirmed($order_id)
{
$order = self::get($order_id, ['customer', 'address']);
$mail = new ConfirmationCommande($order);
return Mail::to($order->customer->email)->send($mail);
}
public static function sendPreparation($order_id)
{
$order = self::get($order_id, ['customer', 'address']);
$mail = new Preparation($order);
return Mail::to($order->customer->email)->send($mail);
}
public static function sendShipping($order_id)
{
$order = self::get($order_id, ['customer', 'address']);
$mail = new Acheminement($order);
return Mail::to($order->customer->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 [
@@ -111,16 +45,6 @@ class Orders
];
}
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();
@@ -130,20 +54,6 @@ class Orders
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);
@@ -180,9 +90,9 @@ class Orders
public static function getNewRef()
{
$ref = date('ym').'00000';
$last_ref = Order::orderBy('id', 'desc')->first();
$lastRef = Order::orderBy('id', 'desc')->first();
return $last_ref ? $last_ref->ref + 1 : $ref + 1;
return $lastRef ? $lastRef->ref + 1 : $ref + 1;
}
public static function getModel()

View File

@@ -3,6 +3,8 @@
namespace App\Traits\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
trait Basic
{
@@ -24,14 +26,38 @@ trait Basic
return $data;
}
public static function getName($id)
public static function getName($id, $len = false)
{
return self::getField($id, 'name');
$name = self::getField($id, 'name');
return $len ? Str::limit($name, $len) : $name;
}
public static function getByUUID($uuid)
public static function getUUID($id)
{
return self::getByField('uuid', $uuid)->first();
return self::getField($id, 'uuid');
}
public static function getByUUID($uuid, $relations = [])
{
return self::getByField('uuid', $uuid, $relations)->first();
}
public static function getIdByUUID($uuid)
{
$model = self::getByUUID($uuid);
return $model ? $model->id : false;
}
public static function generateToken()
{
return Hash::make(self::generateUUID());
}
public static function generateUUID()
{
return Str::uuid()->toString();
}
public static function getFields($field)
@@ -39,9 +65,9 @@ trait Basic
return self::getAll()->pluck($field);
}
public static function getByField($field, $value)
public static function getByField($field, $value, $relations = [])
{
return self::getModel()->where($field, $value);
return self::getModel()->with($relations)->where($field, $value)->get();
}
public static function getField($id, $field)
@@ -58,7 +84,7 @@ trait Basic
public static function store($data)
{
return ($data['id'] ?? false) ? self::update($data) : self::create($data);
return $data['id'] ?? false ? self::update($data) : self::create($data);
}
public static function create($data)
@@ -87,25 +113,32 @@ trait Basic
return self::getModel()->count();
}
public static function firstOrCreate($search, $data)
public static function firstOrCreate($search, $data = false)
{
return self::getModel()::firstOrCreate($search, $data);
return self::getModel()->firstOrCreate($search, $data ? $data : $search);
}
public static function get($id, $relations = false, $relations_count = false)
public static function deleteByField($field, $value)
{
return self::getModelRelations($relations, $relations_count)->find($id);
$model = self::getModel()->where($field, $value);
return $model ? $model->delete() : false;
}
public static function getAll($relations = false, $relations_count = false)
public static function get($id, $relations = false, $relationscount = false)
{
return self::getModelRelations($relations, $relations_count)->get();
return self::getModelRelations($relations, $relationscount)->find($id);
}
public static function getModelRelations($relations = false, $relations_count = false)
public static function getAll($relations = false, $relationscount = false)
{
return self::getModelRelations($relations, $relationscount)->get();
}
public static function getModelRelations($relations = false, $relationscount = false)
{
$model = $relations ? self::getModelWithRelations($relations) : false;
$model = $relations_count ? self::getModelWithCountRelations($relations_count, $model) : $model;
$model = $relationscount ? self::getModelWithCountRelations($relationscount, $model) : $model;
return $model ? $model : self::getModel();
}