account_voucher.py 4.3 KB

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