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