|
@@ -33,34 +33,40 @@ class AccountVoucher(models.Model):
|
|
|
|
|
|
# Step 3: Create sale order and confirm
|
|
# Step 3: Create sale order and confirm
|
|
sale_order = self.env['sale.order'].create({
|
|
sale_order = self.env['sale.order'].create({
|
|
- 'partner_id': int(values['customer_id']),
|
|
|
|
|
|
+ 'partner_id': values.get('customer_id'),
|
|
'order_line': [[0, False, {
|
|
'order_line': [[0, False, {
|
|
- 'product_id': int(v['id']),
|
|
|
|
- 'product_uom_qty': float(v['qty']),
|
|
|
|
- 'price_unit': float(v['price']),
|
|
|
|
- 'state': 'confirmed'
|
|
|
|
- }] for v in values['cart_items']],
|
|
|
|
|
|
+ 'product_id': int(v.get('id')),
|
|
|
|
+ 'product_uom_qty': float(v.get('qty')),
|
|
|
|
+ 'price_unit': float(v.get('price')),
|
|
|
|
+ 'discount': float(v.get('discount')),
|
|
|
|
+ }] for v in values.get('cart_items', [])],
|
|
'picking_policy': 'direct',
|
|
'picking_policy': 'direct',
|
|
'state': 'manual',
|
|
'state': 'manual',
|
|
'date_confirm': date_now,
|
|
'date_confirm': date_now,
|
|
'currency_id': currency_id,
|
|
'currency_id': currency_id,
|
|
'pricelist_id': pricelist.id,
|
|
'pricelist_id': pricelist.id,
|
|
- 'payment_term': int(values['payment_term_id'])
|
|
|
|
|
|
+ 'payment_term': values.get('payment_term_id')
|
|
})
|
|
})
|
|
|
|
|
|
|
|
+
|
|
|
|
+ sale_order.action_button_confirm()
|
|
|
|
+
|
|
# Step 4: Create invoice and calculate due date
|
|
# Step 4: Create invoice and calculate due date
|
|
invoice = self.env['account.invoice'].browse(sale_order.action_invoice_create())
|
|
invoice = self.env['account.invoice'].browse(sale_order.action_invoice_create())
|
|
|
|
|
|
|
|
+
|
|
due_date = parse(date_now) + rd(days=max(invoice.payment_term.line_ids.mapped(lambda x: x.days + x.days2)))
|
|
due_date = parse(date_now) + rd(days=max(invoice.payment_term.line_ids.mapped(lambda x: x.days + x.days2)))
|
|
|
|
|
|
invoice.write({
|
|
invoice.write({
|
|
'currency_id': currency_id,
|
|
'currency_id': currency_id,
|
|
'date_invoice': date_now,
|
|
'date_invoice': date_now,
|
|
- 'date_due': due_date.strftime(date_format)
|
|
|
|
|
|
+ 'date_due': due_date.strftime(date_format),
|
|
|
|
+ 'state': 'open'
|
|
})
|
|
})
|
|
|
|
|
|
# Step 5: Create invoice move lines
|
|
# Step 5: 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_move_lines = invoice._get_analytic_lines()
|
|
invoice_move_lines = invoice._get_analytic_lines()
|
|
@@ -92,7 +98,7 @@ class AccountVoucher(models.Model):
|
|
if line.value != 'balance':
|
|
if line.value != 'balance':
|
|
total_lines.append([due_date, line.value_amount + distribute_percent])
|
|
total_lines.append([due_date, line.value_amount + distribute_percent])
|
|
continue
|
|
continue
|
|
-
|
|
|
|
|
|
+
|
|
total_lines.append([due_date, line.value_amount])
|
|
total_lines.append([due_date, line.value_amount])
|
|
|
|
|
|
for total_line in total_lines:
|
|
for total_line in total_lines:
|
|
@@ -122,17 +128,31 @@ class AccountVoucher(models.Model):
|
|
move_line_values = invoice.group_lines(invoice_move_lines, move_line_values)
|
|
move_line_values = invoice.group_lines(invoice_move_lines, move_line_values)
|
|
move_line_values = invoice.finalize_invoice_move_lines(move_line_values)
|
|
move_line_values = invoice.finalize_invoice_move_lines(move_line_values)
|
|
|
|
|
|
- account_move = self.env['account.move'].create({
|
|
|
|
|
|
+ ctx = dict(self._context, lang=invoice.partner_id.lang, company_id=invoice.company_id.id)
|
|
|
|
+ period = invoice.period_id
|
|
|
|
+
|
|
|
|
+ if not period:
|
|
|
|
+ period = period.with_context(ctx).find(invoice.date_invoice)[:1]
|
|
|
|
+
|
|
|
|
+ if period:
|
|
|
|
+ for line in move_line_values:
|
|
|
|
+ line[2]['period_id'] = period.id
|
|
|
|
+
|
|
|
|
+ ctx['invoice'] = invoice
|
|
|
|
+ ctx_nolang = ctx.copy()
|
|
|
|
+ ctx_nolang.pop('lang', None)
|
|
|
|
+
|
|
|
|
+ account_move = self.env['account.move'].with_context(ctx_nolang).create({
|
|
'ref': invoice.reference or invoice.name,
|
|
'ref': invoice.reference or invoice.name,
|
|
'line_id': move_line_values,
|
|
'line_id': move_line_values,
|
|
'journal_id': invoice.journal_id.id,
|
|
'journal_id': invoice.journal_id.id,
|
|
'date': invoice.date_invoice,
|
|
'date': invoice.date_invoice,
|
|
'narration': invoice.comment,
|
|
'narration': invoice.comment,
|
|
'company_id': invoice.company_id.id,
|
|
'company_id': invoice.company_id.id,
|
|
- 'period_id': invoice.period_id.find(invoice.date_invoice).id
|
|
|
|
|
|
+ 'period_id': period.id
|
|
})
|
|
})
|
|
|
|
|
|
- invoice.write({
|
|
|
|
|
|
+ invoice.with_context(ctx).write({
|
|
'move_id': account_move.id,
|
|
'move_id': account_move.id,
|
|
'period_id': account_move.period_id.id,
|
|
'period_id': account_move.period_id.id,
|
|
'move_name': account_move.name,
|
|
'move_name': account_move.name,
|
|
@@ -140,11 +160,7 @@ class AccountVoucher(models.Model):
|
|
|
|
|
|
account_move.post()
|
|
account_move.post()
|
|
|
|
|
|
- # Step 7: Validate invoice
|
|
|
|
invoice.action_number()
|
|
invoice.action_number()
|
|
- invoice.write({
|
|
|
|
- 'state': 'open'
|
|
|
|
- })
|
|
|
|
|
|
|
|
# Step 8: Create voucher
|
|
# Step 8: Create voucher
|
|
account_voucher = self.create({
|
|
account_voucher = self.create({
|