|
| 1 | +from freezegun import freeze_time |
| 2 | +from odoo import Command |
| 3 | + |
| 4 | +from .common import TestStockCurrencyValuationCommon |
| 5 | + |
| 6 | + |
| 7 | +class TestAvcoReportUom(TestStockCurrencyValuationCommon): |
| 8 | + """La cantidad del reporte de auditoría AVCO va en la UoM de REFERENCIA del |
| 9 | + producto, no en la del movimiento. |
| 10 | +
|
| 11 | + Regresión: la vista SQL de este módulo es una copia de la del core, y la copia |
| 12 | + había perdido la conversión ``sm.quantity * (um.factor / up.factor)`` junto con |
| 13 | + los dos JOIN sobre ``uom_uom``. Con el módulo instalado, una recepción de 1 |
| 14 | + docena informaba cantidad 1 en vez de 12, y como el reporte calcula el costo |
| 15 | + unitario como ``total_value / total_quantity``, el AVCO salía 12 veces más caro. |
| 16 | + """ |
| 17 | + |
| 18 | + def _receipt_in_uom(self, qty, uom, date_str): |
| 19 | + """Recepción validada expresada en `uom`, que puede no ser la del producto.""" |
| 20 | + with freeze_time(date_str): |
| 21 | + picking = self.env["stock.picking"].create( |
| 22 | + { |
| 23 | + "partner_id": self.vendor.id, |
| 24 | + "picking_type_id": self.picking_type_in.id, |
| 25 | + "location_id": self.supplier_location.id, |
| 26 | + "location_dest_id": self.stock_location.id, |
| 27 | + "move_ids": [ |
| 28 | + Command.create( |
| 29 | + { |
| 30 | + "product_id": self.product.id, |
| 31 | + "product_uom_qty": qty, |
| 32 | + "product_uom": uom.id, |
| 33 | + "location_id": self.supplier_location.id, |
| 34 | + "location_dest_id": self.stock_location.id, |
| 35 | + } |
| 36 | + ) |
| 37 | + ], |
| 38 | + } |
| 39 | + ) |
| 40 | + picking.action_confirm() |
| 41 | + move = picking.move_ids |
| 42 | + move.quantity = qty |
| 43 | + move.picked = True |
| 44 | + picking.button_validate() |
| 45 | + self.assertEqual(picking.state, "done") |
| 46 | + return picking, move |
| 47 | + |
| 48 | + def _report_line(self, move): |
| 49 | + line = ( |
| 50 | + self.env["stock.avco.report"] |
| 51 | + .with_company(self.company) |
| 52 | + .search([("product_id", "=", self.product.id), ("res_model_name", "=", "stock.move")]) |
| 53 | + .filtered(lambda r: r.id == move.id) |
| 54 | + ) |
| 55 | + self.assertEqual(len(line), 1, "Se esperaba una línea del reporte para el movimiento.") |
| 56 | + return line |
| 57 | + |
| 58 | + def test_avco_report_quantity_in_product_uom(self): |
| 59 | + uom_dozen = self.env.ref("uom.product_uom_dozen") |
| 60 | + self.assertNotEqual(uom_dozen, self.product.uom_id, "El test necesita una UoM distinta a la del producto.") |
| 61 | + |
| 62 | + self.product.standard_price = 100.0 |
| 63 | + _picking, move = self._receipt_in_uom(1, uom_dozen, self.DAY_1) |
| 64 | + |
| 65 | + line = self._report_line(move) |
| 66 | + # El oráculo es la conversión del propio ORM: la cantidad del reporte tiene que |
| 67 | + # estar en la UoM de referencia del producto, no en la del movimiento. |
| 68 | + expected = uom_dozen._compute_quantity(1, self.product.uom_id) |
| 69 | + self._assert_almost(line.quantity, expected) |
| 70 | + self.assertNotAlmostEqual( |
| 71 | + line.quantity, 1.0, places=2, msg="La cantidad quedó en la UoM del movimiento, sin convertir." |
| 72 | + ) |
| 73 | + |
| 74 | + def test_avco_report_unit_cost_uses_converted_quantity(self): |
| 75 | + uom_dozen = self.env.ref("uom.product_uom_dozen") |
| 76 | + self.product.standard_price = 100.0 |
| 77 | + _picking, move = self._receipt_in_uom(1, uom_dozen, self.DAY_1) |
| 78 | + |
| 79 | + line = self._report_line(move) |
| 80 | + # Consecuencia de la conversión: el costo unitario se calcula sobre la cantidad |
| 81 | + # convertida. Se compara contra los propios valores del reporte para no depender |
| 82 | + # de cómo se valorizó la recepción. |
| 83 | + self._assert_almost(line.avco_value, line.total_value / line.total_quantity) |
| 84 | + self._assert_almost(line.total_quantity, uom_dozen._compute_quantity(1, self.product.uom_id)) |
0 commit comments