Thursday, October 3, 2019

period between start date and end date in odoo



We can calculate the period of time between start date and end date by following the steps bellow

  • go to the python class and define start_date field which will contain the start date , end_date field which will contain the end date , period_years which will contain the years between the start date and the end date which we will calculate , period_months which will contain the months between the start date and the end date which we will calculate , period_days which will contain the days between the start date and the end date which we will calculate as bellow

start_date = fields.Date(string="Start Date", required=True)
end_date = fields.Date(string="End Date", required=True)
period_years = fields.Integer(string="Years", compute="get_period_time")
period_months = fields.Integer(string="Months", compute="get_period_time")
period_days = fields.Integer(string="Days", compute="get_period_time")

  • create get_period_time function which will calculate the date between the start date and the end date as

    @api.one
    def get_period_time(self):
        """
        function to calcolate the period of time 
        when we know the start date and the end date
        """
        if self.start_date and self.end_date:
            start_date = str(self.start_date)
            end_date = str(self.end_date)

            start_year_as_int = int(start_date[0]+start_date[1]+start_date[2]+start_date[3])
            start_month_as_int = int(start_date[5]+start_date[6])
            start_day_as_int = int(start_date[8]+start_date[9])

            end_year_as_int = int(end_date[0]+end_date[1]+end_date[2]+end_date[3])
            end_month_as_int = int(end_date[5]+end_date[6])
            end_day_as_int = int(end_date[8]+end_date[9])

            period_years = end_year_as_int-start_year_as_int
            period_months = end_month_as_int-start_month_as_int
            period_days = end_day_as_int-start_day_as_int

            months_list_1 = ['04','06','09','11']
            months_list_2 = ['01','03','05','07','08','10','12']

            if period_days < 0:
                if str(end_month_as_int) == '02':
                    if end_year_as_int%4 == 0:
                        period_days = 29+period_days
                    if end_year_as_int%4 != 0:
                        period_days = 28+period_days
                for index in range(0,4):
                    if end_month_as_int == int(months_list_1[index]):
                        period_days = 30+period_days
                for index in range(0,7):
                    if end_month_as_int == int(months_list_2    [index]):
                        period_days = 31+period_days
                period_months = period_months-1
            if period_months < 0:
                period_months = 12+period_months
                period_years = period_years-1

            self.period_years = period_years
            self.period_months = period_months
            self.period_days = period_days

  • design our fields view as below , which will show the fields as in the image

                              <group colspan="4" col="4">
                                    <field name="start_date"/>
                                    <field name="end_date"/>
                                    <table>
                                        <tr>
                                            <td>
                                                <b style="font-size:13px;">
                                                    Period&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
                                                </b>
                                            </td>
                                            <td><field name="period_years" nolabel="1"/></td>
                                            <td>&#160;Y&#160;&#160;-&#160;&#160;</td>
                                            <td><field name="period_months" nolabel="1"/></td>
                                            <td>&#160;M&#160;&#160;-&#160;&#160;</td>
                                            <td><field name="period_days" nolabel="1"/></td>
                                            <td>&#160;D</td>
                                        </tr>
                                    </table>
                                </group>

No comments:

Post a Comment

Odoo Invoice Qr code issues

There are two main issues must of us facing with the QR code in Odoo invoice & these issues are 1/ QR code displayed as broken image w...