Search code examples
pythonmoduleodoo

Odoo set condition OR


in a custom module I have this situation:

@api.onchange('barcode')
    def barcode_scanning(self):
        """Barcode decode."""
        if self.barcode:
            scan_barcode = self.barcode
            barcode = scan_barcode
            qty_position = scan_barcode.find("'")
            price_position = scan_barcode.find('/')

            if price_position > 0:
                price = scan_barcode[:price_position].replace(',','.')
                barcode = scan_barcode[price_position + 1:]
            else:
                price = 0

            if qty_position > 0:
                qty = scan_barcode[price_position + 1:qty_position].replace(',','.')
                barcode = scan_barcode[qty_position + 1:]
            else:
                if float(price) > 0:
                    qty = 0
                else:
                    qty = 1

I need: qty_position = scan_barcode.find("'") command find "'" OR "". Can anyone help me, and write me code modified, for find "'" or "". I'm not a developer but I user, I don't know how edit this code. Thanks


Solution

  • To keep is simple, I have added an if statement which check if previous find function found something that is qty_position = scan_barcode.find("'") if it did not find it then find if this exist qty_position = scan_barcode.find("") Please see below the code. if this helps.

        def barcode_scanning(self):
            """Barcode decode."""
            if self.barcode:
                scan_barcode = self.barcode
                barcode = scan_barcode
                qty_position = scan_barcode.find("'")
                if qty_position < 0:
                    qty_position = scan_barcode.find("")
                price_position = scan_barcode.find('/')
    
                if price_position > 0:
                    price = scan_barcode[:price_position].replace(',','.')
                    barcode = scan_barcode[price_position + 1:]
                else:
                    price = 0
    
                if qty_position > 0:
                    qty = scan_barcode[price_position + 1:qty_position].replace(',','.')
                    barcode = scan_barcode[qty_position + 1:]
                else:
                    if float(price) > 0:
                        qty = 0
                    else:
                        qty = 1