Files
YNWA Fawzy f037a936f3 Fixes:
- fix type when filtering product by price met target.
- fix the lowest price was overridden if price wasn't crawled.
- fix store tabs not working
- fix some european amazon using "." as thousands separator.
- keep last 3 days of logs only.
- added volume to .env to fix 2FA not working after restarting container
- added volume for database
-

New Stores:
- best buy

New Features:

- add other users to the system and manage their tokens.
- add telegram notification.
- add percentage drop.
- robot detection for amazon ( if the system was detected as robot, it will stop executing and try the next product, the same product that was blocked will be crawled again in the next batch)

Took 6 hours 50 minutes
2024-09-29 18:31:13 +04:00

289 lines
11 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Enums\StatusEnum;
use App\Filament\Resources\ProductResource\Pages\CreateProduct;
use App\Filament\Resources\ProductResource\Pages\EditProduct;
use App\Filament\Resources\ProductResource\Pages\ListProducts;
use App\Filament\Resources\ProductResource\Pages\ViewProduct;
use App\Filament\Resources\ProductResource\RelationManagers;
use App\Helpers\CurrencyHelper;
use App\Helpers\ProductHelper;
use App\Helpers\StoreHelper;
use App\Helpers\URLHelper;
use App\Models\Product;
use Archilex\ToggleIconColumn\Columns\ToggleIconColumn;
use Filament\Forms;
use Filament\Forms\Components\DatePicker;
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\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
class ProductResource extends Resource
{
protected static ?string $model = Product::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
protected static ?int $navigationSort=1;
protected static ?string $recordTitleAttribute="name";
protected static bool $isGloballySearchable=true;
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->nullable()
->hiddenOn(CreateProduct::class),
TextInput::make('url')
->required(fn($operation)=> $operation=="create")
->autofocus(fn($operation)=> $operation=="create")
->url()
->label('URL of product')
->live(onBlur: true )
->afterStateUpdated(function ($state){
if($state){
$url=new URLHelper($state);
if ($url->store )
StoreHelper::is_unique($url);
}
}),
Select::make('status')
->options(StatusEnum::class)
->default(StatusEnum::Published)
->preload()
->native(false),
TextInput::make('notify_price')
->nullable()
->numeric(),
TextInput::make('notify_percentage')
->nullable()
->hintIcon("heroicon-o-information-circle", "Get notified when price drops below specified percentage")
->suffix('%')
->numeric(),
Select::make('categories')
->relationship('categories','name')
->createOptionForm([Forms\Components\TextInput::make('name')->required()])
->multiple()
->nullable()
->preload(),
DatePicker::make('snoozed_until')
->label("Snooze Notification Until"),
Section::make("Cross Stores Notification Settings")
->columns(4)
->schema([
Forms\Components\Toggle::make('only_official')
->label("Official Sellers Only")
->inline(false),
Forms\Components\Toggle::make('favourite')
->label("Add To Favourite")
->inline(false),
Forms\Components\Toggle::make('stock')
->label("Alert When Stock Available")
->inline(false),
Forms\Components\TextInput::make('lowest_within')
->label("Alert if Product lowest within")
->nullable()
->suffix('days')
->maxValue(65535),
TextInput::make('max_notifications')
->label("Max Notification Sent Daily")
->integer()
->minValue(0)
->numeric()
->placeholder("unlimited")
->hintIcon("heroicon-o-information-circle",
"this is for products that fluctuate in price, it won't send any more notification UNLESS the price is less than earlier"),
])
->collapsible(),
Section::make("Variants")
->hiddenOn(["view", "edit"])
->columns(4)
->schema([
Forms\Components\Toggle::make('variations')
->label("choose other variations to include")
->inline(false)
->reactive()
->afterStateUpdated(function ($component, $get , $state){
if (!$get('url')){
Notification::make()
->danger()
->title("URL Field is empty")
->send();
return ;
}
if ($state ){
$url=new URLHelper($get('url'));
$final_class_name="App\Helpers\StoresAvailable\\" . Str::ucfirst( explode(".",$url->store->domain)[0]);
$variations = call_user_func($final_class_name . '::get_variations' , $url->final_url );
$component->getContainer()
->getComponent('variation_options')
->options($variations)
->disabled(false)
->multiple()
->native(false);
}
}),
Select::make('variation_options')
->key('variation_options')
->preload()
->disabled()
->placeholder("choose variation")
]),
]);
}
public static function table(Table $table): Table
{
$stores=StoreHelper::get_stores_with_active_products();
$currencies=CurrencyHelper::get_currencies();
return $table
->modifyQueryUsing(function ($query){
$query->with([
"product_stores:id,product_id,store_id,price,notify_price,updated_at,highest_price,lowest_price",
]);
})
->columns([
ImageColumn::make('image')->alignCenter(),
TextColumn::make('name')
->words(5)
->limit(50)
->searchable()
->sortable(),
TextColumn::make('status')
->badge()
->color(fn ($state) => StatusEnum::get_badge($state)),
TextColumn::make('product_stores.store_id')
->formatStateUsing(function ($state) use($stores){
return $stores[$state]["name"];
})
->listWithLineBreaks(),
TextColumn::make('product_stores.price')
->formatStateUsing(function ($record) use ($currencies, $stores) {
return ProductHelper::prepare_multiple_prices_in_table($record, $currencies, $stores);
})->label('Prices'),
TextColumn::make('product_stores.notify_price')
->formatStateUsing(function ($record) use ($currencies, $stores) {
return ProductHelper::prepare_multiple_notify_prices_in_table($record, $currencies, $stores);
})->label('Notify at'),
TextColumn::make('product_stores.highest_price')
->label("Highest Price")
->listWithLineBreaks()
->color('danger'),
TextColumn::make('product_stores.lowest_price')
->label("Lowest Price")
->listWithLineBreaks()
->color('success'),
ToggleIconColumn::make('favourite')
->onIcon("heroicon-s-star")
->offIcon("heroicon-o-star"),
TextColumn::make('product_stores.updated_at')
->listWithLineBreaks()
->label('Last Update'),
])
->defaultSort('favourite' , 'desc')
->filters([
SelectFilter::make('status')
->options(StatusEnum::class)
->preload()
->multiple(),
Filter::make('notify_price')->query(function ($query){
$query->whereHas(
'product_stores',function($query){
$query->whereRaw('product_store.price <= product_store.notify_price');
}
);})
->label('Price Met Target')
->toggle(),
Filter::make('favourite')->query(function (Builder $query) {
$query->where('favourite', 1);
})
->label('Favourite product')
->toggle(),
Filter::make('highest_price')->query(function (Builder $query) {
return $query->whereHas('product_stores', function ($query){
$query->whereColumn('price' , '<=' , 'highest_price');
});
})
->label('Lower Than Highest Price')
->toggle(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function getRelations(): array
{
return [
RelationManagers\StoresRelationManager::class
];
}
public static function getPages(): array
{
return [
'index' => ListProducts::route('/'),
'create' => CreateProduct::route('/create'),
'edit' => EditProduct::route('/{record}/edit'),
'view' =>ViewProduct::route('/{record}'),
];
}
}