Class: Account
- Inherits:
-
Entity
- Object
- Entity
- Account
- Defined in:
- lib/africompta/entities/account.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#account ⇒ Object
This is the parent account.
- #account=(a) ⇒ Object
- #accounts ⇒ Object
- #bool_to_s(b) ⇒ Object
- #data_set(f, v) ⇒ Object
- #delete(force = false) ⇒ Object
- #dump(mov = false) ⇒ Object
- #dump_rec(mov = false) ⇒ Object
- #get_archive(year = Date.today.year - 1, month = Date.today.month) ⇒ Object
- #get_archives ⇒ Object
- #get_path(sep = '::', p = '', first = true) ⇒ Object
-
#get_tree(depth = -1)) {|_self, depth| ... } ⇒ Object
This gets the tree under that account, breadth-first.
- #get_tree_debug(ind = '') {|_self| ... } ⇒ Object
-
#get_tree_depth {|_self| ... } ⇒ Object
This gets the tree under that account, depth-first.
- #is_empty ⇒ Object
- #listp_path(depth = -1)) ⇒ Object
-
#movements(from = nil, to = nil) ⇒ Object
Sort first regarding inverse date (newest first), then description, and finally the value.
- #movements_dst ⇒ Object
- #movements_src ⇒ Object
- #multiplier ⇒ Object
- #new_index ⇒ Object
- #parent ⇒ Object
- #parent=(a) ⇒ Object
- #path(sep = '::', p = '', first = true) ⇒ Object
- #path_id(sep = '::', p = '', first = true) ⇒ Object
- #print_pdf(file, recursive = false) ⇒ Object
- #print_pdf_document(pdf) ⇒ Object
- #set(name, desc, parent, multiplier = 1, users = [], keep_total = false) ⇒ Object
-
#set_child_multiplier_total(m, t) ⇒ Object
Be sure that all descendants have the same multiplier and keep_total.
-
#set_nochildmult(name, desc, parent = nil, multiplier = 1, users = [], keep_total = false) ⇒ Object
Sets different new parameters.
- #to_s(add_path = false) ⇒ Object
- #total_form ⇒ Object
- #update_total(precision = 3) ⇒ Object
Class Method Details
.total_form(v) ⇒ Object
993 994 995 996 997 |
# File 'lib/africompta/entities/account.rb', line 993 def self.total_form(v) (v.to_f * 1000 + 0.5).floor.to_s.tap do |s| :go while s.gsub!(/^([^.]*)(\ d)(?=(\ d { 3 })+)/, "\\1\\2,") end end |
Instance Method Details
#account ⇒ Object
This is the parent account
864 865 866 |
# File 'lib/africompta/entities/account.rb', line 864 def account Accounts.match_by_id(self.account_id) end |
#account=(a) ⇒ Object
868 869 870 |
# File 'lib/africompta/entities/account.rb', line 868 def account=(a) self.account_id = a.class == Account ? a.id : a end |
#accounts ⇒ Object
850 851 852 853 854 855 856 857 858 859 860 861 |
# File 'lib/africompta/entities/account.rb', line 850 def accounts # Some hand-optimized stuff. This would be written shorter like this: # Accounts.matches_by_account_id( self.id ) # But the code below is 3 times faster for some big data ret = [] Accounts.data.each { |k, v| if v[:account_id] == self.id ret.push Accounts.get_data_instance(k) end } ret end |
#bool_to_s(b) ⇒ Object
811 812 813 |
# File 'lib/africompta/entities/account.rb', line 811 def bool_to_s(b) b ? 'true' : 'false' end |
#data_set(f, v) ⇒ Object
673 674 675 676 677 678 679 680 681 |
# File 'lib/africompta/entities/account.rb', line 673 def data_set(f, v) if !@proxy.loading if !%w( _total _rev_index _global_id ).index(f.to_s) dputs(4) { "Updating index for field #{f.inspect} - #{@pre_init} - #{@proxy.loading}: #{v}" } new_index end end super(f, v) end |
#delete(force = false) ⇒ Object
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 |
# File 'lib/africompta/entities/account.rb', line 892 def delete(force = false) if not is_empty && force movements_src.each { |m| dputs(3) { "Deleting movement #{m.to_json}" } m.delete } end if is_empty dputs(2) { "Deleting account #{self.name}-#{self.id}" } self.account_id = nil self.deleted = true else dputs(1) { "Refusing to delete account #{name}" } return false end return true end |
#dump(mov = false) ⇒ Object
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 |
# File 'lib/africompta/entities/account.rb', line 956 def dump(mov = false) t = (self.keep_total ? 'K' : '.') + "#{self.multiplier.to_s.rjust(2, '+')}" acc_desc = ["**#{t}**#{self.path_id}#{self.deleted ? ' -- deleted' : ''}"] dputs(1) { acc_desc.first } acc_desc + if mov movements.collect { |m| m_desc = " #{m.to_json}" dputs(1) { m_desc } m_desc } else [] end end |
#dump_rec(mov = false) ⇒ Object
972 973 974 975 976 977 978 |
# File 'lib/africompta/entities/account.rb', line 972 def dump_rec(mov = false) ret = [] get_tree_depth { |a| ret.push a.dump(mov) } ret.flatten end |
#get_archive(year = Date.today.year - 1, month = Date.today.month) ⇒ Object
1007 1008 1009 |
# File 'lib/africompta/entities/account.rb', line 1007 def get_archive(year = Date.today.year - 1, month = Date.today.month) dputs(0) { 'Error: not implemented yet' } end |
#get_archives ⇒ Object
999 1000 1001 1002 1003 1004 1005 |
# File 'lib/africompta/entities/account.rb', line 999 def get_archives if archive = AccountRoot.archive archive.accounts.collect { |arch| Accounts.get_by_path("#{arch.path}::#{path.sub(/^Root::/, '')}") }.select { |a| a } end end |
#get_path(sep = '::', p = '', first = true) ⇒ Object
722 723 724 |
# File 'lib/africompta/entities/account.rb', line 722 def get_path(sep = '::', p = '', first = true) path(sep, p, first) end |
#get_tree(depth = -1)) {|_self, depth| ... } ⇒ Object
This gets the tree under that account, breadth-first
684 685 686 687 688 689 690 |
# File 'lib/africompta/entities/account.rb', line 684 def get_tree(depth = -1) yield self, depth return if depth == 0 accounts.sort { |a, b| a.name <=> b.name }.each { |a| a.get_tree(depth - 1) { |b| yield b, depth - 1 } } end |
#get_tree_debug(ind = '') {|_self| ... } ⇒ Object
700 701 702 703 704 705 706 |
# File 'lib/africompta/entities/account.rb', line 700 def get_tree_debug(ind = '') yield self dputs(1) { "get_tree_ #{ind}#{self.name}" } accounts.sort { |a, b| a.name <=> b.name }.each { |a| a.get_tree_debug("#{ind} ") { |b| yield b } } end |
#get_tree_depth {|_self| ... } ⇒ Object
This gets the tree under that account, depth-first
693 694 695 696 697 698 |
# File 'lib/africompta/entities/account.rb', line 693 def get_tree_depth accounts.sort { |a, b| a.name <=> b.name }.each { |a| a.get_tree_depth { |b| yield b } } yield self end |
#is_empty ⇒ Object
828 829 830 831 832 833 834 835 836 |
# File 'lib/africompta/entities/account.rb', line 828 def is_empty size = self.movements.select { |m| m.value.to_f != 0.0 }.size dputs(2) { "Account #{self.name} has #{size} non-zero elements" } dputs(4) { "Non-zero elements: #{movements.inspect}" } if size == 0 and self.accounts.size == 0 return true end return false end |
#listp_path(depth = -1)) ⇒ Object
980 981 982 983 984 985 986 987 |
# File 'lib/africompta/entities/account.rb', line 980 def listp_path(depth = -1) acc = [] get_tree(depth) { |a| acc.push [a.id, a.path] } dputs(3) { "Ret is #{acc.inspect}" } acc end |
#movements(from = nil, to = nil) ⇒ Object
Sort first regarding inverse date (newest first), then description, and finally the value
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
# File 'lib/africompta/entities/account.rb', line 773 def movements(from = nil, to = nil) dputs(5) { 'Account::movements' } movs = (movements_src + movements_dst) if (from != nil and to != nil) movs.delete_if { |m| (m.date < from || m.date > to) } dputs(3) { 'Rejected some elements' } end movs.delete_if { |m| m.value == 0 } sorted = movs.sort { |a, b| ret = 0 if a.date and b.date ret = a.date.to_s <=> b.date.to_s end if ret == 0 ret = a.rev_index <=> b.rev_index =begin if a.desc and b.desc ret = a.desc <=> b.desc end if ret == 0 if a.value and b.value ret = a.value.to_f <=> b.value.to_f end end =end end if ret ret * -1 else dputs(0) { "Error: Ret shouldn't be nil... #{self.path}" } 0 end } sorted end |
#movements_dst ⇒ Object
884 885 886 |
# File 'lib/africompta/entities/account.rb', line 884 def movements_dst Movements.matches_by_account_dst_id(self.id) end |
#movements_src ⇒ Object
880 881 882 |
# File 'lib/africompta/entities/account.rb', line 880 def movements_src Movements.matches_by_account_src_id(self.id) end |
#multiplier ⇒ Object
888 889 890 |
# File 'lib/africompta/entities/account.rb', line 888 def multiplier _multiplier.to_i end |
#new_index ⇒ Object
726 727 728 729 730 731 732 733 734 |
# File 'lib/africompta/entities/account.rb', line 726 def new_index() if !u_l = Users.match_by_name('local') dputs(0) { "Oups - user 'local' was not here: #{caller}" } u_l = Users.create('local') end self.rev_index = u_l.account_index u_l.account_index += 1 dputs(3) { "Index for account #{name} is #{rev_index}" } end |
#parent ⇒ Object
872 873 874 |
# File 'lib/africompta/entities/account.rb', line 872 def parent account end |
#parent=(a) ⇒ Object
876 877 878 |
# File 'lib/africompta/entities/account.rb', line 876 def parent=(a) self.account = a end |
#path(sep = '::', p = '', first = true) ⇒ Object
708 709 710 711 712 713 714 |
# File 'lib/africompta/entities/account.rb', line 708 def path(sep = '::', p='', first=true) if (acc = self.account) return acc.path(sep, p, false) + sep + self.name else return self.name end end |
#path_id(sep = '::', p = '', first = true) ⇒ Object
716 717 718 719 720 |
# File 'lib/africompta/entities/account.rb', line 716 def path_id(sep = '::', p='', first=true) (self.account ? "#{self.account.path_id(sep, p, false)}#{sep}" : '') + "#{self.name}-#{self.id}" end |
#print_pdf(file, recursive = false) ⇒ Object
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 |
# File 'lib/africompta/entities/account.rb', line 936 def print_pdf(file, recursive = false) Prawn::Document.generate(file, :page_size => 'A4', :page_layout => :landscape, :bottom_margin => 2.cm, :top_margin => 2.cm) do |pdf| if recursive get_tree_depth { |a| a.print_pdf_document(pdf) } else print_pdf_document(pdf) end pdf.repeat(:all, :dynamic => true) do pdf.draw_text self.path, :at => [0, -20] pdf.draw_text pdf.page_number, :at => [14.85.cm, -20] end end end |
#print_pdf_document(pdf) ⇒ Object
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 |
# File 'lib/africompta/entities/account.rb', line 910 def print_pdf_document(pdf) sum = 0 pdf.font_size 10 movs = movements.select { |m| m.value.abs >= 0.001 }.sort { |a, b| a.date <=> b.date } if movs.length > 0 header = [['', {:content => "#{path}", :colspan => 2, :align => :left}, {:content => "#{id}", :align => :right}], %w(Date Description Other # Value Sum).collect { |ch| {:content => ch, :align => :center} }] pdf.table(header + movs.collect { |m| other = m.get_other_account(self) value = m.get_value(self) [{:content => m.date.to_s, :align => :center}, m.desc, other.name, {:content => "#{other.id}", :align => :right}, {:content => "#{Account.total_form(value)}", :align => :right}, {:content => "#{Account.total_form(sum += value)}", :align => :right}] }, :header => true, :column_widths => [70, 400, 100, 40, 75, 75]) pdf.move_down(2.cm) end end |
#set(name, desc, parent, multiplier = 1, users = [], keep_total = false) ⇒ Object
764 765 766 767 768 769 |
# File 'lib/africompta/entities/account.rb', line 764 def set(name, desc, parent, multiplier = 1, users = [], keep_total = false) dputs(3) { "Going to set #{name}-#{parent}-#{multiplier}" } set_nochildmult(name, desc, parent, multiplier, users, keep_total) # All descendants shall have the same multiplier set_child_multiplier_total(multiplier, total) end |
#set_child_multiplier_total(m, t) ⇒ Object
Be sure that all descendants have the same multiplier and keep_total
839 840 841 842 843 844 845 846 847 848 |
# File 'lib/africompta/entities/account.rb', line 839 def set_child_multiplier_total(m, t) dputs(3) { "Setting multiplier from #{name} to #{m} and keep_total to #{t}" } self.multiplier = m self.keep_total = t return if not accounts accounts.each { |acc| acc.set_child_multiplier_total(m, t) } self end |
#set_nochildmult(name, desc, parent = nil, multiplier = 1, users = [], keep_total = false) ⇒ Object
Sets different new parameters.
752 753 754 755 756 757 758 759 760 761 762 |
# File 'lib/africompta/entities/account.rb', line 752 def set_nochildmult(name, desc, parent = nil, multiplier = 1, users = [], keep_total = false) self.name, self.desc, self.keep_total = name, desc, keep_total parent and self.account_id = parent # TODO: implement link between user-table and account-table # self.users = users ? users.join(":") : "" self.multiplier = multiplier self.keep_total = keep_total update_total self end |
#to_s(add_path = false) ⇒ Object
815 816 817 818 819 820 821 822 823 824 825 826 |
# File 'lib/africompta/entities/account.rb', line 815 def to_s(add_path = false) if account || true dputs(4) { "Account-desc: #{name.to_s}, #{global_id}, #{account_id.inspect}" } "#{desc}\r#{global_id}\t" + "#{sprintf('%.3f', total.to_f.round(3))}\t#{name.to_s}\t#{multiplier.to_i.to_s}\t" + (account_id ? ((account_id > 0) ? account.global_id.to_s : '') : '') + "\t#{bool_to_s(self.deleted)}" + "\t#{bool_to_s(self.keep_total)}" + (add_path ? "\t#{path}" : '') else 'nope' end end |
#total_form ⇒ Object
989 990 991 |
# File 'lib/africompta/entities/account.rb', line 989 def total_form Account.total_form(total) end |
#update_total(precision = 3) ⇒ Object
736 737 738 739 740 741 742 743 744 745 746 747 748 749 |
# File 'lib/africompta/entities/account.rb', line 736 def update_total(precision = 3) # Recalculate everything. dputs(4) { "Calculating total for #{self.path_id} with mult #{self.multiplier}" } self.total = (0.0).to_f dputs(4) { "Total before update is #{self.total} - #{self.total.class.name}" } self.movements.each { |m| v = m.get_value(self) dputs(5) { "Adding value #{v.inspect} to #{self.total.inspect}" } self.total = self.total.to_f + v.to_f dputs(5) { "And getting #{self.total.inspect}" } } self.total = self.total.to_f.round(precision) dputs(4) { "Final total is #{self.total} - #{self.total.class.name}" } end |