[IMP] lunch: account balance on new order view

This commit is contained in:
Valerie Pirenne
2015-08-05 10:43:44 +02:00
committed by David Monjoie
parent 4474b5ac70
commit a067129d4d
2 changed files with 40 additions and 3 deletions
+22 -2
View File
@@ -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):
+18 -1
View File
@@ -172,6 +172,7 @@
</p>
</field>
</record>
<menuitem name="Your Lunch Account" parent="menu_lunch_title" id="lunch_cashmove_menu_form" action="lunch_cashmove_action_account" sequence="3"/>
<!--Action for Administrate Orders group by supplier-->
@@ -367,13 +368,29 @@
<field name='state' widget='statusbar' statusbar_visible='new,confirmed'/>
</header>
<sheet>
<field name="balance_visible" invisible="1"/>
<div class="alert alert-warning" role="alert" attrs="{'invisible': ['|',('cash_move_balance', '&gt;', 0),('balance_visible', '=', False)]}">
Warning : Your balance is insufficient.
</div>
<group>
<group>
<field name='user_id'
context="{'default_groups_ref': ['base.group_user', 'base.group_partner_manager', 'lunch.group_lunch_user']}"/>
<field name='date'/>
</group>
<group>
<field name='date'/>
<div class="oe_button_box" name="button_box">
<button class="oe_stat_button"
attrs="{'invisible': [('balance_visible', '=', False)]}"
name="%(lunch_cashmove_action_account)d"
icon="fa-money"
type="action">
<div class="o_form_field o_stat_info">
<span class="o_stat_value"><field name="cash_move_balance" widget="monetary"/></span>
<span class="o_stat_text">Balance</span>
</div>
</button>
</div>
</group>
</group>
<field name='alerts' attrs="{'invisible': ['|',('state','!=','new'),('alerts','=',False)]}" class="oe_inline alert alert-danger"/>