from lxml import etree
-import netsvc
-from osv import fields, osv, orm
-from tools.translate import _
-import decimal_precision as dp
-import tools
+from openerp import netsvc
+from openerp.osv import fields, osv, orm
+from openerp.tools.translate import _
+import openerp.addons.decimal_precision as dp
+from openerp import tools
class account_move_line(osv.osv):
_name = "account.move.line"
del data[f]
return data
+ def _prepare_analytic_line(self, cr, uid, obj_line, context=None):
+ """
+ Prepare the values given at the create() of account.analytic.line upon the validation of a journal item having
+ an analytic account. This method is intended to be extended in other modules.
+
+ :param obj_line: browse record of the account.move.line that triggered the analytic line creation
+ """
+ return {'name': obj_line.name,
+ 'date': obj_line.date,
+ 'account_id': obj_line.analytic_account_id.id,
+ 'unit_amount': obj_line.quantity,
+ 'product_id': obj_line.product_id and obj_line.product_id.id or False,
+ 'product_uom_id': obj_line.product_uom_id and obj_line.product_uom_id.id or False,
+ 'amount': (obj_line.credit or 0.0) - (obj_line.debit or 0.0),
+ 'general_account_id': obj_line.account_id.id,
+ 'journal_id': obj_line.journal_id.analytic_journal_id.id,
+ 'ref': obj_line.ref,
+ 'move_id': obj_line.id,
+ 'user_id': uid,
+ }
+
def create_analytic_lines(self, cr, uid, ids, context=None):
acc_ana_line_obj = self.pool.get('account.analytic.line')
for obj_line in self.browse(cr, uid, ids, context=context):
if obj_line.analytic_account_id:
if not obj_line.journal_id.analytic_journal_id:
- raise osv.except_osv(_('No Analytic Journal !'),_("You have to define an analytic journal on the '%s' journal!") % (obj_line.journal_id.name, ))
- amt = (obj_line.credit or 0.0) - (obj_line.debit or 0.0)
- vals_lines = {
- 'name': obj_line.name,
- 'date': obj_line.date,
- 'account_id': obj_line.analytic_account_id.id,
- 'unit_amount': obj_line.quantity,
- 'product_id': obj_line.product_id and obj_line.product_id.id or False,
- 'product_uom_id': obj_line.product_uom_id and obj_line.product_uom_id.id or False,
- 'amount': amt,
- 'general_account_id': obj_line.account_id.id,
- 'journal_id': obj_line.journal_id.analytic_journal_id.id,
- 'ref': obj_line.ref,
- 'move_id': obj_line.id,
- 'user_id': uid
- }
- acc_ana_line_obj.create(cr, uid, vals_lines)
+ raise osv.except_osv(_('No Analytic Journal!'),_("You have to define an analytic journal on the '%s' journal!") % (obj_line.journal_id.name, ))
+ vals_line = self._prepare_analytic_line(cr, uid, obj_line, context=context)
+ acc_ana_line_obj.create(cr, uid, vals_line)
return True
def _default_get_move_form_hook(self, cursor, user, data):
'statement_id': fields.many2one('account.bank.statement', 'Statement', help="The bank statement used for bank reconciliation", select=1),
'reconcile_id': fields.many2one('account.move.reconcile', 'Reconcile', readonly=True, ondelete='set null', select=2),
'reconcile_partial_id': fields.many2one('account.move.reconcile', 'Partial Reconcile', readonly=True, ondelete='set null', select=2),
- 'reconcile': fields.function(_get_reconcile, type='char', string='Reconcile'),
+ 'reconcile': fields.function(_get_reconcile, type='char', string='Reconcile Ref'),
'amount_currency': fields.float('Amount Currency', help="The amount expressed in an optional other currency if it is a multi-currency entry.", digits_compute=dp.get_precision('Account')),
- 'amount_residual_currency': fields.function(_amount_residual, string='Residual Amount', multi="residual", help="The residual amount on a receivable or payable of a journal entry expressed in its currency (maybe different of the company currency)."),
+ 'amount_residual_currency': fields.function(_amount_residual, string='Residual Amount in Currency', multi="residual", help="The residual amount on a receivable or payable of a journal entry expressed in its currency (maybe different of the company currency)."),
'amount_residual': fields.function(_amount_residual, string='Residual Amount', multi="residual", help="The residual amount on a receivable or payable of a journal entry expressed in the company currency."),
'currency_id': fields.many2one('res.currency', 'Currency', help="The optional other currency if it is a multi-currency entry."),
'journal_id': fields.related('move_id', 'journal_id', string='Journal', type='many2one', relation='account.journal', required=True, select=True,
store = {
'account.move': (_get_move_lines, ['period_id'], 20)
}),
- 'blocked': fields.boolean('Litigation', help="You can check this box to mark this journal item as a litigation with the associated partner"),
+ 'blocked': fields.boolean('No Follow-up', help="You can check this box to mark this journal item as a litigation with the associated partner"),
'partner_id': fields.many2one('res.partner', 'Partner', select=1, ondelete='restrict'),
'date_maturity': fields.date('Due date', select=True ,help="This field is used for payable and receivable journal entries. You can put the limit date for the payment of this line."),
'date': fields.related('move_id','date', string='Effective date', type='date', required=True, select=True,
type='many2one', relation='account.invoice', fnct_search=_invoice_search),
'account_tax_id':fields.many2one('account.tax', 'Tax'),
'analytic_account_id': fields.many2one('account.analytic.account', 'Analytic Account'),
- 'company_id': fields.related('account_id', 'company_id', type='many2one', relation='res.company', string='Company', store=True, readonly=True)
+ 'company_id': fields.related('account_id', 'company_id', type='many2one', relation='res.company',
+ string='Company', store=True, readonly=True)
}
def _get_date(self, cr, uid, context=None):
context or {}
period_obj = self.pool.get('account.period')
dt = time.strftime('%Y-%m-%d')
- if ('journal_id' in context) and ('period_id' in context):
+ if context.get('journal_id') and context.get('period_id'):
cr.execute('SELECT date FROM account_move_line ' \
'WHERE journal_id = %s AND period_id = %s ' \
'ORDER BY id DESC limit 1',
if context.get('period_id', False):
return context['period_id']
account_period_obj = self.pool.get('account.period')
- ids = account_period_obj.find(cr, uid, context=context)
+ ctx = dict(context, account_period_prefer_normal=True)
+ ids = account_period_obj.find(cr, uid, context=ctx)
period_id = False
if ids:
period_id = ids[0]
return False
return True
+ def _check_currency_and_amount(self, cr, uid, ids, context=None):
+ for l in self.browse(cr, uid, ids, context=context):
+ if (l.amount_currency and not l.currency_id):
+ return False
+ return True
+
+ def _check_currency_amount(self, cr, uid, ids, context=None):
+ for l in self.browse(cr, uid, ids, context=context):
+ if l.amount_currency:
+ if (l.amount_currency > 0.0 and l.credit > 0.0) or (l.amount_currency < 0.0 and l.debit > 0.0):
+ return False
+ return True
+
+ def _check_currency_company(self, cr, uid, ids, context=None):
+ for l in self.browse(cr, uid, ids, context=context):
+ if l.currency_id.id == l.company_id.currency_id.id:
+ return False
+ return True
+
_constraints = [
(_check_no_view, 'You cannot create journal items on an account of type view.', ['account_id']),
(_check_no_closed, 'You cannot create journal items on closed account.', ['account_id']),
(_check_company_id, 'Account and Period must belong to the same company.', ['company_id']),
(_check_date, 'The date of your Journal Entry is not in the defined period! You should change the date or remove this constraint from the journal.', ['date']),
(_check_currency, 'The selected account of your Journal Entry forces to provide a secondary currency. You should remove the secondary currency on the account or select a multi-currency view on the journal.', ['currency_id']),
+ (_check_currency_and_amount, "You cannot create journal items with a secondary currency without recording both 'currency' and 'amount currency' field.", ['currency_id','amount_currency']),
+ (_check_currency_amount, 'The amount expressed in the secondary currency must be positive when the journal item is a debit and negative when if it is a credit.', ['amount_currency']),
+ (_check_currency_company, "You cannot provide a secondary currency if it is the same than the company one." , ['currency_id']),
]
#TODO: ONCHANGE_ACCOUNT_ID: set account_tax_id
}
return result
- def onchange_account_id(self, cr, uid, ids, account_id, context=None):
- res = {'value': {}}
- if account_id:
- res['value']['account_tax_id'] = [x.id for x in self.pool.get('account.account').browse(cr, uid, account_id, context=context).tax_ids]
- return res
-
- def onchange_partner_id(self, cr, uid, ids, move_id, partner_id, account_id=None, debit=0, credit=0, date=False, journal=False):
+ def onchange_partner_id(self, cr, uid, ids, move_id, partner_id, account_id=None, debit=0, credit=0, date=False, journal=False, context=None):
partner_obj = self.pool.get('res.partner')
payment_term_obj = self.pool.get('account.payment.term')
journal_obj = self.pool.get('account.journal')
return {'value':val}
if not date:
date = datetime.now().strftime('%Y-%m-%d')
- part = partner_obj.browse(cr, uid, partner_id)
-
- if part.property_payment_term:
- res = payment_term_obj.compute(cr, uid, part.property_payment_term.id, 100, date)
+ jt = False
+ if journal:
+ jt = journal_obj.browse(cr, uid, journal, context=context).type
+ part = partner_obj.browse(cr, uid, partner_id, context=context)
+
+ payment_term_id = False
+ if jt and jt in ('purchase', 'purchase_refund') and part.property_supplier_payment_term:
+ payment_term_id = part.property_supplier_payment_term.id
+ elif jt and part.property_payment_term:
+ payment_term_id = part.property_payment_term.id
+ if payment_term_id:
+ res = payment_term_obj.compute(cr, uid, payment_term_id, 100, date)
if res:
val['date_maturity'] = res[0][0]
if not account_id:
id1 = part.property_account_payable.id
id2 = part.property_account_receivable.id
- if journal:
- jt = journal_obj.browse(cr, uid, journal).type
+ if jt:
if jt in ('sale', 'purchase_refund'):
val['account_id'] = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, id2)
elif jt in ('purchase', 'sale_refund'):
elif part.supplier:
val['account_id'] = fiscal_pos_obj.map_account(cr, uid, part and part.property_account_position or False, id1)
if val.get('account_id', False):
- d = self.onchange_account_id(cr, uid, ids, val['account_id'])
+ d = self.onchange_account_id(cr, uid, ids, account_id=val['account_id'], partner_id=part.id, context=context)
val.update(d['value'])
return {'value':val}
- def onchange_account_id(self, cr, uid, ids, account_id=False, partner_id=False):
+ def onchange_account_id(self, cr, uid, ids, account_id=False, partner_id=False, context=None):
account_obj = self.pool.get('account.account')
partner_obj = self.pool.get('res.partner')
fiscal_pos_obj = self.pool.get('account.fiscal.position')
val = {}
if account_id:
- res = account_obj.browse(cr, uid, account_id)
+ res = account_obj.browse(cr, uid, account_id, context=context)
tax_ids = res.tax_ids
if tax_ids and partner_id:
- part = partner_obj.browse(cr, uid, partner_id)
+ part = partner_obj.browse(cr, uid, partner_id, context=context)
tax_id = fiscal_pos_obj.map_tax(cr, uid, part and part.property_account_position or False, tax_ids)[0]
else:
tax_id = tax_ids and tax_ids[0].id or False
def list_partners_to_reconcile(self, cr, uid, context=None):
cr.execute(
- """
- SELECT partner_id
- FROM (
- SELECT l.partner_id, p.last_reconciliation_date, SUM(l.debit) AS debit, SUM(l.credit) AS credit
+ """SELECT partner_id FROM (
+ SELECT l.partner_id, p.last_reconciliation_date, SUM(l.debit) AS debit, SUM(l.credit) AS credit, MAX(l.create_date) AS max_date
FROM account_move_line l
RIGHT JOIN account_account a ON (a.id = l.account_id)
RIGHT JOIN res_partner p ON (l.partner_id = p.id)
WHERE a.reconcile IS TRUE
AND l.reconcile_id IS NULL
- AND (p.last_reconciliation_date IS NULL OR l.date > p.last_reconciliation_date)
AND l.state <> 'draft'
GROUP BY l.partner_id, p.last_reconciliation_date
) AS s
- WHERE debit > 0 AND credit > 0
+ WHERE debit > 0 AND credit > 0 AND (last_reconciliation_date IS NULL OR max_date > last_reconciliation_date)
ORDER BY last_reconciliation_date""")
- ids = cr.fetchall()
- ids = len(ids) and [x[0] for x in ids] or []
- return self.pool.get('res.partner').name_get(cr, uid, ids, context=context)
+ ids = [x[0] for x in cr.fetchall()]
+ if not ids:
+ return []
+
+ # To apply the ir_rules
+ partner_obj = self.pool.get('res.partner')
+ ids = partner_obj.search(cr, uid, [('id', 'in', ids)], context=context)
+ return partner_obj.name_get(cr, uid, ids, context=context)
def reconcile_partial(self, cr, uid, ids, type='auto', context=None, writeoff_acc_id=False, writeoff_period_id=False, writeoff_journal_id=False):
move_rec_obj = self.pool.get('account.move.reconcile')
else:
currency_id = line.company_id.currency_id
if line.reconcile_id:
- raise osv.except_osv(_('Warning!'), _('Already reconciled.'))
+ raise osv.except_osv(_('Warning'), _("Journal Item '%s' (id: %s), Move '%s' is already reconciled!") % (line.name, line.id, line.move_id.name))
if line.reconcile_partial_id:
for line2 in line.reconcile_partial_id.line_partial_ids:
if not line2.reconcile_id:
if r[0][1] != None:
raise osv.except_osv(_('Error!'), _('Some entries are already reconciled.'))
- if (not currency_obj.is_zero(cr, uid, account.company_id.currency_id, writeoff)) or \
+ if context.get('fy_closing'):
+ # We don't want to generate any write-off when being called from the
+ # wizard used to close a fiscal year (and it doesn't give us any
+ # writeoff_acc_id).
+ pass
+ elif (not currency_obj.is_zero(cr, uid, account.company_id.currency_id, writeoff)) or \
(account.currency_id and (not currency_obj.is_zero(cr, uid, account.currency_id, currency))):
if not writeoff_acc_id:
raise osv.except_osv(_('Warning!'), _('You have to provide an account for the write off/exchange difference entry.'))
if context is None:
context = {}
period_pool = self.pool.get('account.period')
- pids = period_pool.search(cr, user, [('date_start','<=',date), ('date_stop','>=',date)])
+ ctx = dict(context, account_period_prefer_normal=True)
+ pids = period_pool.find(cr, user, date, context=ctx)
if pids:
res.update({
'period_id':pids[0]
'has been confirmed.') % res[2])
return res
- def _remove_move_reconcile(self, cr, uid, move_ids=None, context=None):
+ def _remove_move_reconcile(self, cr, uid, move_ids=None, opening_reconciliation=False, context=None):
# Function remove move rencocile ids related with moves
obj_move_line = self.pool.get('account.move.line')
obj_move_rec = self.pool.get('account.move.reconcile')
unlink_ids += rec_ids
unlink_ids += part_rec_ids
if unlink_ids:
+ if opening_reconciliation:
+ obj_move_rec.write(cr, uid, unlink_ids, {'opening_reconciliation': False})
obj_move_rec.unlink(cr, uid, unlink_ids)
return True
for line in self.browse(cr, uid, ids, context=context):
ctx = context.copy()
- if ('journal_id' not in ctx):
+ if not ctx.get('journal_id'):
if line.move_id:
ctx['journal_id'] = line.move_id.journal_id.id
else:
ctx['journal_id'] = line.journal_id.id
- if ('period_id' not in ctx):
+ if not ctx.get('period_id'):
if line.move_id:
ctx['period_id'] = line.move_id.period_id.id
else:
period = period_obj.browse(cr, uid, period_id, context=context)
for (state,) in result:
if state == 'done':
- raise osv.except_osv(_('Error !'), _('You can not add/modify entries in a closed period %s of journal %s.' % (period.name,journal.name)))
+ raise osv.except_osv(_('Error!'), _('You can not add/modify entries in a closed period %s of journal %s.' % (period.name,journal.name)))
if not result:
jour_period_obj.create(cr, uid, {
'name': (journal.code or journal.name)+':'+(period.name or ''),
if context is None:
context = {}
if vals.get('move_id', False):
- company_id = self.pool.get('account.move').read(cr, uid, vals['move_id'], ['company_id']).get('company_id', False)
- if company_id:
- vals['company_id'] = company_id[0]
+ move = self.pool.get('account.move').browse(cr, uid, vals['move_id'], context=context)
+ if move.company_id:
+ vals['company_id'] = move.company_id.id
+ if move.date and not vals.get('date'):
+ vals['date'] = move.date
if ('account_id' in vals) and not account_obj.read(cr, uid, vals['account_id'], ['active'])['active']:
raise osv.except_osv(_('Bad Account!'), _('You cannot use an inactive account.'))
if 'journal_id' in vals and vals['journal_id']:
move_id = move_obj.create(cr, uid, v, context)
vals['move_id'] = move_id
else:
- raise osv.except_osv(_('No piece number !'), _('Cannot create an automatic sequence for this piece.\nPut a sequence in the journal definition for automatic numbering or create a sequence manually for this piece.'))
+ raise osv.except_osv(_('No Piece Number!'), _('Cannot create an automatic sequence for this piece.\nPut a sequence in the journal definition for automatic numbering or create a sequence manually for this piece.'))
ok = not (journal.type_control_ids or journal.account_control_ids)
if ('account_id' in vals):
account = account_obj.browse(cr, uid, vals['account_id'], context=context)