Example #1
0
def import_data(filespec):
    db = Database('instance/database.sqlite').connect()
    category = Category(db)
    uom = Uom(db)
    item = Item(db)
    trx = Transaction(db)

    with open(filespec, newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            print('Name: {}, Category: {}'.format(row['item_name'],
                                                  row['category_name']))
            #import pdb;pdb.set_trace()
            #create a Category if not exists
            cats = category.select_one(
                where='name = "{}"'.format(row['category_name']))
            if not cats:
                cats = category.new()
                cats.name = row["category_name"]
                category.save(cats)
            cat_id = cats.id
            #create UOM if not exists
            uom_name = row['uom'].strip()
            if uom_name == '':
                uom_name = "Ea."

            uoms = uom.select_one(where='name = "{}"'.format(uom_name))
            if not uoms:
                uoms = uom.new()
                uoms.name = uom_name
                uom.save(uoms)

            items = item.select_one(
                where='name = "{}"'.format(row['item_name'], ))
            if not items:
                items = item.new()
                items.name = row['item_name']
                items.uom = uom_name
                items.cat_id = cat_id
                item.save(items)

            # Create a transaction record
            trxs = trx.new()
            trx.update(trxs, row)
            trxs.created = datetime.strptime(row['created'], '%Y-%m-%d')
            trxs.item_id = items.id
            trx.save(trxs)

        try:
            db.commit()
        except Exception as e:
            db.rollback()
            print(str(e))
Example #2
0
def edit(id=None):
    setExits()
    g.title = "Edit {} Record".format(g.title)

    item = Item(g.db)
    transactionList = None
    #import pdb;pdb.set_trace()
    if request.form:
        id = request.form.get('id', None)

    id = cleanRecordID(id)

    if id < 0:
        flash("Invalid Record ID")
        return redirect(g.listURL)

    categories = Category(g.db).select()
    uoms = Uom(g.db).select()

    if id >= 0 and not request.form:
        if id == 0:
            rec = item.new()  # allow creation of new properties
            item.save(rec)  # need an id for transactions
            g.db.commit()  # have to commit this to protect the ID we just got
            # This name changes behavure of the Cancel link in the edit form
            g.cancelURL = url_for('.cancel') + "{}/".format(rec.id)

        else:
            rec = item.get(id)

        if not rec:
            flash('Record not Found')
            return redirect(g.listURL)

    #import pdb;pdb.set_trace()

    if request.form:
        rec = item.get(id)
        if rec:
            item.update(rec, request.form)
            if validate_form():
                item.save(rec)
                try:
                    g.db.commit()
                    return redirect(g.listURL)

                except Exception as e:
                    g.db.rollback()
                    flash(
                        printException('Error attempting to save Item record',
                                       str(e)))
                    return redirect(g.listURL)
            else:
                pass  # There are imput errors

        else:
            flash('Record not Found')
            return redirect(g.listURL)

    transactionList = get_trx_list_for_item(rec.id)
    transferList = get_transfer_list_for_item(rec.id)
    qoh_list = get_qoh_by_warehouse(rec.id)
    on_hand = item.stock_on_hand(id)

    return render_template('item_edit.html',
                           rec=rec,
                           categories=categories,
                           uoms=uoms,
                           transactionList=transactionList,
                           transferList=transferList,
                           qoh_list=qoh_list,
                           on_hand=on_hand)