[IMP] lunch: account balance on new order view
This commit is contained in:
committed by
David Monjoie
parent
4474b5ac70
commit
a067129d4d
@@ -4,7 +4,7 @@ import datetime
|
|||||||
|
|
||||||
from openerp import models, fields, api, _
|
from openerp import models, fields, api, _
|
||||||
import openerp.addons.decimal_precision as dp
|
import openerp.addons.decimal_precision as dp
|
||||||
from openerp.exceptions import UserError
|
from openerp.exceptions import AccessError, UserError
|
||||||
|
|
||||||
|
|
||||||
class LunchOrder(models.Model):
|
class LunchOrder(models.Model):
|
||||||
@@ -36,6 +36,8 @@ class LunchOrder(models.Model):
|
|||||||
compute='_compute_get_previous_order_ids')
|
compute='_compute_get_previous_order_ids')
|
||||||
company_id = fields.Many2one('res.company', related='user_id.company_id', store=True)
|
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)
|
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.one
|
||||||
@api.depends('order_line_ids')
|
@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)])
|
prev_cashmove = self.env['lunch.cashmove'].search([('user_id', '=', self.user_id.id)])
|
||||||
balance = sum(cashmove.amount for cashmove in prev_cashmove)
|
balance = sum(cashmove.amount for cashmove in prev_cashmove)
|
||||||
|
|
||||||
if self.total > balance:
|
if self.total > 0 and self.total > balance:
|
||||||
return {
|
return {
|
||||||
'warning': {
|
'warning': {
|
||||||
'title': _('Insufficient balance'),
|
'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.one
|
||||||
@api.constrains('date')
|
@api.constrains('date')
|
||||||
def _check_date(self):
|
def _check_date(self):
|
||||||
|
|||||||
@@ -172,6 +172,7 @@
|
|||||||
</p>
|
</p>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
|
||||||
<menuitem name="Your Lunch Account" parent="menu_lunch_title" id="lunch_cashmove_menu_form" action="lunch_cashmove_action_account" sequence="3"/>
|
<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-->
|
<!--Action for Administrate Orders group by supplier-->
|
||||||
@@ -367,13 +368,29 @@
|
|||||||
<field name='state' widget='statusbar' statusbar_visible='new,confirmed'/>
|
<field name='state' widget='statusbar' statusbar_visible='new,confirmed'/>
|
||||||
</header>
|
</header>
|
||||||
<sheet>
|
<sheet>
|
||||||
|
<field name="balance_visible" invisible="1"/>
|
||||||
|
<div class="alert alert-warning" role="alert" attrs="{'invisible': ['|',('cash_move_balance', '>', 0),('balance_visible', '=', False)]}">
|
||||||
|
Warning : Your balance is insufficient.
|
||||||
|
</div>
|
||||||
<group>
|
<group>
|
||||||
<group>
|
<group>
|
||||||
<field name='user_id'
|
<field name='user_id'
|
||||||
context="{'default_groups_ref': ['base.group_user', 'base.group_partner_manager', 'lunch.group_lunch_user']}"/>
|
context="{'default_groups_ref': ['base.group_user', 'base.group_partner_manager', 'lunch.group_lunch_user']}"/>
|
||||||
|
<field name='date'/>
|
||||||
</group>
|
</group>
|
||||||
<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>
|
||||||
</group>
|
</group>
|
||||||
<field name='alerts' attrs="{'invisible': ['|',('state','!=','new'),('alerts','=',False)]}" class="oe_inline alert alert-danger"/>
|
<field name='alerts' attrs="{'invisible': ['|',('state','!=','new'),('alerts','=',False)]}" class="oe_inline alert alert-danger"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user