|
@@ -329,8 +329,7 @@ class Purchases(http.Controller):
|
|
|
'pricelist_id': pricelist_id,
|
|
|
'payment_term_id': payment_term_id,
|
|
|
'location_id': self.get_stock_location_id(),
|
|
|
- 'invoice_method': 'order',
|
|
|
- 'state': 'draft'
|
|
|
+ 'invoice_method': 'order'
|
|
|
})
|
|
|
|
|
|
'''
|
|
@@ -343,89 +342,53 @@ class Purchases(http.Controller):
|
|
|
for picking in purchase_order.picking_ids:
|
|
|
picking.force_assign()
|
|
|
picking.action_done()
|
|
|
-
|
|
|
- '''
|
|
|
- Purchase order
|
|
|
- '''
|
|
|
- def create_invoice_lines(self, purchase_order_id):
|
|
|
- purchase_order_obj = request.env['purchase.order']
|
|
|
- account_invoice_line_obj = request.env['account.invoice.line']
|
|
|
-
|
|
|
- purchase_order = purchase_order_obj.browse(purchase_order_id)
|
|
|
- invoice_lines = {}
|
|
|
-
|
|
|
- for line in purchase_order.order_line:
|
|
|
- if (not line.invoiced) and (line.state not in ('draft', 'cancel')):
|
|
|
- if not line.partner_id.id in invoice_lines:
|
|
|
- invoice_lines[line.partner_id.id] = []
|
|
|
|
|
|
- account_id = purchase_order_obj._choose_account_from_po_line(line)
|
|
|
- account_invoice_line_values = purchase_order_obj._prepare_inv_line(account_id, line)
|
|
|
- account_invoice_line_values.update({
|
|
|
- 'origin': line.order_id.name
|
|
|
- })
|
|
|
-
|
|
|
- account_invoice_line_id = account_invoice_line_obj.create(account_invoice_line_values)
|
|
|
-
|
|
|
- line.write({
|
|
|
- 'invoiced': True,
|
|
|
- 'invoice_lines': [(4, account_invoice_line_id.id)]
|
|
|
- })
|
|
|
-
|
|
|
- invoice_lines[line.partner_id.id].append((line, account_invoice_line_id.id))
|
|
|
-
|
|
|
- return invoice_lines
|
|
|
+ purchase_order.write({
|
|
|
+ 'state': 'done'
|
|
|
+ })
|
|
|
|
|
|
'''
|
|
|
- Prepare invoice
|
|
|
+ Validate invoice
|
|
|
'''
|
|
|
- def prepare_invoice(self, purchase_order_id, invoice_lines):
|
|
|
- purchase_order = request.env['purchase.order'].browse(purchase_order_id)
|
|
|
+ def prepare_invoice(self, invoice_ids, currency_id, date_today):
|
|
|
+ assert len(invoice_ids) == 1
|
|
|
|
|
|
- assert len(purchase_order.invoice_ids) == 1
|
|
|
-
|
|
|
- journal = request.env['account.journal'].search([('type', '=', 'purchase')])
|
|
|
- date_due = parse(purchase_order.date_approve) + rd(days=max(purchase_order.payment_term_id.line_ids.mapped(lambda x: x.days + x.days2)))
|
|
|
-
|
|
|
- for value in invoice_lines.values():
|
|
|
- lines = map(lambda x : x[1], value)
|
|
|
- orders = list(set(map(lambda x : x[0].order_id, value)))
|
|
|
-
|
|
|
- purchase_order.invoice_ids.write({
|
|
|
- 'name': orders[0].name or '',
|
|
|
- 'origin': orders[0].name or '',
|
|
|
- 'type': 'in_invoice',
|
|
|
- 'journal_id': journal.id or False,
|
|
|
- 'reference': purchase_order.partner_id.ref,
|
|
|
- 'account_id': purchase_order.partner_id.property_account_payable.id,
|
|
|
- 'invoice_line': [(6, 0, lines)],
|
|
|
- 'currency_id': purchase_order.currency_id.id,
|
|
|
- 'payment_term': purchase_order.payment_term_id.id,
|
|
|
- 'fiscal_position': purchase_order.partner_id.property_account_position.id,
|
|
|
- 'date_invoice': purchase_order.date_approve,
|
|
|
- 'date_due': date_due,
|
|
|
- 'state': 'open'
|
|
|
- })
|
|
|
+ invoice = request.env['account.invoice'].browse(invoice_ids)
|
|
|
+
|
|
|
+ date_due = parse(date_today) + rd(days=max(invoice.payment_term.line_ids.mapped(lambda x: x.days + x.days2)))
|
|
|
+
|
|
|
+ invoice.write({
|
|
|
+ 'currency_id': currency_id,
|
|
|
+ 'date_invoice': date_today,
|
|
|
+ 'date_due': date_due.strftime(DATE_FORMAT),
|
|
|
+ 'state': 'open'
|
|
|
+ })
|
|
|
|
|
|
'''
|
|
|
Create move lines
|
|
|
'''
|
|
|
def create_invoice_move_lines(self, invoice_ids, paid_amount, date_today):
|
|
|
assert len(invoice_ids) == 1
|
|
|
-
|
|
|
invoice = request.env['account.invoice'].browse(invoice_ids)
|
|
|
+ is_purchase = False
|
|
|
+
|
|
|
+ scoped_context = dict(request.context, lang=invoice.partner_id.lang)
|
|
|
+
|
|
|
invoice_move_lines = invoice._get_analytic_lines()
|
|
|
decimal_precision = request.env['decimal.precision'].precision_get('Account')
|
|
|
|
|
|
- compute_taxes = request.env['account.invoice.tax'].compute(invoice)
|
|
|
+ compute_taxes = request.env['account.invoice.tax'].compute(invoice.with_context(lang=invoice.partner_id.lang))
|
|
|
invoice.check_tax_lines(compute_taxes)
|
|
|
invoice._recompute_tax_amount()
|
|
|
|
|
|
invoice_move_lines += request.env['account.invoice.tax'].move_line_get(invoice.id)
|
|
|
+ total, total_currency, invoice_move_lines = invoice.with_context(scoped_context).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)
|
|
|
+ if total < 0:
|
|
|
+ total = total * -1
|
|
|
+ is_purchase = True
|
|
|
|
|
|
- paid_percentage = paid_amount / round(total, decimal_precision)
|
|
|
+ paid_percentage = abs(paid_amount / round(total, decimal_precision))
|
|
|
distributed_percentage = -(paid_percentage / len(invoice.payment_term.line_ids))
|
|
|
|
|
|
payment_lines = []
|
|
@@ -453,17 +416,21 @@ class Purchases(http.Controller):
|
|
|
if current_price < 0.0:
|
|
|
continue
|
|
|
|
|
|
- paid_amount += current_price
|
|
|
+ paid_amount = paid_amount + current_price
|
|
|
+ price = current_price if payment_line[1] else round(total - paid_amount, decimal_precision) or total
|
|
|
+
|
|
|
+ if is_purchase:
|
|
|
+ price = price * -1
|
|
|
|
|
|
invoice_move_lines.append({
|
|
|
'type': 'dest',
|
|
|
'name': '/',
|
|
|
- 'price': current_price if payment_line[1] else round(total - paid_amount, decimal_precision) or total,
|
|
|
+ 'price': price,
|
|
|
'account_id': invoice.account_id.id,
|
|
|
'date_maturity': payment_line[0],
|
|
|
'amount_currency': invoice.company_id.currency_id.compute(payment_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
|
|
|
+ 'ref': invoice.type in ('in_invoice', 'in_refund') and invoice.reference or invoice.number
|
|
|
})
|
|
|
|
|
|
payment_lines = []
|
|
@@ -500,7 +467,7 @@ class Purchases(http.Controller):
|
|
|
account_move = request.env['account.move'].with_context(ctx_nolang).create({
|
|
|
'ref': invoice.reference or invoice.name,
|
|
|
'line_id': move_line_values,
|
|
|
- 'journal_id': invoice.journal_id.id,
|
|
|
+ 'journal_id': invoice.journal_id.with_context(request.context, lang=invoice.partner_id.lang).id,
|
|
|
'date': invoice.date_invoice,
|
|
|
'narration': invoice.comment,
|
|
|
'company_id': invoice.company_id.id,
|
|
@@ -520,10 +487,13 @@ class Purchases(http.Controller):
|
|
|
'''
|
|
|
Number to invoice
|
|
|
'''
|
|
|
- def number_invoice(self, invoice_ids):
|
|
|
+ def validate_invoice(self, invoice_ids):
|
|
|
assert len(invoice_ids) == 1
|
|
|
|
|
|
- request.env['account.invoice'].browse(invoice_ids).action_number()
|
|
|
+ invoice = request.env['account.invoice'].browse(invoice_ids)
|
|
|
+
|
|
|
+ invoice.action_number()
|
|
|
+ invoice.invoice_validate()
|
|
|
|
|
|
'''
|
|
|
Create voucher
|
|
@@ -534,7 +504,7 @@ class Purchases(http.Controller):
|
|
|
|
|
|
account_voucher = request.env['account.voucher'].create({
|
|
|
'reference': account_move.name,
|
|
|
- 'type': 'receipt',
|
|
|
+ 'type': 'payment',
|
|
|
'journal_id': account_journal.id,
|
|
|
'company_id': account_move.company_id.id,
|
|
|
'pre_line': True,
|
|
@@ -542,16 +512,16 @@ class Purchases(http.Controller):
|
|
|
'period_id': account_move.period_id.id,
|
|
|
'date': account_move.date,
|
|
|
'partner_id': account_move.partner_id.id,
|
|
|
- 'account_id': account_journal.default_credit_account_id.id,
|
|
|
+ 'account_id': account_journal.default_debit_account_id.id,
|
|
|
'currency_id': currency_id,
|
|
|
- 'line_cr_ids': [[0, False, {
|
|
|
+ 'line_dr_ids': [[0, False, {
|
|
|
'date_due': l.date_maturity,
|
|
|
'account_id': l.account_id.id,
|
|
|
'date_original': l.invoice.date_invoice,
|
|
|
'move_line_id': l.id,
|
|
|
'amount_original': abs(l.credit or l.debit or 0.0),
|
|
|
'amount_unreconciled': abs(l.amount_residual),
|
|
|
- 'amount': abs(l.debit) if account_move.date == l.date_maturity else 0.0,
|
|
|
+ 'amount': abs(l.credit) if account_move.date == l.date_maturity else 0.0,
|
|
|
'reconcile': account_move.date == l.date_maturity,
|
|
|
'currency_id': currency_id
|
|
|
}] for l in account_move.line_id]
|
|
@@ -609,9 +579,14 @@ class Purchases(http.Controller):
|
|
|
def create_bank_statement_lines(self, account_voucher_id, reference=None):
|
|
|
account_voucher = request.env['account.voucher'].browse(account_voucher_id)
|
|
|
|
|
|
+ amount = account_voucher.amount
|
|
|
+
|
|
|
+ if account_voucher.type == 'payment':
|
|
|
+ amount = amount * -1
|
|
|
+
|
|
|
return [[0, False, {
|
|
|
'name': account_voucher.reference,
|
|
|
- 'amount': account_voucher.amount,
|
|
|
+ 'amount': amount,
|
|
|
'partner_id': account_voucher.partner_id.id,
|
|
|
'voucher_id': account_voucher.id,
|
|
|
'journal_id': account_voucher.journal_id.id,
|
|
@@ -647,51 +622,39 @@ class Purchases(http.Controller):
|
|
|
# Confirm purchase
|
|
|
self.confirm_purchase_order(purchase_order.id)
|
|
|
self.make_info_log('Purchase order confirmed')
|
|
|
-
|
|
|
- # Create invoice lines
|
|
|
- invoice_lines = self.create_invoice_lines(purchase_order.id)
|
|
|
- self.make_info_log('Invoice lines created')
|
|
|
-
|
|
|
- # Invoice preparation
|
|
|
- self.prepare_invoice(purchase_order.id, invoice_lines)
|
|
|
+
|
|
|
+ # Validate invoice
|
|
|
+ invoice_ids = purchase_order.invoice_ids.mapped(lambda x: x.id)
|
|
|
+ self.prepare_invoice(invoice_ids, currency_id, date_now)
|
|
|
self.make_info_log('Invoice prepared')
|
|
|
|
|
|
- # # Create invoice
|
|
|
- # invoice_ids = self.create_invoice(purchase_order.id)
|
|
|
- # print(invoice_ids)
|
|
|
- # self.make_info_log('Invoice created')
|
|
|
-
|
|
|
- # # Prepare invoice
|
|
|
- # self.prepare_invoice(invoice_ids, currency_id, date_now)
|
|
|
- # self.make_info_log('Invoice prepared')
|
|
|
-
|
|
|
- # # Create invoice move line
|
|
|
- # invoice_move_lines = self.create_invoice_move_lines(invoice_ids, float(kw.get('payment')), date_now)
|
|
|
- # self.make_info_log('Invoice move lines created')
|
|
|
+ # Create invoice move lines
|
|
|
+ invoice_move_lines = self.create_invoice_move_lines(invoice_ids, float(kw.get('payment')), date_now)
|
|
|
+ self.make_info_log('Invoice move lines created')
|
|
|
|
|
|
- # # Create account move
|
|
|
- # account_move = self.create_account_move(invoice_ids, invoice_move_lines)
|
|
|
- # self.make_info_log('Account move created')
|
|
|
+ # Create account move
|
|
|
+ account_move = self.create_account_move(invoice_ids, invoice_move_lines)
|
|
|
+ self.make_info_log('Account move created')
|
|
|
|
|
|
- # # Number invoice
|
|
|
- # self.number_invoice(invoice_ids)
|
|
|
- # self.make_info_log('Number invoice ok')
|
|
|
+ # Validate invoice
|
|
|
+ self.validate_invoice(invoice_ids)
|
|
|
+ self.make_info_log('Invoice validated')
|
|
|
|
|
|
- # # Create account voucher
|
|
|
- # account_voucher = self.create_account_voucher(account_move.id, kw.get('journalId'), currency_id, float(kw.get('payment')))
|
|
|
- # self.make_info_log('Account voucher created')
|
|
|
+ # Create account voucher
|
|
|
+ account_voucher = self.create_account_voucher(account_move.id, kw.get('journalId'), currency_id, float(kw.get('payment')))
|
|
|
+ self.make_info_log('Account voucher created')
|
|
|
|
|
|
- # # Close invoice
|
|
|
- # self.close_invoice(invoice_ids)
|
|
|
- # self.make_info_log('Attempt close invoice')
|
|
|
+ # Close invoice
|
|
|
+ self.close_invoice(invoice_ids)
|
|
|
+ self.make_info_log('Attempt close invoice')
|
|
|
|
|
|
- # # Create account bank statement lines
|
|
|
- # account_bank_statement_lines = self.create_bank_statement_lines(account_voucher.id)
|
|
|
- # self.make_info_log('Bank statement lines created')
|
|
|
+ # Create account bank statement lines
|
|
|
+ account_bank_statement_lines = self.create_bank_statement_lines(account_voucher.id)
|
|
|
+ self.make_info_log('Bank statement lines created')
|
|
|
|
|
|
- # # Create account bank statement
|
|
|
- # self.create_bank_statement(account_voucher.id, account_bank_statement_lines, date_now)
|
|
|
- # self.make_info_log('Bank statement created')
|
|
|
+ # Create account bank statement
|
|
|
+ self.create_bank_statement(account_voucher.id, account_bank_statement_lines, date_now)
|
|
|
+ self.make_info_log('Bank statement created')
|
|
|
|
|
|
return {
|
|
|
'status': 'ok'
|