X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=bin%2Fmozilla%2Fgl.pl;h=b1c640a2f59a02436c077d4864d6ee8aa0d896e4;hb=273b5e046f1dab732ece657a756ada58d7669b9d;hp=e0a5811d24525ad81de0b84c2d53761593acfffe;hpb=ee072e4f077213bf6f8792ca8f0a1afebbb6282f;p=kivitendo-erp.git diff --git a/bin/mozilla/gl.pl b/bin/mozilla/gl.pl index e0a5811d2..b1c640a2f 100644 --- a/bin/mozilla/gl.pl +++ b/bin/mozilla/gl.pl @@ -31,16 +31,21 @@ # #====================================================================== -use SL::GL; -use SL::PE; - -use Data::Dumper; +use utf8; +use strict; -require "$form->{path}/arap.pl"; +use POSIX qw(strftime); +use List::Util qw(sum); -1; +use SL::FU; +use SL::GL; +use SL::IS; +use SL::PE; +use SL::ReportGenerator; -# end of main +require "bin/mozilla/common.pl"; +require "bin/mozilla/drafts.pl"; +require "bin/mozilla/reportgenerator.pl"; # this is for our long dates # $locale->text('January') @@ -71,919 +76,559 @@ require "$form->{path}/arap.pl"; # $locale->text('Dec') sub add { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + $main::auth->assert('general_ledger'); + + my $form = $main::form; + my %myconfig = %main::myconfig; + + return $main::lxdebug->leave_sub() if (load_draft_maybe()); $form->{title} = "Add"; - $form->{callback} = - "$form->{script}?action=add&path=$form->{path}&login=$form->{login}&password=$form->{password}" - unless $form->{callback}; + $form->{callback} = "gl.pl?action=add" unless $form->{callback}; # we use this only to set a default date + # yep. aber er holt hier auch schon ALL_CHARTS. Aufwand / Nutzen? jb GL->transaction(\%myconfig, \%$form); - map { - $chart .= - "" - } @{ $form->{chart} }; - map { - $tax .= - qq|" - } @{ $form->{chart} }; - $form->{chart} = $chart; - map { - $tax .= - qq|"; - $form->{debitchart} = - ""; - } else { - $form->{creditchart} = $form->{chart}; - $form->{creditchart} =~ - s/value=\"$form->{creditchartselected}\"/value=\"$form->{creditchartselected}\" selected/; + push @a, {}; + $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1"; + if ($debitcredit) { + $debitcount++; + } else { + $creditcount++; + } - $form->{debitchart} = $form->{chart}; - $form->{debitchart} =~ - s/value=\"$form->{debitchartselected}\"/value=\"$form->{debitchartselected}\" selected/; - } - ($debitaccno, $debittaxkey) = split(/--/, $form->{debitchartselected}); - ($creditaccno, $credittaxkey) = split(/--/, $form->{creditchartselected}); - if ($debittaxkey > 0) { - $form->{taxchart} = $form->unescape($form->{taxchart}); - $form->{taxchart} =~ s/selected//ig; - $form->{taxchart} =~ - s/\"$debittaxkey--([^\"]*)\"/\"$debittaxkey--$1\" selected/; - - $rate = $1; - - if ($form->{taxincluded}) { - $form->{debit} = - $form->parse_amount(\%myconfig, $form->{amount}) / ($rate + 1); - $form->{credit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1; - $form->{tax} = - $form->parse_amount(\%myconfig, $form->{amount}) / ($rate + 1) * $rate; - } else { - $form->{debit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1; - $form->{credit} = - $form->parse_amount(\%myconfig, $form->{amount}) * ($rate + 1); - $form->{tax} = $form->parse_amount(\%myconfig, $form->{amount}) * $rate; - } - } else { - $form->{taxchart} = $form->unescape($form->{taxchart}); - $form->{taxchart} =~ s/selected//ig; - $form->{taxchart} =~ - s/\"$credittaxkey--([^\"]*)\"/\"$credittaxkey--$1\" selected/; - $rate = $1; - - if ($form->{taxincluded}) { - $form->{debit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1; - $form->{credit} = - $form->parse_amount(\%myconfig, $form->{amount}) / ($rate + 1); - $form->{tax} = - $form->parse_amount(\%myconfig, $form->{amount}) / ($rate + 1) * $rate; - } else { - $form->{debit} = - $form->parse_amount(\%myconfig, $form->{amount}) * ($rate + 1); - $form->{credit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1; - $form->{tax} = $form->parse_amount(\%myconfig, $form->{amount}) * $rate; + if (($debitcount >= 2) && ($creditcount == 2)) { + $form->{"credit_$i"} = 0; + $form->{"tax_$i"} = 0; + $creditcount--; + $form->{creditlock} = 1; + } + if (($creditcount >= 2) && ($debitcount == 2)) { + $form->{"debit_$i"} = 0; + $form->{"tax_$i"} = 0; + $debitcount--; + $form->{debitlock} = 1; + } + if (($creditcount == 1) && ($debitcount == 2)) { + $form->{creditlock} = 1; + } + if (($creditcount == 2) && ($debitcount == 1)) { + $form->{debitlock} = 1; + } + if ($debitcredit && $credittax) { + $form->{"taxchart_$i"} = "0--0.00"; + } + if (!$debitcredit && $debittax) { + $form->{"taxchart_$i"} = "0--0.00"; + } + $amount = + ($form->{"debit_$i"} == 0) + ? $form->{"credit_$i"} + : $form->{"debit_$i"}; + my $j = $#a; + if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) { + $form->{"taxchart_$i"} = "0--0.00"; + $form->{"tax_$i"} = 0; + } + my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"}); + if ($taxkey > 1) { + if ($debitcredit) { + $debittax = 1; + } else { + $credittax = 1; + } + }; + my ($tmpnetamount,$tmpdiff); + ($tmpnetamount,$form->{"tax_$i"},$tmpdiff) = $form->calculate_tax($amount,$rate,$form->{taxincluded} *= 1,2); + + for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} } + $count++; } } - if ($form->{credit_splited}) { - $form->{debit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1; - $form->{credit} = $credit_save; - $form->{tax} = 0; - } elsif ($form->{debit_splited}) { - $form->{credit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1; - $form->{debit} = $debit_save; - $form->{tax} = 0; + + for my $i (1 .. $count) { + my $j = $i - 1; + for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} } + } + + for my $i ($count + 1 .. $form->{rowcount}) { + for (@flds) { delete $form->{"${_}_$i"} } } - &check_project; + + $form->{rowcount} = $count + 1; &display_form; + $main::lxdebug->leave_sub(); - $lxdebug->leave_sub(); } sub display_form { - $lxdebug->enter_sub(); + my ($init) = @_; + $main::lxdebug->enter_sub(); + + $main::auth->assert('general_ledger'); + + my $form = $main::form; + my %myconfig = %main::myconfig; - &form_header; + &form_header($init); # for $i (1 .. $form->{rowcount}) { # $form->{totaldebit} += $form->parse_amount(\%myconfig, $form->{"debit_$i"}); @@ -991,376 +636,305 @@ sub display_form { # # &form_row($i); # } - + &display_rows($init); &form_footer; + $main::lxdebug->leave_sub(); - $lxdebug->leave_sub(); } -sub form_header { - $lxdebug->enter_sub(); - - $title = $form->{title}; - $form->{title} = $locale->text("$title General Ledger Transaction"); - $readonly = ($form->{id}) ? "readonly" : ""; +sub display_rows { + my ($init) = @_; + $main::lxdebug->enter_sub(); - $form->{urldebit} = - "$form->{script}?action=split_debit&path=$form->{path}&login=$form->{login}&password=$form->{password}"; - $form->{urlcredit} = - "$form->{script}?action=split_credit&path=$form->{path}&login=$form->{login}&password=$form->{password}"; + $main::auth->assert('general_ledger'); - # $locale->text('Add General Ledger Transaction') - # $locale->text('Edit General Ledger Transaction') - map { $form->{$_} =~ s/\"/"/g } qw(reference description chart); + my $form = $main::form; + my %myconfig = %main::myconfig; + my $cgi = $::request->{cgi}; - $form->{selectdepartment} =~ s/ selected//; - $form->{taxchart} =~ s/ selected//; - $form->{selectdepartment} =~ - s/option>\Q$form->{department}\E/option selected>$form->{department}/; + $form->{debit_1} = 0 if !$form->{"debit_1"}; + $form->{totaldebit} = 0; + $form->{totalcredit} = 0; - if (($rows = $form->numtextrows($form->{description}, 50)) > 1) { - $description = - qq||; - } else { - $description = - qq||; + my %project_labels = (); + my @project_values = (""); + foreach my $item (@{ $form->{"ALL_PROJECTS"} }) { + push(@project_values, $item->{"id"}); + $project_labels{$item->{"id"}} = $item->{"projectnumber"}; } - $taxincluded = ($form->{taxincluded}) ? "checked" : ""; - - if (!$form->{id}) { - $taxincluded = "checked"; + my %chart_labels = (); + my @chart_values = (); + my %charts = (); + my $taxchart_init; + foreach my $item (@{ $form->{ALL_CHARTS} }) { + if ($item->{charttype} eq 'H'){ #falls ÃŒberschrift + next; #ÃŒberspringen (Bug 1150) + } + my $key = $item->{accno} . "--" . $item->{tax_id}; + $taxchart_init = $item->{tax_id} unless (@chart_values); + push(@chart_values, $key); + $chart_labels{$key} = $item->{accno} . "--" . $item->{description}; + $charts{$item->{accno}} = $item; } - $amount = - qq||; - - $department = qq| - - | . $locale->text('Department') . qq| - - - -| if $form->{selectdepartment}; - - $form->{fokus} = "gl.reference"; - $form->{remote} = 1; - - # use JavaScript Calendar or not - $form->{jsscript} = $jscalendar; - $jsscript = ""; - if ($form->{jsscript}) { - - # with JavaScript Calendar - $button1 = qq| - {transdate} tabindex="2" $readonly> - text('button') - . qq|> - |; - - #write Trigger - $jsscript = - Form->write_trigger(\%myconfig, "1", "transdate", "BL", "trigger1", "", - "", ""); - } else { + my ($source, $memo, $source_hidden, $memo_hidden); + for my $i (1 .. $form->{rowcount}) { + if ($form->{show_details}) { + $source = qq| + |; + $memo = qq| + |; + } else { + $source_hidden = qq| + |; + $memo_hidden = qq| + |; + } - # without JavaScript Calendar - $button1 = - qq|{transdate} tabindex="2" $readonly>|; - } + my $selected_accno_full; + my ($accno_row) = split(/--/, $form->{"accno_$i"}); + my $item = $charts{$accno_row}; + $selected_accno_full = "$item->{accno}--$item->{tax_id}"; + + my $selected_taxchart = $form->{"taxchart_$i"}; + my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full); + my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_accno_$i"}); + + my %taxchart_labels = (); + my @taxchart_values = (); + my %taxcharts = (); + my $filter_accno; + $filter_accno = $::form->{ALL_CHARTS}[0]->{accno}; + $filter_accno = $selected_accno if (!$init and $i < $form->{rowcount}); + foreach my $item ( GL->get_tax_dropdown($filter_accno) ) { + my $key = $item->{id} . "--" . $item->{rate}; + $taxchart_init = $key if ($taxchart_init == $item->{id}); + push(@taxchart_values, $key); + $taxchart_labels{$key} = $item->{taxdescription} . " " . $item->{rate} * 100 . ' %'; + $taxcharts{$item->{id}} = $item; + } - $form->header; + if ($previous_accno && + ($previous_accno eq $selected_accno) && + ($previous_tax_id ne $selected_tax_id)) { + my $item = $taxcharts{$selected_tax_id}; + $selected_taxchart = "$item->{id}--$item->{rate}"; + } - print qq| - + $selected_accno = '' if ($init); + $selected_taxchart ||= $taxchart_init; + + my $accno = qq|| . + NTI($cgi->popup_menu('-name' => "accno_$i", + '-id' => "accno_$i", + '-onChange' => "updateTaxes($i);", + '-style' => 'width:200px', + '-values' => \@chart_values, + '-labels' => \%chart_labels, + '-default' => $selected_accno_full)) + . $cgi->hidden('-name' => "previous_accno_$i", + '-default' => $selected_accno_full) + . qq||; + my $tax_ddbox = qq|| . + NTI($cgi->popup_menu('-name' => "taxchart_$i", + '-id' => "taxchart_$i", + '-style' => 'width:200px', + '-values' => \@taxchart_values, + '-labels' => \%taxchart_labels, + '-default' => $selected_taxchart)) + . qq||; + + my ($fx_transaction, $checked); + if ($init) { + if ($form->{transfer}) { + $fx_transaction = qq| + + |; + } -
{script}> + } else { + if ($form->{"debit_$i"} != 0) { + $form->{totaldebit} += $form->{"debit_$i"}; + if (!$form->{taxincluded}) { + $form->{totaldebit} += $form->{"tax_$i"}; + } + } else { + $form->{totalcredit} += $form->{"credit_$i"}; + if (!$form->{taxincluded}) { + $form->{totalcredit} += $form->{"tax_$i"}; + } + } -{id}> + for (qw(debit credit tax)) { + $form->{"${_}_$i"} = + ($form->{"${_}_$i"}) + ? $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) + : ""; + } -{closedto}> -{locked}> - -escape($form->{taxchart}) . qq|> - - - + if ($i < $form->{rowcount}) { + if ($form->{transfer}) { + $checked = ($form->{"fx_transaction_$i"}) ? "1" : ""; + my $x = ($checked) ? "x" : ""; + $fx_transaction = qq| + $x + |; + } + $form->hide_form("accno_$i"); + } else { + if ($form->{transfer}) { + $fx_transaction = qq| + + |; + } + } + } + my $debitreadonly = ""; + my $creditreadonly = ""; + if ($i == $form->{rowcount}) { + if ($form->{debitlock}) { + $debitreadonly = "readonly"; + } elsif ($form->{creditlock}) { + $creditreadonly = "readonly"; + } + } - - - - - - - - - - -$jsscript |; + } + + $form->hide_form(qw(rowcount selectaccno)); + + $main::lxdebug->leave_sub(); - $lxdebug->leave_sub(); } -sub form_footer { - $lxdebug->enter_sub(); - ($dec) = ($form->{totaldebit} =~ /\.(\d+)/); - $dec = length $dec; - $decimalplaces = ($dec > 2) ? $dec : 2; - $taxrowcount = - ($form->{creditrowcount} > $form->{debitrowcount}) - ? $form->{creditrowcount} - : $form->{debitrowcount}; - $radieren = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0; - - map { - $form->{$_} = - $form->format_amount(\%myconfig, $form->{$_}, $decimalplaces, " ") - } qw(totaldebit totalcredit); +sub _get_radieren { + return ($::instance_conf->get_gl_changeable == 2) ? ($::form->current_date(\%::myconfig) eq $::form->{gldate}) : ($::instance_conf->get_gl_changeable == 1); +} - print qq| -
$form->{title}
- - - - - - |; - if ($form->{id}) { - print qq| - - - - - |; - } - print qq| - $department|; - if ($form->{id}) { - print qq| - - - - - |; - } else { - print qq| - - - - |; - } - print qq| - - - - - |; - if ($form->{debit_splited}) { - print qq| - - - - - - {debit_splited}> - - - - - - {credit_splited}> - - |; - } elsif ($form->{credit_splited}) { + my $projectnumber = + NTI($cgi->popup_menu('-name' => "project_id_$i", + '-values' => \@project_values, + '-labels' => \%project_labels, + '-default' => $form->{"project_id_$i"} )); + my $projectnumber_hidden = qq| + |; + + my $copy2credit = $i == 1 ? 'onkeyup="copy_debit_to_credit()"' : ''; + + print qq| + $accno + + $fx_transaction + + + + $tax_ddbox|; + + if ($form->{show_details}) { + print qq| + $source + $memo + +|; + } else { print qq| - - - - - {debit_splited}> - - - - - - - {credit_splited}> - |; - } else { + $source_hidden + $memo_hidden + $projectnumber_hidden + |; + } print qq| - - - - - - {debit_splited}> - - - - - - - {credit_splited}> - - - - - - - |; - } - print qq| -
| . $locale->text('Reference') . qq| - - - - $button1 - -
| . $locale->text('Date') . qq|
-
| . $locale->text('Belegnummer') . qq| - - - - - -
| . $locale->text('Buchungsdatum') . qq|{gldate} $readonly>
-
| . $locale->text('Description') . qq|$description - - - - - -
| . $locale->text('Mitarbeiter') . qq|{employee} $readonly>
-
| . $locale->text('Description') . qq|$description
| . $locale->text('Betrag') . qq|$amount - - - - - -
| . $locale->text('MwSt. inkl.') . qq|
-
| . $locale->text('Debit') . qq|Betrag gesplittet EUR
| . $locale->text('Credit') . qq| EUR
 $form->{"tax_$i"}$projectnumber
| . $locale->text('Debit') . qq| EUR
| . $locale->text('Credit') . qq|Betrag gesplittet EUR
| . $locale->text('Debit') . qq| EUR
| . $locale->text('Credit') . qq| EUR
| . $locale->text('Tax') . qq| EUR
-

-|; - for $i (2 .. $form->{creditrowcount}) { - print qq| - - - |; - } - for $i (2 .. $form->{debitrowcount}) { - print qq| - - - |; - } - if ($taxrowcount > 1) { - for $i (2 .. $taxrowcount) { - print qq| - - - |; - } - } - print qq| -{path}> -{login}> -{password}> +sub form_header { + $::lxdebug->enter_sub; + $::auth->assert('general_ledger'); - -
-|; + my ($init) = @_; + + my @old_project_ids = grep { $_ } map{ $::form->{"project_id_$_"} } 1..$::form->{rowcount}; - $transdate = $form->datetonum($form->{transdate}, \%myconfig); - $closedto = $form->datetonum($form->{closedto}, \%myconfig); + $::form->get_lists("projects" => { "key" => "ALL_PROJECTS", + "all" => 0, + "old_id" => \@old_project_ids }, + "charts" => { "key" => "ALL_CHARTS", + "transdate" => $::form->{transdate} }); - if ($form->{id}) { + GL->get_chart_balances('charts' => $::form->{ALL_CHARTS}); - print qq||; + my $title = $::form->{title}; + $::form->{title} = $::locale->text("$title General Ledger Transaction"); + # $locale->text('Add General Ledger Transaction') + # $locale->text('Edit General Ledger Transaction') - # Löschen und ändern von Buchungen nicht mehr möglich (GoB) nur am selben Tag möglich + map { $::form->{$_} =~ s/\"/"/g } + qw(chart taxchart); - if (!$form->{locked} && $radieren) { - print qq| - - |; - } + $::form->{selectdepartment} =~ s/ selected//; + $::form->{selectdepartment} =~ + s/option>\Q$::form->{department}\E/option selected>$::form->{department}/; - # if ($transdate > $closedto) { - # print qq| - # |; - # } + if ($init) { + $::request->{layout}->focus("#reference"); + $::form->{taxincluded} = "1"; } else { - if ($transdate > $closedto) { - print qq| - |; - } + $::request->{layout}->focus("#accno_$::form->{rowcount}"); } - if ($form->{menubar}) { - require "$form->{path}/menu.pl"; - &menubar; - } + $::form->{previous_id} ||= "--"; + $::form->{previous_gldate} ||= "--"; + + $::form->header; + print $::form->parse_html_template('gl/form_header', { + hide_title => $title, + readonly => $::form->{id} && ($::form->{locked} || !_get_radieren()), + }); + + $::lxdebug->leave_sub; + +} - print " -
+sub form_footer { + $::lxdebug->enter_sub; + $::auth->assert('general_ledger'); + + my ($follow_ups, $follow_ups_due); + + if ($::form->{id}) { + $follow_ups = FU->follow_ups('trans_id' => $::form->{id}); + $follow_ups_due = sum map { $_->{due} * 1 } @{ $follow_ups || [] }; + } - - -"; + print $::form->parse_html_template('gl/form_footer', { + radieren => _get_radieren(), + follow_ups => $follow_ups, + follow_ups_due => $follow_ups_due, + }); - $lxdebug->leave_sub(); + $::lxdebug->leave_sub; } sub delete { - $lxdebug->enter_sub(); + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my $locale = $main::locale; $form->header; print qq| - - -
{script}> + |; - map { $form->{$_} =~ s/\"/"/g } qw(reference description chart); + map { $form->{$_} =~ s/\"/"/g } qw(reference description); delete $form->{header}; - foreach $key (keys %$form) { - print qq|\n|; + foreach my $key (keys %$form) { + next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key})); + print qq|\n|; } print qq| @@ -1374,122 +948,183 @@ sub delete { . $locale->text('Yes') . qq|">
|; + $main::lxdebug->leave_sub(); - $lxdebug->leave_sub(); } sub yes { - $lxdebug->enter_sub(); - - $form->redirect($locale->text('Transaction deleted!')) - if (GL->delete_transaction(\%myconfig, \%$form)); + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my %myconfig = %main::myconfig; + my $locale = $main::locale; + + if (GL->delete_transaction(\%myconfig, \%$form)){ + # saving the history + if(!exists $form->{addition} && $form->{id} ne "") { + $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber}; + $form->{addition} = "DELETED"; + $form->save_history; + } + # /saving the history + $form->redirect($locale->text('Transaction deleted!')) + } $form->error($locale->text('Cannot delete transaction!')); + $main::lxdebug->leave_sub(); - $lxdebug->leave_sub(); } -sub post { - $lxdebug->enter_sub(); +sub post_transaction { + $main::lxdebug->enter_sub(); + + my $form = $main::form; + my %myconfig = %main::myconfig; + my $locale = $main::locale; # check if there is something in reference and date $form->isblank("reference", $locale->text('Reference missing!')); $form->isblank("transdate", $locale->text('Transaction Date missing!')); $form->isblank("description", $locale->text('Description missing!')); - $transdate = $form->datetonum($form->{transdate}, \%myconfig); - $closedto = $form->datetonum($form->{closedto}, \%myconfig); - - ($debitaccno, $debittaxkey) = split(/--/, $form->{debitchartselected}); - ($creditaccno, $credittaxkey) = split(/--/, $form->{creditchartselected}); + my $transdate = $form->datetonum($form->{transdate}, \%myconfig); + my $closedto = $form->datetonum($form->{closedto}, \%myconfig); - $credit_save = $form->{credit}; - $debit_save = $form->{debit}; + my @a = (); + my $count = 0; + my $debittax = 0; + my $credittax = 0; + my $debitcount = 0; + my $creditcount = 0; + my $debitcredit; + my %split_safety = (); - # check project - &check_project; - ($taxkey, $taxrate) = split(/--/, $form->{taxchartselected}); + my $dbh = $form->dbconnect_noauto(\%myconfig); + my ($notax_id) = selectrow_query($form, $dbh, "SELECT id FROM tax WHERE taxkey = 0 LIMIT 1", ); + $dbh->disconnect; - if ($debittaxkey > 0) { - $form->{taxchart} = $form->unescape($form->{taxchart}); - $form->{taxchart} =~ s/\"$debittaxkey--([^\"]*)\"/\"$debittaxkey--$1\"/; + my @flds = qw(accno debit credit projectnumber fx_transaction source memo tax taxchart); - $rate = ($form->{taxchart} =~ /selected/) ? $taxrate : $1; - $form->{taxkey} = - ($form->{taxchart} =~ /selected/) ? $taxkey : $debittaxkey; + for my $i (1 .. $form->{rowcount}) { + next if $form->{"debit_$i"} eq "" && $form->{"credit_$i"} eq ""; - if ($form->{storno}) { - $form->{debit} = $form->parse_amount(\%myconfig, $form->{debit}); - $form->{credit} = $form->parse_amount(\%myconfig, $form->{credit}); - $form->{tax} = $form->parse_amount(\%myconfig, $form->{tax}); - } else { - if ($form->{taxincluded}) { - $form->{debit} = - $form->parse_amount(\%myconfig, $form->{amount}) / ($rate + 1); - $form->{credit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1; - $form->{tax} = - $form->parse_amount(\%myconfig, $form->{amount}) / ($rate + 1) * - $rate; - } else { - $form->{debit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1; - $form->{credit} = - $form->parse_amount(\%myconfig, $form->{amount}) * ($rate + 1); - $form->{tax} = - $form->parse_amount(\%myconfig, $form->{amount}) * $rate; - } + for (qw(debit credit tax)) { + $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}); } - $form->{debittaxkey} = 1; - } else { - $form->{taxchart} = $form->unescape($form->{taxchart}); - $form->{taxchart} =~ s/\"$credittaxkey--([^\"]*)\"/\"$credittaxkey--$1\"/; + push @a, {}; + $debitcredit = ($form->{"debit_$i"} == 0) ? "0" : "1"; - $rate = ($form->{taxchart} =~ /selected/) ? $taxrate : $1; - $form->{taxkey} = - ($form->{taxchart} =~ /selected/) ? $taxkey : $credittaxkey; + $split_safety{ $form->{"debit_$i"} <=> 0 }++; + $split_safety{ - $form->{"credit_$i"} <=> 0 }++; - if ($form->{storno}) { - $form->{debit} = $form->parse_amount(\%myconfig, $form->{debit}); - $form->{credit} = $form->parse_amount(\%myconfig, $form->{credit}); - $form->{tax} = $form->parse_amount(\%myconfig, $form->{tax}); + if ($debitcredit) { + $debitcount++; } else { - if ($form->{taxincluded}) { - $form->{debit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1; - $form->{credit} = - $form->parse_amount(\%myconfig, $form->{amount}) / ($rate + 1); - $form->{tax} = - $form->parse_amount(\%myconfig, $form->{amount}) / ($rate + 1) * - $rate; + $creditcount++; + } + + if (($debitcount >= 2) && ($creditcount == 2)) { + $form->{"credit_$i"} = 0; + $form->{"tax_$i"} = 0; + $creditcount--; + $form->{creditlock} = 1; + } + if (($creditcount >= 2) && ($debitcount == 2)) { + $form->{"debit_$i"} = 0; + $form->{"tax_$i"} = 0; + $debitcount--; + $form->{debitlock} = 1; + } + if (($creditcount == 1) && ($debitcount == 2)) { + $form->{creditlock} = 1; + } + if (($creditcount == 2) && ($debitcount == 1)) { + $form->{debitlock} = 1; + } + if ($debitcredit && $credittax) { + $form->{"taxchart_$i"} = "$notax_id--0.00"; + } + if (!$debitcredit && $debittax) { + $form->{"taxchart_$i"} = "$notax_id--0.00"; + } + my $amount = ($form->{"debit_$i"} == 0) + ? $form->{"credit_$i"} + : $form->{"debit_$i"}; + my $j = $#a; + if (($debitcredit && $credittax) || (!$debitcredit && $debittax)) { + $form->{"taxchart_$i"} = "$notax_id--0.00"; + $form->{"tax_$i"} = 0; + } + my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"}); + if ($taxkey > 1) { + if ($debitcredit) { + $debittax = 1; } else { - $form->{debit} = - $form->parse_amount(\%myconfig, $form->{amount}) * ($rate + 1); - $form->{credit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1; - $form->{tax} = - $form->parse_amount(\%myconfig, $form->{amount}) * $rate; + $credittax = 1; } + + my ($tmpnetamount,$tmpdiff); + ($tmpnetamount,$form->{"tax_$i"},$tmpdiff) = $form->calculate_tax($amount,$rate,$form->{taxincluded} *= 1,2); + if ($debitcredit) { + $form->{"debit_$i"} = $tmpnetamount; + } else { + $form->{"credit_$i"} = $tmpnetamount; + } + + } else { + $form->{"tax_$i"} = 0; } - $form->{debittaxkey} = 0; + for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} } + $count++; + } + + if ($split_safety{-1} > 1 && $split_safety{1} > 1) { + $::form->error($::locale->text("Split entry detected. The values you have entered will result in an entry with more than one position on both debit and credit. " . + "Due to known problems involving accounting software kivitendo does not allow these.")); + } + + for my $i (1 .. $count) { + my $j = $i - 1; + for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} } + } + + for my $i ($count + 1 .. $form->{rowcount}) { + for (@flds) { delete $form->{"${_}_$i"} } } - if ($form->{credit_splited}) { - $form->{debit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1; - $form->{credit} = $credit_save; - $form->{tax} = 0; - } elsif ($form->{debit_splited}) { - $form->{credit} = $form->parse_amount(\%myconfig, $form->{amount}) * 1; - $form->{debit} = $debit_save; - $form->{tax} = 0; + + my ($debit, $credit, $taxtotal); + for my $i (1 .. $form->{rowcount}) { + my $dr = $form->{"debit_$i"}; + my $cr = $form->{"credit_$i"}; + my $tax = $form->{"tax_$i"}; + if ($dr && $cr) { + $form->error($locale->text('Cannot post transaction with a debit and credit entry for the same account!')); + } + $debit += $dr + $tax if $dr; + $credit += $cr + $tax if $cr; + $taxtotal += $tax if $form->{taxincluded} } + $form->{taxincluded} = 0 if !$taxtotal; + # this is just for the wise guys + + $form->error($locale->text('Cannot post transaction above the maximum future booking date!')) + if ($form->date_max_future($form->{"transdate"}, \%myconfig)); $form->error($locale->text('Cannot post transaction for a closed period!')) - if ($transdate <= $closedto); - $form->error($locale->text('Soll- und Habenkonto sind gleich!')) - if ($debitaccno eq $creditaccno); - $form->error($locale->text('Keine Steuerautomatik möglich!')) - if ($debittaxkey && $credittaxkey && !($taxkey == 0)); + if ($form->date_closed($form->{"transdate"}, \%myconfig)); + if ($form->round_amount($debit, 2) != $form->round_amount($credit, 2)) { + $form->error($locale->text('Out of balance transaction!')); + } + + if ($form->round_amount($debit, 2) + $form->round_amount($credit, 2) == 0) { + $form->error($locale->text('Empty transaction!')); + } - if (($errno = GL->post_transaction(\%myconfig, \%$form)) <= -1) { + if ((my $errno = GL->post_transaction(\%myconfig, \%$form)) <= -1) { $errno *= -1; + my @err; $err[1] = $locale->text('Cannot have a value in both Debit and Credit!'); $err[2] = $locale->text('Debit and credit out of balance!'); $err[3] = $locale->text('Cannot post a transaction without a value!'); @@ -1497,395 +1132,109 @@ sub post { $form->error($err[$errno]); } undef($form->{callback}); - $form->redirect("Buchung gespeichert. Buchungsnummer = " . $form->{id}); - - $lxdebug->leave_sub(); -} - -sub post_as_new { - $lxdebug->enter_sub(); - - $form->{id} = 0; - &add; - - $lxdebug->leave_sub(); -} - -sub storno { - $lxdebug->enter_sub(); - - $form->{id} = 0; - $form->{storno} = 1; - &post; - - $lxdebug->leave_sub(); -} - -sub split_debit { - $lxdebug->enter_sub(); - - # change callback - $form->{old_callback} = $form->escape($form->{callback}, 1); - $form->{callback} = $form->escape("$form->{script}?action=update", 1); - - # delete action - delete $form->{action}; - - # save all other form variables in a previousform variable - foreach $key (keys %$form) { - - # escape ampersands - $form->{$key} =~ s/&/%26/g; - $previousform .= qq|$key=$form->{$key}&|; - } - chop $previousform; - $previousform = $form->escape($previousform, 1); - if (!$form->{debitpost}) { - $form->{debitpost} = $form->{amount}; + # saving the history + if(!exists $form->{addition} && $form->{id} ne "") { + $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber}; + $form->{addition} = "SAVED"; + $form->{what_done} = $locale->text("Buchungsnummer") . " = " . $form->{id}; + $form->save_history; } - $form->{previousform} = $previousform; - &display_split_debit(); + # /saving the history - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); } -sub split_credit { - $lxdebug->enter_sub(); - - # change callback - $form->{old_callback} = $form->escape($form->{callback}, 1); - $form->{callback} = $form->escape("$form->{script}?action=update", 1); +sub post { + $main::lxdebug->enter_sub(); - # delete action - delete $form->{action}; + $main::auth->assert('general_ledger'); - # save all other form variables in a previousform variable - foreach $key (keys %$form) { + my $form = $main::form; + my $locale = $main::locale; - # escape ampersands - $form->{$key} =~ s/&/%26/g; - $previousform .= qq|$key=$form->{$key}&|; - } - chop $previousform; - $previousform = $form->escape($previousform, 1); - if (!$form->{creditpost}) { - $form->{creditpost} = $form->{amount}; + if ($::myconfig{mandatory_departments} && !$form->{department}) { + $form->{saved_message} = $::locale->text('You have to specify a department.'); + update(); + exit; } - $form->{previousform} = $previousform; - &display_split_credit(); - $lxdebug->leave_sub(); -} -sub display_split_credit { - $lxdebug->enter_sub(); - $form->{taxchart} = $form->unescape($form->{taxchart}); + $form->{title} = $locale->text("$form->{title} General Ledger Transaction"); + $form->{storno} = 0; - $form->header; + post_transaction(); - print qq| - -
-| - . $locale->text('Credit') . qq|: | - . $form->format_amount(\%myconfig, $form->{amount}, 2) - . qq| EUR
-| - . $locale->text('Still to post') - . qq|: EUR
- - -|; - for $i (2 .. $form->{creditrowcount}) { - $form->{"creditchart_$i"} = $form->{chart}; - $form->{"creditchart_$i"} =~ - s/value=\"$form->{"creditchartselected_$i"}\"/value=\"$form->{"creditchartselected_$i"}\" selected/; - - $form->{"taxchart_$i"} = $form->{taxchart}; - $form->{"taxchart_$i"} =~ - s/value=\"$form->{"taxchartselected_$i"}\"/value=\"$form->{"taxchartselected_$i"}\" selected/; - $position = $i - 1; - print qq| - - - - - - - - - - - - - - - - - - - - + remove_draft() if $form->{remove_draft}; -|; - } - print qq| -
Position $position| . $locale->text('Amount') . qq| $1 EUR
| . $locale->text('Tax') . qq| EUR Steuerkorrektur

- - -{path}> -{login}> -{password}> -{callback}> -{old_callback}> -{amount}> - - -escape($form->{chart}) . qq|> -escape($form->{taxchart}) . qq|> -

- - -

- - - -|; + $form->{callback} = build_std_url("action=add&DONT_LOAD_DRAFT=1", "show_details"); + $form->redirect($form->{callback}); - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); } -sub display_split_debit { - $lxdebug->enter_sub(); - $form->{taxchart} = $form->unescape($form->{taxchart}); +sub post_as_new { + $main::lxdebug->enter_sub(); - $form->header; + $main::auth->assert('general_ledger'); - print qq| - -
-| - . $locale->text('Debit') . qq|: | - . $form->format_amount(\%myconfig, $form->{amount}, 2) - . qq| EUR
-| - . $locale->text('Still to post') - . qq|: EUR
- - -|; - for $i (2 .. $form->{debitrowcount}) { - $form->{"debitchart_$i"} = $form->{chart}; - $form->{"debitchart_$i"} =~ - s/value=\"$form->{"debitchartselected_$i"}\"/value=\"$form->{"debitchartselected_$i"}\" selected/; - - $form->{"taxchart_$i"} = $form->{taxchart}; - $form->{"taxchart_$i"} =~ - s/value=\"$form->{"taxchartselected_$i"}\"/value=\"$form->{"taxchartselected_$i"}\" selected/; - $position = $i - 1; - print qq| - - - - - - - - - - - - - - - - - - - - + my $form = $main::form; -|; - } - print qq| -
Position $position| . $locale->text('Amount') . qq| $1 EUR
| . $locale->text('Tax') . qq| EUR Steuerkorrektur

- - -{path}> -{login}> -{password}> -{callback}> -{old_callback}> -{amount}> - - -escape($form->{chart}) . qq|> -escape($form->{taxchart}) . qq|> -

- - -

- - - -|; + $form->{id} = 0; + &add; + $main::lxdebug->leave_sub(); - $lxdebug->leave_sub(); } -sub new_account { - $lxdebug->enter_sub(); +sub storno { + $main::lxdebug->enter_sub(); - $form->{chart} = $form->unescape($form->{chart}); + $main::auth->assert('general_ledger'); - if ($form->{credit_splited}) { - $form->{creditpost} = $form->{amount}; + my $form = $main::form; + my %myconfig = %main::myconfig; + my $locale = $main::locale; - for $i (2 .. $form->{creditrowcount}) { - $form->{"credit_$i"} = - $form->parse_amount(\%myconfig, $form->{"credit_$i"}); - $form->{creditpost} -= $form->{"credit_$i"}; - ($taxkey, $taxrate) = split(/--/, $form->{"taxchartselected_$i"}); - if ($form->{"tax_manual_$i"}) { - $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"}); - } else { - $form->{"tax_$i"} = $form->{"credit_$i"} * $taxrate; - } - $form->{creditpost} -= $form->{"tax_$i"}; + # don't cancel cancelled transactions + if (IS->has_storno(\%myconfig, $form, 'gl')) { + $form->{title} = $locale->text("Cancel Accounts Receivables Transaction"); + $form->error($locale->text("Transaction has already been cancelled!")); + } - } - if ($form->{"credit_$form->{creditrowcount}"}) { - $form->{creditrowcount}++; - } + GL->storno($form, \%myconfig, $form->{id}); - &display_split_credit; + # saving the history + if(!exists $form->{addition} && $form->{id} ne "") { + $form->{snumbers} = "ordnumber_$form->{ordnumber}"; + $form->{addition} = "STORNO"; + $form->save_history; } - if ($form->{debit_splited}) { - $form->{debitpost} = $form->{amount}; - - for $i (2 .. $form->{debitrowcount}) { - $form->{"debit_$i"} = - $form->parse_amount(\%myconfig, $form->{"debit_$i"}); - $form->{debitpost} -= $form->{"debit_$i"}; - ($taxkey, $taxrate) = split(/--/, $form->{"taxchartselected_$i"}); - if ($form->{"tax_manual_$i"}) { - $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"}); - } else { - $form->{"tax_$i"} = $form->{"debit_$i"} * $taxrate; - } - $form->{debitpost} -= $form->{"tax_$i"}; + # /saving the history - } - if ($form->{"debit_$form->{debitrowcount}"}) { - $form->{debitrowcount}++; - } + $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id}); - &display_split_debit; - } - $lxdebug->leave_sub(); + $main::lxdebug->leave_sub(); } -sub close { - $lxdebug->enter_sub(); - - # save the new form variables before splitting previousform - map { $newform{$_} = $form->{$_} } keys %$form; +sub continue { + call_sub($main::form->{nextsub}); +} - $previousform = $form->unescape($form->{previousform}); +sub get_tax_dropdown { + $main::lxdebug->enter_sub(); - # don't trample on previous variables - map { delete $form->{$_} } keys %newform; + my $form = $main::form; + my @tax_accounts = GL->get_tax_dropdown($form->{accno}); - # now take it apart and restore original values - foreach $item (split /&/, $previousform) { - ($key, $value) = split /=/, $item, 2; - $value =~ s/%26/&/g; - $form->{$key} = $value; + foreach my $item (@tax_accounts) { + $item->{taxdescription} = $::locale->{iconv_utf8}->convert($item->{taxdescription}); + $item->{taxdescription} .= ' ' . $form->round_amount($item->{rate} * 100); } - if ($newform{credit_splited}) { - $form->{credit} = 0; - $form->{credit_splited} = $newform{credit_splited}; - $form->{creditrowcount} = $newform{creditrowcount}; - for $i (2 .. $form->{creditrowcount}) { - $form->{"creditchartselected_$i"} = $newform{"creditchartselected_$i"}; - $form->{"credit_$i"} = $newform{"credit_$i"}; - $form->{"credit_$i"} = - $form->parse_amount(\%myconfig, $form->{"credit_$i"}); - $form->{"taxchartselected_$i"} = $newform{"taxchartselected_$i"}; - ($taxkey, $taxrate) = split(/--/, $form->{"taxchartselected_$i"}); - if ($newform{"tax_manual_$i"}) { - $form->{"tax_$i"} = - $form->parse_amount(\%myconfig, $newform{"tax_$i"}); - } else { - $form->{"tax_$i"} = $form->{"credit_$i"} * $taxrate; - } - $form->{credit} += $form->round_amount($form->{"credit_$i"}, 2); - $form->{credit} += $form->round_amount($form->{"tax_$i"}, 2); - } - } else { - $form->{debit} = 0; - $form->{debit_splited} = $newform{debit_splited}; - $form->{debitrowcount} = $newform{debitrowcount}; - for $i (2 .. $form->{debitrowcount}) { - $form->{"debitchartselected_$i"} = $newform{"debitchartselected_$i"}; - $form->{"debit_$i"} = $newform{"debit_$i"}; - $form->{"debit_$i"} = - $form->parse_amount(\%myconfig, $form->{"debit_$i"}); - $form->{"taxchartselected_$i"} = $newform{"taxchartselected_$i"}; - ($taxkey, $taxrate) = split(/--/, $form->{"taxchartselected_$i"}); - if ($newform{"tax_manual_$i"}) { - $form->{"tax_$i"} = - $form->parse_amount(\%myconfig, $newform{"tax_$i"}); - } else { - $form->{"tax_$i"} = $form->{"debit_$i"} * $taxrate; - } - $form->{debit} += $form->round_amount($form->{"debit_$i"}, 2); - $form->{debit} += $form->round_amount($form->{"tax_$i"}, 2); - } - } - delete $form->{action}; - $callback = $form->unescape($form->{callback}); - $form->{callback} = $form->unescape($form->{old_callback}); - delete $form->{old_callback}; + $form->{TAX_ACCOUNTS} = [ @tax_accounts ]; - # put callback together - foreach $key (keys %$form) { + print $form->ajax_response_header, $form->parse_html_template("gl/update_tax_accounts"); - # do single escape for Apache 2.0 - $value = $form->escape($form->{$key}, 1); - $callback .= qq|&$key=$value|; - } - $form->{callback} = $callback; + $main::lxdebug->leave_sub(); - # redirect - $form->redirect; - $lxdebug->leave_sub(); } + +1;