From a067129d4d53a8028de053840609792d753fdaae Mon Sep 17 00:00:00 2001 From: Valerie Pirenne Date: Wed, 17 Jun 2015 16:03:47 +0530 Subject: [PATCH] [IMP] lunch: account balance on new order view --- addons/lunch/models/lunch.py | 24 ++++++++++++++++++++++-- addons/lunch/views/lunch_views.xml | 19 ++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/addons/lunch/models/lunch.py b/addons/lunch/models/lunch.py index 3aa1590ab0b..42bec93d07d 100644 --- a/addons/lunch/models/lunch.py +++ b/addons/lunch/models/lunch.py @@ -4,7 +4,7 @@ import datetime from openerp import models, fields, api, _ import openerp.addons.decimal_precision as dp -from openerp.exceptions import UserError +from openerp.exceptions import AccessError, UserError class LunchOrder(models.Model): @@ -36,6 +36,8 @@ class LunchOrder(models.Model): compute='_compute_get_previous_order_ids') company_id = fields.Many2one('res.company', related='user_id.company_id', store=True) currency_id = fields.Many2one('res.currency', related='company_id.currency_id', readonly=True, store=True) + cash_move_balance = fields.Monetary(compute='_compute_cash_move_balance', multi='cash_move_balance') + balance_visible = fields.Boolean(compute='_compute_cash_move_balance', multi='cash_move_balance') @api.one @api.depends('order_line_ids') @@ -80,7 +82,7 @@ class LunchOrder(models.Model): prev_cashmove = self.env['lunch.cashmove'].search([('user_id', '=', self.user_id.id)]) balance = sum(cashmove.amount for cashmove in prev_cashmove) - if self.total > balance: + if self.total > 0 and self.total > balance: return { 'warning': { 'title': _('Insufficient balance'), @@ -88,6 +90,24 @@ class LunchOrder(models.Model): }, } + @api.one + @api.depends('user_id') + def _compute_cash_move_balance(self): + # To avoid hardcoding who can(not) see a given user's cash moves, searching + # in superuser then trying to access it in current user is the only way to + # know whether we have the right to see this or not. Trying to search with + # the current user directly would simply hide the records we are not supposed + # to see, preventing us from knowing whether we can't see those cash moves due + # to security reasons or we can see them but there aren't any for this user. + try: + domain = [('user_id', '=', self.user_id.id)] + lunch_move_ids = self.env['lunch.cashmove'].sudo().search(domain).sudo(self.env.uid) + self.cash_move_balance = sum(lunch_move_ids.mapped('amount')) + self.balance_visible = True + except AccessError: + self.cash_move_balance = 0 + self.balance_visible = False + @api.one @api.constrains('date') def _check_date(self): diff --git a/addons/lunch/views/lunch_views.xml b/addons/lunch/views/lunch_views.xml index 84aadd2992d..25d606e95c7 100644 --- a/addons/lunch/views/lunch_views.xml +++ b/addons/lunch/views/lunch_views.xml @@ -172,6 +172,7 @@

+ @@ -367,13 +368,29 @@ + + + - +
+ +