Thursday 8 November 2018

Setting Custom Query in ERPNext

frappe.ui.form.on("Material Request", "onload", function(frm) {
    frm.set_query("item_code","items", function() {
        return {
            "query" : "custom_app.controllers.queries.custom_item_query",
            "filters": {
                "item_group": frm.doc.item_group
            }
        };
    });
});

Learn vanilla JavaScript in and out

Most of the web is built on JavaScript, whether we like it or not. It is becoming like JVM in that it has laying on top of it the technologies we all use such as frameworks, babel, webpack, etc., most of which aren't even written in vanilla JavaScript anymore; they're written in all sorts of transpiled/compiled languages such as TypeScript, and they produce a JavaScript "bytecode" (in reference to the JVM comparison). Moreover, we have things like Web Assembly on the horizon and that pushes further the notion of JavaScript being like a JVM.

Knowing JavaScript in-depth will be invaluable to you as a front end developer, even if you don't use it on a day to day basis.

That being said, if you want to get "current", I would recommend the things you already mentioned, as well as some other things. Bear in mind, the best advise would be something that is specifically catered towards a particular goal you want to achieve. This is just general advice:


  • Learn frameworks and be open minded to different approaches and understand their costs and benefits
  • Learn how to use build tools (Webpack, etc.) so you can automate your setup and build steps, as well as package them for others to use.
  • Learn to write tests to ensure your code is robust and predictable.
  • Learn TypeScript, ES6+, etc. These technologies are being heavily used nowadays. They provide assurances and productivity gains that vanilla JavaScript just can't keep up with.
  • Learn about CSS preprocessors like SASS. You'll thank me later.
  • Learn Node.js. The majority of JavaScript stuff is built on Node these days.
  • Learn how to use Chrome Dev Tools to help you debug your JavaScript/CSS. You can literally step through your code and see what's happening, view performance details, test network connection, and the list goes on. It's very insightful, saves a lot of time and is completely free to use of course.
  • Learn about the event loop in JavaScript and how it allows asynchronous code even though there is only a single thread that handles the event loop, which makes it a synchronous language.
  • Learn about the JavaScript runtime execution process. For instance, do you know how "scope" is handled and how it creates a lot of gotchas in the language?
  • Learn about security. Why can 'eval' be bad to use? How does one perform XSS attacks and also prevent them? How can you prevent SQL injection?

There are a lot more things we could cover, but you get the idea. You should understand all of the fundamentals and then some. This will make you highly demanded in the current playing field for as far as the eye can see.

CDROM error during sudo python install.py --develop

Edit /etc/apt/sources.list file and remove all lines having “cdrom”


ProgrammingError: (1146, "Table 'xxxxxxxxxxxxxxxxx.help' doesn't exist") while clicking Help > Documentation

Reason: Help Database not synchronized

Run the bench migrate command to sync the help database

bench.utils.CommandFailedError: git pull upstream develop

Reason: Pending git commits

==================================================================
Do the following if you haven't made local changes to files in the application:
go to folder /home/frappe/frappe-bench/apps/erpnext
and run the following command:

git reset --hard
git pull
go to folder /home/frappe/frappe-bench/apps/frappe
and run the following command:

git reset --hard
git pull
go to folder /home/frappe/.bench
and run the following command:

git reset --hard
git pull
Finally goto /home/frappe/frappe-bench
and run:

bench update

========================
Go to the directory, either frappe-bench/apps/erpnext or frappe-bench/apps/frappe
Type git status
Check the list of the untracked files. There might be files, which do or don't belong there.
Type git add --all to track those files
Type git stash
Run bench update
It should run successfully, if not, start over with 1. and repeat for the other directory

Create new Site in ERPNext

bench new-site <SITE NAME>
bench set-nginx-port <SITE NAME> <PORT NUMBER>
bench --site <SITE NAME> install-app erpnext
bench restart

MySQL going down due to insufficient memory

seems that our droplets doesnt have enogh memory some times and mysql cant allocate what it needs and crash, so we need to create a swap space to help this.
this is what I did:


On the command line
root@myserver:~# dd if=/dev/zero of=/swap.dat bs=1024 count=512k
root@myserver:~# mkswap /swap.dat
root@myserver:~# swapon /swap.dat
root@myserver:~# vim /etc/fstab
vim is started to edit, add the following to the end of /etc/fstab file
/swap.dat      none    swap    sw      0       0

Then, edit the mysql config file
root@myserver:~# vim /etc/mysql/my.cnf
And add this under [mysqld]
innodb_buffer_pool_size=64M

Finally reload mysql
root@myserver:~#  service mysqld reload

Switch to Developer mode in ERPNext

Edit sites/common-site-config.json
Insert a row"developer_mode": 1

Edit sites/<site name>/ site-config.json
Insert a row
"developer_mode": 1

Restart bench
=========================================

bench set-config developer_mode 1
clear cache

SSH from Linux

PuTTY is using different key format than the OpenSSH and OpenSSH is failing to report correctly the errors about unknown format in some versions.

If you want to use the PuTTY key in OpenSSH, you need to convert the private key to OpenSSH format in PuTTYGen (as described on SO):


AT PC:
Open PuttyGen
Click Load
Load your private key
Go to Conversions->Export OpenSSH and export your private key
Save it as ghouseam.ossh

THROUGH FTP:
Copy the above private key to /ppk

AT SERVER (ftp client):
ssh root@206.189.128.155 -i ghouseam-ossh

Remote access to MariaDB


Edit my config file (vi /etc/mysql/my.cnf) :

Mark off line “bind-address 127.0.0.x”

Insert new line below as “bind-address 0.0.0.0”



CREATE 12 USER ‘user_remote_R_only’@‘localhost’ IDENTIFIED BY ‘myStrongeSTpassword’;

CREATE USER ‘user_remote_R_only’@’%’ IDENTIFIED BY ‘myStrongeSTpassword’;

GRANT SELECT ON *.* TO ‘user_remote_R_only’@‘localhost’;

GRANT SELECT ON *.* TO ‘user_remote_R_only’@’%’;

flush privileges;



Restart the MYSQL Server sudo service mysql restart

Increase Swap size in Linux

sudo swapon -s
sudo swapoff /extraswap
sudo fallocate -l 4G /extraswap
sudo mkswap /extraswap
sudo swapon /extraswap
sudo swapon -s

Hide Dashboard in Documents in ERPNext

put this code in your custom script of that doctype
cur_frm.dashboard.frm.fields[0].df.collapsible=0

Create new DocType in ERPNext

In Doc Type, Create a new document type under any specific module

Edit ~/frappe-bench/apps/erpnext/erpnext/config/module.py


Under any specific Label (sub module), within "items", Insert

{
"type": "doctype",
"name": "doctype name",
},

Configure Static IP Address on Linux VM in VMware Player


If you run VMware Player, you would have 3 networking options for virtual machines running there: Bridged, NAT, Host-Only. In the latest 5.0.1, I also found a new one: LAN Segment. This blog has a nice explanation on these three settings if you want to get more details.

In most of cases, I use NAT for networking because the virtual machine can have Internet access which allows me to install additional software as needed. By default, VMware Player uses DHCP to dynamically assign IP address while using NAT. So you cannot guarantee to get same IP address after each rebooting.

You can set static IP address for a VM running on VMware Player with a little trick. On a Debian Linux machine like Ubuntu, you can edit a configuration file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ sudo vim /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5)
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
  address 192.168.47.200
  netmask 255.255.255.0
  broadcast 192.168.47.255
  gateway 192.168.47.2
dns-nameservers 192.168.47.2

After saving the file, you would like to restart the network so that the change take effect:

1
$ sudo service networking restart

In my case, the VMnet8 which is the network adapter for the NAT network has an IP address of 192.168.47.1. When I first modified the interfaces file, I used it as the gateway IP address and failed to ping outside the network.

To figure it out why, I reversed back to the DHCP configuration (change the ifacce line to “iface eth0 dhcp” and delete the lines afterwards). In the DHCP mode, typing command “route –n” will show working gateway IP address: 192.168.47.2.

By default, the gateway IP is configured to use X.X.X.2. This can be changed with Virtual Network Editor which is not included in VMware Player. I will show you how to hack it in next post.

ERPNext Backup

change to bench directory
erpnext@server:~$ cd frappe-bench/
erpnext@server:~/frappe-bench$

use command
erpnext@server:~/frappe-bench$ bench backup
or command
erpnext@server:~/frappe-bench$ bench backup-all-sites

your database backups will be in ~/frappe-bench/sites/[sitename]/private/backups/
check out for the most recent backup with current time, there may be backups there already.

Now take backups of files
erpnext@server:~/frappe-bench/sites/[sitename]/public$ tar cvf ~/[sitename]-files.tar files
2 Backup files:
1. Database backup
2. Files backup

Setup bench on other machine where you want to restore.
Extract files in appropriate site's sub directory ~/frappe-bench/sites/[sitename]/public
Restore database backup
erpnext@other-server:~/frappe-bench$ bench restore SQL_FILE_PATH

Create AWS EC2 Instance

Create EC2 Instance by choosing
AMI = “Ubuntu Server 16.04 LTS (HVM), SSD Volume Type - ami-059eeca93cf09eebd”
INSTANCE TYPE = t2.small instance
STORAGE SPACE = 50 GB
SECURITY GROUP = “default” (existing)

Create an Elastic IP
Associate to the above instance created
Click “default” security group in the instance


Stop and Start the instance
Configure Putty using Elastic IP
Putty as “ubuntu”
su to “root” using sudo su –
edit /root/.ssh/authorized_keys and remove initial lines upto ssh-rsa
Restart Instance

Compile Python File

python -m compileall <file_1>.py <file_n>.py

The compiled “.pyc” file will be available with suffixed name
Copy the file in the parent folder.
Rename it as required with “.pyc” extension

Run the file as usual with python command

Install ERPNext in Ubuntu

SSH into the machine
  • login as “root”
Prerequisites
  • sudo apt-get update -y
  • #sudo apt-get upgrade -y
  • sudo apt-get install python3-minimal -y
  • sudo apt-get install build-essential python3-setuptools -y
  • sudo apt-get install build-essential libssl-dev libffi-dev python3-dev
  • sudo apt-get install python3-pip
  • python3 -m pip install --upgrade pip
Adding Frappe User as sudoer and switching into it
  • sudo adduser erpuser
  • usermod -aG sudo erpuser
  • su - erpuser
  • #cd /home/erpuser/
Easyinstaller
System Login
  • login as administrator/admin

Install Python in a Desktop PC

Prepare a Desktop PC
  • Identify a Desktop PC (Intel I5, 4GB RAM)
  • ping the system having MS ACCESS Time Entry Device database in LAN
  • ping the ERPNext server (eg:ermass.com.sa)
Download & Install Python
Set Environment Variables
  • Set “PY_HOME” System Environment Variables as “D:\Python” in Control Panel

  • Edit “path” System Environment Variables in Control Panel and add following lines:








Install Packages
  • Go to D:\Python\Scripts folder
  • Run pip install pyodbc
  • Run pip install json
  • Run pip install requests
  • Run pip install pathlib