~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to lunch/lunch.py

  • Committer: Fabien Pinckaers
  • Date: 2008-11-12 06:43:12 UTC
  • Revision ID: fp@tinyerp.com-20081112064312-fp85io97i1e95tuz
moved

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution   
 
5
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
1
22
from osv import osv, fields
2
23
import time
3
24
 
4
25
class lunch_category(osv.osv):
5
 
        _name = 'lunch.category'
6
 
        _description = "Category"
7
 
 
8
 
        _columns = {
9
 
                'name': fields.char('Name', required=True, size=50),
10
 
        }
11
 
 
12
 
        _order = 'name'
 
26
    _name = 'lunch.category'
 
27
    _description = "Category"
 
28
 
 
29
    _columns = {
 
30
        'name': fields.char('Name', required=True, size=50),
 
31
    }
 
32
 
 
33
    _order = 'name'
13
34
 
14
35
lunch_category()
15
36
 
16
37
class lunch_product(osv.osv):
17
 
        _name = 'lunch.product'
18
 
 
19
 
        def _category_name_get(self, cr, uid, context={}):
20
 
                obj = self.pool.get('lunch.category')
21
 
                cat_ids= obj.search(cr,uid,[])
22
 
                res = obj.read(cr,uid,cat_ids,['name', 'category'])
23
 
                return [(str(r['id']), r['name']) for r in res]+ [('0','')]
24
 
 
25
 
        _columns = {
26
 
                'name': fields.char('Name', size=50, required=True),
27
 
                'category_id': fields.selection(_category_name_get, 'Category', size=32),
28
 
                'description': fields.char('Description', size=128, required=False),
29
 
                'price': fields.float('Price', digits=(16,2)),
30
 
        }
 
38
    _name = 'lunch.product'
 
39
 
 
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','')]
 
45
 
 
46
    _columns = {
 
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'),
 
52
    }
 
53
 
 
54
    _defaults = {
 
55
        'active': lambda *a : True,
 
56
        }
31
57
 
32
58
lunch_product()
33
59
 
34
60
class lunch_cashbox(osv.osv):
35
 
        _name='lunch.cashbox'
36
 
 
37
 
        def amount_available(self, cr, uid, ids, field_name, arg, context):
38
 
                cr.execute("SELECT box,sum(amount) from lunch_cashmove where active = 't' group by box")
39
 
                r = dict(cr.fetchall())
40
 
                for i in ids :
41
 
                        r.setdefault(i,0)
42
 
                return r
43
 
 
44
 
        _columns={
45
 
                'manager':fields.many2one('res.users','Manager'),
46
 
                'name':fields.char('Name',size=30,required=True, unique = True),
47
 
                'sum_remain': fields.function(amount_available, method=True, string='Remained Total'),
48
 
                }
 
61
    _name='lunch.cashbox'
 
62
 
 
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())
 
66
        for i in ids :
 
67
            r.setdefault(i,0)
 
68
        return r
 
69
 
 
70
    _columns={
 
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'),
 
74
        }
49
75
 
50
76
lunch_cashbox()
51
77
 
53
79
 
54
80
 
55
81
class lunch_cashmove(osv.osv):
56
 
        _name= 'lunch.cashmove'
57
 
 
58
 
        _columns={
59
 
                'name': fields.char('Name',size=128),
60
 
                'user_cashmove': fields.many2one('res.users','User Name', required=True),
61
 
                'amount': fields.float('Amount', digits=(16,2)),
62
 
                'box':fields.many2one('lunch.cashbox','Box Name',size=30,required=True),
63
 
                'active':fields.boolean('Active'),
64
 
                'create_date': fields.datetime('Created date', readonly=True),
65
 
                }
66
 
 
67
 
        _defaults={
68
 
        'active': lambda *a: True,
69
 
        }
 
82
    _name= 'lunch.cashmove'
 
83
 
 
84
    _columns={
 
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),
 
91
        }
 
92
 
 
93
    _defaults={
 
94
    'active': lambda *a: True,
 
95
    }
70
96
 
71
97
lunch_cashmove()
72
98
 
73
99
 
74
100
 
75
101
class lunch_order(osv.osv):
76
 
        _name='lunch.order'
77
 
        _rec_name= "user_id"
78
 
 
79
 
        def _price_get(self, cr, uid, ids, name, args, context=None):
80
 
                res = {}
81
 
                for o in self.browse(cr, uid, ids):
82
 
                        res[o.id] = o.product.price
83
 
                return res
84
 
 
85
 
        _columns={
86
 
                'user_id': fields.many2one('res.users','User Name', required=True,
87
 
                        readonly=True, states={'draft':[('readonly',False)]}),
88
 
                'product':fields.many2one('lunch.product','Product', required=True,
89
 
                        readonly=True, states={'draft':[('readonly',False)]}, change_default=True),
90
 
                'date': fields.date('Date',readonly=True,states={'draft':[('readonly',False)]}),
91
 
                'cashmove':fields.many2one('lunch.cashmove', 'CashMove' , readonly=True  ),
92
 
                'descript':fields.char('Description Order', readonly=True, size=50,
93
 
                        states={'draft':[('readonly',False)]}),
94
 
                'state': fields.selection([('draft','Draft'), ('confirmed','Confirmed'),],
95
 
                        'State', readonly=True, select=True),
96
 
                'price': fields.function(_price_get, method=True, string="Price"),
97
 
        }
98
 
 
99
 
        _defaults={
100
 
                'user_id': lambda self,cr,uid,context: uid,
101
 
                'date': lambda self,cr,uid,context: time.strftime('%Y-%m-%d'),
102
 
                'state': lambda self,cr,uid,context: 'draft',
103
 
        }
104
 
 
105
 
        def confirm(self,cr,uid,ids,box,context):
106
 
                cashmove_ref= self.pool.get('lunch.cashmove')
107
 
                for order in self.browse(cr,uid,ids):
108
 
                        if order.state == 'confirmed':
109
 
                                continue
110
 
                        new_id= cashmove_ref.create(cr,uid,{'name': order.product.name+' order',
111
 
                                                        'amount':-order.product.price,
112
 
                                                        'user_cashmove':order.user_id.id,
113
 
                                                        'box':box,
114
 
                                                        'active':True,
115
 
                                                        })
116
 
                        self.write(cr,uid,[order.id],{'cashmove':new_id, 'state':'confirmed'})
117
 
                return {}
118
 
 
119
 
        def lunch_order_cancel(self,cr,uid,ids,context):
120
 
                orders= self.browse(cr,uid,ids)
121
 
                for order in orders:
122
 
                        if not order.cashmove:
123
 
                                continue
124
 
                        self.pool.get('lunch.cashmove').unlink(cr, uid, [order.cashmove.id])
125
 
                self.write(cr,uid,ids,{'state':'draft'})
126
 
                return {}
127
 
 
128
 
        def onchange_product(self, cr, uid, ids, product):
129
 
                if not product:
130
 
                        return {'value': {'price': 0.0}}
131
 
                price = self.pool.get('lunch.product').read(cr, uid, product, ['price'])['price']
132
 
                return {'value': {'price': price}}
 
102
    _name='lunch.order'
 
103
    _rec_name= "user_id"
 
104
 
 
105
    def _price_get(self, cr, uid, ids, name, args, context=None):
 
106
        res = {}
 
107
        for o in self.browse(cr, uid, ids):
 
108
            res[o.id] = o.product.price
 
109
        return res
 
110
 
 
111
    _columns={
 
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"),
 
123
    }
 
124
 
 
125
    _defaults={
 
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',
 
129
    }
 
130
 
 
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':
 
135
                continue
 
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,
 
139
                            'box':box,
 
140
                            'active':True,
 
141
                            })
 
142
            self.write(cr,uid,[order.id],{'cashmove':new_id, 'state':'confirmed'})
 
143
        return {}
 
144
 
 
145
    def lunch_order_cancel(self,cr,uid,ids,context):
 
146
        orders= self.browse(cr,uid,ids)
 
147
        for order in orders:
 
148
            if not order.cashmove:
 
149
                continue
 
150
            self.pool.get('lunch.cashmove').unlink(cr, uid, [order.cashmove.id])
 
151
        self.write(cr,uid,ids,{'state':'draft'})
 
152
        return {}
 
153
 
 
154
    def onchange_product(self, cr, uid, ids, product):
 
155
        if not 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}}
133
159
 
134
160
lunch_order()
135
161
 
136
162
class report_lunch_amount(osv.osv):
137
 
        _name='report.lunch.amount'
138
 
        _description = "Amount available by user and box"
139
 
        _auto = False
140
 
        _rec_name= "user"
141
 
 
142
 
        _columns = {
143
 
                'user_id': fields.many2one('res.users','User Name',readonly=True),
144
 
                'amount': fields.float('Amount', readonly=True, digits=(16,2)),
145
 
                'box':fields.many2one('lunch.cashbox','Box Name',size=30,readonly=True),
146
 
                }
147
 
 
148
 
        def init(self, cr):
149
 
                cr.execute("""
150
 
                        create or replace view report_lunch_amount as (
151
 
                                select
152
 
                                        min(lc.id) as id,
153
 
                                        lc.user_cashmove as user_id,
154
 
                                        sum(amount) as amount,
155
 
                                        lc.box as box
156
 
                                from
157
 
                                        lunch_cashmove lc
158
 
                                where
159
 
                                        active = 't'
160
 
                                group by lc.user_cashmove, lc.box
161
 
                                )""")
 
163
    _name='report.lunch.amount'
 
164
    _description = "Amount available by user and box"
 
165
    _auto = False
 
166
    _rec_name= "user"
 
167
 
 
168
    _columns = {
 
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),
 
172
        }
 
173
 
 
174
    def init(self, cr):
 
175
        cr.execute("""
 
176
            create or replace view report_lunch_amount as (
 
177
                select
 
178
                    min(lc.id) as id,
 
179
                    lc.user_cashmove as user_id,
 
180
                    sum(amount) as amount,
 
181
                    lc.box as box
 
182
                from
 
183
                    lunch_cashmove lc
 
184
                where
 
185
                    active = 't'
 
186
                group by lc.user_cashmove, lc.box
 
187
                )""")
162
188
 
163
189
report_lunch_amount()
 
190
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
191