X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FIS.pm;h=f79952ec94098ce98cd9aa5ccc1f938b64140f93;hb=9f4ef62c42dceef5a27ad3a0d8c8d260ffef9848;hp=609d3c015fcfc3517102c848c3ec31f027911ffd;hpb=0aa885f4f0bb531a7691a641f954ff296ffa04a5;p=kivitendo-erp.git diff --git a/SL/IS.pm b/SL/IS.pm index 609d3c015..f79952ec9 100644 --- a/SL/IS.pm +++ b/SL/IS.pm @@ -442,9 +442,9 @@ sub invoice_details { my $sortorder = ""; if ($form->{groupitems}) { $sortorder = - qq|ORDER BY pg.partsgroup, a.oid|; + qq|ORDER BY pg.partsgroup, a.position|; } else { - $sortorder = qq|ORDER BY a.oid|; + $sortorder = qq|ORDER BY a.position|; } my $query = @@ -506,7 +506,25 @@ sub invoice_details { push(@{ $form->{TEMPLATE_ARRAYS}->{taxrate_nofmt} }, $form->{"${item}_rate"} * 100); push(@{ $form->{TEMPLATE_ARRAYS}->{taxnumber} }, $form->{"${item}_taxnumber"}); - my $tax_obj = SL::DB::Manager::Tax->find_by(taxnumber => $form->{"${item}_taxnumber"}); + # taxnumber is used for grouping the amount of the various taxes + + # this code assumes that at most one tax entry can point to the same + # chart_id, even though chart_id does not have a unique constraint! + + # this chart_id is then looked up via its accno, which is the key that is + # used to group the different taxes by for a record + + # not every tax has a taxnumber (e.g. tax-free), but that is ok, because + # then there would be no tax amount to assign it to + + my $tax_objs = SL::DB::Manager::Tax->get_objects_from_sql( + sql => 'SELECT * FROM tax WHERE chart_id = (SELECT id FROM chart WHERE accno = ?)', + args => [ $form->{"${item}_taxnumber"} ] + ); + my $tax_obj; + if ( $tax_objs ) { + $tax_obj = $tax_objs->[0]; + } my $description = $tax_obj ? $tax_obj->translated_attribute('taxdescription', $form->{language_id}, 0) : ''; push(@{ $form->{TEMPLATE_ARRAYS}->{taxdescription} }, $description . q{ } . 100 * $form->{"${item}_rate"} . q{%}); } @@ -562,20 +580,6 @@ sub invoice_details { $main::lxdebug->leave_sub(); } -sub project_description { - $main::lxdebug->enter_sub(); - - my ($self, $dbh, $id) = @_; - my $form = \%main::form; - - my $query = qq|SELECT description FROM project WHERE id = ?|; - my ($description) = selectrow_query($form, $dbh, $query, conv_i($id)); - - $main::lxdebug->leave_sub(); - - return $_; -} - sub customer_details { $main::lxdebug->enter_sub(); @@ -682,18 +686,19 @@ sub customer_details { } sub post_invoice { - my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_; + my ($self, $myconfig, $form, $provided_dbh, %params) = @_; $main::lxdebug->enter_sub(); - my $rc = SL::DB->client->with_transaction(\&_post_invoice, $self, $myconfig, $form, $provided_dbh, $payments_only); + my $rc = SL::DB->client->with_transaction(\&_post_invoice, $self, $myconfig, $form, $provided_dbh, %params); $::lxdebug->leave_sub; return $rc; } sub _post_invoice { - my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_; + my ($self, $myconfig, $form, $provided_dbh, %params) = @_; + my $payments_only = $params{payments_only}; my $dbh = $provided_dbh || SL::DB->client->dbh; my $restricter = SL::HTML::Restrict->create; @@ -1144,6 +1149,8 @@ SQL } } + my %already_cleared = %{ $params{already_cleared} // {} }; + # record payments and offsetting AR if (!$form->{storno}) { for my $i (1 .. $form->{paidaccounts}) { @@ -1173,9 +1180,16 @@ SQL # record AR $amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} + $diff, 2); + my $new_cleared = !$form->{"acc_trans_id_$i"} ? 'f' + : !$already_cleared{$form->{"acc_trans_id_$i"}} ? 'f' + : $already_cleared{$form->{"acc_trans_id_$i"}}->{amount} != $form->{"paid_$i"} * -1 ? 'f' + : $already_cleared{$form->{"acc_trans_id_$i"}}->{accno} != $accno ? 'f' + : $already_cleared{$form->{"acc_trans_id_$i"}}->{cleared} ? 't' + : 'f'; + if ($form->{amount}{ $form->{id} }{ $form->{AR} } != 0) { $query = - qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, tax_id, taxkey, project_id, chart_link) + qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, tax_id, taxkey, project_id, cleared, chart_link) VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, (SELECT tax_id FROM taxkeys @@ -1191,9 +1205,9 @@ SQL WHERE accno = ?) AND startdate <= ? ORDER BY startdate DESC LIMIT 1), - ?, + ?, ?, (SELECT link FROM chart WHERE accno = ?))|; - @values = (conv_i($form->{"id"}), $form->{AR}, $amount, $form->{"datepaid_$i"}, $form->{AR}, conv_date($taxdate), $form->{AR}, conv_date($taxdate), $project_id, $form->{AR}); + @values = (conv_i($form->{"id"}), $form->{AR}, $amount, $form->{"datepaid_$i"}, $form->{AR}, conv_date($taxdate), $form->{AR}, conv_date($taxdate), $project_id, $new_cleared, $form->{AR}); do_query($form, $dbh, $query, @values); } @@ -1202,7 +1216,7 @@ SQL my $gldate = (conv_date($form->{"gldate_$i"}))? conv_date($form->{"gldate_$i"}) : conv_date($form->current_date($myconfig)); $query = - qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, tax_id, taxkey, project_id, chart_link) + qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, tax_id, taxkey, project_id, cleared, chart_link) VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, (SELECT tax_id FROM taxkeys @@ -1218,10 +1232,10 @@ SQL WHERE accno = ?) AND startdate <= ? ORDER BY startdate DESC LIMIT 1), - ?, + ?, ?, (SELECT link FROM chart WHERE accno = ?))|; @values = (conv_i($form->{"id"}), $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"}, - $gldate, $form->{"source_$i"}, $form->{"memo_$i"}, $accno, conv_date($taxdate), $accno, conv_date($taxdate), $project_id, $accno); + $gldate, $form->{"source_$i"}, $form->{"memo_$i"}, $accno, conv_date($taxdate), $accno, conv_date($taxdate), $project_id, $new_cleared, $accno); do_query($form, $dbh, $query, @values); # exchangerate difference @@ -1620,6 +1634,15 @@ sub _post_payment { $old_form = save_form(); + $query = <{id}); + # Delete all entries in acc_trans from prior payments. if (SL::DB::Default->get->payments_changeable != 0) { $self->_delete_payments($form, $dbh); @@ -1666,7 +1689,7 @@ sub _post_payment { ($form->{AR}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id})); # Post the new payments. - $self->post_invoice($myconfig, $form, $dbh, 1); + $self->post_invoice($myconfig, $form, $dbh, payments_only => 1, already_cleared => \%already_cleared); restore_form($old_form); @@ -2064,7 +2087,8 @@ sub _retrieve_invoice { # get tax rates and description my $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno}; $query = - qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber FROM tax t + qq|SELECT c.accno, t.taxdescription, t.rate, c.accno as taxnumber + FROM tax t LEFT JOIN chart c ON (c.id = t.chart_id) WHERE t.id IN (SELECT tk.tax_id FROM taxkeys tk @@ -2158,7 +2182,7 @@ sub get_customer { $ref = selectfirst_hashref_query($form, $dbh, $query, @values); delete $ref->{salesman_id} if !$ref->{salesman_id}; - delete $ref->{payment_id} if $form->{payment_id}; + delete $ref->{payment_id} if !$ref->{payment_id}; map { $form->{$_} = $ref->{$_} } keys %$ref; @@ -2388,7 +2412,7 @@ sub retrieve_item { # get tax rates and description my $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno}; $query = - qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber + qq|SELECT c.accno, t.taxdescription, t.rate, c.accno as taxnumber FROM tax t LEFT JOIN chart c ON (c.id = t.chart_id) WHERE t.id in @@ -2437,7 +2461,6 @@ sub retrieve_item { } $ref->{onhand} *= 1; - push @{ $form->{item_list} }, $ref; } $sth->finish;