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