summaryrefslogtreecommitdiff
path: root/LedgerSMB/DBObject/Reconciliation.pm
blob: 1b5e9b973f4864b0a90069d8efe4a878611c3255 (plain)
  1. =pod
  2. =head1 NAME
  3. LedgerSMB::DBObject::Reconciliation - LedgerSMB class defining the core
  4. database interaction logic for Reconciliation.
  5. =head1 SYOPSIS
  6. This module creates object instances based on LedgerSMB's in-database ORM.
  7. =head1 METHODS
  8. =over
  9. =item new ($class, base => $LedgerSMB::hash)
  10. This is the base constructor for all child classes. It must be used with base
  11. argument because this is necessary for database connectivity and the like.
  12. Of course the base object can be any object that inherits LedgerSMB, so you can
  13. use any subclass of that. The per-session dbh is passed between the objects
  14. this way as is any information that is needed.
  15. =item reconcile($self, $total, $month, $entries)
  16. Accepts the total balance, as well as a list of all entries from the bank
  17. statement as an array reference, and generates the pending report from
  18. this list.
  19. The first entry is always the total balance of the general ledger as
  20. compared to the balance held by the bank.
  21. Month is taken to be the date that the statement as represented by Entries
  22. is applicable to.
  23. Returns the new report ID. || An arrayref of entries.
  24. =item approve($self,$reportid)
  25. Approves the pending report $reportid.
  26. Checks for error codes in the pending report, and approves the report if none
  27. are found.
  28. Limitations: The creating user may not approve the report.
  29. Returns 1 on success.
  30. =item correct_entry($self, $report_id, $source_control_number, $new_balance)
  31. If the given entry $source_control_number in the report $report_id has an error
  32. code, the entry will be updated with $new_balance, and the error code
  33. recomputed.
  34. Returns the error code assigned to this entry.
  35. 0 for success
  36. 1 for found in general ledger, but does not match $new_balance
  37. 2 $source_control_number cannot be found in the general ledger
  38. =item get_report($self, $report_id)
  39. Collects all the rows from the database in the given report. Returns an
  40. arrayref of entries in the table for the given report_id.
  41. Returns undef in the event of no records found.
  42. =item get_corrections($self, $report_id, $entry_id)
  43. Returns the corrections made for the selected report and entry.
  44. Returns undef in the event of no corrections found.
  45. =item entry ($self,$report_id,$entry_id)
  46. Returns a single entry from the pending reports table, either cleared or
  47. uncleared.
  48. =back
  49. =head1 Copyright (C) 2007, The LedgerSMB core team.
  50. This file is licensed under the Gnu General Public License version 2, or at your
  51. option any later version. A copy of the license should have been included with
  52. your software.
  53. =cut
  54. package LedgerSMB::DBObject::Reconciliation;
  55. use base qw(LedgerSMB::DBObject);
  56. use LedgerSMB::DBObject;
  57. use LedgerSMB::Reconciliation::CSV;
  58. # don't need new
  59. sub import_file {
  60. my $self = shift @_;
  61. # We need to know what the format is, for the text file. We should have a set of formats
  62. # for the given file, listed in the DB, or based on modules available.
  63. #
  64. # Probably based on modules.
  65. # my $module = 'LedgerSMB/Reconciliaton/CSV/'.$self->{file_format};
  66. # require $module."pm";
  67. # my $obj_name = $module;
  68. # $obj_name =~ s/\//::/g;
  69. # my $parser = $obj_name::new(base=>$self);
  70. # $self->filename is currently a lie. There's no facility in the LSMB
  71. # design to accomadate an uploaded file.
  72. my $csv = LedgerSMB::Reconciliation::CSV->new(base=>$self);
  73. $csv->process();
  74. return $self->{entries};
  75. }
  76. sub approve {
  77. my $self = shift @_;
  78. # the user should be embedded into the $self object.
  79. my $report_id = shift @_;
  80. my $code = $self->exec_method(funcname=>'report_approve', args=>[$report_id]); # user
  81. if ($code == 0) { # no problem.
  82. return $code;
  83. }
  84. # this is destined to change as we figure out the Error system.
  85. elsif ($code == 99) {
  86. $self->error("User $self->{user}->{name} cannot approve report, must be a different user.");
  87. }
  88. }
  89. sub new_report {
  90. my $self = shift @_;
  91. my $total = shift @_;
  92. my $month = shift @_;
  93. my $entries = shift @_; # expects an arrayref.
  94. # Total is in here somewhere, too
  95. # gives us a report ID to insert with.
  96. my $report_id = $self->exec_method(funcname=>'reconciliation__new_report_id');
  97. # Now that we have this, we need to create the internal report representation.
  98. # Ideally, we OUGHT to not return anything here, save the report number.
  99. unshift @{$entries}, {
  100. scn => -1,
  101. balance=> $total,
  102. old_balance=> $self->exec_method(funcname=>'reconciliation__current_balance'),
  103. date=>$month
  104. };
  105. for my $entry ( @{$entries} ) {
  106. # Codes:
  107. # 0 is success
  108. # 1 is found, but mismatch
  109. # 2 is not found
  110. # in_scn INT,
  111. #in_amount INT,
  112. #in_account INT,
  113. #in_user TEXT,
  114. #in_date TIMESTAMP
  115. $code = $self->exec_method(
  116. funcname=>'reconciliation__add_entry',
  117. args=>[
  118. $report_id,
  119. $entry->{scn},
  120. $entry->{amount}, # needs leading 0's trimmed.
  121. $entry->{account},
  122. $self->{user},
  123. $self->{date}
  124. ]
  125. );
  126. $entry{report_id} = $report_id;
  127. }
  128. $self->exec_method(funcname=>'reconciliation__pending_transactions', args=>[$report_id, $date]);
  129. return ($report_id, $entries); # returns the report ID.
  130. }
  131. sub correct_entry {
  132. my $self = shift @_;
  133. my $report_id = $self->{report_id}; # shift @_;
  134. my $scn = $self->{id}; #shift @_;
  135. my $new_amount = $self->{new_amount}; #shift @_;
  136. # correct should return the new code value - whether or not it actually "matches"
  137. my $code = $self->exec_method(
  138. funcname=>'reconciliation__correct',
  139. args=>[$report_id, $scn, $new_amount]
  140. );
  141. return $code[0]->{'correct'};
  142. }
  143. sub get_report {
  144. my $self = shift @_;
  145. return $self->exec_method(funcname=>'reconciliation__report', args=>[$self->{report_id}]);
  146. }
  147. sub get_corrections {
  148. my $self = shift @_;
  149. return $self->exec_method(
  150. funcname=>'reconciliation__corrections',
  151. args=>[$self->{report_id}, $self->{entry_id}]
  152. );
  153. }
  154. sub entry {
  155. my $self = shift @_;
  156. return $self->exec_method(
  157. funcname=>'reconciliation__single_entry',
  158. args=>[$self->{report_id}, $self->{entry_id}]
  159. );
  160. }
  161. sub search {
  162. my $self = shift @_;
  163. my $type = shift;
  164. return $self->exec_method(
  165. funcname=>'reconciliation__search',
  166. args=>[$self->{date_begin}, $self->{date_end}, $self->{account}, $self->{status}]
  167. );
  168. }
  169. sub get_pending {
  170. my $self = shift @_;
  171. return $self->exec_method(
  172. funcname=>'reconciliation__pending',
  173. args=>[$self->{month}]
  174. );
  175. }
  176. sub get_report_list {
  177. my $self = shift @_;
  178. return $self->exec_method(
  179. funcname=>'reconciliation__report_list',
  180. args=>[$self->{account},$self->{report}]
  181. );
  182. }
  183. sub get_accounts {
  184. my $self = shift @_;
  185. return $self->exec_method(
  186. funcname=>'reconciliation__account_list',
  187. );
  188. }
  189. 1;