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