Skip to content

Commit 41a4d77

Browse files
author
Carlos Garcia
committed
Modificados lod modelos relacionados con Producto para usar las nuevas clases.
1 parent 20c3d09 commit 41a4d77

File tree

7 files changed

+119
-66
lines changed

7 files changed

+119
-66
lines changed

Core/Lib/CostPriceTools.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2020-2023 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2020-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -37,6 +37,10 @@ class CostPriceTools
3737
{
3838
public static function update(Variante $variant): void
3939
{
40+
if (empty($variant->id())) {
41+
return;
42+
}
43+
4044
$policy = Tools::settings('default', 'costpricepolicy');
4145

4246
switch ($policy) {

Core/Model/Base/ModelOnChangeClass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
/**
2323
* ModelOnChangeClass is a class with methods to model the changes of values in the properties of the model.
2424
*
25+
* @deprecated Use FacturaScripts\Core\Template\ModelClass instead
2526
* @author Carlos Garcia Gomez <carlos@facturascripts.com>
2627
*/
2728
abstract class ModelOnChangeClass extends ModelClass

Core/Model/Producto.php

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2012-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2012-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -20,12 +20,10 @@
2020
namespace FacturaScripts\Core\Model;
2121

2222
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
23-
use FacturaScripts\Core\Model\Base\ModelClass;
24-
use FacturaScripts\Core\Model\Base\ModelTrait;
2523
use FacturaScripts\Core\Model\Base\TaxRelationTrait;
24+
use FacturaScripts\Core\Template\ModelClass;
25+
use FacturaScripts\Core\Template\ModelTrait;
2626
use FacturaScripts\Core\Tools;
27-
use FacturaScripts\Dinamic\Model\Fabricante as DinFabricante;
28-
use FacturaScripts\Dinamic\Model\Familia as DinFamilia;
2927
use FacturaScripts\Dinamic\Model\ProductoImagen as DinProductoImagen;
3028
use FacturaScripts\Dinamic\Model\Variante as DinVariante;
3129

@@ -181,16 +179,20 @@ class Producto extends ModelClass
181179
*/
182180
public $ventasinstock;
183181

184-
public function __get($name)
182+
public function __get($key)
185183
{
186-
if ($name === 'precio_iva') {
184+
if (isset($this->attributes[$key])) {
185+
return $this->attributes[$key];
186+
}
187+
188+
if ($key === 'precio_iva') {
187189
return $this->priceWithTax();
188190
}
189191

190192
return null;
191193
}
192194

193-
public function clear()
195+
public function clear(): void
194196
{
195197
parent::clear();
196198
$this->actualizado = Tools::dateTime();
@@ -227,26 +229,21 @@ public function delete(): bool
227229
return parent::delete();
228230
}
229231

230-
public function getFabricante(): Fabricante
232+
public function getFabricante(): ?Fabricante
231233
{
232-
$fabricante = new DinFabricante();
233-
$fabricante->loadFromCode($this->codfabricante);
234-
return $fabricante;
234+
return $this->belongsTo(Fabricante::class, 'codfabricante');
235235
}
236236

237-
public function getFamilia(): Familia
237+
public function getFamilia(): ?Familia
238238
{
239-
$familia = new DinFamilia();
240-
$familia->loadFromCode($this->codfamilia);
241-
return $familia;
239+
return $this->belongsTo(Familia::class, 'codfamilia');
242240
}
243241

244242
/**
245243
* @return ProductoImagen[]
246244
*/
247245
public function getImages(bool $imgVariant = true): array
248246
{
249-
$image = new DinProductoImagen();
250247
$where = [new DataBaseWhere('idproducto', $this->idproducto)];
251248

252249
// solo si queremos lás imágenes del producto y no de las variantes
@@ -255,17 +252,15 @@ public function getImages(bool $imgVariant = true): array
255252
}
256253

257254
$orderBy = ['orden' => 'ASC'];
258-
return $image->all($where, $orderBy, 0, 0);
255+
return DinProductoImagen::all($where, $orderBy, 0, 0);
259256
}
260257

261258
/**
262259
* @return Variante[]
263260
*/
264261
public function getVariants(): array
265262
{
266-
$variantModel = new DinVariante();
267-
$where = [new DataBaseWhere('idproducto', $this->idproducto)];
268-
return $variantModel->all($where, [], 0, 0);
263+
return $this->hasMany(Variante::class, 'idproducto');
269264
}
270265

271266
public function install(): string
@@ -295,7 +290,7 @@ public function primaryDescriptionColumn(): string
295290
return 'referencia';
296291
}
297292

298-
public function setPriceWithTax(float $price)
293+
public function setPriceWithTax(float $price): bool
299294
{
300295
$newPrice = (100 * $price) / (100 + $this->getTax()->iva);
301296
foreach ($this->getVariants() as $variant) {
@@ -306,6 +301,8 @@ public function setPriceWithTax(float $price)
306301
}
307302

308303
$this->precio = round($newPrice, self::ROUND_DECIMALS);
304+
305+
return true;
309306
}
310307

311308
public static function tableName(): string
@@ -366,7 +363,7 @@ public function test(): bool
366363
/**
367364
* Updated product price or reference if any change in variants.
368365
*/
369-
public function update(): void
366+
public function updateInfo(): void
370367
{
371368
$newPrecio = 0.0;
372369
$newReferencia = null;
@@ -393,7 +390,7 @@ public function update(): void
393390
}
394391
}
395392

396-
protected function saveInsert(array $values = []): bool
393+
protected function saveInsert(): bool
397394
{
398395
// comprobamos si la referencia ya existe
399396
$where = [new DataBaseWhere('referencia', $this->referencia)];
@@ -402,7 +399,7 @@ protected function saveInsert(array $values = []): bool
402399
return false;
403400
}
404401

405-
if (false === parent::saveInsert($values)) {
402+
if (false === parent::saveInsert()) {
406403
return false;
407404
}
408405

@@ -411,11 +408,11 @@ protected function saveInsert(array $values = []): bool
411408
$variant->precio = $this->precio;
412409
$variant->referencia = $this->referencia;
413410
$variant->stockfis = $this->stockfis;
414-
if ($variant->save()) {
415-
return true;
411+
if (false === $variant->save()) {
412+
$this->delete();
413+
return false;
416414
}
417415

418-
$this->delete();
419-
return false;
416+
return true;
420417
}
421418
}

Core/Model/ProductoImagen.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2012-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2012-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -20,6 +20,8 @@
2020
namespace FacturaScripts\Core\Model;
2121

2222
use FacturaScripts\Core\Lib\MyFilesToken;
23+
use FacturaScripts\Core\Template\ModelClass;
24+
use FacturaScripts\Core\Template\ModelTrait;
2325
use FacturaScripts\Core\Tools;
2426
use FacturaScripts\Core\UploadedFile;
2527
use FacturaScripts\Dinamic\Model\AttachedFile as DinAttachedFile;
@@ -32,9 +34,9 @@
3234
* @author Carlos García Gómez <carlos@facturascripts.com>
3335
* @author José Antonio Cuello Principal <yopli2000@gmail.com>
3436
*/
35-
class ProductoImagen extends Base\ModelClass
37+
class ProductoImagen extends ModelClass
3638
{
37-
use Base\ModelTrait;
39+
use ModelTrait;
3840

3941
const THUMBNAIL_PATH = '/MyFiles/Tmp/Thumbnails/';
4042

@@ -184,11 +186,6 @@ public function install(): string
184186
return parent::install();
185187
}
186188

187-
public static function primaryColumn(): string
188-
{
189-
return 'id';
190-
}
191-
192189
public static function tableName(): string
193190
{
194191
return 'productos_imagenes';
@@ -226,6 +223,7 @@ protected function getThumbnailPath(string $path, bool $token, bool $parmaToken)
226223
} elseif ($token && $parmaToken) {
227224
return $path . '?myft=' . MyFilesToken::get($path, true);
228225
}
226+
229227
return $path;
230228
}
231229
}

Core/Model/ProductoProveedor.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* This file is part of FacturaScripts
4-
* Copyright (C) 2020-2023 Carlos Garcia Gomez <carlos@facturascripts.com>
4+
* Copyright (C) 2020-2025 Carlos Garcia Gomez <carlos@facturascripts.com>
55
*
66
* This program is free software: you can redistribute it and/or modify
77
* it under the terms of the GNU Lesser General Public License as
@@ -21,9 +21,9 @@
2121

2222
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
2323
use FacturaScripts\Core\DataSrc\Divisas;
24-
use FacturaScripts\Core\Model\Base\ModelOnChangeClass;
25-
use FacturaScripts\Core\Model\Base\ModelTrait;
2624
use FacturaScripts\Core\Model\Base\ProductRelationTrait;
25+
use FacturaScripts\Core\Template\ModelClass;
26+
use FacturaScripts\Core\Template\ModelTrait;
2727
use FacturaScripts\Core\Tools;
2828
use FacturaScripts\Dinamic\Lib\CostPriceTools;
2929
use FacturaScripts\Dinamic\Model\Divisa as DinDivisa;
@@ -36,7 +36,7 @@
3636
*
3737
* @author Carlos Garcia Gomez <carlos@facturascripts.com>
3838
*/
39-
class ProductoProveedor extends ModelOnChangeClass
39+
class ProductoProveedor extends ModelClass
4040
{
4141
use ModelTrait;
4242
use ProductRelationTrait;
@@ -77,14 +77,20 @@ class ProductoProveedor extends ModelOnChangeClass
7777
/** @var float */
7878
public $stock;
7979

80-
public function __get($name)
80+
public function __get($key)
8181
{
82-
if ($name == 'descripcion') {
82+
if (isset($this->attributes[$key])) {
83+
return $this->attributes[$key];
84+
}
85+
86+
if ($key == 'descripcion') {
8387
return $this->getVariant()->getProducto()->descripcion;
8488
}
89+
90+
return null;
8591
}
8692

87-
public function clear()
93+
public function clear(): void
8894
{
8995
parent::clear();
9096
$this->actualizado = Tools::dateTime();
@@ -136,11 +142,6 @@ public function install(): string
136142
return parent::install();
137143
}
138144

139-
public static function primaryColumn(): string
140-
{
141-
return 'id';
142-
}
143-
144145
public static function tableName(): string
145146
{
146147
return 'productosprov';
@@ -181,34 +182,32 @@ public function url(string $type = 'auto', string $list = 'List'): string
181182
/**
182183
* This method is called after a record is deleted on the database (delete).
183184
*/
184-
protected function onDelete()
185+
protected function onDelete(): void
185186
{
186187
CostPriceTools::update($this->getVariant());
188+
187189
parent::onDelete();
188190
}
189191

190192
/**
191193
* This method is called after a new record is saved on the database (saveInsert).
192194
*/
193-
protected function onInsert()
195+
protected function onInsert(): void
194196
{
195197
CostPriceTools::update($this->getVariant());
198+
196199
parent::onInsert();
197200
}
198201

199202
/**
200203
* This method is called after a record is updated on the database (saveUpdate).
201204
*/
202-
protected function onUpdate()
205+
protected function onUpdate(): void
203206
{
204-
if ($this->previousData['neto'] !== $this->neto) {
207+
if ($this->isDirty('neto')) {
205208
CostPriceTools::update($this->getVariant());
206209
}
207-
parent::onUpdate();
208-
}
209210

210-
protected function setPreviousData(array $fields = [])
211-
{
212-
parent::setPreviousData(array_merge(['neto'], $fields));
211+
parent::onUpdate();
213212
}
214213
}

0 commit comments

Comments
 (0)