'set missing item values'

def set_missing_item_details(self, for_validate=False):

    from erpnext.stock.get_item_details import get_item_details
    from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
    if hasattr(self, u'items'):
        parent_dict = {}
        for fieldname in self.meta.get_valid_columns():
            parent_dict[fieldname] = self.get(fieldname)
        if (self.doctype in [u'Quotation', u'Sales    Order', u'Delivery    Note', u'Sales    Invoice']):
            document_type = u'{}    Item'.format(self.doctype)
            parent_dict.update({u'document_type': document_type})
        for item in self.get(u'items'):
            if item.get(u'item_code'):
                args = parent_dict.copy()
                args.update(item.as_dict())
                args[u'doctype'] = self.doctype
                args[u'name'] = self.name
                if (not args.get(u'transaction_date')):
                    args[u'transaction_date'] = args.get(u'posting_date')
                if self.get(u'is_subcontracted'):
                    args[u'is_subcontracted'] = self.is_subcontracted
                ret = get_item_details(args)
                for (fieldname, value) in ret.items():
                    if (item.meta.get_field(fieldname) and (value is not None)):
                        if ((item.get(fieldname) is None) or (fieldname in force_item_fields)):
                            item.set(fieldname, value)
                        elif ((fieldname in [u'cost_center', u'conversion_factor']) and (not item.get(fieldname))):
                            item.set(fieldname, value)
                        elif (fieldname == u'serial_no'):
                            stock_qty = ((item.get(u'stock_qty') * (-1)) if (item.get(u'stock_qty') < 0) else item.get(u'stock_qty'))
                            if (stock_qty != len(get_serial_nos(item.get(u'serial_no')))):
                                item.set(fieldname, value)
                        elif ((fieldname == u'conversion_factor') and (not item.get(u'conversion_factor'))):
                            item.set(fieldname, value)
                if ret.get(u'pricing_rule'):
                    item.set(u'discount_percentage', ret.get(u'discount_percentage'))
                    if (ret.get(u'pricing_rule_for') == u'Price'):
                        item.set(u'pricing_list_rate', ret.get(u'pricing_list_rate'))
                    if item.price_list_rate:
                        item.rate = flt((item.price_list_rate * (1.0 - (flt(item.discount_percentage) / 100.0))), item.precision(u'rate'))
        if (self.doctype == u'Purchase    Invoice'):
            self.set_expense_account(for_validate)
