account_voucher.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -*-
  2. from openerp import api, fields, models, cli
  3. from openerp.exceptions import except_orm
  4. from datetime import datetime
  5. from dateutil.relativedelta import relativedelta as rd
  6. from dateutil.parser import parse
  7. from operator import itemgetter
  8. class AccountVoucher(models.Model):
  9. _inherit = 'account.voucher'
  10. @api.model
  11. def create_from_pos(self, values):
  12. date_now = fields.Date.context_today(self)
  13. date_format = '%Y-%m-%d'
  14. # Step 1: Create Sale Order and Confirm
  15. sale_order_line_values = [
  16. [0, False, {
  17. 'product_id': int(item['id']),
  18. 'product_uom_qty': float(item['qty']),
  19. 'price_unit': float(item['price']) ,
  20. 'state': 'confirmed'
  21. }] for item in values['cart_items']]
  22. sale_order_values = {
  23. 'partner_id': int(values['customer_id']),
  24. 'order_line': sale_order_line_values,
  25. 'picking_policy': 'direct',
  26. 'state': 'manual',
  27. 'date_confirm': date_now,
  28. 'payment_term': 5
  29. }
  30. sale_order = self.env['sale.order'].create(sale_order_values)
  31. # Step 2: Create Account Invoice and calculate due_date
  32. invoice_id = sale_order.action_invoice_create()
  33. invoice = self.env['account.invoice'].browse(invoice_id)
  34. days_to_due = max(invoice.payment_term.line_ids.mapped(lambda x: x.days + x.days2))
  35. due_date = parse(date_now) + rd(days=days_to_due)
  36. invoice.write({
  37. 'date_invoice': date_now,
  38. 'date_due': due_date.strftime(date_format)
  39. })
  40. # Step 3: Create invoice move lines
  41. amount_paid = float(values['amount_paid'])
  42. decimal_precision = self.env['decimal.precision'].precision_get('Account')
  43. invoice = invoice.with_context(lang=invoice.partner_id.lang)
  44. invoice_move_lines = invoice._get_analytic_lines() + self.env['account.invoice.tax'].move_line_get(invoice_id)
  45. compute_taxes = self.env['account.invoice.tax'].compute(invoice)
  46. invoice.check_tax_lines(compute_taxes)
  47. total, total_currency, invoice_move_lines = invoice.compute_invoice_totals(invoice.company_id.currency_id, invoice.reference, invoice_move_lines)
  48. same_currency = invoice.currency_id != invoice.company_id.currency_id
  49. percent_paid = amount_paid / total
  50. distribute_percent = -(percent_paid / len(invoice.payment_term.line_ids))
  51. total_lines = []
  52. for line in invoice.payment_term.line_ids:
  53. due_date = (parse(date_now) + rd(days=line.days + line.days2)).strftime(date_format)
  54. if percent_paid:
  55. total_lines.append((date_now, round(percent_paid, decimal_precision)))
  56. percent_paid = 0
  57. if date_format == date_now:
  58. distribute_percent = -((total_lines[0] - line.value_amount) / (len(invoice.payment_term.line_ids) - 1))
  59. continue
  60. if line.value != 'balance':
  61. total_lines.append((due_date, round(line.value_amount + distribute_percent, decimal_precision)))
  62. continue
  63. total_lines.append((due_date, line.value_amount))
  64. import pdb; pdb.set_trace()
  65. print total_lines
  66. # -----------------------------------------------------------------------------------------------------
  67. # def compute(self, cr, uid, id, value, date_ref=False, context=None):
  68. # if not date_ref:
  69. # date_ref = datetime.now().strftime('%Y-%m-%d')
  70. # pt = self.browse(cr, uid, id, context=context)
  71. # amount = value
  72. # result = []
  73. # obj_precision = self.pool.get('decimal.precision')
  74. # prec = obj_precision.precision_get(cr, uid, 'Account')
  75. # for line in pt.line_ids:
  76. # if line.value == 'fixed':
  77. # amt = round(line.value_amount, prec)
  78. # elif line.value == 'procent':
  79. # amt = round(value * line.value_amount, prec)
  80. # elif line.value == 'balance':
  81. # amt = round(amount, prec)
  82. # if amt:
  83. # next_date = (datetime.strptime(date_ref, '%Y-%m-%d') + relativedelta(days=line.days))
  84. # if line.days2 < 0:
  85. # next_first_date = next_date + relativedelta(day=1,months=1) #Getting 1st of next month
  86. # next_date = next_first_date + relativedelta(days=line.days2)
  87. # if line.days2 > 0:
  88. # next_date += relativedelta(day=line.days2, months=1)
  89. # result.append( (next_date.strftime('%Y-%m-%d'), amt) )
  90. # amount -= amt
  91. # amount = reduce(lambda x,y: x+y[1], result, 0.0)
  92. # dist = round(value-amount, prec)
  93. # if dist:
  94. # result.append( (time.strftime('%Y-%m-%d'), dist) )
  95. # return result