Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

Islamic date in a textbox, it should automatically convert into english date &show another textbox. Python code shows error(No s

By
Remya
on 4/22/14, 5:00 AM 2,050 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>

0

Jagdish Panchal

--Jagdish Panchal--
2674
| 5 3 6
Gandhinagar, India
--Jagdish Panchal--

OpenERP Developer

Jagdish Panchal
On 4/22/14, 5:42 AM

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

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

Remya
on 4/22/14, 6:09 AM

you can use.

Jagdish Panchal
on 4/22/14, 6:40 AM

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

Remya
on 4/22/14, 7:52 AM

Sorry i don't know

Jagdish Panchal
on 4/22/14, 8:19 AM

I have update code check it

Jagdish Panchal
on 4/22/14, 8:55 AM

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

Remya
on 4/22/14, 10:20 AM

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

Remya
on 4/22/14, 11:20 AM

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

Remya
on 4/22/14, 11:45 AM

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

charlesraj
on 4/23/14, 2:04 AM

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

Jagdish Panchal
on 4/23/14, 2:12 AM

_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?

charlesraj
on 4/23/14, 2:24 AM

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

Jagdish Panchal
on 4/23/14, 2:45 AM

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

charlesraj
on 4/23/14, 4:36 AM

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

rosey
on 4/23/14, 6:33 AM

Sorry not able to solve.

Jagdish Panchal
on 4/23/14, 6:49 AM

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?

rosey
on 4/23/14, 7:06 AM

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

rosey
on 4/23/14, 7:08 AM

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

Jagdish Panchal
on 4/23/14, 7:56 AM

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?
rosey
on 4/23/14, 8:04 AM

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

Jagdish Panchal
on 4/23/14, 8:10 AM

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.

rosey
on 4/23/14, 8:55 AM

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?

rosey
on 4/23/14, 9:29 AM

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
Jagdish Panchal
on 4/23/14, 9:48 AM

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

rosey
on 4/23/14, 9:57 AM

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?

rosey
on 4/23/14, 10:13 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 4/22/14, 5:00 AM
Seen: 2050 times
Last updated: 3/16/15, 8:10 AM