Skip to content

Commit

Permalink
[MIG] contract_sale_generation: Migraiton to 11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainvh committed May 27, 2019
1 parent 044a117 commit 6d0007a
Show file tree
Hide file tree
Showing 16 changed files with 611 additions and 61 deletions.
71 changes: 51 additions & 20 deletions contract_sale_generation/README.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
======================================
Contracts Management - Recurring Sales
======================================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fcontract-lightgray.png?logo=github
:target: https://github.com/OCA/contract/tree/11.0/contract_sale_generation
:alt: OCA/contract
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/contract-11-0/contract-11-0-contract_sale_generation
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/110/11.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

This module extends functionality of contracts to be able to generate sales
orders instead of invoices.

=============================
Contracts for recurrent sales
=============================
**Table of contents**

This module extends functionality of contracts to be able to generate sales
orders instead of invoices.
.. contents::
:local:

Usage
=====
Expand All @@ -21,38 +45,45 @@ To use this module, you need to:
* Type defines document that contract will generate, can be "Sales" or "Invoices"
* Sale Autoconfirm, validate Sales Orders if type is "Sales"

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/110/10.0

Bug Tracker
===========

Bugs are tracked on `GitHub Issues
<https://github.com/OCA/contract/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.
Bugs are tracked on `GitHub Issues <https://github.com/OCA/contract/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/contract/issues/new?body=module:%20contract_sale_generation%0Aversion:%2011.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* PESOL
* Okia

Contributors
------------
~~~~~~~~~~~~

* Angel Moya <angel.moya@pesol.es>
* Florent THOMAS <florent.thomas@mind-and-go.com>
* Sylvain Van Hoof <sylvain@okia.be>

Maintainer
----------
Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

This module is maintained by the OCA.

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

To contribute to this module, please visit https://odoo-community.org.
This module is part of the `OCA/contract <https://github.com/OCA/contract/tree/11.0/contract_sale_generation>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 0 additions & 1 deletion contract_sale_generation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# -*- coding: utf-8 -*-
from . import models
5 changes: 2 additions & 3 deletions contract_sale_generation/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Pesol (<http://pesol.es>)
# Copyright 2017 Angel Moya <angel.moya@pesol.es>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html)


{
'name': 'Contracts Management - Recurring Sales',
'version': '10.0.3.0.0',
'version': '11.0.1.0.0',
'category': 'Contract Management',
'license': 'AGPL-3',
'author': "PESOL, "
'author': "PESOL, Okia, "
"Odoo Community Association (OCA)",
'website': 'https://github.com/oca/contract',
'depends': ['contract', 'sale'],
Expand Down
9 changes: 5 additions & 4 deletions contract_sale_generation/data/contract_cron.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?xml version="1.0" encoding='UTF-8'?>
<odoo>

<record model="ir.cron" id="account_analytic_cron_for_sale">
<record id="account_analytic_cron_for_sale" model="ir.cron">
<field name="name">Generate Recurring sales from Contracts</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="model" eval="'account.analytic.account'"/>
<field name="function" eval="'cron_recurring_create_sale'"/>
<field name="args" eval="'()'"/>
<field name="user_id" ref="base.user_root"/>
<field name="state">code</field>
<field name="model_id" ref="analytic.model_account_analytic_account"/>
<field name="code">model.cron_recurring_create_sale()</field>
</record>

</odoo>
2 changes: 1 addition & 1 deletion contract_sale_generation/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import account_analytic_contract
from . import account_analytic_account
from . import sale_order
58 changes: 41 additions & 17 deletions contract_sale_generation/models/account_analytic_account.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# -*- coding: utf-8 -*-
# © 2004-2010 OpenERP SA
# © 2014 Angel Moya <angel.moya@domatix.com>
# © 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
# © 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
# Copyright 2004-2010 OpenERP SA
# Copyright 2014 Angel Moya <angel.moya@domatix.com>
# Copyright 2015 Pedro M. Baeza <pedro.baeza@tecnativa.com>
# Copyright 2016 Carlos Dauden <carlos.dauden@tecnativa.com>
# Copyright 2016-2017 LasLabs Inc.
# Copyright 2017 Pesol (<http://pesol.es>)
# Copyright 2017 Angel Moya <angel.moya@pesol.es>
# Copyright 2018 Therp BV <https://therp.nl>.
# Copyright 2019 Sylvain Van Hoof <sylvain@okia.be>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import api, models, fields
Expand All @@ -30,7 +30,12 @@ def _prepare_sale_line(self, line, order_id):
sale_line.product_id_change()
sale_line_vals = sale_line._convert_to_write(sale_line._cache)
# Insert markers
name = self._insert_markers(line.name)
contract = line.analytic_account_id
lang_obj = self.env['res.lang']
lang = lang_obj.search(
[('code', '=', contract.partner_id.lang)])
date_format = lang.date_format or '%m/%d/%Y'
name = self._insert_markers(line, date_format)
sale_line_vals.update({
'name': name,
'discount': line.discount,
Expand All @@ -51,22 +56,23 @@ def _prepare_sale(self):
'origin': self.name,
'company_id': self.company_id.id,
'user_id': self.partner_id.user_id.id,
'project_id': self.id
'contract_id': self.id
})
# Get other invoice values from partner onchange
sale.onchange_partner_id()
return sale._convert_to_write(sale._cache)

@api.multi
def _create_invoice(self):
def _create_invoice(self, invoice=False):
"""
Create invoices
@param self: single record of account.invoice
@return: MUST return an invoice recordset
"""
self.ensure_one()
if self.type == 'invoice':
return super(AccountAnalyticAccount, self)._create_invoice()
return super(AccountAnalyticAccount, self).\
_create_invoice(invoice=invoice)
else:
return self.env['account.invoice']

Expand All @@ -92,28 +98,46 @@ def _create_sale(self):

@api.multi
def recurring_create_sale(self):
"""
Create sales from contracts
"""Create sales from contracts
:param int limit:
Max of sales to create.
:return: sales created
"""
sales = self.env['sale.order']
for contract in self:
if not contract.check_dates_valid():
continue
# Re-read contract with correct company
ctx = contract.get_invoice_context()
ref_date = contract.recurring_next_date or fields.Date.today()
if (contract.date_start > ref_date or
contract.date_end and contract.date_end < ref_date):
if self.env.context.get('cron'):
continue # Don't fail on cron jobs
raise ValidationError(
_("You must review start and end dates!\n%s") %
contract.name
)
old_date = fields.Date.from_string(ref_date)
new_date = old_date + self.get_relative_delta(
contract.recurring_rule_type, contract.recurring_interval)
ctx = self.env.context.copy()
ctx.update({
'old_date': old_date,
'next_date': new_date,
# Force company for correct evaluation of domain access rules
'force_company': contract.company_id.id,
})
sales |= contract.with_context(ctx)._create_sale()
contract.write({
'recurring_next_date': fields.Date.to_string(ctx['next_date'])
'recurring_next_date': fields.Date.to_string(new_date)
})
return sales

@api.model
def cron_recurring_create_sale(self):
today = fields.Date.today()
contracts = self.search([
contracts = self.with_context(cron=True).search([
('recurring_invoices', '=', True),
('recurring_next_date', '<=', today),
('type', '=', 'sale'),
'|',
('date_end', '=', False),
('date_end', '>=', today),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Pesol (<http://pesol.es>)
# Copyright 2017 Angel Moya <angel.moya@pesol.es>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
Expand Down
14 changes: 14 additions & 0 deletions contract_sale_generation/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2019 Sylvain Van Hoof <sylvain@okia.be>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class SaleOrder(models.Model):
_inherit = 'sale.order'

contract_id = fields.Many2one(
'account.analytic.account',
string='Contract',
readonly=True
)
3 changes: 3 additions & 0 deletions contract_sale_generation/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Angel Moya <angel.moya@pesol.es>
* Florent THOMAS <florent.thomas@mind-and-go.com>
* Sylvain Van Hoof <sylvain@okia.be>
2 changes: 2 additions & 0 deletions contract_sale_generation/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module extends functionality of contracts to be able to generate sales
orders instead of invoices.
12 changes: 12 additions & 0 deletions contract_sale_generation/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
To use this module, you need to:

#. Go to Accounting -> Contracts and select or create a new contract.
#. Check *Generate recurring invoices automatically*.
#. Fill fields for selecting the recurrency and invoice parameters:

* Type defines document that contract will generate, can be "Sales" or "Invoices"
* Sale Autoconfirm, validate Sales Orders if type is "Sales"

.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas
:alt: Try me on Runbot
:target: https://runbot.odoo-community.org/runbot/110/10.0
Loading

0 comments on commit 6d0007a

Please sign in to comment.