1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of FacturaScripts |
4
|
|
|
* Copyright (C) 2021 Carlos Garcia Gomez <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, either version 3 of the |
9
|
|
|
* License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Lesser General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
namespace FacturaScripts\Test\Core\Model; |
20
|
|
|
|
21
|
|
|
use FacturaScripts\Core\App\AppSettings; |
22
|
|
|
use FacturaScripts\Core\Model\Fabricante; |
23
|
|
|
use FacturaScripts\Core\Model\Familia; |
24
|
|
|
use FacturaScripts\Core\Model\Impuesto; |
25
|
|
|
use FacturaScripts\Core\Model\Producto; |
26
|
|
|
use FacturaScripts\Core\Model\ProductoProveedor; |
27
|
|
|
use FacturaScripts\Core\Model\Proveedor; |
28
|
|
|
use FacturaScripts\Core\Model\Stock; |
29
|
|
|
use FacturaScripts\Test\Core\LogErrorsTrait; |
30
|
|
|
use PHPUnit\Framework\TestCase; |
31
|
|
|
|
32
|
|
|
final class ProductoTest extends TestCase |
33
|
|
|
{ |
34
|
|
|
use LogErrorsTrait; |
35
|
|
|
|
36
|
|
|
/* |
37
|
|
|
* Test Almacen: crear y no poder borrar todos. |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
public function testBlocked() |
41
|
|
|
{ |
42
|
|
|
$product = $this->getTestProduct(); |
43
|
|
|
$product->secompra = true; |
44
|
|
|
$product->sevende = true; |
45
|
|
|
$product->publico = true; |
46
|
|
|
$product->bloqueado = true; |
47
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
48
|
|
|
$product->loadFromCode($product->primaryColumnValue()); |
49
|
|
|
$this->assertFalse($product->publico, 'product-blocked-can-public'); |
50
|
|
|
$this->assertFalse($product->secompra, 'product-blocked-can-buy'); |
51
|
|
|
$this->assertFalse($product->sevende, 'product-blocked-can-sale'); |
52
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testCreate() |
56
|
|
|
{ |
57
|
|
|
$product = $this->getTestProduct(); |
58
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
59
|
|
|
$this->assertNotNull($product->primaryColumnValue(), 'estado-product-not-stored'); |
60
|
|
|
$this->assertTrue($product->exists(), 'product-cant-persist'); |
61
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testCreateWithOutReference() |
65
|
|
|
{ |
66
|
|
|
$product = new Producto(); |
67
|
|
|
$product->descripcion = 'Test Product'; |
68
|
|
|
$this->assertFalse($product->save(), 'product-cant-save'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testFamily() |
72
|
|
|
{ |
73
|
|
|
$family = new Familia(); |
74
|
|
|
$family->descripcion = 'Test Family'; |
75
|
|
|
$this->assertTrue($family->save(), 'family-cant-save'); |
76
|
|
|
|
77
|
|
|
$product = $this->getTestProduct(); |
78
|
|
|
$product->codfamilia = $family->primaryColumnValue(); |
79
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
80
|
|
|
|
81
|
|
|
$this->assertTrue($family->delete(), 'product-cant-delete'); |
82
|
|
|
$product->loadFromCode($product->primaryColumnValue()); |
83
|
|
|
$this->assertTrue(empty($product->codfamilia), 'product-family-not-empty'); |
84
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testManufacturer() |
88
|
|
|
{ |
89
|
|
|
$manufacturer = new Fabricante(); |
90
|
|
|
$manufacturer->nombre = 'Test Manufacturer'; |
91
|
|
|
$this->assertTrue($manufacturer->save(), 'manufacturer-cant-save'); |
92
|
|
|
|
93
|
|
|
$product = $this->getTestProduct(); |
94
|
|
|
$product->codfabricante = $manufacturer->primaryColumnValue(); |
95
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
96
|
|
|
|
97
|
|
|
$this->assertTrue($manufacturer->delete(), 'manufacturer-cant-delete'); |
98
|
|
|
$product->loadFromCode($product->primaryColumnValue()); |
99
|
|
|
$this->assertTrue(empty($product->codfabricante), 'product-manufacturer-not-empty'); |
100
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testCostPricePolicy1() |
104
|
|
|
{ |
105
|
|
|
$settings = new AppSettings(); |
106
|
|
|
$settings->set('default', 'costpricepolicy', ''); |
107
|
|
|
|
108
|
|
|
$product = $this->getTestProduct(); |
109
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
110
|
|
|
|
111
|
|
|
$variant = $product->getVariants()[0]; |
112
|
|
|
$variant->coste = 100; |
113
|
|
|
$this->assertTrue($variant->save(), 'variant-cant-save'); |
114
|
|
|
|
115
|
|
|
$supplier = new Proveedor(); |
116
|
|
|
$supplier->codproveedor = 'Test'; |
117
|
|
|
$supplier->nombre = 'Test Supplier'; |
118
|
|
|
$supplier->cifnif = '12345678A'; |
119
|
|
|
$this->assertTrue($supplier->save(), 'supplier-cant-save'); |
120
|
|
|
|
121
|
|
|
$supplierProduct = new ProductoProveedor(); |
122
|
|
|
$supplierProduct->codproveedor = $supplier->codproveedor; |
123
|
|
|
$supplierProduct->referencia = $product->referencia; |
124
|
|
|
$supplierProduct->idproducto = $product->idproducto; |
125
|
|
|
$supplierProduct->precio = 200; |
126
|
|
|
$this->assertTrue($supplierProduct->save(), 'supplier-product-cant-save'); |
127
|
|
|
|
128
|
|
|
$variant->loadFromCode($variant->primaryColumnValue()); |
129
|
|
|
$this->assertTrue($variant->coste == 100, 'variant-cost-should-not-change'); |
130
|
|
|
|
131
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
132
|
|
|
$this->assertTrue($supplier->delete(), 'supplier-cant-delete'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testCostPricePolicy2() |
136
|
|
|
{ |
137
|
|
|
$settings = new AppSettings(); |
138
|
|
|
$settings->set('default', 'costpricepolicy', 'last-price'); |
139
|
|
|
|
140
|
|
|
$product = $this->getTestProduct(); |
141
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
142
|
|
|
|
143
|
|
|
$variant = $product->getVariants()[0]; |
144
|
|
|
$variant->coste = 50; |
145
|
|
|
$this->assertTrue($variant->save(), 'variant-cant-save'); |
146
|
|
|
|
147
|
|
|
$supplier1 = new Proveedor(); |
148
|
|
|
$supplier1->codproveedor = 'Test1'; |
149
|
|
|
$supplier1->nombre = 'Test Supplier 1'; |
150
|
|
|
$supplier1->cifnif = '12345678A'; |
151
|
|
|
$this->assertTrue($supplier1->save(), 'supplier-cant-save'); |
152
|
|
|
|
153
|
|
|
$supplierProduct1 = new ProductoProveedor(); |
154
|
|
|
$supplierProduct1->codproveedor = $supplier1->codproveedor; |
155
|
|
|
$supplierProduct1->referencia = $product->referencia; |
156
|
|
|
$supplierProduct1->idproducto = $product->idproducto; |
157
|
|
|
$supplierProduct1->precio = 100; |
158
|
|
|
$supplierProduct1->actualizado = date( |
159
|
|
|
ProductoProveedor::DATETIME_STYLE, |
160
|
|
|
strtotime("- 1 days") |
161
|
|
|
); |
162
|
|
|
$this->assertTrue($supplierProduct1->save(), 'supplier-product-cant-save'); |
163
|
|
|
|
164
|
|
|
$supplier2 = new Proveedor(); |
165
|
|
|
$supplier2->codproveedor = 'Test2'; |
166
|
|
|
$supplier2->nombre = 'Test Supplier 2'; |
167
|
|
|
$supplier2->cifnif = '12345678A'; |
168
|
|
|
$this->assertTrue($supplier2->save(), 'supplier-cant-save'); |
169
|
|
|
|
170
|
|
|
$supplierProduct2 = new ProductoProveedor(); |
171
|
|
|
$supplierProduct2->codproveedor = $supplier2->codproveedor; |
172
|
|
|
$supplierProduct2->referencia = $product->referencia; |
173
|
|
|
$supplierProduct2->idproducto = $product->idproducto; |
174
|
|
|
$supplierProduct2->precio = 200; |
175
|
|
|
$this->assertTrue($supplierProduct2->save(), 'supplier-product-cant-save'); |
176
|
|
|
|
177
|
|
|
$variant->loadFromCode($variant->primaryColumnValue()); |
178
|
|
|
$this->assertTrue($variant->coste == 200, 'variant-cost-not-last'); |
179
|
|
|
|
180
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
181
|
|
|
$this->assertTrue($supplier1->delete(), 'supplier-cant-delete'); |
182
|
|
|
$this->assertTrue($supplier2->delete(), 'supplier-cant-delete'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testCostPricePolicy3() |
186
|
|
|
{ |
187
|
|
|
$settings = new AppSettings(); |
188
|
|
|
$settings->set('default', 'costpricepolicy', 'average-price'); |
189
|
|
|
|
190
|
|
|
$product = $this->getTestProduct(); |
191
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
192
|
|
|
|
193
|
|
|
$variant = $product->getVariants()[0]; |
194
|
|
|
$variant->coste = 50; |
195
|
|
|
$this->assertTrue($variant->save(), 'variant-cant-save'); |
196
|
|
|
|
197
|
|
|
$supplier1 = new Proveedor(); |
198
|
|
|
$supplier1->codproveedor = 'Test1'; |
199
|
|
|
$supplier1->nombre = 'Test Supplier 1'; |
200
|
|
|
$supplier1->cifnif = '12345678A'; |
201
|
|
|
$this->assertTrue($supplier1->save(), 'supplier-cant-save'); |
202
|
|
|
|
203
|
|
|
$supplierProduct1 = new ProductoProveedor(); |
204
|
|
|
$supplierProduct1->codproveedor = $supplier1->codproveedor; |
205
|
|
|
$supplierProduct1->referencia = $product->referencia; |
206
|
|
|
$supplierProduct1->idproducto = $product->idproducto; |
207
|
|
|
$supplierProduct1->precio = 100; |
208
|
|
|
$this->assertTrue($supplierProduct1->save(), 'supplier-product-cant-save'); |
209
|
|
|
|
210
|
|
|
$supplier2 = new Proveedor(); |
211
|
|
|
$supplier2->codproveedor = 'Test2'; |
212
|
|
|
$supplier2->nombre = 'Test Supplier 2'; |
213
|
|
|
$supplier2->cifnif = '12345678A'; |
214
|
|
|
$this->assertTrue($supplier2->save(), 'supplier-cant-save'); |
215
|
|
|
|
216
|
|
|
$supplierProduct2 = new ProductoProveedor(); |
217
|
|
|
$supplierProduct2->codproveedor = $supplier2->codproveedor; |
218
|
|
|
$supplierProduct2->referencia = $product->referencia; |
219
|
|
|
$supplierProduct2->idproducto = $product->idproducto; |
220
|
|
|
$supplierProduct2->precio = 200; |
221
|
|
|
$this->assertTrue($supplierProduct2->save(), 'supplier-product-cant-save'); |
222
|
|
|
|
223
|
|
|
$variant->loadFromCode($variant->primaryColumnValue()); |
224
|
|
|
$this->assertTrue($variant->coste == 150, 'variant-cost-not-average'); |
225
|
|
|
|
226
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
227
|
|
|
$this->assertTrue($supplier1->delete(), 'supplier-cant-delete'); |
228
|
|
|
$this->assertTrue($supplier2->delete(), 'supplier-cant-delete'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function testStock() |
232
|
|
|
{ |
233
|
|
|
$product = $this->getTestProduct(); |
234
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
235
|
|
|
|
236
|
|
|
$stock = new Stock(); |
237
|
|
|
$stock->idproducto = $product->idproducto; |
238
|
|
|
$stock->referencia = $product->referencia; |
239
|
|
|
$stock->codalmacen = AppSettings::get('default', 'codalmacen'); |
240
|
|
|
$stock->cantidad = 99; |
241
|
|
|
$this->assertTrue($stock->save(), 'stock-cant-save'); |
242
|
|
|
|
243
|
|
|
$product->loadFromCode($product->primaryColumnValue()); |
244
|
|
|
$variant = $product->getVariants()[0]; |
245
|
|
|
$this->assertTrue($product->stockfis == $stock->cantidad, 'product-different-stock'); |
246
|
|
|
$this->assertTrue($variant->stockfis == $stock->cantidad, 'variant-different-stock'); |
247
|
|
|
|
248
|
|
|
$product->nostock = true; |
249
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
250
|
|
|
$product->loadFromCode($product->primaryColumnValue()); |
251
|
|
|
$variant->loadFromCode($variant->primaryColumnValue()); |
252
|
|
|
$this->assertTrue($product->stockfis == 0, 'product-cant-stock'); |
253
|
|
|
$this->assertTrue($variant->stockfis == 0, 'variant-cant-stock'); |
254
|
|
|
$this->assertFalse($stock->loadFromCode($stock->primaryColumnValue()), 'stock-cant-load-after-delete'); |
255
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testTax() |
259
|
|
|
{ |
260
|
|
|
$tax = new Impuesto(); |
261
|
|
|
$tax->descripcion = 'Test Tax'; |
262
|
|
|
$tax->iva = 99; |
263
|
|
|
$this->assertTrue($tax->save(), 'tax-cant-save'); |
264
|
|
|
|
265
|
|
|
$product = $this->getTestProduct(); |
266
|
|
|
$product->codimpuesto = $tax->primaryColumnValue(); |
267
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
268
|
|
|
|
269
|
|
|
$this->assertTrue($tax->delete(), 'tax-cant-delete'); |
270
|
|
|
$product->loadFromCode($product->primaryColumnValue()); |
271
|
|
|
$this->assertTrue(empty($product->codimpuesto), 'product-tax-not-empty'); |
272
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function testVariante() |
276
|
|
|
{ |
277
|
|
|
$product = $this->getTestProduct(); |
278
|
|
|
$product->precio = 10; |
279
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
280
|
|
|
|
281
|
|
|
$variants = $product->getVariants(); |
282
|
|
|
$this->assertEquals(1, count($variants), 'product-more-than-one-variant'); |
283
|
|
|
|
284
|
|
|
$variant = $variants[0]; |
285
|
|
|
$this->assertTrue($variant->referencia == $product->referencia, 'product-variant-diferent-reference'); |
286
|
|
|
$this->assertTrue($variant->precio == $product->precio, 'product-variant-diferent-price'); |
287
|
|
|
|
288
|
|
|
$product->price = 50; |
|
|
|
|
289
|
|
|
$product->save(); |
290
|
|
|
$variant->loadFromCode($variant->primaryColumnValue()); |
291
|
|
|
$this->assertTrue($variant->precio == $product->precio, 'product-variant-diferent-price'); |
292
|
|
|
|
293
|
|
|
$variant->coste = 100; |
294
|
|
|
$variant->margen = 20; |
295
|
|
|
$this->assertTrue($variant->save(), 'variant-cant-save'); |
296
|
|
|
$variant->loadFromCode($variant->primaryColumnValue()); |
297
|
|
|
$this->assertTrue($variant->precio == 120, 'variant-difference-price'); |
298
|
|
|
|
299
|
|
|
$this->assertFalse($variant->delete(), 'variant-cant-delete'); |
300
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
301
|
|
|
$this->assertEquals(0, count($product->getVariants()), 'variant-must-be-delete'); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/* Para una variante, |
305
|
|
|
* si tiene precio de coste y se aplica un margen (modificas el margen) se tiene que recalcular el precio de venta */ |
306
|
|
|
|
307
|
|
|
|
308
|
|
|
protected function tearDown() |
309
|
|
|
{ |
310
|
|
|
$this->logErrors(); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
private function getTestProduct() |
314
|
|
|
{ |
315
|
|
|
$product = new Producto(); |
316
|
|
|
$product->referencia = 'Test'; |
317
|
|
|
$product->descripcion = 'Test Product'; |
318
|
|
|
return $product; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|