Saturday, 5 September 2015

Js/jQuery load alternative images synchronously?

You can find lot of links over Internet, how to load alternative images but most of them give examples of asynchronously loading not synchronously one. I was facing the same problem and it took around 4 hours for me to fix it and now it looks simple :)

Scenario 1(simple):
You have two images and want to show the 2nd one, in case 1st one is not valid.

Use following image tag:

<img src="'+src+'" onError="this.onerror=null;this.src=\'\'" />; 

Note: Please don't forget to add this.onerror=null otherwise browser may stuck in a loop.

Scenario 1(typical):
You have three images:

1. Show image1 if valid otherwise image2.
2. If image2 is also invalid show 3rd one.

In this case we have to use above code as well as one more function for middle image.

Not working
You can use following code in JS and looks like it will work but will not

var src = first_image_src
var image = new Image();
image.src = src;
if (image.complete) {  

src = second_image_src; 

<img src="'+src+'" onError="this.onerror=null;this.src=\'+third_image_sorce+'" />;

It will always show you first(if valid) or 3rd image(if 1st one not valid), not second one although valid one.

Reason: Above JS code makes an ajax GET asynchronous request to check whether the image is valid or not. It will not wait till the response comes due to it's single threaded feature.

Working solution
To solve above problem we have to either make an synchronous request or use asynchronous in such a way the it looks like request is synchronous one. 

Here is the code:

function getSecondImage(element, second_image_src){
    var image = new Image();
    image.src = second_image_src;
    image.onerror = function(){$(element).prop('onError',null)};  
    image.onload = function(){$(element).prop('src',second_image_src)};      

var img_tag = '<img src="'+src+'" onError="this.onerror=getSecondImage(this,\''+second_image_src+'\');this.src=\'+third_image_sorce+'" />';

1. It will first check whether 1st image is proper if it's fine, 1st image will be loaded.
2. If 1st image is not valid, getSecondImage method will be called.
3. getSecondImage method will check if second image is valid or not. If it's valid it will update src of the image and that image will be loaded otherwise returns null.

4. If getSecondImage returns null then third image will be shown.

That's it.

If need any mode clarification or facing issue with above code please come back to me with your valuable comments.

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

Thursday, 27 August 2015

Laravel Unique Validation

Hi, You can find most of the things on the following link regarding unique validation in Laravel.

But I would like to add one more case to make it little easy to understand:

articleName => unique:articles,article_name,{isset(article_id) : article_id : NULL },article_id,user_id,{user_id}

The query that this will give you is: 

Select count(*) as aggregate from articles where article_name = <given_article_name> and article_id <> {article_id} and user_id={user_id}

It will check unique article name per user. Multiple users can have same article name but one user can't have same article name again, only once.  

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


Tuesday, 4 August 2015

Implementation of free3of9 bar-code in Odoo/OpenERP

Following are the steps to implement free3of9 bar-code in Odoo/OpenERP.

1. Install fonts(.ttf) files on your server.

a. You can download free3of9 bar-code files from here:

b. Extract/unzip folder and copy extracted 'free3of9' folder in '/usr/share/fonts/' folder only with .ttf files.

2. Odoo use two codes for 3of9 bar-code. 

Standard39 for Normal 3of9 and

Extended39 for Extended 3of9 or New 3of9

3. In Odoo you can use it as follow based on your .rml or .xml file

a. For .rml file:

<barCode code="Standard39" fontName="Times-Roman" fontSize="30" alignment="CENTER"> [[ or '' ]] </barCode>

b. For .xml file:

<img t-att-src="'/report/barcode/?type=%s&amp;value=%s&amp;width=%s&amp;height=%s' % ('Standard39',, 600, 100)" style="width:300px;height:50px;"/>

If still it's not working, restart your server. 

Note: Follow first two steps as root user only.

Hopefully it will help someone. Kindly post your valuable comments, if any improvement is needed.

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

Monday, 3 August 2015

Odoo/OpenERP: Barcodes supported by Odoo

Following is the list/matrix of most popular bar-codes supported by Odoo.

1D Product
1D industrial
Code Name
Bar-code Type
Odoo Code
Code Name
Bar-code Type
Odoo Code
Code Name
Bar-code Type
Odoo Code
Code 39
QR Code
Code 39 (extended)

Code 93

Code 93 (extended)

Code 128


Code 11

Kindly let me know if I missed something.

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

Sunday, 2 August 2015

Odoo/OpenERP: Picking List printing blank barcode?

Are you facing a problem with bar-code in picking list. Is it displaying nothing?

Reason is: Odoo uses reportlab to print barcode images and reason of showing blank barcode means reportlab fonts are missing.

Follow following steps to install reportlab fonts:

1. Login to your server as root user.

2. Open terminal and go to location: '/usr/lib/python2.7/dist-packages/reportlab/'

Check if 'fonts' directory is there? If not, create it.

3. Go to 'fonts' folder and Download Zip file from here:

i.e $ wget

extract it and delete 

4. Restart your server.

That's it.

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

Reference Links: