summaryrefslogtreecommitdiff
path: root/scripts/drafts.pl
blob: 7d6008ec7be769c54c76524312ae982e611af40c (plain)
  1. package LedgerSMB::Scripts::drafts;
  2. our $VERSION = '0.1';
  3. use LedgerSMB::DBObject::Draft;
  4. use LedgerSMB::Template;
  5. use strict;
  6. sub search {
  7. my ($request) = @_;
  8. $request->{class_types} = [
  9. {text => $request->{_locale}->text('AR'), value => 'ar'},
  10. {text => $request->{_locale}->text('AP'), value => 'ap'},
  11. {text => $request->{_locale}->text('GL'), value => 'gl'},
  12. ];
  13. my $template = LedgerSMB::Template->new(
  14. user =>$request->{_user},
  15. locale => $request->{_locale},
  16. path => 'UI',
  17. template => 'batch/search_transactions',
  18. format => 'HTML'
  19. );
  20. $template->render($request);
  21. }
  22. sub list_drafts_draft_approve {
  23. my ($request) = @_;
  24. my $draft= LedgerSMB::DBObject::Draft->new(base => $request);
  25. for my $row (1 .. $draft->{rowcount}){
  26. if ($draft->{"draft_" .$draft->{"row_$row"}}){
  27. $draft->{id} = $draft->{"row_$row"};
  28. $draft->approve;
  29. }
  30. }
  31. search($request);
  32. }
  33. sub list_drafts_draft_delete {
  34. my ($request) = @_;
  35. my $draft= LedgerSMB::DBObject::Draft->new(base => $request);
  36. for my $row (1 .. $draft->{rowcount}){
  37. if ($draft->{"draft_" .$draft->{"row_$row"}}){
  38. $draft->{id} = $draft->{"row_$row"};
  39. $draft->delete;
  40. }
  41. }
  42. search($request);
  43. }
  44. sub list_drafts {
  45. my ($request) = @_;
  46. my $draft= LedgerSMB::DBObject::Draft->new(base => $request);
  47. my $callback = 'drafts.pl?action=list_drafts';
  48. for (qw(type reference amount_gy amount_lt)){
  49. if (defined $draft->{$_}){
  50. $callback .= "&$_=$draft->{$_}";
  51. }
  52. }
  53. my @search_results = $draft->search;
  54. $draft->{script} = "drafts.pl";
  55. $draft->{callback} = $draft->escape(string => $callback);
  56. my @columns =
  57. qw(select id transdate reference description amount);
  58. my $base_href = "drafts.pl";
  59. my $search_href = "$base_href?action=list_transactions";
  60. my $draft_href= "$base_href?action=get_transaction";
  61. for my $key (
  62. qw(class_id approved created_by description amount_gt amount_lt)
  63. ){
  64. $search_href .= "&$key=$draft->{key}";
  65. }
  66. my %column_heading = (
  67. 'select' => $draft->{_locale}->text('Select'),
  68. amount => {
  69. text => $draft->{_locale}->text('AR/AP/GL Total'),
  70. href => "$search_href&order_by=transaction_total"
  71. },
  72. description => {
  73. text => $draft->{_locale}->text('Description'),
  74. href => "$search_href&order_by=description"
  75. },
  76. id => {
  77. text => $draft->{_locale}->text('ID'),
  78. href => "$search_href&order_by=id"
  79. },
  80. reference => {
  81. text => $draft->{_locale}->text('Reference'),
  82. href => "$search_href&order_by=reference"
  83. },
  84. transdate => {
  85. text => $draft->{_locale}->text('Date'),
  86. href => "$search_href&order_by=transdate"
  87. },
  88. );
  89. my $count = 0;
  90. my @rows;
  91. for my $result (@search_results){
  92. ++$count;
  93. $draft->{"row_$count"} = $result->{id};
  94. push @rows, {
  95. 'select' => {
  96. input => {
  97. type => 'checkbox',
  98. value => 1,
  99. name => "draft_$result->{id}"
  100. }
  101. },
  102. amount => $draft->format_amount(
  103. amount => $result->{amount}
  104. ),
  105. reference => {
  106. text => $result->{reference},
  107. href => "$request->{type}.pl?action=edit&id=$result->{id}" .
  108. "&callback=$draft->{callback}",
  109. },
  110. description => $result->{description},
  111. transdate => $result->{transdate},
  112. id => $result->{id},
  113. };
  114. }
  115. $draft->{rowcount} = $count;
  116. my $template = LedgerSMB::Template->new(
  117. user => $request->{_user},
  118. locale => $request->{_locale},
  119. path => 'UI',
  120. template => 'form-dynatable',
  121. format => ($draft->{format}) ? $draft->{format} : 'HTML',
  122. );
  123. my $hiddens = $draft->take_top_level();
  124. $draft->{rowcount} = "$count";
  125. delete $draft->{search_results};
  126. $template->render({
  127. form => $draft,
  128. columns => \@columns,
  129. heading => \%column_heading,
  130. rows => \@rows,
  131. hiddens => $hiddens,
  132. buttons => [{
  133. name => 'action',
  134. type => 'submit',
  135. text => $request->{_locale}->text('Post'),
  136. value => 'draft_approve',
  137. class => 'submit',
  138. },{
  139. name => 'action',
  140. type => 'submit',
  141. text => $request->{_locale}->text('Delete'),
  142. value => 'draft_delete',
  143. class => 'submit',
  144. }]
  145. });
  146. }
  147. sub delete_drafts {
  148. my ($request) = @_;
  149. }