Untitled

 avatar
unknown
plain_text
9 months ago
1.8 kB
8
Indexable
@api.model
    def is_weekend(self, date):
        weekdays = self.env.company._get_renting_forbidden_days()

        if isinstance(date, str):
            date = fields.Date.from_string(date)
        if time.strptime(date.strftime("%Y-%m-%d"), "%Y-%m-%d").tm_wday in (6, 7):
            return True
        return False


    @api.depends('rental_start_date', 'rental_return_date')
    def _compute_duration(self):
        weekends = []

        self.duration_days = 0
        self.remaining_hours = 0


        for order in self:
            if order.rental_start_date and order.rental_return_date:

                order.rental_start_date = order.rental_start_date.replace(hour=18, minute=30, second=0)
                order.rental_return_date = order.rental_return_date.replace(hour=18, minute=29, second=0)

                date_first = order.rental_start_date + relativedelta(days=1)
                date_last = order.rental_return_date
                date_next = date_first
                weekend_day_count = 0

                while date_next <= date_last:

                    if self.is_weekend(date_next):
                        weekends.append(date_next)
                        weekend_day_count += 1
                    date_next = date_next + timedelta(days=1)

                print ('>>>>>>>>>>>>>>>>>>>>>>>weekend_day_count>>>', weekend_day_count)

                duration = order.rental_return_date - order.rental_start_date

                vals = dict(hour=(duration.days * 24 + duration.seconds / 3600))
                vals['weekend_duration'] = dict(hour=(weekend_day_count * 24))
                
                duration_days = math.ceil(vals['hour'] / 24) - math.ceil(vals['weekend_duration']['hour'] / 24)

                order.duration_days = duration_days
                order.remaining_hours = 00
Editor is loading...
Leave a Comment