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