'Pull sales orders  which are pending to deliver based on criteria selected'

def get_open_sales_orders(self):

    so_filter = item_filter = u''
    if self.from_date:
        so_filter += u'    and    so.transaction_date    >=    %(from_date)s'
    if self.to_date:
        so_filter += u'    and    so.transaction_date    <=    %(to_date)s'
    if self.customer:
        so_filter += u'    and    so.customer    =    %(customer)s'
    if self.project:
        so_filter += u'    and    so.project    =    %(project)s'
    if self.fg_item:
        item_filter += u'    and    so_item.item_code    =    %(item)s'
    open_so = frappe.db.sql(u'\n DCTB  DCTB  DCTB select    distinct    so.name,    so.transaction_date,    so.customer,    so.base_grand_total\n DCTB  DCTB  DCTB from    `tabSales    Order`    so,    `tabSales    Order    Item`    so_item\n DCTB  DCTB  DCTB where    so_item.parent    =    so.name\n DCTB  DCTB  DCTB  DCTB and    so.docstatus    =    1    and    so.status    not    in    ("Stopped",    "Closed")\n DCTB  DCTB  DCTB  DCTB and    so.company    =    %(company)s\n DCTB  DCTB  DCTB  DCTB and    so_item.qty    >    so_item.delivered_qty    {0}    {1}\n DCTB  DCTB  DCTB  DCTB and    (exists    (select    name    from    `tabBOM`    bom    where    bom.item=so_item.item_code\n DCTB  DCTB  DCTB  DCTB  DCTB  DCTB and    bom.is_active    =    1)\n DCTB  DCTB  DCTB  DCTB  DCTB or    exists    (select    name    from    `tabPacked    Item`    pi\n DCTB  DCTB  DCTB  DCTB  DCTB  DCTB where    pi.parent    =    so.name    and    pi.parent_item    =    so_item.item_code\n DCTB  DCTB  DCTB  DCTB  DCTB  DCTB  DCTB and    exists    (select    name    from    `tabBOM`    bom    where    bom.item=pi.item_code\n DCTB  DCTB  DCTB  DCTB  DCTB  DCTB  DCTB  DCTB and    bom.is_active    =    1)))\n DCTB  DCTB  DCTB '.format(so_filter, item_filter), {u'from_date': self.from_date, u'to_date': self.to_date, u'customer': self.customer, u'project': self.project, u'item': self.fg_item, u'company': self.company}, as_dict=1)
    self.add_so_in_table(open_so)
