This question has been flagged
1 Reply
6985 Views

I want Islamic Arabic date in my database. So i add a textbox and manually enter the Islamic Arabic date on that textbox. When i enter Islamic Arabic date, it will automatically convert into Gregorian date.

But i don't know, where should i give the logic?

from openerp.osv import fields, osv 
import math
from datetime import date


def intPart(floatNum):
      if floatNum < -0.0000001: return math.ceil(floatNum - 0.0000001)
      return math.floor(floatNum + 0.0000001)

def Hijri2Gregorian(yr,mth,day):
    jd1 = intPart((11 * yr + 3) / 30.0)
    jd2 = intPart((mth - 1) / 2.0)
    jd = jd1 + 354 * yr + 30 * mth - jd2 + day + 1948440 - 385

    if jd > 2299160:
       l = jd + 68569
       n = intPart((4 * l) / 146097.0)
       l = l - intPart((146097 * n + 3) / 4.0)
       i = intPart((4000 * (l + 1)) / 1461001.0)
       l = l - intPart((1461 * i) / 4.0) + 31
       j = intPart((80 * l) / 2447.0)
       d = l - intPart((2447 * j) / 80.0)
       l = intPart(j / 11.0)
       m = j + 2 - 12 * l
       y = 100 * (n - 49) + i + l
    else:
       j = jd + 1402
       k = intPart((j - 1) / 1461.0)
       l = j - 1461 * k
       n = intPart((l - 1) / 365.0) - intPart(l / 1461.0)
       i = l - 365 * n + 30
       j = intPart((80 * i) / 2447.0)
       d = i - intPart((2447 * j) / 80.0)
       i = intPart(j / 11.0)
       m = j + 2 - 12 * i
       y = 4 * k + n + i - 4716

    return y, m, d

class hr_extra(osv.osv): 
      _inherit = "hr.employee"


      def _get_hijri_date(self, cr, uid, ids, name, args, context=None):
          res = {}
          for self_data in self.browse(cr, uid, ids, context=context):
              islamic_date = self_data.islamic_date
              list = islamic_date.split('.')
              yr = islamic_date(str(list[0]))
              mth = islamic_date(str(list[1]))
              day = islamic_date(str(list[2]))
              Hijri2Gregorian(yr,mth,day)              
          return res

         

      _columns = {
                 'islamic_date' : fields.char('Islamic Date'),
                 'english_date' : fields.function(_get_hijri_date, select=True,string='English Date'),
                 #'date' : fields.date(_get_english_date,string='Alert Date'),
                }
hr_extra()

<record id="hr_extra_form" model="ir.ui.view">

<field name="name">hr.extra.form.inherit</field>

<field name="model">hr.employee</field>

<field name="inherit_id" ref="hr.view_employee_form"/>

<field name="arch" type="xml"> <field name="otherid" position="after">

<field name="islamic_date"/>

<field name="english_date"/>

</field>

</field>

</record>

Avatar
Discard
Best Answer

Hi,

Crate function field where you want add Gregorian date call "Hijri2Gregorian" this function in your function field code. Paste above code in your module.

Try this code

py file

from openerp.osv import fields, osv import math

def intPart(floatNum):
      if floatNum < -0.0000001: return math.ceil(floatNum - 0.0000001)
      return math.floor(floatNum + 0.0000001)

def Hijri2Gregorian(yr, mth, day):
    jd1 = intPart((11 * yr + 3) / 30.0)
    jd2 = intPart((mth - 1) / 2.0)
    jd = jd1 + 354 * yr + 30 * mth - jd2 + day + 1948440 - 385

    if jd > 2299160:
       l = jd + 68569
       n = intPart((4 * l) / 146097.0)
       l = l - intPart((146097 * n + 3) / 4.0)
       i = intPart((4000 * (l + 1)) / 1461001.0)
       l = l - intPart((1461 * i) / 4.0) + 31
       j = intPart((80 * l) / 2447.0)
       d = l - intPart((2447 * j) / 80.0)
       l = intPart(j / 11.0)
       m = j + 2 - 12 * l
       y = 100 * (n - 49) + i + l
    else:
       j = jd + 1402
       k = intPart((j - 1) / 1461.0)
       l = j - 1461 * k
       n = intPart((l - 1) / 365.0) - intPart(l / 1461.0)
       i = l - 365 * n + 30
       j = intPart((80 * i) / 2447.0)
       d = i - intPart((2447 * j) / 80.0)
       i = intPart(j / 11.0)
       m = j + 2 - 12 * i
       y = 4 * k + n + i - 4716

    return y, m, d

class hr_extra(osv.osv): _inherit = "hr.employee"


def _get_hijr_date(self, cr, ud, ids, name, arg, context=None):
    res = {}
    for self_data in self.browse(cr, uid, ids, context=context):
        islamic_date = self_data.islamic_date

        # get Pass yesr, month and day in this function
        yr = islamic_date.year
        mth = islamic_date.month
        day = islamic_date.day

        Hijri2Gregorian(yr, mth, day)
    return res

_columns = {
            'islamic_date' : fields.char('Islamic Date'),
            'english_date' : fields.function(Hijri2Gregorian,'English Date'),

           }
hr_extra()

xml file

<record id="hr_extra_form" model="ir.ui.view"> 
<field name="name">hr.extra.form.inherit</field> 
<field name="model">hr.employee</field> 
<field name="inherit_id" ref="hr.view_employee_form"/> 
<field name="arch" type="xml"> 
    <field name="otherid" position="after">
        <field name="islamic_date"/> 
        <field name="english_date"/>
    </field>            
</field>
</record>

Thanks

Avatar
Discard
Author

https://launchpad.net/pycalverter Can we use these files to convert hijri to geogorian?

you can use.

Author

Did you know, how to implement https://launchpad.net/pycalverter? i dont know where to add this files

Sorry i don't know

I have update code check it

Author

I pass 1428,12,29 like this, but shows error. TypeError: Hijri2Gregorian() takes exactly 3 arguments (7 given)

Author

NameError: global name 'yr' is not defined. this error also. I tried to solve this.

Author

@jack, what happened to this code? I didn't get the output.

@Jack, i have the same error, please help.

Pass argument 'yr', 'mnt' 'day'

_get_hijr_date(self, cr, ud, ids, name,yr, mth, day, arg, context=None): for self_data in self.browse(cr, uid, ids,yr, mth, day, context=context): i have pass both like this. which is correct? is the two lines are correct?

Don't change in code jsut Hijri2Gregorian(yr, mth, day) in this you need to pass yr, mth, day this value

i am really sorry .I did n't get your reply, I am not familier with python code.Please specify where should i add yr,mth,day

@Jack, i also tried your code. I got the same issue. How can i solve this? Please guide me. Waiting for your reply.

Sorry not able to solve.

class hr_extra(osv.osv): _inherit = "hr.employee"

def _get_hijr_date(self, cr, ud, ids, name, arg, context=None):
    res = {}
    for self_data in self.browse(cr, uid, ids, context=context):
        islamic_date = self_data.islamic_date

        # get Pass yesr, month and day in this function
        Hijri2Gregorian(yr, mth, day)
    return res

_columns = { 'islamic_date' : fields.char('Islamic Date'), 'english_date' : fields.function(Hijri2Gregorian,'English Date'),

       }

hr_extra() Can you please correct this code?

I have doubt on this code. I dont know python.

yes problem in _get_hijr_date in this method you need pass 3 parameter value Hijri2Gregorian(yr, mth, day) in this function. This three parameter value you get islamic_date from this variable you can get year,month and day and pass this in Hijri2Gregorian(yr, mth, day) your problem will solve

Can you edit your code plz? def _get_hijr_date(self, cr, ud, ids, name, arg, context=None): res = {} for self_data in self.browse(cr, uid, ids, context=context): islamic_date = self_data.islamic_date

    # get Pass yesr, month and day in this function
    Hijri2Gregorian(yr, mth, day)
return res. where should i change code?

class hr_extra(osv.osv): _inherit = "hr.employee"

def _get_hijr_date(self, cr, ud, ids, name, arg, context=None):
    res = {}
    for self_data in self.browse(cr, uid, ids, context=context):
        islamic_date = self_data.islamic_date

        # get Pass yesr, month and day in this function
        Hijri2Gregorian(yr, mth, day)
    return res

_columns = { 'islamic_date' : fields.char('Islamic Date'), 'english_date' : fields.function( _get_hijr_date, string='English Date', type='date', select=True), } hr_extra()

File "/opt/openerp/server/openerp/addons/date12/hr_date.py", line 48, in _get_hijri_date Hijri2Gregorian(yr, mth, day) NameError: global name 'yr' is not defined Error occured.

I really dont know where to define yr,mth,day globally. So that i cant solve the issue. where should i define yr,mth,day globally? def _get_hijr_date(self, cr, ud, ids, name, arg,yr,mth,day, context=None): or for self_data in self.browse(cr, uid, ids,yr,mth,day, context=context): this. Which is right?

try this def _get_hijr_date(self, cr, ud, ids, name, arg, context=None): res = {} for self_data in self.browse(cr, uid, ids, context=context): islamic_date = self_data.islamic_date

    # get Pass yesr, month and day in this function
    yr = islamic_date.year
    mth = islamic_date.month
    day = islamic_date.day

    Hijri2Gregorian(yr, mth, day)
return res

Did i want to create year,month,day in my py file? Where is these three values calling?

Now i got you jack. In Islamic date field, i'm giving like this : 1.3.1428(day.month.year). then i have to separate these values with the dot separator. I dont know what is next step?