Skip to content

Commit

Permalink
[ADD] warranty: created new module warranty
Browse files Browse the repository at this point in the history
- Add warranty wizard
- Add warranty check box
- Add warranty config in setting
- Add warranty config tree view editable
  • Loading branch information
amya-odoo committed Sep 24, 2024
1 parent ababe7f commit 0b1f226
Show file tree
Hide file tree
Showing 27 changed files with 465 additions and 259 deletions.
1 change: 0 additions & 1 deletion installment/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"depends": ["base", "sale_subscription", "documents", "account"],
"data": [
"security/ir.model.access.csv",
"wizard/document_wizard.xml",
"wizard/add_emi_wizard.xml",
"views/installment_views.xml",
"views/res_config_settings_views.xml",
Expand Down
54 changes: 28 additions & 26 deletions installment/models/installment_installment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@
import logging
from odoo import api, Command, models, fields

_logger = logging.getLogger(__name__)


class Installment(models.Model):
_name = "installment.installment"

@api.model
def _cron_create_invoice(self):
_logger.info(" account cron---->")
today = fields.Date.today()
config_param = self.env["ir.config_parameter"]
delay_penalty_process = int(
config_param.get_param("installment.delay_penalty_process", default=0)
)
invoices = self.env["account.move"].search(
[
("move_type", "=", "out_invoice"),
("state", "=", "posted"),
("payment_state", "=", "not_paid"),
("penalty_applied", "=", False),
("invoice_date_due", "<=", today - timedelta(days=delay_penalty_process)),
]
config_param.get_param(
"installment.delay_penalty_process", default=0)
)
_logger.info(" account---->>>")
invoices = self.env["account.move"].search([
("move_type", "=", "out_invoice"),
("state", "=", "posted"),
("payment_state", "=", "not_paid"),
("penalty_applied", "=", False),
(
"invoice_date_due",
"<=",
today - timedelta(days=delay_penalty_process),
),
])
for invoice in invoices:
list = self._calculate_penalty(invoice.amount_total)
for line in invoice.line_ids:
if (line.product_id.id == self.env.ref("installment.product_installment").id):
sale_order = self.env['sale.order'].search([("invoice_ids", "=", invoice.id)])
sale_order = self.env["sale.order"].search(
[("invoice_ids", "=", invoice.id)]
)
# print(sale_order.name)
values = {
"move_type": "out_invoice",
Expand Down Expand Up @@ -64,19 +65,14 @@ def _cron_create_invoice(self):
}
new_invoice = self.env["account.move"].create(values)
new_invoice.action_post()
_logger.info(new_invoice.name)
# Set the invoice with sale order
# print("Invoice set Sale Order")
# print(sale_order.invoice_ids)
# print(new_invoice.line_ids)
sale_order.order_line.invoice_lines = [
Command.set(new_invoice.line_ids.ids)
]
# print(sale_order.name)
# print(sale_order.invoice_ids)
invoice.write({
invoice.write(
{
"penalty_applied": True,
})
}
)
else:
continue

Expand All @@ -86,8 +82,14 @@ def _calculate_penalty(self, amount_total):
Return the list[ penatly amount, due process day, percentage ]
"""
config_param = self.env["ir.config_parameter"]
delay_penalty_percentage = int(config_param.get_param("installment.delay_penalty_percentage", default=0))
delay_penalty_process = int(config_param.get_param("installment.delay_penalty_process", default=0))
delay_penalty_percentage = int(
config_param.get_param(
"installment.delay_penalty_percentage", default=0)
)
delay_penalty_process = int(
config_param.get_param(
"installment.delay_penalty_process", default=0)
)
float_delay_penalty_percentage = delay_penalty_percentage / 100
return [
amount_total * float_delay_penalty_percentage,
Expand Down
2 changes: 1 addition & 1 deletion installment/models/res_config_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ResConfigSettings(models.TransientModel):
)
delay_penalty_percentage = fields.Integer(string="Delay Penalty Percentage", config_parameter="installment.delay_penalty_percentage")
delay_penalty_process = fields.Integer(string="Delay Penalty Process", config_parameter="installment.delay_penalty_process")
nid = fields.Boolean(config_parameter="installment.nid")
nid = fields.Boolean(string="National ID (NID)",config_parameter="installment.nid")
salary_components = fields.Boolean(string="Salary Components", config_parameter="installment.salary_components")
bank_statement = fields.Boolean(string="Bank Statement", config_parameter="installment.bank_statement")
bank_rate_letter = fields.Boolean(string="Bank Rate Letter", config_parameter="installment.bank_rate_letter")
Expand Down
95 changes: 95 additions & 0 deletions installment/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,98 @@

class SalesOrder(models.Model):
_inherit = "sale.order"

def document_upload(self):
workspace_name = "Installment"
sub_folder = self.name
documents = {
'name': 'Documents',
'type': 'ir.actions.act_window',
'res_model': 'documents.document',
'view_mode': 'kanban,tree,form',
}

# Search for existing workspace folder
existing_workspace = self.env["documents.folder"].search(
[("name", "=", workspace_name)], limit=1
)

# Create workspace folder if it doesn't exist
if not existing_workspace:
existing_workspace = self.env["documents.folder"].create({
"name": workspace_name,
"parent_folder_id": None,
"description": f"Workspace for {workspace_name}",
"has_write_access": True,
})

# Search for existing subfolder within the workspace
existing_sub_folder = self.env["documents.folder"].search(
[("name", "=", sub_folder), ("parent_folder_id", "=", existing_workspace.id)], limit=1
)

# Create subfolder if it doesn't exist
if not existing_sub_folder:
existing_sub_folder = self.env["documents.folder"].create({
"name": sub_folder,
"parent_folder_id": existing_workspace.id,
"description": f"Subfolder for {sub_folder} under {workspace_name}",
"has_write_access": True,
})
for name in self._get_document_list():
self._request_document(name, existing_sub_folder)
else:
# Fetch existing documents in the subfolder
exiting_doc_list = self.env["documents.document"].search(
[("folder_id", "=", existing_sub_folder.id)]
)

# Get the list of documents that need to be created
all_doc = self._get_document_list()
for doc in exiting_doc_list:
if doc.name in all_doc:
all_doc.remove(doc.name)

# Request documents if they don't exist
for name in all_doc:
self._request_document(name, existing_sub_folder)

documents['domain'] = [('folder_id', '=', existing_sub_folder.id)]
documents['context'] = {'searchpanel_default_folder_id': existing_sub_folder.id}

return documents

def _get_document_list(self):
# Fetching configuration parameters
config_param = self.env["ir.config_parameter"]

# Initialize the list to store required documents
document_list = []

# Check each config parameter and add the corresponding document name if enabled
if config_param.get_param("installment.nid", default=False):
document_list.append("National ID (NID)")
if config_param.get_param("installment.salary_components", default=False):
document_list.append("Salary Components")
if config_param.get_param("installment.bank_statement", default=False):
document_list.append("Bank Statement")
if config_param.get_param("installment.bank_rate_letter", default=False):
document_list.append("Bank Rate Letter")
if config_param.get_param("installment.rental_contract", default=False):
document_list.append("Rental Contract")
if config_param.get_param("installment.ownership_contract", default=False):
document_list.append("Ownership Contract")

return document_list

def _request_document(self, name, subfolder):

document = self.env['documents.document'].create({
'name': name,
'type': 'empty',
'folder_id': subfolder.id,
'owner_id': self.env.user.id,
'partner_id': self.partner_id.id if self.partner_id else False,
})

return document
1 change: 0 additions & 1 deletion installment/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
installment.access_installment_installment,access_installment_installment,installment.model_installment_installment,base.group_user,1,1,1,1
installment.access_add_emi,access_add_emi,installment.model_add_emi,base.group_user,1,1,1,1
installment.access_installment_document,access_installment_document,installment.model_installment_document,base.group_user,1,1,1,1
5 changes: 2 additions & 3 deletions installment/views/installment_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
<xpath expr="//div[@name='so_button_below_order_lines']/button" position="before">
<button name="installment.action_add_emi" class="btn btn-primary" invisible="state not in ['draft']" string="Add EMI" type="action"/>
</xpath>
<xpath expr="//div[hasclass('oe_button_box')]" position="inside">
<!-- <button name="action_upload_documents" class="btn btn-primary " icon="fa-file-text" string="Document" type="object"/> -->
<button name="installment.action_document_upload" class="btn btn-primary " icon="fa-file-text" string="Document" type="action"/>

<xpath expr="//div[@name='button_box']" position="inside">
<button name="document_upload" class="btn btn-primary " icon="fa-file-text" string="Document" type="object"/>
</xpath>
</field>
</record>
Expand Down
11 changes: 11 additions & 0 deletions installment/views/res_config_settings_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,27 @@
<field name="max_duration" style="width: 40%; min-width: 4rem;"/>
<span> years</span>
</setting>

<setting string="Anual Rate Percentage" help="Define Annual Rate Percentage">
<field name="annual_rate" style="width: 40%; min-width: 4rem;"/>
<span> %Per Years</span>
</setting>

<setting string="Down Payment" help=" Define Down Payment Percentage">
<field name="down_payment" style="width: 40%; min-width: 4rem;"/>
<span> %From Product Price</span>
</setting>

<setting string="Administrative Expenses Percentage" help="Define Administrative Expense Percentage">
<field name="administrative_expenses" style="width: 40%; min-width: 4rem;"/>
<span>%From Amount After D.Payment</span>
</setting>

<setting string="Delay Penalty Percentage" help="Define Delay Penalty Percentage">
<field name="delay_penalty_percentage" style="width: 40%; min-width: 4rem;"/>
<span>%From Monthly Amount</span>
</setting>

<setting string="Delay Penalty Process" help="Define Delay Penalty Process">
<field name="delay_penalty_process" style="width: 40%; min-width: 4rem;"/>
<span>Days</span>
Expand All @@ -35,22 +40,28 @@
</div>
</setting>
</block>

<block title="Needed Documents" name="needed_document">
<setting string="Nid">
<field name="nid"/>
</setting>

<setting string="Salary Components">
<field name="salary_components"/>
</setting>

<setting string="Bank Statement">
<field name="bank_statement"/>
</setting>

<setting string="Bank Rate Letter">
<field name="bank_rate_letter"/>
</setting>

<setting string="Rental Contract">
<field name="rental_contract"/>
</setting>

<setting string="Ownership Contract">
<field name="ownership_contract"/>
</setting>
Expand Down
1 change: 0 additions & 1 deletion installment/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from . import add_emi
from . import document_wizard
67 changes: 36 additions & 31 deletions installment/wizard/add_emi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class AddEmi(models.TransientModel):
_name = "add.emi"

total_so_amount = fields.Float(readonly=True)
down_payment = fields.Float(readonly=True)
remaining_amount = fields.Float(readonly=True)
Expand All @@ -14,64 +15,68 @@ class AddEmi(models.TransientModel):
@api.model
def default_get(self, fields):
res = super().default_get(fields) or {}

active_id = self.env.context["active_id"]
sale_order = self.env["sale.order"].browse(active_id)

config_param = self.env["ir.config_parameter"]
max_duration = float(config_param.get_param("installment.max_duration", default=0.0))

if max_duration == 0:
raise UserError("Max Duration is zero.")

down_payment_percentage = int(config_param.get_param("installment.down_payment", default=0))
annual_rate = int(config_param.get_param("installment.annual_rate", default=0))
administrative_expenses = int(config_param.get_param("installment.administrative_expenses", default=0.0))

# Total
total = sale_order.amount_total

# Down Payment calculated
down_payment_evaluate = (down_payment_percentage / 100) * total

# Administrative Expenses
remaining = total - down_payment_evaluate
adminstrative_expense = (administrative_expenses / 100) * remaining
total_remaining = remaining + adminstrative_expense

# Annually Interest
annuall_intrest = ((annual_rate / 100) * total_remaining) * max_duration
total_remaining += annuall_intrest

# Installment Amount
installment_months = max_duration * 12
installment_ammount = total_remaining / installment_months
# print(annuall_intrest)
res.update(
{
"total_so_amount": sale_order.amount_total,
"down_payment": down_payment_evaluate,
"remaining_amount": sale_order.amount_total - down_payment_evaluate,
"interest": annuall_intrest,
"number_monthly_installment": max_duration * 12,
"installement_amount": installment_ammount,
}
)

res.update({
"total_so_amount": sale_order.amount_total,
"down_payment": down_payment_evaluate,
"remaining_amount": sale_order.amount_total - down_payment_evaluate,
"interest": annuall_intrest,
"number_monthly_installment": max_duration * 12,
"installement_amount": installment_ammount,
})
return res

def add_installment(self):
active_id = self.env.context["active_id"]
sale_order = self.env["sale.order"].browse(active_id)

sale_order.order_line = [
Command.create(
{
"order_id": sale_order.id,
"price_unit": self.installement_amount,
"product_id": self.env.ref("installment.product_installment").id,
"product_uom": self.env.ref("uom.product_uom_unit").id,
"tax_id": None,
}
),
Command.create(
{
"order_id": sale_order.id,
"product_uom_qty": 1.0,
"price_unit": self.down_payment,
"product_id": self.env.ref("installment.product_down_payment").id,
"product_uom": self.env.ref("uom.product_uom_unit").id,
"tax_id": None,
},
),
]
Command.create({
"order_id": sale_order.id,
"price_unit": self.installement_amount,
"product_id": self.env.ref("installment.product_installment").id,
"product_uom": self.env.ref("uom.product_uom_unit").id,
"tax_id": None,
}),
Command.create({
"order_id": sale_order.id,
"product_uom_qty": 1.0,
"price_unit": self.down_payment,
"product_id": self.env.ref("installment.product_down_payment").id,
"product_uom": self.env.ref("uom.product_uom_unit").id,
"tax_id": None,
})]

return
Loading

0 comments on commit 0b1f226

Please sign in to comment.