From c227185dfa478ab9351b0051cca7c370dbf24e4a Mon Sep 17 00:00:00 2001 From: ljmwkx <1340053001@qq.com> Date: Fri, 20 Dec 2024 18:53:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=A9=E4=B8=9A=20=E5=BC=80=E5=A7=8B?= =?UTF-8?q?=E7=BB=93=E6=9D=9F=E6=97=B6=E9=97=B4=20=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../models/property_lease_contract.py | 271 +++++++++++------- .../views/property_lease_contract.xml | 15 +- 2 files changed, 182 insertions(+), 104 deletions(-) diff --git a/yuthon_property_approval/models/property_lease_contract.py b/yuthon_property_approval/models/property_lease_contract.py index 70700adb1..98722605b 100644 --- a/yuthon_property_approval/models/property_lease_contract.py +++ b/yuthon_property_approval/models/property_lease_contract.py @@ -44,9 +44,9 @@ class PropertyLeaseContract(models.Model): address_for_delivery = fields.Char(string="送达地址") lease_term = fields.Float(string="租期") lease_term_year = fields.Integer(string="租期") - lease_term_year_uom = fields.Selection([('year', '年')], default="year", readonly=True, string="免租期单位") + # lease_term_year_uom = fields.Selection([('year', '年')], default="year", readonly=True, string="免租期单位") lease_term_month = fields.Integer(string="租期") - lease_term_month_uom = fields.Selection([('month', '月')], default="month", readonly=True, string="免租期单位") + # lease_term_month_uom = fields.Selection([('month', '月')], default="month", readonly=True, string="免租期单位") term_start_time = fields.Date(string="租期开始时间", default=fields.Datetime.now) term_end_time = fields.Date(string="租期结束时间") cycle_dict =fields.Selection([('monthly','月度'), ('quarter','季度'), ('half','半年'),('year','年度')],string='付款方式') @@ -61,12 +61,15 @@ class PropertyLeaseContract(models.Model): increment_price = fields.Float(string="递增单价") increment_amount = fields.Float(string="递增金额") rent_free_period = fields.Integer(string="免租期") - rent_free_period_uom = fields.Selection([('year', '年'), ('month', '月')], default="year", string="免租期单位") + rent_free_period_year = fields.Integer(string="免租期") + rent_free_period_month = fields.Integer(string="免租期") + # rent_free_period_uom = fields.Selection([('year', '年'), ('month', '月')], default="year", string="免租期单位") breach_time = fields.Date(string="违约计算时点") percentage_liquidated_damages = fields.Char(string="违约金比例") date_of_payment = fields.Date(string="每月支付日期") unit_price = fields.Float(string="单价") - deposit = fields.Float(string="押金/履约保证金") + deposit = fields.Float(string="押金") + performance_bonds = fields.Float(string="履约保证金") first_year_rent = fields.Float(string="首年租金") total_contract_receivables = fields.Float(string="合同应收") tax_rate = fields.Float(string="税率") @@ -95,6 +98,31 @@ class PropertyLeaseContract(models.Model): contract_time = fields.Date(string="合同签订日期") handover_time = fields.Date(string="交接日期") + @api.onchange("term_start_time", "term_end_time", + "increments_every_few_years", "unit_price", + "first_years_begin", "increment_method", + "increment_price", "increment_percentage", "increment_amount", + "rent_free_period_year", "rent_free_period_month", "total_area") + def onchange_first_year_rent(self): + for record in self: + # 计算首年 + if not record.term_start_time: + raise UserError(_("请填写租期开始时间!")) + if not record.term_end_time: + raise UserError(_("请填写租期结束时间!")) + + start_date = record.term_start_time + end_date = record.term_end_time + relativedelta(days=1) + if start_date + relativedelta(months=12) < end_date: + end_date = start_date + relativedelta(months=12) + all_period_rent = record.my_compute_all_period_rent(start_date, end_date) + record.first_year_rent = all_period_rent + + all_start_date = record.term_start_time + all_end_date = record.term_end_time + relativedelta(days=1) + record.total_contract_receivables = record.my_compute_all_period_rent(all_start_date, all_end_date) + + @api.onchange("term_end_time", "term_start_time") def onchange_term_end_time(self): for record in self: @@ -111,7 +139,7 @@ class PropertyLeaseContract(models.Model): year = record.lease_term_year if record.lease_term_year else 0 month = record.lease_term_month if record.lease_term_month else 0 months = year * 12 + month - record.term_end_time = record.term_start_time + relativedelta(months=months) + record.term_end_time = record.term_start_time + relativedelta(months=months) - relativedelta(days=1) @staticmethod def divide_time_range_fixed(start_time, end_time, months): @@ -136,6 +164,78 @@ class PropertyLeaseContract(models.Model): # start_time = end_of_range # return time_ranges + def my_compute_all_period_rent(self, start_date, end_date): + first_years_begin = self.first_years_begin if self.first_years_begin else 0 + increments_every_few_years = self.increments_every_few_years + monthly_amount = self.total_area * self.unit_price # 月租金 + unit_price = self.unit_price # 单价 + rent_free_period_month = 0 + # 先计算免租期 + if self.rent_free_period_year: + rent_free_period_month += self.rent_free_period_year * 12 + if self.rent_free_period_month: + rent_free_period_month += self.rent_free_period_month + free_start = start_date # 免租期开始 + if rent_free_period_month: + free_start = start_date + relativedelta(months=rent_free_period_month) + + all_period_rent = 0 + if not increments_every_few_years: + # 计算剩余日期 + delta = relativedelta(end_date, free_start) + months = delta.years * 12 + delta.months + all_period_rent += monthly_amount * months + else: + # 不递增年份的计算 + increments_start = free_start # 平稳和递增开始 + all_months = 0 # 第几月份 + # 有免递增年份的计算 + if first_years_begin: + first_index = 0 # 第几递增周期年 + divided_year_segments = self.divide_time_range_fixed(start_date, end_date, 12) + for segment_year in divided_year_segments: + first_index += 1 + increments_start = segment_year[1] + if first_years_begin and first_index > first_years_begin: + # 进入 递增时间 跳出平稳计费极端 + break + delta = relativedelta(segment_year[1], segment_year[0]) + months = delta.years * 12 + delta.months + for month in range(months): + all_months += 1 + # 判断是否是免租期 + if all_months > rent_free_period_month: + all_period_rent += monthly_amount + # 开始递增 + divided_increments_segments = self.divide_time_range_fixed(increments_start, end_date, increments_every_few_years * 12) + index = 0 # 第几递增周期年 + for increments_year in divided_increments_segments: + index += 1 + """ + increment_percentage = fields.Float(string="递增百分比") + increment_price = fields.Float(string="递增单价") + increment_amount = fields.Float(string="递增金额") + """ + increments_month = 0 + if self.increment_method == "increment_price" and self.increment_price: + unit_price = unit_price + self.increment_price + monthly_amount = self.total_area * unit_price + if self.increment_method == "percentage" and self.increment_percentage: + monthly_amount = monthly_amount * self.increment_percentage + if self.increment_method == "increment_amount" and self.increment_amount: + monthly_amount = monthly_amount + self.increment_amount + delta = relativedelta(increments_year[1], increments_year[0]) + months = delta.years * 12 + delta.months + # 判断第几月份进入 付费租期 + for month in range(months): + all_months += 1 + # 判断是否是免租期 + if all_months > rent_free_period_month: + increments_month += 1 + if increments_month: + all_period_rent += monthly_amount * increments_month + return all_period_rent + def action_generate_rental_charges(self): """ 生成租金费用明细 @@ -149,64 +249,9 @@ class PropertyLeaseContract(models.Model): self.sudo().env["property.rent.list"].search([("contract_id", "=", record.id)]).unlink() start_date = record.term_start_time - end_date = record.term_end_time - first_years_begin = record.first_years_begin if record.first_years_begin else 0 - increments_every_few_years = record.increments_every_few_years - monthly_amount = record.total_area * record.unit_price # 月租金 - unit_price = record.unit_price # 单价 - rent_free_period_month = 0 - # 先计算免租期 - if record.rent_free_period and not record.rent_free_period_uom: - raise UserError("请先填写免租期的单位!") + end_date = record.term_end_time + relativedelta(days=1) + all_period_rent = record.my_compute_all_period_rent(start_date, end_date) - if record.rent_free_period and record.rent_free_period_uom: - if record.rent_free_period_uom == "year": - rent_free_period_month = record.rent_free_period * 12 - if record.rent_free_period_uom == "month": - rent_free_period_month = record.rent_free_period - free_start = start_date # 免租期开始 - if rent_free_period_month: - free_start = start_date + relativedelta(months=rent_free_period_month) - - first_index = 0 # 第几递增周期年 - increments_start = free_start # 平稳和递增开始 - all_period_rent = 0 - if not increments_every_few_years: - delta = relativedelta(end_date, free_start) - months = delta.years * 12 + delta.months - all_period_rent += monthly_amount * months - else: - # 不递增年份的计算 - if first_years_begin: - divided_year_segments = self.divide_time_range_fixed(free_start, end_date, 12) - for segment_year in divided_year_segments: - first_index += 1 - if first_years_begin and first_index > first_years_begin: - increments_start = segment_year[0] - break - delta = relativedelta(increments_start, free_start) - months = delta.years * 12 + delta.months - all_period_rent = all_period_rent + (monthly_amount * months) - # 开始递增 - divided_increments_segments = self.divide_time_range_fixed(increments_start, end_date, increments_every_few_years * 12) - index = 0 # 第几递增周期年 - for increments_year in divided_increments_segments: - index += 1 - """ - increment_percentage = fields.Float(string="递增百分比") - increment_price = fields.Float(string="递增单价") - increment_amount = fields.Float(string="递增金额") - """ - delta = relativedelta(increments_year[1], increments_year[0]) - months = delta.years * 12 + delta.months - if record.increment_method == "increment_price" and record.increment_price: - unit_price = unit_price + record.increment_price - monthly_amount = record.total_area * unit_price - if record.increment_method == "percentage" and record.increment_percentage: - monthly_amount = monthly_amount * record.increment_percentage - if record.increment_method == "increment_amount" and record.increment_amount: - monthly_amount = monthly_amount + record.increment_amount - all_period_rent += monthly_amount * months self.sudo().env["property.rent.list"].create({ "contract_id": record.id, "contract_code": record.code, @@ -254,7 +299,7 @@ class PropertyLeaseContract(models.Model): self.sudo().env["property.rent.breakdown.line"].search([("breakdown_id", "=", record.id)]).unlink() start_date = record.term_start_time - end_date = record.term_end_time + end_date = record.term_end_time + relativedelta(days=1) first_years_begin = record.first_years_begin if record.first_years_begin else 0 increments_every_few_years = record.increments_every_few_years @@ -262,14 +307,10 @@ class PropertyLeaseContract(models.Model): unit_price = record.unit_price # 单价 rent_free_period_month = 0 # 先计算免租期 - if record.rent_free_period and not record.rent_free_period_uom: - raise UserError("请先填写免租期的单位!") - - if record.rent_free_period and record.rent_free_period_uom: - if record.rent_free_period_uom == "year": - rent_free_period_month = record.rent_free_period * 12 - if record.rent_free_period_uom == "month": - rent_free_period_month = record.rent_free_period + if record.rent_free_period_year: + rent_free_period_month += record.rent_free_period_year * 12 + if record.rent_free_period_month: + rent_free_period_month += record.rent_free_period_month free_start = start_date # 免租期开始 if rent_free_period_month: free_start = start_date + relativedelta(months=rent_free_period_month) @@ -278,7 +319,7 @@ class PropertyLeaseContract(models.Model): "monthly_amount": monthly_amount, "breakdown_id": record.id, "start_data": start_date, - "end_data": free_start, + "end_data": free_start - relativedelta(days=1), "month_number": rent_free_period_month, "period_rent": 0, }) @@ -294,36 +335,50 @@ class PropertyLeaseContract(models.Model): "monthly_amount": monthly_amount, "breakdown_id": record.id, "start_data": increments_start, - "end_data": end_date, + "end_data": end_date - relativedelta(days=1), "month_number": months, "period_rent": monthly_amount * months, }) # 如果有递增 else: - first_index = 0 # 第几递增周期年 + all_months = 0 # 第几月份 # 有免递增年份的计算 if first_years_begin: - divided_year_segments = self.divide_time_range_fixed(free_start, end_date, 12) + first_index = 0 # 第几递增周期年 + first_period_rent = 0 # 总收费 + first_months = 0 + divided_year_segments = self.divide_time_range_fixed(start_date, end_date, 12) for segment_year in divided_year_segments: first_index += 1 + increments_start = segment_year[1] if first_years_begin and first_index > first_years_begin: - increments_start = segment_year[0] + # 进入 递增时间 跳出平稳计费极端 break - delta = relativedelta(increments_start, free_start) - months = delta.years * 12 + delta.months - self.sudo().env["property.rent.breakdown.line"].create({ - "unit_price": unit_price, - "monthly_amount": monthly_amount, - "breakdown_id": record.id, - "start_data": free_start, - "end_data": increments_start, - "month_number": months, - "period_rent": monthly_amount * months, - }) + delta = relativedelta(segment_year[1], segment_year[0]) + months = delta.years * 12 + delta.months + # 判断第几月份进入 付费租期 + for month in range(months): + all_months += 1 + # 判断是否是免租期 + if all_months > rent_free_period_month: + first_period_rent += monthly_amount + first_months += 1 + + if first_period_rent: + self.sudo().env["property.rent.breakdown.line"].create({ + "unit_price": unit_price, + "monthly_amount": monthly_amount, + "breakdown_id": record.id, + "start_data": increments_start - relativedelta(months=first_months), + "end_data": increments_start - relativedelta(days=1), + "month_number": first_months, + "period_rent": first_period_rent, + }) # 开始递增 divided_increments_segments = self.divide_time_range_fixed(increments_start, end_date, increments_every_few_years*12) index = 0 # 第几递增周期年 + for increments_year in divided_increments_segments: index += 1 """ @@ -331,29 +386,47 @@ class PropertyLeaseContract(models.Model): increment_price = fields.Float(string="递增单价") increment_amount = fields.Float(string="递增金额") """ - delta = relativedelta(increments_year[1], increments_year[0]) - months = delta.years * 12 + delta.months + increments_month = 0 + # delta = relativedelta(increments_year[1], increments_year[0]) + # months = delta.years * 12 + delta.months if record.increment_method == "increment_price" and record.increment_price: unit_price = unit_price + record.increment_price monthly_amount = record.total_area * unit_price if record.increment_method == "percentage" and record.increment_percentage: monthly_amount = monthly_amount * record.increment_percentage + unit_price = monthly_amount / record.total_area if record.increment_method == "increment_amount" and record.increment_amount: monthly_amount = monthly_amount + record.increment_amount - self.sudo().env["property.rent.breakdown.line"].create({ - "unit_price": unit_price, - "monthly_amount": monthly_amount, - "breakdown_id": record.id, - "start_data": increments_year[0], - "end_data": increments_year[1], - "month_number": months, - "period_rent": monthly_amount * months, - }) + unit_price = monthly_amount / record.total_area + delta = relativedelta(increments_year[1], increments_year[0]) + months = delta.years * 12 + delta.months + # 判断第几月份进入 付费租期 + for month in range(months): + # 判断第几月份进入 付费租期 + all_months += 1 + # 判断是否是免租期 + if all_months > rent_free_period_month: + increments_month += 1 + if increments_month: + self.sudo().env["property.rent.breakdown.line"].create({ + "unit_price": unit_price, + "monthly_amount": monthly_amount, + "breakdown_id": record.id, + "start_data": increments_year[1] - relativedelta(months=increments_month), + "end_data": increments_year[1] - relativedelta(days=1), + "month_number": increments_month, + "period_rent": monthly_amount * increments_month, + }) @api.onchange("first_year_rent") def _onchange_first_year_rent(self): for rec in self: - rec.deposit = rec.first_year_rent * 2 + rec.performance_bonds = rec.first_year_rent * 2 + + @api.onchange("total_area", "unit_price") + def _onchange_deposit(self): + for record in self: + record.deposit = record.total_area * record.unit_price * 2 # 月租金 class PropertyLeaseContractLine(models.Model): _name = 'property.lease.contract.line' diff --git a/yuthon_property_approval/views/property_lease_contract.xml b/yuthon_property_approval/views/property_lease_contract.xml index 49341e848..1159a1f94 100644 --- a/yuthon_property_approval/views/property_lease_contract.xml +++ b/yuthon_property_approval/views/property_lease_contract.xml @@ -56,9 +56,11 @@