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