[IMP]: lunch: code optimization and apply csv file and doc string
[odoo/odoo.git] / addons / lunch / lunch.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from osv import osv, fields
23 import time
24
25 class lunch_category(osv.osv):
26     """ Lunch category """
27
28     _name = 'lunch.category'
29     _description = "Category"
30
31     _columns = {
32         'name': fields.char('Name', required=True, size=50),
33     }
34     _order = 'name'
35
36 lunch_category()
37
38
39 class lunch_product(osv.osv):
40     """ Lunch Product """
41
42     _name = 'lunch.product'
43     _description = "Lunch Product"
44
45     def _category_name_get(self, cr, uid, context={}):
46
47         """ Get category name
48         @param cr: the current row, from the database cursor,
49         @param uid: the current user’s ID for security checks,"""
50
51         obj = self.pool.get('lunch.category')
52         cat_ids = obj.search(cr,uid,[])
53         res = obj.read(cr, uid, cat_ids, ['name', 'category'])
54         return [(str(r['id']), r['name']) for r in res]+ [('0','')]
55
56     _columns = {
57         'name': fields.char('Name', size=50, required=True),
58         'category_id': fields.selection(_category_name_get, 'Category', size=32),
59         'description': fields.char('Description', size=128, required=False),
60         'price': fields.float('Price', digits=(16,2)),
61         'active': fields.boolean('Active'),
62     }
63
64     _defaults = {
65         'active': lambda *a : True,
66         }
67
68 lunch_product()
69
70
71 class lunch_cashbox(osv.osv):
72     """ cashbox for Lunch """
73
74     _name = 'lunch.cashbox'
75     _description = "Cashbox for Lunch "
76
77
78     def amount_available(self, cr, uid, ids, field_name, arg, context):
79
80         """ count available amount
81         @param cr: the current row, from the database cursor,
82         @param uid: the current user’s ID for security checks,
83         @param ids: List of create menu’s IDs """
84
85         cr.execute("SELECT box,sum(amount) from lunch_cashmove where active = 't' group by box")
86         amount = dict(cr.fetchall())
87         for i in ids:
88             amount.setdefault(i, 0)
89         return amount
90
91     _columns = {
92         'manager': fields.many2one('res.users', 'Manager'),
93         'name': fields.char('Name', size=30, required=True, unique = True),
94         'sum_remain': fields.function(amount_available, method=True, string='Remained Total'),
95         }
96
97 lunch_cashbox()
98
99
100 class lunch_cashmove(osv.osv):
101     """ Move cah """
102
103     _name = 'lunch.cashmove'
104     _description = "Move cash"
105
106     _columns = {
107         'name': fields.char('Name', size=128),
108         'user_cashmove': fields.many2one('res.users', 'User Name', required=True),
109         'amount': fields.float('Amount', digits=(16, 2)),
110         'box': fields.many2one('lunch.cashbox', 'Box Name', size=30, required=True),
111         'active': fields.boolean('Active'),
112         'create_date': fields.datetime('Created date', readonly=True),
113         }
114
115     _defaults = {
116     'active': lambda *a: True,
117     }
118
119 lunch_cashmove()
120
121
122 class lunch_order(osv.osv):
123     """ Apply lunch order """
124
125     _name = 'lunch.order'
126     _description = "Lunch Order"
127     _rec_name = "user_id"
128
129     def _price_get(self, cr, uid, ids, name, args, context=None):
130
131         """ Get Price of Product
132          @param cr: the current row, from the database cursor,
133          @param uid: the current user’s ID for security checks,
134          @param ids: List of Lunch order’s IDs"""
135
136         res = {}
137         for price in self.browse(cr, uid, ids):
138             res[price.id] = price.product.price
139         return res
140
141     _columns = {
142         'user_id': fields.many2one('res.users', 'User Name', required=True, \
143             readonly=True, states={'draft':[('readonly', False)]}),
144         'product': fields.many2one('lunch.product', 'Product', required=True, \
145             readonly=True, states={'draft':[('readonly', False)]}, change_default=True),
146         'date': fields.date('Date', readonly=True, states={'draft':[('readonly', False)]}),
147         'cashmove': fields.many2one('lunch.cashmove', 'CashMove' , readonly=True),
148         'descript': fields.char('Description Order', readonly=True, size=50, \
149             states = {'draft':[('readonly', False)]}),
150         'state': fields.selection([('draft', 'Draft'), ('confirmed', 'Confirmed'), ], \
151             'State', readonly=True, select=True),
152         'price': fields.function(_price_get, method=True, string="Price"),
153     }
154
155     _defaults = {
156         'user_id': lambda self,cr,uid,context: uid,
157         'date': lambda self,cr,uid,context: time.strftime('%Y-%m-%d'),
158         'state': lambda self,cr,uid,context: 'draft',
159     }
160
161     def confirm(self,cr,uid,ids,box,context):
162
163         """ confirm order
164         @param cr: the current row, from the database cursor,
165         @param uid: the current user’s ID for security checks,
166         @param ids: List of confirm order’s IDs """
167
168         cashmove_ref = self.pool.get('lunch.cashmove')
169         for order in self.browse(cr, uid, ids):
170             if order.state == 'confirmed':
171                 continue
172             new_id = cashmove_ref.createcr, uid, {'name': order.product.name+' order',
173                             'amount':-order.product.price,
174                             'user_cashmove':order.user_id.id,
175                             'box':box,
176                             'active':True,
177                             }
178             self.write(cr, uid, [order.id], {'cashmove': new_id, 'state': 'confirmed'})
179         return {}
180
181     def lunch_order_cancel(self, cr, uid, ids, context):
182
183         """" cancel order
184          @param cr: the current row, from the database cursor,
185          @param uid: the current user’s ID for security checks,
186          @param ids: List of create menu’s IDs """
187
188         orders = self.browse(cr, uid, ids)
189         for order in orders:
190             if not order.cashmove:
191                 continue
192         self.pool.get('lunch.cashmove').unlink(cr, uid, [order.cashmove.id])
193         self.write(cr, uid, ids, {'state':'draft'})
194         return {}
195
196     def onchange_product(self, cr, uid, ids, product):
197
198         """ Get price for Product
199          @param cr: the current row, from the database cursor,
200          @param uid: the current user’s ID for security checks,
201          @param ids: List of create menu’s IDs """
202
203         if not product:
204             return {'value': {'price': 0.0}}
205         price = self.pool.get('lunch.product').read(cr, uid, product, ['price'])['price']
206         return {'value': {'price': price}}
207
208 lunch_order()
209
210
211 class report_lunch_amount(osv.osv):
212     """ Lunch Amount Report """
213
214     _name = 'report.lunch.amount'
215     _description = "Amount available by user and box"
216     _auto = False
217     _rec_name = "user"
218
219     _columns = {
220         'user_id': fields.many2one('res.users', 'User Name', readonly=True),
221         'amount': fields.float('Amount', readonly=True, digits=(16, 2)),
222         'box': fields.many2one('lunch.cashbox', 'Box Name', size=30, readonly=True),
223         }
224
225     def init(self, cr):
226
227         """ @param cr: the current row, from the database cursor"""
228
229         cr.execute("""
230             create or replace view report_lunch_amount as (
231                 select
232                     min(lc.id) as id,
233                     lc.user_cashmove as user_id,
234                     sum(amount) as amount,
235                     lc.box as box
236                 from
237                     lunch_cashmove lc
238                 where
239                     active = 't'
240                 group by lc.user_cashmove, lc.box
241                 )""")
242
243 report_lunch_amount()
244
245 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
246