[IMP] PoS backend
[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         return res
49
50     _columns = {
51         'name': fields.char('Description / Reason', size=32, required=True),
52         'journal_id': fields.selection(pos_box_entries.get_journal, "Cash Register", required=True, size=-1),
53         'product_id': fields.selection(_get_expense_product, "Operation", required=True, size=-1),
54         'amount': fields.float('Amount', digits=(16, 2), required=True),
55         'session_id' : fields.many2one('pos.session', 'Session'),
56         'user_id' : fields.many2one('res.users', 'User'),
57     }
58     _defaults = {
59         'journal_id': 1,
60         'product_id': 1,
61         'user_id' : lambda obj, cr, uid, context: uid,
62     }
63     def get_out(self, cr, uid, ids, context=None):
64
65         """
66          Create the entries in the CashBox   .
67          @param self: The object pointer.
68          @param cr: A database cursor
69          @param uid: ID of the user currently logged in
70          @param context: A standard dictionary
71          @return :Return of operation of product
72         """
73         vals = {}
74         statement_obj = self.pool.get('account.bank.statement')
75         statement_line_obj = self.pool.get('account.bank.statement.line')
76         product_obj = self.pool.get('product.product')
77         res_obj = self.pool.get('res.users')
78         for data in  self.read(cr, uid, ids, context=context):
79             curr_company = res_obj.browse(cr, uid, uid, context=context).company_id.id
80             statement_ids = statement_obj.search(cr, uid, [('journal_id', '=', data['journal_id']), ('company_id', '=', curr_company), ('user_id', '=', uid), ('state', '=', 'open')], context=context)
81             monday = (datetime.today() + relativedelta(weekday=0)).strftime('%Y-%m-%d')
82             sunday = (datetime.today() + relativedelta(weekday=6)).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)], context=context)
84             stat_done = statement_obj.browse(cr, uid, done_statmt, context=context)
85             am = 0.0
86             product = product_obj.browse(cr, uid, data['product_id'], context=context)
87             acc_id = product.property_account_expense or product.categ_id.property_account_expense_categ
88             if not acc_id:
89                 raise osv.except_osv(_('Error!'), _('please check that account is set to %s.')%(product.name))
90             if not statement_ids:
91                 raise osv.except_osv(_('Error!'), _('You have to open at least one cashbox.'))
92             vals['statement_id'] = statement_ids[0]
93             vals['journal_id'] = data['journal_id']
94             vals['account_id'] = acc_id.id
95             amount = data['amount'] or 0.0
96             if data['amount'] > 0:
97                 amount = -data['amount']
98             vals['amount'] = amount
99             vals['name'] = "%s: %s " % (product.name, data['name'])
100             statement_line_obj.create(cr, uid, vals, context=context)
101         return {}
102
103 pos_box_out()
104
105
106 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: