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