~sjrct/homebank/resize-xfer-window

« back to all changes in this revision

Viewing changes to src/rep-budget.c

  • Committer: Maxime Doyen
  • Date: 2023-12-09 09:48:49 UTC
  • Revision ID: homebank@free.fr-20231209094849-rdoz0pmsutvxh1r4
5.7.3 release

Show diffs side-by-side

added added

removed removed

Lines of Context:
760
760
}
761
761
 
762
762
 
763
 
static void repbudget_compute_cat_spent(guint32 key, gdouble amount, gdouble *tmp_spent, gdouble *tmp_budget)
764
 
{
765
 
Category *cat;
766
 
 
767
 
        cat = da_cat_get(key);
768
 
        if(cat)
769
 
        {
770
 
                DB( g_print(" cat %02d:%02d (sub=%d), bud=%.2f\n", cat->parent, cat->key, (cat->flags & GF_SUB), tmp_budget[cat->key]) );
771
 
 
772
 
                if( (cat->flags & GF_FORCED) || (cat->flags & GF_BUDGET) )
773
 
                {
774
 
                        DB( g_print("  + spend %.2f to cat %d\n", amount, cat->key) );
775
 
                        tmp_spent[cat->key] += amount;
776
 
                }
777
 
 
778
 
                //#1825653 subcat without budget must be computed
779
 
                if( (cat->flags & GF_SUB) )
780
 
                {
781
 
                Category *pcat = da_cat_get(cat->parent);
782
 
 
783
 
                        if(pcat)
784
 
                        {
785
 
                                if( (cat->flags & GF_FORCED) || (cat->flags & GF_BUDGET) || (pcat->flags & GF_FORCED) || (pcat->flags & GF_BUDGET) )
786
 
                                {
787
 
                                        DB( g_print("  + spend %.2f to parent %d\n", amount, cat->parent) );
788
 
                                        tmp_spent[pcat->key] += amount;
789
 
                                }
790
 
 
791
 
                        }
792
 
                }
793
 
        }                               
794
 
}
795
 
 
796
 
 
797
763
static void repbudget_fill_budget_for_category(Category *catitem, gdouble *tmp_budget, gint startmonth, gint nbmonth)
798
764
{
799
765
 
847
813
}
848
814
 
849
815
 
 
816
static void repbudget_compute_cat_spent(guint32 key, gint tmptype, gdouble amount, gdouble *tmp_spent)
 
817
{
 
818
Category *cat;
 
819
 
 
820
        //5.7.3 filter on type
 
821
        if( (tmptype == 1 && amount > 0) ||  (tmptype == 2 && amount< 0) )
 
822
                return;
 
823
 
 
824
        cat = da_cat_get(key);
 
825
        if(cat)
 
826
        {
 
827
                DB( g_print(" cat %02d:%02d (sub=%d)\n", cat->parent, cat->key, (cat->flags & GF_SUB)) );
 
828
 
 
829
                if( (cat->flags & GF_FORCED) || (cat->flags & GF_BUDGET) )
 
830
                {
 
831
                        DB( g_print("  + spend %.2f to cat %d\n", amount, cat->key) );
 
832
                        tmp_spent[cat->key] += amount;
 
833
                }
 
834
 
 
835
                //#1825653 subcat without budget must be computed
 
836
                if( (cat->flags & GF_SUB) )
 
837
                {
 
838
                Category *pcat = da_cat_get(cat->parent);
 
839
 
 
840
                        if(pcat)
 
841
                        {
 
842
                                if( (cat->flags & GF_FORCED) || (cat->flags & GF_BUDGET) || (pcat->flags & GF_FORCED) || (pcat->flags & GF_BUDGET) )
 
843
                                {
 
844
                                        DB( g_print("  + spend %.2f to parent %d\n", amount, cat->parent) );
 
845
                                        tmp_spent[pcat->key] += amount;
 
846
                                }
 
847
 
 
848
                        }
 
849
                }
 
850
        }
 
851
                                
 
852
}
 
853
 
 
854
 
850
855
static void budget_compute_category(struct repbudget_data *data, GtkTreeModel *model, gdouble *tmp_spent, gdouble *tmp_budget, guint startmonth, guint nbmonth, gboolean tmptype, gboolean tmponlyout)
851
856
{
852
857
GList *lcat, *list;
887
892
                        for(i=0;i<nbsplit;i++)
888
893
                        {
889
894
                                split = da_splits_get(ope->splits, i);
890
 
                                repbudget_compute_cat_spent(split->kcat, hb_amount_base(split->amount, ope->kcur), tmp_spent, tmp_budget);
 
895
                                repbudget_compute_cat_spent(split->kcat, tmptype, hb_amount_base(split->amount, ope->kcur), tmp_spent);
891
896
                        }
892
897
                }
893
898
                else
894
899
                {
895
 
                        repbudget_compute_cat_spent(ope->kcat, hb_amount_base(ope->amount, ope->kcur), tmp_spent, tmp_budget);
 
900
                        repbudget_compute_cat_spent(ope->kcat, tmptype, hb_amount_base(ope->amount, ope->kcur), tmp_spent);
896
901
                }
897
902
 
898
903
                list = g_list_next(list);
900
905
 
901
906
 
902
907
        DB( g_print("\n -- populate budget listview --\n") );
 
908
 
 
909
        DB( g_printf(" type=%d, onlyout=%d\n", tmptype, tmponlyout) );
903
910
        
904
911
        id = 0;
905
912
        list = lcat;
915
922
                name = catitem->key == 0 ? "(None)" : catitem->name;
916
923
                guint pos = catitem->key;
917
924
 
 
925
                DB( g_print(" eval %d: %d '%s' b:%d f:%d : spen=%.2f bud=%.2f \n", 
 
926
                        id, pos, name, (catitem->flags & GF_BUDGET), (catitem->flags & GF_FORCED), 
 
927
                        tmp_spent[pos], tmp_budget[pos] ) );
 
928
 
918
929
                // display expense or income (filter on amount and not category hypothetical flag
919
 
                if( tmptype == 1 && tmp_budget[pos] > 0 )
920
 
                        goto budnext;
921
 
 
922
 
                if( tmptype == 2 && tmp_budget[pos] < 0 )
923
 
                        goto budnext;
924
 
 
925
 
                DB( g_print(" eval %d '%s' : spen=%.2f bud=%.2f \n", pos, name, tmp_spent[pos], tmp_budget[pos] ) );
926
 
 
927
 
                if( (catitem->flags & (GF_BUDGET|GF_FORCED)) || tmp_budget[pos] /*|| tmp_spent[pos]*/)
 
930
                /*if( (tmptype == 1 && tmp_spent[pos] > 0)
 
931
                ||  (tmptype == 2 && tmp_spent[pos] < 0)
 
932
                  )
 
933
                {
 
934
                        DB( g_printf("  >skip: type filter\n") );
 
935
                        goto budnext;
 
936
                }*/
 
937
 
 
938
 
 
939
                // display budget with forced/budget or budget value != 0.00
 
940
                //if( (catitem->flags & (GF_BUDGET|GF_FORCED)) || tmp_budget[pos] || tmp_spent[pos] )
 
941
                if( hb_amount_equal(tmp_budget[pos], 0.0) == FALSE || hb_amount_equal(tmp_spent[pos], 0.0) == FALSE )
928
942
                {
929
943
                gdouble result, rawrate;
 
944
                gint fulfilled;
930
945
                gchar *status;
931
946
 
932
947
                        result = budget_compute_result(tmp_budget[pos], tmp_spent[pos]);
959
974
                                }
960
975
                        }
961
976
 
962
 
                        if(tmponlyout == TRUE && outofbudget == FALSE)
 
977
                        if((tmponlyout == TRUE && outofbudget == FALSE) && !(catitem->flags & GF_MIXED) )
 
978
                        {
 
979
                                DB( g_printf("  >skip: only out filter is on\n") );
963
980
                                goto nextins;
 
981
                        }
964
982
 
965
983
                        tmpparent = NULL;
966
984
                        Category *tmpcat = da_cat_get(pos);
977
995
                                }
978
996
                        }
979
997
 
980
 
                        DB( g_print(" => insert '%s' s:%.2f b:%.2f r:%.2f (%%%.2f) '%s' '%d'\n\n", name, tmp_spent[pos], tmp_budget[pos], result, rawrate, status, outofbudget ) );
 
998
                        DB( g_print("  >insert '%s' s:%.2f b:%.2f r:%.2f (%%%.2f) '%s' '%d'\n", name, tmp_spent[pos], tmp_budget[pos], result, rawrate, status, outofbudget ) );
 
999
 
 
1000
                        //5.7.3 dont fulfill if no budget
 
1001
                        fulfilled = 0;
 
1002
                        if( hb_amount_equal(tmp_budget[pos], 0.0) == FALSE )
 
1003
                                fulfilled = (gint)hb_amount_round(rawrate*100, 0);
981
1004
 
982
1005
                        gtk_tree_store_insert_with_values (GTK_TREE_STORE(model), &iter, tmpparent, -1,
983
1006
                                LST_BUDGET_POS, id++,
985
1008
                                LST_BUDGET_NAME, name,
986
1009
                                LST_BUDGET_SPENT, tmp_spent[pos],
987
1010
                                LST_BUDGET_BUDGET, tmp_budget[pos],
988
 
                                LST_BUDGET_FULFILLED, (gint)(rawrate*100),
 
1011
                                //#2043223
 
1012
                                //LST_BUDGET_FULFILLED, (gint)(rawrate*100),
 
1013
                                LST_BUDGET_FULFILLED, fulfilled,
989
1014
                                LST_BUDGET_RESULT, result,
990
1015
                                LST_BUDGET_STATUS, status,
991
1016
                                -1);
999
1024
                        }
1000
1025
                }
1001
1026
 
1002
 
budnext:
1003
 
                DB( g_print(" -- next\n") );
 
1027
//budnext:
1004
1028
                list = g_list_next(list);
1005
1029
        }
1006
1030
 
2050
2074
}
2051
2075
 
2052
2076
 
2053
 
 
2054
2077
static void 
2055
2078
lst_repbud_cell_data_function_name (GtkTreeViewColumn *col, GtkCellRenderer *renderer, GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
2056
2079
{
2057
 
GtkTreePath *path;
2058
 
gchar *text = NULL;
2059
 
gint style  = PANGO_STYLE_NORMAL;
2060
 
 
2061
 
        gtk_tree_model_get(model, iter, 
2062
 
                LST_BUDGET_NAME, &text,
2063
 
                -1);
2064
 
 
2065
 
        path = gtk_tree_model_get_path(model, iter);
2066
 
        if( gtk_tree_path_get_depth(path) > 1 )
2067
 
        {
2068
 
                style = PANGO_STYLE_OBLIQUE;
2069
 
        }
2070
 
        gtk_tree_path_free(path);
2071
 
 
2072
 
        g_object_set(renderer, 
2073
 
                //"style-set", TRUE,
2074
 
                "style" , style, 
2075
 
                "text", text, 
2076
 
                NULL);
2077
 
 
2078
 
        g_free(text);
 
2080
gint tmpmode;
 
2081
 
 
2082
        tmpmode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(model), "mode-data"));
 
2083
 
 
2084
        if( tmpmode != 1 )
 
2085
        {
 
2086
        Category *item;
 
2087
        guint32 key;
 
2088
        gchar *markup = NULL;
 
2089
 
 
2090
                gtk_tree_model_get(model, iter, 
 
2091
                        LST_BUDGET_KEY, &key,
 
2092
                        -1);
 
2093
 
 
2094
                item = da_cat_get(key);
 
2095
                if(item)
 
2096
                {
 
2097
                        markup = item->typename;
 
2098
                }
 
2099
 
 
2100
                g_object_set(renderer, "markup", markup, NULL);
 
2101
        }
 
2102
        else
 
2103
        {
 
2104
        gchar *name;
 
2105
        
 
2106
                gtk_tree_model_get(model, iter, 
 
2107
                        LST_BUDGET_NAME, &name,
 
2108
                        -1);    
 
2109
 
 
2110
                g_object_set(renderer, "text", name, NULL);
 
2111
                g_free(name);
 
2112
        }
 
2113
 
2079
2114
}
2080
2115
 
2081
2116
 
2153
2188
                           GtkTreeModel      *model,
2154
2189
                           GtkTreeIter       *iter,
2155
2190
                           gpointer           user_data)
2156
 
   {
 
2191
{
 
2192
gdouble budget;
2157
2193
gint rawrate;
2158
2194
gchar buf[16];
2159
2195
 
2160
 
        gtk_tree_model_get(model, iter, 
 
2196
        gtk_tree_model_get(model, iter,
 
2197
                LST_BUDGET_BUDGET, &budget, 
2161
2198
                LST_BUDGET_FULFILLED, &rawrate,
2162
2199
                -1);
2163
2200
 
2164
 
        if(rawrate != 0.0)
 
2201
        if( hb_amount_equal(budget, 0.0) == FALSE )
2165
2202
        {
2166
2203
                g_snprintf(buf, sizeof(buf), "%d %%", rawrate);
2167
2204
                g_object_set(renderer, "text", buf, NULL);