/
sale_order.py
122 lines (108 loc) · 6.05 KB
/
sale_order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from odoo import models, fields, api, _
import json
import logging
_logger = logging.getLogger(__name__)
class SaleOrder(models.Model):
_inherit = "sale.order"
vat_discriminated = fields.Boolean(
compute='_compute_vat_discriminated',
)
sale_checkbook_id = fields.Many2one(
'sale.checkbook',
readonly=True,
states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
)
@api.depends(
'partner_id.l10n_ar_afip_responsibility_type_id',
'company_id.l10n_ar_company_requires_vat',)
def _compute_vat_discriminated(self):
for rec in self:
# si tiene checkbook y discrimna en funcion al partner pero no tiene responsabilidad seteada,
# dejamos comportamiento nativo de odoo de discriminar impuestos
discriminate_taxes = rec.sale_checkbook_id.discriminate_taxes
if discriminate_taxes == 'yes':
vat_discriminated = True
elif discriminate_taxes == 'no':
vat_discriminated = False
else:
vat_discriminated = rec.company_id.l10n_ar_company_requires_vat and \
rec.partner_id.l10n_ar_afip_responsibility_type_id.code in ['1'] or False
rec.vat_discriminated = vat_discriminated
@api.onchange('company_id')
def set_sale_checkbook(self):
if self.env.user.has_group('l10n_ar_sale.use_sale_checkbook') and \
self.company_id:
self.sale_checkbook_id = self.env['sale.checkbook'].search(
[('company_id', 'in', [self.company_id.id, False])], limit=1)
else:
self.sale_checkbook_id = False
@api.model_create_multi
def create(self, vals):
for val in vals:
if self.env.user.has_group('l10n_ar_sale.use_sale_checkbook') and \
val.get('name', _('New')) == _('New') and \
val.get('sale_checkbook_id'):
sale_checkbook = self.env['sale.checkbook'].browse(
val.get('sale_checkbook_id'))
val['name'] = sale_checkbook.sequence_id and\
sale_checkbook.sequence_id._next() or _('New')
return super(SaleOrder, self).create(vals)
def _compute_tax_totals(self):
""" Mandamos en contexto el invoice_date para calculo de impuesto con partner aliquot
ver módulo l10n_ar_account_withholding. Además acá reemplazamos el método _compute_tax_totals del módulo sale original de odoo"""
for rec in self:
rec = rec.with_context(invoice_date=rec.date_order)
super(SaleOrder, rec)._compute_tax_totals()
report_or_portal_view = 'commit_assetsbundle' in self.env.context or \
not self.env.context.get('params', {}).get('view_type') == 'form'
if not report_or_portal_view:
return
for order in self.filtered(lambda x: not x.vat_discriminated):
tax_groups = order.order_line.mapped('tax_id.tax_group_id')
if not tax_groups:
continue
to_remove_ids = tax_groups.filtered(lambda x: x.l10n_ar_vat_afip_code).ids
tax_group_name = list(order.tax_totals['groups_by_subtotal'].keys())[0]
tax_group_vals = order.tax_totals['groups_by_subtotal'].get(tax_group_name)
updated_tax_group_vals = list(filter(lambda x: x.get('tax_group_id') not in to_remove_ids, tax_group_vals))
new_totals = order.tax_totals
new_totals['groups_by_subtotal'].update({tax_group_name: updated_tax_group_vals})
order.tax_totals = new_totals
def _get_name_sale_report(self, report_xml_id):
""" Method similar to the '_get_name_invoice_report' of l10n_latam_invoice_document
Basically it allows different localizations to define it's own report
This method should actually go in a sale_ux module that later can be extended by different localizations
Another option would be to use report_substitute module and setup a subsitution with a domain
"""
self.ensure_one()
if self.company_id.country_id.code == 'AR':
return 'l10n_ar_sale.report_saleorder_document'
return report_xml_id
def _l10n_ar_include_vat(self):
self.ensure_one()
discriminate_taxes = self.sale_checkbook_id.discriminate_taxes
if discriminate_taxes == 'yes':
return False
elif discriminate_taxes == 'no':
return True
else:
return not (
self.company_id.l10n_ar_company_requires_vat and
self.partner_id.l10n_ar_afip_responsibility_type_id.code in ['1'] or False)
def _create_invoices(self, grouped=False, final=False, date=None):
""" Por alguna razon cuando voy a crear la factura a traves de una devolucion, no me esta permitiendo crearla
y validarla porque resulta el campo tipo de documento esta quedando vacio. Este campo se llena y computa
automaticamente al generar al modificar el diaro de una factura.
Si hacemos la prueba funcional desde la interfaz funciona, si intento importar la factura con el importador de
Odoo funciona, pero si la voy a crear desde la devolucion inventario no se rellena dicho campo.
Para solventar decimos si tenemos facturas que usan documentos y que no tienen un tipo de documento, intentamos
computarlo y asignarlo, esto aplica para cuando generamos una factura desde una orden de venta o suscripcion """
invoices = super()._create_invoices(grouped=grouped, final=final, date=date)
# Intentamos Completar el dato tipo de documento si no seteado
to_fix = invoices.filtered(lambda x: x.l10n_latam_use_documents and not x.l10n_latam_document_type_id)
to_fix._compute_l10n_latam_available_document_types()
return invoices