Move extra bills to credit cards

This commit is contained in:
Andrew Tomaka 2023-03-03 18:12:59 -05:00
parent c14a408f9a
commit 0887a5f6b9
Signed by: atomaka
GPG key ID: 61209BF70A5B18BE
38 changed files with 242 additions and 251 deletions

View file

@ -0,0 +1,70 @@
class CreditCardBillsController < ApplicationController
before_action :set_credit_card_bill, only: %i[ show edit update destroy ]
# GET /credit_card_bills or /credit_card_bills.json
def index
@credit_card_bills = CreditCardBill.all
end
# GET /credit_card_bills/1 or /credit_card_bills/1.json
def show
end
# GET /credit_card_bills/new
def new
@credit_card_bill = CreditCardBill.new
end
# GET /credit_card_bills/1/edit
def edit
end
# POST /credit_card_bills or /credit_card_bills.json
def create
@credit_card_bill = CreditCardBill.new(credit_card_bill_params)
respond_to do |format|
if @credit_card_bill.save
format.html { redirect_to credit_card_bill_url(@credit_card_bill), notice: "Credit card bill was successfully created." }
format.json { render :show, status: :created, location: @credit_card_bill }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @credit_card_bill.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /credit_card_bills/1 or /credit_card_bills/1.json
def update
respond_to do |format|
if @credit_card_bill.update(credit_card_bill_params)
format.html { redirect_to credit_card_bill_url(@credit_card_bill), notice: "Credit card bill was successfully updated." }
format.json { render :show, status: :ok, location: @credit_card_bill }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @credit_card_bill.errors, status: :unprocessable_entity }
end
end
end
# DELETE /credit_card_bills/1 or /credit_card_bills/1.json
def destroy
@credit_card_bill.destroy
respond_to do |format|
format.html { redirect_to credit_card_bills_url, notice: "Credit card bill was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_credit_card_bill
@credit_card_bill = CreditCardBill.find(params[:id])
end
# Only allow a list of trusted parameters through.
def credit_card_bill_params
params.require(:credit_card_bill).permit(:description, :amount)
end
end

View file

@ -65,6 +65,6 @@ class ExpensesController < ApplicationController
# Only allow a list of trusted parameters through.
def expense_params
params.require(:expense).permit(:description, :payment, :period, :autopaid, :estimated)
params.require(:expense).permit(:description, :payment, :period, :credit_card, :estimated)
end
end

View file

@ -1,70 +0,0 @@
class ExtraBillsController < ApplicationController
before_action :set_extra_bill, only: %i[ show edit update destroy ]
# GET /extra_bills or /extra_bills.json
def index
@extra_bills = ExtraBill.all
end
# GET /extra_bills/1 or /extra_bills/1.json
def show
end
# GET /extra_bills/new
def new
@extra_bill = ExtraBill.new
end
# GET /extra_bills/1/edit
def edit
end
# POST /extra_bills or /extra_bills.json
def create
@extra_bill = ExtraBill.new(extra_bill_params)
respond_to do |format|
if @extra_bill.save
format.html { redirect_to extra_bill_url(@extra_bill), notice: "Extra bill was successfully created." }
format.json { render :show, status: :created, location: @extra_bill }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @extra_bill.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /extra_bills/1 or /extra_bills/1.json
def update
respond_to do |format|
if @extra_bill.update(extra_bill_params)
format.html { redirect_to extra_bill_url(@extra_bill), notice: "Extra bill was successfully updated." }
format.json { render :show, status: :ok, location: @extra_bill }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @extra_bill.errors, status: :unprocessable_entity }
end
end
end
# DELETE /extra_bills/1 or /extra_bills/1.json
def destroy
@extra_bill.destroy
respond_to do |format|
format.html { redirect_to extra_bills_url, notice: "Extra bill was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_extra_bill
@extra_bill = ExtraBill.find(params[:id])
end
# Only allow a list of trusted parameters through.
def extra_bill_params
params.require(:extra_bill).permit(:description, :amount, :deduct_autopaid)
end
end

View file

@ -0,0 +1,5 @@
class CreditCardBill < ApplicationRecord
def unpaid
amount - Expense.credit_card_monthly_total
end
end

View file

@ -19,7 +19,7 @@ class Expense < ApplicationRecord
Expense.all.map(&:monthly).sum.round(2)
end
def self.autopaid_total
Expense.where(autopaid: true).map(&:monthly).sum.round(2)
def self.credit_card_monthly_total
Expense.where(credit_card: true).map(&:monthly).sum.round(2)
end
end

View file

@ -1,5 +0,0 @@
class ExtraBill < ApplicationRecord
def adjusted_amount
deduct_autopaid? ? amount - Expense.autopaid_total : amount
end
end

View file

@ -1,27 +1,22 @@
<div id="<%= dom_id extra_bill %>">
<div id="<%= dom_id credit_card_bill %>">
<p class="my-5">
<strong class="block font-medium mb-1">Description:</strong>
<%= extra_bill.description %>
<%= credit_card_bill.description %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Amount:</strong>
<%= extra_bill.amount %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Adjusted Amount:</strong>
<%= extra_bill.adjusted_amount %>
<%= credit_card_bill.amount %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Accounted For:</strong>
<%= Expense.autopaid_total %>
<%= Expense.credit_card_monthly_total %>
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Deduct autopaid:</strong>
<%= extra_bill.deduct_autopaid %>
<strong class="block font-medium mb-1">Unpaid:</strong>
<%= credit_card_bill.unpaid %>
</p>
<p class="my-5">
@ -38,7 +33,7 @@
<tr class="even:bg-gray-50 border-b">
<td class="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap"><%= member.name %></td>
<td class="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap"><%= member.burden_percent * 100 %>%</td>
<td class="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap"><%= number_to_currency(member.burden_amount(total_amount: extra_bill.adjusted_amount)) %></td>
<td class="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap"><%= number_to_currency(member.burden_amount(total_amount: credit_card_bill.unpaid)) %></td>
</tr>
<% end %>
</tbody>
@ -46,8 +41,8 @@
</p>
<% if action_name != "show" %>
<%= link_to "Show this extra bill", extra_bill, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to 'Edit this extra bill', edit_extra_bill_path(extra_bill), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Show this credit card bill", credit_card_bill, class: "rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to 'Edit this credit card bill', edit_credit_card_bill_path(credit_card_bill), class: "rounded-lg py-3 ml-2 px-5 bg-gray-100 inline-block font-medium" %>
<hr class="mt-6">
<% end %>
</div>

View file

@ -0,0 +1,2 @@
json.extract! credit_card_bill, :id, :description, :amount, :created_at, :updated_at
json.url credit_card_bill_url(credit_card_bill, format: :json)

View file

@ -1,10 +1,9 @@
<%= form_with(model: extra_bill, class: "contents") do |form| %>
<% if extra_bill.errors.any? %>
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(extra_bill.errors.count, "error") %> prohibited this extra_bill from being saved:</h2>
<%= form_with(model: credit_card_bill, class: "contents") do |form| %>
<% if credit_card_bill.errors.any? %> <div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
<h2><%= pluralize(credit_card_bill.errors.count, "error") %> prohibited this credit_card_bill from being saved:</h2>
<ul>
<% extra_bill.errors.each do |error| %>
<% credit_card_bill.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
@ -21,11 +20,6 @@
<%= form.text_field :amount, class: "block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
<div class="my-5">
<%= form.label :deduct_autopaid %>
<%= form.check_box :deduct_autopaid, class: "block mt-2 h-5 w-5" %>
</div>
<div class="inline">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>

View file

@ -0,0 +1,8 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing credit card bill</h1>
<%= render "form", credit_card_bill: @credit_card_bill %>
<%= link_to "Show this credit card bill", @credit_card_bill, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to credit card bills", credit_card_bills_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View file

@ -0,0 +1,14 @@
<div class="w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Credit card bills</h1>
<%= link_to 'New credit card bill', new_credit_card_bill_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="credit_card_bills" class="min-w-full">
<%= render @credit_card_bills %>
</div>
</div>

View file

@ -0,0 +1 @@
json.array! @credit_card_bills, partial: "credit_card_bills/credit_card_bill", as: :credit_card_bill

View file

@ -0,0 +1,7 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New credit card bill</h1>
<%= render "form", credit_card_bill: @credit_card_bill %>
<%= link_to 'Back to credit card bills', credit_card_bills_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View file

@ -0,0 +1,15 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<%= render @credit_card_bill %>
<%= link_to 'Edit this credit_card_bill', edit_credit_card_bill_path(@credit_card_bill), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<div class="inline-block ml-2">
<%= button_to 'Destroy this credit_card_bill', credit_card_bill_path(@credit_card_bill), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
<%= link_to 'Back to credit_card_bills', credit_card_bills_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
</div>

View file

@ -0,0 +1 @@
json.partial! "credit_card_bills/credit_card_bill", credit_card_bill: @credit_card_bill

View file

@ -49,7 +49,7 @@
<th scope="col" class="text-sm font-medium text-gray-900 px-6 py-4 text-left">Payment</th>
<th scope="col" class="text-sm font-medium text-gray-900 px-6 py-4 text-left">Period</th>
<th scope="col" class="text-sm font-medium text-gray-900 px-6 py-4 text-left">Montly</th>
<th scope="col" class="text-sm font-medium text-gray-900 px-6 py-4 text-left">Autopaid</th>
<th scope="col" class="text-sm font-medium text-gray-900 px-6 py-4 text-left">credit_card</th>
<th scope="col" class="text-sm font-medium text-gray-900 px-6 py-4 text-left">Estimated</th>
</tr>
</thead>
@ -60,7 +60,7 @@
<td class="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap"><%= number_to_currency(expense.payment) %></td>
<td class="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap"><%= expense.period %></td>
<td class="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap"><%= number_to_currency(expense.monthly) %></td>
<td class="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap"><%= expense.autopaid %></td>
<td class="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap"><%= expense.credit_card %></td>
<td class="text-sm text-gray-900 font-light px-6 py-4 whitespace-nowrap"><%= expense.estimated %></td>
</tr>
<% end %>

View file

@ -15,8 +15,8 @@
</p>
<p class="my-5">
<strong class="block font-medium mb-1">Autopaid:</strong>
<%= expense.autopaid %>
<strong class="block font-medium mb-1">Credit card:</strong>
<%= expense.credit_card %>
</p>
<p class="my-5">

View file

@ -1,2 +1,2 @@
json.extract! expense, :id, :description, :payment, :period, :autopaid, :estimated, :created_at, :updated_at
json.extract! expense, :id, :description, :payment, :period, :credit_card, :estimated, :created_at, :updated_at
json.url expense_url(expense, format: :json)

View file

@ -32,8 +32,8 @@
</div>
<div class="my-5">
<%= form.label :autopaid %>
<%= form.check_box :autopaid, class: "block mt-2 h-5 w-5" %>
<%= form.label :credit_card %>
<%= form.check_box :credit_card, class: "block mt-2 h-5 w-5" %>
</div>
<div class="my-5">

View file

@ -1,2 +0,0 @@
json.extract! extra_bill, :id, :description, :amount, :deduct_autopaid, :created_at, :updated_at
json.url extra_bill_url(extra_bill, format: :json)

View file

@ -1,8 +0,0 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">Editing extra bill</h1>
<%= render "form", extra_bill: @extra_bill %>
<%= link_to "Show this extra bill", @extra_bill, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<%= link_to "Back to extra bills", extra_bills_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View file

@ -1,14 +0,0 @@
<div class="w-full">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<div class="flex justify-between items-center">
<h1 class="font-bold text-4xl">Extra bills</h1>
<%= link_to 'New extra bill', new_extra_bill_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
</div>
<div id="extra_bills" class="min-w-full">
<%= render @extra_bills %>
</div>
</div>

View file

@ -1 +0,0 @@
json.array! @extra_bills, partial: "extra_bills/extra_bill", as: :extra_bill

View file

@ -1,7 +0,0 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New extra bill</h1>
<%= render "form", extra_bill: @extra_bill %>
<%= link_to 'Back to extra bills', extra_bills_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>

View file

@ -1,15 +0,0 @@
<div class="mx-auto md:w-2/3 w-full flex">
<div class="mx-auto">
<% if notice.present? %>
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
<% end %>
<%= render @extra_bill %>
<%= link_to 'Edit this extra_bill', edit_extra_bill_path(@extra_bill), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
<div class="inline-block ml-2">
<%= button_to 'Destroy this extra_bill', extra_bill_path(@extra_bill), method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %>
</div>
<%= link_to 'Back to extra_bills', extra_bills_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
</div>

View file

@ -1 +0,0 @@
json.partial! "extra_bills/extra_bill", extra_bill: @extra_bill

View file

@ -16,7 +16,7 @@
<ul class="flex">
<li class="mr-6"><%= link_to "Dashboard", root_path, class: "text-white" %></li>
<li class="mr-6"><%= link_to "Expenses", expenses_path, class: "text-white" %></li>
<li class="mr-6"><%= link_to "Extra Bills", extra_bills_path, class: "text-white" %></li>
<li class="mr-6"><%= link_to "Credit Card Bills", credit_card_bills_path, class: "text-white" %></li>
<li class="mr-6"><%= link_to "Incomes", incomes_path, class: "text-white" %></li>
<li class="mr-6"><%= link_to "Members", members_path, class: "text-white" %></li>
</ul>