52d2be0a3d079eb05663f9218dfd1ddcf5cbe3e2
[odoo/odoo.git] / addons / point_of_sale / wizard / pos_box_out.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 pos_box_entries
29 print  dir(pos_box_entries)
30
31 class pos_box_out(osv.osv_memory):
32     _name = 'pos.box.out'
33     _description = 'Pos Box Out'
34     
35     def _get_expense_product(self,cr,uid,context):
36         
37         """ 
38              Make the selection list of expense product.            
39              @param self: The object pointer.
40              @param cr: A database cursor
41              @param uid: ID of the user currently logged in
42              @param context: A standard dictionary 
43              @return :Return of operation of product 
44         """            
45         obj = self.pool.get('product.product')
46         company_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.id
47         ids = obj.search(cr, uid, ['&', ('expense_pdt', '=', True), '|', ('company_id', '=', company_id), ('company_id', '=', None)])
48         res = obj.read(cr, uid, ids, ['id', 'name'], context)
49         res = [(r['id'], r['name']) for r in res]
50         res.insert(0, ('', ''))
51         return res    
52     
53     _columns = {
54                 'name': fields.char('Name', size=32,required=True),
55                 'journal_id': fields.selection(pos_box_entries.get_journal, "Journal",required=True),
56                 'product_id': fields.selection(_get_expense_product, "Operation",required=True),
57                 'amount' :fields.float('Amount', digits=(16,2)),
58                 'ref':fields.char('Ref', size=32),
59     }
60     _defaults = {
61                  'journal_id': lambda *a: 1,
62                  'product_id': lambda *a: 1,
63                 }
64     def get_out(self, cr, uid, ids, context):
65         
66         """ 
67              Create the entries in the CashBox   .            
68              @param self: The object pointer.
69              @param cr: A database cursor
70              @param uid: ID of the user currently logged in
71              @param context: A standard dictionary 
72              @return :Return of operation of product 
73         """         
74         args = {}
75         statement_obj= self.pool.get('account.bank.statement')
76         statement_line_obj=self.pool.get('account.bank.statement.line')
77         product_obj= self.pool.get('product.template')
78         productp_obj= self.pool.get('product.product')
79         res_obj = self.pool.get('res.users')
80         for data in  self.read(cr, uid, ids):
81                         
82             curr_company = res_obj.browse(cr,uid,uid).company_id.id
83             statement_id = statement_obj.search(cr,uid, [('journal_id','=',data['journal_id']),('company_id','=',curr_company),('user_id','=',uid),('state','=','open')])
84             monday = (DateTime.now() + DateTime.RelativeDateTime(weekday=(DateTime.Monday,0))).strftime('%Y-%m-%d')
85             sunday = (DateTime.now() + DateTime.RelativeDateTime(weekday=(DateTime.Sunday,0))).strftime('%Y-%m-%d')
86             done_statmt = statement_obj.search(cr,uid, [('date','>=',monday+' 00:00:00'),('date','<=',sunday+' 23:59:59'),('journal_id','=',data['journal_id']),('company_id','=',curr_company),('user_id','=',uid)])
87             stat_done = statement_obj.browse(cr,uid, done_statmt)
88             address_u = res_obj.browse(cr,uid,uid).address_id
89             am = 0.0
90             
91             amount_check = productp_obj.browse(cr,uid,data['product_id']).am_out or False
92             for st in stat_done:
93                 for s in st.line_ids:
94                     if address_u and s.partner_id==address_u.partner_id and s.am_out:
95                         am+=s.amount
96             if (-data['amount'] or 0.0)+ am <-(res_obj.browse(cr,uid,uid).company_id.max_diff or 0.0) and amount_check:
97                 val = (res_obj.browse(cr,uid,uid).company_id.max_diff or 0.0)+ am
98                 raise osv.except_osv(_('Error !'), _('The maximum value you can still withdraw is exceeded. \n Remaining value is equal to %d ')%(val))
99         
100             acc_id = product_obj.browse(cr,uid,data['product_id']).property_account_income
101             if not acc_id:
102                  raise osv.except_osv(_('Error !'), _('please check that account is set to %s')%(product_obj.browse(cr,uid,data['product_id']).name))
103             if not statement_id:
104                  raise osv.except_osv(_('Error !'), _('You have to open at least one cashbox'))
105             if statement_id:
106                 statement_id = statement_id[0]
107             if not statement_id:
108                 statement_id = statement_obj.create(cr,uid,{'date':time.strftime('%Y-%m-%d 00:00:00'),
109                                                 'journal_id':data['journal_id'],
110                                                 'company_id':curr_company,
111                                                 'user_id':uid,
112                                                 })
113             args['statement_id']= statement_id
114             args['journal_id']= data['journal_id']
115             if acc_id:
116                 args['account_id']= acc_id.id
117             amount= data['amount'] or 0.0
118             if data['amount'] > 0:
119                 amount= -data['amount']
120             args['amount'] = amount
121             if productp_obj.browse(cr,uid,data['product_id']).am_out:
122                 args['am_out'] = True
123             args['ref'] = data['ref'] or ''
124             args['name'] = "%s: %s "%(product_obj.browse(cr,uid,data['product_id']).name, data['name'].decode('utf8'))
125             address_u = res_obj.browse(cr,uid,uid).address_id
126             if address_u:
127                 partner_id = address_u.partner_id and address_u.partner_id.id or None
128                 args['partner_id'] = partner_id
129             statement_line_id = statement_line_obj.create(cr, uid, args)
130         return {}
131
132 pos_box_out()