Merge branch 'add-settle-support' into 'master'
Add settle support See merge request saburly/gangsta/roraccounting!13
This commit was merged in pull request #13.
This commit is contained in:
@@ -26,6 +26,19 @@ class HomiesController < ApplicationController
|
|||||||
json_response(Homie.cash(importance))
|
json_response(Homie.cash(importance))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def settle
|
||||||
|
homie_id = params[:homie_id].to_i
|
||||||
|
homie = Homie.find(homie_id)
|
||||||
|
|
||||||
|
if homie.settle
|
||||||
|
json_response []
|
||||||
|
else
|
||||||
|
error_response :bad_request
|
||||||
|
end
|
||||||
|
rescue StandardError
|
||||||
|
error_response :bad_request
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def homie_params
|
def homie_params
|
||||||
|
|||||||
@@ -10,4 +10,18 @@ class Homie < ApplicationRecord
|
|||||||
{ homie: homie, amount: total }
|
{ homie: homie, amount: total }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def settle
|
||||||
|
total = MoneyMove.where(homie_id: id).all.sum(:amount)
|
||||||
|
all_money_moves = MoneyMove.where(homie_id: id).all
|
||||||
|
|
||||||
|
transaction do
|
||||||
|
all_money_moves.destroy_all
|
||||||
|
|
||||||
|
MoneyMove.create_settle_record(id, total)
|
||||||
|
end
|
||||||
|
true
|
||||||
|
rescue StandardError => e
|
||||||
|
false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,6 +13,16 @@ class MoneyMove < ApplicationRecord
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.create_settle_record(homie_id, amount)
|
||||||
|
new_desc = "Settled on #{DateTime.now.utc}"
|
||||||
|
settle_record = MoneyMove.new(
|
||||||
|
homie_id: homie_id,
|
||||||
|
amount: amount,
|
||||||
|
description: new_desc
|
||||||
|
)
|
||||||
|
settle_record.save
|
||||||
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
private
|
private
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect, useReducer } from 'react';
|
||||||
import { Button, Table } from 'react-materialize';
|
import { Button, Table } from 'react-materialize';
|
||||||
import './Cash.css';
|
import './Cash.css';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { MAKE_MONEY_MOVE } from '../RouteNames';
|
import { MAKE_MONEY_MOVE } from '../RouteNames';
|
||||||
import { withRouter } from 'react-router-dom';
|
import { withRouter } from 'react-router-dom';
|
||||||
import { formatMoney } from '../common/formatting';
|
import { formatMoney } from '../common/formatting';
|
||||||
|
import YesNoModal from "../common/YesNoModal";
|
||||||
|
import { errorToast } from "../common/errorHelpers";
|
||||||
|
import M from 'materialize-css';
|
||||||
|
|
||||||
const Cash = (props) => {
|
const Cash = (props) => {
|
||||||
const [homiesCash, setHomiesCash] = useState([]);
|
const [homiesCash, setHomiesCash] = useState([]);
|
||||||
|
const [, forceUpdate] = useReducer(x => x + 1, 0);
|
||||||
//const [importance, setImportance] = useState(10);
|
//const [importance, setImportance] = useState(10);
|
||||||
|
|
||||||
useEffect( () => {
|
useEffect( () => {
|
||||||
@@ -22,6 +26,20 @@ const Cash = (props) => {
|
|||||||
getCashForHomies();
|
getCashForHomies();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const settleFlowForHomie = async (id) => {
|
||||||
|
try {
|
||||||
|
const response = await axios.delete(`/api/homies/${id}/settle`);
|
||||||
|
if (response.status === 200 && response.data) {
|
||||||
|
M.toast({ html: 'Settled' });
|
||||||
|
}else{
|
||||||
|
errorToast();
|
||||||
|
}
|
||||||
|
} catch(e) {
|
||||||
|
errorToast();
|
||||||
|
}
|
||||||
|
forceUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
const cashTableBody = homiesCash.map( (homieLine) => {
|
const cashTableBody = homiesCash.map( (homieLine) => {
|
||||||
return (
|
return (
|
||||||
<tr key={homieLine.homie.id}>
|
<tr key={homieLine.homie.id}>
|
||||||
@@ -32,7 +50,13 @@ const Cash = (props) => {
|
|||||||
{ formatMoney(homieLine.amount) }
|
{ formatMoney(homieLine.amount) }
|
||||||
</td>
|
</td>
|
||||||
<td className="cash-cell-left">
|
<td className="cash-cell-left">
|
||||||
[ settle ]
|
[
|
||||||
|
<YesNoModal
|
||||||
|
body={"Maan, y'a sure about this? Flow history goes bye bye!"}
|
||||||
|
yesAction={() => settleFlowForHomie(homieLine.homie.id)}
|
||||||
|
triggerNode={<a href='#'>Settle</a>}
|
||||||
|
/>
|
||||||
|
]
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -4,10 +4,13 @@ Rails.application.routes.draw do
|
|||||||
resources :money_moves
|
resources :money_moves
|
||||||
resources :chips, only: %i[index create destroy]
|
resources :chips, only: %i[index create destroy]
|
||||||
resources :chip_values, only: %i[create update destroy]
|
resources :chip_values, only: %i[create update destroy]
|
||||||
resources :homies do
|
resources :homies, param: :homie_id do
|
||||||
collection do
|
collection do
|
||||||
get 'cash'
|
get 'cash'
|
||||||
end
|
end
|
||||||
|
member do
|
||||||
|
delete 'settle'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user