-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] sustainability: Allow supplerinfo to be used for purchase order
- Loading branch information
1 parent
87ea8d0
commit f0e2310
Showing
11 changed files
with
250 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
# from . import product_supplierinfo | ||
from . import product_supplierinfo | ||
from . import account_move_line | ||
from . import product_template | ||
from . import purchase_order | ||
from . import purchase_order_line | ||
from . import res_partner | ||
from . import carbon_factor | ||
from . import carbon_line_origin | ||
from . import carbon_line_mixin | ||
from . import carbon_mixin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from odoo import models, _ | ||
|
||
class CarbonLineMixin(models.AbstractModel): | ||
_inherit = 'carbon.line.mixin' | ||
|
||
def _get_computation_levels_mapping(self): | ||
mapping = super()._get_computation_levels_mapping() | ||
mapping['product.supplierinfo'] = _("Product supplier") | ||
return mapping |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from odoo import models, _ | ||
|
||
class CarbonMixin(models.AbstractModel): | ||
_inherit = 'carbon.mixin' | ||
|
||
def _CARBON_MODELS(self): | ||
res = super()._CARBON_MODELS() | ||
res.append('product.supplierinfo') | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,28 @@ | ||
from odoo import models | ||
from odoo import api, models | ||
|
||
|
||
class ProductSupplierInfo(models.Model): | ||
_name = "product.supplierinfo" | ||
_inherit = ["product.supplierinfo", "carbon.mixin"] | ||
|
||
def _get_carbon_in_fallback_records(self) -> list: | ||
self.ensure_one() | ||
res = super()._get_carbon_in_fallback_records() | ||
return res + [self.partner_id] | ||
def _update_carbon_in_fields(self, vals): | ||
""" | ||
Helper method to update carbon_in_is_manual and carbon_in_mode based on carbon_in_factor_id. | ||
""" | ||
if vals.get("carbon_in_factor_id"): | ||
vals["carbon_in_is_manual"] = True | ||
vals["carbon_in_mode"] = "manual" | ||
else: | ||
vals["carbon_in_is_manual"] = False | ||
vals["carbon_in_mode"] = "auto" | ||
return vals | ||
|
||
def _get_carbon_out_fallback_records(self) -> list: | ||
self.ensure_one() | ||
res = super()._get_carbon_out_fallback_records() | ||
return res + [self.partner_id] | ||
@api.model | ||
def create(self, vals): | ||
vals = self._update_carbon_in_fields(vals) | ||
return super().create(vals) | ||
|
||
def write(self, vals): | ||
if "carbon_in_factor_id" in vals: | ||
vals = self._update_carbon_in_fields(vals) | ||
return super().write(vals) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import test_purchase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from odoo.addons.sustainability.tests.common import CarbonCommon | ||
|
||
from odoo import Command | ||
|
||
|
||
class CarbonPurchaseCommon(CarbonCommon): | ||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
|
||
cls.partner_2 = cls.env["res.partner"].create( | ||
{ | ||
"name": "Test Partner number 2", | ||
"email": "[email protected]", | ||
"phone": "+123456782", | ||
"street": "123 Test Street 2", | ||
"city": "Test City 2", | ||
"country_id": cls.env.ref("base.us").id, | ||
} | ||
) | ||
|
||
cls.kg_uom_id = cls.env.ref("uom.product_uom_kgm") | ||
cls.product_template = cls.env["product.template"].create( | ||
dict( | ||
categ_id=cls.product_category.id, | ||
name="Wooden Chair", | ||
uom_id=cls.kg_uom_id.id, | ||
uom_po_id=cls.kg_uom_id.id, | ||
list_price=100, | ||
seller_ids=[ | ||
Command.create( | ||
dict( | ||
currency_id=cls.currency_usd.id, | ||
delay=1, | ||
min_qty=1, | ||
partner_id=cls.partner.id, | ||
price=50, | ||
carbon_in_factor_id=cls.carbon_factor_monetary.id, | ||
) | ||
), | ||
Command.create( | ||
dict( | ||
currency_id=cls.currency_usd.id, | ||
delay=4, | ||
min_qty=1, | ||
partner_id=cls.partner_2.id, | ||
price=30, | ||
carbon_in_factor_id=cls.carbon_factor_default_fallback.id, | ||
) | ||
), | ||
], | ||
) | ||
) | ||
cls.product_product = cls.env["product.product"].search( | ||
[("product_tmpl_id", "=", cls.product_template.id)], limit=1 | ||
) | ||
cls.purchase = cls.env["purchase.order"].create( | ||
dict( | ||
partner_id=cls.partner.id, | ||
order_line=[ | ||
Command.create( | ||
dict( | ||
product_id=cls.product_product.id, | ||
product_qty=1.0, | ||
product_uom=cls.kg_uom_id.id, | ||
price_unit=100.0, | ||
) | ||
), | ||
], | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from odoo.addons.sustainability_purchase.tests.common import CarbonPurchaseCommon | ||
|
||
|
||
class TestCarbonPurchase(CarbonPurchaseCommon): | ||
def test_create_purchase_order(self): | ||
# Check with the default values | ||
self.purchase.action_recompute_carbon() | ||
self.assertEqual(round(self.purchase.carbon_debt, 2), 2.38) | ||
|
||
# Check with a different partner | ||
self.purchase.partner_id = self.partner_2.id | ||
self.purchase.action_recompute_carbon() | ||
self.assertEqual(round(self.purchase.carbon_debt, 2), 952.4) | ||
|
||
# Check with a different price | ||
self.purchase.order_line[0].price_unit = 200 | ||
self.purchase.action_recompute_carbon() | ||
self.assertEqual(round(self.purchase.carbon_debt, 2), 1904.8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters