Browse Source

commit incial

Rodney Enciso Arias 7 years ago
commit
c9b933ec85
8 changed files with 125 additions and 0 deletions
  1. 2 0
      __init__.py
  2. BIN
      __init__.pyc
  3. 37 0
      __openerp__.py
  4. 2 0
      models/__init__.py
  5. BIN
      models/__init__.pyc
  6. 69 0
      models/contract.py
  7. BIN
      models/contract.pyc
  8. 15 0
      views/contract_view.xml

+ 2 - 0
__init__.py

@@ -0,0 +1,2 @@
+# -*- coding: utf-8 -*-
+from . import models

BIN
__init__.pyc


+ 37 - 0
__openerp__.py

@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+
+###############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2015 Domatix (<www.domatix.com>).
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+###############################################################################
+
+{
+    'name': 'Contract Date',
+    'summary': 'Change date in contracts and their invoices',
+    'version': '8.0.1.0.0',
+    'author': 'Domatix, Odoo Community Association (OCA), Eiru Software / Rodney Enciso Arias',
+    'website': 'http://www.eiru.com.py',
+    'depends': ['account_analytic_analysis'],
+    'category': 'Sales Management',
+    'license': 'AGPL-3',
+    'data': [
+        'views/contract_view.xml',
+    ],
+    'installable': True,
+    'auto_install': False,
+}

+ 2 - 0
models/__init__.py

@@ -0,0 +1,2 @@
+# -*- coding: utf-8 -*-
+from . import contract

BIN
models/__init__.pyc


+ 69 - 0
models/contract.py

@@ -0,0 +1,69 @@
+# -*- coding: utf-8 -*-
+from openerp import models, fields, api
+from dateutil.relativedelta import relativedelta
+import datetime
+import time
+
+class AccountAnalyticAccountDate(models.Model):
+    _inherit = 'account.analytic.account'
+
+    date_invoice = fields.Date('Fecha de la factura')
+
+    @api.model
+    def _prepare_invoice_data(self, contract):
+        date_vals = super(AccountAnalyticAccountDate, self).\
+            _prepare_invoice_data(
+            contract)
+        if contract.date_invoice:
+            date_vals['date_invoice'] = contract.date_invoice
+        return date_vals
+
+    def _recurring_create_invoice(self, cr, uid, ids, automatic=False, context=None):
+        context = context or {}
+        invoice_ids = []
+        current_date =  time.strftime('%Y-%m-%d')
+        if ids:
+            contract_ids = ids
+        else:
+            contract_ids = self.search(cr, uid, [('recurring_next_date','<=', current_date), ('state','=', 'open'), ('recurring_invoices','=', True), ('type', '=', 'contract')])
+        if contract_ids:
+            cr.execute('SELECT company_id, array_agg(id) as ids FROM account_analytic_account WHERE id IN %s GROUP BY company_id', (tuple(contract_ids),))
+            for company_id, ids in cr.fetchall():
+                context_contract = dict(context, company_id=company_id, force_company=company_id)
+                for contract in self.browse(cr, uid, ids, context=context_contract):
+                    try:
+                        invoice_values = self._prepare_invoice(cr, uid, contract, context=context_contract)
+                        invoice_ids.append(self.pool['account.invoice'].create(cr, uid, invoice_values, context=context))
+                        next_date = datetime.datetime.strptime(contract.recurring_next_date or current_date, "%Y-%m-%d")
+                        next_invoice_date = datetime.datetime.strptime(contract.date_invoice or current_date, "%Y-%m-%d")
+                        interval = contract.recurring_interval
+                        if contract.recurring_rule_type == 'daily':
+                            new_date = next_date+relativedelta(days=+interval)
+                        elif contract.recurring_rule_type == 'weekly':
+                            new_date = next_date+relativedelta(weeks=+interval)
+                        elif contract.recurring_rule_type == 'monthly':
+                            new_date = next_date+relativedelta(months=+interval)
+                        else:
+                            new_date = next_date+relativedelta(years=+interval)
+                        #calcula new invoice date
+                        if contract.recurring_rule_type == 'daily':
+                            new_invoice_date = next_invoice_date+relativedelta(days=+interval)
+                        elif contract.recurring_rule_type == 'weekly':
+                            new_invoice_date = next_invoice_date+relativedelta(weeks=+interval)
+                        elif contract.recurring_rule_type == 'monthly':
+                            new_invoice_date = next_invoice_date+relativedelta(months=+interval)
+                        else:
+                            new_invoice_date = next_invoice_date+relativedelta(years=+interval)
+                        #recurring_next_date
+                        self.write(cr, uid, [contract.id], {'recurring_next_date': new_date.strftime('%Y-%m-%d')}, context=context)
+                        #date invoice
+                        self.write(cr, uid, [contract.id], {'date_invoice': new_invoice_date.strftime('%Y-%m-%d')}, context=context)
+                        if automatic:
+                            cr.commit()
+                    except Exception:
+                        if automatic:
+                            cr.rollback()
+                            _logger.exception('Fail to create recurring invoice for contract %s', contract.code)
+                        else:
+                            raise
+        return invoice_ids

BIN
models/contract.pyc


+ 15 - 0
views/contract_view.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0"?>
+<openerp>
+    <data>
+        <record id="account_analytic_account_journal_form" model="ir.ui.view">
+            <field name="name">account.analytic.account.journal.form</field>
+            <field name="model">account.analytic.account</field>
+            <field name="inherit_id" ref="analytic.view_account_analytic_account_form" />
+            <field name="arch" type="xml">
+                <xpath expr="//field[@name='template_id']" position="before">
+                    <field name="date_invoice" />
+                </xpath>
+            </field>
+        </record>
+    </data>
+</openerp>