This question has been flagged
1 Reply
4363 Views

I created two text fields.

Islamic & English fields.

When i enter hijri date in Islamic field. Automatically convert into english and display it in English field.

I got a code from the forum, but it throws error. Below is my code.

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, uid, 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()

Error: TypeError: Hijri2Gregorian() takes exactly 3 arguments (7 given)

Updated Code(24/4/2014) 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
          islamic_date = '%.2f' % islamic_date
          list = str(islamic_date).split('.')
          yr = islamic_date(int(list[0]))
          mth = islamic_date(int(list[1]))
          day = islamic_date(int(list[2]))
          # get Pass yesr, month and day in this function
          #yr = 1428
          #mth = 1
          #day = 2
          Hijri2Gregorian(yr,mth,day)
      return res

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

hr_extra()

Avatar
Discard
Best Answer

Hi,

This code may help you

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, uid, ids, name, arg,context=None):
      res = {}
      for self_data in self.browse(cr, uid, ids, context=context):
          islamic_date = self_data.islamic_date
          if  islamic_date != False or  islamic_date !='':
              # get Pass yesr, month and day in this function
              yr = islamic_date.year
              mth = islamic_date.mth
              day = islamic_date.day
             Hijri2Gregorian(yr,mth,day)
      return res

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

            }

hr_extra()
Avatar
Discard
Author

Can you please check my updated code? Is this right?

update ans check it.

Author

File "/opt/openerp/server/openerp/addons/date_check/hr_date.py", line 56, in _get_hijr_date yr = islamic_date.year AttributeError: 'bool' object has no attribute 'year' error

add value in this field islamic_date

Author

Still the same above issue.

Author

islamic_date date is a single field. so we want to split the date into year, month,day. But we didnt write that code. I dont know how to split the date.