Example #1
0
    def next_location(self):

        # Get the latest box based on its id
        box = Box.objects.filter(account=self.id).latest('id')

        # Num of empty locations given by empty accession fields
        empty_locations = BoxItem.objects.filter(used=False, box=box.id).count()

        # If box is full, create a new one
        if not empty_locations:
            latest_box = Box.objects.filter(account=self.id).latest('id')
            box = Box()
            box.account = self
            box.number = int(latest_box.number) + 1
            box.save()

            next_location = BoxItem.objects.get(box=box.id, slot='A1')

            return int(next_location.id)

        # Get the highest location value as an integer
        last_location_id = BoxItem.objects.filter(box=box.id).latest('id')
        last_location_id = int(last_location_id.id)

        # Next location_id value is last_location - empty_locations + 1
        next_location = last_location_id - empty_locations + 1

        return next_location