false, 'reference' => '', 'slug' => '', 'picture' => '', 'sell_unit' => '', 'weight_with_unit' => '', // * Prix ttc du produit 'price_ttc' => 0, // * Prix ttc du produit remisé 'undiscounted_price_ttc' => 0, 'tax_rate' => 0, 'discount_type' => null, 'discount_rate' => 0, ]; /** * Controller * * @return void */ public function __construct() { $this->middleware(function (Request $request, callable $next) { $reponse = $next($request); if (\request()->routeIs('bo.*') and !\request()->ajax()) { if (!DeliveryMode::getCached()->where('status', true)->count()) { Session::flash('bo.warning', trans('Aucun mode de livraison n\'est disponible')); } if (!PaymentMode::getCached()->where('status', true)->count()) { Session::flash('bo.warning', trans('Aucun mode de paiement n\'est disponible')); } } return $reponse; }); } /** * Build Search query on specified fields * splitting search into words or using whole sentence * * @param \Illuminate\Database\Eloquent\Builder $query The eloquent query builder. * @param string $search The query string. * @param callable $searchQuery The query string. * @param string ...$fields Either a simple array or a string * in case of a string the two fields * would be concatenate to make the search. * @return \Illuminate\Database\Eloquent\Builder */ protected function searchQuery( Builder $query, string $search, callable $searchQuery = null, string ...$fields ): Builder { if (count($fields)) { // Search using words. $words = explode(' ', $search); $query->where(function ($query) use ($searchQuery, $words, $search, $fields) { foreach ($words as $word) { if (!strlen($word)) { continue; } $this->searchOnFields($query, $word, $fields); } //end foreach // Search whole using whole sentence. $this->searchOnFields($query, $search, $fields); if ($searchQuery) { $searchQuery($query); } }); } //end if return $query; } /** * Build query search on all fields * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $search * @param array $fields * @return \Illuminate\Database\Eloquent\Builder */ private function searchOnFields(Builder $query, string $search, array $fields): Builder { $table = $query->getModel()->getTable(); foreach ($fields as $f) { $f = str_replace('?', '\?', $f); if (is_array($f)) { $query->orWhereRaw( sprintf("UPPER(CONCAT(%s)) LIKE UPPER(?)", collect($f)->map(function ($q) use ($table) { return "`$table`.`$q`"; })->implode(', \' \', ')), ['%' . filter_var($search, FILTER_SANITIZE_STRING) . '%'] ); } else { $query->orWhereRaw( "UPPER(`$table`.`$f`) LIKE UPPER(?)", ['%' . filter_var($search, FILTER_SANITIZE_STRING) . '%'] ); } } return $query; } /** * Sort columns with a query * * @param \Illuminate\Database\Eloquent\Builder $query * @param array $ignore * @return \Illuminate\Database\Eloquent\Builder */ protected function sortQuery(Builder $query, array $ignore = []): Builder { $rName = request()->route()->getName(); $table = $query->getModel()->getTable(); if (request()->sort_col) { Session::put("$rName.sort_col", request()->sort_col); Session::put("$rName.sorted", true); } if (request()->sort_way) { Session::put("$rName.sort_way", request()->sort_way); Session::put("$rName.sorted", true); } if (request()->rst) { Session::remove("$rName.sorted"); Session::remove("$rName.sort_col"); Session::remove("$rName.sort_way"); } if (request()->sort_col and request()->sort_way and Schema::hasColumn($table, request()->sort_col)) { if ( !isset($ignore) or (isset($ignore) and !in_array(request()->sort_col, $ignore)) ) { $query = $query->orderBy(\sprintf("$table.%s", request()->sort_col), request()->sort_way); } } elseif (Schema::hasColumn($table, 'order')) { $query = $query->orderBy('order', 'ASC'); Session::put("$rName.sort_col", 'order'); Session::put("$rName.sort_way", 'asc'); } return $query; } /** * Filter basket items to keep only the orderable ones * * ! Assert Model found, status true and orderable true. * * @param \Gloudemans\Shoppingcart\Cart $basket * @param array $messages Will be filled with messages on warnings. * @return \Gloudemans\Shoppingcart\Cart */ protected function filterBasketKeepOnlyOrderableItems(Cart $basket, array &$messages = []): Cart { \collect($basket->content())->each(function (CartItem $item) use (&$basket, &$messages) { /** @var \App\Models\Merchandise\Product|null */ $product = $item->model; if (!$product or (!$product and (!$product->status or !$product->orderable))) { $basket->remove($item->rowId); $messages[] = \trans( 'Le produit :name a été retiré du panier car il n\'est plus disponible', [ 'name' => $item->name ] ); } }); return $basket; } /** * Generate and download transaction invoice * * ! Appliable for both Fo and Bo controllers * * @param \App\Models\Shop\Order $order * @param \App\Models\Payment\Transaction $transaction * @return \Symfony\Component\HttpFoundation\Response */ public function downloadTransactionInvoice(Order $order, Transaction $transaction): Response { $this->authorize('download', $transaction); $title = trans('Commande du :date - :orderRef - :transRef', [ 'date' => $transaction->created_at->isoFormat('DD\/MM\/YYYY'), 'orderRef' => $order->reference, 'transRef' => $transaction->reference, ]); $sluggedTitle = Str::slug(\implode(' ', [ config('app.name'), $transaction->type->equals(TransactionType::debit()) ? trans('Facture') : trans('Avoir'), $transaction->created_at->isoFormat('YYYY-MM-DD'), ])); return \response()->streamDownload(function () use ($transaction, $title) { echo $transaction->generateInvoice($title)->output(); }, "{$sluggedTitle}.pdf", [ 'Cache-Control' => 'public, must-revalidate, max-age=0', 'Content-Description' => 'File Transfer', 'Content-Type' => 'application/pdf', 'Expires' => 'Sat, 26 Jul 1997 05:00:00 GMT', 'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT', // For glory. 'X-Generator' => 'mPDF' ]); } /** * Generate and download transaction invoice * * ! Appliable for both Fo and Bo controllers * * @param \App\Models\Shop\Order $order * @return \Symfony\Component\HttpFoundation\Response */ public function downloadOrderInvoice(Order $order): Response { $this->authorize('download', $order); $title = trans('Commande du :date - :orderRef - :transRef', [ 'date' => $order->created_at->isoFormat('DD\/MM\/YYYY'), 'orderRef' => $order->reference, 'transRef' => trans('Toutes transactions'), ]); $sluggedTitle = Str::slug(\implode(' ', [ config('app.name'), trans('Facture'), $order->created_at->isoFormat('YYYY-MM-DD'), ])); return \response()->streamDownload(function () use ($order, $title) { echo $order->generateInvoice($title)->output(); }, "{$sluggedTitle}.pdf", [ 'Cache-Control' => 'public, must-revalidate, max-age=0', 'Content-Description' => 'File Transfer', 'Content-Type' => 'application/pdf', 'Expires' => 'Sat, 26 Jul 1997 05:00:00 GMT', 'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT', // For glory. 'X-Generator' => 'mPDF' ]); } }