1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from osv import osv, fields
25 class lunch_category(osv.osv):
26 _name = 'lunch.category'
27 _description = "Category"
30 'name': fields.char('Name', required=True, size=50),
37 class lunch_product(osv.osv):
38 _name = 'lunch.product'
40 def _category_name_get(self, cr, uid, context={}):
41 obj = self.pool.get('lunch.category')
42 cat_ids = obj.search(cr, uid, [])
43 res = obj.read(cr, uid, cat_ids, ['name', 'category'])
44 return [(str(r['id']), r['name']) for r in res] + [('0', '')]
47 'name': fields.char('Name', size=50, required=True),
48 'category_id': fields.selection(_category_name_get, 'Category', size=32),
49 'description': fields.char('Description', size=128, required=False),
50 'price': fields.float('Price', digits=(16, 2)),
51 'active': fields.boolean('Active'),
55 'active': lambda * a : True,
60 class lunch_cashbox(osv.osv):
61 _name = 'lunch.cashbox'
63 def amount_available(self, cr, uid, ids, field_name, arg, context):
64 cr.execute("SELECT box,sum(amount) from lunch_cashmove where active = 't' group by box")
65 r = dict(cr.fetchall())
71 'manager':fields.many2one('res.users', 'Manager'),
72 'name':fields.char('Name', size=30, required=True, unique=True),
73 'sum_remain': fields.function(amount_available, method=True, string='Remained Total'),
81 class lunch_cashmove(osv.osv):
82 _name = 'lunch.cashmove'
85 'name': fields.char('Name', size=128),
86 'user_cashmove': fields.many2one('res.users', 'User Name', required=True),
87 'amount': fields.float('Amount', digits=(16, 2)),
88 'box':fields.many2one('lunch.cashbox', 'Box Name', size=30, required=True),
89 'active':fields.boolean('Active'),
90 'create_date': fields.datetime('Created date', readonly=True),
94 'active': lambda * a: True,
101 class lunch_order(osv.osv):
102 _name = 'lunch.order'
103 _rec_name = "user_id"
105 def _price_get(self, cr, uid, ids, name, args, context=None):
107 for o in self.browse(cr, uid, ids):
108 res[o.id] = o.product.price
112 'user_id': fields.many2one('res.users', 'User Name', required=True,
113 readonly=True, states={'draft':[('readonly', False)]}),
114 'product':fields.many2one('lunch.product', 'Product', required=True,
115 readonly=True, states={'draft':[('readonly', False)]}, change_default=True),
116 'date': fields.date('Date', readonly=True, states={'draft':[('readonly', False)]}),
117 'cashmove':fields.many2one('lunch.cashmove', 'CashMove' , readonly=True),
118 'descript':fields.char('Description Order', readonly=True, size=50,
119 states={'draft':[('readonly', False)]}),
120 'state': fields.selection([('draft', 'Draft'), ('confirmed', 'Confirmed'), ],
121 'State', readonly=True, select=True),
122 'price': fields.function(_price_get, method=True, string="Price"),
126 'user_id': lambda self, cr, uid, context: uid,
127 'date': lambda self, cr, uid, context: time.strftime('%Y-%m-%d'),
128 'state': lambda self, cr, uid, context: 'draft',
131 def confirm(self, cr, uid, ids, box, context):
132 cashmove_ref = self.pool.get('lunch.cashmove')
133 for order in self.browse(cr, uid, ids):
134 if order.state == 'confirmed':
136 new_id = cashmove_ref.create(cr, uid, {'name': order.product.name + ' order',
137 'amount':-order.product.price,
138 'user_cashmove':order.user_id.id,
142 self.write(cr, uid, [order.id], {'cashmove':new_id, 'state':'confirmed'})
145 def lunch_order_cancel(self, cr, uid, ids, context):
146 orders = self.browse(cr, uid, ids)
148 if not order.cashmove:
150 self.pool.get('lunch.cashmove').unlink(cr, uid, [order.cashmove.id])
151 self.write(cr, uid, ids, {'state':'draft'})
154 def onchange_product(self, cr, uid, ids, product):
156 return {'value': {'price': 0.0}}
157 price = self.pool.get('lunch.product').read(cr, uid, product, ['price'])['price']
158 return {'value': {'price': price}}
162 class report_lunch_amount(osv.osv):
163 _name = 'report.lunch.amount'
164 _description = "Amount available by user and box"
169 'user_id': fields.many2one('res.users', 'User Name', readonly=True),
170 'amount': fields.float('Amount', readonly=True, digits=(16, 2)),
171 'box':fields.many2one('lunch.cashbox', 'Box Name', size=30, readonly=True),
176 create or replace view report_lunch_amount as (
179 lc.user_cashmove as user_id,
180 sum(amount) as amount,
186 group by lc.user_cashmove, lc.box
189 report_lunch_amount()
190 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: