Carlos Incaser
9 years ago
10 changed files with 451 additions and 365 deletions
-
56contract/README.rst
-
22contract/__init__.py
-
44contract/__openerp__.py
-
262contract/account_analytic_analysis_recurring.py
-
44contract/account_analytic_analysis_recurring_view.xml
-
0contract/data/contract_cron.xml
-
3contract/models/__init__.py
-
256contract/models/contract.py
-
26contract/models/invoice.py
-
103contract/views/contract.xml
@ -0,0 +1,56 @@ |
|||||
|
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg |
||||
|
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html |
||||
|
:alt: License: AGPL-3 |
||||
|
|
||||
|
======== |
||||
|
Contract |
||||
|
======== |
||||
|
|
||||
|
This module helps you to manage contracts with recurring invoices. |
||||
|
|
||||
|
Usage |
||||
|
===== |
||||
|
|
||||
|
To use this module, you need to: |
||||
|
|
||||
|
|
||||
|
.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas |
||||
|
:alt: Try me on Runbot |
||||
|
:target: https://runbot.odoo-community.org/runbot/167/8.0 |
||||
|
|
||||
|
For further information, please visit: |
||||
|
|
||||
|
* https://www.odoo.com/forum/help-1 |
||||
|
|
||||
|
Bug Tracker |
||||
|
=========== |
||||
|
|
||||
|
Bugs are tracked on `GitHub Issues <https://github.com/OCA/contract/issues>`_. |
||||
|
In case of trouble, please check there if your issue has already been reported. |
||||
|
If you spotted it first, help us smashing it by providing a detailed and welcomed feedback |
||||
|
`here <https://github.com/OCA/contract/issues/new?body=module:%20contract%0Aversion:%208.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. |
||||
|
|
||||
|
Credits |
||||
|
======= |
||||
|
|
||||
|
Contributors |
||||
|
------------ |
||||
|
|
||||
|
* Pedro M. Baeza <pedro.baeza@serviciosbaeza.com> |
||||
|
* Carlos Dauden <carlos@incaser.es> |
||||
|
* Angel Moya <angel.moya@domatix.com> |
||||
|
|
||||
|
Maintainer |
||||
|
---------- |
||||
|
|
||||
|
.. image:: https://odoo-community.org/logo.png |
||||
|
:alt: Odoo Community Association |
||||
|
:target: https://odoo-community.org |
||||
|
|
||||
|
This module is maintained by the OCA. |
||||
|
|
||||
|
OCA, or the Odoo Community Association, is a nonprofit organization whose |
||||
|
mission is to support the collaborative development of Odoo features and |
||||
|
promote its widespread use. |
||||
|
|
||||
|
To contribute to this module, please visit https://odoo-community.org. |
@ -1,22 +1,2 @@ |
|||||
# -*- coding: utf-8 -*- |
# -*- coding: utf-8 -*- |
||||
############################################################################## |
|
||||
# |
|
||||
# OpenERP, Open Source Management Solution |
|
||||
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>) |
|
||||
# |
|
||||
# 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/>. |
|
||||
# |
|
||||
############################################################################## |
|
||||
|
|
||||
from . import account_analytic_analysis_recurring |
|
||||
|
from . import models |
@ -1,51 +1,19 @@ |
|||||
# -*- coding: utf-8 -*- |
# -*- coding: utf-8 -*- |
||||
############################################################################## |
|
||||
# |
|
||||
# OpenERP, Open Source Management Solution |
|
||||
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). |
|
||||
# |
|
||||
# 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/>. |
|
||||
# |
|
||||
############################################################################## |
|
||||
|
|
||||
|
# © 2016 Incaser Informatica S.L. - Carlos Dauden |
||||
|
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html |
||||
|
|
||||
{ |
{ |
||||
'name': 'Contracts Management recurring', |
'name': 'Contracts Management recurring', |
||||
'version': '0.1', |
|
||||
|
'version': '9.0.1.0.0', |
||||
'category': 'Other', |
'category': 'Other', |
||||
'description': """ |
|
||||
This module adds a new feature in contracts to manage recurring invoicing |
|
||||
========================================================================= |
|
||||
|
|
||||
This is a backport of the new V8 feature available in trunk and saas. With |
|
||||
the V8 release this module will be deprecated. |
|
||||
|
|
||||
It also adds a little feature, you can use #START# and #END# in the contract |
|
||||
line description to automatically insert the dates of the invoiced period. |
|
||||
|
|
||||
Backport done By Yannick Buron. |
|
||||
""", |
|
||||
'author': "OpenERP SA,Odoo Community Association (OCA)", |
'author': "OpenERP SA,Odoo Community Association (OCA)", |
||||
'website': 'http://openerp.com', |
'website': 'http://openerp.com', |
||||
'depends': ['base', 'account_analytic_analysis'], |
|
||||
|
'depends': ['base', 'account', 'analytic'], |
||||
'data': [ |
'data': [ |
||||
'security/ir.model.access.csv', |
'security/ir.model.access.csv', |
||||
'account_analytic_analysis_recurring_cron.xml', |
|
||||
'account_analytic_analysis_recurring_view.xml', |
|
||||
|
'data/contract_cron.xml', |
||||
|
'views/contract.xml', |
||||
], |
], |
||||
'demo': [], |
|
||||
'test': [], |
|
||||
'installable': True, |
'installable': True, |
||||
'images': [], |
'images': [], |
||||
} |
} |
@ -1,262 +0,0 @@ |
|||||
# -*- coding: utf-8 -*- |
|
||||
############################################################################## |
|
||||
# |
|
||||
# OpenERP, Open Source Management Solution |
|
||||
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>). |
|
||||
# |
|
||||
# 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/>. |
|
||||
# |
|
||||
############################################################################## |
|
||||
from dateutil.relativedelta import relativedelta |
|
||||
import datetime |
|
||||
import logging |
|
||||
import time |
|
||||
|
|
||||
from openerp.osv import orm, fields |
|
||||
from openerp.tools.translate import _ |
|
||||
from openerp.addons.decimal_precision import decimal_precision as dp |
|
||||
|
|
||||
_logger = logging.getLogger(__name__) |
|
||||
|
|
||||
|
|
||||
class AccountAnalyticInvoiceLine(orm.Model): |
|
||||
_name = "account.analytic.invoice.line" |
|
||||
|
|
||||
def _amount_line( |
|
||||
self, cr, uid, ids, prop, unknow_none, unknow_dict, context=None): |
|
||||
res = {} |
|
||||
for line in self.browse(cr, uid, ids, context=context): |
|
||||
res[line.id] = line.quantity * line.price_unit |
|
||||
if line.analytic_account_id.pricelist_id: |
|
||||
cur = line.analytic_account_id.pricelist_id.currency_id |
|
||||
res[line.id] = self.pool.get('res.currency').round( |
|
||||
cr, uid, cur, res[line.id]) |
|
||||
return res |
|
||||
|
|
||||
_columns = { |
|
||||
'product_id': fields.many2one( |
|
||||
'product.product', 'Product', required=True), |
|
||||
'analytic_account_id': fields.many2one( |
|
||||
'account.analytic.account', 'Analytic Account'), |
|
||||
'name': fields.text('Description', required=True), |
|
||||
'quantity': fields.float('Quantity', required=True), |
|
||||
'uom_id': fields.many2one( |
|
||||
'product.uom', 'Unit of Measure', required=True), |
|
||||
'price_unit': fields.float('Unit Price', required=True), |
|
||||
'price_subtotal': fields.function( |
|
||||
_amount_line, string='Sub Total', |
|
||||
type="float", digits_compute=dp.get_precision('Account')), |
|
||||
} |
|
||||
_defaults = { |
|
||||
'quantity': 1, |
|
||||
} |
|
||||
|
|
||||
def product_id_change( |
|
||||
self, cr, uid, ids, product, uom_id, qty=0, name='', |
|
||||
partner_id=False, price_unit=False, pricelist_id=False, |
|
||||
company_id=None, context=None): |
|
||||
context = context or {} |
|
||||
uom_obj = self.pool.get('product.uom') |
|
||||
company_id = company_id or False |
|
||||
context.update( |
|
||||
{'company_id': company_id, |
|
||||
'force_company': company_id, |
|
||||
'pricelist_id': pricelist_id}) |
|
||||
|
|
||||
if not product: |
|
||||
return { |
|
||||
'value': {'price_unit': 0.0}, |
|
||||
'domain': {'product_uom': []}} |
|
||||
if partner_id: |
|
||||
part = self.pool.get('res.partner').browse( |
|
||||
cr, uid, partner_id, context=context) |
|
||||
if part.lang: |
|
||||
context.update({'lang': part.lang}) |
|
||||
|
|
||||
result = {} |
|
||||
res = self.pool.get('product.product').browse( |
|
||||
cr, uid, product, context=context) |
|
||||
result.update( |
|
||||
{'name': res.partner_ref or False, |
|
||||
'uom_id': uom_id or res.uom_id.id or False, |
|
||||
'price_unit': res.list_price or 0.0}) |
|
||||
if res.description: |
|
||||
result['name'] += '\n' + res.description |
|
||||
|
|
||||
res_final = {'value': result} |
|
||||
if result['uom_id'] != res.uom_id.id: |
|
||||
new_price = uom_obj._compute_price( |
|
||||
cr, uid, res.uom_id.id, |
|
||||
res_final['value']['price_unit'], result['uom_id']) |
|
||||
res_final['value']['price_unit'] = new_price |
|
||||
return res_final |
|
||||
|
|
||||
|
|
||||
class AccountAnalyticAccount(orm.Model): |
|
||||
_name = "account.analytic.account" |
|
||||
_inherit = "account.analytic.account" |
|
||||
|
|
||||
_columns = { |
|
||||
'recurring_invoice_line_ids': fields.one2many( |
|
||||
'account.analytic.invoice.line', 'analytic_account_id', |
|
||||
'Invoice Lines'), |
|
||||
'recurring_invoices': fields.boolean( |
|
||||
'Generate recurring invoices automatically'), |
|
||||
'recurring_rule_type': fields.selection( |
|
||||
[('daily', 'Day(s)'), |
|
||||
('weekly', 'Week(s)'), |
|
||||
('monthly', 'Month(s)'), |
|
||||
('yearly', 'Year(s)'), |
|
||||
], 'Recurrency', |
|
||||
help="Invoice automatically repeat at specified interval"), |
|
||||
'recurring_interval': fields.integer( |
|
||||
'Repeat Every', help="Repeat every (Days/Week/Month/Year)"), |
|
||||
'recurring_next_date': fields.date('Date of Next Invoice'), |
|
||||
} |
|
||||
|
|
||||
_defaults = { |
|
||||
'recurring_interval': 1, |
|
||||
'recurring_next_date': lambda *a: time.strftime('%Y-%m-%d'), |
|
||||
'recurring_rule_type': 'monthly' |
|
||||
} |
|
||||
|
|
||||
def copy(self, cr, uid, id, default=None, context=None): |
|
||||
# Reset next invoice date |
|
||||
default['recurring_next_date'] = \ |
|
||||
self._defaults['recurring_next_date']() |
|
||||
return super(AccountAnalyticAccount, self).copy( |
|
||||
cr, uid, id, default=default, context=context) |
|
||||
|
|
||||
def onchange_recurring_invoices( |
|
||||
self, cr, uid, ids, recurring_invoices, |
|
||||
date_start=False, context=None): |
|
||||
value = {} |
|
||||
if date_start and recurring_invoices: |
|
||||
value = {'value': {'recurring_next_date': date_start}} |
|
||||
return value |
|
||||
|
|
||||
def _prepare_invoice_line(self, cr, uid, line, invoice_id, context=None): |
|
||||
fpos_obj = self.pool['account.fiscal.position'] |
|
||||
lang_obj = self.pool['res.lang'] |
|
||||
product = line.product_id |
|
||||
account_id = product.property_account_income.id |
|
||||
if not account_id: |
|
||||
account_id = product.categ_id.property_account_income_categ.id |
|
||||
contract = line.analytic_account_id |
|
||||
fpos = contract.partner_id.property_account_position or False |
|
||||
account_id = fpos_obj.map_account(cr, uid, fpos, account_id) |
|
||||
taxes = product.taxes_id or False |
|
||||
tax_id = fpos_obj.map_tax(cr, uid, fpos, taxes) |
|
||||
if 'old_date' in context: |
|
||||
lang_ids = lang_obj.search( |
|
||||
cr, uid, [('code', '=', contract.partner_id.lang)], |
|
||||
context=context) |
|
||||
format = lang_obj.browse( |
|
||||
cr, uid, lang_ids, context=context)[0].date_format |
|
||||
line.name = line.name.replace( |
|
||||
'#START#', context['old_date'].strftime(format)) |
|
||||
line.name = line.name.replace( |
|
||||
'#END#', context['next_date'].strftime(format)) |
|
||||
return { |
|
||||
'name': line.name, |
|
||||
'account_id': account_id, |
|
||||
'account_analytic_id': contract.id, |
|
||||
'price_unit': line.price_unit or 0.0, |
|
||||
'quantity': line.quantity, |
|
||||
'uos_id': line.uom_id.id or False, |
|
||||
'product_id': line.product_id.id or False, |
|
||||
'invoice_id': invoice_id, |
|
||||
'invoice_line_tax_id': [(6, 0, tax_id)], |
|
||||
} |
|
||||
|
|
||||
def _prepare_invoice(self, cr, uid, contract, context=None): |
|
||||
if context is None: |
|
||||
context = {} |
|
||||
inv_obj = self.pool['account.invoice'] |
|
||||
journal_obj = self.pool['account.journal'] |
|
||||
if not contract.partner_id: |
|
||||
raise orm.except_orm( |
|
||||
_('No Customer Defined!'), |
|
||||
_("You must first select a Customer for Contract %s!") % |
|
||||
contract.name) |
|
||||
partner = contract.partner_id |
|
||||
fpos = partner.property_account_position or False |
|
||||
journal_ids = journal_obj.search( |
|
||||
cr, uid, |
|
||||
[('type', '=', 'sale'), |
|
||||
('company_id', '=', contract.company_id.id or False)], |
|
||||
limit=1) |
|
||||
if not journal_ids: |
|
||||
raise orm.except_orm( |
|
||||
_('Error!'), |
|
||||
_('Please define a sale journal for the company "%s".') % |
|
||||
(contract.company_id.name or '',)) |
|
||||
partner_payment_term = partner.property_payment_term.id |
|
||||
inv_data = { |
|
||||
'reference': contract.code or False, |
|
||||
'account_id': partner.property_account_receivable.id, |
|
||||
'type': 'out_invoice', |
|
||||
'partner_id': partner.id, |
|
||||
'currency_id': partner.property_product_pricelist.currency_id.id, |
|
||||
'journal_id': len(journal_ids) and journal_ids[0] or False, |
|
||||
'date_invoice': contract.recurring_next_date, |
|
||||
'origin': contract.name, |
|
||||
'fiscal_position': fpos and fpos.id, |
|
||||
'payment_term': partner_payment_term, |
|
||||
'company_id': contract.company_id.id or False, |
|
||||
} |
|
||||
invoice_id = inv_obj.create(cr, uid, inv_data, context=context) |
|
||||
for line in contract.recurring_invoice_line_ids: |
|
||||
invoice_line_vals = self._prepare_invoice_line( |
|
||||
cr, uid, line, invoice_id, context=context) |
|
||||
self.pool['account.invoice.line'].create( |
|
||||
cr, uid, invoice_line_vals, context=context) |
|
||||
inv_obj.button_compute(cr, uid, [invoice_id], context=context) |
|
||||
return invoice_id |
|
||||
|
|
||||
def recurring_create_invoice(self, cr, uid, automatic=False, context=None): |
|
||||
if context is None: |
|
||||
context = {} |
|
||||
current_date = time.strftime('%Y-%m-%d') |
|
||||
contract_ids = self.search( |
|
||||
cr, uid, |
|
||||
[('recurring_next_date', '<=', current_date), |
|
||||
('state', '=', 'open'), |
|
||||
('recurring_invoices', '=', True)]) |
|
||||
for contract in self.browse(cr, uid, contract_ids, context=context): |
|
||||
next_date = datetime.datetime.strptime( |
|
||||
contract.recurring_next_date or current_date, "%Y-%m-%d") |
|
||||
interval = contract.recurring_interval |
|
||||
old_date = next_date |
|
||||
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) |
|
||||
else: |
|
||||
new_date = next_date + relativedelta(months=+interval) |
|
||||
context['old_date'] = old_date |
|
||||
context['next_date'] = new_date |
|
||||
# Force company for correct evaluate domain access rules |
|
||||
context['force_company'] = contract.company_id.id |
|
||||
# Re-read contract with correct company |
|
||||
contract = self.browse(cr, uid, contract.id, context=context) |
|
||||
self._prepare_invoice( |
|
||||
cr, uid, contract, context=context |
|
||||
) |
|
||||
self.write( |
|
||||
cr, uid, [contract.id], |
|
||||
{'recurring_next_date': new_date.strftime('%Y-%m-%d')}, |
|
||||
context=context |
|
||||
) |
|
||||
return True |
|
@ -1,44 +0,0 @@ |
|||||
<?xml version="1.0" encoding="utf-8"?> |
|
||||
<openerp> |
|
||||
<data> |
|
||||
|
|
||||
<record id="account_analytic_account_recurring_form_form" model="ir.ui.view"> |
|
||||
<field name="name">account.analytic.account.invoice.recurring.form.inherit</field> |
|
||||
<field name="model">account.analytic.account</field> |
|
||||
<field name="inherit_id" ref="account_analytic_analysis.account_analytic_account_form_form"/> |
|
||||
<field eval="40" name="priority"/> |
|
||||
<field name="arch" type="xml"> |
|
||||
<group name='invoice_on_timesheets' position='after'> |
|
||||
<separator string="Recurring Invoices" attrs="{'invisible': [('recurring_invoices','!=',True)]}"/> |
|
||||
<div> |
|
||||
<field name="recurring_invoices" on_change="onchange_recurring_invoices(recurring_invoices, date_start)" class="oe_inline"/> |
|
||||
<label for="recurring_invoices" /> |
|
||||
<button class="oe_link" name="recurring_create_invoice" attrs="{'invisible': [('recurring_invoices','!=',True)]}" string=". create invoices" type="object" groups="base.group_no_one"/> |
|
||||
</div> |
|
||||
<group attrs="{'invisible': [('recurring_invoices','!=',True)]}"> |
|
||||
<label for="recurring_interval"/> |
|
||||
<div> |
|
||||
<field name="recurring_interval" class="oe_inline" attrs="{'required': [('recurring_invoices', '=', True)]}"/> |
|
||||
<field name="recurring_rule_type" class="oe_inline" attrs="{'required': [('recurring_invoices', '=', True)]}"/> |
|
||||
</div> |
|
||||
<field name="recurring_next_date"/> |
|
||||
</group> |
|
||||
<label for="recurring_invoice_line_ids" attrs="{'invisible': [('recurring_invoices','=',False)]}"/> |
|
||||
<div attrs="{'invisible': [('recurring_invoices','=',False)]}"> |
|
||||
<field name="recurring_invoice_line_ids"> |
|
||||
<tree string="Account Analytic Lines" editable="bottom"> |
|
||||
<field name="product_id" on_change="product_id_change(product_id, uom_id, quantity, name, parent.partner_id, price_unit, parent.pricelist_id, parent.company_id)"/> |
|
||||
<field name="name"/> |
|
||||
<field name="quantity"/> |
|
||||
<field name="uom_id"/> |
|
||||
<field name="price_unit"/> |
|
||||
<field name="price_subtotal"/> |
|
||||
</tree> |
|
||||
</field> |
|
||||
</div> |
|
||||
</group> |
|
||||
</field> |
|
||||
</record> |
|
||||
|
|
||||
</data> |
|
||||
</openerp> |
|
@ -0,0 +1,3 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
from . import contract |
||||
|
from . import invoice |
@ -0,0 +1,256 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# © 2016 Incaser Informatica S.L. - Carlos Dauden |
||||
|
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html |
||||
|
|
||||
|
from dateutil.relativedelta import relativedelta |
||||
|
import logging |
||||
|
import time |
||||
|
|
||||
|
from openerp import api, exceptions, fields, models |
||||
|
from openerp.addons.decimal_precision import decimal_precision as dp |
||||
|
from openerp.exceptions import ValidationError |
||||
|
from openerp.tools.translate import _ |
||||
|
|
||||
|
_logger = logging.getLogger(__name__) |
||||
|
|
||||
|
|
||||
|
class AccountAnalyticInvoiceLine(models.Model): |
||||
|
_name = "account.analytic.invoice.line" |
||||
|
|
||||
|
product_id = fields.Many2one( |
||||
|
'product.product', string='Product', required=True) |
||||
|
analytic_account_id = fields.Many2one( |
||||
|
'account.analytic.account', string='Analytic Account') |
||||
|
name = fields.Text(string='Description', required=True) |
||||
|
quantity = fields.Float(default=1.0, required=True) |
||||
|
uom_id = fields.Many2one( |
||||
|
'product.uom', string='Unit of Measure', required=True) |
||||
|
price_unit = fields.Float('Unit Price', required=True) |
||||
|
price_subtotal = fields.Float( |
||||
|
compute='_compute_price_subtotal', |
||||
|
digits_compute=dp.get_precision('Account'), |
||||
|
string='Sub Total') |
||||
|
discount = fields.Float( |
||||
|
string='Discount (%)', |
||||
|
digits=dp.get_precision('Discount'), |
||||
|
copy=True, |
||||
|
help='Discount that is applied in generated invoices.' |
||||
|
' It should be less or equal to 100') |
||||
|
|
||||
|
@api.multi |
||||
|
@api.depends('quantity', 'price_unit', 'discount') |
||||
|
def _compute_price_subtotal(self): |
||||
|
for line in self: |
||||
|
subtotal = line.quantity * line.price_unit |
||||
|
discount = line.discount / 100 |
||||
|
subtotal *= 1 - discount |
||||
|
if line.analytic_account_id.pricelist_id: |
||||
|
cur = line.analytic_account_id.pricelist_id.currency_id |
||||
|
line.price_subtotal = cur.round(subtotal) |
||||
|
else: |
||||
|
line.price_subtotal = subtotal |
||||
|
|
||||
|
@api.one |
||||
|
@api.constrains('discount') |
||||
|
def _check_discount(self): |
||||
|
if self.discount > 100: |
||||
|
raise ValidationError(_("Discount should be less or equal to 100")) |
||||
|
|
||||
|
@api.multi |
||||
|
@api.onchange('product_id') |
||||
|
def product_id_change(self): |
||||
|
if not self.product_id: |
||||
|
return {'domain': {'uom_id': []}} |
||||
|
|
||||
|
vals = {} |
||||
|
domain = {'uom_id': [ |
||||
|
('category_id', '=', self.product_id.uom_id.category_id.id)]} |
||||
|
if not self.uom_id or (self.product_id.uom_id.category_id.id != self.uom_id.category_id.id): |
||||
|
vals['uom_id'] = self.product_id.uom_id |
||||
|
|
||||
|
product = self.product_id.with_context( |
||||
|
lang=self.analytic_account_id.partner_id.lang, |
||||
|
partner=self.analytic_account_id.partner_id.id, |
||||
|
quantity=self.quantity, |
||||
|
date=self.analytic_account_id.recurring_next_date, |
||||
|
pricelist=self.analytic_account_id.pricelist_id.id, |
||||
|
uom=self.uom_id.id |
||||
|
) |
||||
|
|
||||
|
name = product.name_get()[0][1] |
||||
|
if product.description_sale: |
||||
|
name += '\n' + product.description_sale |
||||
|
vals['name'] = name |
||||
|
|
||||
|
vals['price_unit'] = product.price |
||||
|
self.update(vals) |
||||
|
return {'domain': domain} |
||||
|
|
||||
|
|
||||
|
class AccountAnalyticAccount(models.Model): |
||||
|
_inherit = 'account.analytic.account' |
||||
|
|
||||
|
@api.model |
||||
|
def _default_journal(self): |
||||
|
company_id = self.env.context.get( |
||||
|
'company_id', self.env.user.company_id.id) |
||||
|
domain = [ |
||||
|
('type', '=', 'sale'), |
||||
|
('company_id', '=', company_id)] |
||||
|
return self.env['account.journal'].search(domain, limit=1) |
||||
|
|
||||
|
pricelist_id = fields.Many2one( |
||||
|
comodel_name='product.pricelist', |
||||
|
string='Pricelist') |
||||
|
date_start = fields.Date(default=fields.Date.context_today) |
||||
|
recurring_invoice_line_ids = fields.One2many( |
||||
|
comodel_name='account.analytic.invoice.line', |
||||
|
inverse_name='analytic_account_id', |
||||
|
string='Invoice Lines') |
||||
|
recurring_invoices = fields.Boolean( |
||||
|
string='Generate recurring invoices automatically') |
||||
|
recurring_rule_type = fields.Selection( |
||||
|
[('daily', 'Day(s)'), |
||||
|
('weekly', 'Week(s)'), |
||||
|
('monthly', 'Month(s)'), |
||||
|
('yearly', 'Year(s)'), |
||||
|
], |
||||
|
default='monthly', |
||||
|
string='Recurrency', |
||||
|
help="Invoice automatically repeat at specified interval") |
||||
|
recurring_interval = fields.Integer( |
||||
|
default=1, |
||||
|
string='Repeat Every', |
||||
|
help="Repeat every (Days/Week/Month/Year)") |
||||
|
recurring_next_date = fields.Date( |
||||
|
default=fields.Date.context_today, |
||||
|
string='Date of Next Invoice') |
||||
|
journal_id = fields.Many2one( |
||||
|
'account.journal', |
||||
|
string='Journal', |
||||
|
default=_default_journal, |
||||
|
domain="[('type', '=', 'sale'),('company_id', '=', company_id)]") |
||||
|
|
||||
|
def copy(self, default=None): |
||||
|
# Reset next invoice date |
||||
|
default['recurring_next_date'] = \ |
||||
|
self._defaults['recurring_next_date']() |
||||
|
return super(AccountAnalyticAccount, self).copy(default=default) |
||||
|
|
||||
|
@api.onchange('recurring_invoices') |
||||
|
def _onchange_recurring_invoices(self): |
||||
|
if self.date_start and self.recurring_invoices: |
||||
|
self.recurring_next_date = self.date_start |
||||
|
|
||||
|
@api.model |
||||
|
def _insert_markers(self, line, date_start, date_end, date_format): |
||||
|
line = line.replace('#START#', date_start.strftime(date_format)) |
||||
|
line = line.replace('#END#', date_end.strftime(date_format)) |
||||
|
return line |
||||
|
|
||||
|
@api.model |
||||
|
def _prepare_invoice_line(self, line, invoice_id): |
||||
|
product = line.product_id |
||||
|
account_id = product.property_account_income_id.id or \ |
||||
|
product.categ_id.property_account_income_categ_id.id |
||||
|
contract = line.analytic_account_id |
||||
|
fpos = contract.partner_id.property_account_position_id |
||||
|
account_id = fpos.map_account(account_id) |
||||
|
tax_id = fpos.map_tax(product.taxes_id) |
||||
|
name = line.name |
||||
|
if 'old_date' in self.env.context and 'next_date' in self.env.context: |
||||
|
lang_obj = self.env['res.lang'] |
||||
|
contract = line.analytic_account_id |
||||
|
lang = lang_obj.search( |
||||
|
[('code', '=', contract.partner_id.lang)]) |
||||
|
date_format = lang.date_format or '%m/%d/%Y' |
||||
|
name = self._insert_markers( |
||||
|
name, self.env.context['old_date'], |
||||
|
self.env.context['next_date'], date_format) |
||||
|
return { |
||||
|
'name': name, |
||||
|
'account_id': account_id, |
||||
|
'account_analytic_id': contract.id, |
||||
|
'price_unit': line.price_unit, |
||||
|
'quantity': line.quantity, |
||||
|
'uos_id': line.uom_id.id, |
||||
|
'product_id': line.product_id.id, |
||||
|
'invoice_id': invoice_id, |
||||
|
'invoice_line_tax_id': [(6, 0, tax_id)], |
||||
|
'discount': line.discount, |
||||
|
} |
||||
|
|
||||
|
@api.model |
||||
|
def _prepare_invoice(self, contract): |
||||
|
if not contract.partner_id: |
||||
|
raise ValidationError( |
||||
|
_('No Customer Defined!'), |
||||
|
_("You must first select a Customer for Contract %s!") % |
||||
|
contract.name) |
||||
|
partner = contract.partner_id |
||||
|
fpos = partner.property_account_position_id |
||||
|
journal_ids = self.env['account.journal'].search( |
||||
|
[('type', '=', 'sale'), |
||||
|
('company_id', '=', contract.company_id.id)], |
||||
|
limit=1) |
||||
|
if not journal_ids: |
||||
|
raise ValidationError( |
||||
|
_('Error!'), |
||||
|
_('Please define a sale journal for the company "%s".') % |
||||
|
(contract.company_id.name or '',)) |
||||
|
inv_data = { |
||||
|
'reference': contract.code, |
||||
|
'account_id': partner.property_account_receivable_id.id, |
||||
|
'type': 'out_invoice', |
||||
|
'partner_id': partner.id, |
||||
|
'currency_id': partner.property_product_pricelist.currency_id.id, |
||||
|
'journal_id': journal_ids.id, |
||||
|
'date_invoice': contract.recurring_next_date, |
||||
|
'origin': contract.name, |
||||
|
'fiscal_position': fpos and fpos.id, |
||||
|
'payment_term': partner.property_payment_term_id.id, |
||||
|
'company_id': contract.company_id.id, |
||||
|
'journal_id': contract.journal_id.id, |
||||
|
'contract_id': contract.id, |
||||
|
} |
||||
|
# if contract.journal_id: |
||||
|
# inv_data['journal_id'] = contract.journal_id.id |
||||
|
invoice = self.env['account.invoice'].create(inv_data) |
||||
|
for line in contract.recurring_invoice_line_ids: |
||||
|
invoice_line_vals = self._prepare_invoice_line(line, invoice.id) |
||||
|
self.env['account.invoice.line'].create(invoice_line_vals) |
||||
|
# invoice.button_compute() |
||||
|
return invoice |
||||
|
|
||||
|
@api.model |
||||
|
def recurring_create_invoice(self, automatic=False): |
||||
|
current_date = time.strftime('%Y-%m-%d') |
||||
|
contracts = self.search( |
||||
|
[('recurring_next_date', '<=', current_date), |
||||
|
('account_type', '=', 'normal'), |
||||
|
('recurring_invoices', '=', True)]) |
||||
|
for contract in contracts: |
||||
|
next_date = fields.Date.from_string( |
||||
|
contract.recurring_next_date or fields.Date.today()) |
||||
|
interval = contract.recurring_interval |
||||
|
old_date = next_date |
||||
|
if contract.recurring_rule_type == 'daily': |
||||
|
new_date = next_date + relativedelta(days=interval - 1) |
||||
|
elif contract.recurring_rule_type == 'weekly': |
||||
|
new_date = next_date + relativedelta(weeks=interval, days=-1) |
||||
|
else: |
||||
|
new_date = next_date + relativedelta(months=interval, days=-1) |
||||
|
ctx = self.env.context.copy() |
||||
|
ctx.update({ |
||||
|
'old_date': old_date, |
||||
|
'next_date': new_date, |
||||
|
# Force company for correct evaluate domain access rules |
||||
|
'force_company': contract.company_id.id, |
||||
|
}) |
||||
|
# Re-read contract with correct company |
||||
|
contract = self.with_context(ctx).browse(contract.id) |
||||
|
self.with_context(ctx)._prepare_invoice(contract) |
||||
|
contract.write({ |
||||
|
'recurring_next_date': new_date.strftime('%Y-%m-%d') |
||||
|
}) |
||||
|
return True |
@ -0,0 +1,26 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# © 2016 Incaser Informatica S.L. - Carlos Dauden |
||||
|
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html |
||||
|
|
||||
|
from openerp import api, fields, models |
||||
|
|
||||
|
|
||||
|
class AccountInvoice(models.Model): |
||||
|
_inherit = 'account.invoice' |
||||
|
|
||||
|
contract_id = fields.Many2one( |
||||
|
'account.analytic.account', |
||||
|
string='Contract') |
||||
|
analytic_account_ids = fields.Many2many( |
||||
|
comodel_name='account.analytic.account', |
||||
|
compute='_compute_analytic_account_ids', |
||||
|
store=True, |
||||
|
string='Contracts') |
||||
|
|
||||
|
@api.multi |
||||
|
@api.depends('invoice_line_ids.account_analytic_id') |
||||
|
def _compute_analytic_account_ids(self): |
||||
|
for invoice in self: |
||||
|
invoice.analytic_account_ids = invoice.mapped( |
||||
|
'invoice_line_ids.account_analytic_id' |
||||
|
) |
@ -0,0 +1,103 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<openerp> |
||||
|
<data> |
||||
|
|
||||
|
<record id="act_recurring_invoices" model="ir.actions.act_window"> |
||||
|
<field name="context">{'search_default_contract_id': |
||||
|
[active_id], |
||||
|
'default_contract_id': active_id} |
||||
|
</field> |
||||
|
<field name="name">Invoices</field> |
||||
|
<field name="res_model">account.invoice</field> |
||||
|
<field name="view_id" ref="account.invoice_tree" /> |
||||
|
<field name="search_view_id" ref="account.view_account_invoice_filter"/> |
||||
|
</record> |
||||
|
|
||||
|
<record id="act_analytic_invoices" model="ir.actions.act_window"> |
||||
|
<field name="context">{'search_default_analytic_account_ids': |
||||
|
[active_id], 'default_analytic_account_ids': active_id}</field> |
||||
|
<field name="name">Invoices</field> |
||||
|
<field name="res_model">account.invoice</field> |
||||
|
<field name="domain">[('analytic_account_ids','=',active_id)]</field> |
||||
|
<field name="view_id" ref="account.invoice_tree" /> |
||||
|
<field name="search_view_id" ref="account.view_account_invoice_filter" /> |
||||
|
</record> |
||||
|
|
||||
|
<record id="account_analytic_account_recurring_form_form" model="ir.ui.view"> |
||||
|
<field name="name">account.analytic.account.invoice.recurring.form.inherit</field> |
||||
|
<field name="model">account.analytic.account</field> |
||||
|
<field name="inherit_id" ref="analytic.view_account_analytic_account_form"/> |
||||
|
<field eval="40" name="priority"/> |
||||
|
<field name="arch" type="xml"> |
||||
|
<xpath expr='//div[@name="button_box"]' position='inside'> |
||||
|
<button class="oe_stat_button" type="action" icon="fa-edit" |
||||
|
name="%(contract.act_analytic_invoices)d" |
||||
|
string="Invoices" help="Invoices related with this contract"> |
||||
|
</button> |
||||
|
</xpath> |
||||
|
<notebook position="before"> |
||||
|
<separator string="Recurring Invoices" attrs="{'invisible': [('recurring_invoices','!=',True)]}"/> |
||||
|
<div> |
||||
|
<field name="recurring_invoices" class="oe_inline"/> |
||||
|
<label for="recurring_invoices" /> |
||||
|
<button class="oe_link" name="recurring_create_invoice" attrs="{'invisible': [('recurring_invoices','!=',True)]}" string="Create invoices" type="object" groups="base.group_no_one"/> |
||||
|
<button attrs="{'invisible': [('recurring_invoices','!=',True)]}" |
||||
|
name="%(contract.act_recurring_invoices)d" |
||||
|
string="⇒ show invoices" class="oe_link" type="action"/> |
||||
|
</div> |
||||
|
<group attrs="{'invisible': [('recurring_invoices','!=',True)]}"> |
||||
|
<field name="journal_id" /> |
||||
|
<field name="pricelist_id"/> |
||||
|
<label for="recurring_interval"/> |
||||
|
<div> |
||||
|
<field name="recurring_interval" class="oe_inline" attrs="{'required': [('recurring_invoices', '=', True)]}"/> |
||||
|
<field name="recurring_rule_type" class="oe_inline" attrs="{'required': [('recurring_invoices', '=', True)]}"/> |
||||
|
</div> |
||||
|
<field name="date_start"/> |
||||
|
<field name="recurring_next_date"/> |
||||
|
</group> |
||||
|
<label for="recurring_invoice_line_ids" attrs="{'invisible': [('recurring_invoices','=',False)]}"/> |
||||
|
<div attrs="{'invisible': [('recurring_invoices','=',False)]}"> |
||||
|
<field name="recurring_invoice_line_ids"> |
||||
|
<tree string="Account Analytic Lines" editable="bottom"> |
||||
|
<field name="product_id"/> |
||||
|
<field name="name"/> |
||||
|
<field name="quantity"/> |
||||
|
<field name="uom_id"/> |
||||
|
<field name="price_unit"/> |
||||
|
<field name="discount" groups="sale.group_discount_per_so_line" /> |
||||
|
<field name="price_subtotal"/> |
||||
|
</tree> |
||||
|
</field> |
||||
|
</div> |
||||
|
</notebook> |
||||
|
</field> |
||||
|
</record> |
||||
|
|
||||
|
<!-- Inherited Analytic Account list for contracts --> |
||||
|
<record id="view_account_analytic_account_journal_tree" model="ir.ui.view"> |
||||
|
<field name="name">account.analytic.account.journal.list</field> |
||||
|
<field name="model">account.analytic.account</field> |
||||
|
<field name="inherit_id" ref="analytic.view_account_analytic_account_list" /> |
||||
|
<field name="arch" type="xml"> |
||||
|
<field name="partner_id" position="before"> |
||||
|
<field name="journal_id" groups="account.group_account_user"/> |
||||
|
</field> |
||||
|
</field> |
||||
|
</record> |
||||
|
|
||||
|
<!-- Analytic Account search view for contract --> |
||||
|
<record id="view_account_analytic_account_journal_search" model="ir.ui.view"> |
||||
|
<field name="name">account.analytic.account.journal.search</field> |
||||
|
<field name="model">account.analytic.account</field> |
||||
|
<field name="inherit_id" |
||||
|
ref="analytic.view_account_analytic_account_search" /> |
||||
|
<field name="arch" type="xml"> |
||||
|
<field name="name" position="before"> |
||||
|
<field name="journal_id"/> |
||||
|
</field> |
||||
|
</field> |
||||
|
</record> |
||||
|
|
||||
|
</data> |
||||
|
</openerp> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue