~koi-accounting-modules-maintainer/koi-accounting-modules/7.0-fixed-asset

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from osv import osv,fields
from tools.translate import _

class wizard_cash_receipt(osv.osv_memory):
    _name = 'account.wizard_cash_receipt'
    _description = 'Wizard Cash Receipt'

    _columns = {
                'journal_id' : fields.many2one(string='Journal', obj='account.journal', required=True),
                'name' : fields.char(string='Description', size=100, required=True),
                'date':fields.date(string='Date', required=True),
                }

    def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):
        res = super(wizard_cash_receipt, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
        x = []
        mod_obj = self.pool.get('ir.model.data')
        obj_account_voucher_type = self.pool.get('account.voucher_type')
        if context is None: context = {}

        voucher_type = context.get('voucher_type')

        if voucher_type and view_type == 'form':

                kriteria = [('name','=',voucher_type)]
                voucher_type_ids = obj_account_voucher_type.search(cr, uid, kriteria)[0]

                voucher = obj_account_voucher_type.browse(cr, uid, voucher_type_ids, context=context)

                result = mod_obj.get_object_reference(cr, uid, voucher.modul_origin, voucher.model_view_form)
                result = result and result[1] or False
                view_id = result
                
                if voucher.allowed_journal_ids:
                        for journal in voucher.allowed_journal_ids:
                                x.append(journal.id)
                domain_journal = list(set(x))

                for field in res['fields']:
                    if field == 'journal_id':
                        res['fields'][field]['domain'] = [('id','in',domain_journal)]
        return res

    def view_init(self, cr, uid, fields_list, context=None):

        obj_account_invoice = self.pool.get('account.invoice')

        if context is None:
            context = {}
            
        res = super(wizard_cash_receipt, self).view_init(cr, uid, fields_list, context=context)
        
        record_id = context and context.get('active_id', False)
        
        if record_id:

            invoice = obj_account_invoice.browse(cr, uid, record_id, context=context)

            if invoice.state != 'open' :
                raise osv.except_osv(_('Warning !'), _("You may only pay invoices that are Open !"))
        return res

    def get_total(self, cr, uid, context=None):
        obj_account_invoice = self.pool.get('account.invoice')
        record_id = context.get('active_ids')
        total = 0.00

        for data in record_id:
            invoice_id = obj_account_invoice.browse(cr, uid, data)
            total = total + invoice_id.residual
        
        return total
                            
    def cash_receipt(self, cr, uid, ids, context=None):
        obj_account_cash_receipt = self.pool.get('account.cash_receipt')
        obj_account_voucher_line = self.pool.get('account.voucher.line')
        obj_account_move_line = self.pool.get('account.move.line')
        obj_account_invoice = self.pool.get('account.invoice')
        obj_account_journal = self.pool.get('account.journal')
        obj_account_voucher_type = self.pool.get('account.voucher_type')
        obj_account_period = self.pool.get('account.period')

        record_id = context.get('active_ids')

        wizard = self.read(cr, uid, ids[0], context=context)

        #raise osv.except_osv(_('Error !'), _('%s')%record_id[0])

        journal = obj_account_journal.browse(cr, uid, wizard['journal_id'][0])

        voucher_type_ids = obj_account_voucher_type.search(cr, uid, [('name','=','Cash Receipt')])[0]

        voucher_type = obj_account_voucher_type.browse(cr, uid, voucher_type_ids)
        
        period_id = obj_account_period.find(cr, uid, wizard['date'], context)

        val_header = {
                        'journal_id' : wizard['journal_id'][0],
                        'name' : wizard['name'],
                        'date' : wizard['date'],
                        'account_id' : journal.default_debit_account_id.id,
                        'voucher_type_id' : voucher_type.id,
                        'type' : voucher_type.default_header_type,
                        'amount' : self.get_total(cr, uid, context),
                        'period_id' : period_id[0],
                        }

        new_account_cash_receipt_id = obj_account_cash_receipt.create(cr, uid, val_header, context)

        for data in record_id:
            invoice_id = obj_account_invoice.browse(cr, uid, data)

            move_line_ids = obj_account_move_line.search(cr, uid, [('move_id','=',invoice_id.move_id.id)])

            for move_line in obj_account_move_line.browse(cr, uid, move_line_ids):
                if move_line.account_id.type == 'receivable':
                    amount = obj_account_voucher_line.compute_amount(cr, uid, move_line.id, move_line.journal_id.id, move_line.currency_id.id)['amount']
                    val = {
                            'voucher_id' : new_account_cash_receipt_id,
                            'account_id' : move_line.account_id.id,
                            'move_line_id' : move_line.id,
                            'name' : wizard['name'],
                            'amount' : amount,
                            'type' : 'cr',
                            'partner_id' : move_line.partner_id.id,
                            }
                    new_account_cash_receipt_detail_id = obj_account_voucher_line.create(cr, uid, val, context)

        return  {
                        'res_id' : new_account_cash_receipt_id,
                        'name' : 'Cash Receipt',
                        'view_type' : 'form',
                        'view_mode' : 'form',
                        'res_model' : 'account.cash_receipt', #TODO
                        'type' : 'ir.actions.act_window',
                        'context' : context,
                        }
        
wizard_cash_receipt()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: