|
19 | 19 |
|
20 | 20 | namespace FacturaScripts\Test\Core\Model; |
21 | 21 |
|
22 | | -use FacturaScripts\Core\Base\DataBase\DataBaseWhere; |
23 | 22 | use FacturaScripts\Core\DataSrc\Impuestos; |
24 | 23 | use FacturaScripts\Core\Lib\Calculator; |
25 | 24 | use FacturaScripts\Core\Model\AlbaranProveedor; |
26 | 25 | use FacturaScripts\Core\Model\Almacen; |
27 | 26 | use FacturaScripts\Core\Model\Empresa; |
| 27 | +use FacturaScripts\Core\Model\Stock; |
28 | 28 | use FacturaScripts\Core\Tools; |
| 29 | +use FacturaScripts\Core\Where; |
29 | 30 | use FacturaScripts\Test\Traits\DefaultSettingsTrait; |
30 | 31 | use FacturaScripts\Test\Traits\LogErrorsTrait; |
31 | 32 | use FacturaScripts\Test\Traits\RandomDataTrait; |
@@ -247,6 +248,84 @@ public function testCreateProductLine(): void |
247 | 248 | $this->assertTrue($product->delete(), 'can-not-delete-product-3'); |
248 | 249 | } |
249 | 250 |
|
| 251 | + public function testApproveAfterManualStockChangeKeepsFinalStock(): void |
| 252 | + { |
| 253 | + // creamos un proveedor |
| 254 | + $subject = $this->getRandomSupplier(); |
| 255 | + $this->assertTrue($subject->save(), 'can-not-save-supplier-4'); |
| 256 | + |
| 257 | + // creamos un producto |
| 258 | + $product = $this->getRandomProduct(); |
| 259 | + $this->assertTrue($product->save(), 'can-not-save-product-4'); |
| 260 | + |
| 261 | + // creamos un albarán |
| 262 | + $doc = new AlbaranProveedor(); |
| 263 | + $doc->setSubject($subject); |
| 264 | + $this->assertTrue($doc->save(), 'can-not-create-albaran-proveedor-4'); |
| 265 | + |
| 266 | + // añadimos el producto con 10 unidades |
| 267 | + $line = $doc->getNewProductLine($product->referencia); |
| 268 | + $line->cantidad = 10; |
| 269 | + $line->pvpunitario = 10; |
| 270 | + $this->assertTrue($line->save(), 'can-not-save-line-4'); |
| 271 | + |
| 272 | + // comprobamos que el albarán ha sumado stock |
| 273 | + $product->reload(); |
| 274 | + $this->assertEquals(10, $product->stockfis, 'albaran-proveedor-product-do-not-update-stock-4'); |
| 275 | + |
| 276 | + $stock = new Stock(); |
| 277 | + $where = [ |
| 278 | + Where::eq('codalmacen', $doc->codalmacen), |
| 279 | + Where::eq('referencia', $product->referencia), |
| 280 | + ]; |
| 281 | + $this->assertTrue($stock->loadWhere($where), 'can-not-load-stock-4'); |
| 282 | + $this->assertEquals(10, $stock->cantidad, 'stock-not-created-from-delivery-note-4'); |
| 283 | + |
| 284 | + // simulamos una corrección manual del stock a 4 unidades |
| 285 | + $stock->cantidad = 4; |
| 286 | + $this->assertTrue($stock->save(), 'can-not-update-stock-4'); |
| 287 | + |
| 288 | + $stock->reload(); |
| 289 | + $product->reload(); |
| 290 | + $this->assertEquals(4, $stock->cantidad, 'manual-stock-change-not-persist-4'); |
| 291 | + $this->assertEquals(4, $product->stockfis, 'manual-product-stock-change-not-persist-4'); |
| 292 | + |
| 293 | + // aprobamos el albarán para generar la factura |
| 294 | + $statusFound = false; |
| 295 | + foreach ($doc->getAvailableStatus() as $status) { |
| 296 | + if (empty($status->generadoc)) { |
| 297 | + continue; |
| 298 | + } |
| 299 | + |
| 300 | + $statusFound = true; |
| 301 | + $doc->idestado = $status->idestado; |
| 302 | + $this->assertTrue($doc->save(), 'can-not-approve-albaran-proveedor-4'); |
| 303 | + break; |
| 304 | + } |
| 305 | + |
| 306 | + $this->assertTrue($statusFound, 'no-status-with-document-generation-4'); |
| 307 | + |
| 308 | + // el stock final debe seguir siendo 4: el albarán deja de sumar y la factura lo vuelve a añadir |
| 309 | + $stock->reload(); |
| 310 | + $product->reload(); |
| 311 | + $this->assertEquals(4, $stock->cantidad, 'bad-final-stock-after-approve-4'); |
| 312 | + $this->assertEquals(4, $product->stockfis, 'bad-final-product-stock-after-approve-4'); |
| 313 | + |
| 314 | + // comprobamos que la factura se ha generado |
| 315 | + $children = $doc->childrenDocuments(); |
| 316 | + $this->assertNotEmpty($children, 'factura-no-generada-4'); |
| 317 | + |
| 318 | + // eliminamos |
| 319 | + foreach ($children as $child) { |
| 320 | + $this->assertTrue($child->delete(), 'factura-cant-delete-4'); |
| 321 | + } |
| 322 | + $this->assertTrue($doc->delete(), 'can-not-delete-albaran-proveedor-4'); |
| 323 | + $this->assertFalse($line->exists(), 'linea-albaran-proveedor-still-exists-4'); |
| 324 | + $this->assertTrue($subject->getDefaultAddress()->delete(), 'contacto-cant-delete'); |
| 325 | + $this->assertTrue($subject->delete(), 'can-not-delete-proveedor-4'); |
| 326 | + $this->assertTrue($product->delete(), 'can-not-delete-product-4'); |
| 327 | + } |
| 328 | + |
250 | 329 | public function testPropertiesLength(): void |
251 | 330 | { |
252 | 331 | // creamos un proveedor |
@@ -294,7 +373,7 @@ public function testSecondCompany(): void |
294 | 373 |
|
295 | 374 | // obtenemos el almacén de la empresa 2 |
296 | 375 | $warehouse = new Almacen(); |
297 | | - $where = [new DataBaseWhere('idempresa', $company2->idempresa)]; |
| 376 | + $where = [Where::eq('idempresa', $company2->idempresa)]; |
298 | 377 | $warehouse->loadWhere($where); |
299 | 378 |
|
300 | 379 | // creamos un proveedor |
|
0 commit comments