Skip to content

Commit

Permalink
Merge PR #2849 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by rvalyi
  • Loading branch information
OCA-git-bot committed Sep 26, 2024
2 parents 52a62b9 + 3efb527 commit 18c06c8
Show file tree
Hide file tree
Showing 20 changed files with 1,359 additions and 35 deletions.
19 changes: 15 additions & 4 deletions l10n_br_fiscal/models/document_line_mixin_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

class FiscalDocumentLineMixinMethods(models.AbstractModel):
_name = "l10n_br_fiscal.document.line.mixin.methods"
_description = "Document Fiscal Mixin Methods"
_description = "Fiscal Document Mixin Methods"

@api.model
def inject_fiscal_fields(
Expand Down Expand Up @@ -190,7 +190,7 @@ def _compute_taxes(self, taxes, cst=None):
self.ensure_one()
return taxes.compute_taxes(
company=self.company_id,
partner=self.partner_id,
partner=self._get_fiscal_partner(),
product=self.product_id,
price_unit=self.price_unit,
quantity=self.quantity,
Expand Down Expand Up @@ -355,6 +355,17 @@ def _document_comment(self):
d.__document_comment_vals(), d.manual_additional_data
)

def _get_fiscal_partner(self):
"""
Meant to be overriden when the l10n_br_fiscal.document partner_id should not
be the same as the sale.order, purchase.order, account.move (...) partner_id.
(In the case of invoicing, the invoicing partner set by the user should
get priority over any invoicing contact returned by address_get.)
"""
self.ensure_one()
return self.partner_id

@api.onchange("fiscal_operation_id")
def _onchange_fiscal_operation_id(self):
if self.fiscal_operation_id:
Expand All @@ -363,7 +374,7 @@ def _onchange_fiscal_operation_id(self):
self._onchange_commercial_quantity()
self.fiscal_operation_line_id = self.fiscal_operation_id.line_definition(
company=self.company_id,
partner=self.partner_id,
partner=self._get_fiscal_partner(),
product=self.product_id,
)
self._onchange_fiscal_operation_line_id()
Expand All @@ -375,7 +386,7 @@ def _onchange_fiscal_operation_line_id(self):
if self.fiscal_operation_line_id:
mapping_result = self.fiscal_operation_line_id.map_fiscal_taxes(
company=self.company_id,
partner=self.partner_id,
partner=self._get_fiscal_partner(),
product=self.product_id,
ncm=self.ncm_id,
nbm=self.nbm_id,
Expand Down
18 changes: 15 additions & 3 deletions l10n_br_fiscal/models/document_mixin_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class FiscalDocumentMixinMethods(models.AbstractModel):
_name = "l10n_br_fiscal.document.mixin.methods"
_description = "Document Fiscal Mixin Methods"
_description = "Fiscal Document Mixin Methods"

def _prepare_br_fiscal_dict(self, default=False):
self.ensure_one()
Expand Down Expand Up @@ -92,10 +92,22 @@ def _document_comment(self):
)
d.fiscal_line_ids._document_comment()

def _get_fiscal_partner(self):
"""
Meant to be overriden when the l10n_br_fiscal.document partner_id should not
be the same as the sale.order, purchase.order, account.move (...) partner_id.
(In the case of invoicing, the invoicing partner set by the user should
get priority over any invoicing contact returned by address_get.)
"""
self.ensure_one()
return self.partner_id

@api.onchange("partner_id")
def _onchange_partner_id_fiscal(self):
if self.partner_id:
self.ind_final = self.partner_id.ind_final
partner = self._get_fiscal_partner()
if partner:
self.ind_final = partner.ind_final
for line in self._get_amount_lines():
# reload fiscal data, operation line, cfop, taxes, etc.
line._onchange_fiscal_operation_id()
Expand Down
232 changes: 232 additions & 0 deletions l10n_br_purchase/demo/l10n_br_purchase.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,238 @@
<value eval="[ref('main_pl_only_products_2_2')]" />
</function>

<!-- Endereço de Cobrança Diferente do Partner,a Fatura criada deverá ter
outro partner, o Contato definido como Endereço de Faturamento/Cobrança,
TODO: deveria criar a fatura usando Partner do Pedido como o Parceiro
para Entrega/partner_shipping_id?
No caso l10n_br_purchase_stock isso acontece
-->
<record id="main_company-purchase_2" model="purchase.order">
<field name="name">l10n_br_purchase - Endereço de Cobrança Diferente</field>
<field name="partner_id" ref="l10n_br_base.res_partner_cliente7_rs" />
<field name="state">draft</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_compras" />
<field name="user_id" ref="base.user_admin" />
<field name="company_id" ref="base.main_company" />
</record>

<function model="purchase.order" name="_onchange_fiscal_operation_id">
<value eval="[ref('main_company-purchase_2')]" />
</function>

<record id="main_company-purchase_line_2_1" model="purchase.order.line">
<field name="order_id" ref="main_company-purchase_2" />
<field name="name">Office Chair Black</field>
<field name="product_id" ref="product.product_product_12" />
<field name="product_qty">4</field>
<field name="product_uom" ref="uom.product_uom_unit" />
<!-- Apesar do Preço ser defindo aqui o _onchange_product_id_fiscal altera o valor -->
<field name="price_unit">25.0</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_compras" />
<field
name="fiscal_operation_line_id"
ref="l10n_br_fiscal.fo_compras_compras"
/>
<field name="date_planned" eval="time.strftime('%Y-%m-%d')" />
</record>

<function model="purchase.order.line" name="_onchange_product_id_fiscal">
<value eval="[ref('main_company-purchase_line_2_1')]" />
</function>

<record id="main_company-purchase_line_2_2" model="purchase.order.line">
<field name="order_id" ref="main_company-purchase_2" />
<field name="name">Drawer Black</field>
<field name="product_id" ref="product.product_product_16" />
<field name="product_qty">2</field>
<field name="product_uom" ref="uom.product_uom_unit" />
<field name="price_unit">50.00</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_compras" />
<field
name="fiscal_operation_line_id"
ref="l10n_br_fiscal.fo_compras_compras"
/>
<field name="date_planned" eval="time.strftime('%Y-%m-%d')" />
</record>

<function model="purchase.order.line" name="_onchange_product_id_fiscal">
<value eval="[ref('main_company-purchase_line_2_2')]" />
</function>

<!-- Endereço de Cobrança Diferente do Partner, nesse caso o Endereço já
está informado, não deve ter o Parceiro para Entrega
-->
<record id="main_company-purchase_3" model="purchase.order">
<field name="name">l10n_br_purchase - Endereço de Cobrança Informado</field>
<field
name="partner_id"
ref="l10n_br_base.res_partner_cliente7_rs_end_cobranca"
/>
<field name="state">draft</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_compras" />
<field name="user_id" ref="base.user_admin" />
<field name="company_id" ref="base.main_company" />
</record>

<function model="purchase.order" name="_onchange_fiscal_operation_id">
<value eval="[ref('main_company-purchase_3')]" />
</function>

<record id="main_company-purchase_line_3_1" model="purchase.order.line">
<field name="order_id" ref="main_company-purchase_3" />
<field name="name">Office Chair Black</field>
<field name="product_id" ref="product.product_product_12" />
<field name="product_qty">4</field>
<field name="product_uom" ref="uom.product_uom_unit" />
<!-- Apesar do Preço ser defindo aqui o _onchange_product_id_fiscal altera o valor -->
<field name="price_unit">25.0</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_compras" />
<field
name="fiscal_operation_line_id"
ref="l10n_br_fiscal.fo_compras_compras"
/>
<field name="date_planned" eval="time.strftime('%Y-%m-%d')" />
</record>

<function model="purchase.order.line" name="_onchange_product_id_fiscal">
<value eval="[ref('main_company-purchase_line_3_1')]" />
</function>

<record id="main_company-purchase_line_3_2" model="purchase.order.line">
<field name="order_id" ref="main_company-purchase_3" />
<field name="name">Drawer Black</field>
<field name="product_id" ref="product.product_product_16" />
<field name="product_qty">2</field>
<field name="product_uom" ref="uom.product_uom_unit" />
<field name="price_unit">50.00</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_compras" />
<field
name="fiscal_operation_line_id"
ref="l10n_br_fiscal.fo_compras_compras"
/>
<field name="date_planned" eval="time.strftime('%Y-%m-%d')" />
</record>

<function model="purchase.order.line" name="_onchange_product_id_fiscal">
<value eval="[ref('main_company-purchase_line_3_2')]" />
</function>

<!-- Partner usado é o Contato de Entrega, a Fatura é criada com
o Partner do Pedido
-->
<record id="main_company-purchase_4" model="purchase.order">
<field
name="name"
>l10n_br_purchase - Contato do Endereço de Entrega Informado</field>
<field
name="partner_id"
ref="l10n_br_base.res_partner_cliente2_sp_end_entrega"
/>
<field name="state">draft</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_compras" />
<field name="user_id" ref="base.user_admin" />
<field name="company_id" ref="base.main_company" />
</record>

<function model="purchase.order" name="_onchange_fiscal_operation_id">
<value eval="[ref('main_company-purchase_4')]" />
</function>

<record id="main_company-purchase_line_4_1" model="purchase.order.line">
<field name="order_id" ref="main_company-purchase_4" />
<field name="name">Office Chair Black</field>
<field name="product_id" ref="product.product_product_12" />
<field name="product_qty">4</field>
<field name="product_uom" ref="uom.product_uom_unit" />
<!-- Apesar do Preço ser defindo aqui o _onchange_product_id_fiscal altera o valor -->
<field name="price_unit">25.0</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_compras" />
<field
name="fiscal_operation_line_id"
ref="l10n_br_fiscal.fo_compras_compras"
/>
<field name="date_planned" eval="time.strftime('%Y-%m-%d')" />
</record>

<function model="purchase.order.line" name="_onchange_product_id_fiscal">
<value eval="[ref('main_company-purchase_line_4_1')]" />
</function>

<record id="main_company-purchase_line_4_2" model="purchase.order.line">
<field name="order_id" ref="main_company-purchase_4" />
<field name="name">Drawer Black</field>
<field name="product_id" ref="product.product_product_16" />
<field name="product_qty">2</field>
<field name="product_uom" ref="uom.product_uom_unit" />
<field name="price_unit">50.00</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_compras" />
<field
name="fiscal_operation_line_id"
ref="l10n_br_fiscal.fo_compras_compras"
/>
<field name="date_planned" eval="time.strftime('%Y-%m-%d')" />
</record>

<function model="purchase.order.line" name="_onchange_product_id_fiscal">
<value eval="[ref('main_company-purchase_line_4_2')]" />
</function>

<!-- Caso do Pedido com o Partner que tem um Contato com um Endereço de Entrega
a Fatura é criada com o Partner do Pedido -->
<record id="main_company-purchase_5" model="purchase.order">
<field
name="name"
>l10n_br_purchase - Partner que tem um Endereço de Entrega</field>
<field name="partner_id" ref="l10n_br_base.res_partner_cliente2_sp" />
<field name="state">draft</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_compras" />
<field name="user_id" ref="base.user_admin" />
<field name="company_id" ref="base.main_company" />
</record>

<function model="purchase.order" name="_onchange_fiscal_operation_id">
<value eval="[ref('main_company-purchase_5')]" />
</function>

<record id="main_company-purchase_line_5_1" model="purchase.order.line">
<field name="order_id" ref="main_company-purchase_5" />
<field name="name">Office Chair Black</field>
<field name="product_id" ref="product.product_product_12" />
<field name="product_qty">4</field>
<field name="product_uom" ref="uom.product_uom_unit" />
<!-- Apesar do Preço ser defindo aqui o _onchange_product_id_fiscal altera o valor -->
<field name="price_unit">25.0</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_compras" />
<field
name="fiscal_operation_line_id"
ref="l10n_br_fiscal.fo_compras_compras"
/>
<field name="date_planned" eval="time.strftime('%Y-%m-%d')" />
</record>

<function model="purchase.order.line" name="_onchange_product_id_fiscal">
<value eval="[ref('main_company-purchase_line_3_1')]" />
</function>

<record id="main_company-purchase_line_5_2" model="purchase.order.line">
<field name="order_id" ref="main_company-purchase_5" />
<field name="name">Drawer Black</field>
<field name="product_id" ref="product.product_product_16" />
<field name="product_qty">2</field>
<field name="product_uom" ref="uom.product_uom_unit" />
<field name="price_unit">50.00</field>
<field name="fiscal_operation_id" ref="l10n_br_fiscal.fo_compras" />
<field
name="fiscal_operation_line_id"
ref="l10n_br_fiscal.fo_compras_compras"
/>
<field name="date_planned" eval="time.strftime('%Y-%m-%d')" />
</record>

<function model="purchase.order.line" name="_onchange_product_id_fiscal">
<value eval="[ref('main_company-purchase_line_5_2')]" />
</function>

<!-- Empresa Lucro Presumido -->
<!-- Purchase Order with only products test -->
<record id="lp_po_only_products" model="purchase.order">
Expand Down
9 changes: 9 additions & 0 deletions l10n_br_purchase/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,12 @@ def _prepare_invoice(self):
}
)
return invoice_vals

def _get_fiscal_partner(self):
self.ensure_one()
partner = super()._get_fiscal_partner()
if partner.id != partner.address_get(["invoice"]).get("invoice"):
partner = self.env["res.partner"].browse(
partner.address_get(["invoice"]).get("invoice")
)
return partner
9 changes: 9 additions & 0 deletions l10n_br_purchase/models/purchase_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,12 @@ def _prepare_account_move_line(self, move=False):
values.update(fiscal_values)

return values

def _get_fiscal_partner(self):
self.ensure_one()
partner = super()._get_fiscal_partner()
if partner.id != partner.address_get(["invoice"]).get("invoice"):
partner = self.env["res.partner"].browse(
partner.address_get(["invoice"]).get("invoice")
)
return partner
Loading

0 comments on commit 18c06c8

Please sign in to comment.