diff --git a/app/Datatables/ParentDataTable.php b/app/Datatables/ParentDataTable.php index 02e52e66..87ff6c28 100644 --- a/app/Datatables/ParentDataTable.php +++ b/app/Datatables/ParentDataTable.php @@ -50,19 +50,31 @@ class ParentDataTable extends DataTable return self::getButtonEdit().self::getButtonDel(); } - public function getButtonEdit() + public function getButtonEdit($field = 'id', $title = 'Modifier') { - return ''; + return view('components.form.buttons.edit', [ + 'dataId' => '{{$'.$field.'}}', + 'class' => 'btn-sm mr-2', + 'title' => $title, + ]); } - public function getButtonShow() + public function getButtonShow($field = 'id', $title = 'Voir') { - return ''; + return view('components.form.buttons.show', [ + 'dataId' => '{{$'.$field.'}}', + 'class' => 'btn-sm mr-2', + 'title' => $title, + ]); } - public function getButtonDel() + public function getButtonDel($field ='id', $title = 'Effacer') { - return ''; + return view('components.form.buttons.delete', [ + 'dataId' => '{{$'.$field.'}}', + 'class' => 'btn-sm mr-2', + 'title' => $title, + ]); } public function makeColumnButtons() diff --git a/app/Datatables/Shop/CustomerInvoicesDataTable.php b/app/Datatables/Shop/CustomerInvoicesDataTable.php new file mode 100644 index 00000000..4b5768da --- /dev/null +++ b/app/Datatables/Shop/CustomerInvoicesDataTable.php @@ -0,0 +1,80 @@ +url = route('Shop.Invoices.index'); + } + + public function query(Invoice $model) + { + $customerId = Auth::id(); + $model = $model->byCustomer($customerId)->with(['address']); + + return $this->buildQuery($model); + } + + public function getHtmlButtons() + { + $buttons = view('components.form.button', [ + 'dataId' => '{{$uuid}}', + 'class' => 'btn-sm btn-secondary btn-invoice mr-2', + 'icon' => 'fa-file-pdf', + 'title' => 'Télécharger la facture', + 'url' => route('Shop.Invoices.pdf') . '/{{$uuid}}', + ]); + + $buttons .= self::getButtonShow('uuid', 'Voir la facture'); + + return $buttons; + } + + public function modifier($datatables) + { + $datatables + ->editColumn('status', function (Invoice $invoice) { + return Invoices::getStatus($invoice->status); + }) + ->editColumn('created_at', function (Invoice $invoice) { + return $invoice->created_at->isoFormat('DD/MM/YY HH:mm'); + }) + ->editColumn('payment_type', function (Invoice $invoice) { + return InvoicePayments::getPaymentType($invoice->payment_type); + }) + ->rawColumns(['action']); + + return parent::modifier($datatables); + } + + protected function getColumns() + { + return [ + Column::make('created_at')->title('Date'), + Column::make('ref')->title('Ref'), + Column::make('payment_type')->title('Règlement'), + Column::make('total_shipped')->title('Montant')->class('text-right'), + Column::make('status')->title('Statut'), + $this->makeColumnButtons(), + ]; + } +} diff --git a/app/Datatables/Shop/CustomerOrdersDataTable.php b/app/Datatables/Shop/CustomerOrdersDataTable.php index dc1eaa36..a0fe7f7c 100644 --- a/app/Datatables/Shop/CustomerOrdersDataTable.php +++ b/app/Datatables/Shop/CustomerOrdersDataTable.php @@ -4,8 +4,8 @@ namespace App\Datatables\Shop; use App\Datatables\ParentDataTable as DataTable; use App\Models\Shop\Order; -use App\Repositories\Shop\InvoicePayments; use App\Repositories\Shop\Orders; +use Illuminate\Support\Facades\Auth; use Yajra\DataTables\Html\Column; class CustomerOrdersDataTable extends DataTable @@ -18,6 +18,8 @@ class CustomerOrdersDataTable extends DataTable public $stateSave = true; + public $url = null; + public function __construct() { $this->url = route('Shop.Orders.index'); @@ -26,11 +28,16 @@ class CustomerOrdersDataTable extends DataTable public function query(Order $model) { $customerId = Auth::id(); - $model = $model->byCustomer($customerId)->with(['delivery']); + $model = $model->byCustomer($customerId); return $this->buildQuery($model); } + public function getHtmlButtons() + { + return self::getButtonShow('uuid', 'Voir la commande'); + } + public function modifier($datatables) { $datatables @@ -38,10 +45,7 @@ class CustomerOrdersDataTable extends DataTable return Orders::getStatus($order->status); }) ->editColumn('created_at', function (Order $order) { - return $order->created_at->toDateTimeString(); - }) - ->editColumn('payment_type', function (Order $order) { - return InvoicePayments::getPaymentType($order->payment_type); + return $order->created_at->isoFormat('DD/MM/YY HH:mm'); }) ->rawColumns(['action']); @@ -53,7 +57,6 @@ class CustomerOrdersDataTable extends DataTable return [ Column::make('created_at')->title('Date'), Column::make('ref')->title('Ref'), - Column::make('payment_type')->title('Règlement'), Column::make('total_shipped')->title('Montant')->class('text-right'), Column::make('status')->title('Statut'), $this->makeColumnButtons(), diff --git a/app/Http/Controllers/Shop/Controller.php b/app/Http/Controllers/Shop/Controller.php index d31997ee..32073d6a 100644 --- a/app/Http/Controllers/Shop/Controller.php +++ b/app/Http/Controllers/Shop/Controller.php @@ -6,4 +6,8 @@ use App\Http\Controllers\Controller as ParentController; class Controller extends ParentController { + public function __construct() + { + $this->middleware('auth.check'); + } } diff --git a/app/Http/Controllers/Shop/CustomerController.php b/app/Http/Controllers/Shop/CustomerController.php index ef568e63..77fad96c 100644 --- a/app/Http/Controllers/Shop/CustomerController.php +++ b/app/Http/Controllers/Shop/CustomerController.php @@ -2,20 +2,26 @@ namespace App\Http\Controllers\Shop; -use App\Http\Controllers\Controller; +use App\Http\Controllers\Shop\Controller; use App\Repositories\Shop\Customers; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; class CustomerController extends Controller { - public function profile($id = false) + public function profile() { - $data = Customers::editProfile($id); + $data = Customers::editProfile(Customers::getId()); return view('Shop.Customers.profile', $data); } + public static function checkAuth() + { + if (Customers::isNotConnected()) { + return redirect()->route('Shop.login'); + } + } + public function modalProfile($id = false) { $data = [ @@ -27,9 +33,8 @@ class CustomerController extends Controller public function edit() { - $id = Auth::id(); $data = [ - 'customer' => Customers::edit($id), + 'customer' => Customers::edit(Customers::getId()), ]; return view('Shop.Customers.edit', $data); diff --git a/app/Http/Controllers/Shop/InvoiceController.php b/app/Http/Controllers/Shop/InvoiceController.php index 58827921..c1b064b9 100644 --- a/app/Http/Controllers/Shop/InvoiceController.php +++ b/app/Http/Controllers/Shop/InvoiceController.php @@ -2,22 +2,25 @@ namespace App\Http\Controllers\Shop; -use App\Http\Controllers\Controller; +use App\Http\Controllers\Shop\Controller; +use App\Datatables\Shop\CustomerInvoicesDataTable; use App\Repositories\Shop\InvoicePDF; use App\Repositories\Shop\Invoices; class InvoiceController extends Controller { - public function index() + public function index(CustomerInvoicesDataTable $dataTable) { - // + return $dataTable->render('Shop.Invoices.partials.list'); } - public function show($uuid) + public function view($uuid) { - $data = Invoices::getByUUID($uuid); + $data = [ + 'invoice' => Invoices::view($uuid), + ]; - return view('Shop.Invoices.show', $data); + return view('Shop.Invoices.view', $data); } public function pdf($uuid) diff --git a/app/Http/Controllers/Shop/OrderController.php b/app/Http/Controllers/Shop/OrderController.php index bc9865e1..35ee6ca4 100644 --- a/app/Http/Controllers/Shop/OrderController.php +++ b/app/Http/Controllers/Shop/OrderController.php @@ -2,8 +2,8 @@ namespace App\Http\Controllers\Shop; -use App\Datatables\Shop\OrdersDataTable; -use App\Http\Controllers\Controller; +use App\Datatables\Shop\CustomerOrdersDataTable; +use App\Http\Controllers\Shop\Controller; use App\Repositories\Core\User\ShopCart; use App\Repositories\Shop\Baskets; use App\Repositories\Shop\Customers; @@ -17,7 +17,7 @@ use Illuminate\Http\Request; class OrderController extends Controller { - public function index(OrdersDataTable $dataTable) + public function index(CustomerOrdersDataTable $dataTable) { return $dataTable->render('Shop.Orders.partials.list'); } diff --git a/app/Http/Controllers/Shop/SearchController.php b/app/Http/Controllers/Shop/SearchController.php index bef8f4c5..46014c41 100644 --- a/app/Http/Controllers/Shop/SearchController.php +++ b/app/Http/Controllers/Shop/SearchController.php @@ -12,7 +12,8 @@ class SearchController extends Controller { $data = $request->input(); $data['product_type'] = 'botanic'; - $articles = Searches::getResults($request->input()); + // $articles = Searches::getResults($request->input()); + $articles = Searches::search($request->input()); $data = [ 'articles' => $articles, 'articles_count' => $articles ? count($articles) : 0, @@ -20,8 +21,6 @@ class SearchController extends Controller 'product_type' => $data['product_type'], ]; - // dump($data); - // exit; return view('Shop.Search.results', $data); } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index deb65e86..5ed09fb9 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -54,6 +54,7 @@ class Kernel extends HttpKernel 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, + 'auth.check' => \App\Http\Middleware\CheckAuth::class, 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, diff --git a/app/Http/Middleware/CheckAuth.php b/app/Http/Middleware/CheckAuth.php new file mode 100644 index 00000000..54d19b1e --- /dev/null +++ b/app/Http/Middleware/CheckAuth.php @@ -0,0 +1,19 @@ +where($this->table.'.name', 'LIKE', '%'.$str.'%'); } - public function scopeSearch($query, $str) + public function scopeRawSearch($query, $str) { return $query->where($this->table.'.name', 'LIKE', '%'.$str.'%'); } @@ -148,6 +147,11 @@ class Article extends Model implements HasMedia return $query->whereIn($this->table.'.product_type', [Variety::class, Specie::class]); } + public function scopeByIDs($query, $ids) + { + return $query->whereIN($this->table.'.id', $ids); + } + public function scopeMerchandise($query) { return $query->byProduct(Merchandise::class); @@ -218,6 +222,7 @@ class Article extends Model implements HasMedia return [ 'id' => (int) $this->id, + 'article_nature_id' => $this->article_nature_id, 'name' => $this->name, 'description' => html_entity_decode(strip_tags($description)), ]; diff --git a/app/Models/Shop/Invoice.php b/app/Models/Shop/Invoice.php index 99aa9e45..94132b75 100644 --- a/app/Models/Shop/Invoice.php +++ b/app/Models/Shop/Invoice.php @@ -40,6 +40,11 @@ class Invoice extends Model return $this->belongsTo(CustomerAddress::class, 'invoice_address_id'); } + public function scopeByCustomer($query, $customerId) + { + return $query->where('customer_id', $customerId); + } + public function scopeByUUID($query, $uuid) { return $query->where('uuid', $uuid); diff --git a/app/Repositories/Shop/Articles.php b/app/Repositories/Shop/Articles.php index 3a1de02c..d282af91 100644 --- a/app/Repositories/Shop/Articles.php +++ b/app/Repositories/Shop/Articles.php @@ -18,7 +18,7 @@ class Articles public static function autocomplete($str) { - $data = Article::byAutocomplete($str)->orderBy('name')->limit(30)->pluck('name', 'id'); + $data = Article::byAutocomplete($str)->orderBy('name')->limit(20)->pluck('name', 'id'); $export = []; foreach ($data as $key => $name) { $export[] = ['value' => $key, 'text' => $name]; @@ -143,6 +143,7 @@ class Articles public static function getArticlesToSell($options) { $articles = self::getArticlesWithOffers($options); + $searchOrder = array_flip($options['ids']->toArray() ?? []); foreach ($articles as $article) { $price_lists = $article->offers[0]->tariff->price_lists->toArray(); if (! count($price_lists)) { @@ -150,6 +151,7 @@ class Articles } if (! is_array($data[$article->name] ?? false)) { $data[$article->name] = self::getDataForSale($article); + $data[$article->name]['searchOrder'] = $searchOrder[$article->id]; } $prices = $price_lists[0]['price_list_values'][0]; $article_nature_name = strtolower($article->article_nature->name); @@ -248,20 +250,17 @@ class Articles public static function getModelByOptions($options = false) { - $category_id = $options['category_id'] ?? false; - $search = $options['search'] ?? false; - $tags = $options['tags'] ?? false; $article_nature_id = $options['article_nature_id'] ?? false; $article_nature_ids = $options['article_nature_ids'] ?? false; - $product_type = $options['product_type'] ?? false; $model = $options['homepage'] ?? false ? Article::homepage()->visible() : Article::visible(); - $model = $category_id ? $model->byCategoryParent($category_id) : $model; - $model = $tags ? $model->byTags($tags) : $model; - $model = $search ? $model->search($search) : $model; + $model = $options['category_id'] ?? false ? $model->byCategoryParent($options['category_id']) : $model; + $model = $options['tags'] ?? false ? $model->byTags($options['tags']) : $model; + $model = $options['search'] ?? false ? $model->rawSearch($options['search']) : $model; + $model = $options['ids'] ?? false ? $model->byIDs($options['ids']) : $model; $model = $article_nature_id ? $model->byArticleNature($article_nature_id) : $model; $model = $article_nature_ids ? $model->byArticleNatures($article_nature_ids) : $model; - switch ($product_type) { + switch ($options['product_type'] ?? false) { case 'botanic': $model = $model->botanic(); break; @@ -324,11 +323,6 @@ class Articles return $data; } - public static function getPricesByArticle($article) - { - return Prices::getByArticle($article->id); - } - public static function storeFull($data) { $images = $data['images'] ?? false; diff --git a/app/Repositories/Shop/Customers.php b/app/Repositories/Shop/Customers.php index bd2208ae..c921a863 100644 --- a/app/Repositories/Shop/Customers.php +++ b/app/Repositories/Shop/Customers.php @@ -2,6 +2,7 @@ namespace App\Repositories\Shop; +use App\Datatables\Shop\CustomerInvoicesDataTable; use App\Datatables\Shop\CustomerOrdersDataTable; use App\Models\Shop\Customer; use App\Repositories\Core\File; @@ -54,13 +55,14 @@ class Customers public static function editProfile($id = false) { - $id = $id ? $id : self::getId(); $datatableOrders = new CustomerOrdersDataTable(); + $datatableInvoices = new CustomerInvoicesDataTable(); return [ 'customer' => self::get($id, ['addresses', 'deliveries'])->toArray(), 'deliveries' => Deliveries::getAllWithSaleChannel()->toArray(), 'orders' => $datatableOrders->html(), + 'invoices' => $datatableInvoices->html(), ]; } diff --git a/app/Repositories/Shop/Invoices.php b/app/Repositories/Shop/Invoices.php index 0d09c129..873b9b49 100644 --- a/app/Repositories/Shop/Invoices.php +++ b/app/Repositories/Shop/Invoices.php @@ -29,39 +29,54 @@ class Invoices public static function init() { return [ - 'statuses' => Invoices::statuses(), + 'statuses' => self::statuses(), 'payment_types' => InvoicePayments::paymentTypes(), ]; } - public static function getFull($id) + public static function view($uuid) { - return self::get($id, ['address', 'payments', 'order.customer', 'order.delivery_address', 'order.detail']); + $data = self::getFullByUUID($uuid)->toArray(); + $data['payment_type'] = InvoicePayments::getPaymentType($data['payment_type']); + $data['status'] = self::getStatus($data['status']); + + return $data; } - public static function getByUUID($uuid) + public static function getFullByUUID($id) { - return Invoice::byUUID($uuid)->first(); + return self::getByUUID($id, self::full()); + } + + public static function getFull($id) + { + return self::get($id, self::full()); + } + + public static function full() + { + return [ + 'address', + 'customer', + 'order.delivery_address', + 'order.detail', + 'order.sale_channel', + 'payments', + ]; } public static function saveInvoice($orderId, $data) { $data['order_id'] = $orderId; $data['date_invoice'] = date('Y-m-d'); + $data['date_due'] = Carbon::now()->addMonth()->format('Y-m-d'); return self::store($data); } - public static function countByMonth() - { - $start = Carbon::now()->beginOfMonth(); - - return Invoice::where('created_at', '>', $start); - } - public static function create($data) { - InvoiceStats::increase($data['total_taxed']); + InvoiceStats::increase($data['total']); $data['uuid'] = Str::uuid()->toString(); $data['ref'] = self::getNewRef(); @@ -71,7 +86,7 @@ class Invoices public static function delete($id) { $invoice = self::get($id); - InvoiceStats::decrease($invoice->total_priced); + InvoiceStats::decrease($invoice->total); return Invoice::destroy($id); } diff --git a/app/Repositories/Shop/Orders.php b/app/Repositories/Shop/Orders.php index 62517610..e3ac303f 100644 --- a/app/Repositories/Shop/Orders.php +++ b/app/Repositories/Shop/Orders.php @@ -14,6 +14,15 @@ class Orders { use Basic, DateStats; + public static function view($uuid) + { + $data = self::getFullByUUID($uuid)->toArray(); + $data['payment_type'] = InvoicePayments::getPaymentType($data['payment_type']); + $data['status'] = self::getStatus($data['status']); + + return $data; + } + public static function getLast($nb = 10) { return Order::with('customer')->orderBy('id', 'DESC')->take($nb)->get(); @@ -40,24 +49,12 @@ class Orders public static function getFullByUUID($uuid) { - return self::getFull(self::getIdByUUID($uuid)); + return self::getByUUID($uuid, self::full()); } public static function getFull($id) { - return Order::with(['customer', 'delivery', 'delivery_address', 'detail', 'invoice.address', 'sale_channel']) - ->byID($id)->first(); - } - - public static function view($uuid) - { - $data = []; - $order = self::getFullByUUID($uuid); - $data = $order->toArray(); - $data['payment_type'] = InvoicePayments::getPaymentType($order->payment_type); - $data['status'] = Orders::getStatus($order->status); - - return $data; + return self::get($id, self::full()); } public static function saveOrder($data) @@ -160,6 +157,11 @@ class Orders return $lastRef ? $lastRef->ref + 1 : $ref + 1; } + public static function full() + { + return ['customer', 'delivery', 'delivery_address', 'detail', 'invoice.address', 'sale_channel']; + } + public static function getModel() { return Order::query(); diff --git a/app/Repositories/Shop/Searches.php b/app/Repositories/Shop/Searches.php index 6dc2e7b1..d958c131 100644 --- a/app/Repositories/Shop/Searches.php +++ b/app/Repositories/Shop/Searches.php @@ -6,9 +6,11 @@ use App\Models\Shop\Article; class Searches { - public static function search($query) + public static function search($options) { - return Article::withAvailableOffers()->search($query)->get(); + return collect(Articles::getArticlesToSell([ + 'ids' => Article::search($options['search_name'])->get()->pluck('id'), + ]))->sortBy('searchOrder')->toArray(); } public static function getResults($options) { diff --git a/config/scout.php b/config/scout.php index dff17852..77d6c51b 100644 --- a/config/scout.php +++ b/config/scout.php @@ -136,8 +136,8 @@ return [ 'key' => env('MEILISEARCH_KEY', null), 'index-settings' => [ Article::class => [ - 'filterableAttributes'=> ['id', 'name', 'description'], - 'sortableAttributes' => ['created_at'], + 'filterableAttributes'=> ['article_nature_id'], + 'sortableAttributes' => ['name', 'created_at'], ], ], ], diff --git a/resources/views/Shop/Customers/partials/invoices.blade.php b/resources/views/Shop/Customers/partials/invoices.blade.php deleted file mode 100644 index 5ac52600..00000000 --- a/resources/views/Shop/Customers/partials/invoices.blade.php +++ /dev/null @@ -1,28 +0,0 @@ -@include('Shop.Orders.partials.list', ['dataTable' => $orders]) - -@if ($customer['orders'] ?? false) -
| - {{ Carbon\Carbon::parse($order['created_at'])->format('d-m-Y') }} - | -- Numero facture - | -- {{ $order['total_shipped'] }} € - | -- - - - | -