~slobodni-programi/addons-sp/61

« back to all changes in this revision

Viewing changes to l10n_hr_account/account.py

  • Committer: Goran Kliska
  • Date: 2011-11-10 09:30:26 UTC
  • Revision ID: goran.kliska@slobodni-programi.hr-20111110093026-q4dqc6lmfoo60ngh
croatian VAT rules and postings on payment

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
                                  ], 
46
46
                                  'Model', 
47
47
                             help='Model poziva na broj'),
48
 
        'default_debit_partner_id' :fields.many2one('res.partner', 'Default Debit Partner', help="Default Credit Partner for payment"),
49
 
        'default_credit_partner_id':fields.many2one('res.partner', 'Default Credit Partner', help="Default Credit Partner for payment"),
50
48
    }
51
49
 
52
50
    def create_sequence(self, cr, uid, vals, context=None):
82
80
 
83
81
account_journal()
84
82
 
85
 
class account_voucher(osv.osv):
86
 
    _inherit = "account.voucher"
87
 
 
88
 
    #Copy from account_voucher
89
 
    def action_move_line_create(self, cr, uid, ids, context=None):
90
 
        #res = super(account_voucher, self).action_move_line_create(cr, uid, ids, context=context)
91
 
 
92
 
        def _get_payment_term_lines(term_id, amount):
93
 
            term_pool = self.pool.get('account.payment.term')
94
 
            if term_id and amount:
95
 
                terms = term_pool.compute(cr, uid, term_id, amount)
96
 
                return terms
97
 
            return False
98
 
        if context is None:
99
 
            context = {}
100
 
        move_pool = self.pool.get('account.move')
101
 
        move_line_pool = self.pool.get('account.move.line')
102
 
        currency_pool = self.pool.get('res.currency')
103
 
        tax_obj = self.pool.get('account.tax')
104
 
        seq_obj = self.pool.get('ir.sequence')
105
 
        for inv in self.browse(cr, uid, ids, context=context):
106
 
            if inv.move_id:
107
 
                continue
108
 
            context_multi_currency = context.copy()
109
 
            context_multi_currency.update({'date': inv.date})
110
 
 
111
 
            if inv.number:
112
 
                name = inv.number
113
 
            elif inv.journal_id.sequence_id:
114
 
                name = seq_obj.get_id(cr, uid, inv.journal_id.sequence_id.id)
115
 
            else:
116
 
                raise osv.except_osv(_('Error !'), _('Please define a sequence on the journal !'))
117
 
            if not inv.reference:
118
 
                ref = name.replace('/','')
119
 
            else:
120
 
                ref = inv.reference
121
 
 
122
 
            move = {
123
 
                'name': name,
124
 
                'journal_id': inv.journal_id.id,
125
 
                'narration': inv.narration,
126
 
                'date': inv.date,
127
 
                'ref': ref,
128
 
                'period_id': inv.period_id and inv.period_id.id or False
129
 
            }
130
 
            move_id = move_pool.create(cr, uid, move)
131
 
 
132
 
            #create the first line manually
133
 
            company_currency = inv.journal_id.company_id.currency_id.id
134
 
            current_currency = inv.currency_id.id
135
 
            debit = 0.0
136
 
            credit = 0.0
137
 
            # TODO: is there any other alternative then the voucher type ??
138
 
            # -for sale, purchase we have but for the payment and receipt we do not have as based on the bank/cash journal we can not know its payment or receipt
139
 
            if inv.type in ('purchase', 'payment'):
140
 
                credit = currency_pool.compute(cr, uid, current_currency, company_currency, inv.amount, context=context_multi_currency)
141
 
            elif inv.type in ('sale', 'receipt'):
142
 
                debit = currency_pool.compute(cr, uid, current_currency, company_currency, inv.amount, context=context_multi_currency)
143
 
            if debit < 0:
144
 
                credit = -debit
145
 
                debit = 0.0
146
 
            if credit < 0:
147
 
                debit = -credit
148
 
                credit = 0.0
149
 
            sign = debit - credit < 0 and -1 or 1
150
 
 
151
 
            # KGB change start: take new partner_id from journal
152
 
            new_partner_id = inv.partner_id.id  #no change
153
 
            if debit > 0 and inv.journal_id.default_debit_partner_id:
154
 
                new_partner_id = inv.journal_id.default_debit_partner_id.id
155
 
            if credit > 0 and inv.journal_id.default_credit_partner_id:
156
 
                new_partner_id = inv.journal_id.default_credit_partner_id.id
157
 
            # KGB change end: take new partner_id from journal
158
 
            
159
 
            #create the first line of the voucher
160
 
            move_line = {
161
 
                'name': inv.name or '/',
162
 
                'debit': debit,
163
 
                'credit': credit,
164
 
                'account_id': inv.account_id.id,
165
 
                'move_id': move_id,
166
 
                'journal_id': inv.journal_id.id,
167
 
                'period_id': inv.period_id.id,
168
 
                'partner_id': new_partner_id,  #KGB changed inv.partner_id.id,
169
 
                'currency_id': company_currency <> current_currency and  current_currency or False,
170
 
                'amount_currency': company_currency <> current_currency and sign * inv.amount or 0.0,
171
 
                'date': inv.date,
172
 
                'date_maturity': inv.date_due
173
 
            }
174
 
            move_line_pool.create(cr, uid, move_line)
175
 
            rec_list_ids = []
176
 
            line_total = debit - credit
177
 
            if inv.type == 'sale':
178
 
                line_total = line_total - currency_pool.compute(cr, uid, inv.currency_id.id, company_currency, inv.tax_amount, context=context_multi_currency)
179
 
            elif inv.type == 'purchase':
180
 
                line_total = line_total + currency_pool.compute(cr, uid, inv.currency_id.id, company_currency, inv.tax_amount, context=context_multi_currency)
181
 
 
182
 
            for line in inv.line_ids:
183
 
                #create one move line per voucher line where amount is not 0.0
184
 
                if not line.amount:
185
 
                    continue
186
 
                #we check if the voucher line is fully paid or not and create a move line to balance the payment and initial invoice if needed
187
 
                if line.amount == line.amount_unreconciled:
188
 
                    amount = line.move_line_id.amount_residual or 0.00 #residual amount in company currency
189
 
                else:
190
 
                    amount = currency_pool.compute(cr, uid, current_currency, company_currency, line.untax_amount or line.amount, context=context_multi_currency)
191
 
                move_line = {
192
 
                    'journal_id': inv.journal_id.id,
193
 
                    'period_id': inv.period_id.id,
194
 
                    'name': line.name or '/',
195
 
                    'account_id': line.account_id.id,
196
 
                    'move_id': move_id,
197
 
                    'partner_id': inv.partner_id.id,
198
 
                    'currency_id': company_currency <> current_currency and current_currency or False,
199
 
                    'analytic_account_id': line.account_analytic_id and line.account_analytic_id.id or False,
200
 
                    'quantity': 1,
201
 
                    'credit': 0.0,
202
 
                    'debit': 0.0,
203
 
                    'date': inv.date
204
 
                }
205
 
                if amount < 0:
206
 
                    amount = -amount
207
 
                    if line.type == 'dr':
208
 
                        line.type = 'cr'
209
 
                    else:
210
 
                        line.type = 'dr'
211
 
                if (line.type=='dr'):
212
 
                    line_total += amount
213
 
                    move_line['debit'] = amount
214
 
                else:
215
 
                    line_total -= amount
216
 
                    move_line['credit'] = amount
217
 
 
218
 
                if inv.tax_id and inv.type in ('sale', 'purchase'):
219
 
                    move_line.update({
220
 
                        'account_tax_id': inv.tax_id.id,
221
 
                    })
222
 
                if move_line.get('account_tax_id', False):
223
 
                    tax_data = tax_obj.browse(cr, uid, [move_line['account_tax_id']], context=context)[0]
224
 
                    if not (tax_data.base_code_id and tax_data.tax_code_id):
225
 
                        raise osv.except_osv(_('No Account Base Code and Account Tax Code!'),_("You have to configure account base code and account tax code on the '%s' tax!") % (tax_data.name))
226
 
                sign = (move_line['debit'] - move_line['credit']) < 0 and -1 or 1
227
 
                move_line['amount_currency'] = company_currency <> current_currency and sign * line.amount or 0.0
228
 
                voucher_line = move_line_pool.create(cr, uid, move_line)
229
 
                if line.move_line_id.id:
230
 
                    rec_ids = [voucher_line, line.move_line_id.id]
231
 
                    rec_list_ids.append(rec_ids)
232
 
 
233
 
            if not currency_pool.is_zero(cr, uid, inv.currency_id, line_total):
234
 
                diff = line_total
235
 
                account_id = False
236
 
                write_off_name = ''
237
 
                if inv.payment_option == 'with_writeoff':
238
 
                    account_id = inv.writeoff_acc_id.id
239
 
                    write_off_name = inv.comment
240
 
                elif inv.type in ('sale', 'receipt'):
241
 
                    account_id = inv.partner_id.property_account_receivable.id
242
 
                else:
243
 
                    account_id = inv.partner_id.property_account_payable.id
244
 
                move_line = {
245
 
                    'name': write_off_name or name,
246
 
                    'account_id': account_id,
247
 
                    'move_id': move_id,
248
 
                    'partner_id': inv.partner_id.id,
249
 
                    'date': inv.date,
250
 
                    'credit': diff > 0 and diff or 0.0,
251
 
                    'debit': diff < 0 and -diff or 0.0,
252
 
                    #'amount_currency': company_currency <> current_currency and currency_pool.compute(cr, uid, company_currency, current_currency, diff * -1, context=context_multi_currency) or 0.0,
253
 
                    #'currency_id': company_currency <> current_currency and current_currency or False,
254
 
                }
255
 
                move_line_pool.create(cr, uid, move_line)
256
 
            self.write(cr, uid, [inv.id], {
257
 
                'move_id': move_id,
258
 
                'state': 'posted',
259
 
                'number': name,
260
 
            })
261
 
            move_pool.post(cr, uid, [move_id], context={})
262
 
            for rec_ids in rec_list_ids:
263
 
                if len(rec_ids) >= 2:
264
 
                    move_line_pool.reconcile_partial(cr, uid, rec_ids)
265
 
        return True
266
 
 
267
 
account_voucher()                
268
 
             
269
83
            
270
84
class account_move(osv.osv):
271
85
    _inherit = "account.move"