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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution	
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
#    $Id$
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import osv, fields
import time

class lunch_category(osv.osv):
    _name = 'lunch.category'
    _description = "Category"

    _columns = {
        'name': fields.char('Name', required=True, size=50),
    }

    _order = 'name'

lunch_category()

class lunch_product(osv.osv):
    _name = 'lunch.product'

    def _category_name_get(self, cr, uid, context={}):
        obj = self.pool.get('lunch.category')
        cat_ids= obj.search(cr,uid,[])
        res = obj.read(cr,uid,cat_ids,['name', 'category'])
        return [(str(r['id']), r['name']) for r in res]+ [('0','')]

    _columns = {
        'name': fields.char('Name', size=50, required=True),
        'category_id': fields.selection(_category_name_get, 'Category', size=32),
        'description': fields.char('Description', size=128, required=False),
        'price': fields.float('Price', digits=(16,2)),
        'active': fields.boolean('Active'),
    }

    _defaults = {
        'active': lambda *a : True,
        }

lunch_product()

class lunch_cashbox(osv.osv):
    _name='lunch.cashbox'

    def amount_available(self, cr, uid, ids, field_name, arg, context):
        cr.execute("SELECT box,sum(amount) from lunch_cashmove where active = 't' group by box")
        r = dict(cr.fetchall())
        for i in ids :
            r.setdefault(i,0)
        return r

    _columns={
        'manager':fields.many2one('res.users','Manager'),
        'name':fields.char('Name',size=30,required=True, unique = True),
        'sum_remain': fields.function(amount_available, method=True, string='Remained Total'),
        }

lunch_cashbox()




class lunch_cashmove(osv.osv):
    _name= 'lunch.cashmove'

    _columns={
        'name': fields.char('Name',size=128),
        'user_cashmove': fields.many2one('res.users','User Name', required=True),
        'amount': fields.float('Amount', digits=(16,2)),
        'box':fields.many2one('lunch.cashbox','Box Name',size=30,required=True),
        'active':fields.boolean('Active'),
        'create_date': fields.datetime('Created date', readonly=True),
        }

    _defaults={
    'active': lambda *a: True,
    }

lunch_cashmove()



class lunch_order(osv.osv):
    _name='lunch.order'
    _rec_name= "user_id"

    def _price_get(self, cr, uid, ids, name, args, context=None):
        res = {}
        for o in self.browse(cr, uid, ids):
            res[o.id] = o.product.price
        return res

    _columns={
        'user_id': fields.many2one('res.users','User Name', required=True,
            readonly=True, states={'draft':[('readonly',False)]}),
        'product':fields.many2one('lunch.product','Product', required=True,
            readonly=True, states={'draft':[('readonly',False)]}, change_default=True),
        'date': fields.date('Date',readonly=True,states={'draft':[('readonly',False)]}),
        'cashmove':fields.many2one('lunch.cashmove', 'CashMove' , readonly=True  ),
        'descript':fields.char('Description Order', readonly=True, size=50,
            states={'draft':[('readonly',False)]}),
        'state': fields.selection([('draft','Draft'), ('confirmed','Confirmed'),],
            'State', readonly=True, select=True),
        'price': fields.function(_price_get, method=True, string="Price"),
    }

    _defaults={
        'user_id': lambda self,cr,uid,context: uid,
        'date': lambda self,cr,uid,context: time.strftime('%Y-%m-%d'),
        'state': lambda self,cr,uid,context: 'draft',
    }

    def confirm(self,cr,uid,ids,box,context):
        cashmove_ref= self.pool.get('lunch.cashmove')
        for order in self.browse(cr,uid,ids):
            if order.state == 'confirmed':
                continue
            new_id= cashmove_ref.create(cr,uid,{'name': order.product.name+' order',
                            'amount':-order.product.price,
                            'user_cashmove':order.user_id.id,
                            'box':box,
                            'active':True,
                            })
            self.write(cr,uid,[order.id],{'cashmove':new_id, 'state':'confirmed'})
        return {}

    def lunch_order_cancel(self,cr,uid,ids,context):
        orders= self.browse(cr,uid,ids)
        for order in orders:
            if not order.cashmove:
                continue
            self.pool.get('lunch.cashmove').unlink(cr, uid, [order.cashmove.id])
        self.write(cr,uid,ids,{'state':'draft'})
        return {}

    def onchange_product(self, cr, uid, ids, product):
        if not product:
            return {'value': {'price': 0.0}}
        price = self.pool.get('lunch.product').read(cr, uid, product, ['price'])['price']
        return {'value': {'price': price}}

lunch_order()

class report_lunch_amount(osv.osv):
    _name='report.lunch.amount'
    _description = "Amount available by user and box"
    _auto = False
    _rec_name= "user"

    _columns = {
        'user_id': fields.many2one('res.users','User Name',readonly=True),
        'amount': fields.float('Amount', readonly=True, digits=(16,2)),
        'box':fields.many2one('lunch.cashbox','Box Name',size=30,readonly=True),
        }

    def init(self, cr):
        cr.execute("""
            create or replace view report_lunch_amount as (
                select
                    min(lc.id) as id,
                    lc.user_cashmove as user_id,
                    sum(amount) as amount,
                    lc.box as box
                from
                    lunch_cashmove lc
                where
                    active = 't'
                group by lc.user_cashmove, lc.box
                )""")

report_lunch_amount()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: