'perform various (sometimes conditional) validations on warehouse'

def validate_warehouse(self):

    source_mandatory = [u'Material    Issue', u'Material    Transfer', u'Subcontract', u'Material    Transfer    for    Manufacture']
    target_mandatory = [u'Material    Receipt', u'Material    Transfer', u'Subcontract', u'Material    Transfer    for    Manufacture']
    validate_for_manufacture_repack = any([d.bom_no for d in self.get(u'items')])
    if ((self.purpose in source_mandatory) and (self.purpose not in target_mandatory)):
        self.to_warehouse = None
        for d in self.get(u'items'):
            d.t_warehouse = None
    elif ((self.purpose in target_mandatory) and (self.purpose not in source_mandatory)):
        self.from_warehouse = None
        for d in self.get(u'items'):
            d.s_warehouse = None
    for d in self.get(u'items'):
        if ((not d.s_warehouse) and (not d.t_warehouse)):
            d.s_warehouse = self.from_warehouse
            d.t_warehouse = self.to_warehouse
        if (not (d.s_warehouse or d.t_warehouse)):
            frappe.throw(_(u'Atleast    one    warehouse    is    mandatory'))
        if ((self.purpose in source_mandatory) and (not d.s_warehouse)):
            if self.from_warehouse:
                d.s_warehouse = self.from_warehouse
            else:
                frappe.throw(_(u'Source    warehouse    is    mandatory    for    row    {0}').format(d.idx))
        if ((self.purpose in target_mandatory) and (not d.t_warehouse)):
            if self.to_warehouse:
                d.t_warehouse = self.to_warehouse
            else:
                frappe.throw(_(u'Target    warehouse    is    mandatory    for    row    {0}').format(d.idx))
        if (self.purpose in [u'Manufacture', u'Repack']):
            if validate_for_manufacture_repack:
                if d.bom_no:
                    d.s_warehouse = None
                    if (not d.t_warehouse):
                        frappe.throw(_(u'Target    warehouse    is    mandatory    for    row    {0}').format(d.idx))
                    elif (self.pro_doc and ((cstr(d.t_warehouse) != self.pro_doc.fg_warehouse) and (cstr(d.t_warehouse) != self.pro_doc.scrap_warehouse))):
                        frappe.throw(_(u'Target    warehouse    in    row    {0}    must    be    same    as    Production    Order').format(d.idx))
                else:
                    d.t_warehouse = None
                    if (not d.s_warehouse):
                        frappe.throw(_(u'Source    warehouse    is    mandatory    for    row    {0}').format(d.idx))
        if ((cstr(d.s_warehouse) == cstr(d.t_warehouse)) and (not (self.purpose == u'Material    Transfer    for    Manufacture'))):
            frappe.throw(_(u'Source    and    target    warehouse    cannot    be    same    for    row    {0}').format(d.idx))
