summaryrefslogtreecommitdiff
path: root/scripts/vouchers.pl
blob: d6ed9b23e4cce685458e332f9762fa7f600ad10a (plain)
  1. #!/usr/bin/perl
  2. # This file is copyright (C) 2007the LedgerSMB core team and licensed under
  3. # the GNU General Public License. For more information please see the included
  4. # LICENSE and COPYRIGHT files
  5. # THIS FILE NEEDS POD
  6. package LedgerSMB::Scripts::vouchers;
  7. our $VERSION = '0.1';
  8. $menufile = "menu.ini";
  9. use LedgerSMB::Batch;
  10. use LedgerSMB::Voucher;
  11. use LedgerSMB::Template;
  12. use strict;
  13. # custom_batch_types hash provides hooks for handling additional batch types
  14. # beyond the default types. Entries can be added in a custom file.
  15. # Each entry is a hash, keyed by name, with the following keys:
  16. # * map_to int (maps to another type, not needed for new types in batch_class
  17. # table)
  18. # * select_method (maps to the selection stored proc)
  19. #
  20. # for example:
  21. # $custom_batch_types->{ap_sample} =
  22. # {map_to => 1,
  23. # select_method => 'custom_sample_ap_select'};
  24. #
  25. # --CT
  26. our $custom_batch_types = {};
  27. eval { do "scripts/custom/vouchers.pl"};
  28. sub create_batch {
  29. my ($request) = @_;
  30. $request->{hidden} = [
  31. {name => "batch_type", value => $request->{batch_type}},
  32. ];
  33. my $batch = LedgerSMB::Batch->new({base => $request});
  34. $batch->{class_id} = $batch->get_class_id($batch->{batch_type});
  35. $batch->get_search_results({mini => 1});
  36. my $template = LedgerSMB::Template->new(
  37. user =>$request->{_user},
  38. locale => $request->{_locale},
  39. path => 'UI',
  40. template => 'create_batch',
  41. format => 'HTML'
  42. );
  43. $template->render($batch);
  44. }
  45. sub create_vouchers {
  46. my ($request) = shift @_;
  47. my $batch = LedgerSMB::Batch->new({base => $request});
  48. $batch->{batch_class} = $request->{batch_type};
  49. $batch->create;
  50. add_vouchers($batch);
  51. }
  52. sub add_vouchers {
  53. # This function is not safe for caching as long as the scripts are in bin.
  54. # This is because these scripts import all functions into the *current*
  55. # namespace. People using fastcgi and modperl should *not* cache this
  56. # module at the moment. -- CT
  57. # Also-- request is in 'our' scope here due to the redirect logic.
  58. our ($request) = shift @_;
  59. use LedgerSMB::Form;
  60. my $batch = LedgerSMB::Batch->new({base => $request});
  61. our $vouchers_dispatch =
  62. {
  63. ap => {script => 'bin/ap.pl', function => sub {add()}},
  64. ar => {script => 'bin/ar.pl', function => sub {add()}},
  65. gl => {script => 'bin/gl.pl', function => sub {add()}},
  66. receipt => {script => 'scripts/payment.pl',
  67. function => sub {
  68. my ($request) = @_;
  69. $request->{account_class} = 2;
  70. LedgerSMB::Scripts::payment::payments($request);
  71. }},
  72. payment => {script => 'scripts/payment.pl',
  73. function => sub {
  74. my ($request) = @_;
  75. $request->{account_class} = 1;
  76. LedgerSMB::Scripts::payment::payments($request);
  77. }},
  78. payment_reversal => {
  79. script => 'scripts/payment.pl',
  80. function => sub {
  81. my ($request) = @_;
  82. $request->{account_class} = 1;
  83. LedgerSMB::Scripts::payment::get_search_criteria($request, $custom_batch_types);
  84. }},
  85. receipt_reversal => {
  86. script => 'scripts/payment.pl',
  87. function => sub {
  88. my ($request) = @_;
  89. $request->{account_class} = 2;
  90. LedgerSMB::Scripts::payment::get_search_criteria($request, $custom_batch_types);
  91. }},
  92. };
  93. our $form = new Form;
  94. our %myconfig = ();
  95. %myconfig = %{$request->{_user}};
  96. our $locale = $request->{_locale};
  97. for (keys %$request){
  98. $form->{$_} = $request->{$_};
  99. }
  100. $form->{batch_id} = $batch->{id};
  101. $form->{approved} = 0;
  102. $form->{transdate} = $request->{batch_date};
  103. $request->{batch_id} = $batch->{id};
  104. $request->{approved} = 0;
  105. $request->{transdate} = $request->{batch_date};
  106. my $script = $vouchers_dispatch->{$request->{batch_type}}{script};
  107. $form->{script} = $script;
  108. $form->{script} =~ s|.*/||;
  109. delete $form->{id};
  110. delete $request->{id};
  111. if ($script =~ /^bin/){
  112. # Note that the line below is generally considered incredibly bad form.
  113. # However, the code we are including is going to require it for now.
  114. # -- CT
  115. { no strict; no warnings 'redefine'; do $script; }
  116. } elsif ($script =~ /scripts/) {
  117. # Maybe we should move this to a require statement? --CT
  118. { do $script }
  119. }
  120. $vouchers_dispatch->{$request->{batch_type}}{function}($request);
  121. }
  122. sub search_batch {
  123. my ($request) = @_;
  124. my $batch_request = LedgerSMB::Batch->new(base => $request);
  125. $batch_request->get_search_criteria($custom_batch_types);
  126. my $template = LedgerSMB::Template->new(
  127. user => $request->{_user},
  128. locale => $request->{_locale},
  129. path => 'UI/batch',
  130. template => 'filter',
  131. format => 'HTML',
  132. );
  133. $template->render($batch_request);
  134. }
  135. sub list_batches {
  136. my ($request) = @_;
  137. my $batch = LedgerSMB::Batch->new(base => $request);
  138. my @search_results = $batch->get_search_results({custom_types => $custom_batch_types});
  139. $batch->{script} = "vouchers.pl";
  140. my @columns =
  141. qw(select id control_code description transaction_total payment_total);
  142. my $base_href = "vouchers.pl";
  143. my $search_href = "$base_href?action=list_batches";
  144. my $batch_href = "$base_href?action=get_batch";
  145. for my $key (
  146. qw(class_id approved created_by description amount_gt amount_lt)
  147. ){
  148. $search_href .= "&$key=$batch->{key}";
  149. }
  150. my %column_heading = (
  151. 'select' => $batch->{_locale}->text('Select'),
  152. transaction_total => {
  153. text => $batch->{_locale}->text('AR/AP/GL Total'),
  154. href => "$search_href&order_by=transaction_total"
  155. },
  156. payment_total => {
  157. text => $batch->{_locale}->text('Paid/Received Total'),
  158. href => "$search_href&order_by=payment_total"
  159. },
  160. description => {
  161. text => $batch->{_locale}->text('Description'),
  162. href => "$search_href&order_by=description"
  163. },
  164. control_code => {
  165. text => $batch->{_locale}->text('Batch Number'),
  166. href => "$search_href&order_by=control_code"
  167. },
  168. id => {
  169. text => $batch->{_locale}->text('ID'),
  170. href => "$search_href&order_by=control_code"
  171. },
  172. );
  173. my $count = 0;
  174. my @rows;
  175. for my $result (@search_results){
  176. ++$count;
  177. $batch->{"row_$count"} = $result->{id};
  178. push @rows, {
  179. 'select' => {
  180. input => {
  181. type => 'checkbox',
  182. value => 1,
  183. name => "batch_$result->{id}"
  184. }
  185. },
  186. transaction_total => $batch->format_amount(
  187. amount => $result->{transaction_total}
  188. ),
  189. payment_total => $batch->format_amount (
  190. amount => $result->{payment_total}
  191. ),
  192. description => $result->{description},
  193. control_code => {
  194. text => $result->{control_code},
  195. href => "$batch_href&batch_id=$result->{id}",
  196. },
  197. id => $result->{id},
  198. };
  199. }
  200. $batch->{rowcount} = $count;
  201. my $template = LedgerSMB::Template->new(
  202. user => $request->{_user},
  203. locale => $request->{_locale},
  204. path => 'UI',
  205. template => 'form-dynatable',
  206. format => ($batch->{format}) ? $batch->{format} : 'HTML',
  207. );
  208. my $hiddens = $batch->take_top_level();
  209. $batch->{rowcount} = "$count";
  210. delete $batch->{search_results};
  211. $template->render({
  212. form => $batch,
  213. columns => \@columns,
  214. heading => \%column_heading,
  215. rows => \@rows,
  216. hiddens => $hiddens,
  217. buttons => [{
  218. name => 'action',
  219. type => 'submit',
  220. text => $request->{_locale}->text('Post'),
  221. value => 'batch_approve',
  222. class => 'submit',
  223. },{
  224. name => 'action',
  225. type => 'submit',
  226. text => $request->{_locale}->text('Delete'),
  227. value => 'batch_delete',
  228. class => 'submit',
  229. }]
  230. });
  231. }
  232. sub get_batch {
  233. my ($request) = @_;
  234. my $batch = LedgerSMB::Batch->new(base => $request);
  235. $batch->{script} = 'vouchers.pl';
  236. my $rows = [];
  237. $batch->{id} ||= $batch->{batch_id};
  238. # $batch->get;
  239. my @vouchers = $batch->list_vouchers;
  240. my $base_href = "vouchers.pl?action=get_batch&batch_id=$batch->{batch_id}";
  241. my @columns = qw(selected id description batch_class reference amount date);
  242. my $heading = {
  243. selected => $request->{_locale}->text('Selected'),
  244. id => {
  245. text => $request->{_locale}->text('ID'),
  246. href => "$base_href&order_by=id"
  247. },
  248. description => {
  249. href => "$base_href&order_by=description",
  250. text => $request->{_locale}->text('Description'),
  251. },
  252. batch_class => {
  253. text => $request->{_locale}->text('Class'),
  254. href => "$base_href&order_by=class"
  255. },
  256. amount => {
  257. text => $request->{_locale}->text('Amount'),
  258. href => "$base_href&order_by=amount"
  259. },
  260. reference => {
  261. text => $request->{_locale}->text('Source/Reference'),
  262. href => "$base_href&order_by=reference"
  263. },
  264. date => {
  265. text => $request->{_locale}->text('Date'),
  266. href => "$base_href&order_by=date"
  267. }
  268. };
  269. my $classcount;
  270. my $count = 1;
  271. for my $row (@vouchers) {
  272. $classcount = ($classcount + 1) % 2;
  273. $classcount ||= 0;
  274. push @$rows, {
  275. description => $row->{description},
  276. id => $row->{id},
  277. batch_class => $row->{batch_class},
  278. amount => $batch->format_amount(amount => $row->{amount}),
  279. date => $row->{transaction_date},
  280. reference => $row->{reference},
  281. i => "$classcount",
  282. selected => {
  283. input => {
  284. type => 'checkbox',
  285. name => "voucher_$row->{id}",
  286. value => "1"
  287. }
  288. }
  289. };
  290. $batch->{"row_$count"} = $row->{id};
  291. ++$count;
  292. }
  293. $batch->{rowcount} = $count;
  294. $batch->{title} = "Batch ID: $batch->{batch_id}";
  295. my $template = LedgerSMB::Template->new(
  296. user => $request->{_user},
  297. locale => $request->{_locale},
  298. path => 'UI',
  299. template => 'form-dynatable',
  300. format => ($batch->{format}) ? $batch->{format} : 'HTML',
  301. );
  302. my $hiddens = $batch->take_top_level();
  303. $template->render({
  304. form => $batch,
  305. columns => \@columns,
  306. heading => $heading,
  307. rows => $rows,
  308. hiddens => $hiddens,
  309. buttons => [{
  310. name => 'action',
  311. type => 'submit',
  312. text => $request->{_locale}->text('Post Batch'),
  313. value => 'batch_approve',
  314. class => 'submit',
  315. },{
  316. name => 'action',
  317. type => 'submit',
  318. text => $request->{_locale}->text('Delete Batch'),
  319. value => 'batch_delete',
  320. class => 'submit',
  321. },{
  322. name => 'action',
  323. type => 'submit',
  324. text => $request->{_locale}->text('Delete Vouchers'),
  325. value => 'voucher_delete',
  326. class => 'submit',
  327. }]
  328. });
  329. }
  330. sub list_batches_batch_delete {
  331. batch_delete(@_);
  332. }
  333. sub list_batches_batch_approve {
  334. batch_approve(@_);
  335. }
  336. sub get_batch__batch_approve {
  337. batch_approve(@_);
  338. }
  339. sub get_batch_voucher_delete {
  340. my ($request) = @_;
  341. my $batch = LedgerSMB::Batch->new(base => $request);
  342. for my $count (1 .. $batch->{rowcount}){
  343. my $voucher_id = $batch->{"row_$count"};
  344. next unless $batch->{"voucher_$voucher_id"};
  345. $batch->delete_voucher($voucher_id);
  346. }
  347. search_batch($request);
  348. }
  349. sub batch_approve {
  350. my ($request) = @_;
  351. my $batch = LedgerSMB::Batch->new(base => $request);
  352. for my $count (1 .. $batch->{rowcount}){
  353. next unless $batch->{"batch_" . $batch->{"row_$count"}};
  354. $batch->{batch_id} = $batch->{"row_$count"};
  355. $batch->post;
  356. }
  357. search_batch($request);
  358. }
  359. sub batch_delete {
  360. my ($request) = @_;
  361. my $batch = LedgerSMB::Batch->new(base => $request);
  362. for my $count (1 .. $batch->{rowcount}){
  363. next unless $batch->{"batch_" . $batch->{"row_$count"}};
  364. $batch->{batch_id} = $batch->{"row_$count"};
  365. $batch->delete;
  366. }
  367. search_batch($request);
  368. }
  369. eval { do "scripts/custom/vouchers.pl"};
  370. 1;