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