1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of FacturaScripts |
4
|
|
|
* Copyright (C) 2017-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
|
|
|
|
20
|
|
|
namespace FacturaScripts\Test\Core\Model; |
21
|
|
|
|
22
|
|
|
use FacturaScripts\Core\App\AppSettings; |
23
|
|
|
use FacturaScripts\Core\Base\DataBase\DataBaseWhere; |
24
|
|
|
use FacturaScripts\Core\Model\Almacen; |
25
|
|
|
use FacturaScripts\Core\Model\Producto; |
26
|
|
|
use FacturaScripts\Core\Model\Stock; |
27
|
|
|
use FacturaScripts\Core\Model\Variante; |
28
|
|
|
use FacturaScripts\Test\Core\LogErrorsTrait; |
29
|
|
|
use PHPUnit\Framework\TestCase; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
final class StockTest extends TestCase |
33
|
|
|
{ |
34
|
|
|
use LogErrorsTrait; |
35
|
|
|
|
36
|
|
|
public function testCreate() |
37
|
|
|
{ |
38
|
|
|
$product = new Producto(); |
39
|
|
|
$product->referencia = 'Test'; |
40
|
|
|
$product->descripcion = 'Test Product'; |
41
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
42
|
|
|
|
43
|
|
|
$stock = new Stock(); |
44
|
|
|
$stock->codalmacen = AppSettings::get('default', 'codalmacen'); |
45
|
|
|
$stock->idproducto = $product->idproducto; |
46
|
|
|
$stock->referencia = $product->referencia; |
47
|
|
|
|
48
|
|
|
// control sobre la creación |
49
|
|
|
$this->assertTrue($stock->save(), 'stock-cant-save'); |
50
|
|
|
$this->assertNotNull($stock->primaryColumnValue(), 'stock-not-stored'); |
51
|
|
|
$this->assertTrue($stock->exists(), 'stock-cant-persist'); |
52
|
|
|
|
53
|
|
|
// control sobre el borrado |
54
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
55
|
|
|
$this->assertFalse($stock->loadFromCode($stock->idstock), 'stock-not-removed-when-delete-product'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testCreateWithoutProduct() |
59
|
|
|
{ |
60
|
|
|
$stock = new Stock(); |
61
|
|
|
$stock->codalmacen = AppSettings::get('default', 'codalmacen'); |
62
|
|
|
$this->assertFalse($stock->save(), 'stock-cant-save-without-product'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testProductStock() |
66
|
|
|
{ |
67
|
|
|
$product = new Producto(); |
68
|
|
|
$product->referencia = 'Test'; |
69
|
|
|
$product->descripcion = 'Test Product'; |
70
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
71
|
|
|
|
72
|
|
|
$stock = new Stock(); |
73
|
|
|
$stock->codalmacen = AppSettings::get('default', 'codalmacen'); |
74
|
|
|
$stock->idproducto = $product->idproducto; |
75
|
|
|
$stock->referencia = $product->referencia; |
76
|
|
|
$stock->cantidad = 10; |
77
|
|
|
|
78
|
|
|
// control sobre la creación y stock |
79
|
|
|
$this->assertTrue($stock->save(), 'stock-cant-save'); |
80
|
|
|
|
81
|
|
|
// refrescamos producto y su stock |
82
|
|
|
$product->loadFromCode($product->idproducto); |
83
|
|
|
$this->assertTrue($product->stockfis !== 10, 'stock-not-update'); |
84
|
|
|
|
85
|
|
|
$this->assertTrue($stock->delete(), 'stock-cant-delete'); |
86
|
|
|
$product->loadFromCode($product->idproducto); |
87
|
|
|
$this->assertTrue($product->stockfis !== 0, 'stock-not-update'); |
88
|
|
|
|
89
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testProductAvailableStock() |
93
|
|
|
{ |
94
|
|
|
$product = new Producto(); |
95
|
|
|
$product->referencia = 'Test'; |
96
|
|
|
$product->descripcion = 'Test Product'; |
97
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
98
|
|
|
|
99
|
|
|
$stock = new Stock(); |
100
|
|
|
$stock->codalmacen = AppSettings::get('default', 'codalmacen'); |
101
|
|
|
$stock->idproducto = $product->idproducto; |
102
|
|
|
$stock->referencia = $product->referencia; |
103
|
|
|
$stock->cantidad = 10; |
104
|
|
|
$stock->disponible = 10; |
105
|
|
|
$stock->reservada = 5; |
106
|
|
|
|
107
|
|
|
// control sobre available stock |
108
|
|
|
$this->assertTrue($stock->save(), 'stock-cant-save'); |
109
|
|
|
$stock->loadFromCode($stock->idstock); |
110
|
|
|
$this->assertTrue($stock->disponible < $stock->cantidad, 'stock-available-wrong'); |
111
|
|
|
|
112
|
|
|
$this->assertTrue($stock->delete(), 'stock-cant-delete'); |
113
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testVariantStock() |
117
|
|
|
{ |
118
|
|
|
$product = new Producto(); |
119
|
|
|
$product->referencia = 'Test'; |
120
|
|
|
$product->descripcion = 'Test Product'; |
121
|
|
|
$this->assertTrue($product->save(), 'product-cant-save'); |
122
|
|
|
|
123
|
|
|
$warehouse1 = new Almacen(); |
124
|
|
|
$warehouse1->nombre = 'Warehouse test B'; |
125
|
|
|
$this->assertTrue($warehouse1->save(), 'warehouse-cant-save'); |
126
|
|
|
|
127
|
|
|
$warehouse2 = new Almacen(); |
128
|
|
|
$warehouse2->nombre = 'Warehouse test B'; |
129
|
|
|
$this->assertTrue($warehouse2->save(), 'warehouse-cant-save'); |
130
|
|
|
|
131
|
|
|
$stock1 = new Stock(); |
132
|
|
|
$stock1->codalmacen = $warehouse1->codalmacen; |
133
|
|
|
$stock1->idproducto = $product->idproducto; |
134
|
|
|
$stock1->referencia = $product->referencia; |
135
|
|
|
$stock1->cantidad = 10; |
136
|
|
|
$this->assertTrue($stock1->save(), 'stock-cant-save'); |
137
|
|
|
|
138
|
|
|
$stock2 = new Stock(); |
139
|
|
|
$stock2->codalmacen = $warehouse2->codalmacen; |
140
|
|
|
$stock2->idproducto = $product->idproducto; |
141
|
|
|
$stock2->referencia = $product->referencia; |
142
|
|
|
$stock2->cantidad = 5; |
143
|
|
|
$this->assertTrue($stock2->save(), 'stock-cant-save'); |
144
|
|
|
|
145
|
|
|
// control stock variante |
146
|
|
|
$variant = new Variante(); |
147
|
|
|
$this->assertTrue($variant->loadFromCode('', [ new DataBaseWhere('referencia', $product->referencia) ]), 'variant-not-found'); |
148
|
|
|
$this->assertTrue($variant->stockfis == ($stock1->cantidad + $stock2->cantidad), 'stock-variant-wrong'); |
149
|
|
|
|
150
|
|
|
$this->assertTrue($stock1->delete(), 'stock-cant-delete'); |
151
|
|
|
$this->assertTrue($stock2->delete(), 'stock-cant-delete'); |
152
|
|
|
$this->assertTrue($product->delete(), 'product-cant-delete'); |
153
|
|
|
$this->assertTrue($warehouse1->delete(), 'warehouse-cant-delete'); |
154
|
|
|
$this->assertTrue($warehouse2->delete(), 'warehouse-cant-delete'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
protected function tearDown() |
158
|
|
|
{ |
159
|
|
|
$this->logErrors(); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|