Skip to content

Commit

Permalink
[IMP] sale_order_rental: separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
oihane committed Nov 18, 2019
1 parent f074896 commit 55dba3e
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 180 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()
51 changes: 0 additions & 51 deletions sale_order_rental/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,54 +63,3 @@ def action_confirm(self):
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.'))
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,6 @@
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools.float_utils import float_round


class StockPicking(models.Model):
_inherit = 'stock.picking'

expected_delivery_date = fields.Date(
string='Expected Delivery Date',
related='sale_id.expected_delivery_date')
expected_end_date = fields.Date(
string='Expected Return Date', related='sale_id.expected_end_date')
rental_days = fields.Integer(
related='sale_id.rental_days')

@api.multi
def _return_rental(self):
self.ensure_one()
if self.state == 'cancel':
return
res = {'picking_id': self.id}
product_return_moves = []
move_dest_exists = False
for move in self.move_lines.filtered('sale_line_id.rental_days'):
if move.scrapped:
continue
if move.move_dest_ids:
move_dest_exists = True
quantity = move.product_qty - sum(move.move_dest_ids.filtered(
lambda m: m.state in ['partially_available', 'assigned',
'done']).mapped('move_line_ids').mapped(
'product_qty'))
quantity = float_round(
quantity, precision_rounding=move.product_uom.rounding)
product_return_moves.append(
(0, 0, {'product_id': move.product_id.id,
'quantity': quantity,
'move_id': move.id,
'uom_id': move.product_id.uom_id.id}))
if not product_return_moves:
return
if self.location_id.usage == 'internal':
res.update({
'parent_location_id':
(self.picking_type_id.warehouse_id and
self.picking_type_id.warehouse_id.view_location_id.id or
self.location_id.location_id.id),
})
location_id = self.location_id.id
if self.picking_type_id.return_picking_type_id\
.default_location_dest_id.return_location:
location_id = \
self.picking_type_id.return_picking_type_id\
.default_location_dest_id.id
res.update({
'product_return_moves': product_return_moves,
'move_dest_exists': move_dest_exists,
'original_location_id': self.location_id.id,
'location_id': location_id,
})
return_wiz = self.env['stock.return.picking'].create(res)
return_wiz._create_returns()
return True


class StockMove(models.Model):
Expand Down
65 changes: 65 additions & 0 deletions sale_order_rental/models/stock_picking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 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.tools.float_utils import float_round


class StockPicking(models.Model):
_inherit = 'stock.picking'

expected_delivery_date = fields.Date(
string='Expected Delivery Date',
related='sale_id.expected_delivery_date')
expected_end_date = fields.Date(
string='Expected Return Date', related='sale_id.expected_end_date')
rental_days = fields.Integer(
related='sale_id.rental_days')

@api.multi
def _return_rental(self):
self.ensure_one()
if self.state == 'cancel':
return
res = {'picking_id': self.id}
product_return_moves = []
move_dest_exists = False
for move in self.move_lines.filtered('sale_line_id.rental_days'):
if move.scrapped:
continue
if move.move_dest_ids:
move_dest_exists = True
quantity = move.product_qty - sum(move.move_dest_ids.filtered(
lambda m: m.state in ['partially_available', 'assigned',
'done']).mapped('move_line_ids').mapped(
'product_qty'))
quantity = float_round(
quantity, precision_rounding=move.product_uom.rounding)
product_return_moves.append(
(0, 0, {'product_id': move.product_id.id,
'quantity': quantity,
'move_id': move.id,
'uom_id': move.product_id.uom_id.id}))
if not product_return_moves:
return
if self.location_id.usage == 'internal':
res.update({
'parent_location_id':
(self.picking_type_id.warehouse_id and
self.picking_type_id.warehouse_id.view_location_id.id or
self.location_id.location_id.id),
})
location_id = self.location_id.id
if self.picking_type_id.return_picking_type_id\
.default_location_dest_id.return_location:
location_id = \
self.picking_type_id.return_picking_type_id\
.default_location_dest_id.id
res.update({
'product_return_moves': product_return_moves,
'move_dest_exists': move_dest_exists,
'original_location_id': self.location_id.id,
'location_id': location_id,
})
return_wiz = self.env['stock.return.picking'].create(res)
return_wiz._create_returns()
return True

0 comments on commit 55dba3e

Please sign in to comment.