X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAR.pm;h=692967fae01a3024dad8c82fb3cacdef5b79bb3f;hb=071e5546e649fc0108f64adfb1d0f8356dbe2716;hp=7d037838cf10ae9258bd0dd062133f6e51a2360e;hpb=09fe7f33a735ecc3f4a3112ad716f674983b00cc;p=kivitendo-erp.git diff --git a/SL/AR.pm b/SL/AR.pm index 7d037838c..692967fae 100644 --- a/SL/AR.pm +++ b/SL/AR.pm @@ -35,9 +35,12 @@ package AR; use Data::Dumper; +use SL::DATEV qw(:CONSTANTS); use SL::DBUtils; use SL::IO; use SL::MoreCommon; +use SL::DB::Default; +use SL::TransNumber; use strict; @@ -66,40 +69,11 @@ sub post_transaction { $form->{AR_amounts}{receivables} = $form->{ARselected}; $form->{AR}{receivables} = $form->{ARselected}; - # parsing - for $i (1 .. $form->{rowcount}) { - $form->{"amount_$i"} = $form->round_amount($form->parse_amount($myconfig, $form->{"amount_$i"}) * $form->{exchangerate}, 2); - $form->{amount} += $form->{"amount_$i"}; - $form->{"tax_$i"} = $form->parse_amount($myconfig, $form->{"tax_$i"}); - } - - # this is for ar - $form->{tax} = 0; - $form->{netamount} = 0; - $form->{total_tax} = 0; - - # taxincluded doesn't make sense if there is no amount - $form->{taxincluded} = 0 unless $form->{amount}; - - for $i (1 .. $form->{rowcount}) { - ($form->{"tax_id_$i"}) = split /--/, $form->{"taxchart_$i"}; - - $query = qq|SELECT c.accno, t.taxkey, t.rate FROM tax t LEFT JOIN chart c ON (c.id = t.chart_id) WHERE t.id = ? ORDER BY c.accno|; - ($form->{AR_amounts}{"tax_$i"}, $form->{"taxkey_$i"}, $form->{"taxrate_$i"}) = selectrow_query($form, $dbh, $query, $form->{"tax_id_$i"}); + $form->{tax} = 0; # is this still needed? - if ($form->{taxincluded} *= 1) { - $tax = $form->{"korrektur_$i"} - ? $form->{"tax_$i"} - : $form->{"amount_$i"} - ($form->{"amount_$i"} / ($form->{"taxrate_$i"} + 1)); # should be same as taxrate * amount / (taxrate + 1) - $form->{"amount_$i"} = $form->round_amount($form->{"amount_$i"} - $tax, 2); - $form->{"tax_$i"} = $form->round_amount($tax, 2); - } else { - $form->{"tax_$i"} = $form->{"amount_$i"} * $form->{"taxrate_$i"} unless $form->{"korrektur_$i"}; - $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"} * $form->{exchangerate}, 2); - } - $form->{netamount} += $form->{"amount_$i"}; - $form->{total_tax} += $form->{"tax_$i"}; - } + # main calculation of rowcount loop inside Form method, amount_$i and tax_$i get formatted + $form->{taxincluded} = 0 unless $form->{taxincluded}; + ($form->{netamount},$form->{total_tax},$form->{amount}) = $form->calculate_arap('sell', $form->{taxincluded}, $form->{exchangerate}); # adjust paidaccounts if there is no date in the last row # this does not apply to stornos, where the paid field is set manually @@ -114,7 +88,6 @@ sub post_transaction { $form->{datepaid} = $form->{"datepaid_$i"}; } - $form->{amount} = $form->{netamount} + $form->{total_tax}; } $form->{paid} = $form->round_amount($form->{paid} * ($form->{exchangerate} || 1), 2); @@ -132,15 +105,17 @@ sub post_transaction { } else { $query = qq|SELECT nextval('glid')|; ($form->{id}) = selectrow_query($form, $dbh, $query); - $query = qq|INSERT INTO ar (id, invnumber, employee_id) VALUES (?, 'dummy', ?)|; - do_query($form, $dbh, $query, $form->{id}, $form->{employee_id}); - $form->{invnumber} = $form->update_defaults($myconfig, "invnumber", $dbh) unless $form->{invnumber}; + $query = qq|INSERT INTO ar (id, invnumber, employee_id, currency_id, taxzone_id) VALUES (?, 'dummy', ?, (SELECT id FROM currencies WHERE name=?), (SELECT taxzone_id FROM customer WHERE id = ?))|; + do_query($form, $dbh, $query, $form->{id}, $form->{employee_id}, $form->{currency}, $form->{customer_id}); + if (!$form->{invnumber}) { + my $trans_number = SL::TransNumber->new(type => 'invoice', dbh => $dbh, number => $form->{partnumber}, id => $form->{id}); + $form->{invnumber} = $trans_number->create_unique; + } } } # update department ($null, $form->{department_id}) = split(/--/, $form->{department}); - $form->{department_id} *= 1; # amount for AR account $form->{receivables} = $form->round_amount($form->{amount}, 2) * -1; @@ -154,12 +129,14 @@ sub post_transaction { qq|UPDATE ar set invnumber = ?, ordnumber = ?, transdate = ?, customer_id = ?, taxincluded = ?, amount = ?, duedate = ?, paid = ?, - netamount = ?, curr = ?, notes = ?, department_id = ?, - employee_id = ?, storno = ?, storno_id = ? + netamount = ?, notes = ?, department_id = ?, + employee_id = ?, storno = ?, storno_id = ?, globalproject_id = ?, + direct_debit = ? WHERE id = ?|; my @values = ($form->{invnumber}, $form->{ordnumber}, conv_date($form->{transdate}), conv_i($form->{customer_id}), $form->{taxincluded} ? 't' : 'f', $form->{amount}, - conv_date($form->{duedate}), $form->{paid}, $form->{netamount}, $form->{currency}, $form->{notes}, conv_i($form->{department_id}), - conv_i($form->{employee_id}), $form->{storno} ? 't' : 'f', $form->{storno_id}, conv_i($form->{id})); + conv_date($form->{duedate}), $form->{paid}, $form->{netamount}, $form->{notes}, conv_i($form->{department_id}), + conv_i($form->{employee_id}), $form->{storno} ? 't' : 'f', $form->{storno_id}, + conv_i($form->{globalproject_id}), $form->{direct_debit} ? 't' : 'f', conv_i($form->{id})); do_query($form, $dbh, $query, @values); # add individual transactions for AR, amount and taxes @@ -168,27 +145,36 @@ sub post_transaction { my $project_id = conv_i($form->{"project_id_$i"}); # insert detail records in acc_trans - $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) - VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|; - @values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"amount_$i"}), conv_i($form->{"amount_$i"}), conv_date($form->{transdate}), $project_id, - conv_i($form->{"taxkey_$i"})); + $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link) + VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?, ?, (SELECT c.link FROM chart c WHERE c.accno = ?))|; + @values = (conv_i($form->{id}), $form->{AR_amounts}{"amount_$i"}, conv_i($form->{"amount_$i"}), conv_date($form->{transdate}), $project_id, + conv_i($form->{"taxkey_$i"}), conv_i($form->{"tax_id_$i"}), $form->{AR_amounts}{"amount_$i"}); do_query($form, $dbh, $query, @values); if ($form->{"tax_$i"} != 0) { # insert detail records in acc_trans - $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) - VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?)|; - @values = (conv_i($form->{id}), conv_i($form->{AR_amounts}{"tax_$i"}), conv_i($form->{"tax_$i"}), conv_date($form->{transdate}), $project_id, - conv_i($form->{"taxkey_$i"})); + $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link) + VALUES (?, (SELECT c.id FROM chart c WHERE c.accno = ?), ?, ?, ?, ?, ?, (SELECT c.link FROM chart c WHERE c.accno = ?))|; + @values = (conv_i($form->{id}), $form->{AR_amounts}{"tax_$i"}, conv_i($form->{"tax_$i"}), conv_date($form->{transdate}), $project_id, + conv_i($form->{"taxkey_$i"}), conv_i($form->{"tax_id_$i"}), $form->{AR_amounts}{"tax_$i"}); do_query($form, $dbh, $query, @values); } } } # add recievables - $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey) - VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|; - @values = (conv_i($form->{id}), $form->{AR_amounts}{receivables}, conv_i($form->{receivables}), conv_date($form->{transdate}), $form->{AR_amounts}{receivables}); + $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, tax_id, chart_link) + VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?), + (SELECT tax_id + FROM taxkeys + WHERE chart_id= (SELECT id + FROM chart + WHERE accno = ?) + AND startdate <= ? + ORDER BY startdate DESC LIMIT 1), + (SELECT c.link FROM chart c WHERE c.accno = ?))|; + @values = (conv_i($form->{id}), $form->{AR_amounts}{receivables}, conv_i($form->{receivables}), conv_date($form->{transdate}), + $form->{AR_amounts}{receivables}, $form->{AR_amounts}{receivables}, conv_date($form->{transdate}), $form->{AR_amounts}{receivables}); do_query($form, $dbh, $query, @values); } else { @@ -199,6 +185,11 @@ sub post_transaction { # add paid transactions for my $i (1 .. $form->{paidaccounts}) { + + if ($form->{"acc_trans_id_$i"} && $payments_only && (SL::DB::Default->get->payments_changeable == 0)) { + next; + } + if ($form->{"paid_$i"} != 0) { my $project_id = conv_i($form->{"paid_project_id_$i"}); @@ -220,28 +211,57 @@ sub post_transaction { if ($amount != 0) { # add receivable - $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey) - VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|; - @values = (conv_i($form->{id}), $form->{AR}{receivables}, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AR}{receivables}); + $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, project_id, taxkey, tax_id, chart_link) + VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?), + (SELECT tax_id + FROM taxkeys + WHERE chart_id= (SELECT id + FROM chart + WHERE accno = ?) + AND startdate <= ? + ORDER BY startdate DESC LIMIT 1), + (SELECT c.link FROM chart c WHERE c.accno = ?))|; + @values = (conv_i($form->{id}), $form->{AR}{receivables}, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AR}{receivables}, $form->{AR}{receivables}, conv_date($form->{"datepaid_$i"}), + $form->{AR}{receivables}); + do_query($form, $dbh, $query, @values); } if ($form->{"paid_$i"} != 0) { - my $project_id = conv_i($form->{"paid_project_id_$i"}); # add payment + my $project_id = conv_i($form->{"paid_project_id_$i"}); + my $gldate = (conv_date($form->{"gldate_$i"}))? conv_date($form->{"gldate_$i"}) : conv_date($form->current_date($myconfig)); $amount = $form->{"paid_$i"} * -1; - $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, source, memo, project_id, taxkey) - VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|; - @values = (conv_i($form->{id}), $form->{AR}{"paid_$i"}, $amount, conv_date($form->{"datepaid_$i"}), $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $form->{AR}{"paid_$i"}); + $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, project_id, taxkey, tax_id, chart_link) + VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, ?, (SELECT taxkey_id FROM chart WHERE accno = ?), + (SELECT tax_id + FROM taxkeys + WHERE chart_id= (SELECT id + FROM chart + WHERE accno = ?) + AND startdate <= ? + ORDER BY startdate DESC LIMIT 1), + (SELECT c.link FROM chart c WHERE c.accno = ?))|; + @values = (conv_i($form->{id}), $form->{AR}{"paid_$i"}, $amount, conv_date($form->{"datepaid_$i"}), $gldate, $form->{"source_$i"}, $form->{"memo_$i"}, $project_id, $form->{AR}{"paid_$i"}, + $form->{AR}{"paid_$i"}, conv_date($form->{"datepaid_$i"}), $form->{AR}{"paid_$i"}); do_query($form, $dbh, $query, @values); # exchangerate difference for payment $amount = $form->round_amount( $form->{"paid_$i"} * ($form->{"exchangerate_$i"} - 1) * -1, 2); if ($amount != 0) { - $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) - VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|; - @values = (conv_i($form->{id}), $form->{AR}{"paid_$i"}, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AR}{"paid_$i"}); + $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey, tax_id, chart_link) + VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, (SELECT taxkey_id FROM chart WHERE accno = ?), + (SELECT tax_id + FROM taxkeys + WHERE chart_id= (SELECT id + FROM chart + WHERE accno = ?) + AND startdate <= ? + ORDER BY startdate DESC LIMIT 1), + (SELECT c.link FROM chart c WHERE c.accno = ?))|; + @values = (conv_i($form->{id}), $form->{AR}{"paid_$i"}, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AR}{"paid_$i"}, + $form->{AR}{"paid_$i"}, conv_date($form->{"datepaid_$i"}), $form->{AR}{"paid_$i"}); do_query($form, $dbh, $query, @values); } @@ -250,9 +270,17 @@ sub post_transaction { if ($amount != 0) { my $accno = ($amount > 0) ? $form->{fxgain_accno} : $form->{fxloss_accno}; - $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey) - VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, (SELECT taxkey_id FROM chart WHERE accno = ?))|; - @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $accno); + $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, fx_transaction, cleared, project_id, taxkey, tax_id, chart_link) + VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, 't', 'f', ?, (SELECT taxkey_id FROM chart WHERE accno = ?), + (SELECT tax_id + FROM taxkeys + WHERE chart_id= (SELECT id + FROM chart + WHERE accno = ?) + AND startdate <= ? + ORDER BY startdate DESC LIMIT 1), + (SELECT c.link FROM chart c WHERE c.accno = ?))|; + @values = (conv_i($form->{id}), $accno, $amount, conv_date($form->{"datepaid_$i"}), $project_id, $accno, $accno, conv_date($form->{"datepaid_$i"}), $accno); do_query($form, $dbh, $query, @values); } } @@ -265,6 +293,28 @@ sub post_transaction { IO->set_datepaid(table => 'ar', id => $form->{id}, dbh => $dbh); + # safety check datev export + if ($::instance_conf->get_datev_check_on_ar_transaction) { + my $transdate = $::form->{transdate} ? DateTime->from_lxoffice($::form->{transdate}) : undef; + $transdate ||= DateTime->today; + + my $datev = SL::DATEV->new( + exporttype => DATEV_ET_BUCHUNGEN, + format => DATEV_FORMAT_KNE, + dbh => $dbh, + from => $transdate, + to => $transdate, + trans_id => $form->{id}, + ); + + $datev->export; + + if ($datev->errors) { + $dbh->rollback; + die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors; + } + } + my $rc = 1; if (!$provided_dbh) { $rc = $dbh->commit(); @@ -326,10 +376,12 @@ sub post_payment { $old_form = save_form(); # Delete all entries in acc_trans from prior payments. - $self->_delete_payments($form, $dbh); + if (SL::DB::Default->get->payments_changeable != 0) { + $self->_delete_payments($form, $dbh); + } # Save the new payments the user made before cleaning up $form. - my $payments_re = '^datepaid_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^paid_project_id_\d+$|^AR_paid_\d+$|^paidaccounts$'; + my $payments_re = '^datepaid_\d+$|^gldate_\d+$|^acc_trans_id_\d+$|^memo_\d+$|^source_\d+$|^exchangerate_\d+$|^paid_\d+$|^paid_project_id_\d+$|^AR_paid_\d+$|^paidaccounts$'; map { $payments{$_} = $form->{$_} } grep m/$payments_re/, keys %{ $form }; # Clean up $form so that old content won't tamper the results. @@ -344,13 +396,10 @@ sub post_payment { # Set up the content of $form in the way that AR::post_transaction() expects. - $self->setup_form($form); - - ($form->{defaultcurrency}) = selectrow_query($form, $dbh, qq|SELECT curr FROM defaults|); - $form->{defaultcurrency} = (split m/:/, $form->{defaultcurrency})[0]; - $form->{currency} = $form->{defaultcurrency} if ($form->{defaultcurrency} && ($form->{currency} =~ m/^\s*$/)); + $self->setup_form($form, 1); - $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate}); + $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate}); + $form->{defaultcurrency} = $form->get_default_currency($myconfig); # Get the AR accno (which is normally done by Form::create_links()). $query = @@ -385,12 +434,10 @@ sub delete_transaction { # connect to database, turn AutoCommit off my $dbh = $form->dbconnect_noauto($myconfig); + # acc_trans entries are deleted by database triggers. my $query = qq|DELETE FROM ar WHERE id = ?|; do_query($form, $dbh, $query, $form->{id}); - $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|; - do_query($form, $dbh, $query, $form->{id}); - # commit my $rc = $dbh->commit; $dbh->disconnect; @@ -411,14 +458,15 @@ sub ar_transactions { my @values; my $query = - qq|SELECT DISTINCT a.id, a.invnumber, a.ordnumber, a.transdate, | . + qq|SELECT DISTINCT a.id, a.invnumber, a.ordnumber, a.cusordnumber, a.transdate, | . qq| a.duedate, a.netamount, a.amount, a.paid, | . qq| a.invoice, a.datepaid, a.terms, a.notes, a.shipvia, | . qq| a.shippingpoint, a.storno, a.storno_id, a.globalproject_id, | . qq| a.marge_total, a.marge_percent, | . qq| a.transaction_description, | . qq| pr.projectnumber AS globalprojectnumber, | . - qq| c.name, c.customernumber, c.country, c.ustid, | . + qq| c.name, c.customernumber, c.country, c.ustid, b.description as customertype, | . + qq| c.id as customer_id, | . qq| e.name AS employee, | . qq| e2.name AS salesman, | . qq| tz.description AS taxzone, | . @@ -432,13 +480,25 @@ sub ar_transactions { ) AS charts } . qq|FROM ar a | . qq|JOIN customer c ON (a.customer_id = c.id) | . + qq|LEFT JOIN contacts cp ON (a.cp_id = cp.cp_id) | . qq|LEFT JOIN employee e ON (a.employee_id = e.id) | . qq|LEFT JOIN employee e2 ON (a.salesman_id = e2.id) | . qq|LEFT JOIN project pr ON (a.globalproject_id = pr.id)| . - qq|LEFT JOIN tax_zones tz ON (tz.id = c.taxzone_id)| . - qq|LEFT JOIN payment_terms pt ON (pt.id = c.payment_id)|; + qq|LEFT JOIN tax_zones tz ON (tz.id = a.taxzone_id)| . + qq|LEFT JOIN payment_terms pt ON (pt.id = a.payment_id)| . + qq|LEFT JOIN business b ON (b.id = c.business_id)| . + qq|LEFT JOIN department d ON (d.id = a.department_id)|; my $where = "1 = 1"; + + unless ( $::auth->assert('show_ar_transactions', 1) ) { + $where .= " AND NOT invoice = 'f' "; # remove ar transactions from Sales -> Reports -> Invoices + }; + + if ($form->{customernumber}) { + $where .= " AND c.customernumber = ?"; + push(@values, $form->{customernumber}); + } if ($form->{customer_id}) { $where .= " AND a.customer_id = ?"; push(@values, $form->{customer_id}); @@ -446,12 +506,26 @@ sub ar_transactions { $where .= " AND c.name ILIKE ?"; push(@values, $form->like($form->{customer})); } - if ($form->{department}) { - my ($null, $department_id) = split /--/, $form->{department}; + if ($form->{"cp_name"}) { + $where .= " AND (cp.cp_name ILIKE ? OR cp.cp_givenname ILIKE ?)"; + push(@values, ('%' . $form->{"cp_name"} . '%')x2); + } + if ($form->{business_id}) { + my $business_id = $form->{business_id}; + $where .= " AND c.business_id = ?"; + push(@values, $business_id); + } + if ($form->{department_id}) { + my $department_id = $form->{department_id}; $where .= " AND a.department_id = ?"; push(@values, $department_id); } - foreach my $column (qw(invnumber ordnumber notes transaction_description)) { + if ($form->{department}) { + my $department = "%" . $form->{department} . "%"; + $where .= " AND d.description ILIKE ?"; + push(@values, $department); + } + foreach my $column (qw(invnumber ordnumber cusordnumber notes transaction_description)) { if ($form->{$column}) { $where .= " AND a.$column ILIKE ?"; push(@values, $form->like($form->{$column})); @@ -461,8 +535,12 @@ sub ar_transactions { $where .= qq|AND ((a.globalproject_id = ?) OR EXISTS | . qq| (SELECT * FROM invoice i | . - qq| WHERE i.project_id = ? AND i.trans_id = a.id))|; - push(@values, $form->{"project_id"}, $form->{"project_id"}); + qq| WHERE i.project_id = ? AND i.trans_id = a.id) | . + qq| OR EXISTS | . + qq| (SELECT * FROM acc_trans at | . + qq| WHERE at.project_id = ? AND at.trans_id = a.id)| . + qq| )|; + push(@values, $form->{project_id}, $form->{project_id}, $form->{project_id}); } if ($form->{transdatefrom}) { @@ -480,12 +558,27 @@ sub ar_transactions { } } + if (!$main::auth->assert('sales_all_edit', 1)) { + # only show own invoices + $where .= " AND a.employee_id = (select id from employee where login= ?)"; + push (@values, $form->{login}); + } else { + if ($form->{employee_id}) { + $where .= " AND a.employee_id = ?"; + push @values, conv_i($form->{employee_id}); + } + if ($form->{salesman_id}) { + $where .= " AND a.salesman_id = ?"; + push @values, conv_i($form->{salesman_id}); + } + }; + my @a = qw(transdate invnumber name); push @a, "employee" if $form->{l_employee}; my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC'; my $sortorder = join(', ', map { "$_ $sortdir" } @a); - if (grep({ $_ eq $form->{sort} } qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description))) { + if (grep({ $_ eq $form->{sort} } qw(id transdate duedate invnumber ordnumber cusordnumber name datepaid employee shippingpoint shipvia transaction_description))) { $sortorder = $form->{sort} . " $sortdir"; } @@ -521,30 +614,36 @@ sub get_transdate { sub setup_form { $main::lxdebug->enter_sub(); - my ($self, $form) = @_; + my ($self, $form, $for_post_payments) = @_; my ($exchangerate, $akey, $j, $k, $index, $taxamount, $totaltax, $taxrate, $diff, $totalwithholding, $withholdingrate, - $totalamount, $taxincluded, $tax); + $totalamount, $tax); # forex $form->{forex} = $form->{exchangerate}; $exchangerate = $form->{exchangerate} ? $form->{exchangerate} : 1; foreach my $key (keys %{ $form->{AR_links} }) { - # if there is a value we have an old entry $j = 0; $k = 0; + # if there is a value we have an old entry next unless $form->{acc_trans}{$key}; + # do not use old entries for payments. They come from the form + # even if they are not changeable (then they are in hiddens) + next if $for_post_payments && $key eq "AR_paid"; + for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) { if ($key eq "AR_paid") { $j++; $form->{"AR_paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{accno}; + $form->{"acc_trans_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{acc_trans_id}; # reverse paid $form->{"paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1; $form->{"datepaid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate}; + $form->{"gldate_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{gldate}; $form->{"source_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{source}; $form->{"memo_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{memo}; $form->{"forex_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate}; @@ -602,7 +701,6 @@ sub setup_form { } } - $form->{taxincluded} = $taxincluded if ($form->{id}); $form->{paidaccounts} = 1 if not defined $form->{paidaccounts}; if ($form->{taxincluded} && $form->{taxrate} && $totalamount) { @@ -665,14 +763,14 @@ sub storno { } for my $row (@$rowref) { - delete @$row{qw(itime mtime link)}; + delete @$row{qw(itime mtime link acc_trans_id)}; $query = sprintf 'INSERT INTO acc_trans (%s) VALUES (%s)', join(', ', keys %$row), join(', ', map '?', values %$row); $row->{trans_id} = $new_id; $row->{amount} *= -1; do_query($form, $dbh, $query, (values %$row)); } - map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id); + map { IO->set_datepaid(table => 'ar', id => $_, dbh => $dbh) } ($id, $new_id); $dbh->commit; @@ -681,4 +779,3 @@ sub storno { 1; -