11-Flask

Flask(Part.69)| 【割引機能の利用(一括適用)】

python| まとめ | 現役エンジニア&プログラミングスクール講師「python」のまとめページです。pythonに関して抑えておきたい知識や文法やにについて記事をまとめています。まとめページの下部には「おすすめの学習書籍」「おすすめのITスクール情報」「おすすめ求人サイト」について情報を掲載中...

目標

  • 会計に対して割引機能を実装する

割引機能

ここでは割引を商品ごとでなく一括して適用する機能を実装します。作業は前回同様、feature-discountブランチで行います。

apps/registerapp/app.py

apps/registerapp/app.pyに次の内容を追加します。

ファイルの末尾

@register_bp.route("/bill/<int:bill_id>/apply_bulk_discount", methods=["POST"])
@login_required
def apply_bulk_discount(bill_id):
    discount_id = request.form.get("discount_id")
    if not discount_id:
        flash("割引が選択されていません。", "warning")
        return redirect(url_for("register.view_bill", bill_id=bill_id))

    discount = Discount.query.get(discount_id)
    if not discount:
        flash("割引が見つかりません。", "danger")
        return redirect(url_for("register.view_bill", bill_id=bill_id))

    orders = Order.query.filter_by(bill_id=bill_id).all()
    for order in orders:
        order.discount_id = discount.id
        order.discount_number = discount.number

    db.session.commit()
    flash("すべての注文に割引を適用しました。", "success")
    return redirect(url_for("register.view_bill", bill_id=bill_id))

apps/registerapp/templates/register_bill.html

apps/registerapp/templates/register_bill.htmlに次の内容を追加します。

<!-- 一括割引フォーム -->
{% if bill.status != 'paid' %}
<form action="{{ url_for('register.apply_bulk_discount', bill_id=bill.id) }}" method="post" style="margin-bottom: 1em;">
    <label for="bulk_discount_id">一括割引:</label>
    <select name="discount_id" id="bulk_discount_id" class="custom-select">
        {% if discount_list %}
        {% for discount in discount_list %}
        <option value="{{ discount.id }}">{{ discount.number }}%{% if discount.priority %}(優先){% endif %}</option>
        {% endfor %}
        {% endif %}
    </select>
    <button type="submit" class="update-button">適用</button>
</form>

次は「register_bill.html」の全体です。ここでは、Billモデルの status が「paid」の場合に商品個数の変更、割引率、消費税率の変更が出来ないようにも修正しています。

この続きはNoteとなります。

今回は以上になります。

「python」おすすめ書籍 ベスト3 | 現役エンジニア&プログラミングスクール講師「python」の学習でお勧めしたい書籍をご紹介しています。お勧めする理由としては、考え方、イメージなどを適切に捉えていること、「生のpython」に焦点をあてて解説をしている書籍であることなどが理由です。勿論、この他にも良い書籍はありますが、特に質の高かったものを選んで記事にしています。ページの下部には「おすすめのITスクール情報」「おすすめ求人サイト」について情報を掲載中。...

ブックマークのすすめ

「ほわほわぶろぐ」を常に検索するのが面倒だという方はブックマークをお勧めします。ブックマークの設定は別記事にて掲載しています。

「お気に入り」の登録・削除方法【Google Chrome / Microsoft Edge】「お気に入り」の登録・削除方法【Google Chrome / Microsoft Edge】について解説している記事です。削除方法も掲載しています。...
【パソコン選び】失敗しないための重要ポイント | 現役エンジニア&プログラミングスクール講師【パソコン選び】失敗しないための重要ポイントについての記事です。パソコンのタイプと購入時に検討すべき点・家電量販店で見かけるCPUの見方・購入者が必要とするメモリ容量・HDDとSSDについて・ディスプレイの種類・バッテリーの持ち時間や保証・Officeソフト・ウィルス対策ソフトについて書いています。...

RELATED POST
11-Flask

Flask(Part.64)| 【QRコードアプリケーションのカスタマイズ(1)】

2025年4月26日
プログラミング学習 おすすめ書籍情報発信 パソコン初心者 エンジニア希望者 新人エンジニア IT業界への就職・転職希望者 サポートサイト Programming learning Recommended schools Recommended books Information dissemination Computer beginners Prospective engineers New engineers Prospective job seekers in the IT industry Support site
11-Flask

Flask(Part.39)| 【レジ管理アプリケーションのプログラミング(1)】

2025年4月1日
プログラミング学習 おすすめ書籍情報発信 パソコン初心者 エンジニア希望者 新人エンジニア IT業界への就職・転職希望者 サポートサイト Programming learning Recommended schools Recommended books Information dissemination Computer beginners Prospective engineers New engineers Prospective job seekers in the IT industry Support site
11-Flask

Flask(Part.23)| 【テーブル管理アプリケーションのプログラミング】

2025年3月16日
プログラミング学習 おすすめ書籍情報発信 パソコン初心者 エンジニア希望者 新人エンジニア IT業界への就職・転職希望者 サポートサイト Programming learning Recommended schools Recommended books Information dissemination Computer beginners Prospective engineers New engineers Prospective job seekers in the IT industry Support site