diff --git a/intrastat_base/models/intrastat_common.py b/intrastat_base/models/intrastat_common.py
index a18ea07..ae892c0 100644
--- a/intrastat_base/models/intrastat_common.py
+++ b/intrastat_base/models/intrastat_common.py
@@ -85,14 +85,16 @@ class IntrastatCommon(models.AbstractModel):
return True
@api.model
- def _check_xml_schema(self, xml_root, xml_string, xsd_file):
+ def _check_xml_schema(self, xml_string, xsd_file):
'''Validate the XML file against the XSD'''
from lxml import etree
+ from StringIO import StringIO
xsd_etree_obj = etree.parse(
tools.file_open(xsd_file))
official_schema = etree.XMLSchema(xsd_etree_obj)
try:
- official_schema.assertValid(xml_root)
+ t = etree.parse(StringIO(xml_string))
+ official_schema.assertValid(t)
except Exception, e:
# if the validation of the XSD fails, we arrive here
logger = logging.getLogger(__name__)
@@ -124,6 +126,13 @@ class IntrastatCommon(models.AbstractModel):
'datas_fname': filename})
return attach.id
+ @api.multi
+ def _unlink_attachments(self):
+ atts = self.env['ir.attachment'].search(
+ [('res_model', '=', self._name),
+ ('res_id', '=', self.id)])
+ atts.unlink()
+
@api.model
def _open_attach_view(self, attach_id, title='XML file'):
'''Returns an action which opens the form view of the
@@ -140,6 +149,20 @@ class IntrastatCommon(models.AbstractModel):
}
return action
+ @api.multi
+ def _generate_xml(self):
+ """
+ Inherit this method in the localization module
+ to generate the INTRASTAT Declaration XML file
+
+ Returns:
+ string with XML data
+
+ Call the _check_xml_schema() method
+ before returning the XML string.
+ """
+ return False
+
@api.one
def send_reminder_email(self, mail_template_xmlid):
mail_template = self.env.ref(mail_template_xmlid)
diff --git a/intrastat_product/models/intrastat_product_declaration.py b/intrastat_product/models/intrastat_product_declaration.py
index ab35776..db561e2 100644
--- a/intrastat_product/models/intrastat_product_declaration.py
+++ b/intrastat_product/models/intrastat_product_declaration.py
@@ -23,7 +23,7 @@
##############################################################################
from openerp import models, fields, api, _
-from openerp.exceptions import RedirectWarning, ValidationError
+from openerp.exceptions import RedirectWarning, ValidationError, Warning
import openerp.addons.decimal_precision as dp
from datetime import datetime, date
from dateutil.relativedelta import relativedelta
@@ -438,6 +438,7 @@ class IntrastatProductDeclaration(models.Model):
@api.multi
def action_gather(self):
self.ensure_one()
+ self._check_generate_lines()
self._note = ''
self._uom_refs = {
'weight_uom_categ': self.env.ref('product.product_uom_categ_kgm'),
@@ -518,6 +519,22 @@ class IntrastatProductDeclaration(models.Model):
cl.write({'declaration_line_id': declaration_line.id})
return True
+ @api.multi
+ def generate_xml(self):
+ """ generate the INTRASTAT Declaration XML file """
+ self.ensure_one()
+ self._check_generate_xml()
+ self._unlink_attachments()
+ xml_string = self._generate_xml()
+ if xml_string:
+ attach_id = self._attach_xml_file(
+ xml_string, '%s_%s' % (self.type, self.revision))
+ return self._open_attach_view(attach_id)
+ else:
+ raise Warning(
+ _("Programming Error."),
+ _("No XML File has been generated."))
+
@api.multi
def done(self):
self.write({'state': 'done'})
diff --git a/intrastat_product/views/intrastat_product_declaration.xml b/intrastat_product/views/intrastat_product_declaration.xml
index ed429e3..860c8da 100644
--- a/intrastat_product/views/intrastat_product_declaration.xml
+++ b/intrastat_product/views/intrastat_product_declaration.xml
@@ -12,7 +12,13 @@
attrs="{'invisible': ['|', ('state', '!=', 'draft'), ('action', '=', 'nihil')]}"
string="Generate lines from invoices"
class="oe_highlight"/>
-
+
+
@@ -34,6 +40,7 @@
+