Esempio n. 1
0
def import_stock_report_dump(sourcefile):
    with open(sourcefile) as f:
        reader = csv.DictReader(f)

        for row in reader:
            site = Location.get_by_code(row['Site ID'])

            if not site:
                print(row)
                continue

            reporter = Connection.objects.get(identity=row['Mobile']).contact.worker
            created = utc.localize(datetime.utcfromtimestamp(int(row['Timestamp'])))
            logs = []
            for chunk in row['Items'].split(';'):
                bits = chunk.split()
                item = Item.get_by_code(bits[0])
                received = int(bits[1])
                holding = int(bits[2])
            
                logs.append(InventoryLog.objects.create(item=item,
                    last_quantity_received=received,
                    current_holding=holding))

            report = StockReport.objects.create(site=site, reporter=reporter,
                created=created)
            report.logs.add(*logs)

    print('Done')
Esempio n. 2
0
def import_stockout_dump(sourcefile):
    with open(sourcefile) as f:
        reader = csv.DictReader(f)

        for row in reader:
            site = Location.get_by_code(row['Site ID'])
            reporter = Connection.objects.get(identity=row['Mobile']).contact.worker
            created = utc.localize(datetime.utcfromtimestamp(int(row['Timestamp'])))
            modified = created

            stockout = StockOutReport.objects.create(site=site,
                reporter=reporter, created=created, modified=modified)

            items = []
            for item_code in row['Items'].split(','):
                items.append(Item.get_by_code(item_code.strip()))

            stockout.items.add(*items)

    print('Done')