Add a badge for stock

This commit is contained in:
Krzysztof Płaczek
2024-11-23 11:34:00 +01:00
parent 0db4bacff4
commit a6b108cb74
5 changed files with 35 additions and 14 deletions

View File

@@ -1,7 +1,6 @@
{ {
"require": { "require": {
"guzzlehttp/guzzle": "^7.0", "guzzlehttp/guzzle": "^7.0",
"symfony/var-dumper": "^7.0",
"illuminate/database": "11.26.0.0", "illuminate/database": "11.26.0.0",
"ext-json": "*", "ext-json": "*",
"symfony/console": "^7.0", "symfony/console": "^7.0",
@@ -18,5 +17,8 @@
"psr-4": { "psr-4": {
"Krzysiej\\RyobiCrawler\\": "src/" "Krzysiej\\RyobiCrawler\\": "src/"
} }
},
"require-dev": {
"symfony/var-dumper": "^7.1"
} }
} }

14
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "4eef5feb5e2635d7fb7c69c8ed01a84c", "content-hash": "a6920081bf40809334b8dda2da16752d",
"packages": [ "packages": [
{ {
"name": "brick/math", "name": "brick/math",
@@ -4123,16 +4123,16 @@
}, },
{ {
"name": "symfony/var-dumper", "name": "symfony/var-dumper",
"version": "v7.1.5", "version": "v7.1.8",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/var-dumper.git", "url": "https://github.com/symfony/var-dumper.git",
"reference": "e20e03889539fd4e4211e14d2179226c513c010d" "reference": "7bb01a47b1b00428d32b5e7b4d3b2d1aa58d3db8"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/var-dumper/zipball/e20e03889539fd4e4211e14d2179226c513c010d", "url": "https://api.github.com/repos/symfony/var-dumper/zipball/7bb01a47b1b00428d32b5e7b4d3b2d1aa58d3db8",
"reference": "e20e03889539fd4e4211e14d2179226c513c010d", "reference": "7bb01a47b1b00428d32b5e7b4d3b2d1aa58d3db8",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -4186,7 +4186,7 @@
"dump" "dump"
], ],
"support": { "support": {
"source": "https://github.com/symfony/var-dumper/tree/v7.1.5" "source": "https://github.com/symfony/var-dumper/tree/v7.1.8"
}, },
"funding": [ "funding": [
{ {
@@ -4202,7 +4202,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2024-09-16T10:07:02+00:00" "time": "2024-11-08T15:46:42+00:00"
}, },
{ {
"name": "symfony/var-exporter", "name": "symfony/var-exporter",

View File

@@ -11,7 +11,10 @@ final class IndexController extends BaseController
#[Route('/', name: 'app_home')] #[Route('/', name: 'app_home')]
public function __invoke(): Response public function __invoke(): Response
{ {
$products = Product::with('price')->orderByDesc('starred')->orderByDesc('created_by')->get(); $products = Product::with(['currentStock', 'price'])
->orderByDesc('starred')
->orderByDesc('created_by')
->get();
return $this->render('productList.html.twig', ['products' => $products]); return $this->render('productList.html.twig', ['products' => $products]);
} }
} }

View File

@@ -44,6 +44,13 @@ class Product extends Model
return $this->hasMany(Stock::class); return $this->hasMany(Stock::class);
} }
public function currentStock(): HasOne
{
return $this->stock()->one()->ofMany()->withDefault(function (Stock $stock) {
$stock->stock = 0;
});
}
public function toggleStarred(): self public function toggleStarred(): self
{ {
$this->starred = !$this->starred; $this->starred = !$this->starred;

View File

@@ -7,7 +7,6 @@
<th></th> <th></th>
<th></th> <th></th>
<th>Name</th> <th>Name</th>
<th class="d-none d-md-table-cell">Code</th>
<th>Categories</th> <th>Categories</th>
<th></th> <th></th>
<th>Price</th> <th>Price</th>
@@ -16,10 +15,18 @@
</thead> </thead>
{% for product in products %} {% for product in products %}
<tr> <tr>
<td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none" href="{{ path('app_star', {'productId': product.id}) }}">{% if product.starred == true %}{% else %}{% endif %}</a></td> <td class="align-middle font-weight-bold h3"><a class="text-warning text-decoration-none"
href="{{ path('app_star', {'productId': product.id}) }}">{% if product.starred == true %}{% else %}{% endif %}</a></td>
<td class="align-middle" style="width: 120px;"><img src='{{ product.image }}&width=70' class='border rounded p-1' alt='{{ product.name }}'/></td> <td class="align-middle" style="width: 120px;"><img src='{{ product.image }}&width=70' class='border rounded p-1' alt='{{ product.name }}'/></td>
<td class="align-middle"><a href='{{ path('app_product', {'productId': product.id}) }}' class="text-decoration-none">{{ product.name }}</a></td> <td class="align-middle">
<td class="align-middle d-none d-md-table-cell">{{ product.subTitle }}</td> <a href='{{ path('app_product', {'productId': product.id}) }}' class="text-decoration-none">{{ product.name }}</a>
{% if product.currentStock.stock > 0 %}
<span class="badge text-bg-light">stock: {{ product.currentStock.stock }}</span>
{% else %}
<span class="badge text-bg-warning">out of stock</span>
{% endif %}
<span class="badge text-bg-light">{{ product.subTitle }}</span>
</td>
<td class="align-middle"> <td class="align-middle">
<ul class='nav'> <ul class='nav'>
{% for category in product.categories %} {% for category in product.categories %}
@@ -29,7 +36,9 @@
</td> </td>
<td class="align-middle"><a href='https://pl.ryobitools.eu/{{ product.url }}'>link</a></td> <td class="align-middle"><a href='https://pl.ryobitools.eu/{{ product.url }}'>link</a></td>
<td class="align-middle">{{ product.price.last.price | format_currency('PLN', {}, 'pl') }}</td> <td class="align-middle">{{ product.price.last.price | format_currency('PLN', {}, 'pl') }}</td>
<td class="align-middle">{% if product.price.last.price != product.price.last.productStandardPrice %}<span class="badge text-bg-warning text-decoration-line-through">{{ product.price.last.productStandardPrice | format_currency('PLN', {}, 'pl') }}</span>&nbsp;<span class="badge text-bg-success">{{ ((1 - product.price.last.price / product.price.last.productStandardPrice)*100)|number_format(0) }}%</span>{% else %}{% endif %}</td> <td class="align-middle">{% if product.price.last.price != product.price.last.productStandardPrice %}<span
class="badge text-bg-warning text-decoration-line-through">{{ product.price.last.productStandardPrice | format_currency('PLN', {}, 'pl') }}</span>&nbsp;<span
class="badge text-bg-success">{{ ((1 - product.price.last.price / product.price.last.productStandardPrice)*100)|number_format(0) }}%</span>{% else %}{% endif %}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>