You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2004-2010 OpenERP SA
  3. # © 2014 Angel Moya <angel.moya@domatix.com>
  4. # © 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  5. # © 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
  6. # Copyright 2016-2017 LasLabs Inc.
  7. # Copyright 2017 Pesol (<http://pesol.es>)
  8. # Copyright 2017 Angel Moya <angel.moya@pesol.es>
  9. # Copyright 2018 Therp BV <https://therp.nl>.
  10. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  11. from odoo import api, models, fields
  12. from odoo.exceptions import ValidationError
  13. from odoo.tools.translate import _
  14. class AccountAnalyticAccount(models.Model):
  15. _inherit = 'account.analytic.account'
  16. @api.model
  17. def _prepare_sale_line(self, line, order_id):
  18. sale_line = self.env['sale.order.line'].new({
  19. 'order_id': order_id,
  20. 'product_id': line.product_id.id,
  21. 'product_qty': line.quantity,
  22. 'product_uom_qty': line.quantity,
  23. 'product_uom': line.uom_id.id,
  24. })
  25. # Get other sale line values from product onchange
  26. sale_line.product_id_change()
  27. sale_line_vals = sale_line._convert_to_write(sale_line._cache)
  28. # Insert markers
  29. name = self._insert_markers(line.name)
  30. sale_line_vals.update({
  31. 'name': name,
  32. 'discount': line.discount,
  33. 'price_unit': line.price_unit,
  34. })
  35. return sale_line_vals
  36. @api.multi
  37. def _prepare_sale(self):
  38. self.ensure_one()
  39. if not self.partner_id:
  40. raise ValidationError(
  41. _("You must first select a Customer for Contract %s!") %
  42. self.name)
  43. sale = self.env['sale.order'].new({
  44. 'partner_id': self.partner_id,
  45. 'date_order': self.recurring_next_date,
  46. 'origin': self.name,
  47. 'company_id': self.company_id.id,
  48. 'user_id': self.partner_id.user_id.id,
  49. 'project_id': self.id
  50. })
  51. # Get other invoice values from partner onchange
  52. sale.onchange_partner_id()
  53. return sale._convert_to_write(sale._cache)
  54. @api.multi
  55. def _create_invoice(self):
  56. """
  57. Create invoices
  58. @param self: single record of account.invoice
  59. @return: MUST return an invoice recordset
  60. """
  61. self.ensure_one()
  62. if self.type == 'invoice':
  63. return super(AccountAnalyticAccount, self)._create_invoice()
  64. else:
  65. return self.env['account.invoice']
  66. @api.multi
  67. def _create_sale(self):
  68. """
  69. Create Sale orders
  70. @param self: single record of sale.order
  71. @return: MUST return a sale.order recordset
  72. """
  73. self.ensure_one()
  74. if self.type == 'sale':
  75. sale_vals = self._prepare_sale()
  76. sale = self.env['sale.order'].create(sale_vals)
  77. for line in self.recurring_invoice_line_ids:
  78. sale_line_vals = self._prepare_sale_line(line, sale.id)
  79. self.env['sale.order.line'].create(sale_line_vals)
  80. if self.sale_autoconfirm:
  81. sale.action_confirm()
  82. return sale
  83. else:
  84. return self.env['sale.order']
  85. @api.multi
  86. def recurring_create_sale(self):
  87. """
  88. Create sales from contracts
  89. :return: sales created
  90. """
  91. sales = self.env['sale.order']
  92. for contract in self:
  93. if not contract.check_dates_valid():
  94. continue
  95. # Re-read contract with correct company
  96. ctx = contract.get_invoice_context()
  97. sales |= contract.with_context(ctx)._create_sale()
  98. contract.write({
  99. 'recurring_next_date': fields.Date.to_string(ctx['next_date'])
  100. })
  101. return sales
  102. @api.model
  103. def cron_recurring_create_sale(self):
  104. today = fields.Date.today()
  105. contracts = self.search([
  106. ('recurring_invoices', '=', True),
  107. ('recurring_next_date', '<=', today),
  108. '|',
  109. ('date_end', '=', False),
  110. ('date_end', '>=', today),
  111. ])
  112. return contracts.recurring_create_sale()