Sunday, 5 October 2014

OpenERP: one2one relational field example

one2one relational field is deprecated in OpenERP version>5 but you can achieve the same using many2one relational field. You can achieve it in following two ways :

1) using many2one field in both the objects (http://tutorialopenerp.wordpress.com/2014/04/23/one2one/)

2)  using inheritance by deligation

You can easily find the first solution with little search over internet so let's start with 2nd solution.

Scenario : 

I want to create a one2one relation between two objects of openerp

hr.employee

and

hr.employee.medical.details

What I should do 

i. Add _inherits section in hr_employee class
ii. Add field medical_detail_id in hr_employee class

class hr_employee(osv.osv):
    _name = 'hr.employee'
    _inherits = {'hr.employee.medical.details': "medical_detail_id"}
    _inherit = 'hr.employee'    
    _columns = {
             'emp_code':fields.char('Employee Code', size=6, readonly=True),
             'medical_detail_id':fields.many2one('hr.employee.medical.details', 'Medical       
             Details',help="Link this employee to it's medical details", ondelete="cascade", required=True),
             ............
    }

You don't have to do anything with 'hr.employee.medical.details' object

iii. Add fields in hr_employee_view.xml file

<group col="4" string="Medical Details">            
    <field name="medical_detail_id" invisible="1" required="0" colspan="4" />
    <field name="blood_group" />
    <field name="medicine" />
    <field name="medicine_desc" />
    <field name="allergies" />
    <field name="allergies_desc" />
</group>

and you are done. It will create exactly one record each employee entry in
hr.employee.medical.details table.

Please let me know if you are facing any issue to implement it.


Thanks!!!!!!!!!!!!!!!! Enjoy Programming :)

Saturday, 4 October 2014

OpenERP: Generate Employee Code

Employee code can be generated in following three simple steps:

1. Create a sequence file

emp_code_sequence_view.xml

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <!-- Sequences for employee code -->
        <record id="emp_code_sequence" model="ir.sequence.type">
            <field name="name">Employee Code</field>
            <field name="code">hr.employee</field>
        </record>

        <record id="seq_hr_employee" model="ir.sequence">
            <field name="name">Employee Code</field>
            <field name="code">hr.employee</field>
   <field name="prefix">EMP</field>
            <field name="padding">5</field>
            <field name="company_id" eval="False"/>
        </record>

    </data>
</openerp>


2. Add new field to class

class hr_employee(osv.osv):
    _name = 'hr.employee'
    _inherit = 'hr.employee'    
    _columns = {
                'emp_code':fields.char('Employee Code', size=8, readonly=True)
    }

3. Update database

def create(self, cr, uid, vals, context=None):
        vals['emp_code']=self.pool.get('ir.sequence').get(cr, uid,'hr.employee')
        res=super(hr_employee, self).create(cr, uid, vals)
        return res

create function will be called only when new employee is added.

and you are done.

Employee code will start from EMP00001

You can generate emp_code without sequence file as well and write code directly in create function as well. But standard way is using sequence file only.

Note: Don't forget to include 'emp_code_sequence_view.xml' file in data section of '__openerp__.py' file.


Thanks!!!!!!!!! Enjoy Programming :)


Sunday, 10 August 2014

OpenERP: Calculate difference between two dates

from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta

date_format = '%Y-%m-%d'

joining_date = '2013-08-23'
current_date = (datetime.today()).strftime(date_format)

d1 = datetime.strptime(joining_date, date_format).date()
d2 = datetime.strptime(current_date, date_format).date()
r = relativedelta(d2,d1)

print r.years
print r.months
print r.days


Thanks!!!!!!!!! Enjoy Programming :)

Saturday, 2 August 2014

File upload not working on PHP Server

Few days back, our team faced a new issue in file upload on server. It was restricting user to upload file size over 8KB while the following parameters were already set :

php_value file_uploads  On
php_value upload_max_filesize 200M
php_value memory_limit  512M
php_value post_max_size 200M
php_value max_execution_time 1000
php_value max_input_time 300

but still file uploading was not working.

Searched over internet and found that above problem was due to LimitRequestBody Directive of Apache. This parameter Restricts the total size of the HTTP request body sent from the client.

"The LimitRequestBody directive allows the user to set a limit on the allowed size of an HTTP request message body within the context in which the directive is given (server, per-directory, per-file or per-location). If the client request exceeds that limit, the server will return an error response instead of servicing the request. The size of a normal request message body will vary greatly depending on the nature of the resource and the methods allowed on that resource."

If, for example, you are permitting file upload to a particular location, and wish to limit the size of the uploaded file to 100K, you might use the following directive:

LimitRequestBody 102400

Please chekc the following link for full description :

http://httpd.apache.org/docs/2.0/mod/core.html#limitrequestbody


Thanks!!!!!!!! Enjoy Programming :)

Friday, 18 July 2014

OpenERP Common Errors

From last one month started working on OpenERP. While customizing code daily facing one or the other issue. So thought of keeping all of them at one place with solutions for future use and may be can help someone else as well :

1) AttributeError: 'NoneType' object has no attribute '_columns'

Solution : Problem is with your __openerp__.py file. Check depends attribute.

Reference Links :
http://stackoverflow.com/questions/10565288/attributeerror-nonetype-object-has-no-attribute-columns
http://forum.openerp.com/forum/topic24199.html

2) Openerp : new module is not showing into module list?

Solution : If you have developed a new module and it's in the addons folder but still not showing in the module list than follow following steps :

a) Restart OpenERP server by using command :

./openerp-server --addons-path=XXXX/XXXXXXX/addons/ (path of addons directory)

b) Go to the browser and open

localhost:8069 or 127.0.0.1:8069

c) Login into OpenERP; Go to Settings menu; Users; Select your user, edit it and check the check-box of 'Technical Features', save it and reload the web page.

d) Again Go to Settings, you will find two more options under Modules section

  • Update Modules List
  • Apply Scheduled Upgrades

Click on 'Update Modules list' option. Update modules.

e) Now search your module using 'Installed Modules' option.

That's it.

Reference Links :

3) Model XXX is declared but cannot be added/loaded

Solution : Problem is with xxx model entry in ir_model table.

a) Login to PostgeSQL database using psql or pgAdmin III

b) Run following commands :

Delete from ir_model_constraint where model=(Select id from ir_model where name='xxx');

Delete from ir_model where name='xxx';

Hopefully this will fix your problem.

Reference Links :
Many more to come.....