Reviewers: ,


Please review this at http://codereview.tryton.org/931002/

Affected files:
  M trytond/modules/sale/sale.py
  A trytond/modules/sale_invoice_grouped/__init__.py
  A trytond/modules/sale_invoice_grouped/party.py
  A trytond/modules/sale_invoice_grouped/party.xml
  A trytond/modules/sale_invoice_grouped/sale.py
  A trytond/modules/sale_invoice_grouped/tryton.cfg
  A trytond/modules/sale_invoice_grouped/view/party_form.xml


Index: trytond/modules/sale/sale.py
===================================================================

--- a/trytond/modules/sale/sale.py
+++ b/trytond/modules/sale/sale.py
@@ -654,13 +654,8 @@
                 res[line.id] = val
         return res
 
-    def _get_invoice_sale(self, invoice_type):
-        '''
-        Return invoice of type invoice_type
-        '''
-        pool = Pool()
-        Invoice = pool.get('account.invoice')
-        Journal = pool.get('account.journal')
+    def invoice_data(self, invoice_type):
+        Journal = Pool().get('account.journal')
 
         journals = Journal.search([
                 ('type', '=', 'revenue'),
@@ -670,18 +665,25 @@
         else:
             journal = None
 
+        return {
+            'company': self.company,
+            'type': invoice_type,
+            'journal': journal,
+            'party': self.party,
+            'invoice_address': self.invoice_address,
+            'currency': self.currency,
+            'account': self.party.account_receivable,
+            'payment_term': self.payment_term,
+            }
+
+    def _get_invoice_sale(self, invoice_type):
+        '''
+        Return invoice of type invoice_type
+        '''
+        Invoice = Pool().get('account.invoice')
+
         with Transaction().set_user(0, set_context=True):
-            return Invoice(
-                company=self.company,
-                type=invoice_type,
-                reference=self.reference,
-                journal=journal,
-                party=self.party,
-                invoice_address=self.invoice_address,
-                currency=self.currency,
-                account=self.party.account_receivable,
-                payment_term=self.payment_term,
-                )
+            return Invoice(**self.invoice_data(invoice_type))
 
     def create_invoice(self, invoice_type):
         '''
@@ -701,7 +703,9 @@
             return
 
         invoice = self._get_invoice_sale(invoice_type)
-        invoice.lines = list(chain.from_iterable(invoice_lines.itervalues()))
+        invoice.lines = ((list(invoice.lines)
+                if hasattr(invoice, 'lines') else [])
+            + list(chain.from_iterable(invoice_lines.itervalues())))
         invoice.save()
 
         with Transaction().set_user(0, set_context=True):

Index: trytond/modules/sale_invoice_grouped/__init__.py
===================================================================
new file mode 100644

--- /dev/null
+++ b/trytond/modules/sale_invoice_grouped/__init__.py
@@ -0,0 +1,13 @@
+# This file is part of Tryton.  The COPYRIGHT file at the top level of this
+# repository contains the full copyright notices and license terms.
+
+from trytond.pool import Pool
+from .party import *
+from .sale import *
+
+
+def register():
+    Pool.register(
+        Sale,
+        Party,
+        module='sale_invoice_grouped', type_='model')

Index: trytond/modules/sale_invoice_grouped/party.py
===================================================================
new file mode 100644

--- /dev/null
+++ b/trytond/modules/sale_invoice_grouped/party.py
@@ -0,0 +1,13 @@
+# This file is part of Tryton.  The COPYRIGHT file at the top level of this
+# repository contains the full copyright notices and license terms.
+
+from trytond.model import fields
+from trytond.pool import PoolMeta
+
+__metaclass__ = PoolMeta
+
+
+class Party:
+    __name__ = 'party.party'
+
+    group_sale_invoice = fields.Boolean('Group Sale Invoice')

Index: trytond/modules/sale_invoice_grouped/party.xml
===================================================================
new file mode 100644

--- /dev/null
+++ b/trytond/modules/sale_invoice_grouped/party.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0"?>
+<tryton>
+    <data>
+        <record model="ir.ui.view" id="party_view_form">
+            <field name="model">party.party</field>
+            <field name="inherit" ref="party.party_view_form"/>
+            <field name="name">party_form</field>
+        </record>
+    </data>
+</tryton>

Index: trytond/modules/sale_invoice_grouped/sale.py
===================================================================
new file mode 100644

--- /dev/null
+++ b/trytond/modules/sale_invoice_grouped/sale.py
@@ -0,0 +1,26 @@
+# This file is part of Tryton.  The COPYRIGHT file at the top level of this
+# repository contains the full copyright notices and license terms.
+
+from trytond.pool import Pool, PoolMeta
+
+__metaclass__ = PoolMeta
+
+
+class Sale:
+    __name__ = 'sale.sale'
+
+    def _get_common_invoice_data(self, invoice_type):
+        data = self.invoice_data(invoice_type)
+        data['state'] = 'draft'
+        return data
+
+    def _get_invoice_sale(self, invoice_type):
+        Invoice = Pool().get('account.invoice')
+        if self.party.group_sale_invoice:
+            invoice_domain = [(key, '=', value)
+                for key, value in self._get_common_invoice_data(invoice_type)
+                    .iteritems()]
+            grouped_invoice = Invoice.search(invoice_domain, limit=1)
+            if grouped_invoice:
+                return grouped_invoice[0]
+        return super(Sale, self)._get_invoice_sale(invoice_type)

Index: trytond/modules/sale_invoice_grouped/tryton.cfg
===================================================================
new file mode 100644

--- /dev/null
+++ b/trytond/modules/sale_invoice_grouped/tryton.cfg
@@ -0,0 +1,9 @@
+[tryton]
+version=2.9.0
+depends:
+    ir
+    res
+    account_invoice
+    sale
+xml:
+    party.xml

Index: trytond/modules/sale_invoice_grouped/view/party_form.xml
===================================================================
new file mode 100644

--- /dev/null
+++ b/trytond/modules/sale_invoice_grouped/view/party_form.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0"?>
+<data>
+    <xpath expr="/form/group[@id='checkboxes']" position="inside">
+        <label name="group_sale_invoice"/>
+        <field name="group_sale_invoice"/>
+    </xpath>
+</data>

Reply via email to