use SL::MoreCommon;
use SL::IC;
use SL::IO;
+use SL::TransNumber;
use Data::Dumper;
use strict;
my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_;
# connect to database, turn off autocommit
- my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig);
+ my $dbh = $provided_dbh ? $provided_dbh : $form->get_standard_dbh;
my ($query, $sth, $null, $project_id, @values);
my $exchangerate = 0;
}
$form->{defaultcurrency} = $form->get_default_currency($myconfig);
-
- ($null, $form->{department_id}) = split(/--/, $form->{department});
+ # Seit neuestem wird die department_id schon übergeben UND $form->department nicht mehr
+ # korrekt zusammengebaut. Sehr wahrscheinlich beim Umstieg auf T8 kaputt gegangen
+ # Ich lass den Code von 2005 erstmal noch stehen ;-) jb 03-2011
+ if (!$form->{department_id}){
+ ($null, $form->{department_id}) = split(/--/, $form->{department});
+ }
my $all_units = AM->retrieve_units($myconfig, $form);
&reverse_invoice($dbh, $form);
} else {
+ my $trans_number = SL::TransNumber->new(type => $form->{type}, dbh => $dbh, number => $form->{invnumber}, save => 1);
+ $form->{invnumber} = $trans_number->create_unique unless $trans_number->is_unique;
+
$query = qq|SELECT nextval('glid')|;
($form->{"id"}) = selectrow_query($form, $dbh, $query);
$query = qq|UPDATE ar SET paid = ? WHERE id = ?|;
do_query($form, $dbh, $query, $form->{paid}, conv_i($form->{id}));
- if (!$provided_dbh) {
- $dbh->commit();
- $dbh->disconnect();
- }
+ $dbh->commit if !$provided_dbh;
$main::lxdebug->leave_sub();
return;
cp_id = ?, marge_total = ?, marge_percent = ?,
globalproject_id = ?, delivery_customer_id = ?,
transaction_description = ?, delivery_vendor_id = ?,
- donumber = ?
+ donumber = ?, invnumber_for_credit_note = ?
WHERE id = ?|;
@values = ( $form->{"invnumber"}, $form->{"ordnumber"}, $form->{"quonumber"}, $form->{"cusordnumber"},
conv_date($form->{"invdate"}), conv_date($form->{"orddate"}), conv_date($form->{"quodate"}), conv_i($form->{"customer_id"}),
conv_i($form->{"cp_id"}), 1 * $form->{marge_total} , 1 * $form->{marge_percent},
conv_i($form->{"globalproject_id"}), conv_i($form->{"delivery_customer_id"}),
$form->{transaction_description}, conv_i($form->{"delivery_vendor_id"}),
- $form->{"donumber"}, #das entsprechende feld lieferscheinnummer aus der html-form 12.02.09 jb
+ $form->{"donumber"}, $form->{"invnumber_for_credit_note"},
conv_i($form->{"id"}));
do_query($form, $dbh, $query, @values);
# save printed, emailed and queued
$form->save_status($dbh);
- Common::webdav_folder($form) if ($main::webdav);
+ Common::webdav_folder($form);
# Link this record to the records it was created from.
RecordLinks->create_links('dbh' => $dbh,
'table' => 'ar',);
my $rc = 1;
- if (!$provided_dbh) {
- $dbh->commit();
- $dbh->disconnect();
- }
+ $dbh->commit if !$provided_dbh;
$main::lxdebug->leave_sub();
# sellprice is the cost of the item
my $linetotal = $form->round_amount(($ref->{sellprice} * $qty) / ( ($ref->{price_factor} || 1) * ( $basefactor || 1 )), 2);
- if (!$main::eur) {
+ if (!$::lx_office_conf{system}->{eur}) {
$ref->{expense_accno} = ($form->{"expense_accno_$row"}) ? $form->{"expense_accno_$row"} : $ref->{expense_accno};
# add to expense
$form->{amount_cogs}{ $form->{id} }{ $ref->{expense_accno} } += -$linetotal;
sub delete_invoice {
$main::lxdebug->enter_sub();
- my ($self, $myconfig, $form, $spool) = @_;
+ my ($self, $myconfig, $form) = @_;
# connect to database
my $dbh = $form->dbconnect_noauto($myconfig);
my @values = (conv_i($form->{id}));
+ # Falls wir ein Storno haben, müssen zwei Felder in der stornierten Rechnung wieder
+ # zurückgesetzt werden. Vgl:
+ # id | storno | storno_id | paid | amount
+ #----+--------+-----------+---------+-----------
+ # 18 | f | | 0.00000 | 119.00000
+ # ZU:
+ # 18 | t | | 119.00000 | 119.00000
+ #
+ if($form->{storno}){
+ # storno_id auslesen und korrigieren
+ my ($invoice_id) = selectfirst_array_query($form, $dbh, qq|SELECT storno_id FROM ar WHERE id = ?|,@values);
+ do_query($form, $dbh, qq|UPDATE ar SET storno = 'f', paid = 0 WHERE id = ?|, $invoice_id);
+ }
+
# delete AR record
do_query($form, $dbh, qq|DELETE FROM ar WHERE id = ?|, @values);
$dbh->disconnect;
if ($rc) {
+ my $spool = $::lx_office_conf{paths}->{spool};
map { unlink "$spool/$_" if -f "$spool/$_"; } @spoolfiles;
}
a.duedate, a.taxincluded, a.curr AS currency, a.shipto_id, a.cp_id,
a.employee_id, a.salesman_id, a.payment_id,
a.language_id, a.delivery_customer_id, a.delivery_vendor_id, a.type,
- a.transaction_description,
+ a.transaction_description, a.donumber, a.invnumber_for_credit_note,
a.marge_total, a.marge_percent,
- e.name AS employee, a.donumber
+ e.name AS employee
FROM ar a
LEFT JOIN employee e ON (e.id = a.employee_id)
WHERE a.id = ?|;
}
$sth->finish;
- Common::webdav_folder($form) if ($main::webdav);
+ Common::webdav_folder($form);
}
my $rc = $dbh->commit;
my ($price, $selectedpricegroup_id) = split(/--/, $form->{"sellprice_pg_$i"});
my $pricegroup_old = $form->{"pricegroup_old_$i"};
+
+ # sellprice has format 13,0000 or 0,00000, can't check for 0 numerically
+ my $sellprice = $form->{"sellprice_$i"};
+ my $pricegroup_id = $form->{"pricegroup_id_$i"};
$form->{"new_pricegroup_$i"} = $selectedpricegroup_id;
$form->{"old_pricegroup_$i"} = $pricegroup_old;
$pkr->{price} = $form->format_amount($myconfig, $pkr->{price}, 5);
if ($selectedpricegroup_id eq undef) {
- if ($pkr->{pricegroup_id} eq $form->{customer_klass}) {
+ # new entries in article list, either old invoice was loaded (edit) or a new article was added
+ # Case A: open old invoice, no pricegroup selected
+ # Case B: add new article to invoice, no pricegroup selected
+
+ # to distinguish case A and B the variable pricegroup_id_$i is used
+ # for new articles this variable isn't defined, for loaded articles it is
+ # sellprice can't be used, as it already has 0,00 set
+ if ($pkr->{pricegroup_id} eq $form->{"pricegroup_id_$i"} and defined $form->{"pricegroup_id_$i"}) {
+ # Case A
$pkr->{selected} = ' selected';
+ } elsif ($pkr->{pricegroup_id} eq $form->{customer_klass}
+ and not defined $form->{"pricegroup_id_$i"}
+ and $pkr->{price} != 0 # only use customer pricegroup price if it has a value, else use default_sellprice
+ # for the case where pricegroup prices haven't been set
+ ) {
+ # Case B: use default pricegroup of customer
+
+ $pkr->{selected} = ' selected'; # unless $form->{selected};
+
# no customer pricesgroup set
if ($pkr->{price} == $pkr->{default_sellprice}) {
} else {
# this sub should not set anything and only return. --sschoeling, 20090506
-# $form->{"sellprice_$i"} = $pkr->{price};
+# is this correct? put in again... -- grichardson 20110119
+ $form->{"sellprice_$i"} = $pkr->{price};
}
- } elsif ($pkr->{price} == $pkr->{default_sellprice}) {
+ } elsif ($pkr->{price} == $pkr->{default_sellprice} and $pkr->{default_sellprice} != 0) {
$pkr->{price} = $form->{"sellprice_$i"};
$pkr->{selected} = ' selected';
}
- } else {
+ }
+
+ # existing article: pricegroup or price changed
+ if ($selectedpricegroup_id or $selectedpricegroup_id == 0) {
if ($selectedpricegroup_id ne $pricegroup_old) {
+ # pricegroup has changed
if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
$pkr->{selected} = ' selected';
}
- } elsif ( ( $form->parse_amount($myconfig, $price_new)
- != $form->parse_amount($myconfig, $form->{"sellprice_$i"}))
- and ($price_new ne 0)) {
+ } elsif ( ($form->parse_amount($myconfig, $price_new)
+ != $form->parse_amount($myconfig, $form->{"sellprice_$i"}))
+ and ($price_new ne 0) and defined $price_new) {
+ # sellprice has changed
+ # when loading existing invoices $price_new is NULL
if ($pkr->{pricegroup_id} == 0) {
$pkr->{price} = $form->{"sellprice_$i"};
$pkr->{selected} = ' selected';
}
} elsif ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
+ # neither sellprice nor pricegroup changed
$pkr->{selected} = ' selected';
- if ( ($pkr->{pricegroup_id} == 0)
- and ($pkr->{price} == $form->{"sellprice_$i"})) {
+ if ( ($pkr->{pricegroup_id} == 0) and ($pkr->{price} == $form->{"sellprice_$i"})) {
# $pkr->{price} = $form->{"sellprice_$i"};
} else {
$pkr->{price} = $form->{"sellprice_$i"};