@@ -9,7 +9,7 @@ import { DATE_MONTH, DATE_YEAR } from '../../constants/date.constants';
9
9
import { BudgetDto } from '../../interfaces/services/budget.interface' ;
10
10
import { getPipeMoneyNumber , getPipeMoneyString } from '../../utils/money.util' ;
11
11
import { AppBudgetService } from '../../services/budget' ;
12
- import { COLOR_SUCCESS } from '../../constants/colors' ;
12
+ import { COLOR_DANGER , COLOR_SUCCESS } from '../../constants/colors' ;
13
13
import { useNavigation } from '@react-navigation/native' ;
14
14
import { StackNavigationProp } from '@react-navigation/stack' ;
15
15
import { BudgetsContext } from '../../contexts/budgetsContext' ;
@@ -62,20 +62,35 @@ const CreateBudgetScreen: React.FC = () => {
62
62
setBudgetForm ( prev => ( { ...prev , categories, debts, total : getPipeMoneyString ( total ) } ) )
63
63
}
64
64
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 ) ;
69
68
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 ( "." , "," ) } ) )
71
81
}
72
82
73
83
const savebudget = async ( ) => {
74
- const { categories, total } = budgetForm ;
84
+ const { categories, total, debts } = budgetForm ;
85
+
75
86
const categoriesDto = categories . map ( category => (
76
87
{ categoryId : category . category . id , total : getPipeMoneyNumber ( category . total ) }
77
88
) ) . filter ( c => c . total > 0 ) ;
78
89
90
+ const debtsDto = debts . map ( debt => (
91
+ { debtId : debt . debt . id , total : getPipeMoneyNumber ( debt . total ) }
92
+ ) ) . filter ( d => d . total > 0 ) ;
93
+
79
94
const erros = [ ] ;
80
95
if ( ! categoriesDto ?. length ) erros . push ( "Adicione ao menos uma categoria" )
81
96
if ( ! getPipeMoneyNumber ( total ) ) erros . push ( "O total é obrigatório e maior que zero" )
@@ -89,7 +104,7 @@ const CreateBudgetScreen: React.FC = () => {
89
104
}
90
105
91
106
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 }
93
108
await AppBudgetService . create ( budgetDto )
94
109
await getBudgetsBalance ( )
95
110
navigation . goBack ( )
@@ -139,6 +154,7 @@ const CreateBudgetScreen: React.FC = () => {
139
154
</ View >
140
155
141
156
< 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 >
142
158
143
159
{ budgetForm . categories . map ( ( cate , i ) => (
144
160
< View key = { i } style = { style . containerCategory } >
@@ -156,7 +172,7 @@ const CreateBudgetScreen: React.FC = () => {
156
172
options = { INPUT_MASK_OPTIONS }
157
173
value = { budgetForm ?. categories [ i ] ?. total }
158
174
onChangeText = { ( total : string ) => {
159
- setTotal ( total , i )
175
+ setTotal ( total , i , "Categories" )
160
176
setBudgetForm ( prev => ( {
161
177
...prev , categories : budgetForm . categories . map ( ( category , index ) => {
162
178
if ( index == i ) category . total = total ;
@@ -186,7 +202,7 @@ const CreateBudgetScreen: React.FC = () => {
186
202
options = { INPUT_MASK_OPTIONS }
187
203
value = { budgetForm ?. debts [ i ] ?. total }
188
204
onChangeText = { ( total : string ) => {
189
- setTotal ( total , i )
205
+ setTotal ( total , i , "Debts" )
190
206
setBudgetForm ( prev => ( {
191
207
...prev , debts : budgetForm . debts . map ( ( debt , index ) => {
192
208
if ( index == i ) debt . total = total ;
0 commit comments