Browse Source

[RFC] contract_sale v12

pull/437/head
Nikul Chaudhary 5 years ago
parent
commit
da76db0522
  1. 4
      contract_sale/README.rst
  2. 2
      contract_sale/__manifest__.py
  3. 1
      contract_sale_invoicing/__manifest__.py
  4. 1
      contract_sale_invoicing/models/__init__.py
  5. 118
      contract_sale_invoicing/models/contract.py
  6. 10
      contract_sale_invoicing/models/sale_order.py
  7. 2
      contract_sale_invoicing/views/contract_view.xml
  8. 14
      contract_sale_invoicing/views/sale_order.xml

4
contract_sale/README.rst

@ -71,6 +71,10 @@ Contributors
* Levent Karakaş * Levent Karakaş
* Bejaoui Souheil <souheil.bejaoui@acsone.eu> * Bejaoui Souheil <souheil.bejaoui@acsone.eu>
* Serpent Consulting Services Pvt. Ltd. <support@serpentcs.com>
* Nikul Chaudhary <nikul.chaudhary.serpentcs@gmail.com>
Maintainers Maintainers
~~~~~~~~~~~ ~~~~~~~~~~~

2
contract_sale/__manifest__.py

@ -9,7 +9,7 @@
'Odoo Community Association (OCA)', 'Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/contract', 'website': 'https://github.com/OCA/contract',
'depends': [ 'depends': [
'sale',
'sale_management',
'contract', 'contract',
], ],
'data': [ 'data': [

1
contract_sale_invoicing/__manifest__.py

@ -16,5 +16,6 @@
], ],
'data': [ 'data': [
'views/contract_view.xml', 'views/contract_view.xml',
'views/sale_order.xml',
], ],
} }

1
contract_sale_invoicing/models/__init__.py

@ -1,3 +1,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import sale_order
from . import contract from . import contract

118
contract_sale_invoicing/models/contract.py

@ -12,20 +12,112 @@ class ContractContract(models.Model):
help='If checked include sales with same analytic account to invoice ' help='If checked include sales with same analytic account to invoice '
'in contract invoice creation.', 'in contract invoice creation.',
) )
group_by = fields.Selection(
[('analytic_account', 'Analytic Account'),
('contract', 'Contract')],
default='analytic_account',
string='Group By'
)
@api.multi @api.multi
def _recurring_create_invoice(self, date_ref=False): def _recurring_create_invoice(self, date_ref=False):
invoices = super()._recurring_create_invoice(date_ref) invoices = super()._recurring_create_invoice(date_ref)
if not self.invoicing_sales:
return invoices
sales = self.env['sale.order'].search([
('analytic_account_id', '=', self.group_id.id),
('partner_invoice_id', 'child_of',
self.partner_id.commercial_partner_id.ids),
('invoice_status', '=', 'to invoice'),
('date_order', '<=',
'{} 23:59:59'.format(self.recurring_next_date)),
])
if sales:
invoice_ids = sales.action_invoice_create()
invoices |= self.env['account.invoice'].browse(invoice_ids)[:1]
for rec in self:
if not rec.invoicing_sales:
return invoices
sales = self.env['sale.order'].search([
('analytic_account_id', '=', rec.group_id.id),
('partner_invoice_id', 'child_of',
rec.partner_id.commercial_partner_id.ids),
('invoice_status', '=', 'to invoice'),
('date_order', '<=',
'{} 23:59:59'.format(rec.recurring_next_date)),
])
if sales:
invoice_ids = sales.action_invoice_create()
invoices |= self.env['account.invoice'].browse(invoice_ids)[:1]
@api.multi
def _prepare_invoice_line_dict(self, contract_line_rec, invoice_line,
remain_qty):
return {
'invoice_id': False,
'uom_id': contract_line_rec.uom_id.id,
'product_id': invoice_line.get('product_id'),
'quantity': remain_qty or 0,
'discount': contract_line_rec.discount,
'contract_line_id': contract_line_rec.id,
'name': contract_line_rec.name,
'account_analytic_id': False,
'price_unit': contract_line_rec.price_unit
}
@api.multi
def _prepare_recurring_invoices_values(self, date_ref=False):
invoices_values = super(ContractContract, self
)._prepare_recurring_invoices_values()
updated_invoices_values = []
for invoice_val in invoices_values:
invoice_line_values = {}
invoice_line_list = []
for invoice_line in invoice_val.get('invoice_line_ids', []):
invoice_line = invoice_line[2] or {}
contract_line_rec = self.env['contract.line'].\
browse(invoice_line.get('contract_line_id', False))
if contract_line_rec and contract_line_rec.contract_id and\
contract_line_rec.contract_id.invoicing_sales:
order_ids = self.env['sale.order'].search([
('partner_id', '=',
contract_line_rec.contract_id.partner_id.id),
('contract_id', '=', contract_line_rec.contract_id.id),
])
sale_order_line_product_qty = {}
for order_id in order_ids:
if not order_id.order_line.mapped('invoice_lines'):
for line in order_id.order_line:
if sale_order_line_product_qty.\
get(line.product_id.id):
sale_order_line_product_qty[
line.product_id.id
] += line.product_uom_qty
else:
sale_order_line_product_qty[
line.product_id.id
] = line.product_uom_qty
if invoice_line.get('product_id'
) in sale_order_line_product_qty:
if sale_order_line_product_qty.\
get(line.product_id.id
) > invoice_line.get('quantity'):
remain_qty = sale_order_line_product_qty.\
get(invoice_line.get('product_id')
) - invoice_line.get('quantity') or 0
invoice_line_values =\
self._prepare_invoice_line_dict(
contract_line_rec, invoice_line, remain_qty
) or {}
invoice_line_list.append(invoice_line_values)
sale_order_line_product_qty.\
pop(invoice_line.get('product_id'))
invoice_val['invoice_line_ids'] +=\
[(0, 0, invoice_line_val
)for invoice_line_val in invoice_line_list]
updated_invoices_values.append(invoice_val)
return updated_invoices_values
@api.depends('contract_line_ids')
def _compute_sale_order_count(self):
super(ContractContract, self)._compute_sale_order_count()
contract_count = len(
self.contract_line_ids.
mapped('sale_order_line_id.order_id.contract_id')) or 0
self.sale_order_count += contract_count
@api.multi
def action_view_sales_orders(self):
res = super(ContractContract, self).action_view_sales_orders()
contracts = self.contract_line_ids.mapped(
'sale_order_line_id.order_id.contract_id'
)
res.get('domain')[0][2].extend(contracts)
return res

10
contract_sale_invoicing/models/sale_order.py

@ -0,0 +1,10 @@
# Copyright (C) 2019 Open Source Integrators
# Copyright (C) 2019 Serpent Consulting Services
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import fields, models
class SaleOrder(models.Model):
_inherit = 'sale.order'
contract_id = fields.Many2one('contract.contract', string='Contract')

2
contract_sale_invoicing/views/contract_view.xml

@ -10,6 +10,8 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="recurring_next_date" position="after"> <field name="recurring_next_date" position="after">
<field name="invoicing_sales"/> <field name="invoicing_sales"/>
<field name="group_by"
attrs="{'invisible': [('invoicing_sales', '=', False)]}"/>
</field> </field>
</field> </field>
</record> </record>

14
contract_sale_invoicing/views/sale_order.xml

@ -0,0 +1,14 @@
<odoo>
<record id="view_order_form_form_view" model="ir.ui.view">
<field name="name">sale.order.form.view</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<field name="partner_id" position="after">
<field name="contract_id" domain="[('partner_id', '=', partner_id)]"/>
</field>
</field>
</record>
</odoo>
Loading…
Cancel
Save