Update rental_contract.py

This commit is contained in:
zainab2097 2024-08-08 15:46:30 +03:00 committed by GitHub
parent edddbc79fb
commit c3c64bd4b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 35 additions and 54 deletions

View File

@ -508,22 +508,16 @@ class RentalContract(models.Model):
:return:
"""
property_id = self.property_id.id
tax_id = 0
date_from = datetime.strptime(datetime.strftime(self.date_from, '%Y-%m-%d'), '%Y-%m-%d').date()
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
# tax_ids = self.env['account.tax'].search([('type_tax_use', '=', 'sale')], limit=1)
# if not tax_ids:
# raise exceptions.ValidationError(_('To Proceed, You must have a sale tax role'))
# for tax in tax_ids:
# tax_id = tax.id
################################# section 1 ################################
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
@ -533,69 +527,56 @@ 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"))
################################# section 2 generate lines of payment based on given information ############
# 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)
date_from = datetime.strptime(datetime.strftime(self.date_from, '%Y-%m-%d'), '%Y-%m-%d').date()
service_date_from = datetime.strptime(datetime.strftime(self.date_from, '%Y-%m-%d'), '%Y-%m-%d').date()
next_date = date_from
service_next_date = service_date_from
service_next_date = date_from
payment = 0
service = 0
no_year = 1
no_payment = 1
# Service Fixed In Yearly Based
while payment < no_payments:
raise_line = [line for line in self.annual_raise_ids if
line.due_date_raise == next_date]
if len(raise_line) > 0:
amount = raise_line[0].rent_amount_after_raise
rent_amount_per_payment = amount / (rent_factor / months)
year = date_from.year
# 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',
(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',
(
str(_('Payment') + str(payment + 1)),
str(self.id),
str(date_from.strftime('%Y-%m-%d')),
str(self.property_id.id),
str(rent_amount_per_payment),
'0.0',
'0.0',
str(self.user_id.id),
str(self.env.user.company_id.id),
str('draft'),
f'Payment {payment + 1}',
self.id,
next_date.strftime('%Y-%m-%d'),
self.property_id.id,
rent_amount_per_payment,
water_amount_per_payment if payment < no_services_payment else 0.0,
services_amount_per_payment if payment < no_services_payment else 0.0,
self.user_id.id,
self.env.user.company_id.id,
'draft',
))
if self.rent_kind in ['month', 'year']:
next_date += relativedelta(months=months)
if date_to > next_date:
date_from = next_date
# increase while loop by 1 to move forward
# Move to the next payment date
next_date += relativedelta(months=months)
payment += 1
while no_services_payment > service and service_next_date <= date_to:
# update query to set the value of service based on it date
query = """update rent_payment set service_cost = %s, water_cost = %s where due_date = %s and contract_id = %s """
self._cr.execute(query, (str(round(services_amount_per_payment, 2)),
str(round(water_amount_per_payment, 2)),
str(service_date_from.strftime('%Y-%m-%d')),
str(self.id)))
if self.rent_kind in ['month', 'year']:
service_next_date += relativedelta(months=service_months)
if date_to > service_next_date:
service_date_from = service_next_date
service += 1
# TODO: Find a Proper Way To Solve The Problem
# 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')))
# Update amounts (e.g., tax calculations) for each rent payment line
for line in self.rent_payment_ids:
# line.get_tax_amount()
line.get_untaxed_amount()
line.get_total_amount()
###################################### End of Generating Payment ############################
###################################### End of Generating Payment ############################
class ResPartner(models.Model):
_inherit = 'res.partner'