|
@ -23,15 +23,14 @@ import datetime |
|
|
import logging |
|
|
import logging |
|
|
import time |
|
|
import time |
|
|
|
|
|
|
|
|
from openerp.osv import osv, fields |
|
|
|
|
|
|
|
|
from openerp.osv import orm, fields |
|
|
from openerp.tools.translate import _ |
|
|
from openerp.tools.translate import _ |
|
|
|
|
|
|
|
|
from openerp.addons.decimal_precision import decimal_precision as dp |
|
|
from openerp.addons.decimal_precision import decimal_precision as dp |
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__) |
|
|
_logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class account_analytic_invoice_line(osv.osv): |
|
|
|
|
|
|
|
|
class AccountAnalyticInvoiceLine(orm.Model): |
|
|
_name = "account.analytic.invoice.line" |
|
|
_name = "account.analytic.invoice.line" |
|
|
|
|
|
|
|
|
def _amount_line( |
|
|
def _amount_line( |
|
@ -104,7 +103,7 @@ class account_analytic_invoice_line(osv.osv): |
|
|
return res_final |
|
|
return res_final |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class account_analytic_account(osv.osv): |
|
|
|
|
|
|
|
|
class AccountAnalyticAccount(orm.Model): |
|
|
_name = "account.analytic.account" |
|
|
_name = "account.analytic.account" |
|
|
_inherit = "account.analytic.account" |
|
|
_inherit = "account.analytic.account" |
|
|
|
|
|
|
|
@ -132,6 +131,13 @@ class account_analytic_account(osv.osv): |
|
|
'recurring_rule_type': 'monthly' |
|
|
'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( |
|
|
def onchange_recurring_invoices( |
|
|
self, cr, uid, ids, recurring_invoices, |
|
|
self, cr, uid, ids, recurring_invoices, |
|
|
date_start=False, context=None): |
|
|
date_start=False, context=None): |
|
@ -141,39 +147,36 @@ class account_analytic_account(osv.osv): |
|
|
return value |
|
|
return value |
|
|
|
|
|
|
|
|
def _prepare_invoice(self, cr, uid, contract, context=None): |
|
|
def _prepare_invoice(self, cr, uid, contract, context=None): |
|
|
context = context or {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if context is None: |
|
|
|
|
|
context = {} |
|
|
inv_obj = self.pool.get('account.invoice') |
|
|
inv_obj = self.pool.get('account.invoice') |
|
|
journal_obj = self.pool.get('account.journal') |
|
|
journal_obj = self.pool.get('account.journal') |
|
|
fpos_obj = self.pool.get('account.fiscal.position') |
|
|
fpos_obj = self.pool.get('account.fiscal.position') |
|
|
lang_obj = self.pool.get('res.lang') |
|
|
lang_obj = self.pool.get('res.lang') |
|
|
|
|
|
|
|
|
if not contract.partner_id: |
|
|
if not contract.partner_id: |
|
|
raise osv.except_osv( |
|
|
|
|
|
|
|
|
raise orm.except_orm( |
|
|
_('No Customer Defined!'), |
|
|
_('No Customer Defined!'), |
|
|
_("You must first select a Customer for Contract %s!") % |
|
|
_("You must first select a Customer for Contract %s!") % |
|
|
contract.name) |
|
|
contract.name) |
|
|
|
|
|
|
|
|
fpos = contract.partner_id.property_account_position or False |
|
|
|
|
|
|
|
|
partner = contract.partner_id |
|
|
|
|
|
fpos = partner.property_account_position or False |
|
|
journal_ids = journal_obj.search( |
|
|
journal_ids = journal_obj.search( |
|
|
cr, uid, |
|
|
cr, uid, |
|
|
[('type', '=', 'sale'), |
|
|
[('type', '=', 'sale'), |
|
|
('company_id', '=', contract.company_id.id or False)], |
|
|
('company_id', '=', contract.company_id.id or False)], |
|
|
limit=1) |
|
|
limit=1) |
|
|
if not journal_ids: |
|
|
if not journal_ids: |
|
|
raise osv.except_osv( |
|
|
|
|
|
|
|
|
raise orm.except_orm( |
|
|
_('Error!'), |
|
|
_('Error!'), |
|
|
_('Please define a sale journal for the company "%s".') % |
|
|
_('Please define a sale journal for the company "%s".') % |
|
|
(contract.company_id.name or '', )) |
|
|
(contract.company_id.name or '', )) |
|
|
|
|
|
|
|
|
partner_payment_term = contract.partner_id.property_payment_term.id |
|
|
partner_payment_term = contract.partner_id.property_payment_term.id |
|
|
|
|
|
|
|
|
inv_data = { |
|
|
inv_data = { |
|
|
'reference': contract.code or False, |
|
|
'reference': contract.code or False, |
|
|
'account_id': contract.partner_id.property_account_receivable.id, |
|
|
|
|
|
|
|
|
'account_id': partner.property_account_receivable.id, |
|
|
'type': 'out_invoice', |
|
|
'type': 'out_invoice', |
|
|
'partner_id': contract.partner_id.id, |
|
|
|
|
|
'currency_id': contract.partner_id.property_product_pricelist.id, |
|
|
|
|
|
|
|
|
'partner_id': partner.id, |
|
|
|
|
|
'currency_id': partner.property_product_pricelist.currency_id.id, |
|
|
'journal_id': len(journal_ids) and journal_ids[0] or False, |
|
|
'journal_id': len(journal_ids) and journal_ids[0] or False, |
|
|
'date_invoice': contract.recurring_next_date, |
|
|
'date_invoice': contract.recurring_next_date, |
|
|
'origin': contract.name, |
|
|
'origin': contract.name, |
|
@ -182,18 +185,14 @@ class account_analytic_account(osv.osv): |
|
|
'company_id': contract.company_id.id or False, |
|
|
'company_id': contract.company_id.id or False, |
|
|
} |
|
|
} |
|
|
invoice_id = inv_obj.create(cr, uid, inv_data, context=context) |
|
|
invoice_id = inv_obj.create(cr, uid, inv_data, context=context) |
|
|
|
|
|
|
|
|
for line in contract.recurring_invoice_line_ids: |
|
|
for line in contract.recurring_invoice_line_ids: |
|
|
|
|
|
|
|
|
res = line.product_id |
|
|
res = line.product_id |
|
|
account_id = res.property_account_income.id |
|
|
account_id = res.property_account_income.id |
|
|
if not account_id: |
|
|
if not account_id: |
|
|
account_id = res.categ_id.property_account_income_categ.id |
|
|
account_id = res.categ_id.property_account_income_categ.id |
|
|
account_id = fpos_obj.map_account(cr, uid, fpos, account_id) |
|
|
account_id = fpos_obj.map_account(cr, uid, fpos, account_id) |
|
|
|
|
|
|
|
|
taxes = res.taxes_id or False |
|
|
taxes = res.taxes_id or False |
|
|
tax_id = fpos_obj.map_tax(cr, uid, fpos, taxes) |
|
|
tax_id = fpos_obj.map_tax(cr, uid, fpos, taxes) |
|
|
|
|
|
|
|
|
if 'old_date' in context: |
|
|
if 'old_date' in context: |
|
|
lang_ids = lang_obj.search( |
|
|
lang_ids = lang_obj.search( |
|
|
cr, uid, [('code', '=', contract.partner_id.lang)], |
|
|
cr, uid, [('code', '=', contract.partner_id.lang)], |
|
@ -204,7 +203,6 @@ class account_analytic_account(osv.osv): |
|
|
'#START#', context['old_date'].strftime(format)) |
|
|
'#START#', context['old_date'].strftime(format)) |
|
|
line.name = line.name.replace( |
|
|
line.name = line.name.replace( |
|
|
'#END#', context['next_date'].strftime(format)) |
|
|
'#END#', context['next_date'].strftime(format)) |
|
|
|
|
|
|
|
|
invoice_line_vals = { |
|
|
invoice_line_vals = { |
|
|
'name': line.name, |
|
|
'name': line.name, |
|
|
'account_id': account_id, |
|
|
'account_id': account_id, |
|
@ -216,42 +214,35 @@ class account_analytic_account(osv.osv): |
|
|
'invoice_id': invoice_id, |
|
|
'invoice_id': invoice_id, |
|
|
'invoice_line_tax_id': [(6, 0, tax_id)], |
|
|
'invoice_line_tax_id': [(6, 0, tax_id)], |
|
|
} |
|
|
} |
|
|
self.pool.get('account.invoice.line').create( |
|
|
|
|
|
|
|
|
self.pool['account.invoice.line'].create( |
|
|
cr, uid, invoice_line_vals, context=context) |
|
|
cr, uid, invoice_line_vals, context=context) |
|
|
|
|
|
|
|
|
inv_obj.button_compute(cr, uid, [invoice_id], context=context) |
|
|
inv_obj.button_compute(cr, uid, [invoice_id], context=context) |
|
|
return invoice_id |
|
|
return invoice_id |
|
|
|
|
|
|
|
|
def recurring_create_invoice(self, cr, uid, automatic=False, context=None): |
|
|
def recurring_create_invoice(self, cr, uid, automatic=False, context=None): |
|
|
context = context or {} |
|
|
|
|
|
|
|
|
if context is None: |
|
|
|
|
|
context = {} |
|
|
current_date = time.strftime('%Y-%m-%d') |
|
|
current_date = time.strftime('%Y-%m-%d') |
|
|
|
|
|
|
|
|
contract_ids = self.search( |
|
|
contract_ids = self.search( |
|
|
cr, uid, |
|
|
cr, uid, |
|
|
[('recurring_next_date', '<=', current_date), |
|
|
[('recurring_next_date', '<=', current_date), |
|
|
('state', '=', 'open'), |
|
|
('state', '=', 'open'), |
|
|
('recurring_invoices', '=', True)]) |
|
|
('recurring_invoices', '=', True)]) |
|
|
for contract in self.browse(cr, uid, contract_ids, context=context): |
|
|
for contract in self.browse(cr, uid, contract_ids, context=context): |
|
|
|
|
|
|
|
|
next_date = datetime.datetime.strptime( |
|
|
next_date = datetime.datetime.strptime( |
|
|
contract.recurring_next_date or current_date, "%Y-%m-%d") |
|
|
contract.recurring_next_date or current_date, "%Y-%m-%d") |
|
|
interval = contract.recurring_interval |
|
|
interval = contract.recurring_interval |
|
|
|
|
|
old_date = next_date |
|
|
if contract.recurring_rule_type == 'daily': |
|
|
if contract.recurring_rule_type == 'daily': |
|
|
old_date = next_date-relativedelta(days=+interval) |
|
|
|
|
|
new_date = next_date + relativedelta(days=+interval) |
|
|
new_date = next_date + relativedelta(days=+interval) |
|
|
elif contract.recurring_rule_type == 'weekly': |
|
|
elif contract.recurring_rule_type == 'weekly': |
|
|
old_date = next_date-relativedelta(weeks=+interval) |
|
|
|
|
|
new_date = next_date + relativedelta(weeks=+interval) |
|
|
new_date = next_date + relativedelta(weeks=+interval) |
|
|
else: |
|
|
else: |
|
|
old_date = next_date+relativedelta(months=+interval) |
|
|
|
|
|
new_date = next_date + relativedelta(months=+interval) |
|
|
new_date = next_date + relativedelta(months=+interval) |
|
|
|
|
|
|
|
|
context['old_date'] = old_date |
|
|
context['old_date'] = old_date |
|
|
context['next_date'] = datetime.datetime.strptime( |
|
|
|
|
|
contract.recurring_next_date or current_date, "%Y-%m-%d") |
|
|
|
|
|
|
|
|
context['next_date'] = new_date |
|
|
self._prepare_invoice( |
|
|
self._prepare_invoice( |
|
|
cr, uid, contract, context=context) |
|
|
cr, uid, contract, context=context) |
|
|
|
|
|
|
|
|
self.write( |
|
|
self.write( |
|
|
cr, uid, [contract.id], |
|
|
cr, uid, [contract.id], |
|
|
{'recurring_next_date': new_date.strftime('%Y-%m-%d')}, |
|
|
{'recurring_next_date': new_date.strftime('%Y-%m-%d')}, |
|
|