Skip to content

Commit

Permalink
[IMP] sale_order_rental: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oihane committed Nov 20, 2019
1 parent 24006fa commit 95113a8
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 328 deletions.
5 changes: 4 additions & 1 deletion sale_order_rental/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from . import sale_order
from . import stock
from . import sale_order_line
from . import stock_picking
from . import stock_move
from . import account_invoice
from . import account_invoice_line
66 changes: 0 additions & 66 deletions sale_order_rental/models/account_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,69 +63,3 @@ def get_taxes_values(self):
tax_grouped[key]['amount'] += val['amount']
tax_grouped[key]['base'] += round_curr(val['base'])
return tax_grouped


class AccountInvoiceLine(models.Model):
_inherit = 'account.invoice.line'

sale_expected_delivery_date = fields.Date(
string='Expected Delivery Date',
compute='_compute_sale_rental_dates', store=True)
sale_expected_end_date = fields.Date(
string='Expected Return Date',
compute='_compute_sale_rental_dates', store=True)
rental_days = fields.Integer(
string='Rental Days',
compute='_compute_sale_rental_dates', store=True)

@api.depends('sale_line_ids',
'sale_line_ids.expected_delivery_date',
'sale_line_ids.expected_end_date',
'sale_line_ids.rental_days')
def _compute_sale_rental_dates(self):
for line in self.filtered('sale_line_ids'):
sale_lines = line.sale_line_ids.filtered(
lambda l: l.expected_delivery_date and
l.expected_end_date)
if sale_lines:
line.sale_expected_delivery_date = min(sale_lines.mapped(
'expected_delivery_date'))
line.sale_expected_end_date = max(sale_lines.mapped(
'expected_end_date'))
line.rental_days = sum(sale_lines.mapped('rental_days'))

@api.depends('price_unit', 'discount', 'invoice_line_tax_ids', 'quantity',
'rental_days', 'product_id', 'invoice_id.partner_id',
'invoice_id.currency_id', 'invoice_id.company_id',
'invoice_id.date_invoice', 'invoice_id.date')
def _compute_price(self):
for line in self:
if line.rental_days:
currency = (
line.invoice_id and line.invoice_id.currency_id or None)
price = line.price_unit * (1 - (line.discount or 0.0) / 100.0)
taxes = False
quantity = line.quantity * line.rental_days
if line.invoice_line_tax_ids:
taxes = line.invoice_line_tax_ids.compute_all(
price, currency, quantity, product=line.product_id,
partner=line.invoice_id.partner_id)
line.price_subtotal = price_subtotal_signed = taxes[
'total_excluded'] if taxes else quantity * price
line.price_total = taxes[
'total_included'] if taxes else line.price_subtotal
if (line.invoice_id.currency_id and
line.invoice_id.currency_id !=
line.invoice_id.company_id.currency_id):
currency = line.invoice_id.currency_id
date = line.invoice_id._get_currency_rate_date()
price_subtotal_signed = currency._convert(
price_subtotal_signed,
line.invoice_id.company_id.currency_id,
line.company_id or line.env.user.company_id,
date or fields.Date.today())
sign = line.invoice_id.type in ['in_refund',
'out_refund'] and -1 or 1
line.price_subtotal_signed = price_subtotal_signed * sign
else:
super(AccountInvoiceLine, line)._compute_price()
69 changes: 69 additions & 0 deletions sale_order_rental/models/account_invoice_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright 2019 Oihana Larrañaga - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from odoo import api, fields, models


class AccountInvoiceLine(models.Model):
_inherit = 'account.invoice.line'

sale_expected_delivery_date = fields.Date(
string='Expected Delivery Date',
compute='_compute_sale_rental_dates', store=True)
sale_expected_end_date = fields.Date(
string='Expected Return Date',
compute='_compute_sale_rental_dates', store=True)
rental_days = fields.Integer(
string='Rental Days',
compute='_compute_sale_rental_dates', store=True)

@api.depends('sale_line_ids',
'sale_line_ids.expected_delivery_date',
'sale_line_ids.expected_end_date',
'sale_line_ids.rental_days')
def _compute_sale_rental_dates(self):
for line in self.filtered('sale_line_ids'):
sale_lines = line.sale_line_ids.filtered(
lambda l: l.expected_delivery_date and
l.expected_end_date)
if sale_lines:
line.sale_expected_delivery_date = min(sale_lines.mapped(
'expected_delivery_date'))
line.sale_expected_end_date = max(sale_lines.mapped(
'expected_end_date'))
line.rental_days = sum(sale_lines.mapped('rental_days'))

@api.depends('price_unit', 'discount', 'invoice_line_tax_ids', 'quantity',
'rental_days', 'product_id', 'invoice_id.partner_id',
'invoice_id.currency_id', 'invoice_id.company_id',
'invoice_id.date_invoice', 'invoice_id.date')
def _compute_price(self):
for line in self:
if line.rental_days:
currency = (
line.invoice_id and line.invoice_id.currency_id or None)
price = line.price_unit * (1 - (line.discount or 0.0) / 100.0)
taxes = False
quantity = line.quantity * line.rental_days
if line.invoice_line_tax_ids:
taxes = line.invoice_line_tax_ids.compute_all(
price, currency, quantity, product=line.product_id,
partner=line.invoice_id.partner_id)
line.price_subtotal = price_subtotal_signed = taxes[
'total_excluded'] if taxes else quantity * price
line.price_total = taxes[
'total_included'] if taxes else line.price_subtotal
if (line.invoice_id.currency_id and
line.invoice_id.currency_id !=
line.invoice_id.company_id.currency_id):
currency = line.invoice_id.currency_id
date = line.invoice_id._get_currency_rate_date()
price_subtotal_signed = currency._convert(
price_subtotal_signed,
line.invoice_id.company_id.currency_id,
line.company_id or line.env.user.company_id,
date or fields.Date.today())
sign = line.invoice_id.type in ['in_refund',
'out_refund'] and -1 or 1
line.price_subtotal_signed = price_subtotal_signed * sign
else:
super(AccountInvoiceLine, line)._compute_price()
60 changes: 3 additions & 57 deletions sale_order_rental/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,70 +47,16 @@ def _inverse_expected_dates(self):
'expected_delivery_date': order.expected_delivery_date,
'expected_end_date': order.expected_end_date,
})
if order.expected_delivery_date and order.expected_end_date:
order.rental_days = ((order.expected_end_date -
order.expected_delivery_date).days + 1)

@api.multi
def action_confirm(self):
for order in self:
if ((not order.expected_delivery_date and any(order.mapped(
'order_line.expected_delivery_date'))) or
(not order.expected_end_date and any(order.mapped(
'order_line.expected_end_date')))):
raise ValidationError(_('Missing dates'))
res = super(SaleOrder, self).action_confirm()
for order in self:
if any(order.mapped('order_line.rental_days')):
for picking in order.picking_ids.filtered(
lambda p: p.state not in ('done', 'cancel')):
picking._return_rental()
return res


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

expected_delivery_date = fields.Date(
string='Expected Delivery Date')
expected_end_date = fields.Date(
string='Expected Return Date')
rental_days = fields.Integer(
string='Rental Days', compute='_compute_rental_days', store=True)

@api.depends('expected_delivery_date', 'expected_end_date')
def _compute_rental_days(self):
for line in self.filtered(
lambda l: l.expected_delivery_date and l.expected_end_date):
line.rental_days = (
line.expected_end_date - line.expected_delivery_date).days + 1

@api.depends('product_uom_qty', 'discount', 'price_unit', 'tax_id',
'rental_days')
def _compute_amount(self):
for line in self:
if line.rental_days:
price = line.price_unit * (1 - (line.discount or 0.0) / 100.0)
taxes = line.tax_id.compute_all(
price, line.order_id.currency_id,
line.product_uom_qty * line.rental_days,
product=line.product_id,
partner=line.order_id.partner_shipping_id)
line.update({
'price_tax': sum(
t.get('amount', 0.0) for t in taxes.get('taxes', [])),
'price_total': taxes['total_included'],
'price_subtotal': taxes['total_excluded'],
})
else:
super(SaleOrderLine, line)._compute_amount()

@api.constrains('expected_delivery_date', 'expected_end_date')
def _check_expected_dates(self):
for line in self.filtered(
lambda l: l.expected_delivery_date or l.expected_end_date):
if ((line.expected_delivery_date and not line.expected_end_date)
or (line.expected_end_date and
not line.expected_delivery_date)):
raise ValidationError(_('There must be expected delivery '
'date and expected end date.'))
if line.expected_delivery_date > line.expected_end_date:
raise ValidationError(_('Expected end date must be after '
'expected delivery date.'))
55 changes: 55 additions & 0 deletions sale_order_rental/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2019 Oihana Larrañaga - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


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

expected_delivery_date = fields.Date(
string='Expected Delivery Date')
expected_end_date = fields.Date(
string='Expected Return Date')
rental_days = fields.Integer(
string='Rental Days', compute='_compute_rental_days', store=True)

@api.depends('expected_delivery_date', 'expected_end_date')
def _compute_rental_days(self):
for line in self.filtered(
lambda l: l.expected_delivery_date and l.expected_end_date):
line.rental_days = (
line.expected_end_date - line.expected_delivery_date).days + 1

@api.depends('product_uom_qty', 'discount', 'price_unit', 'tax_id',
'rental_days')
def _compute_amount(self):
for line in self:
if line.rental_days:
price = line.price_unit * (1 - (line.discount or 0.0) / 100.0)
taxes = line.tax_id.compute_all(
price, line.order_id.currency_id,
line.product_uom_qty * line.rental_days,
product=line.product_id,
partner=line.order_id.partner_shipping_id)
line.update({
'price_tax': sum(
t.get('amount', 0.0) for t in taxes.get('taxes', [])),
'price_total': taxes['total_included'],
'price_subtotal': taxes['total_excluded'],
})
else:
super(SaleOrderLine, line)._compute_amount()

@api.constrains('expected_delivery_date', 'expected_end_date')
def _check_expected_dates(self):
for line in self.filtered(
lambda l: l.expected_delivery_date or l.expected_end_date):
if ((line.expected_delivery_date and not line.expected_end_date)
or (line.expected_end_date and
not line.expected_delivery_date)):
raise ValidationError(_('There must be expected delivery '
'date and expected end date.'))
if line.expected_delivery_date > line.expected_end_date:
raise ValidationError(_('Expected end date must be after '
'expected delivery date.'))
Loading

0 comments on commit 95113a8

Please sign in to comment.