summaryrefslogtreecommitdiff
path: root/scripts/vouchers.pl
blob: 4a3c1129b2273fc4dd3b241b8c3d0c84bb090efc (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. };
  60. our $form = new Form;
  61. our $locale = $request->{_locale};
  62. for (keys %$request){
  63. $form->{$_} = $request->{$_};
  64. }
  65. $form->{batch_id} = $batch->{id};
  66. $form->{approved} = 0;
  67. $form->{transdate} = $request->{batch_date};
  68. $request->{batch_id} = $batch->{id};
  69. $request->{approved} = 0;
  70. $request->{transdate} = $request->{batch_date};
  71. my $script = $vouchers_dispatch->{$request->{batch_type}}{script};
  72. $form->{script} = $script;
  73. $form->{script} =~ s|.*/||;
  74. if ($script =~ /^bin/){
  75. # Note that the line below is generally considered incredibly bad form.
  76. # However, the code we are including is going to require it for now.
  77. # -- CT
  78. { no strict; no warnings 'redefine'; do $script; }
  79. } elsif ($script =~ /scripts/) {
  80. # Maybe we should move this to a require statement? --CT
  81. { do $script }
  82. }
  83. $vouchers_dispatch->{$request->{batch_type}}{function}($request);
  84. }
  85. sub search_batch {
  86. my ($request) = @_;
  87. my $batch_request = LedgerSMB::Batch->new(base => $request);
  88. $batch_request->get_search_criteria();
  89. my $template = LedgerSMB::Template->new(
  90. user => $request->{_user},
  91. locale => $request->{_locale},
  92. path => 'UI/batch',
  93. template => 'filter',
  94. format => 'HTML',
  95. );
  96. $template->render($batch_request);
  97. }
  98. sub list_batches {
  99. my ($request) = @_;
  100. my $batch = LedgerSMB::Batch->new(base => $request);
  101. my @search_results = $batch->get_search_results;
  102. $batch->{script} = "vouchers.pl";
  103. my @columns =
  104. qw(select id control_code description transaction_total payment_total);
  105. my $base_href = "vouchers.pl";
  106. my $search_href = "$base_href?action=list_batches";
  107. my $batch_href = "$base_href?action=get_batch";
  108. for my $key (
  109. qw(class_id approved created_by description amount_gt amount_lt)
  110. ){
  111. $search_href .= "&$key=$batch->{key}";
  112. }
  113. my %column_heading = (
  114. 'select' => $batch->{_locale}->text('Select'),
  115. transaction_total => {
  116. text => $batch->{_locale}->text('AR/AP/GL Total'),
  117. href => "$search_href&order_by=transaction_total"
  118. },
  119. payment_total => {
  120. text => $batch->{_locale}->text('Paid/Received Total'),
  121. href => "$search_href&order_by=payment_total"
  122. },
  123. description => {
  124. text => $batch->{_locale}->text('Description'),
  125. href => "$search_href&order_by=description"
  126. },
  127. control_code => {
  128. text => $batch->{_locale}->text('Batch Number'),
  129. href => "$search_href&order_by=control_code"
  130. },
  131. id => {
  132. text => $batch->{_locale}->text('ID'),
  133. href => "$search_href&order_by=control_code"
  134. },
  135. );
  136. my $count = 0;
  137. my @rows;
  138. for my $result (@search_results){
  139. ++$count;
  140. $batch->{"row_$count"} = $result->{id};
  141. push @rows, {
  142. 'select' => {
  143. input => {
  144. type => 'checkbox',
  145. value => 1,
  146. name => "batch_$result->{id}"
  147. }
  148. },
  149. transaction_total => $batch->format_amount(
  150. amount => $result->{transaction_total}
  151. ),
  152. payment_total => $batch->format_amount (
  153. amount => $result->{payment_total}
  154. ),
  155. description => $result->{description},
  156. control_code => {
  157. text => $result->{control_code},
  158. href => "$batch_href&batch_id=$result->{id}",
  159. },
  160. id => $result->{id},
  161. };
  162. }
  163. $batch->{rowcount} = $count;
  164. my $template = LedgerSMB::Template->new(
  165. user => $request->{_user},
  166. locale => $request->{_locale},
  167. path => 'UI',
  168. template => 'form-dynatable',
  169. format => ($batch->{format}) ? $batch->{format} : 'HTML',
  170. );
  171. my $hiddens = $batch->take_top_level();
  172. $batch->{rowcount} = "$count";
  173. delete $batch->{search_results};
  174. $template->render({
  175. form => $batch,
  176. columns => \@columns,
  177. heading => \%column_heading,
  178. rows => \@rows,
  179. hiddens => $hiddens,
  180. buttons => [{
  181. name => 'action',
  182. type => 'submit',
  183. text => $request->{_locale}->text('Post'),
  184. value => 'batch_approve',
  185. class => 'submit',
  186. },{
  187. name => 'action',
  188. type => 'submit',
  189. text => $request->{_locale}->text('Delete'),
  190. value => 'batch_delete',
  191. class => 'submit',
  192. }]
  193. });
  194. }
  195. sub get_batch {
  196. my ($request) = @_;
  197. my $batch = LedgerSMB::Batch->new(base => $request);
  198. my $rows = [];
  199. $batch->{id} ||= $batch->{batch_id};
  200. # $batch->get;
  201. my @vouchers = $batch->list_vouchers;
  202. my $base_href = "vouchers.pl?action=get_batch&batch_id=$batch->{batch_id}";
  203. my @columns = qw(id description batch_class reference amount date);
  204. my $heading = {
  205. id => {
  206. text => $request->{_locale}->text('ID'),
  207. href => "$base_href&order_by=id"
  208. },
  209. description => {
  210. href => "$base_href&order_by=description",
  211. text => $request->{_locale}->text('Description'),
  212. },
  213. batch_class => {
  214. text => $request->{_locale}->text('Class'),
  215. href => "$base_href&order_by=class"
  216. },
  217. amount => {
  218. text => $request->{_locale}->text('Amount'),
  219. href => "$base_href&order_by=amount"
  220. },
  221. reference => {
  222. text => $request->{_locale}->text('Source/Reference'),
  223. href => "$base_href&order_by=reference"
  224. },
  225. date => {
  226. text => $request->{_locale}->text('Date'),
  227. href => "$base_href&order_by=date"
  228. }
  229. };
  230. my $classcount;
  231. for my $row (@vouchers) {
  232. $classcount = ($classcount + 1) % 2;
  233. $classcount ||= 0;
  234. push @$rows, {
  235. description => $row->{description},
  236. id => $row->{id},
  237. batch_class => $row->{batch_class},
  238. amount => $batch->format_amount(amount => $row->{amount}),
  239. date => $row->{transaction_date},
  240. reference => $row->{reference},
  241. class => "listrow$classcount"
  242. };
  243. }
  244. $batch->{title} = "Batch ID: $batch->{batch_id}";
  245. my $template = LedgerSMB::Template->new(
  246. user => $request->{_user},
  247. locale => $request->{_locale},
  248. path => 'UI',
  249. template => 'form-dynatable',
  250. format => ($batch->{format}) ? $batch->{format} : 'HTML',
  251. );
  252. my $hiddens = $batch->take_top_level();
  253. $template->render({
  254. form => $batch,
  255. columns => \@columns,
  256. heading => $heading,
  257. rows => $rows,
  258. hiddens => $hiddens,
  259. buttons => [{
  260. name => 'action',
  261. type => 'submit',
  262. text => $request->{_locale}->text('Post'),
  263. value => 'batch_approve',
  264. class => 'submit',
  265. },{
  266. name => 'action',
  267. type => 'submit',
  268. text => $request->{_locale}->text('Delete'),
  269. value => 'batch_delete',
  270. class => 'submit',
  271. }]
  272. });
  273. }
  274. sub list_batches_batch_delete {
  275. batch_delete(@_);
  276. }
  277. sub list_batches_batch_approve {
  278. batch_approve(@_);
  279. }
  280. sub batch_approve {
  281. my ($request) = @_;
  282. my $batch = LedgerSMB::Batch->new(base => $request);
  283. for my $count (1 .. $batch->{rowcount}){
  284. next unless $batch->{"batch_" . $batch->{"row_$count"}};
  285. $batch->{batch_id} = $batch->{"row_$count"};
  286. $batch->post;
  287. }
  288. search_batch($request);
  289. }
  290. sub batch_delete {
  291. my ($request) = @_;
  292. my $batch = LedgerSMB::Batch->new(base => $request);
  293. for my $count (1 .. $batch->{rowcount}){
  294. next unless $batch->{"batch_" . $batch->{"row_$count"}};
  295. $batch->{batch_id} = $batch->{"row_$count"};
  296. $batch->delete;
  297. }
  298. search_batch($request);
  299. }
  300. eval { do "scripts/custom/Voucher.pl"};
  301. 1;