|
@@ -6,8 +6,6 @@ from datetime import datetime
|
|
from dateutil.relativedelta import relativedelta as rd
|
|
from dateutil.relativedelta import relativedelta as rd
|
|
from dateutil.parser import parse
|
|
from dateutil.parser import parse
|
|
|
|
|
|
-from operator import itemgetter
|
|
|
|
-
|
|
|
|
class AccountVoucher(models.Model):
|
|
class AccountVoucher(models.Model):
|
|
_inherit = 'account.voucher'
|
|
_inherit = 'account.voucher'
|
|
|
|
|
|
@@ -36,13 +34,12 @@ class AccountVoucher(models.Model):
|
|
|
|
|
|
sale_order = self.env['sale.order'].create(sale_order_values)
|
|
sale_order = self.env['sale.order'].create(sale_order_values)
|
|
|
|
|
|
- # Step 2: Create Account Invoice and calculate due_date
|
|
|
|
|
|
+ # Step 2: Create innvoice and calculate due_date
|
|
invoice_id = sale_order.action_invoice_create()
|
|
invoice_id = sale_order.action_invoice_create()
|
|
-
|
|
|
|
|
|
+
|
|
invoice = self.env['account.invoice'].browse(invoice_id)
|
|
invoice = self.env['account.invoice'].browse(invoice_id)
|
|
|
|
|
|
- days_to_due = max(invoice.payment_term.line_ids.mapped(lambda x: x.days + x.days2))
|
|
|
|
- due_date = parse(date_now) + rd(days=days_to_due)
|
|
|
|
|
|
+ due_date = parse(date_now) + rd(days=max(invoice.payment_term.line_ids.mapped(lambda x: x.days + x.days2)))
|
|
|
|
|
|
invoice.write({
|
|
invoice.write({
|
|
'date_invoice': date_now,
|
|
'date_invoice': date_now,
|
|
@@ -52,7 +49,6 @@ class AccountVoucher(models.Model):
|
|
# Step 3: Create invoice move lines
|
|
# Step 3: Create invoice move lines
|
|
amount_paid = float(values['amount_paid'])
|
|
amount_paid = float(values['amount_paid'])
|
|
decimal_precision = self.env['decimal.precision'].precision_get('Account')
|
|
decimal_precision = self.env['decimal.precision'].precision_get('Account')
|
|
- invoice = invoice.with_context(lang=invoice.partner_id.lang)
|
|
|
|
|
|
|
|
invoice_move_lines = invoice._get_analytic_lines() + self.env['account.invoice.tax'].move_line_get(invoice_id)
|
|
invoice_move_lines = invoice._get_analytic_lines() + self.env['account.invoice.tax'].move_line_get(invoice_id)
|
|
|
|
|
|
@@ -61,7 +57,6 @@ class AccountVoucher(models.Model):
|
|
|
|
|
|
total, total_currency, invoice_move_lines = invoice.compute_invoice_totals(invoice.company_id.currency_id, invoice.reference, invoice_move_lines)
|
|
total, total_currency, invoice_move_lines = invoice.compute_invoice_totals(invoice.company_id.currency_id, invoice.reference, invoice_move_lines)
|
|
|
|
|
|
- same_currency = invoice.currency_id != invoice.company_id.currency_id
|
|
|
|
percent_paid = amount_paid / total
|
|
percent_paid = amount_paid / total
|
|
distribute_percent = -(percent_paid / len(invoice.payment_term.line_ids))
|
|
distribute_percent = -(percent_paid / len(invoice.payment_term.line_ids))
|
|
|
|
|
|
@@ -71,50 +66,65 @@ class AccountVoucher(models.Model):
|
|
due_date = (parse(date_now) + rd(days=line.days + line.days2)).strftime(date_format)
|
|
due_date = (parse(date_now) + rd(days=line.days + line.days2)).strftime(date_format)
|
|
|
|
|
|
if percent_paid:
|
|
if percent_paid:
|
|
- total_lines.append((date_now, round(percent_paid, decimal_precision)))
|
|
|
|
- percent_paid = 0
|
|
|
|
|
|
+ total_lines.append([date_now, percent_paid])
|
|
|
|
+ percent_paid = amount_paid = 0
|
|
|
|
|
|
if date_format == date_now:
|
|
if date_format == date_now:
|
|
distribute_percent = -((total_lines[0] - line.value_amount) / (len(invoice.payment_term.line_ids) - 1))
|
|
distribute_percent = -((total_lines[0] - line.value_amount) / (len(invoice.payment_term.line_ids) - 1))
|
|
continue
|
|
continue
|
|
|
|
|
|
if line.value != 'balance':
|
|
if line.value != 'balance':
|
|
- total_lines.append((due_date, round(line.value_amount + distribute_percent, decimal_precision)))
|
|
|
|
|
|
+ total_lines.append([due_date, line.value_amount + distribute_percent])
|
|
continue
|
|
continue
|
|
|
|
|
|
- total_lines.append((due_date, line.value_amount))
|
|
|
|
-
|
|
|
|
- import pdb; pdb.set_trace()
|
|
|
|
- print total_lines
|
|
|
|
-
|
|
|
|
-# -----------------------------------------------------------------------------------------------------
|
|
|
|
-# def compute(self, cr, uid, id, value, date_ref=False, context=None):
|
|
|
|
-# if not date_ref:
|
|
|
|
-# date_ref = datetime.now().strftime('%Y-%m-%d')
|
|
|
|
-# pt = self.browse(cr, uid, id, context=context)
|
|
|
|
-# amount = value
|
|
|
|
-# result = []
|
|
|
|
-# obj_precision = self.pool.get('decimal.precision')
|
|
|
|
-# prec = obj_precision.precision_get(cr, uid, 'Account')
|
|
|
|
-# for line in pt.line_ids:
|
|
|
|
-# if line.value == 'fixed':
|
|
|
|
-# amt = round(line.value_amount, prec)
|
|
|
|
-# elif line.value == 'procent':
|
|
|
|
-# amt = round(value * line.value_amount, prec)
|
|
|
|
-# elif line.value == 'balance':
|
|
|
|
-# amt = round(amount, prec)
|
|
|
|
-# if amt:
|
|
|
|
-# next_date = (datetime.strptime(date_ref, '%Y-%m-%d') + relativedelta(days=line.days))
|
|
|
|
-# if line.days2 < 0:
|
|
|
|
-# next_first_date = next_date + relativedelta(day=1,months=1) #Getting 1st of next month
|
|
|
|
-# next_date = next_first_date + relativedelta(days=line.days2)
|
|
|
|
-# if line.days2 > 0:
|
|
|
|
-# next_date += relativedelta(day=line.days2, months=1)
|
|
|
|
-# result.append( (next_date.strftime('%Y-%m-%d'), amt) )
|
|
|
|
-# amount -= amt
|
|
|
|
-
|
|
|
|
-# amount = reduce(lambda x,y: x+y[1], result, 0.0)
|
|
|
|
-# dist = round(value-amount, prec)
|
|
|
|
-# if dist:
|
|
|
|
-# result.append( (time.strftime('%Y-%m-%d'), dist) )
|
|
|
|
-# return result
|
|
|
|
|
|
+ total_lines.append([due_date, line.value_amount])
|
|
|
|
+
|
|
|
|
+ for total_line in total_lines:
|
|
|
|
+ current_price = round(total * total_line[1], decimal_precision)
|
|
|
|
+ amount_paid += current_price
|
|
|
|
+
|
|
|
|
+ invoice_move_lines.append({
|
|
|
|
+ 'type': 'dest',
|
|
|
|
+ 'name': '/',
|
|
|
|
+ 'price': current_price if total_line[1] else total - amount_paid,
|
|
|
|
+ 'account_id': invoice.account_id.id,
|
|
|
|
+ 'date_maturity': invoice.date_due,
|
|
|
|
+ 'amount_currency': invoice.company_id.currency_id.compute(total_line[1], invoice.currency_id) if invoice.currency_id != invoice.company_id.currency_id else False,
|
|
|
|
+ 'currency_id': invoice.currency_id != invoice.company_id.currency_id and invoice.currency_id.id,
|
|
|
|
+ 'ref': invoice.reference
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ total_lines = []
|
|
|
|
+
|
|
|
|
+ accounting_partner = self.env['res.partner']._find_accounting_partner(invoice.partner_id)
|
|
|
|
+ move_line_values = [(0, 0, invoice.line_get_convert(line, accounting_partner.id, invoice.date_invoice)) for line in invoice_move_lines]
|
|
|
|
+ move_line_values = invoice.group_lines(invoice_move_lines, move_line_values)
|
|
|
|
+ move_line_values = invoice.finalize_invoice_move_lines(move_line_values)
|
|
|
|
+
|
|
|
|
+ move_values = {
|
|
|
|
+ 'ref': invoice.reference or invoice.name,
|
|
|
|
+ 'line_id': move_line_values,
|
|
|
|
+ 'journal_id': invoice.journal_id.id,
|
|
|
|
+ 'date': invoice.date_invoice,
|
|
|
|
+ 'narration': invoice.comment,
|
|
|
|
+ 'company_id': invoice.company_id.id,
|
|
|
|
+ 'period_id': invoice.period_id.find(invoice.date_invoice).id
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ account_move = self.env['account.move'].create(move_values)
|
|
|
|
+
|
|
|
|
+ invoice.write({
|
|
|
|
+ 'move_id': account_move.id,
|
|
|
|
+ 'period_id': account_move.period_id.id,
|
|
|
|
+ 'move_name': account_move.name,
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ account_move.post()
|
|
|
|
+
|
|
|
|
+ # Step 4: Validate invoice
|
|
|
|
+ invoice.action_number()
|
|
|
|
+ invoice.write({
|
|
|
|
+ 'state': 'open'
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ # Step 5: Pay Invoice
|