Launchpad automatic translations update.
[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     _columns = {
46         'name': fields.char('Name', size=50, required=True),
47         'category_id': fields.many2one('lunch.category', 'Category'),
48         'description': fields.char('Description', size=128, required=False),
49         'price': fields.float('Price', digits=(16,2)),
50         'active': fields.boolean('Active'),
51     }
52
53     _defaults = {
54         'active': lambda *a : True,
55     }
56
57 lunch_product()
58
59
60 class lunch_cashbox(osv.osv):
61     """ cashbox for Lunch """
62
63     _name = 'lunch.cashbox'
64     _description = "Cashbox for Lunch "
65
66
67     def amount_available(self, cr, uid, ids, field_name, arg, context=None):
68
69         """ count available amount
70         @param cr: the current row, from the database cursor,
71         @param uid: the current user’s ID for security checks,
72         @param ids: List of create menu’s IDs
73         @param context: A standard dictionary for contextual values """
74
75         cr.execute("SELECT box,sum(amount) from lunch_cashmove where active = 't' group by box")
76         amount = dict(cr.fetchall())
77         for i in ids:
78             amount.setdefault(i, 0)
79         return amount
80
81     _columns = {
82         'manager': fields.many2one('res.users', 'Manager'),
83         'name': fields.char('Name', size=30, required=True, unique = True),
84         'sum_remain': fields.function(amount_available, method=True, string='Total Remaining'),
85     }
86
87 lunch_cashbox()
88
89
90 class lunch_cashmove(osv.osv):
91     """ Move cash """
92
93     _name = 'lunch.cashmove'
94     _description = "Cash Move"
95
96     _columns = {
97         'name': fields.char('Description', size=128),
98         'user_cashmove': fields.many2one('res.users', 'User Name', required=True),
99         'amount': fields.float('Amount', digits=(16, 2)),
100         'box': fields.many2one('lunch.cashbox', 'Box Name', size=30, required=True),
101         'active': fields.boolean('Active'),
102         'create_date': fields.datetime('Creation Date', readonly=True),
103     }
104
105     _defaults = {
106         'active': lambda *a: True,
107     }
108
109 lunch_cashmove()
110
111
112 class lunch_order(osv.osv):
113     """ Apply lunch order """
114
115     _name = 'lunch.order'
116     _description = "Lunch Order"
117     _rec_name = "user_id"
118
119     def _price_get(self, cr, uid, ids, name, args, context=None):
120
121         """ Get Price of Product
122          @param cr: the current row, from the database cursor,
123          @param uid: the current user’s ID for security checks,
124          @param ids: List of Lunch order’s IDs
125          @param context: A standard dictionary for contextual values """
126
127         res = {}
128         for price in self.browse(cr, uid, ids, context=context):
129             res[price.id] = price.product.price
130         return res
131
132     _columns = {
133         'user_id': fields.many2one('res.users', 'User Name', required=True, \
134             readonly=True, states={'draft':[('readonly', False)]}),
135         'product': fields.many2one('lunch.product', 'Product', required=True, \
136             readonly=True, states={'draft':[('readonly', False)]}, change_default=True),
137         'date': fields.date('Date', readonly=True, states={'draft':[('readonly', False)]}),
138         'cashmove': fields.many2one('lunch.cashmove', 'CashMove' , readonly=True),
139         'descript': fields.char('Description Order', readonly=True, size=250, \
140             states = {'draft':[('readonly', False)]}),
141         'state': fields.selection([('draft', 'Draft'), ('confirmed', 'Confirmed'), ], \
142             'State', readonly=True, select=True),
143         'price': fields.function(_price_get, method=True, string="Price"),
144         'category': fields.many2one('lunch.category','Category'),
145     }
146
147     _defaults = {
148         'user_id': lambda self, cr, uid, context: uid,
149         'date': lambda self, cr, uid, context: time.strftime('%Y-%m-%d'),
150         'state': lambda self, cr, uid, context: 'draft',
151     }
152
153     def confirm(self, cr, uid, ids, box, context=None):
154
155         """ confirm order
156         @param cr: the current row, from the database cursor,
157         @param uid: the current user’s ID for security checks,
158         @param ids: List of confirm order’s IDs
159         @param context: A standard dictionary for contextual values """
160
161         cashmove_ref = self.pool.get('lunch.cashmove')
162         for order in self.browse(cr, uid, ids, context=context):
163             if order.state == 'confirmed':
164                 continue
165             new_id = cashmove_ref.create(cr, uid, {'name': order.product.name+' order',
166                             'amount':-order.product.price,
167                             'user_cashmove':order.user_id.id,
168                             'box':box,
169                             'active':True,
170                             })
171             self.write(cr, uid, [order.id], {'cashmove': new_id, 'state': 'confirmed'})
172         return {}
173
174     def lunch_order_cancel(self, cr, uid, ids, context=None):
175
176         """" cancel order
177          @param cr: the current row, from the database cursor,
178          @param uid: the current user’s ID for security checks,
179          @param ids: List of create menu’s IDs
180          @param context: A standard dictionary for contextual values """
181
182         orders = self.browse(cr, uid, ids, context=context)
183         for order in orders:
184             if not order.cashmove:
185                 continue
186         if order.cashmove.id:
187             self.pool.get('lunch.cashmove').unlink(cr, uid, [order.cashmove.id])
188         self.write(cr, uid, ids, {'state':'draft'})
189         return {}
190
191     def onchange_product(self, cr, uid, ids, product):
192
193         """ Get price for Product
194          @param cr: the current row, from the database cursor,
195          @param uid: the current user’s ID for security checks,
196          @param ids: List of create menu’s IDs
197          @product: Product To Ordered """
198
199         if not product:
200             return {'value': {'price': 0.0}}
201         price = self.pool.get('lunch.product').read(cr, uid, product, ['price'])['price']
202         categ_id = self.pool.get('lunch.product').browse(cr, uid, product).category_id.id
203         return {'value': {'price': price,'category':categ_id}}
204
205 lunch_order()
206
207
208 class report_lunch_amount(osv.osv):
209     """ Lunch Amount Report """
210
211     _name = 'report.lunch.amount'
212     _description = "Amount available by user and box"
213     _auto = False
214     _rec_name = "user"
215
216     _columns = {
217         'user_id': fields.many2one('res.users', 'User Name', readonly=True),
218         'amount': fields.float('Amount', readonly=True, digits=(16, 2)),
219         'box': fields.many2one('lunch.cashbox', 'Box Name', size=30, readonly=True),
220         'year': fields.char('Year', size=4, readonly=True),
221         'month':fields.selection([('01','January'), ('02','February'), ('03','March'), ('04','April'),
222             ('05','May'), ('06','June'), ('07','July'), ('08','August'), ('09','September'),
223             ('10','October'), ('11','November'), ('12','December')], 'Month',readonly=True),
224         'day': fields.char('Day', size=128, readonly=True),
225         'date': fields.date('Created Date', readonly=True),
226     }
227
228     def init(self, cr):
229
230         """ @param cr: the current row, from the database cursor"""
231
232         cr.execute("""
233             create or replace view report_lunch_amount as (
234                 select
235                     min(lc.id) as id,
236                     to_date(to_char(lc.create_date, 'dd-MM-YYYY'),'dd-MM-YYYY') as date,
237                     to_char(lc.create_date, 'YYYY') as year,
238                     to_char(lc.create_date, 'MM') as month,
239                     to_char(lc.create_date, 'YYYY-MM-DD') as day,
240                     lc.user_cashmove as user_id,
241                     sum(amount) as amount,
242                     lc.box as box
243                 from
244                     lunch_cashmove lc
245                 where
246                     active = 't'
247                 group by lc.user_cashmove, lc.box, lc.create_date
248                 )""")
249
250 report_lunch_amount()
251
252 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
253