From: Moritz Bunkus Date: Tue, 1 Jul 2008 10:13:01 +0000 (+0000) Subject: Verknüpfungen zwischen Angeboten, Aufträgen, Lieferscheinen, Rechnungen in einer... X-Git-Tag: release-2.6.0beta1~94 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=5df2b57a097f66f6cecba0aa577c7aaba3422ab6;p=kivitendo-erp.git Verknüpfungen zwischen Angeboten, Aufträgen, Lieferscheinen, Rechnungen in einer eigenen Tabelle speichern. --- diff --git a/SL/DO.pm b/SL/DO.pm index c844b83e8..798d19f72 100644 --- a/SL/DO.pm +++ b/SL/DO.pm @@ -39,6 +39,7 @@ use YAML; use SL::AM; use SL::Common; use SL::DBUtils; +use SL::RecordLinks; sub transactions { $main::lxdebug->enter_sub(); @@ -342,9 +343,17 @@ sub save { # save printed, emailed, queued $form->save_status($dbh); - $self->mark_order_if_delivered('do_id' => $form->{id}, - 'type' => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase', - 'dbh' => $dbh,); + # Link this delivery order to the quotations it was created from. + my @oe_ids = grep { $_ } map { $_ * 1 } split m/\s+/, $form->{oe_ids}; + delete $form->{oe_ids}; + if (scalar @oe_ids) { + my @links = map { { 'from_table' => 'oe', 'from_id' => $_, 'to_table' => 'delivery_orders', 'to_id' => $form->{id} } } @oe_ids; + RecordLinks->create_links('dbh' => $dbh, 'links' => \@links); + } + + $self->mark_orders_if_delivered('do_id' => $form->{id}, + 'type' => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase', + 'dbh' => $dbh,); my $rc = $dbh->commit(); @@ -357,7 +366,7 @@ sub save { return $rc; } -sub mark_order_if_delivered { +sub mark_orders_if_delivered { $main::lxdebug->enter_sub(); my $self = shift; @@ -365,41 +374,36 @@ sub mark_order_if_delivered { Common::check_params(\%params, qw(do_id type)); - my $myconfig = \%main::myconfig; - my $form = $main::form; - - my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); + my $myconfig = \%main::myconfig; + my $form = $main::form; - my $query = qq|SELECT ordnumber FROM delivery_orders WHERE id = ?|; - my ($ordnumber) = selectfirst_array_query($form, $dbh, $query, conv_i($params{do_id})); + my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); - return $main::lxdebug->leave_sub() if (!$ordnumber); + my @links = RecordLinks->get_links('dbh' => $dbh, + 'from_table' => 'oe', + 'to_table' => 'delivery_orders', + 'to_id' => $params{do_id}); - my $vc = $params{type} eq 'purchase' ? 'vendor' : 'customer'; - $query = qq|SELECT id - FROM oe - WHERE NOT COALESCE(quotation, FALSE) - AND (COALESCE(${vc}_id, 0) > 0) - AND (ordnumber = ?) - ORDER BY id - LIMIT 1|; + my ($oe_id) = $links[0]->{from_id} if (scalar @links); - my ($oe_id) = selectfirst_array_query($form, $dbh, $query, $ordnumber); + $main::lxdebug->message(0, "oe_id $oe_id"); return $main::lxdebug->leave_sub() if (!$oe_id); my $all_units = AM->retrieve_all_units(); - my %shipped = $self->get_shipped_qty('type' => $params{type}, - 'ordnumber' => $ordnumber,); - my %ordered = (); - $query = qq|SELECT oi.parts_id, oi.qty, oi.unit, p.unit AS partunit FROM orderitems oi LEFT JOIN parts p ON (oi.parts_id = p.id) WHERE (oi.trans_id = ?)|; my $sth = prepare_execute_query($form, $dbh, $query, $oe_id); + my %shipped = $self->get_shipped_qty('type' => $params{type}, + 'oe_id' => $oe_id,); + my %ordered = (); + + do_statement($form, $sth, $query, $oe_id); + while (my $ref = $sth->fetchrow_hashref()) { $ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor}; @@ -424,10 +428,9 @@ sub mark_order_if_delivered { if ($delivered) { $query = qq|UPDATE oe - SET delivered = TRUE, closed = TRUE + SET delivered = TRUE WHERE id = ?|; do_query($form, $dbh, $query, $oe_id); - $dbh->commit() if (!$params{dbh}); } @@ -1001,25 +1004,33 @@ sub get_shipped_qty { my $self = shift; my %params = @_; - Common::check_params(\%params, qw(type ordnumber)); + Common::check_params(\%params, qw(type oe_id)); my $myconfig = \%main::myconfig; my $form = $main::form; my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); - my $notsales = $params{type} eq 'sales' ? '' : 'NOT'; + my @links = RecordLinks->get_links('dbh' => $dbh, + 'from_table' => 'oe', + 'from_id' => $params{oe_id}, + 'to_table' => 'delivery_orders'); + my @values = map { $_->{to_id} } @links; + + if (!scalar @values) { + $main::lxdebug->leave_sub(); + return (); + } - my $query = + my $query = qq|SELECT doi.parts_id, doi.qty, doi.unit, p.unit AS partunit FROM delivery_order_items doi LEFT JOIN delivery_orders o ON (doi.delivery_order_id = o.id) LEFT JOIN parts p ON (doi.parts_id = p.id) - WHERE ($notsales o.is_sales) - AND (o.ordnumber = ?)|; + WHERE o.id IN (| . join(', ', ('?') x scalar @values) . qq|)|; my %ship = (); - my $entries = selectall_hashref_query($form, $dbh, $query, $params{ordnumber}); + my $entries = selectall_hashref_query($form, $dbh, $query, @values); my $all_units = AM->retrieve_all_units(); foreach my $entry (@{ $entries }) { diff --git a/SL/RecordLinks.pm b/SL/RecordLinks.pm new file mode 100644 index 000000000..eae903e31 --- /dev/null +++ b/SL/RecordLinks.pm @@ -0,0 +1,82 @@ +package RecordLinks; + +use SL::Common; +use SL::DBUtils; + +sub create_links { + $main::lxdebug->enter_sub(); + + my $self = shift; + my %params = @_; + + Common::check_params(\%params, qw(links)); + + if (!scalar @{ $params{links} }) { + $main::lxdebug->leave_sub(); + return; + } + + my $myconfig = \%main::myconfig; + my $form = $main::form; + + my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); + + my $query = qq|INSERT INTO record_links (from_table, from_id, to_table, to_id) VALUES (?, ?, ?, ?)|; + my $sth = prepare_query($form, $dbh, $query); + + foreach my $link (@{ $params{links} }) { + next if ('HASH' ne ref $link); + next if (!$link->{from_table} || !$link->{from_id} || !$link->{to_table} || !$link->{to_id}); + + do_statement($form, $sth, $query, $link->{from_table}, conv_i($link->{from_id}), $link->{to_table}, conv_i($link->{to_id})); + } + + $dbh->commit() unless ($params{dbh}); + + $main::lxdebug->leave_sub(); +} + +sub get_links { + $main::lxdebug->enter_sub(); + + my $self = shift; + my %params = @_; + + Common::check_params(\%params, [ qw(from_table from_id to_table to_id) ]); + + my $myconfig = \%main::myconfig; + my $form = $main::form; + + my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); + + my @conditions = (); + my @values = (); + + foreach my $col (qw(from_table from_id to_table to_id)) { + next unless ($params{$col}); + + if ('ARRAY' eq ref $params{$col}) { + push @conditions, "$col IN (" . join(', ', ('?') x scalar(@{ $params{$col} })) . ")"; + push @values, $col =~ m/table/ ? @{ $params{$col} } : map { conv_i($_) } @{ $params{$col} }; + + } else { + push @conditions, "$col = ?"; + push @values, $col =~ m/table/ ? $params{$col} : conv_i($params{$col}); + } + } + + my $query = qq|SELECT from_table, from_id, to_table, to_id + FROM record_links|; + + if (scalar @conditions) { + $query .= qq| WHERE | . join(' AND ', map { "($_)" } @conditions); + } + + my $links = selectall_hashref_query($form, $dbh, $query, @values); + + $main::lxdebug->leave_sub(); + + return wantarray ? @{ $links } : $links; +} + +1; diff --git a/bin/mozilla/io.pl b/bin/mozilla/io.pl index 16d51bba1..9a1c5aa97 100644 --- a/bin/mozilla/io.pl +++ b/bin/mozilla/io.pl @@ -1883,8 +1883,8 @@ sub _update_ship { AM->retrieve_all_units(); - my %ship = DO->get_shipped_qty('type' => ($form->{type} eq 'purchase_order') ? 'purchase' : 'sales', - 'ordnumber' => $form->{ordnumber},); + my %ship = DO->get_shipped_qty('type' => ($form->{type} eq 'purchase_order') ? 'purchase' : 'sales', + 'oe_id' => $form->{id},); foreach my $i (1..$form->{rowcount}) { next unless ($form->{"id_${i}"}); diff --git a/bin/mozilla/oe.pl b/bin/mozilla/oe.pl index d121ae9e7..6c2569b6f 100644 --- a/bin/mozilla/oe.pl +++ b/bin/mozilla/oe.pl @@ -1814,7 +1814,8 @@ sub sales_order { delete($form->{ordnumber}); } - $form->{cp_id} *= 1; + $form->{cp_id} *= 1; + $form->{oe_ids} = $form->{id}; $form->{title} = $locale->text('Add Sales Order'); $form->{vc} = "customer"; @@ -1883,6 +1884,7 @@ sub delivery_order { require "bin/mozilla/do.pl"; $form->{cp_id} *= 1; + $form->{oe_ids} = $form->{id}; $form->{transdate} = $form->current_date(\%myconfig); delete $form->{duedate}; diff --git a/sql/Pg-upgrade2/record_links.sql b/sql/Pg-upgrade2/record_links.sql new file mode 100644 index 000000000..0ee026664 --- /dev/null +++ b/sql/Pg-upgrade2/record_links.sql @@ -0,0 +1,16 @@ +-- @tag: record_links +-- @description: Verknüpfungen zwischen Aufträgen, Lieferscheinen und Rechnungen +-- @depends: delivery_orders +CREATE TABLE record_links ( + from_table varchar(50) NOT NULL, + from_id integer NOT NULL, + to_table varchar(50) NOT NULL, + to_id integer NOT NULL, + + itime timestamp DEFAULT now() +); + +CREATE INDEX idx_record_links_from_table ON record_links (from_table); +CREATE INDEX idx_record_links_from_id ON record_links (from_id); +CREATE INDEX idx_record_links_to_table ON record_links (to_table); +CREATE INDEX idx_record_links_to_id ON record_links (to_id); diff --git a/templates/webpages/do/form_header_de.html b/templates/webpages/do/form_header_de.html index 01983ab70..80417eec5 100644 --- a/templates/webpages/do/form_header_de.html +++ b/templates/webpages/do/form_header_de.html @@ -71,6 +71,7 @@ + diff --git a/templates/webpages/do/form_header_master.html b/templates/webpages/do/form_header_master.html index 7b0bfcbe3..d156e5429 100644 --- a/templates/webpages/do/form_header_master.html +++ b/templates/webpages/do/form_header_master.html @@ -71,6 +71,7 @@ +