Example #1
0
def _all_data():
    trans = db_session.query(Transaksi).all()
    as_list = [
        [ tr.id, tr.produksi, tr.penjualan, tr.persediaan, tr.permintaan ]
        for tr in trans
    ]
    return pd.DataFrame(as_list)
Example #2
0
def _all_rules():
    rules = db_session.query(Rule).all()
    as_list = [
        [r.produksi, r.penjualan, r.persediaan, r.permintaan]
        for r in rules
    ]
    return pd.DataFrame(as_list)
Example #3
0
def find_one_transaksi(id):
    result = db_session.query(Transaksi).filter(Transaksi.id == id).first()
    if result is None:
        return make_response(f"Can't find data with id {id}", 404)
    as_dict = result.to_dict()
    print("---dict---")
    print(as_dict)
    return jsonify(as_dict)
Example #4
0
def rule_update(id):
    rule = db_session.query(Rule).filter(Rule.id == id).first()

    if rule is None:
        return make_response(f"Can't find data with id {id}", 404)

    content = request.json
    rule = Rule.fromdict(content)
    db_session.add(rule)
    db_session.commit()
    return jsonify(rule.to_dict())
Example #5
0
def update_transaksi(id):
    transaksi = db_session.query(Transaksi).filter(Transaksi.id == id).first()

    content = request.json
    content["tanggal"] = datetime.datetime.strptime(content['tanggal'],
                                                    '%Y-%m-%d')

    transaksi.update(**content)
    db_session.commit()

    return jsonify(transaksi.to_dict())
Example #6
0
def create_transaksi():
    content = request.json

    # Get last transaksi by date
    last_trans = db_session.query(Transaksi).order_by(
        Transaksi.tanggal.desc()).first()
    # Inc by 1 day
    content['tanggal'] = last_trans.tanggal + datetime.timedelta(days=1)
    # content["tanggal"] = datetime.datetime.strptime(content['tanggal'], '%Y-%m-%d')

    n_same_date = db_session.query(Transaksi).filter(
        Transaksi.tanggal == content["tanggal"]).count()
    if n_same_date > 0:
        return make_response("Duplicate date", 500)

    transaksi = Transaksi.fromdict(content)
    db_session.add(transaksi)
    db_session.commit()

    return jsonify(transaksi.to_dict())
Example #7
0
def rule_find():
    result = db_session.query(Rule).all()
    rule_series = pd.Series([r.permintaan for r in result])
    rule_counts = rule_series.value_counts()

    total_rules = len(result)
    _0 = int(rule_counts[0])
    _1 = int(rule_counts[1])
    _2 = int(rule_counts[2])

    counts = [total_rules, _0, _1, _2]
    items = [r.to_dict() for r in result]

    result = {"counts": counts, "items": items}

    return jsonify(result)
Example #8
0
def rule_delete(id):
    db_session.query(Rule).filter(Rule.id == id).delete()
    db_session.commit()
    return "OK"
Example #9
0
def rule_find_one(id):
    db_session = g.get("db_session")
    result = db_session.query(Rule).filter(Rule.id == id).first()
    if result is None:
        return make_response(f"Can't find data with id {id}", 404)
    return jsonify(result.to_dict())
Example #10
0
def list_transaksi():
    trans = db_session.query(Transaksi).order_by(
        Transaksi.tanggal.desc()).all()
    result = [r.to_dict() for r in trans]
    return jsonify(result)
Example #11
0
def last_transaksi():
    last_transaksi = db_session.query(Transaksi).order_by(
        Transaksi.tanggal.desc()).first()
    return jsonify(last_transaksi.to_dict())
Example #12
0
def delete_transaksi(id):
    db_session.query(Transaksi).filter(Transaksi.id == id).delete()
    db_session.commit()
    return "OK"