Skip to content

Commit

Permalink
- remove sponsorship (I am not sponsored anymore, but I appreciate je…
Browse files Browse the repository at this point in the history
…tbrains for their sponsor in the previous years)

- more preparation for groups
- pint fix for some files

Took 4 hours 59 minutes
  • Loading branch information
Cybrarist committed Jan 27, 2025
1 parent cc1194d commit 75c2a74
Show file tree
Hide file tree
Showing 19 changed files with 820 additions and 550 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,3 @@ cp .env.example .env
```

Don't forget to also populate the .env file with your own APP_KEY (a random 32 characters string).

## Sponsors
![Jetbrains](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)
247 changes: 117 additions & 130 deletions app/Console/Commands/DiscountInstallCommand.php

Large diffs are not rendered by default.

97 changes: 77 additions & 20 deletions app/Filament/Resources/GroupResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
use App\Enums\StatusEnum;
use App\Filament\Resources\GroupResource\Pages;
use App\Filament\Resources\GroupResource\RelationManagers;
use App\Helpers\GroupHelper;
use App\Models\Group;
use App\Models\Product;
use App\Models\ProductStore;
use App\Models\Store;
use Filament\Forms;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Arr;

class GroupResource extends Resource
{
Expand All @@ -27,20 +30,33 @@ class GroupResource extends Resource

protected static ?int $navigationSort = 3;

public static array $stores_with_same_currency = [];

public static function form(Form $form): Form
{
if ($form->getRecord()) {
self::$stores_with_same_currency = Store::where('currency_id', $form->getRecord()->currency_id)->pluck('id')->toArray();
}

return $form
->schema([
Forms\Components\TextInput::make("name")
->string()
->required()
->minLength(3),
->default('hi'),

Forms\Components\TextInput::make("notify_price")
->default(0)
->numeric()
->required(),

TextInput::make('notify_percentage')
->nullable()
->hintIcon("heroicon-o-information-circle",
"Get notified when price drops below specified percentage")
->suffix('%')
->numeric(),

Select::make('status')
->options(StatusEnum::to_array())
->default(StatusEnum::Published)
Expand All @@ -51,13 +67,22 @@ public static function form(Form $form): Form
->required()
->relationship("currency", "code")
->preload()
->afterStateUpdated(function ($state, $record) {
if ($state ||
(int) $state != $record?->currency_id
) {
self::$stores_with_same_currency = Store::where('currency_id', $state)->pluck('id')->toArray();
dump(self::$stores_with_same_currency);
}
})
->live()
->native(false),

DatePicker::make('snoozed_until')
->label("Snooze Notification Until"),

Forms\Components\TextInput::make('lowest_within')
->label("Alert if Product lowest within")
->label("Alert if Group lowest within")
->nullable()
->suffix('days')
->maxValue(65535),
Expand All @@ -71,25 +96,59 @@ public static function form(Form $form): Form

Section::make('Products Available')
->schema([
Repeater::make('products')
Repeater::make('products_available')
->schema([
Select::make("product_id")
->label("Products")
->multiple()
->options(function ($record) {
->options(function ($record, $get, $state, $component) {

dump(self::$stores_with_same_currency);
if (! $get('../../currency_id')) {
Notification::make()
->danger()
->title('Please select a currency')
->send();

return [];
}

$current_parent_id = $component->getContainer()->getStatePath(false);

// get the available values across the repeater
$all_products_across_fields = Arr::pluck(
Arr::except($get('../../products_available'),
$current_parent_id), 'product_id'
);

// combine the values into one array
$all_products_across_fields = Arr::collapse($all_products_across_fields);

$available_products = Product::whereNotNull("name")
->whereIn("products.id",
ProductStore::whereIn('store_id', self::$stores_with_same_currency)
->distinct()
->pluck("product_id")
->toArray()
)
->whereNotIn("id", $all_products_across_fields);

if ($record) {
return Product::whereNotNull("name")
->whereNotIn("products.id",
\DB::table("group_product")
->where("group_id", $record->id)
->pluck("product_id")->toArray()
)->pluck("name", "id");
} else {
return Product::whereNotNull("name")->get()->pluck("name", "id");
$available_products->whereNotIn("products.id",
\DB::table("group_product")
->where("group_id", $record->id)
->pluck("product_id")
->toArray()
);
}

return $available_products->pluck("name", "id");
})
->live()
->preload()
->distinct()
->native(false),

TextInput::make('key')
->string(),

Expand All @@ -102,12 +161,12 @@ public static function form(Form $form): Form
Repeater::make('url_products')
->schema([
TextInput::make("url")
->url(),
->url()
->distinct(),

TextInput::make('key')
->string(),
])
->nullable()
->defaultItems(0)
->columns(2),
]),
Expand All @@ -120,12 +179,10 @@ public static function table(Table $table): Table
return $table
->columns([
Tables\Columns\TextColumn::make("name"),
Tables\Columns\TextColumn::make("current_price"),
Tables\Columns\TextColumn::make("highest_price"),
Tables\Columns\TextColumn::make("lowest_price"),
Tables\Columns\TextColumn::make("notify_price"),
Tables\Columns\TextColumn::make("id")
->label("Price")
->formatStateUsing(function ($record) {
return GroupHelper::get_current_price($record);
}),
Tables\Columns\TextColumn::make("status"),

])
Expand Down
53 changes: 3 additions & 50 deletions app/Filament/Resources/GroupResource/Pages/CreateGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,19 @@

namespace App\Filament\Resources\GroupResource\Pages;

use App\Classes\GroupHelper;
use App\Classes\MainStore;
use App\Classes\Stores\Amazon;
use App\Classes\Stores\Argos;
use App\Classes\Stores\Ebay;
use App\Classes\Stores\Walmart;
use App\Classes\URLHelper;
use App\Filament\Resources\GroupResource;
use App\Helpers\URLHelper;
use App\Models\Product;
use Filament\Actions;
use App\Models\ProductStore;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use PhpParser\Node\Arg;

class CreateGroup extends CreateRecord
{
protected static string $resource = GroupResource::class;

protected function mutateFormDataBeforeCreate(array $data): array
{

if ($this->data["url_products"]){
$urls=$this->data["url_products"];
//make sure all urls are correct
foreach ($urls as $single_url){
if ($single_url["url"]){
$url=new URLHelper($single_url['url']);
if (!MainStore::validate_url($url))
$this->halt();
}
}
}

return parent::mutateFormDataBeforeCreate($data); // TODO: Change the autogenerated stub
}


protected function afterCreate()
{
if (Arr::exists($this->data , "products"))
GroupHelper::update_products_records($this->record->id , $this->data["products"]);

if ($this->data["url_products"]){
$urls=$this->data["url_products"];
foreach ($urls as $single_url){
if ($single_url["url"]){
$url=new URLHelper($single_url['url']);
if (!MainStore::validate_url($url))
$this->halt();
else{
$product_id=MainStore::create_product($url);
GroupHelper::update_group_product_record($this->record->id, $product_id, $single_url["key"]);
}
}

}

}
}
}
100 changes: 75 additions & 25 deletions app/Filament/Resources/GroupResource/Pages/EditGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

namespace App\Filament\Resources\GroupResource\Pages;

use App\Classes\GroupHelper;
use App\Classes\MainStore;
use App\Classes\URLHelper;
use App\Filament\Resources\GroupResource;
use App\Models\Product;
use App\Models\ProductStore;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class EditGroup extends EditRecord
{
Expand All @@ -24,30 +21,83 @@ protected function getHeaderActions(): array
];
}

protected function afterSave(): void
{

protected function handleRecordUpdate(Model $record, array $data): Model
{
if (Arr::exists($this->data , "products"))
GroupHelper::update_products_records($record->id , $data["products"]);

if ($this->data["url_products"]){
$urls=$this->data["url_products"];
foreach ($urls as $single_url){
if($single_url["url"]){
$url=new URLHelper($single_url['url']);
if (!MainStore::validate_url($url))
$this->halt();
else{
$product_id=MainStore::create_product($url);
GroupHelper::update_group_product_record($this->record->id, $product_id, $single_url["key"]);
}
$products_to_add_to_group = [];

// products that are already available in the system.
foreach ($this->data['products_available'] as $available_products) {
foreach ($available_products['product_id'] as $available_product) {
$products_to_add_to_group[$available_products['key']][] = (int) $available_product;
}
}

// new products

// check if all stores links have the same currency as the current group
// otherwise stop the process

$count = 1;
$products_to_add_to_app = [];
foreach ($this->data['url_products'] as $new_product_url) {
$url = new \App\Helpers\URLHelper($new_product_url['url']);
// make sure the store url has the same currency as the group.
if ($url->store->currency_id != $this->data['currency_id']) {
Notification::make()
->title('store with different currency is not allowed')
->body("please remove product ({$count}) with link {$new_product_url['url']}")
->danger()
->send();
$this->halt();
}
$count++;

$products_to_add_to_app[] = [
'key' => $new_product_url['key'],
'product_key' => $url->product_unique_key,
'store_id' => $url->store->id,
];
}

}
return parent::handleRecordUpdate($record, $data); // TODO: Change the autogenerated stub
}
// check if that product is already available in database or not.
// if it is, then add it to the list
// if not, we create it and add it to the list

foreach ($products_to_add_to_app as $product_app) {

$product_store = ProductStore::where([
'store_id' => $product_app['store_id'],
'key' => $product_app['product_key'],
])->first();

if (! $product_store) {
$product = Product::create();

$product->product_stores()->create([
"key" => $product_app['product_key'],
"store_id" => $product_app['store_id'],
]);

}

$products_to_add_to_group[$product_app['key']][] = (int) ($product_store?->product_id ?? $product->id);

}

foreach ($products_to_add_to_group as $key => $products) {
// get unique values of the available products per key
$unique_products = array_unique($products);

foreach ($unique_products as $unique_product) {
DB::table('group_product')
->updateOrInsert([
'group_id' => $this->record->id,
'product_id' => $unique_product,
], [
'key' => $key,
]);
}
}
}
}
Loading

0 comments on commit 75c2a74

Please sign in to comment.