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