[fIX]
[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 time
23 from datetime import datetime
24 from dateutil.relativedelta import relativedelta
25
26 from osv import osv, fields
27 from tools.translate import _
28 import pos_box_entries
29
30 class pos_box_out(osv.osv_memory):
31     _name = 'pos.box.out'
32     _description = 'Pos Box Out'
33
34     def _get_expense_product(self, cr, uid, context=None):
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         product_obj = self.pool.get('product.product')
44         company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
45         ids = product_obj.search(cr, uid, ['&', ('expense_pdt', '=', True), '|', ('company_id', '=', company_id), ('company_id', '=', None)], context=context)
46         res = product_obj.read(cr, uid, ids, ['id', 'name'], context=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('Description', size=32, required=True),
53         'journal_id': fields.selection(pos_box_entries.get_journal, "Cash Register", 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': 1,
60          'product_id': 1,
61     }
62     def get_out(self, cr, uid, ids, context=None):
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         vals = {}
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.product')
76         res_obj = self.pool.get('res.users')
77         for data in  self.read(cr, uid, ids, context=context):
78             curr_company = res_obj.browse(cr, uid, uid, context=context).company_id.id
79             statement_id = statement_obj.search(cr, uid, [('journal_id', '=', data['journal_id']), ('company_id', '=', curr_company), ('user_id', '=', uid), ('state', '=', 'open')], context=context)
80             monday = (datetime.today() + relativedelta(weekday=0)).strftime('%Y-%m-%d')
81             sunday = (datetime.today() + relativedelta(weekday=6)).strftime('%Y-%m-%d')
82             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)], context=context)
83             stat_done = statement_obj.browse(cr, uid, done_statmt, context=context)
84             am = 0.0
85             product = product_obj.browse(cr, uid, data['product_id'], context=context)
86             acc_id = product.property_account_income
87             if not acc_id:
88                 raise osv.except_osv(_('Error !'), _('please check that account is set to %s')%(product.name))
89             if not statement_id:
90                 raise osv.except_osv(_('Error !'), _('You have to open at least one cashbox'))
91             if statement_id:
92                 statement_id = statement_id[0]
93             if not statement_id:
94                 statement_id = statement_obj.create(cr, uid, {
95                                     'date': time.strftime('%Y-%m-%d 00:00:00'),
96                                     'journal_id': data['journal_id'],
97                                     'company_id': curr_company,
98                                     'user_id': uid,
99                                 }, context=context)
100             vals['statement_id'] = statement_id
101             vals['journal_id'] = data['journal_id']
102             if acc_id:
103                 vals['account_id'] = acc_id.id
104             amount = data['amount'] or 0.0
105             if data['amount'] > 0:
106                 amount = -data['amount']
107             vals['amount'] = amount
108             vals['ref'] = data['ref'] or ''
109             vals['name'] = "%s: %s " % (product.name, data['name'])
110             statement_line_obj.create(cr, uid, vals, context=context)
111         return {}
112
113 pos_box_out()
114