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

目標

返金処理機能を実装する

返金処理機能

返金処理機能

ここでは、お会計後に、ご返金対応が必要な場合に備えて返金処理機能を実装します。返金処理は各商品ごとに個数を指定して処理できるように実装します。ここでは、前回と同様に、feature-receiptブランチで作業を行います。

必要なファルダとファイルを準備する

返金処理ではregisterアプリケーションやordersappアプリケーションに準備しているmodels.pyファイルにも編集が必要ですが、まずは、返金処理の基となるロジックと表示を行う新規のファイルを作成します。(※アプリケーションのフォルダ名は「refundapp」とします。)

  • apps/refundapp/static/css/style.css
  • apps/refundapp/templates/refund_base.html
  • apps/refundapp/templates/refund.html
  • apps/refundapp/templates/refunded.html
  • apps/refundapp/app.py

apps/refundapp/templates/refund_base.html

apps/refundapp/templates/refund_base.htmlを作成して次のように入力します。

<!DOCTYPE html>
<html lang="ja">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>
        {% block title %}
        {% endblock %}
    </title>
    <link rel="stylesheet" href="{{ url_for('refund.static', filename='css/style.css') }}">
</head>

<body>

    <header>
        {% block header %}
        <!-- 個別ページのヘッダーがここに入る -->
        {% endblock %}
    </header>

    <main>
        {% block content %}
        <!-- 個別ページのコンテンツがここに入る -->
        {% endblock %}
    </main>

    <footer>
        <p>© 2025 howahowa store</p>
    </footer>
</body>

</html>

apps/refundapp/templates/refund.html

apps/refundapp/templates/refund.htmlを作成して次のように入力します。

{% extends 'refund_base.html' %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<h1>伝票詳細</h1>
<p>伝票番号: {{ bill.id }}</p>
<p>テーブル番号: {{ bill.table_id }}</p>
<p>作成日時: {{ bill.get_created_at_jst() if bill.get_created_at_jst() else '' }}</p>
<p style="font-size:xx-large;">合計金額: {{ total_price | int }}</p>
<p style="font-size:xx-large;">返金金額: {{ refunded_total | int }}</p>
<p style="font-size:xx-large;">最終金額: {{ (total_price - refunded_total) | int }}</p>
<h2>注文内容</h2>
<!-- 一括割引フォーム -->
<table>
    <thead>
        <tr>
            <th>商品名</th>
            <th>数量</th>
            <th>税抜き金額</th>
            <th>割引率</th>
            <th>税抜き金額(割引後)</th>
            <th>消費税率</th>
            <th>消費税額</th>
            <th>返金する個数</th>
        </tr>
    </thead>
    <tbody>
        {% for order in orders %}
        <tr>
            <td>{{ order.product_name }}</td>
            <td>
                {% if bill.status != 'paid' %}
                <form action="{{ url_for('register.update_order', bill_id=bill.id, order_id=order.id) }}" method="post">
                    <input type="number" name="quantity" value="{{ order.quantity }}" min="1">
                    <button type="submit" class="update-button">更新</button>
                </form>
                {% else %}
                {{ order.quantity }}
                {% endif %}
            </td>
            <td>{{ order.total | int }}</td>
            <!-- 割引率 -->
            <td>
                {% if bill.status != 'paid' %}
                <form action="{{ url_for('register.update_order_discount', bill_id=bill.id, order_id=order.id) }}"
                    method="post">
                    <select name="discount_id" class="custom-select" onchange="this.form.submit()">
                        {% if discount_list %}
                        {% for discount in discount_list if discount.priority %}
                        <option value="{{ discount.id }}" {% if discount.number==order.discount_number %}selected{%
                            endif %}>
                            {{ discount.number }}%(優先)
                        </option>
                        {% endfor %}
                        {% for discount in discount_list if not discount.priority %}
                        <option value="{{ discount.id }}" {% if discount.number==order.discount_number %}selected{%
                            endif %}>
                            {{ discount.number }}%
                        </option>
                        {% endfor %}
                        {% endif %}
                    </select>
                </form>
                {% else %}
                {{ order.discount_number }}%
                {% endif %}
            </td>
            <td>{{(order.total * (100 - order.discount_number) / 100) | int }}</td>
            <!-- 消費税率 -->
            <td>
                {% if bill.status != 'paid' %}
                <form action="{{ url_for('register.update_order_vat', bill_id=bill.id, order_id=order.id) }}"
                    method="post">
                    <select name="vat_id" class="custom-select" onchange="this.form.submit()">
                        {% if vat_list %}
                        {% for vat in vat_list if vat.priority %}
                        <option value="{{ vat.id }}" {% if vat.number==order.vat_number %}selected{% endif %}>
                            {{ vat.number }}%(優先)
                        </option>
                        {% endfor %}
                        {% for vat in vat_list if not vat.priority %}
                        <option value="{{ vat.id }}" {% if vat.number==order.vat_number %}selected{% endif %}>
                            {{ vat.number }}%
                        </option>
                        {% endfor %}
                        {% endif %}
                    </select>
                </form>
                {% else %}
                {{ order.vat_number }}%
                {% endif %}
            </td>
            <td>{{ ((order.total * (100 - order.discount_number) / 100) / 100 * order.vat_number) | int }}</td>
            <td>
                {% if bill.status != 'paid' %}
                <!-- 通常の削除フォーム(未会計) -->
                <form action="{{ url_for('register.delete_order', bill_id=bill.id, order_id=order.id) }}" method="post"
                    onsubmit="return confirm('本当に削除しますか?');">
                    <button type="submit" class="delete-button">削除</button>
                </form>
                {% else %}
                <!-- 会計済みなら返金フォームを表示 -->
                <form action="{{ url_for('refund.refund_order', order_id=order.id) }}" method="post"
                    onsubmit="return confirm('この商品を返金処理しますか?');">
                    <input type="number" name="refund_quantity" value="1" min="1" max="{{ order.quantity }}" required>
                    <button type="submit" class="refund-button">返金</button>
                </form>
                {% endif %}
            </td>
        </tr>
        {% endfor %}
    </tbody>

</table>
<a href="{{ url_for('register.view_bill', bill_id=bill.id) }}" class="button-link-back">戻る</a>
{% if bill.status == 'paid' %}
<!-- 返金された商品のリスト -->
<h2>返金された商品</h2>
{% if refunded_orders %}
<table>
    <thead>
        <tr>
            <th>商品名</th>
            <th>数量</th>
            <th>税抜き金額</th>
            <th>割引率</th>
            <th>税抜き金額(割引後)</th>
            <th>消費税率</th>
            <th>消費税額</th>
        </tr>
    </thead>
    <tbody>
        {% for refund in refunded_orders %}
        <tr>
            <td>{{ refund.product_name }}</td>
            <td>{{ refund.quantity }}</td>
            <td>{{ refund.refund_amount * refund.quantity | int }}</td>
            <td>{{ refund.discount_number }}%</td>
            <td>{{ (refund.total * (100 - refund.discount_number) / 100) | int }}</td>
            <td>{{ refund.vat_number }}%</td>
            <td>{{ ((refund.total * (100 - refund.discount_number) / 100) / 100 * refund.vat_number) | int }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% else %}
<p>返金された商品はありません。</p>
{% endif %}

{% endif %}
{% endblock %}

apps/refundapp/templates/refunded.html

apps/refundapp/templates/refunded.htmlを作成して次のように入力します。

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

今回は以上になります。

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

ブックマークのすすめ

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

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