summaryrefslogtreecommitdiff
path: root/scripts/Reconciliation.pl
blob: 9c823a09ce69130e2d3a3f9c4cfa6516ba74e34c (plain)
  1. =pod
  2. =head1 NAME
  3. LedgerSMB::Scripts::Reconciliation - LedgerSMB class defining the Controller
  4. functions, template instantiation and rendering.
  5. =head1 SYOPSIS
  6. This module acts as the UI controller class for Reconciliation. It controls
  7. interfacing with the Core Logic and database layers.
  8. =head1 METHODS
  9. =cut
  10. # NOTE: This is a first draft modification to use the current parameter type.
  11. # It will certainly need some fine tuning on my part. Chris
  12. package LedgerSMB::Scripts::Reconciliation;
  13. use LedgerSMB::Template;
  14. use LedgerSMB::DBObject::Reconciliation;
  15. =pod
  16. =over
  17. =item display_report($self, $request, $user)
  18. Renders out the selected report given by the incoming variable report_id.
  19. Returns HTML, or raises an error from being unable to find the selected
  20. report_id.
  21. =back
  22. =cut
  23. sub display_report {
  24. my ($class, $request) = @_;
  25. my $recon = LedgerSMB::Employee->new(base => $request, copy => 'all');
  26. my $template = LedgerSMB::Template->new( user=>$user,
  27. template => "reconciliation/report.html", language => $user->{language},
  28. format=>'html'
  29. );
  30. my $report = $recon->get_report();
  31. my $total = $recon->get_total();
  32. $template->render({report=>$report, total=>$total, recon=>$recon});
  33. }
  34. =pod
  35. =over
  36. =item search($self, $request, $user)
  37. Renders out a list of meta-reports based on the search criteria passed to the
  38. search function.
  39. Meta-reports are report_id, date_range, and likely errors.
  40. Search criteria accepted are
  41. date_begin
  42. date_end
  43. account
  44. status
  45. =back
  46. =cut
  47. sub search {
  48. my ($class, $request) = @_;
  49. if ($request->type() eq "POST") {
  50. # WE HAS DATUMS
  51. # INTENTIONAL BAD PLURALIZATION OF LATIN
  52. my $template = LedgerSMB::Template->new(
  53. user => $user,
  54. template=>'reconciliation/search.html',
  55. language=>$user->{language},
  56. format=>'html'
  57. );
  58. return $template->render();
  59. } else {
  60. my $search = LedgerSMB::Reconciliation->new(base => $request, copy => 'all');
  61. my $results = $search->search();
  62. my $total = $search->total();
  63. my $template = LedgerSMB::Template->new( user => $user,
  64. template => 'reconciliation/report.html', language => $user->{language},
  65. format => 'html');
  66. return $template->render({report => $results, total => $total});
  67. }
  68. }
  69. =pod
  70. =over
  71. =item correct ($self, $request, $user)
  72. Requires report_id, entry_id.
  73. Correct is a strange one. Based on the type of transaction listed in the
  74. report, it will run one of several correction functions in the database.
  75. This is to prevent arbitrary editing of the database by unscrupulous users.
  76. =back
  77. =cut
  78. sub correct {
  79. my ($class, $request) = @_;
  80. if ($request->type() eq "POST") {
  81. my $recon = LedgerSMB::DBObject::Reconciliation->new(base => $request, copy => 'all');
  82. $recon->correct_entry();
  83. # Are we getting data?
  84. if ($recon->{corrected_id}) {
  85. my $template = LedgerSMB::Template->new( user => $user,
  86. template => 'reconciliation/report.html', language => $user->{language},
  87. format => 'html');
  88. $template->render( {
  89. corrected=> $recon->{corrected_id},
  90. report=> $recon->get_report(),
  91. total=> $recon->get_total()
  92. } );
  93. }
  94. else {
  95. # indicate we were unable to correct this entry, with the error code
  96. # spat back to us by the DB.
  97. my $template = LedgerSMB::Template->new( user => $user,
  98. template => 'reconciliation/report.html', language => $user->{language},
  99. format => 'html');
  100. $template->render( {
  101. recon => $recon,
  102. report => $recon->get_report(),
  103. total => $recon->get_total()
  104. } );
  105. }
  106. }
  107. else {
  108. # We are not getting data sent
  109. # ergo, we render out stuff.
  110. if ($request->{report_id} && $request->{entry_id}) {
  111. # draw the editor interface.
  112. my $template = LedgerSMB::Template->new(
  113. user=>$user,
  114. template=>"reconciliation/correct.html",
  115. language=> $user->{language},
  116. format=>'html'
  117. );
  118. my $recon = LedgerSMB::DBObject::Reconciliation->new(base=>$request, copy=>'all');
  119. $template->render($recon->details($request->{report_id}));
  120. }
  121. elsif ($request->{report_id}) {
  122. my $template = LedgerSMB::Template->new(
  123. user=>$user,
  124. template=>"reconciliation/correct.html",
  125. language=> $user->{language},
  126. format=>'html'
  127. );
  128. $class->display_report();
  129. }
  130. }
  131. }
  132. =pod
  133. =over
  134. =item new_report ($self, $request, $user)
  135. Creates a new report, from a selectable set of bank statements that have been
  136. received (or can be received from, depending on implementation)
  137. Allows for an optional selection key, which will return the new report after
  138. it has been created.
  139. =back
  140. =cut
  141. sub new_report {
  142. my ($class, $request) = @_;
  143. # how are we going to allow this to be created? Grr.
  144. # probably select a list of statements that are available to build
  145. # reconciliation reports with.
  146. # This should do some fun stuff.
  147. my $template;
  148. my $return;
  149. if ($request->type() eq "POST") {
  150. # We can assume that we're doing something useful with new data.
  151. # We can also assume that we've got a file.
  152. my $recon = LedgerSMB::DBObject::Reconciliation->new(base => $request, copy => 'all');
  153. # $self is expected to have both the file handling logic, as well as
  154. # the logic to load the processing module.
  155. # Why isn't this testing for errors?
  156. my ($report_id, $entries) = $recon->new_report($recon->import_file());
  157. if ($recon->is_error()) {
  158. $template = LedgerSMB::Template->new(
  159. user=>$user,
  160. template=> 'reconciliation/upload.html',
  161. language=>$user->{language},
  162. format=>'html'
  163. );
  164. return $template->render({error=>$recon->error()});
  165. }
  166. $template = LedgerSMB::Template->new(
  167. user=> $user,
  168. template => 'reconciliation/new_report.html',
  169. language => $user->{language},
  170. format=>'html'
  171. );
  172. return $template->render(
  173. {
  174. entries=>$entries,
  175. report_id=>$report_id
  176. }
  177. );
  178. }
  179. else {
  180. # we can assume we're to generate the "Make a happy new report!" page.
  181. $template = LedgerSMB::Template->new(
  182. user => $user,
  183. template => 'reconciliation/upload.html',
  184. language => $user->{language},
  185. format => 'html'
  186. );
  187. return $template->render();
  188. }
  189. return undef;
  190. }
  191. =pod
  192. =over
  193. =item approve ($self, $request, $user)
  194. Requires report_id
  195. Approves the given report based on id. Generally, the roles should be
  196. configured so as to disallow the same user from approving, as created the report.
  197. Returns a success page on success, returns a new report on failure, showing
  198. the uncorrected entries.
  199. =back
  200. =cut
  201. sub approve {
  202. my ($class, $request) = @_;
  203. # Approve will also display the report in a blurred/opaqued out version,
  204. # with the controls removed/disabled, so that we know that it has in fact
  205. # been cleared. This will also provide for return-home links, auditing,
  206. # etc.
  207. if ($request->type() eq "POST") {
  208. # we need a report_id for this.
  209. my $recon = LedgerSMB::DBObject::Reconciliation->new(base => request, copy=> 'all');
  210. my $template;
  211. my $code = $recon->approve($request->{report_id});
  212. if ($code == 0) {
  213. $template = LedgerSMB::Template->new( user => $user,
  214. template => 'reconciliation/approve.html', language => $user->{language},
  215. format => 'html');
  216. return $template->render();
  217. }
  218. else {
  219. # failure case
  220. $template = LedgerSMB::Template->new(
  221. user => $user,
  222. template => 'reconciliation/report.html',
  223. language => $user->{language},
  224. format => 'html');
  225. return $template->render(
  226. {
  227. entries=>$recon->get_report($request->{report_id}),
  228. total=>$recon->get_total($request->{report_id}),
  229. error_code => $code
  230. }
  231. );
  232. }
  233. }
  234. else {
  235. return $class->display_report($request);
  236. }
  237. }
  238. =pod
  239. =over
  240. =item corrections ($self, $request, $user)
  241. Requires report_id and entry_id.
  242. Loads the selected entry id and all corrections associated with it. If there
  243. aren't any corrections, it will display "no corrections found".
  244. =back
  245. =cut
  246. sub corrections {
  247. my ($class, $request) = @_;
  248. # Load the corrections for a given report & entry id.
  249. # possibly should use a "micro" popup window?
  250. my $recon = LedgerSMB::DBObject::Reconciliation->new(base => request, copy=> 'all');
  251. my $template;
  252. $template = LedgerSMB::Template->new( user => $user,
  253. template => 'reconciliation/corrected.html', language => $user->{language},
  254. format => 'html');
  255. return $template->render(
  256. {
  257. corrections=>$recon->get_corrections(),
  258. entry=>$recon->entry($self->{report_id}, $self->{entry_id})
  259. }
  260. );
  261. }
  262. =pod
  263. =over
  264. =item pending ($self, $request, $user)
  265. Requires {date} and {month}, to handle the month-to-month pending transactions
  266. in the database. No mechanism is provided to grab ALL pending transactions
  267. from the acc_trans table.
  268. =back
  269. =cut
  270. sub pending {
  271. my ($class, $request) = @_;
  272. my $recon = LedgerSMB::DBObject::Reconciliation->new(base=>request, copy=>'all');
  273. my $template;
  274. $template= LedgerSMB::Template->new(
  275. user => $user,
  276. template=>'reconciliation/pending.html',
  277. language=>$user->{language},
  278. format=>'html'
  279. );
  280. if ($request->type() eq "POST") {
  281. return $template->render(
  282. {
  283. pending=>$recon->get_pending($request->{year}."-".$request->{month});
  284. }
  285. );
  286. }
  287. else {
  288. return $template->render();
  289. }
  290. }
  291. # eval { do "scripts/custom/Reconciliation.pl" };
  292. 1;
  293. =pod
  294. =head1 Copyright (C) 2007, The LedgerSMB core team.
  295. This file is licensed under the Gnu General Public License version 2, or at your
  296. option any later version. A copy of the license should have been included with
  297. your software.
  298. =cut