X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/ff159a4d47b9a2d10744dcfc23da2c63605c8a32..eeb5375ee7727c956cc357cc8f90b19d1bfe80b9:/SL/AP.pm diff --git a/SL/AP.pm b/SL/AP.pm index d399b7c1b..753c94b5f 100644 --- a/SL/AP.pm +++ b/SL/AP.pm @@ -44,11 +44,14 @@ use SL::DB::Default; use SL::DB::Draft; use SL::DB::Order; use SL::DB::PurchaseInvoice; +use SL::DB::EmailJournal; +use SL::DB::ValidityToken; use SL::Util qw(trim); use SL::DB; use Data::Dumper; use List::Util qw(sum0); use strict; +use URI::Escape; sub post_transaction { my ($self, $myconfig, $form, $provided_dbh, %params) = @_; @@ -63,6 +66,16 @@ sub post_transaction { sub _post_transaction { my ($self, $myconfig, $form, $provided_dbh, %params) = @_; + my $validity_token; + if (!$form->{id}) { + $validity_token = SL::DB::Manager::ValidityToken->fetch_valid_token( + scope => SL::DB::ValidityToken::SCOPE_PURCHASE_INVOICE_POST(), + token => $form->{form_validity_token}, + ); + + die $::locale->text('The form is not valid anymore.') if !$validity_token; + } + my $payments_only = $params{payments_only}; my $dbh = $provided_dbh || SL::DB->client->dbh; @@ -71,14 +84,50 @@ sub _post_transaction { $form->{defaultcurrency} = $form->get_default_currency($myconfig); $form->{taxincluded} = 0 unless $form->{taxincluded}; + $form->{script} = 'ap.pl' unless $form->{script}; + + # make sure to have a id + my ($query, $sth, @values); + if (!$payments_only) { + # if we have an id delete old records + if ($form->{id}) { + # delete detail records + $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|; + do_query($form, $dbh, $query, $form->{id}); + + } else { + + ($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|); + + $query = + qq|INSERT INTO ap (id, invnumber, employee_id,currency_id, taxzone_id) | . + qq|VALUES (?, ?, (SELECT e.id FROM employee e WHERE e.login = ?), + (SELECT id FROM currencies WHERE name = ?), (SELECT taxzone_id FROM vendor WHERE id = ?) )|; + do_query($form, $dbh, $query, $form->{id}, $form->{invnumber}, $::myconfig{login}, $form->{currency}, $form->{vendor_id}); + + } + } + # check default or record exchangerate if ($form->{currency} eq $form->{defaultcurrency}) { $form->{exchangerate} = 1; } else { $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, 'sell'); - $form->{exchangerate} = $exchangerate || $form->parse_amount($myconfig, $form->{exchangerate}); + $form->{exchangerate} = $form->parse_amount($myconfig, $form->{exchangerate}, 5); + + # if default exchangerate is not defined, define one + unless ($exchangerate) { + $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0, $form->{exchangerate}); + # delete records exchangerate -> if user sets new invdate for record + $query = qq|UPDATE ap set exchangerate = NULL where id = ?|; + do_query($form, $dbh, $query, $form->{"id"}); + } + # update record exchangerate, if the default is set and differs from current + if ($exchangerate && ($form->{exchangerate} != $exchangerate)) { + $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, + 0, $form->{exchangerate}, $form->{id}, 'ap'); + } } - # get the charts selected $form->{AP_amounts}{"amount_$_"} = $form->{"AP_amount_chart_id_$_"} for (1 .. $form->{rowcount}); @@ -110,39 +159,13 @@ sub _post_transaction { # amount for total AP $form->{payables} = $form->{invtotal}; - # update exchangerate - if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) { - $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0, - $form->{exchangerate}); - } - - my ($query, $sth, @values); - if (!$payments_only) { - # if we have an id delete old records - if ($form->{id}) { - - # delete detail records - $query = qq|DELETE FROM acc_trans WHERE trans_id = ?|; - do_query($form, $dbh, $query, $form->{id}); - - } else { - - ($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|); - - $query = - qq|INSERT INTO ap (id, invnumber, employee_id,currency_id, taxzone_id) | . - qq|VALUES (?, ?, (SELECT e.id FROM employee e WHERE e.login = ?), - (SELECT id FROM currencies WHERE name = ?), (SELECT taxzone_id FROM vendor WHERE id = ?) )|; - do_query($form, $dbh, $query, $form->{id}, $form->{invnumber}, $::myconfig{login}, $form->{currency}, $form->{vendor_id}); - - } - $query = qq|UPDATE ap SET invnumber = ?, transdate = ?, ordnumber = ?, vendor_id = ?, taxincluded = ?, amount = ?, duedate = ?, deliverydate = ?, tax_point = ?, paid = ?, netamount = ?, currency_id = (SELECT id FROM currencies WHERE name = ?), notes = ?, department_id = ?, storno = ?, storno_id = ?, - globalproject_id = ?, direct_debit = ?, payment_id = ?, transaction_description = ? + globalproject_id = ?, direct_debit = ?, payment_id = ?, transaction_description = ?, intnotes = ?, + qrbill_data = ? WHERE id = ?|; @values = ($form->{invnumber}, conv_date($form->{transdate}), $form->{ordnumber}, conv_i($form->{vendor_id}), @@ -154,6 +177,8 @@ sub _post_transaction { $form->{storno_id}, conv_i($form->{globalproject_id}), $form->{direct_debit} ? 't' : 'f', conv_i($form->{payment_id}), $form->{transaction_description}, + $form->{intnotes}, + $form->{qrbill_data_encoded} ? uri_unescape($form->{qrbill_data_encoded}) : undef, $form->{id}); do_query($form, $dbh, $query, @values); @@ -161,7 +186,7 @@ sub _post_transaction { # Link this record to the record it was created from. my $convert_from_oe_id = delete $form->{convert_from_oe_id}; - if (!$form->{postasnew} && $convert_from_oe_id) { + if ($convert_from_oe_id) { RecordLinks->create_links('dbh' => $dbh, 'mode' => 'ids', 'from_table' => 'oe', @@ -201,7 +226,7 @@ sub _post_transaction { $form->{"AP_amount_chart_id_$i"}); do_query($form, $dbh, $query, @values); - if ($form->{"tax_$i"} != 0) { + if ($form->{"tax_$i"} != 0 && !$form->{"reverse_charge_$i"}) { # insert detail records in acc_trans $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, | . @@ -408,7 +433,7 @@ sub _post_transaction { } # hook for taxkey 94 - $self->_reverse_charge($myconfig, $form); + $self->_reverse_charge($myconfig, $form) unless $payments_only; # safety check datev export if ($::instance_conf->get_datev_check_on_ap_transaction) { my $datev = SL::DATEV->new( @@ -422,6 +447,9 @@ sub _post_transaction { } } + $validity_token->delete if $validity_token; + delete $form->{form_validity_token}; + return 1; } @@ -526,7 +554,7 @@ sub ap_transactions { my $query = qq|SELECT a.id, a.invnumber, a.transdate, a.duedate, a.amount, a.paid, | . qq| a.ordnumber, v.name, a.invoice, a.netamount, a.datepaid, a.notes, | . - qq| a.globalproject_id, a.storno, a.storno_id, a.direct_debit, | . + qq| a.intnotes, a.globalproject_id, a.storno, a.storno_id, a.direct_debit, | . qq| a.transaction_description, a.itime::DATE AS insertdate, | . qq| pr.projectnumber AS globalprojectnumber, | . qq| e.name AS employee, | . @@ -622,6 +650,10 @@ sub ap_transactions { $where .= " AND a.taxzone_id = ?"; push(@values, $form->{taxzone_id}); } + if ($form->{payment_id}) { + $where .= " AND a.payment_id = ?"; + push(@values, $form->{payment_id}); + } if ($form->{transaction_description}) { $where .= " AND a.transaction_description ILIKE ?"; push(@values, like($form->{transaction_description})); @@ -630,6 +662,10 @@ sub ap_transactions { $where .= " AND a.notes ILIKE ?"; push(@values, like($form->{notes})); } + if ($form->{intnotes}) { + $where .= " AND a.intnotes ILIKE ?"; + push(@values, like($form->{intnotes})); + } if ($form->{project_id}) { $where .= qq| AND ((a.globalproject_id = ?) OR EXISTS | . @@ -658,6 +694,22 @@ sub ap_transactions { $where .= " AND a.duedate <= ?"; push(@values, trim($form->{duedateto})); } + if ($form->{datepaidfrom}) { + $where .= " AND a.datepaid >= ?"; + push(@values, trim($form->{datepaidfrom})); + } + if ($form->{datepaidto}) { + $where .= " AND a.datepaid <= ?"; + push(@values, trim($form->{datepaidto})); + } + if ($form->{insertdatefrom}) { + $where .= " AND a.itime >= ?"; + push(@values, trim($form->{insertdatefrom})); + } + if ($form->{insertdateto}) { + $where .= " AND a.itime <= ?"; + push(@values, trim($form->{insertdateto})); + } if ($form->{open} || $form->{closed}) { unless ($form->{open} && $form->{closed}) { $where .= " AND a.amount <> a.paid" if ($form->{open}); @@ -665,6 +717,29 @@ sub ap_transactions { } } + $form->{fulltext} = trim($form->{fulltext}); + if ($form->{fulltext}) { + my @fulltext_fields = qw(a.notes + a.intnotes + a.shipvia + a.transaction_description + a.quonumber + a.ordnumber + a.invnumber); + $where .= ' AND ('; + $where .= join ' OR ', map {"$_ ILIKE ?"} @fulltext_fields; + + $where .= <{fulltext})) for 1 .. (scalar @fulltext_fields) + 1; + } + if ($form->{parts_partnumber}) { $where .= <{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC'; my $sortorder = join(', ', map { "$_ $sortdir" } @a); - if (grep({ $_ eq $form->{sort} } qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee transaction_description direct_debit department taxzone))) { + if (grep({ $_ eq $form->{sort} } qw(transdate id invnumber ordnumber name netamount tax amount paid datepaid due duedate notes employee transaction_description direct_debit department taxzone insertdate))) { $sortorder = $form->{sort} . " $sortdir"; } @@ -712,6 +787,26 @@ SQL $form->{AP} = [ @result ]; + if ($form->{l_items} && scalar @{ $form->{AP} }) { + my ($items_query, $items_sth); + if ($form->{l_items}) { + $items_query = + qq|SELECT id + FROM invoice + WHERE trans_id = ? + ORDER BY position|; + + $items_sth = prepare_query($form, $dbh, $items_query); + } + + foreach my $ap (@{ $form->{AP} }) { + do_statement($form, $items_sth, $items_query, $ap->{id}); + $ap->{item_ids} = $dbh->selectcol_arrayref($items_sth); + $ap->{item_ids} = undef if !@{$ap->{item_ids}}; + } + $items_sth->finish(); + } + $main::lxdebug->leave_sub(); } @@ -895,6 +990,8 @@ sub setup_form { $form->{"forex_$j"} = $form->{"exchangerate_$i"}; $form->{"AP_paid_$j"} = $form->{acc_trans}{$key}->[$i-1]->{accno}; $form->{"paid_project_id_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{project_id}; + $form->{"defaultcurrency_paid_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{defaultcurrency_paid}; + $form->{"fx_transaction_$j"} = $form->{acc_trans}{$key}->[$i - 1]->{fx_transaction}; $form->{paidaccounts}++; } else { @@ -1016,6 +1113,19 @@ sub _storno { map { IO->set_datepaid(table => 'ap', id => $_, dbh => $dbh) } ($id, $new_id); + if ($form->{workflow_email_journal_id}) { + my $ap_transaction_storno = SL::DB::PurchaseInvoice->new(id => $new_id)->load; + my $email_journal = SL::DB::EmailJournal->new( + id => delete $form->{workflow_email_journal_id} + )->load; + $email_journal->link_to_record_with_attachment( + $ap_transaction_storno, + delete $form->{workflow_email_attachment_id} + ); + $form->{callback} = delete $form->{workflow_email_callback}; + } + + $form->{storno_id} = $id; return 1; }