diff --git a/app/Console/Commands/untranslateShelves.php b/app/Console/Commands/untranslateShelves.php new file mode 100644 index 00000000..9a02bfc7 --- /dev/null +++ b/app/Console/Commands/untranslateShelves.php @@ -0,0 +1,50 @@ +name, true); + $name = $trans['fr']; + $trans = $category->description ? json_decode($category->description, true) : false; + $description = $trans ? $trans['fr'] : ''; + $category->update(['name' => $name, 'description' => $description]); + } + } +} diff --git a/app/Datatables/Shop/CategoriesDataTable.php b/app/Datatables/Shop/CategoriesDataTable.php index dbb7c34e..df985a9a 100644 --- a/app/Datatables/Shop/CategoriesDataTable.php +++ b/app/Datatables/Shop/CategoriesDataTable.php @@ -12,7 +12,7 @@ class CategoriesDataTable extends DataTable public function query(Category $model) { - $model = $model::with(['tags.articles'])->withCount(['articles', 'tags']); + $model = $model::notRoot()->with(['tags.articles'])->withCount(['articles', 'tags']); return $this->buildQuery($model); } diff --git a/app/Datatables/Shop/OffersDataTable.php b/app/Datatables/Shop/OffersDataTable.php index 7c49f64b..ea7f1bcf 100644 --- a/app/Datatables/Shop/OffersDataTable.php +++ b/app/Datatables/Shop/OffersDataTable.php @@ -59,11 +59,11 @@ class OffersDataTable extends DataTable { return [ Column::make('status_id')->title('')->width(40), - Column::make('article.article_nature.name')->title('Nature'), - Column::make('thumb')->title('')->width(40)->searchable(false), - Column::make('article.name')->title('Article'), - Column::make('variation.name')->title('Déclinaison'), - Column::make('tariff.name')->title('Tarif'), + Column::make('article.article_nature.name')->title('Nature')->defaultContent(''), + Column::make('thumb')->title('')->width(40)->searchable(false)->order(false), + Column::make('article.name')->title('Article')->defaultContent(''), + Column::make('variation.name')->title('Déclinaison')->defaultContent(''), + Column::make('tariff.name')->title('Tarif')->defaultContent(''), Column::make('stock_current')->title('Appro im')->searchable(false), Column::make('stock_delayed')->title('Appro délai')->searchable(false), Column::make('stock_ondemand')->title('Dmde')->searchable(false), diff --git a/app/Http/Controllers/Shop/ArticleController.php b/app/Http/Controllers/Shop/ArticleController.php index b7ee3e8b..efa55f72 100644 --- a/app/Http/Controllers/Shop/ArticleController.php +++ b/app/Http/Controllers/Shop/ArticleController.php @@ -14,6 +14,8 @@ class ArticleController extends Controller { $data = self::init(); $data['article'] = Articles::getArticleToSell($id); + // dump($data); + // exit; return view('Shop.Articles.show', $data); } } diff --git a/app/Http/Controllers/Shop/BasketController.php b/app/Http/Controllers/Shop/BasketController.php new file mode 100644 index 00000000..e9169dfc --- /dev/null +++ b/app/Http/Controllers/Shop/BasketController.php @@ -0,0 +1,70 @@ +input('offer_id'); + $quantity = $request->input('quantity'); + $price = Offers::getPrice($offer_id, $quantity)->price_taxed; + return $quantity * $price; + } + + public function addBasket(Request $request) + { + $offer_id = $request->input('offer_id'); + $quantity = $request->input('quantity'); + + if (ShopCart::has($offer_id)) { + $ret = ShopCart::remove($offer_id); + } + $data = Offers::getBasketData($offer_id, $quantity); + $ret = ShopCart::add($data); + return true; + } + + public function basket() + { + $data['basket'] = Offers::getBasket(); + dump($data['basket']->toArray()); + exit; + return view('Shop.Baskets.basket', $data); + } + + public function getBasket() + { + $data = ShopCart::getContent(); + return response()->json(['data' => $data, 'code' => '200']); + } + + public function countBasket() + { + return ShopCart::count(); + } + + public function order(Request $request) + { + ShopCart::clear(); + $data = $request->all(); + unset($data['_token']); + $data['user_id'] = Users::getId(); + Orders::newOrder($data); + return response()->json(['code' => '200']); + + // return redirect()->route('ThirdParty.select'); + } + + public function clearBasket() + { + return ShopCart::clear(); + } +} diff --git a/app/Http/Controllers/Shop/HomeController.php b/app/Http/Controllers/Shop/HomeController.php index e9debb86..1f83f940 100644 --- a/app/Http/Controllers/Shop/HomeController.php +++ b/app/Http/Controllers/Shop/HomeController.php @@ -23,7 +23,9 @@ class HomeController extends Controller $input = $request->input(); $data = self::init(); $data['display_by_rows'] = $input['by_rows'] ?? false; - $data['articles'] = Articles::getArticlesToSell(); + $data['shelves'] = Articles::getArticlesByHomepage(); + // dump($data['shelves']); + // exit; $data['tags'] = TagGroups::getWithTagsAndCountOffers(); return view('Shop.home', $data); } diff --git a/app/Models/Shop/Category.php b/app/Models/Shop/Category.php index 3d6babc4..d877422f 100644 --- a/app/Models/Shop/Category.php +++ b/app/Models/Shop/Category.php @@ -24,12 +24,13 @@ class Category extends parentCategory protected $guarded = ['id']; protected $table = 'categories'; - public $translatable = ['name', 'description']; + public $translatable = []; protected $cascadeDeleteMorph = ['Articles']; protected $fillable = [ + 'visible', + 'homepage', 'slug', 'name', - 'visible', 'description', NestedSet::LFT, NestedSet::RGT, @@ -61,4 +62,14 @@ class Category extends parentCategory { return $query->where('visible', 1); } + + public function scopeHomepage($query) + { + return $query->where('homepage', 1); + } + + public function scopeNotRoot($query) + { + return $query->where('id', '<>', 1); + } } diff --git a/app/Models/Shop/Offer.php b/app/Models/Shop/Offer.php index c5e3eb47..f44fda8a 100644 --- a/app/Models/Shop/Offer.php +++ b/app/Models/Shop/Offer.php @@ -150,4 +150,35 @@ class Offer extends Model $query->active()->bySaleChannel($sale_channel_id); }); } + + public function scopeWithPriceListsBySaleChannel($query, $sale_channel_id) + { + return $query->with([ + 'price_lists' => function($query) use ($sale_channel_id) { + $query->bySaleChannel($sale_channel_id); + } + ]); + } + + public function scopeWithPriceListValuesBySaleChannel($query, $sale_channel_id) + { + return $query->with([ + 'price_lists' => function($query) use ($sale_channel_id) { + $query->bySaleChannel($sale_channel_id); + }, + 'price_lists.price_list_values', + ]); + } + + public function scopeWithPriceBySaleChannelByQuantity($query, $sale_channel_id, $quantity = 1) + { + return $query->with([ + 'price_lists' => function($query) use ($sale_channel_id) { + $query->bySaleChannel($sale_channel_id); + }, + 'price_lists.price_list_values' => function($query) use ($quantity) { + $query->byQuantity($quantity); + }, + ]); + } } diff --git a/app/Repositories/Core/Categories.php b/app/Repositories/Core/Categories.php index dafa5c9c..91ccd930 100644 --- a/app/Repositories/Core/Categories.php +++ b/app/Repositories/Core/Categories.php @@ -20,7 +20,6 @@ class Categories { $categories = self::getCategoryTreeVisibles()->toArray(); return $categories ? self::getChildren($categories[0]['children'], $withFolder) : []; - return $categories ? self::getChildren($categories[0]['children'], $withFolder) : []; } public static function getTree($withFolder = false) @@ -111,7 +110,7 @@ class Categories public static function getModel() { - // return Category::class; - return app('rinvex.categories.category'); + return app(Category::class); + // return app('rinvex.categories.category'); } } diff --git a/app/Repositories/Core/User/ShopCart.php b/app/Repositories/Core/User/ShopCart.php index a8d2cd12..1b3967a9 100644 --- a/app/Repositories/Core/User/ShopCart.php +++ b/app/Repositories/Core/User/ShopCart.php @@ -3,6 +3,7 @@ namespace App\Repositories\Core\User; use App\Repositories\Core\Auth\Users; +use Illuminate\Support\Facades\Auth; use \Cart; class ShopCart @@ -19,9 +20,8 @@ class ShopCart public static function clear() { - Cart::session(1)->clear(); - return Cart::clear(); - // return self::get()->clear(); + Cart::clear(); + return Cart::session(Auth::id())->clear(); } public static function has($id) diff --git a/app/Repositories/Core/User/ShopCartStorage.php b/app/Repositories/Core/User/ShopCartStorage.php index 9075eebd..eff1438d 100644 --- a/app/Repositories/Core/User/ShopCartStorage.php +++ b/app/Repositories/Core/User/ShopCartStorage.php @@ -16,11 +16,11 @@ class ShopCartStorage public function get($key) { - if ($this->has($key)) { - return new CartCollection(CartStorage::find($key)->cart_data); - } else { + if (!$this->has($key)) { return []; } + return new CartCollection(CartStorage::find($key)->cart_data); + } public function put($key, $value) diff --git a/app/Repositories/Shop/Articles.php b/app/Repositories/Shop/Articles.php index c7fd7de9..eb239575 100644 --- a/app/Repositories/Shop/Articles.php +++ b/app/Repositories/Shop/Articles.php @@ -32,7 +32,8 @@ class Articles $article_ids = self::getSiblingsIds($id); $offers = Offers::getOffersByArticles($article_ids, $sale_channel_id); foreach ($offers as $offer) { - $data[strtolower($offer->article_nature->name)] = [ + $data[strtolower($offer->article_nature->name)][] = [ + 'id' => $offer->id, 'name' => $offer->variation->name, 'prices' => $offer->tariff->price_lists->first()->price_list_values->toArray(), ]; @@ -77,7 +78,6 @@ class Articles public static function getArticleToSell($id, $sale_channel_id = false) { - $sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID(); $article = self::get($id); $data = $article->toArray(); $parents = self::getInheritedByProduct($article->product_id, $article->product_type); @@ -126,6 +126,19 @@ class Articles return $data; } + public static function getArticlesByHomepage() + { + $shelves = Categories::getByHomepage(); + foreach ($shelves as $shelve) { + $data[] = [ + 'id' => $shelve->id, + 'name' => $shelve->name, + 'articles' => self::getArticlesToSell($shelve->id), + ]; + } + return $data; + } + public static function getArticlesToSell($category_id = false, $tags = false) { $articles = self::getArticlesWithOffers($category_id, $tags); @@ -135,6 +148,7 @@ class Articles if (count($price_lists)) { if (!is_array($data[$article->name] ?? false)) { $data[$article->name] = [ + 'id' => $article->id, 'description' => (!empty($article->description)) ? $article->description : $article->product->description, 'image' => self::getFullImageByArticle($article), 'product_type' => $article->product_type, diff --git a/app/Repositories/Shop/Categories.php b/app/Repositories/Shop/Categories.php index a3be9a4b..1dd83867 100644 --- a/app/Repositories/Shop/Categories.php +++ b/app/Repositories/Shop/Categories.php @@ -8,6 +8,12 @@ use App\Repositories\Core\Categories as CategoryTrees; class Categories { + + public static function getByHomepage() + { + return Category::homepage()->orderBy('name', 'asc')->get(); + } + public static function getAll() { return Category::orderBy('name', 'asc')->get(); diff --git a/app/Repositories/Shop/Offers.php b/app/Repositories/Shop/Offers.php index 7543f175..ce44944c 100644 --- a/app/Repositories/Shop/Offers.php +++ b/app/Repositories/Shop/Offers.php @@ -3,9 +3,40 @@ namespace App\Repositories\Shop; use App\Models\Shop\Offer; +use App\Repositories\Core\User\ShopCart; class Offers { + + public static function getPrice($id, $quantity = 1, $sale_channel_id = false) + { + $sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID(); + $offer = Offer::withPriceBySaleChannelByQuantity($sale_channel_id, $quantity)->find($id); + return $offer->price_lists->first()->price_list_values->first(); + } + + public static function getBasket() + { + $basket = ShopCart::getContent(); + $offers = Offer::with(['variation', 'article.article_nature'])->whereIn('id', ShopCart::keys())->get(); + dump($basket->toArray()); + dump($offers->toArray()); + exit; + return $data; + } + + 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($articles_ids, $sale_channel_id = false) { $sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID(); @@ -13,7 +44,7 @@ class Offers ->with([ 'article_nature', 'variation', - 'tariff.price_lists' => function($query) use ($sale_channel_id) { + 'tariff.price_lists' => function ($query) use ($sale_channel_id) { $query->bySaleChannel($sale_channel_id); }, 'tariff.price_lists.price_list_values', @@ -30,7 +61,7 @@ class Offers ->with([ 'article_nature', 'variation', - 'tariff.price_lists' => function($query) use ($sale_channel_id) { + 'tariff.price_lists' => function ($query) use ($sale_channel_id) { $query->bySaleChannel($sale_channel_id); }, 'tariff.price_lists.price_list_values', diff --git a/resources/views/Admin/Shop/Categories/form.blade.php b/resources/views/Admin/Shop/Categories/form.blade.php index b033feb3..63a45b84 100644 --- a/resources/views/Admin/Shop/Categories/form.blade.php +++ b/resources/views/Admin/Shop/Categories/form.blade.php @@ -1,7 +1,7 @@