~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to cci_account/cci_account.py

merging new development from indian accounting

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
from osv import fields, osv
 
3
import time
 
4
 
 
5
class account_move_line(osv.osv):
 
6
 
 
7
    def search(self, cr, user, args, offset=0, limit=None, order=None, context=None, count=False):
 
8
        # will check if the partner/account exists in statement lines if not then display all partner's account.move.line
 
9
        for item in args:
 
10
            if (item[0] in ('partner_id','account_id')) and (not item[2]):
 
11
                args.pop(args.index(item))
 
12
 
 
13
        return super(account_move_line,self).search(cr, user, args, offset, limit, order, context, count)
 
14
 
 
15
    _inherit = "account.move.line"
 
16
    _description = "account.move.line"
 
17
 
 
18
account_move_line()
 
19
 
 
20
 
 
21
class account_invoice(osv.osv):
 
22
 
 
23
    _inherit = "account.invoice"
 
24
    _columns = {
 
25
        'dept':fields.many2one('hr.department','Department'),
 
26
        'invoice_special':fields.boolean('Special Invoice'),
 
27
        'internal_note': fields.text('Internal Note'),
 
28
    }
 
29
 
 
30
 
 
31
    def action_move_create(self, cr, uid, ids, context=None):
 
32
        flag = False
 
33
        move_obj = self.pool.get('account.move')
 
34
        move_line_obj = self.pool.get('account.move.line')
 
35
        data_invoice = self.browse(cr,uid,ids[0])
 
36
        #raise an error if one of the account_invoice_line doesn't have an analytic entry
 
37
        for line in data_invoice.invoice_line:
 
38
            if not line.analytics_id:
 
39
                flag = True
 
40
        if flag:
 
41
            raise osv.except_osv('Error!','Invoice line should have Analytic Distribution to create Analytic Entries.')
 
42
        super(account_invoice, self).action_move_create(cr, uid, ids, context)
 
43
 
 
44
        #create other move lines if the invoice_line is related to a check payment or an AWEX credence
 
45
        for inv in self.browse(cr, uid, ids):
 
46
            for item in self.pool.get('account.invoice.line').search(cr, uid, [('invoice_id','=',inv.id)]):
 
47
                line = self.pool.get('account.invoice.line').browse(cr,uid, [item])[0]
 
48
                if line.cci_special_reference:
 
49
                    iml = []
 
50
                    if inv.type in ('in_invoice', 'in_refund'):
 
51
                        ref = inv.reference
 
52
                    else:
 
53
                        ref = self._convert_ref(cr, uid, inv.number)
 
54
                    temp = line.cci_special_reference.split('*')
 
55
                    obj = temp[0]
 
56
                    obj_id = int(temp[1])
 
57
                    obj_ref = self.pool.get(obj).browse(cr, uid, [obj_id])[0]
 
58
                    if obj == "event.registration":
 
59
                        #acc_id = self.pool.get('account.account').search(cr, uid, [('name','=','Creances AWEX - Cheques Formations et Cheques Langues')])[0]
 
60
                        journal_id = self.pool.get('account.journal').search(cr, uid, [('name','=','CFL Journal')])[0]
 
61
                        amount = obj_ref.check_amount
 
62
                    else:
 
63
                        journal_id = self.pool.get('account.journal').search(cr, uid, [('name','=','AWEX Journal')])[0]
 
64
                        #acc_id = self.pool.get('account.account').search(cr, uid, [('name','=','Creances AWEX - Cheques Formations et Cheques Langues')])[0]
 
65
                        amount = obj_ref.awex_amount
 
66
                    acc_id = self.pool.get('account.journal').browse(cr, uid, [journal_id])[0].default_debit_account_id.id
 
67
                    iml.append({
 
68
                        'type': 'dest',
 
69
                        'name': inv['name'] or '/',
 
70
                        'price': amount,
 
71
                        'account_id': acc_id,
 
72
                        'date_maturity': inv.date_due or False,
 
73
                        'amount_currency': False,
 
74
                        'currency_id': inv.currency_id.id or False,
 
75
                        'ref': ref,
 
76
                    })
 
77
                    iml.append({
 
78
                        'type': 'dest',
 
79
                        'name': inv['name'] or '/',
 
80
                        'price': -(amount),
 
81
                        'account_id': inv.account_id.id,
 
82
                        'date_maturity': inv.date_due or False,
 
83
                        'amount_currency': False,
 
84
                        'currency_id': inv.currency_id.id or False,
 
85
                        'ref': ref,
 
86
                    })
 
87
                    date = inv.date_invoice
 
88
                    part = inv.partner_id.id
 
89
                    new_lines = map(lambda x:(0,0,self.line_get_convert(cr, uid, x, part, date, context={})) ,iml)
 
90
                    for item in new_lines:
 
91
                        if item[2]['credit']:
 
92
                            id1 = item[2]['credit']
 
93
 
 
94
                    journal = self.pool.get('account.journal').browse(cr, uid, journal_id)
 
95
                    if journal.sequence_id:
 
96
                        name = self.pool.get('ir.sequence').get_id(cr, uid, journal.sequence_id.id)
 
97
 
 
98
                    move = {'name': name, 'line_id': new_lines, 'journal_id': journal_id}
 
99
                    if inv.period_id:
 
100
                        move['period_id'] = inv.period_id.id
 
101
                        for i in line:
 
102
                            i[2]['period_id'] = inv.period_id.id
 
103
                    move_id = move_obj.create(cr, uid, move)
 
104
                    move_obj.post(cr, uid, [move_id])
 
105
 
 
106
                #this function could be improved in order to enable having more than one translation line per invoice
 
107
                    id1 = move_line_obj.search(cr, uid, [('move_id','=',move_id),('credit','<>',False)])[0]
 
108
                    id2 = move_line_obj.search(cr, uid, [('invoice','=',inv.id),('debit','<>',0)])[0]
 
109
                    move_line_obj.reconcile_partial(cr, uid, [id2,id1], 'manual', context=context)
 
110
 
 
111
        return True
 
112
 
 
113
    #raise an error if the partner has the warning 'alert_others' when we choose him in the account_invoice form
 
114
    def onchange_partner_id(self, cr, uid, ids, type, partner_id,date_invoice=False, payment_term=False, partner_bank_id=False):
 
115
        inv_special=False
 
116
        if partner_id:
 
117
            data_partner = self.pool.get('res.partner').browse(cr,uid,partner_id)
 
118
            inv_special=data_partner.invoice_special
 
119
            if data_partner.alert_others:
 
120
                raise osv.except_osv('Error!',data_partner.alert_explanation or 'Partner is not valid')
 
121
 
 
122
        data=super(account_invoice,self).onchange_partner_id( cr, uid, ids, type, partner_id,date_invoice, payment_term, partner_bank_id)
 
123
        data['value']['invoice_special']=inv_special
 
124
        return data
 
125
 
 
126
    #raise an error when we try to invoice a membership product to a partner with the 'alert_membership' warning set to TRUE
 
127
    def create(self, cr, uid, vals, *args, **kwargs):
 
128
        product_ids = []
 
129
        line_ids = []
 
130
        flag = False
 
131
        inv_line_obj = self.pool.get('account.invoice.line')
 
132
        if 'invoice_line' in vals and vals['invoice_line']:
 
133
            for line in vals['invoice_line']:
 
134
                if type(line[2])==type([]):
 
135
                    data_lines = inv_line_obj.browse(cr, uid, line[2])
 
136
                    for line in data_lines:
 
137
                        if line.product_id:
 
138
                            product_ids.append(line.product_id.id)
 
139
                else:
 
140
                    if line[2].has_key('product_id') and line[2]['product_id']:
 
141
                        product_ids.append(line[2]['product_id'])
 
142
        if 'abstract_line_ids' in vals:
 
143
            for lines in vals['abstract_line_ids']:
 
144
                line_ids.append(lines[1])
 
145
            data_lines = inv_line_obj.browse(cr, uid, line_ids)
 
146
            for line in data_lines:
 
147
                    if line.product_id:
 
148
                        product_ids.append(line.product_id.id)
 
149
        if product_ids:
 
150
            data_product = self.pool.get('product.product').browse(cr,uid,product_ids)
 
151
            for product in data_product:
 
152
                if product.membership:
 
153
                    flag = True
 
154
        if vals['partner_id']:
 
155
            data_partner = self.pool.get('res.partner').browse(cr,uid,vals['partner_id'])
 
156
            if data_partner.alert_membership and flag:
 
157
                raise osv.except_osv('Error!',data_partner.alert_explanation or 'Partner is not valid')
 
158
        return super(account_invoice,self).create(cr, uid, vals, *args, **kwargs)
 
159
 
 
160
    #raise an error when we try to invoice a membership product to a partner with the 'alert_membership' warning set to TRUE
 
161
    def write(self, cr, uid, ids,vals, *args, **kwargs):
 
162
        product_ids = []
 
163
        a = super(account_invoice,self).write(cr, uid, ids,vals, *args, **kwargs)
 
164
        flag = False
 
165
        data_inv = self.browse(cr,uid,ids[0])
 
166
        for lines in data_inv.abstract_line_ids:
 
167
            if lines.product_id:
 
168
                product_ids.append(lines.product_id.id)
 
169
        if product_ids:
 
170
            data_product = self.pool.get('product.product').browse(cr,uid,product_ids)
 
171
            for product in data_product:
 
172
                if product.membership:
 
173
                    flag = True
 
174
        if data_inv.partner_id.alert_membership and flag:
 
175
            raise osv.except_osv('Error!',data_inv.partner_id.alert_explanation or 'Partner is not valid')
 
176
        return a
 
177
 
 
178
account_invoice()
 
179
 
 
180
 
 
181
class sale_order(osv.osv):
 
182
    _inherit = "sale.order"
 
183
    _columns = {
 
184
        'dept' :  fields.many2one('hr.department','Department'),
 
185
    }
 
186
 
 
187
sale_order()
 
188
 
 
189
 
 
190
 
 
191
class account_invoice_line(osv.osv):
 
192
    _inherit = "account.invoice.line"
 
193
    _columns = {
 
194
        'cci_special_reference' : fields.char('Special Reference', size=64),
 
195
    }
 
196
    _defaults = {
 
197
        'cci_special_reference': lambda *a : False,
 
198
    }
 
199
account_invoice_line()
 
200
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
201