# Buggy code
def create_receiver_list(self):
    rec, where_clause = "", ""
    if self.send_to == "All Customer Contact":
        where_clause = (
            self.customer
            and " and customer = '%s'" % self.customer.replace("'", "'")
            or " and ifnull(customer, '') != ''"
        )
    if self.send_to == "All Supplier Contact":
        where_clause = (
            self.supplier
            and " and supplier = '%s'" % self.supplier.replace("'", "'")
            or " and ifnull(supplier, '') != ''"
        )
    if self.send_to == "All Sales Partner Contact":
        where_clause = (
            self.sales_partner
            and " and sales_partner = '%s'" % self.sales_partner.replace("'", "'")
            or " and ifnull(sales_partner, '') != ''"
        )

    if self.send_to in [
        "All Contact",
        "All Customer Contact",
        "All Supplier Contact",
        "All Sales Partner Contact",
    ]:
        rec = frappe.db.sql(
            """select CONCAT(ifnull(first_name,''), ' ', ifnull(last_name,'')),
				mobile_no from `tabContact` where ifnull(mobile_no,'')!='' and
				docstatus != 2 %s"""
            % where_clause
        )

    elif self.send_to == "All Lead (Open)":
        rec = frappe.db.sql("""select lead_name, mobile_no from `tabLead` where
				ifnull(mobile_no,'')!='' and docstatus != 2 and status='Open'""")

    elif self.send_to == "All Employee (Active)":
        where_clause = (
            self.department
            and " and department = '%s'" % self.department.replace("'", "'")
            or ""
        )
        where_clause += (
            self.branch and " and branch = '%s'" % self.branch.replace("'", "'") or ""
        )

        rec = frappe.db.sql(
            """select employee_name, cell_number from
				`tabEmployee` where status = 'Active' and docstatus < 2 and
				ifnull(cell_number,'')!='' %s"""
            % where_clause
        )

    elif self.send_to == "All Sales Person":
        rec = frappe.db.sql("""select sales_person_name,
				tabEmployee.cell_number from `tabSales Person` left join tabEmployee
				on `tabSales Person`.employee = tabEmployee.name
				where ifnull(tabEmployee.cell_number,'')!=''""")

    rec_list = ""

    for d in rec:
        rec_list += d[0] + " - " + d[1] + "\n"
    self.receiver_list = rec_list


# Traceback (InternalError)
Traceback (most recent call last):
File "/home/bench/erpnext/apps/frappe/frappe/app.py", line 55, in application
response = frappe.handler.handle()
File "/home/bench/erpnext/apps/frappe/frappe/handler.py", line 20, in handle
data = execute_cmd(cmd)
File "/home/bench/erpnext/apps/frappe/frappe/handler.py", line 55, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File "/home/bench/erpnext/apps/frappe/frappe/__init__.py", line 1007, in call
return fn(*args, **newargs)
File "/home/bench/erpnext/apps/frappe/frappe/handler.py", line 83, in runserverobj
frappe.desk.form.run_method.runserverobj(method, docs=docs, dt=dt, dn=dn, arg=arg, args=args)
File "/home/bench/erpnext/apps/frappe/frappe/desk/form/run_method.py", line 36, in runserverobj
r = doc.run_method(method)
File "/home/bench/erpnext/apps/frappe/frappe/model/document.py", line 780, in run_method
out = Document.hook(fn)(self, *args, **kwargs)
File "/home/bench/erpnext/apps/frappe/frappe/model/document.py", line 1058, in composer
return composed(self, method, *args, **kwargs)
File "/home/bench/erpnext/apps/frappe/frappe/model/document.py", line 1041, in runner
add_to_return_value(self, fn(self, *args, **kwargs))
File "/home/bench/erpnext/apps/frappe/frappe/model/document.py", line 774, in
fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
File "/home/bench/erpnext/apps/erpnext/erpnext/selling/doctype/sms_center/sms_center.py", line 30, in create_receiver_list
docstatus != 2 %s""" % where_clause)
File "/home/bench/erpnext/apps/frappe/frappe/database/database.py", line 165, in sql
self._cursor.execute(query)
File "/home/bench/erpnext/env/local/lib/python2.7/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/home/bench/erpnext/env/local/lib/python2.7/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/home/bench/erpnext/env/local/lib/python2.7/site-packages/pymysql/connections.py", line 516, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/home/bench/erpnext/env/local/lib/python2.7/site-packages/pymysql/connections.py", line 727, in _read_query_result
result.read()
File "/home/bench/erpnext/env/local/lib/python2.7/site-packages/pymysql/connections.py", line 1066, in read
first_packet = self.connection._read_packet()
File "/home/bench/erpnext/env/local/lib/python2.7/site-packages/pymysql/connections.py", line 683, in _read_packet
packet.check_error()
File "/home/bench/erpnext/env/local/lib/python2.7/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/home/bench/erpnext/env/local/lib/python2.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
InternalError: (1054, u"Unknown column 'customer' in 'where clause'")

# Fixed code
def create_receiver_list(self):
    rec, where_clause = "", ""
    if self.send_to == "All Customer Contact":
        where_clause = " and dl.link_doctype = 'Customer'"
        if self.customer:
            where_clause += (
                " and dl.link_name = '%s'" % self.customer.replace("'", "'")
                or " and ifnull(dl.link_name, '') != ''"
            )
    if self.send_to == "All Supplier Contact":
        where_clause = " and dl.link_doctype = 'Supplier'"
        if self.supplier:
            where_clause += (
                " and dl.link_name = '%s'" % self.supplier.replace("'", "'")
                or " and ifnull(dl.link_name, '') != ''"
            )
    if self.send_to == "All Sales Partner Contact":
        where_clause = " and dl.link_doctype = 'Sales Partner'"
        if self.sales_partner:
            where_clause += (
                "and dl.link_name = '%s'" % self.sales_partner.replace("'", "'")
                or " and ifnull(dl.link_name, '') != ''"
            )
    if self.send_to in [
        "All Contact",
        "All Customer Contact",
        "All Supplier Contact",
        "All Sales Partner Contact",
    ]:
        rec = frappe.db.sql(
            """select CONCAT(ifnull(c.first_name,''), ' ', ifnull(c.last_name,'')),
				c.mobile_no from `tabContact` c, `tabDynamic Link` dl  where ifnull(c.mobile_no,'')!='' and
				c.docstatus != 2 and dl.parent = c.name%s"""
            % where_clause
        )

    elif self.send_to == "All Lead (Open)":
        rec = frappe.db.sql("""select lead_name, mobile_no from `tabLead` where
				ifnull(mobile_no,'')!='' and docstatus != 2 and status='Open'""")

    elif self.send_to == "All Employee (Active)":
        where_clause = (
            self.department
            and " and department = '%s'" % self.department.replace("'", "'")
            or ""
        )
        where_clause += (
            self.branch and " and branch = '%s'" % self.branch.replace("'", "'") or ""
        )

        rec = frappe.db.sql(
            """select employee_name, cell_number from
				`tabEmployee` where status = 'Active' and docstatus < 2 and
				ifnull(cell_number,'')!='' %s"""
            % where_clause
        )

    elif self.send_to == "All Sales Person":
        rec = frappe.db.sql("""select sales_person_name,
				tabEmployee.cell_number from `tabSales Person` left join tabEmployee
				on `tabSales Person`.employee = tabEmployee.name
				where ifnull(tabEmployee.cell_number,'')!=''""")

    rec_list = ""
    for d in rec:
        rec_list += d[0] + " - " + d[1] + "\n"
    self.receiver_list = rec_list