68 lines
2.1 KiB
Python
68 lines
2.1 KiB
Python
from flask import Blueprint, render_template, request, redirect, url_for
|
|
from extensions import db
|
|
from models.quote import Quote, QuoteItem
|
|
from services.totals import compute_quote_totals
|
|
|
|
bp = Blueprint("quotes", __name__, url_prefix="/quotes")
|
|
|
|
@bp.route("/")
|
|
def dashboard():
|
|
quotes = Quote.query.all()
|
|
return render_template("quotes/dashboard.html", quotes=quotes)
|
|
|
|
@bp.route("/new", methods=["GET","POST"])
|
|
@bp.route("/new", methods=["GET","POST"])
|
|
def new_quote():
|
|
from models.client import Client
|
|
|
|
if request.method == "POST":
|
|
q = Quote(
|
|
title=request.form["title"],
|
|
category=request.form["category"],
|
|
client_id=request.form["client_id"]
|
|
)
|
|
db.session.add(q)
|
|
db.session.commit()
|
|
return redirect(url_for("quotes.edit_quote", quote_id=q.id))
|
|
|
|
clients = Client.query.all()
|
|
return render_template("quotes/new.html", clients=clients)
|
|
|
|
|
|
@bp.route("/<int:quote_id>", methods=["GET", "POST"])
|
|
def edit_quote(quote_id):
|
|
q = Quote.query.get_or_404(quote_id)
|
|
|
|
if request.method == "POST":
|
|
q.title = request.form.get("title", q.title)
|
|
|
|
item_ids = request.form.getlist("item_id")
|
|
descriptions = request.form.getlist("description")
|
|
qtys = request.form.getlist("qty")
|
|
prices = request.form.getlist("unit_price")
|
|
|
|
from decimal import Decimal
|
|
|
|
for idx, item_id in enumerate(item_ids):
|
|
item = QuoteItem.query.get(int(item_id))
|
|
if not item or item.quote_id != q.id:
|
|
continue
|
|
|
|
item.description = descriptions[idx]
|
|
item.qty = Decimal(qtys[idx] or "0")
|
|
item.unit_price = Decimal(prices[idx] or "0")
|
|
|
|
compute_quote_totals(q)
|
|
db.session.commit()
|
|
return redirect(url_for("quotes.edit_quote", quote_id=q.id))
|
|
|
|
return render_template("quotes/edit.html", q=q)
|
|
|
|
|
|
@bp.route("/<int:quote_id>/add-item", methods=["POST"])
|
|
def add_item(quote_id):
|
|
item = QuoteItem(quote_id=quote_id, description="New Item", qty=1, unit_price=0)
|
|
db.session.add(item)
|
|
db.session.commit()
|
|
return redirect(url_for("quotes.edit_quote", quote_id=quote_id))
|