Skip to content

Commit 19fee70

Browse files
authored
Merge pull request #17 from Moises088/develop
Develop
2 parents b02e39c + a520fea commit 19fee70

File tree

12 files changed

+120
-33
lines changed

12 files changed

+120
-33
lines changed

src/components/budget/budget-card/index.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { View, Text } from 'react-native';
2+
import { View, Text, Image } from 'react-native';
33
import { ThemeContext } from '../../../contexts/themeContext';
44
import { styles } from './styles';
55
import { FontAwesome5 } from '@expo/vector-icons';
@@ -13,10 +13,16 @@ const BudgetCard: React.FC<{ item: BudgetsBalanceCategory }> = ({ item }) => {
1313

1414
const progress = parseFloat(((item.used * 100) / item.total).toFixed(2));
1515

16+
const Icon: React.FC = () => {
17+
if (item.categoryId) return <FontAwesome5 name={item.category?.icon} size={18} color={theme.text.primary} />
18+
if (item.debtId) return <Image source={item.debt?.logo} style={style.image} />
19+
return <View />
20+
}
21+
1622
return (
1723
<View style={style.container}>
1824
<View style={style.containerIcon}>
19-
<FontAwesome5 name={item.category?.icon} size={18} color={theme.text.primary} />
25+
<Icon />
2026
</View>
2127
<View style={style.containerInfo}>
2228
<View style={style.containerHeader}>

src/components/budget/budget-card/styles.ts

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export const styles = (theme: ThemesConfig) => {
2929
text: {
3030
letterSpacing: 1.05,
3131
color: theme.text.primary
32+
},
33+
image: {
34+
width: 38,
35+
height: 38,
36+
borderRadius: 38
3237
}
3338
})
3439
};

src/components/home/home-budget-itens/index.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@ const HomeBudgetItens: React.FC<{ item: BudgetsBalanceCategory }> = ({ item }) =
2121

2222
const progress = parseFloat(((item.used * 100) / item.total).toFixed(2));
2323

24+
const Icon: React.FC = () => {
25+
if (item.categoryId) return <FontAwesome5 name={item.category?.icon} size={18} color={theme.text.primary} />
26+
if (item.debtId) return <Image source={item.debt?.logo} style={style.image} />
27+
return <View />
28+
}
29+
2430
return (
2531
<View style={style.container}>
2632
<Image source={BACKGROUN_IMAGE} style={style.backgroundImage} />
2733
<View style={style.containerTitle}>
2834
<View style={[style.containerIcon, { backgroundColor: item.category?.color ?? theme.background.primary }]}>
29-
{item.category && (<FontAwesome5 name={item.category.icon} size={22} color={theme.text.primary} />)}
35+
<Icon />
3036
</View>
3137
<Text style={style.text}>{item.category?.name}</Text>
3238
</View>

src/components/home/home-budget-itens/styles.ts

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export const styles = (theme: ThemesConfig) => {
5858
containerProgress: {
5959
width: "90%",
6060
marginLeft: "5%"
61+
},
62+
image: {
63+
width: 38,
64+
height: 38,
65+
borderRadius: 38
6166
}
6267
})
6368
};

src/components/home/home-budget/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const HomeBudget: React.FC = () => {
2626
<Carousel
2727
width={250}
2828
itens={
29-
budgets?.categories.map((budget, i) => (
29+
budgets?.categories.map((budget, i) => i < 5 && (
3030
<HomeBudgetItens key={i} item={budget} />
3131
)) as JSX.Element[] ?? []
3232
}

src/interfaces/services/budget.interface.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CategoryEntity } from "./category.interface";
2+
import { DebtsInstitution } from "./debts.interface";
23

34
interface Budget {
45
year: string;
@@ -7,6 +8,10 @@ interface Budget {
78
categoryId: number;
89
total: number;
910
}[];
11+
debts: {
12+
debtId: number;
13+
total: number;
14+
}[];
1015
}
1116
export interface BudgetDto extends Budget {
1217
total: string
@@ -20,9 +25,11 @@ export interface BudgetEntity extends Budget {
2025
}
2126

2227
export interface BudgetsBalanceCategory {
23-
category: CategoryEntity | undefined;
28+
categoryId?: number;
29+
category?: CategoryEntity;
30+
debtId?: number;
31+
debt?: DebtsInstitution;
2432
used: number;
25-
categoryId: number;
2633
total: number;
2734
}
2835

src/interfaces/services/finance.interface.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CategoryEntity } from "./category.interface";
2-
import { DebtsInstitution } from "./debts.interface";
2+
import { DebtsEntity, DebtsInstitution } from "./debts.interface";
33

44
interface Finance {
55
name: string;
@@ -36,6 +36,7 @@ export interface FinanceBalance {
3636
}
3737

3838
export interface FinanceBalancePerCategory {
39+
bill?: { institution: DebtsInstitution | undefined, id: number };
3940
category: CategoryEntity | undefined;
4041
total: number;
4142
}

src/screens/budget/styles.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ export const styles = (theme: ThemesConfig) => {
100100
marginRight: 4
101101
},
102102
containerButton: {
103-
width: "70%",
104-
marginLeft: "15%",
103+
width: "80%",
104+
marginLeft: "10%",
105105
marginTop: 20,
106106
marginBottom: 20
107107
}

src/screens/create-budget/createBudgetScreen.tsx

+26-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { DATE_MONTH, DATE_YEAR } from '../../constants/date.constants';
99
import { BudgetDto } from '../../interfaces/services/budget.interface';
1010
import { getPipeMoneyNumber, getPipeMoneyString } from '../../utils/money.util';
1111
import { AppBudgetService } from '../../services/budget';
12-
import { COLOR_SUCCESS } from '../../constants/colors';
12+
import { COLOR_DANGER, COLOR_SUCCESS } from '../../constants/colors';
1313
import { useNavigation } from '@react-navigation/native';
1414
import { StackNavigationProp } from '@react-navigation/stack';
1515
import { BudgetsContext } from '../../contexts/budgetsContext';
@@ -62,20 +62,35 @@ const CreateBudgetScreen: React.FC = () => {
6262
setBudgetForm(prev => ({ ...prev, categories, debts, total: getPipeMoneyString(total) }))
6363
}
6464

65-
const setTotal = (total: string, index: number) => {
66-
const sum = budgetForm.categories
67-
.filter((r, i) => i !== index)
68-
.reduce((partialSum, a) => partialSum + parseFloat(a.total.replace(",", ".")), 0);
65+
const setTotal = (total: string, index: number, type: "Categories" | "Debts") => {
66+
let categories = budgetForm.categories.map(c => c);
67+
let debts = budgetForm.debts.map(c => c);
6968

70-
setBudgetForm(prev => ({ ...prev, total: (sum + parseFloat(total.replace(",", "."))).toFixed(2).replace(".", ",") }))
69+
if (type == "Categories") categories = categories.filter((_, i) => i !== index);
70+
if (type == "Debts") debts = debts.filter((_, i) => i !== index);
71+
72+
const formatTotal = (partial: string) => parseFloat(partial.replace(",", "."))
73+
74+
const sumCategories = categories.reduce((partialSum, a) => partialSum + formatTotal(a.total), 0);
75+
const sumDebts = debts.reduce((partialSum, a) => partialSum + formatTotal(a.total), 0);
76+
const totals = formatTotal(total);
77+
78+
const sum = sumCategories + sumDebts + totals;
79+
80+
setBudgetForm(prev => ({ ...prev, total: sum.toFixed(2).replace(".", ",") }))
7181
}
7282

7383
const savebudget = async () => {
74-
const { categories, total } = budgetForm;
84+
const { categories, total, debts } = budgetForm;
85+
7586
const categoriesDto = categories.map(category => (
7687
{ categoryId: category.category.id, total: getPipeMoneyNumber(category.total) }
7788
)).filter(c => c.total > 0);
7889

90+
const debtsDto = debts.map(debt => (
91+
{ debtId: debt.debt.id, total: getPipeMoneyNumber(debt.total) }
92+
)).filter(d => d.total > 0);
93+
7994
const erros = [];
8095
if (!categoriesDto?.length) erros.push("Adicione ao menos uma categoria")
8196
if (!getPipeMoneyNumber(total)) erros.push("O total é obrigatório e maior que zero")
@@ -89,7 +104,7 @@ const CreateBudgetScreen: React.FC = () => {
89104
}
90105

91106
try {
92-
const budgetDto: BudgetDto = { month: filteredMonth, year: filteredYear, categories: categoriesDto, total }
107+
const budgetDto: BudgetDto = { month: filteredMonth, year: filteredYear, categories: categoriesDto, debts: debtsDto, total }
93108
await AppBudgetService.create(budgetDto)
94109
await getBudgetsBalance()
95110
navigation.goBack()
@@ -139,6 +154,7 @@ const CreateBudgetScreen: React.FC = () => {
139154
</View>
140155

141156
<Text style={[style.textButton, { fontSize: 14, marginTop: 10 }]}>Escolha as categorias</Text>
157+
<Text style={[style.textButton, { fontSize: 11, marginTop: 10, color: COLOR_DANGER }]}>Apenas gastos</Text>
142158

143159
{budgetForm.categories.map((cate, i) => (
144160
<View key={i} style={style.containerCategory}>
@@ -156,7 +172,7 @@ const CreateBudgetScreen: React.FC = () => {
156172
options={INPUT_MASK_OPTIONS}
157173
value={budgetForm?.categories[i]?.total}
158174
onChangeText={(total: string) => {
159-
setTotal(total, i)
175+
setTotal(total, i, "Categories")
160176
setBudgetForm(prev => ({
161177
...prev, categories: budgetForm.categories.map((category, index) => {
162178
if (index == i) category.total = total;
@@ -186,7 +202,7 @@ const CreateBudgetScreen: React.FC = () => {
186202
options={INPUT_MASK_OPTIONS}
187203
value={budgetForm?.debts[i]?.total}
188204
onChangeText={(total: string) => {
189-
setTotal(total, i)
205+
setTotal(total, i, "Debts")
190206
setBudgetForm(prev => ({
191207
...prev, debts: budgetForm.debts.map((debt, index) => {
192208
if (index == i) debt.total = total;

src/screens/create-debt/createDebtScreen.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import { StackNavigationProp } from '@react-navigation/stack';
2020
import { useRoute, RouteProp, useNavigation } from "@react-navigation/native";
2121
import { DebtsContext } from '../../contexts/debtsContext';
2222
import { DebtsParamRoute } from '../../interfaces/screens/debts.interface';
23+
import { FinancesContext } from '../../contexts/financesContext';
2324

2425
const CreateDebtScreen: React.FC = () => {
2526

27+
const { getFinancesBalance } = React.useContext(FinancesContext);
2628
const { getDebtsBalance } = React.useContext(DebtsContext);
2729
const { theme } = React.useContext(ThemeContext);
2830
const style = styles(theme);
@@ -44,7 +46,7 @@ const CreateDebtScreen: React.FC = () => {
4446
if (params?.debts) {
4547
const debts = params.debts;
4648
const getTotalPerMonth = () => {
47-
if(debts.total == debts.totalPerMonth) return "0";
49+
if (debts.total == debts.totalPerMonth) return "0";
4850
return getPipeMoneyString(debts.totalPerMonth)
4951
}
5052

@@ -90,13 +92,14 @@ const CreateDebtScreen: React.FC = () => {
9092
institutionName
9193
}
9294

93-
if(!debtId){
95+
if (!debtId) {
9496
await AppDebtsService.create(debtDto);
9597
} else {
9698
await AppDebtsService.update(debtId, debtDto);
9799
}
98100

99101
await getDebtsBalance()
102+
await getFinancesBalance()
100103
navigation.goBack()
101104
} catch (error: any) {
102105
if (error?.message) setValidation([error.message])

src/services/balance.ts

+30-11
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,12 @@ class BalanceService {
9090
const category = categories.find(c => c.id == finance.categoryId);
9191
const getBill = bills.find(bill => bill.id == finance.billId);
9292
const getInstitution = DEBTS_INSTITUTION.find(bill => bill.id == getBill?.institutionId);
93-
if(!getInstitution) return;
9493

95-
const bill = decontextualize<DebtsInstitution>(getInstitution);
96-
if (bill.name == "OUTRO") bill.name = getBill?.institutionName ?? "";
94+
let bill;
95+
if (getInstitution) {
96+
bill = decontextualize<DebtsInstitution>(getInstitution);
97+
if (bill.name == "OUTRO") bill.name = getBill?.institutionName ?? "";
98+
}
9799

98100
return { ...finance, category, bill };
99101
}
@@ -112,16 +114,33 @@ class BalanceService {
112114
const perCategory: FinanceBalancePerCategory[] = [];
113115

114116
for (const balance of balances.finances) {
115-
const category = perCategory.find(category => category?.category?.id == balance.categoryId);
116-
if (category?.total) {
117-
category.total += balance.value;
118-
continue
117+
if (balance.categoryId) {
118+
const category = perCategory.find(category => category?.category?.id == balance.categoryId);
119+
if (category?.total) {
120+
category.total += balance.value;
121+
continue
122+
}
123+
124+
perCategory.push({
125+
bill: undefined,
126+
category: balance.category,
127+
total: balance.value
128+
})
119129
}
120130

121-
perCategory.push({
122-
category: balance.category,
123-
total: balance.value
124-
})
131+
if (balance.billId) {
132+
const bill = perCategory.find(bill => bill.bill?.id == balance.billId);
133+
if (bill?.total) {
134+
bill.total += balance.value;
135+
continue
136+
}
137+
138+
perCategory.push({
139+
bill: { institution: balance.bill, id: balance.billId },
140+
category: undefined,
141+
total: balance.value
142+
})
143+
}
125144
}
126145

127146
return { ...balances, categories: perCategory }

src/services/budget.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ASYNC_BUDGETS } from "../constants/storage.constant";
77
import { AppCategoryService } from "./category";
88
import { AppFinanceService } from "./finance";
99
import { AppBalanceService } from "./balance";
10+
import { AppDebtsService } from "./debts";
1011

1112
class Budget implements BudgetEntity {
1213
id: number;
@@ -16,12 +17,14 @@ class Budget implements BudgetEntity {
1617
month: string;
1718
value: number;
1819
categories: { categoryId: number; total: number; }[];
20+
debts: { debtId: number; total: number; }[];
1921

2022
constructor(budgetDto: BudgetDto, id: number) {
2123
this.id = id + 1;
2224
this.year = budgetDto.year;
2325
this.month = budgetDto.month;
2426
this.categories = budgetDto.categories;
27+
this.debts = budgetDto.debts;
2528
this.value = getPipeMoneyNumber(budgetDto.total);
2629
this.createdAt = getPipeDateTimeString();
2730
}
@@ -36,12 +39,14 @@ class BudgetService implements Services<BudgetEntity, BudgetDto>{
3639

3740
public async getBudgetBalance(month: string, year: string): Promise<BudgetsBalanceEntity | undefined> {
3841
const budgets = await this.find();
42+
const debts = await AppDebtsService.findInstitutions();
3943
const budget = budgets.find(budget => budget.month == month && budget.year == year);
4044
const { categories, totalExpense } = await AppBalanceService.getFinancesBalancePerCategory(month, year, 1);
4145

4246
if (!budget?.categories) return;
4347

44-
const budgetCategoriesFilter: BudgetsBalanceCategory[] = []
48+
const budgetCategoriesFilter: BudgetsBalanceCategory[] = [];
49+
4550
for (const budgetCategory of budget.categories) {
4651
const findCategory = categories.find(c => c.category?.id == budgetCategory.categoryId);
4752
if (!findCategory) {
@@ -55,6 +60,20 @@ class BudgetService implements Services<BudgetEntity, BudgetDto>{
5560
budgetCategoriesFilter.push({ ...budgetCategory, category, used: total })
5661
}
5762

63+
for (const debtCategory of budget.debts) {
64+
const findDebt = categories.find(debt => debt.bill?.id == debtCategory.debtId)
65+
if (!findDebt) {
66+
const debt = await debts.find(debt => debt.id == debtCategory.debtId);
67+
if (!debt) continue;
68+
69+
budgetCategoriesFilter.push({ ...debtCategory, debt, used: 0 });
70+
continue
71+
}
72+
73+
const { bill, total } = findDebt;
74+
budgetCategoriesFilter.push({ ...debtCategory, debt: bill?.institution, used: total })
75+
}
76+
5877
return { ...budget, categories: budgetCategoriesFilter, totalExpense }
5978
}
6079

0 commit comments

Comments
 (0)