From 8c494a40c71572d1f9545df5faf87b08280dc6c7 Mon Sep 17 00:00:00 2001
From: Sebastian Kennedy <skennedy@e-mips.com.ar>
Date: Thu, 1 Nov 2018 14:57:14 -0300
Subject: [PATCH 1/2] [ADD] date_cutoff to invoice and wizards. Calc date_due
 for wizardd

---
 __openerp__.py                                |  2 ++
 models/account_invoice.py                     |  5 ++++-
 views/ranch_purchase_data_view.xml            |  2 +-
 wizard/ranch_purchase_data_invoice_wizard.py  |  5 ++++-
 ...anch_purchase_data_invoice_wizard_view.xml | 19 +++++++++++++++++++
 wizard/ranch_purchase_data_invoice_wizardd.py | 18 ++++++++++++++++--
 ...nch_purchase_data_invoice_wizardd_view.xml | 19 +++++++++++++++++++
 7 files changed, 65 insertions(+), 5 deletions(-)
 create mode 100644 wizard/ranch_purchase_data_invoice_wizard_view.xml
 create mode 100644 wizard/ranch_purchase_data_invoice_wizardd_view.xml

diff --git a/__openerp__.py b/__openerp__.py
index a764dfe..e5046a2 100644
--- a/__openerp__.py
+++ b/__openerp__.py
@@ -33,6 +33,8 @@
     ],
     "data": [
         "views/ranch_purchase_data_view.xml",
+        "wizard/ranch_purchase_data_invoice_wizardd_view.xml",
+        "wizard/ranch_purchase_data_invoice_wizard_view.xml",
         "security/ir.model.access.csv",
     ],
     "installable": True,
diff --git a/models/account_invoice.py b/models/account_invoice.py
index e8ac881..9bb9701 100644
--- a/models/account_invoice.py
+++ b/models/account_invoice.py
@@ -6,7 +6,7 @@
 
 import logging
 
-from openerp import api, models
+from openerp import api, models, fields
 
 
 _logger = logging.getLogger(__name__)
@@ -16,6 +16,9 @@ class AccountInvoice(models.Model):
     _name = 'account.invoice'
     _inherit = 'account.invoice'
 
+    # This fields is only needed for compatibility with legacy system
+    date_cutoff = fields.Date(string='Cutoff Date')
+
     @api.multi
     def onchange_partner_id(
             self, type, partner_id, date_invoice=False,
diff --git a/views/ranch_purchase_data_view.xml b/views/ranch_purchase_data_view.xml
index 9f2a5f5..d71da08 100644
--- a/views/ranch_purchase_data_view.xml
+++ b/views/ranch_purchase_data_view.xml
@@ -23,7 +23,7 @@
                             <field name="payment_system_term_ids" nolabel="1"
                                 attrs="{'readonly': [('state', '=', 'closed')]}">
                                 <tree editable="bottom">
-                                    <field name="payment_method_id"/>
+                                    <field name="payment_method_id" options="{'no_create_edit' : True, 'no_open' : True}"/>
                                     <field name="days"/>
                                     <field name="percentage"/>
                                 </tree>
diff --git a/wizard/ranch_purchase_data_invoice_wizard.py b/wizard/ranch_purchase_data_invoice_wizard.py
index 7bda069..6b4405e 100644
--- a/wizard/ranch_purchase_data_invoice_wizard.py
+++ b/wizard/ranch_purchase_data_invoice_wizard.py
@@ -6,7 +6,7 @@
 
 import logging
 
-from openerp import api, models
+from openerp import api, models, fields
 
 
 _logger = logging.getLogger(__name__)
@@ -16,6 +16,8 @@ class PurchaseDataInvoiceWizard(models.TransientModel):
     _name = "ranch.purchase.data.invoice.wizard"
     _inherit = "ranch.purchase.data.invoice.wizard"
 
+    date_cutoff = fields.Date(string='Cutoff Date')
+
     @api.multi
     def _adjust_invoice_lines(self, purchase_data, invoice_vals,
                               invoice_line_cattle_ids):
@@ -37,5 +39,6 @@ class PurchaseDataInvoiceWizard(models.TransientModel):
                 partner_id and partner.property_account_payable.id),
             'payment_system_id': payment_sys_id.id,
             'payment_system_term_ids': extra_vals,
+            'date_cutoff': self.date_cutoff,
         })
         return res
diff --git a/wizard/ranch_purchase_data_invoice_wizard_view.xml b/wizard/ranch_purchase_data_invoice_wizard_view.xml
new file mode 100644
index 0000000..e4dec3a
--- /dev/null
+++ b/wizard/ranch_purchase_data_invoice_wizard_view.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data>
+
+        <record id="view_ranch_purchase_data_invoice_wizard" model="ir.ui.view">
+            <field name="name">ranch.purchase.data.invoice.wizard.form</field>
+            <field name="model">ranch.purchase.data.invoice.wizard</field>
+            <field name="inherit_id"
+                ref="rafalim_cattle_account.view_ranch_purchase_data_invoice_wizard"/>
+            <field name="priority" eval="10"/>
+            <field name="arch" type="xml">
+                <field name="invoice_date" position="after">
+                    <field name="date_cutoff" required="True"/>
+                </field>
+            </field>
+        </record>   
+
+    </data>
+</openerp>
diff --git a/wizard/ranch_purchase_data_invoice_wizardd.py b/wizard/ranch_purchase_data_invoice_wizardd.py
index 0a99489..f43da4b 100644
--- a/wizard/ranch_purchase_data_invoice_wizardd.py
+++ b/wizard/ranch_purchase_data_invoice_wizardd.py
@@ -5,9 +5,10 @@
 ###############################################################################
 
 import logging
+from datetime import datetime, timedelta
 
-from openerp import api, models
-
+from openerp import api, models, fields
+from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
 
 _logger = logging.getLogger(__name__)
 
@@ -16,6 +17,8 @@ class PurchaseDataInvoiceWizard(models.TransientModel):
     _name = "ranch.purchase.data.invoice.wizardd"
     _inherit = "ranch.purchase.data.invoice.wizardd"
 
+    date_cutoff = fields.Date(string='Cutoff Date')
+
     @api.multi
     def _adjust_invoice_lines(self, purchase_data, invoice_vals,
                               invoice_line_cattle_ids):
@@ -31,11 +34,22 @@ class PurchaseDataInvoiceWizard(models.TransientModel):
         partner_id = invoice_vals.get('partner_id')
         if partner_id:
             partner = partner_model.browse(partner_id)
+
+        days = min(purchase_data.payment_system_term_ids.mapped('days') or [0])
+        date_invoice = invoice_vals.get('date_invoice', False)
+        date_invoice = date_invoice and datetime.strptime(
+            date_invoice, DEFAULT_SERVER_DATE_FORMAT) or datetime.today()
+
+        invoice_date_due = (date_invoice + timedelta(days=days)).\
+            strftime(DEFAULT_SERVER_DATE_FORMAT)
+
         invoice_vals.update({
             'journal_id': payment_sys_id.journal_id.id,
             'account_id': account and account.id or (
                 partner_id and partner.property_account_payable.id),
             'payment_system_id': payment_sys_id.id,
             'payment_system_term_ids': extra_vals,
+            'date_due': invoice_date_due,
+            'date_cutoff': self.date_cutoff,
         })
         return res
diff --git a/wizard/ranch_purchase_data_invoice_wizardd_view.xml b/wizard/ranch_purchase_data_invoice_wizardd_view.xml
new file mode 100644
index 0000000..9965f1c
--- /dev/null
+++ b/wizard/ranch_purchase_data_invoice_wizardd_view.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data>
+
+        <record id="view_ranch_purchase_data_invoice_wizardd" model="ir.ui.view">
+            <field name="name">ranch.purchase.data.invoice.wizardd.form</field>
+            <field name="model">ranch.purchase.data.invoice.wizardd</field>
+            <field name="inherit_id"
+                ref="rafalim_cattle_account.view_ranch_purchase_data_invoice_wizardd"/>
+            <field name="priority" eval="10"/>
+            <field name="arch" type="xml">
+                <field name="invoice_date" position="after">
+                    <field name="date_cutoff" required="True"/>
+                </field>
+            </field>
+        </record>   
+
+    </data>
+</openerp>
-- 
GitLab


From 4f6eae3c03a063f41ef2b7805ab243d045742737 Mon Sep 17 00:00:00 2001
From: Sebastian Kennedy <skennedy@e-mips.com.ar>
Date: Wed, 7 Nov 2018 19:13:57 -0300
Subject: [PATCH 2/2] [FIX] onchange_payment_system on ranch.purchase.data

---
 models/ranch_purchase_data.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/models/ranch_purchase_data.py b/models/ranch_purchase_data.py
index b238dde..00aec28 100644
--- a/models/ranch_purchase_data.py
+++ b/models/ranch_purchase_data.py
@@ -64,9 +64,13 @@ class RanchPurchaseData(models.Model):
                 supplier_ps = partner.payment_system_ids.filtered(
                     lambda x: self._name in
                     x.payment_system_id.allowed_model_ids.mapped('model'))
+
                 ranch_ps = ranch.payment_system_id
-                if partner and ranch_ps == supplier_ps.payment_system_id:
-                    payment_system_term_ids = supplier_ps.system_term_ids
+                ranch_supplier_ps = supplier_ps.filtered(
+                    lambda x: x.payment_system_id==ranch_ps)
+
+                if partner and ranch_supplier_ps:
+                    payment_system_term_ids = ranch_supplier_ps[0].system_term_ids
                 else:
                     payment_system_term_ids = ranch_ps.default_term_ids
 
-- 
GitLab