1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import osv, fields
24 from tools.translate import _
25 from mx import DateTime
26 import pos_box_entries
29 class pos_box_out(osv.osv_memory):
31 _description = 'Pos Box Out'
33 def _get_expense_product(self, cr, uid, context):
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
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, ('', ''))
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),
59 'journal_id': lambda *a: 1,
60 'product_id': lambda *a: 1,
62 def get_out(self, cr, uid, ids, context):
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
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
88 amount_check = productp_obj.browse(cr, uid, data['product_id']).am_out or False
91 if address_u and s.partner_id == address_u.partner_id and s.am_out:
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))
97 acc_id = product_obj.browse(cr, uid, data['product_id']).property_account_income
99 raise osv.except_osv(_('Error !'), _('please check that account is set to %s')%(product_obj.browse(cr, uid, data['product_id']).name))
101 raise osv.except_osv(_('Error !'), _('You have to open at least one cashbox'))
103 statement_id = statement_id[0]
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,
110 args['statement_id'] = statement_id
111 args['journal_id'] = data['journal_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
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)