Browse Source

[IMP] Improved Code base on comments

pull/437/head
Nikul Chaudhary 5 years ago
parent
commit
26d8f86312
  1. 1
      contract_sale/__init__.py
  2. 4
      contract_sale/models/__init__.py
  3. 73
      contract_sale/models/contract.py
  4. 11
      contract_sale/models/sale_order.py
  5. 1
      contract_sale_invoicing/models/__init__.py
  6. 76
      contract_sale_invoicing/models/contract.py

1
contract_sale/__init__.py

@ -1,2 +1 @@
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from . import models

4
contract_sale/models/__init__.py

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

73
contract_sale/models/contract.py

@ -1,73 +0,0 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, models
class ContractContract(models.Model):
_inherit = 'contract.contract'
@api.multi
def _prepare_recurring_invoices_values(self, date_ref=False):
"""
overwrite Base Contract Method
"""
invoices_values = []
for contract in self:
if not date_ref:
date_ref = contract.recurring_next_date
if not date_ref:
# this use case is possible when recurring_create_invoice is
# called for a finished contract
continue
contract_lines = contract._get_lines_to_invoice(date_ref)
if not contract_lines:
continue
invoice_values = contract._prepare_invoice(date_ref)
# Search Contract in sale order
order_ids = self.env['sale.order'].search([
('partner_id', '=', contract.partner_id.id),
('contract_id', '=', contract.id),
])
for line in contract_lines:
invoice_values.setdefault('invoice_line_ids', [])
invoice_line_values = line._prepare_invoice_line(
invoice_id=False
)
if invoice_line_values:
# Check Invoice and If It's Not Created then Updated Qty
for order_id in order_ids:
invoice_ids =\
order_id.order_line.mapped('invoice_lines')
if not invoice_ids:
for line in order_id.order_line:
if line.product_id.id == invoice_line_values.\
get('product_id', False):
invoice_line_values['quantity'
] += line.product_uom_qty
invoice_values['invoice_line_ids'].append(
(0, 0, invoice_line_values)
)
invoices_values.append(invoice_values)
contract_lines._update_recurring_next_date()
return 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

11
contract_sale/models/sale_order.py

@ -1,11 +0,0 @@
# 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(
comodel_name="contract.contract",
string="Contract",
)

1
contract_sale_invoicing/models/__init__.py

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

76
contract_sale_invoicing/models/contract.py

@ -16,16 +16,84 @@ class ContractContract(models.Model):
@api.multi
def _recurring_create_invoice(self, date_ref=False):
invoices = super()._recurring_create_invoice(date_ref)
if not self.invoicing_sales:
for rec in self:
if not rec.invoicing_sales:
return invoices
sales = self.env['sale.order'].search([
('analytic_account_id', '=', self.group_id.id),
('analytic_account_id', '=', rec.group_id.id),
('partner_invoice_id', 'child_of',
self.partner_id.commercial_partner_id.ids),
rec.partner_id.commercial_partner_id.ids),
('invoice_status', '=', 'to invoice'),
('date_order', '<=',
'{} 23:59:59'.format(self.recurring_next_date)),
'{} 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_recurring_invoices_values(self, date_ref=False):
invoices_values = super(ContractContract, self
)._prepare_recurring_invoices_values()
for invoice_val in invoices_values:
invoice_line_values = {}
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 = {
'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
}
invoice_val['invoice_line_ids'
].append((0, 0, invoice_line_values))
return 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
Loading…
Cancel
Save