update
This commit is contained in:
6
fusion_loaners_management/wizard/__init__.py
Normal file
6
fusion_loaners_management/wizard/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2024-2026 Nexa Systems Inc.
|
||||
# License OPL-1 (Odoo Proprietary License v1.0)
|
||||
|
||||
from . import loaner_checkout_wizard
|
||||
from . import loaner_return_wizard
|
||||
159
fusion_loaners_management/wizard/loaner_checkout_wizard.py
Normal file
159
fusion_loaners_management/wizard/loaner_checkout_wizard.py
Normal file
@@ -0,0 +1,159 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2024-2026 Nexa Systems Inc.
|
||||
# License OPL-1 (Odoo Proprietary License v1.0)
|
||||
|
||||
from odoo import models, fields, api, _
|
||||
from odoo.exceptions import UserError
|
||||
import logging
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LoanerCheckoutWizard(models.TransientModel):
|
||||
_name = 'fusion.loaner.checkout.wizard'
|
||||
_description = 'Loaner Checkout Wizard'
|
||||
|
||||
sale_order_id = fields.Many2one('sale.order', string='Sale Order', readonly=True)
|
||||
partner_id = fields.Many2one('res.partner', string='Client', required=True)
|
||||
authorizer_id = fields.Many2one('res.partner', string='Authorizer')
|
||||
|
||||
product_id = fields.Many2one(
|
||||
'product.product',
|
||||
string='Product',
|
||||
domain="[('x_fc_can_be_loaned', '=', True)]",
|
||||
required=True,
|
||||
)
|
||||
lot_id = fields.Many2one(
|
||||
'stock.lot',
|
||||
string='Serial Number',
|
||||
domain="[('product_id', '=', product_id)]",
|
||||
)
|
||||
available_lot_ids = fields.Many2many(
|
||||
'stock.lot',
|
||||
compute='_compute_available_lots',
|
||||
string='Available Serial Numbers',
|
||||
)
|
||||
|
||||
checkout_date = fields.Date(
|
||||
string='Checkout Date',
|
||||
required=True,
|
||||
default=fields.Date.context_today,
|
||||
)
|
||||
loaner_period_days = fields.Integer(string='Loaner Period (Days)', default=7)
|
||||
expected_return_date = fields.Date(
|
||||
string='Expected Return Date',
|
||||
compute='_compute_expected_return',
|
||||
)
|
||||
|
||||
checkout_condition = fields.Selection([
|
||||
('excellent', 'Excellent'),
|
||||
('good', 'Good'),
|
||||
('fair', 'Fair'),
|
||||
('needs_repair', 'Needs Repair'),
|
||||
], string='Condition', default='excellent', required=True)
|
||||
checkout_notes = fields.Text(string='Notes')
|
||||
|
||||
checkout_photo_ids = fields.Many2many(
|
||||
'ir.attachment',
|
||||
'loaner_checkout_wizard_photo_rel',
|
||||
'wizard_id',
|
||||
'attachment_id',
|
||||
string='Photos',
|
||||
)
|
||||
|
||||
delivery_address = fields.Text(string='Delivery Address')
|
||||
|
||||
@api.depends('product_id')
|
||||
def _compute_available_lots(self):
|
||||
for wizard in self:
|
||||
if wizard.product_id:
|
||||
loaner_location = self.env.ref(
|
||||
'fusion_loaners_management.stock_location_loaner',
|
||||
raise_if_not_found=False,
|
||||
)
|
||||
if loaner_location:
|
||||
quants = self.env['stock.quant'].search([
|
||||
('product_id', '=', wizard.product_id.id),
|
||||
('location_id', '=', loaner_location.id),
|
||||
('quantity', '>', 0),
|
||||
])
|
||||
wizard.available_lot_ids = quants.mapped('lot_id')
|
||||
else:
|
||||
wizard.available_lot_ids = self.env['stock.lot'].search([
|
||||
('product_id', '=', wizard.product_id.id),
|
||||
])
|
||||
else:
|
||||
wizard.available_lot_ids = False
|
||||
|
||||
@api.depends('checkout_date', 'loaner_period_days')
|
||||
def _compute_expected_return(self):
|
||||
from datetime import timedelta
|
||||
for wizard in self:
|
||||
if wizard.checkout_date and wizard.loaner_period_days:
|
||||
wizard.expected_return_date = wizard.checkout_date + timedelta(days=wizard.loaner_period_days)
|
||||
else:
|
||||
wizard.expected_return_date = False
|
||||
|
||||
@api.onchange('product_id')
|
||||
def _onchange_product_id(self):
|
||||
if self.product_id:
|
||||
self.loaner_period_days = self.product_id.x_fc_loaner_period_days or 7
|
||||
self.lot_id = False
|
||||
|
||||
@api.model
|
||||
def default_get(self, fields_list):
|
||||
res = super().default_get(fields_list)
|
||||
active_model = self._context.get('active_model')
|
||||
active_id = self._context.get('active_id')
|
||||
if active_model == 'sale.order' and active_id:
|
||||
order = self.env['sale.order'].browse(active_id)
|
||||
res['sale_order_id'] = order.id
|
||||
res['partner_id'] = order.partner_id.id
|
||||
if hasattr(order, 'x_fc_authorizer_id') and order.x_fc_authorizer_id:
|
||||
res['authorizer_id'] = order.x_fc_authorizer_id.id
|
||||
if order.partner_shipping_id:
|
||||
res['delivery_address'] = order.partner_shipping_id.contact_address
|
||||
ICP = self.env['ir.config_parameter'].sudo()
|
||||
default_period = int(ICP.get_param('fusion_loaners.default_loaner_period_days', '7'))
|
||||
res['loaner_period_days'] = default_period
|
||||
return res
|
||||
|
||||
def action_checkout(self):
|
||||
self.ensure_one()
|
||||
if not self.product_id:
|
||||
raise UserError(_("Please select a product."))
|
||||
photo_ids = []
|
||||
for photo in self.checkout_photo_ids:
|
||||
new_attachment = self.env['ir.attachment'].create({
|
||||
'name': photo.name,
|
||||
'datas': photo.datas,
|
||||
'res_model': 'fusion.loaner.checkout',
|
||||
'res_id': 0,
|
||||
})
|
||||
photo_ids.append(new_attachment.id)
|
||||
checkout_vals = {
|
||||
'sale_order_id': self.sale_order_id.id if self.sale_order_id else False,
|
||||
'partner_id': self.partner_id.id,
|
||||
'authorizer_id': self.authorizer_id.id if self.authorizer_id else False,
|
||||
'sales_rep_id': self.env.user.id,
|
||||
'product_id': self.product_id.id,
|
||||
'lot_id': self.lot_id.id if self.lot_id else False,
|
||||
'checkout_date': self.checkout_date,
|
||||
'loaner_period_days': self.loaner_period_days,
|
||||
'checkout_condition': self.checkout_condition,
|
||||
'checkout_notes': self.checkout_notes,
|
||||
'delivery_address': self.delivery_address,
|
||||
}
|
||||
checkout = self.env['fusion.loaner.checkout'].create(checkout_vals)
|
||||
if photo_ids:
|
||||
self.env['ir.attachment'].browse(photo_ids).write({'res_id': checkout.id})
|
||||
checkout.checkout_photo_ids = [(6, 0, photo_ids)]
|
||||
checkout.action_checkout()
|
||||
return {
|
||||
'type': 'ir.actions.act_window',
|
||||
'name': _('Loaner Checkout'),
|
||||
'res_model': 'fusion.loaner.checkout',
|
||||
'res_id': checkout.id,
|
||||
'view_mode': 'form',
|
||||
'target': 'current',
|
||||
}
|
||||
107
fusion_loaners_management/wizard/loaner_return_wizard.py
Normal file
107
fusion_loaners_management/wizard/loaner_return_wizard.py
Normal file
@@ -0,0 +1,107 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2024-2026 Nexa Systems Inc.
|
||||
# License OPL-1 (Odoo Proprietary License v1.0)
|
||||
|
||||
from odoo import models, fields, api, _
|
||||
from odoo.exceptions import UserError
|
||||
import logging
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LoanerReturnWizard(models.TransientModel):
|
||||
_name = 'fusion.loaner.return.wizard'
|
||||
_description = 'Loaner Return Wizard'
|
||||
|
||||
checkout_id = fields.Many2one(
|
||||
'fusion.loaner.checkout',
|
||||
string='Checkout Record',
|
||||
required=True,
|
||||
readonly=True,
|
||||
)
|
||||
|
||||
product_id = fields.Many2one(
|
||||
'product.product',
|
||||
string='Product',
|
||||
related='checkout_id.product_id',
|
||||
readonly=True,
|
||||
)
|
||||
lot_id = fields.Many2one(
|
||||
'stock.lot',
|
||||
string='Serial Number',
|
||||
related='checkout_id.lot_id',
|
||||
readonly=True,
|
||||
)
|
||||
partner_id = fields.Many2one(
|
||||
'res.partner',
|
||||
string='Client',
|
||||
related='checkout_id.partner_id',
|
||||
readonly=True,
|
||||
)
|
||||
checkout_date = fields.Date(
|
||||
string='Checkout Date',
|
||||
related='checkout_id.checkout_date',
|
||||
readonly=True,
|
||||
)
|
||||
days_out = fields.Integer(
|
||||
string='Days Out',
|
||||
related='checkout_id.days_out',
|
||||
readonly=True,
|
||||
)
|
||||
checkout_condition = fields.Selection(
|
||||
related='checkout_id.checkout_condition',
|
||||
readonly=True,
|
||||
)
|
||||
|
||||
return_date = fields.Date(
|
||||
string='Return Date',
|
||||
required=True,
|
||||
default=fields.Date.context_today,
|
||||
)
|
||||
return_condition = fields.Selection([
|
||||
('excellent', 'Excellent'),
|
||||
('good', 'Good'),
|
||||
('fair', 'Fair'),
|
||||
('needs_repair', 'Needs Repair'),
|
||||
('damaged', 'Damaged'),
|
||||
], string='Condition', required=True)
|
||||
return_notes = fields.Text(string='Notes')
|
||||
return_photo_ids = fields.Many2many(
|
||||
'ir.attachment',
|
||||
'loaner_return_wizard_photo_rel',
|
||||
'wizard_id',
|
||||
'attachment_id',
|
||||
string='Photos',
|
||||
)
|
||||
|
||||
@api.model
|
||||
def default_get(self, fields_list):
|
||||
res = super().default_get(fields_list)
|
||||
checkout_id = self._context.get('default_checkout_id')
|
||||
if checkout_id:
|
||||
checkout = self.env['fusion.loaner.checkout'].browse(checkout_id)
|
||||
res['checkout_id'] = checkout.id
|
||||
res['return_condition'] = checkout.checkout_condition
|
||||
return res
|
||||
|
||||
def action_return(self):
|
||||
self.ensure_one()
|
||||
if not self.checkout_id:
|
||||
raise UserError(_("No checkout record found."))
|
||||
if self.checkout_id.state not in ('checked_out', 'overdue', 'rental_pending'):
|
||||
raise UserError(_("This loaner has already been returned or is not in a returnable state."))
|
||||
photo_ids = []
|
||||
for photo in self.return_photo_ids:
|
||||
new_attachment = self.env['ir.attachment'].create({
|
||||
'name': photo.name,
|
||||
'datas': photo.datas,
|
||||
'res_model': 'fusion.loaner.checkout',
|
||||
'res_id': self.checkout_id.id,
|
||||
})
|
||||
photo_ids.append(new_attachment.id)
|
||||
self.checkout_id.action_process_return(
|
||||
return_condition=self.return_condition,
|
||||
return_notes=self.return_notes,
|
||||
return_photos=photo_ids if photo_ids else None,
|
||||
)
|
||||
return {'type': 'ir.actions.act_window_close'}
|
||||
Reference in New Issue
Block a user