Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision ad635c66

Von Moritz Bunkus vor fast 8 Jahren hinzugefügt

  • ID ad635c66023bc107d55b2f2f41f6fcadabd02cc3
  • Vorgänger 1ac7b3f2
  • Nachfolger 9fbc6ff4

Dialogbuchen auf Verwendung des Chart-Pickers umgestellt

Unterschiede anzeigen:

SL/GL.pm
39 39

  
40 40
package GL;
41 41

  
42
use List::Util qw(first);
43

  
42 44
use Data::Dumper;
43 45
use SL::DATEV qw(:CONSTANTS);
44 46
use SL::DBUtils;
47
use SL::DB::Chart;
45 48
use SL::Util qw(trim);
46 49
use SL::DB;
47 50

  
......
131 134

  
132 135
  # insert acc_trans transactions
133 136
  for $i (1 .. $form->{rowcount}) {
134
    # extract accno
135
    my ($accno) = split(/--/, $form->{"accno_$i"});
136 137
    ($form->{"tax_id_$i"}) = split(/--/, $form->{"taxchart_$i"});
137 138
    if ($form->{"tax_id_$i"} ne "") {
138 139
      $query = qq|SELECT taxkey, rate FROM tax WHERE id = ?|;
......
161 162
      $query =
162 163
        qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
163 164
                                  source, memo, project_id, taxkey, ob_transaction, cb_transaction, tax_id, chart_link)
164
           VALUES (?, (SELECT id FROM chart WHERE accno = ?),
165
                   ?, ?, ?, ?, ?, ?, ?, ?, ?, (SELECT link FROM chart WHERE accno = ?))|;
166
      @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{transdate}),
167
                 $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $taxkey, $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f', conv_i($form->{"tax_id_$i"}), $accno);
165
           VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, (SELECT link FROM chart WHERE id = ?))|;
166
      @values = (conv_i($form->{id}), $form->{"accno_id_$i"}, $amount, conv_date($form->{transdate}),
167
                 $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $taxkey, $form->{ob_transaction} ? 't' : 'f', $form->{cb_transaction} ? 't' : 'f', conv_i($form->{"tax_id_$i"}), $form->{"accno_id_$i"});
168 168
      do_query($form, $dbh, $query, @values);
169 169
    }
170 170

  
......
650 650
    $query =
651 651
      qq|SELECT c.accno, t.taxkey AS accnotaxkey, a.amount, a.memo, a.source,
652 652
           a.transdate, a.cleared, a.project_id, p.projectnumber,
653
           a.taxkey, t.rate AS taxrate, t.id,
653
           a.taxkey, t.rate AS taxrate, t.id, a.chart_id,
654 654
           (SELECT c1.accno
655 655
            FROM chart c1, tax t1
656 656
            WHERE (t1.id = t.id) AND (c1.id = t.chart_id)) AS taxaccno,
......
754 754
}
755 755

  
756 756
sub get_chart_balances {
757
  $main::lxdebug->enter_sub();
758

  
759
  my $self     = shift;
760
  my %params   = @_;
761

  
762
  Common::check_params(\%params, qw(charts));
757
  my ($self, @chart_ids) = @_;
763 758

  
764
  my $myconfig = \%main::myconfig;
765
  my $form     = $main::form;
766

  
767
  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
768

  
769
  my @ids      = map { $_->{id} } @{ $params{charts} };
770

  
771
  if (!@ids) {
772
    $main::lxdebug->leave_sub();
773
    return;
774
  }
759
  return () unless @chart_ids;
775 760

  
761
  my $placeholders = join ', ', ('?') x scalar(@chart_ids);
776 762
  my $query = qq|SELECT chart_id, SUM(amount) AS sum
777 763
                 FROM acc_trans
778
                 WHERE chart_id IN (| . join(', ', ('?') x scalar(@ids)) . qq|)
764
                 WHERE chart_id IN (${placeholders})
779 765
                 GROUP BY chart_id|;
780 766

  
781
  my %balances = selectall_as_map($form, $dbh, $query, 'chart_id', 'sum', @ids);
767
  my %balances = selectall_as_map($::form, $::form->get_standard_dbh(\%::myconfig), $query, 'chart_id', 'sum', @chart_ids);
782 768

  
783
  foreach my $chart (@{ $params{charts} }) {
784
    $chart->{balance} = $balances{ $chart->{id} } || 0;
785
  }
786

  
787
  $main::lxdebug->leave_sub();
769
  return %balances;
788 770
}
789 771

  
790
sub get_tax_dropdown {
791
  my ($self, $accno) = @_;
792

  
793
  my $myconfig = \%main::myconfig;
794
  my $form = $main::form;
772
sub get_active_taxes_for_chart {
773
  my ($self, $chart_id, $transdate) = @_;
795 774

  
796
  my $dbh = $form->get_standard_dbh($myconfig);
775
  my $chart         = SL::DB::Chart->new(id => $chart_id)->load;
776
  my $active_taxkey = $chart->get_active_taxkey($transdate);
777
  my $taxes         = SL::DB::Manager::Tax->get_all(
778
    where   => [ chart_categories => { like => '%' . $chart->category . '%' }],
779
    sort_by => 'taxkey, rate',
780
  );
797 781

  
798
  my $query = qq|SELECT category FROM chart WHERE accno = ?|;
799
  my ($category) = selectrow_query($form, $dbh, $query, $accno);
800

  
801
  $query = qq|SELECT * FROM tax WHERE chart_categories like '%$category%' order by taxkey, rate|;
802

  
803
  my $sth = prepare_execute_query($form, $dbh, $query);
804

  
805
  my @tax_accounts = ();
806
  while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
807
    push(@tax_accounts, $ref);
808
  }
782
  my $default_tax            = first { $active_taxkey->tax_id == $_->id } @{ $taxes };
783
  $default_tax->{is_default} = 1 if $default_tax;
809 784

  
810
  return @tax_accounts;
785
  return @{ $taxes };
811 786
}
812 787

  
813 788
1;
bin/mozilla/gl.pl
140 140
      $form->{"project_id_$j"} = $ref->{project_id};
141 141

  
142 142
    } else {
143
      $form->{"accno_$i"} = "$ref->{accno}--$ref->{tax_id}";
143
      $form->{"accno_id_$i"} = $ref->{chart_id};
144 144
      for (qw(fx_transaction source memo)) { $form->{"${_}_$i"} = $ref->{$_} }
145 145
      if ($ref->{amount} < 0) {
146 146
        $form->{totaldebit} -= $ref->{amount};
......
650 650
  my %myconfig = %main::myconfig;
651 651
  my $cgi      = $::request->{cgi};
652 652

  
653
  my %balances = GL->get_chart_balances(map { $_->{id} } @{ $form->{ALL_CHARTS} });
654

  
653 655
  $form->{debit_1}     = 0 if !$form->{"debit_1"};
654 656
  $form->{totaldebit}  = 0;
655 657
  $form->{totalcredit} = 0;
......
661 663
    $project_labels{$item->{"id"}} = $item->{"projectnumber"};
662 664
  }
663 665

  
664
  my %chart_labels = ();
665
  my @chart_values = ();
666
  my %charts = ();
667
  my $taxchart_init;
668
  foreach my $item (@{ $form->{ALL_CHARTS} }) {
669
    if ($item->{charttype} eq 'H'){ # skip headings
670
      next;
671
    }
672
    my $key = $item->{accno} . "--" . $item->{tax_id};
673
    $taxchart_init = $item->{tax_id} unless (@chart_values);
674
    push(@chart_values, $key);
675
    $chart_labels{$key} = $item->{accno} . "--" . $item->{description};
676
    $charts{$item->{accno}} = $item;
677
  }
666
  my %charts_by_id  = map { ($_->{id} => $_) } @{ $::form->{ALL_CHARTS} };
667
  my $default_chart = $::form->{ALL_CHARTS}[0];
668
  my $transdate     = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
678 669

  
679 670
  my ($source, $memo, $source_hidden, $memo_hidden);
680 671
  for my $i (1 .. $form->{rowcount}) {
......
690 681
      <input type="hidden" name="memo_$i" value="$form->{"memo_$i"}" size="16">|;
691 682
    }
692 683

  
693
    my $selected_accno_full;
694
    my ($accno_row) = split(/--/, $form->{"accno_$i"});
695
    my $item = $charts{$accno_row};
696
    $selected_accno_full = "$item->{accno}--$item->{tax_id}";
697

  
698
    my $selected_taxchart = $form->{"taxchart_$i"};
699
    my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
700
    my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_accno_$i"});
701

  
702 684
    my %taxchart_labels = ();
703 685
    my @taxchart_values = ();
704
    my %taxcharts = ();
705
    my $filter_accno;
706
    $filter_accno = $::form->{ALL_CHARTS}[0]->{accno};
707
    $filter_accno = $selected_accno if (!$init and $i < $form->{rowcount});
708
    foreach my $item ( GL->get_tax_dropdown($filter_accno) ) {
709
      my $key = $item->{id} . "--" . $item->{rate};
710
      $taxchart_init = $key if ($taxchart_init == $item->{id});
711
      push(@taxchart_values, $key);
712
      $taxchart_labels{$key} = $item->{taxdescription} . " " . $item->{rate} * 100 . ' %';
713
      $taxcharts{$item->{id}} = $item;
714
    }
715 686

  
716
    if ($previous_accno &&
717
        ($previous_accno eq $selected_accno) &&
718
        ($previous_tax_id ne $selected_tax_id)) {
719
      my $item = $taxcharts{$selected_tax_id};
720
      $selected_taxchart = "$item->{id}--$item->{rate}";
687
    my $accno_id          = $::form->{"accno_id_$i"};
688
    my $chart             = $charts_by_id{$accno_id} // $default_chart;
689
    $accno_id             = $chart->{id};
690
    my $chart_has_changed = $::form->{"previous_accno_id_$i"} && ($accno_id != $::form->{"previous_accno_id_$i"});
691
    my ($first_taxchart, $default_taxchart, $taxchart_to_use);
692

  
693
    foreach my $item ( GL->get_active_taxes_for_chart($accno_id, $transdate) ) {
694
      my $key             = $item->id . "--" . $item->rate;
695
      $first_taxchart   //= $item;
696
      $default_taxchart   = $item if $item->{is_default};
697
      $taxchart_to_use    = $item if $key eq $form->{"taxchart_$i"};
698

  
699
      push(@taxchart_values, $key);
700
      $taxchart_labels{$key} = $item->taxdescription . " " . $item->rate * 100 . ' %';
721 701
    }
722 702

  
723
    $selected_accno      = '' if ($init);
724
    $selected_taxchart ||= $taxchart_init;
703
    $taxchart_to_use      = $default_taxchart // $first_taxchart if $chart_has_changed || !$taxchart_to_use;
704
    my $selected_taxchart = $taxchart_to_use->id . '--' . $taxchart_to_use->rate;
725 705

  
726 706
    my $accno = qq|<td>| .
727
      NTI($cgi->popup_menu('-name' => "accno_$i",
728
                           '-id' => "accno_$i",
729
                           '-onChange' => "updateTaxes($i);",
730
                           '-style' => 'width:200px',
731
                           '-values' => \@chart_values,
732
                           '-labels' => \%chart_labels,
733
                           '-default' => $selected_accno_full))
734
      . $cgi->hidden('-name' => "previous_accno_$i",
735
                     '-default' => $selected_accno_full)
707
      $::request->presenter->chart_picker("accno_id_$i", $accno_id, style => "width: 300px") .
708
      $::request->presenter->hidden_tag("previous_accno_id_$i", $accno_id)
736 709
      . qq|</td>|;
737 710
    my $tax_ddbox = qq|<td>| .
738 711
      NTI($cgi->popup_menu('-name' => "taxchart_$i",
......
808 781
    <input type="hidden" name="project_id_$i" value="$form->{"project_id_$i"}">|;
809 782

  
810 783
    my $copy2credit = $i == 1 ? 'onkeyup="copy_debit_to_credit()"' : '';
784
    my $balance     = $form->format_amount(\%::myconfig, $balances{$accno_id} // 0, 2, 'DRCR');
811 785

  
812 786
    print qq|<tr valign=top>
813 787
    $accno
814
    <td id="chart_balance_$i" align="right">&nbsp;</td>
788
    <td id="chart_balance_$i" align="right">${balance}</td>
815 789
    $fx_transaction
816 790
    <td><input name="debit_$i" size="8" value="$form->{"debit_$i"}" accesskey=$i $copy2credit $debitreadonly></td>
817 791
    <td><input name="credit_$i" size=8 value="$form->{"credit_$i"}" $creditreadonly></td>
......
852 826

  
853 827
  my ($init) = @_;
854 828

  
829
  $::request->layout->add_javascripts("autocomplete_chart.js", "kivi.GL.js");
830

  
855 831
  my @old_project_ids = grep { $_ } map{ $::form->{"project_id_$_"} } 1..$::form->{rowcount};
856 832

  
857 833
  $::form->get_lists("projects"  => { "key"       => "ALL_PROJECTS",
......
863 839

  
864 840
  $::form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all;
865 841

  
866
  GL->get_chart_balances('charts' => $::form->{ALL_CHARTS});
867

  
868 842
  my $title      = $::form->{title};
869 843
  $::form->{title} = $::locale->text("$title General Ledger Transaction");
870 844
  # $locale->text('Add General Ledger Transaction')
......
877 851
    $::request->{layout}->focus("#reference");
878 852
    $::form->{taxincluded} = "1";
879 853
  } else {
880
    $::request->{layout}->focus("#accno_$::form->{rowcount}");
854
    $::request->{layout}->focus("#accno_id_$::form->{rowcount}_name");
881 855
  }
882 856

  
883 857
  $::form->{previous_id}     ||= "--";
......
1222 1196
}
1223 1197

  
1224 1198
sub get_tax_dropdown {
1225
  $main::lxdebug->enter_sub();
1199
  my $transdate    = $::form->{transdate} ? DateTime->from_kivitendo($::form->{transdate}) : DateTime->today_local;
1200
  my @tax_accounts = GL->get_active_taxes_for_chart($::form->{accno_id}, $transdate);
1201
  my $html         = $::form->parse_html_template("gl/update_tax_accounts", { TAX_ACCOUNTS => \@tax_accounts });
1226 1202

  
1227
  my $form = $main::form;
1228
  my @tax_accounts = GL->get_tax_dropdown($form->{accno});
1229

  
1230
  foreach my $item (@tax_accounts) {
1231
    $item->{taxdescription} = $::locale->{iconv_utf8}->convert($item->{taxdescription});
1232
    $item->{taxdescription} .= ' ' . $form->round_amount($item->{rate} * 100);
1233
  }
1234

  
1235
  $form->{TAX_ACCOUNTS} = [ @tax_accounts ];
1236

  
1237
  print $form->ajax_response_header, $form->parse_html_template("gl/update_tax_accounts");
1203
  print $::form->ajax_response_header, $html;
1204
}
1238 1205

  
1239
  $main::lxdebug->leave_sub();
1206
sub get_chart_balance {
1207
  my %balances = GL->get_chart_balances($::form->{accno_id});
1208
  my $balance  = $::form->format_amount(\%::myconfig, $balances{ $::form->{accno_id} }, 2, 'DRCR');
1240 1209

  
1210
  print $::form->ajax_response_header, $balance;
1241 1211
}
1242 1212

  
1243 1213
1;
js/kivi.GL.js
1
namespace('kivi.GL', function(ns) {
2
  "use strict";
3

  
4
  this.show_chart_balance = function(obj) {
5
    var row = $(obj).attr('name').replace(/.*_/, '');
6

  
7
    $.ajax({
8
      url: 'gl.pl?action=get_chart_balance',
9
      data: { accno_id:  $(obj).val() },
10
      dataType: 'html',
11
      success: function (new_html) {
12
        $('#chart_balance_' + row).html(new_html);
13
      }
14
    });
15
  };
16

  
17
  this.update_taxes = function(obj) {
18
    var row = $(obj).attr('name').replace(/.*_/, '');
19

  
20
    $.ajax({
21
      url: 'gl.pl?action=get_tax_dropdown',
22
      data: { accno_id:  $(obj).val(),
23
              transdate: $('#transdate').val() },
24
      dataType: 'html',
25
      success: function (new_html) {
26
        $("#taxchart_" + row).html(new_html);
27
      }
28
    });
29
  };
30
});
templates/webpages/gl/form_footer.html
51 51

  
52 52
  </form>
53 53

  
54
<script type="text/javascript">
55
 <!--
56
$(document).ready(function() {
57
  [%- SET row=0 %]
58
  [%- WHILE row < rowcount %]
59
   [%- SET row=row + 1 %]
60
   $('#accno_id_[% row %]').on('set_item:ChartPicker', function(e, item) {
61
     kivi.GL.show_chart_balance(this);
62
     kivi.GL.update_taxes(this);
63
   });
64
  [%- END %]
65
});
66
-->
67
</script>
templates/webpages/gl/form_header.html
6 6

  
7 7
<script type="text/javascript">
8 8
  <!--
9
function updateTaxes(row)
10
{
11
  var accno  = document.getElementById('accno_' + row);
12
  var taxkey = accno.options[accno.selectedIndex].value;
13
  var reg = /^(.*?)--(\d*)$/;
14
  var found = reg.exec(taxkey);
15
  var account = found[1];
16
  var default_tax_id = found[2];
17

  
18
  $.ajax({
19
    url: 'gl.pl?action=get_tax_dropdown',
20
    data: { accno: account,
21
            selected_tax_id: default_tax_id},
22
    dataType: 'html',
23
    success: function (new_html) {
24
                                $("#taxchart_" + row).html(new_html);
25
                              },
26
  });
27
};
28

  
29 9
  function copy_debit_to_credit() {
30 10
    var txt = document.getElementsByName('debit_1')[0].value;
31 11
    document.getElementsByName('credit_2')[0].value = txt;
......
162 142
[%- END %]
163 143

  
164 144
        </tr>
165

  
166
[%- PROCESS 'gl/form_header_chart_balances_js.html' %]
templates/webpages/gl/form_header_chart_balances_js.html
1
[% USE HTML %]
2
[% USE JavaScript %]
3
[% USE LxERP %]
4

  
5
<script type="text/javascript">
6
 <!--
7
var chart_balances = new Array();
8

  
9
[% FOREACH chart = ALL_CHARTS %]
10
chart_balances[[% loop.count - 1 %]] = '[% JavaScript.escape(LxERP.format_amount(chart.balance, 2, 0, 'DRCR')) %]';
11
[%- END %]
12

  
13
function show_chart_balance(obj) {
14
  var row = $(obj).attr('name').replace(/.*_/, '');
15
  var idx = $('#accno_' + row).prop('selectedIndex');
16
  $('#chart_balance_' + row).html(chart_balances[idx]);
17
}
18

  
19
$(document).ready(function() {
20
  [%- SET row=0 %]
21
  [%- WHILE row < rowcount %]
22
   [%- SET row=row + 1 %]
23
   $('#accno_[% row %]').change(function() { show_chart_balance(this); });
24
   show_chart_balance($('#accno_[% row %]'));
25
  [%- END %]
26
});
27
-->
28
</script>
templates/webpages/gl/update_tax_accounts.html
1
[% USE L %]
1
[% USE L %][%- USE LxERP -%]
2 2
[% FOR row = TAX_ACCOUNTS %]
3
<option value='[% row.id %]--[% row.rate %]'[% IF row.id == selected_tax_id%] selected[% END %]>[% row.taxdescription %] %</option>
3
<option value='[% row.id %]--[% row.rate %]'[% IF row.is_default %] selected[% END %]>[% row.taxdescription %] [% LxERP.round_amount(row.rate * 100) %] %</option>
4 4
[% END %]

Auch abrufbar als: Unified diff