fd7b3a5d7b2392f9aa0ca6881acf42881617fd16
[odoo/odoo.git] / addons / point_of_sale / wizard / pos_box_entries.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 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 import netsvc
23 from osv import osv,fields
24 from tools.translate import _
25 from tools.misc import UpdateableStr
26 from mx import DateTime
27 from tools.translate import _
28 import time
29
30 def get_journal(self,cr,uid,context):
31     """ 
32              Make the selection list of Cash Journal  .       
33              @param self: The object pointer.
34              @param cr: A database cursor
35              @param uid: ID of the user currently logged in
36              @param context: A standard dictionary 
37              @return :Return the list of journal 
38     """            
39         
40     obj = self.pool.get('account.journal')
41     user = self.pool.get('res.users').browse(cr, uid, uid)
42     ids = obj.search(cr, uid, [('type', '=', 'cash'), ('company_id', '=', user.company_id.id)])
43     res = obj.read(cr, uid, ids, ['id', 'name'], context)
44     res = [(r['id'], r['name']) for r in res]
45     res.insert(0, ('', ''))
46     return res
47
48 class pos_box_entries(osv.osv_memory):
49     _name = 'pos.box.entries'
50     _description = 'Pos Box Entries'
51
52
53     
54     def _get_income_product(self,cr,uid,context):
55         
56         """ 
57              Make the selection list of purchasing  products.            
58              @param self: The object pointer.
59              @param cr: A database cursor
60              @param uid: ID of the user currently logged in
61              @param context: A standard dictionary 
62              @return :Return of operation of product 
63         """                  
64         obj = self.pool.get('product.product')
65         ids = obj.search(cr, uid, [('income_pdt', '=', True)])
66         res = obj.read(cr, uid, ids, ['id', 'name'], context)
67         res = [(r['id'], r['name']) for r in res]
68         res.insert(0, ('', ''))
69         
70         return res
71     
72     
73     _columns = {
74                 'name': fields.char('Name', size=32,required=True),
75                 'journal_id': fields.selection(get_journal, "Journal",required=True),
76                 'product_id': fields.selection(_get_income_product, "Operation",required=True),
77                 'amount' :fields.float('Amount', digits=(16,2)),
78                 'ref':fields.char('Ref', size=32),
79     }
80     _defaults = {
81                  'journal_id': lambda *a: 1,
82                  'product_id': lambda *a: 1,
83                 }
84     def get_in(self, cr, uid, ids, context):
85
86         
87         """ 
88              Create the entry of statement in journal   .            
89              @param self: The object pointer.
90              @param cr: A database cursor
91              @param uid: ID of the user currently logged in
92              @param context: A standard dictionary 
93              @return :Return of operation of product 
94         """         
95         statement_obj = self.pool.get('account.bank.statement')
96         product_obj = self.pool.get('product.template')
97         res_obj = self.pool.get('res.users')
98         product_obj=self.pool.get('product.product')
99         bank_statement=self.pool.get('account.bank.statement.line')
100         for data in  self.read(cr, uid, ids):
101             args = {}
102             curr_company = res_obj.browse(cr,uid,uid).company_id.id
103             statement_id = statement_obj.search(cr,uid, [('journal_id','=',data['journal_id']),('company_id','=',curr_company),('user_id','=',uid),('state','=','open')])
104             if not statement_id:
105                 raise osv.except_osv(_('Error !'), _('You have to open at least one cashbox'))
106         
107             product = product_obj.browse(cr, uid, data['product_id'])
108             acc_id = product_obj.browse(cr,uid,data['product_id']).property_account_income
109             if not acc_id:
110                  raise osv.except_osv(_('Error !'), _('please check that account is set to %s')%(product_obj.browse(cr,uid,data['product_id']).name))
111             if statement_id:
112                 statement_id = statement_id[0]
113             if not statement_id:
114                 statement_id = statement_obj.create(cr,uid,{'date':time.strftime('%Y-%m-%d 00:00:00'),
115                                                             'journal_id':data['journal_id'],
116                                                             'company_id':curr_company,
117                                                             'user_id':uid,
118                                                            })
119         
120             args['statement_id'] = statement_id
121             args['journal_id'] =  data['journal_id']
122             if acc_id:
123                 args['account_id'] =  acc_id.id
124             args['amount'] = data['amount'] or 0.0
125             args['ref'] = "%s" %(data['ref'] or '')
126             args['name'] = "%s: %s "% (product_obj.browse(cr,uid,data['product_id']).name, data['name'].decode('utf8'))
127             address_u = res_obj.browse(cr,uid,uid).address_id
128             if address_u:
129                 partner_id = address_u.partner_id and address_u.partner_id.id or None
130                 args['partner_id'] = partner_id
131             statement_line_id =bank_statement.create(cr, uid, args)
132         
133             return {}
134 pos_box_entries()