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