[UPDATE]UPDATE
This commit is contained in:
parent
47822b9614
commit
551e8c657e
|
|
@ -10,11 +10,14 @@ from odoo.tools import exception_to_unicode
|
|||
from odoo import models, fields, api, exceptions, tools, _
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ResPartner(models.Model):
|
||||
_inherit = 'res.partner'
|
||||
|
||||
is_tenant = fields.Boolean(string="Is Tenant?")
|
||||
|
||||
|
||||
|
||||
class RentalContractTemplate(models.Model):
|
||||
_name = 'rental.contract.template'
|
||||
|
||||
|
|
@ -106,7 +109,7 @@ class RentalContract(models.Model):
|
|||
('unit', 'Unit')], string="Rent Method")
|
||||
property_id = fields.Many2one('internal.property', string="Property", tracking=True)
|
||||
unit_ids = fields.Many2many('re.unit', string="Units", tracking=True)
|
||||
partner_id = fields.Many2one('res.partner', string="Renter",domain=[('is_tenant', '=', True)])
|
||||
partner_id = fields.Many2one('res.partner', string="Renter", domain=[('is_tenant', '=', True)])
|
||||
identification_type = fields.Selection(related="partner_id.identification_type", string='Identification Type')
|
||||
identification_number = fields.Char(related="partner_id.identification_number", string='Identification NUmber')
|
||||
identification_issue_date = fields.Date(related="partner_id.identification_issue_date",
|
||||
|
|
@ -207,7 +210,6 @@ class RentalContract(models.Model):
|
|||
payment.get_untaxed_amount() # Recalculate amounts with the new tax
|
||||
payment.get_total_amount()
|
||||
|
||||
|
||||
def action_renew(self):
|
||||
"""
|
||||
Renew the contract and link the previous contract to the new one
|
||||
|
|
@ -310,11 +312,13 @@ class RentalContract(models.Model):
|
|||
'name': contract.name + ' - ' + contract.seq + ' - ' + str(contract.date),
|
||||
'price_unit': amount,
|
||||
'quantity': 1.0,
|
||||
'account_id':contract.accrued_account_id.id,
|
||||
'account_id': contract.accrued_account_id.id,
|
||||
})],
|
||||
'line_ids': [ (0, 0, { 'account_id': contract.accrued_account_id.id, 'debit': 0.0, 'credit': amount,'name': contract.name + ' - ' + contract.seq + ' - ' + str(contract.date), 'quantity':1}),
|
||||
(0, 0, { 'account_id': contract.debit_account_id.id, 'debit': amount, 'credit': 0.0, 'quantity':1}) ]
|
||||
|
||||
'line_ids': [(0, 0, {'account_id': contract.accrued_account_id.id, 'debit': 0.0, 'credit': amount,
|
||||
'name': contract.name + ' - ' + contract.seq + ' - ' + str(contract.date),
|
||||
'quantity': 1}),
|
||||
(0, 0,
|
||||
{'account_id': contract.debit_account_id.id, 'debit': amount, 'credit': 0.0, 'quantity': 1})]
|
||||
|
||||
}
|
||||
return invoice_vals
|
||||
|
|
@ -326,7 +330,7 @@ class RentalContract(models.Model):
|
|||
line_id = invoice.invoice_line_ids[1].id
|
||||
commands = [(2, line_id, 0)]
|
||||
invoice.write({'invoice_line_ids': commands})
|
||||
|
||||
|
||||
self.insurance_invoice_id = invoice.id
|
||||
if self.rent_method != 'property':
|
||||
for unit in self.unit_ids:
|
||||
|
|
@ -530,12 +534,12 @@ class RentalContract(models.Model):
|
|||
date_to = datetime.strptime(datetime.strftime(self.date_to, '%Y-%m-%d'), '%Y-%m-%d').date()
|
||||
months = int(self.rent_type.months)
|
||||
service_months = 12
|
||||
|
||||
|
||||
names = []
|
||||
if self.unit_ids:
|
||||
for unit in self.unit_ids:
|
||||
names.append(unit.name)
|
||||
|
||||
|
||||
# the rent factor is used to get the number of payments
|
||||
# also to get the rent amount based on rent_kind as following
|
||||
rent_factor = 1.0
|
||||
|
|
@ -545,31 +549,31 @@ class RentalContract(models.Model):
|
|||
raise exceptions.ValidationError(_('Please set the rent duration and start date'))
|
||||
if months == 0:
|
||||
raise exceptions.ValidationError(_("In rent type please make sure that the month number is more than 0"))
|
||||
|
||||
|
||||
# Calculate the number of payments and the amounts per payment
|
||||
no_payments = (self.rent_duration * rent_factor) / months
|
||||
no_services_payment = (self.rent_duration * rent_factor) / service_months
|
||||
rent_amount_per_payment = self.cal_rent_amount / (rent_factor / months)
|
||||
water_amount_per_payment = self.water_cost / (rent_factor / service_months)
|
||||
services_amount_per_payment = self.service_amount / (rent_factor / service_months)
|
||||
|
||||
|
||||
next_date = date_from
|
||||
service_next_date = date_from
|
||||
payment = 0
|
||||
service = 0
|
||||
|
||||
|
||||
while payment < no_payments:
|
||||
# Check for any rent raise applicable on the due date
|
||||
raise_line = next((line for line in self.annual_raise_ids if line.due_date_raise == next_date), None)
|
||||
if raise_line:
|
||||
rent_amount_per_payment = raise_line.rent_amount_after_raise / (rent_factor / months)
|
||||
|
||||
|
||||
# Insert rent payment line
|
||||
self._cr.execute('INSERT INTO rent_payment \
|
||||
(name, contract_id, due_date, property_id, amount, water_cost, service_cost, user_id, company_id, state) \
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id',
|
||||
(
|
||||
f'Payment {payment + 1}',
|
||||
(name, contract_id, due_date, property_id, amount, water_cost, service_cost, user_id, company_id, state, tax_id) \
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id',
|
||||
(
|
||||
_('Payment %s') % (payment + 1),
|
||||
self.id,
|
||||
next_date.strftime('%Y-%m-%d'),
|
||||
self.property_id.id,
|
||||
|
|
@ -579,17 +583,18 @@ class RentalContract(models.Model):
|
|||
self.user_id.id,
|
||||
self.env.user.company_id.id,
|
||||
'draft',
|
||||
self.tax_id.id, # Include sales tax
|
||||
self.tax_id.id
|
||||
))
|
||||
|
||||
|
||||
# Move to the next payment date
|
||||
next_date += relativedelta(months=months)
|
||||
payment += 1
|
||||
|
||||
|
||||
# Update any remaining rent payments with service costs
|
||||
query = """UPDATE rent_payment SET service_cost = %s, water_cost = %s WHERE contract_id = %s AND due_date <= %s"""
|
||||
self._cr.execute(query, (services_amount_per_payment, water_amount_per_payment, self.id, date_to.strftime('%Y-%m-%d')))
|
||||
|
||||
self._cr.execute(query,
|
||||
(services_amount_per_payment, water_amount_per_payment, self.id, date_to.strftime('%Y-%m-%d')))
|
||||
|
||||
# Update amounts (e.g., tax calculations) for each rent payment line
|
||||
for line in self.rent_payment_ids:
|
||||
line.get_untaxed_amount()
|
||||
|
|
@ -597,6 +602,7 @@ class RentalContract(models.Model):
|
|||
|
||||
###################################### End of Generating Payment ############################
|
||||
|
||||
|
||||
class ResPartner(models.Model):
|
||||
_inherit = 'res.partner'
|
||||
_description = "Renter"
|
||||
|
|
|
|||
Loading…
Reference in New Issue