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