X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FOE.pm;h=b8250123f9e7aaec75b1754b7cc6183b2a9cf119;hb=fdee0091f00479361820f0f98fe896a14e605b18;hp=72c11ed3249d38f7e14d9490d98c4d81889337d2;hpb=be7bfdb98147bf98249cbc595e5b54c974ad0a89;p=kivitendo-erp.git diff --git a/SL/OE.pm b/SL/OE.pm index 72c11ed32..b8250123f 100644 --- a/SL/OE.pm +++ b/SL/OE.pm @@ -39,6 +39,7 @@ use List::Util qw(max); use SL::AM; use SL::Common; use SL::DBUtils; +use SL::IC; sub transactions { $main::lxdebug->enter_sub(); @@ -148,7 +149,8 @@ sub transactions { push(@values, '%' . $form->{transaction_description} . '%'); } - my $sortorder = join(', ', ("o.id", $form->sort_columns("transdate", $ordnumber, "name"))); + my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC'; + my $sortorder = join(', ', map { "${_} ${sortdir} " } ("o.id", $form->sort_columns("transdate", $ordnumber, "name"))); my %allowed_sort_columns = ( "transdate" => "o.transdate", "reqdate" => "o.reqdate", @@ -162,7 +164,7 @@ sub transactions { "transaction_description" => "o.transaction_description" ); if ($form->{sort} && grep($form->{sort}, keys(%allowed_sort_columns))) { - $sortorder = $allowed_sort_columns{$form->{sort}}; + $sortorder = $allowed_sort_columns{$form->{sort}} . " ${sortdir}"; } $query .= qq| ORDER by | . $sortorder; @@ -200,10 +202,13 @@ sub transactions_for_todo_list { $query = qq|SELECT oe.id, oe.transdate, oe.reqdate, oe.quonumber, oe.transaction_description, oe.amount, + CASE WHEN (COALESCE(oe.customer_id, 0) = 0) THEN 'vendor' ELSE 'customer' END AS vc, c.name AS customer, + v.name AS vendor, e.name AS employee FROM oe LEFT JOIN customer c ON (oe.customer_id = c.id) + LEFT JOIN vendor v ON (oe.vendor_id = v.id) LEFT JOIN employee e ON (oe.employee_id = e.id) WHERE (COALESCE(quotation, FALSE) = TRUE) AND (COALESCE(closed, FALSE) = FALSE) @@ -432,7 +437,7 @@ sub save { globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?, marge_total = ?, marge_percent = ? WHERE id = ?|; - @values = ($form->{ordnumber}, $form->{quonumber}, + @values = ($form->{ordnumber} || '', $form->{quonumber}, $form->{cusordnumber}, conv_date($form->{transdate}), conv_i($form->{vendor_id}), conv_i($form->{customer_id}), $amount, $netamount, conv_date($reqdate), @@ -465,6 +470,26 @@ sub save { # save printed, emailed, queued $form->save_status($dbh); + # Link this record to the records it was created from. + $form->{convert_from_oe_ids} =~ s/^\s+//; + $form->{convert_from_oe_ids} =~ s/\s+$//; + my @convert_from_oe_ids = split m/\s+/, $form->{convert_from_oe_ids}; + delete $form->{convert_from_oe_ids}; + + if (scalar @convert_from_oe_ids) { + RecordLinks->create_links('dbh' => $dbh, + 'mode' => 'ids', + 'from_table' => 'oe', + 'from_ids' => \@convert_from_oe_ids, + 'to_table' => 'oe', + 'to_id' => $form->{id}, + ); + + $self->_close_quotations_rfqs('dbh' => $dbh, + 'from_id' => \@convert_from_oe_ids, + 'to_id' => $form->{id}); + } + if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) { if ($form->{vc} eq 'customer') { $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0); @@ -487,40 +512,48 @@ sub save { return $rc; } -# this function closes multiple orders given in $form->{ordnumber_#}. -# use this for multiple orders that don't have to be saved back -# single orders should use OE::save instead. -sub close_orders { +sub _close_quotations_rfqs { $main::lxdebug->enter_sub(); - my ($self, $myconfig, $form) = @_; + my $self = shift; + my %params = @_; - # get ids from $form - map { push @ids, $form->{"ordnumber_$_"} if $form->{"ordnumber_$_"} } - (1 .. $form->{rowcount}); + Common::check_params(\%params, qw(from_id to_id)); - my $dbh = $form->dbconnect($myconfig); - $query = qq|UPDATE oe SET | . - qq|closed = TRUE | . - qq|WHERE ordnumber IN (| - . join(', ', map { $dbh->quote($_) } @ids) . qq|)|; - $dbh->do($query) || $form->dberror($query); - $dbh->disconnect; + my $myconfig = \%main::myconfig; + my $form = $main::form; - $main::lxdebug->leave_sub(); -} + my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); -sub close_order { - $main::lxdebug->enter_sub(); + my $query = qq|SELECT quotation FROM oe WHERE id = ?|; + my $sth = prepare_query($form, $dbh, $query); - my ($self, $myconfig, $form) = @_; + do_statement($form, $sth, $query, conv_i($params{to_id})); - return $main::lxdebug->leave_sub() unless ($form->{"id"}); + my ($quotation) = $sth->fetchrow_array(); - my $dbh = $form->dbconnect($myconfig); - do_query($form, $dbh, qq|UPDATE oe SET closed = TRUE where id = ?|, - $form->{"id"}); - $dbh->disconnect; + if ($quotation) { + $main::lxdebug->leave_sub(); + return; + } + + my @close_ids; + + foreach my $from_id (@{ $params{from_id} }) { + $from_id = conv_i($from_id); + do_statement($form, $sth, $query, $from_id); + ($quotation) = $sth->fetchrow_array(); + push @close_ids, $from_id if ($quotation); + } + + $sth->finish(); + + if (scalar @close_ids) { + $query = qq|UPDATE oe SET closed = TRUE WHERE id IN (| . join(', ', ('?') x scalar @close_ids) . qq|)|; + do_query($form, $dbh, $query, @close_ids); + + $dbh->commit() unless ($params{dbh}); + } $main::lxdebug->leave_sub(); } @@ -600,13 +633,22 @@ sub retrieve { if ($form->{"multi_id_$_"} and $form->{"trans_id_$_"}) } (1 .. $form->{"rowcount"}); + if ($form->{rowcount} && scalar @ids) { + $form->{convert_from_oe_ids} = join ' ', @ids; + } + # if called in multi id mode, and still only got one id, switch back to single id if ($form->{"rowcount"} and $#ids == 0) { $form->{"id"} = $ids[0]; undef @ids; } - $query_add = qq|, current_date AS transdate, current_date AS reqdate| if (!$form->{id}); + my $query_add = ''; + if (!$form->{id}) { + my $wday = (localtime(time))[6]; + my $next_workday = $wday == 5 ? 3 : $wday == 6 ? 2 : 1; + $query_add = qq|, current_date AS transdate, date(current_date + interval '${next_workday} days') AS reqdate|; + } # get default accounts $query = qq|SELECT (SELECT c.accno FROM chart c WHERE d.inventory_accno_id = c.id) AS inventory_accno, @@ -907,12 +949,16 @@ sub order_details { $form->{"globalprojectnumber"} = $projectnumbers{$form->{"globalproject_id"}}; + $form->{discount} = []; + + IC->prepare_parts_for_printing(); + my @arrays = qw(runningnumber number description longdescription qty ship unit bin partnotes serialnumber reqdate sellprice listprice netprice discount p_discount discount_sub nodiscount_sub linetotal nodiscount_linetotal tax_rate projectnumber - price_factor price_factor_name); + price_factor price_factor_name partsgroup); my $sameitem = ""; foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) { @@ -962,13 +1008,17 @@ sub order_details { push @{ $form->{listprice} }, $form->{"listprice_$i"}; push @{ $form->{price_factor} }, $price_factor->{formatted_factor}; push @{ $form->{price_factor_name} }, $price_factor->{description}; + push @{ $form->{partsgroup} }, $form->{"partsgroup_$i"}; my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"}); my ($dec) = ($sellprice =~ /\.(\d+)/); my $decimalplaces = max 2, length($dec); - my $discount = $form->round_amount($form->{"qty_$i"} * $sellprice * $form->{"discount_$i"} / 100 / $price_factor->{factor}, $decimalplaces); - my $linetotal = $form->round_amount($form->{"qty_$i"} * $sellprice * (100 - $form->{"discount_$i"}) / 100 / $price_factor->{factor}, 2); + my $parsed_discount = $form->parse_amount($myconfig, $form->{"discount_$i"}); + my $linetotal_exact = $form->{"qty_$i"} * $sellprice * (100 - $parsed_discount) / 100 / $price_factor->{factor}; + my $linetotal = $form->round_amount($linetotal_exact, 2); + my $discount = $form->round_amount($form->{"qty_$i"} * $sellprice * $parsed_discount / 100 / $price_factor->{factor} - ($linetotal - $linetotal_exact), + $decimalplaces); my $nodiscount_linetotal = $form->round_amount($form->{"qty_$i"} * $sellprice / $price_factor->{factor}, 2); $form->{"netprice_$i"} = $form->round_amount($form->{"qty_$i"} ? ($linetotal / $form->{"qty_$i"}) : 0, 2); @@ -976,7 +1026,7 @@ sub order_details { $linetotal = ($linetotal != 0) ? $linetotal : ''; - push @{ $form->{discount} }, ($discount != 0) ? $form->format_amount($myconfig, $discount * -1, $decimalplaces) : ''; + push @{ $form->{discount} }, ($discount != 0) ? $form->format_amount($myconfig, $discount * -1, 2) : ''; push @{ $form->{p_discount} }, $form->{"discount_$i"}; $form->{ordtotal} += $linetotal; @@ -1026,9 +1076,9 @@ sub order_details { } if ($taxamount != 0) { - foreach my $item (split / /, $form->{"taxaccounts_$i"}) { - $taxaccounts{$item} += $taxamount * $form->{"${item}_rate"} / $taxrate; - $taxbase{$item} += $taxbase; + foreach my $accno (split / /, $form->{"taxaccounts_$i"}) { + $taxaccounts{$accno} += $taxamount * $form->{"${accno}_rate"} / $taxrate; + $taxbase{$accno} += $taxbase; } }