From: Bernd Bleßmann Date: Thu, 22 Jan 2015 15:36:36 +0000 (+0100) Subject: Item-Positionen für Rechnungen in DB: DB-Upgrade-Skript; Rose X-Git-Tag: release-3.2.0beta~38 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=942291e4ee3bacc216839d2989a63123f87b6bcd;p=kivitendo-erp.git Item-Positionen für Rechnungen in DB: DB-Upgrade-Skript; Rose Positionen der Artikelzeilen für Rechnungen in der Datenbank speichern. --- diff --git a/SL/DB/InvoiceItem.pm b/SL/DB/InvoiceItem.pm index 4374a6cdc..1e0e4e749 100644 --- a/SL/DB/InvoiceItem.pm +++ b/SL/DB/InvoiceItem.pm @@ -3,6 +3,7 @@ package SL::DB::InvoiceItem; use strict; use SL::DB::MetaSetup::InvoiceItem; +use SL::DB::Helper::ActsAsList; use SL::DB::Helper::CustomVariables ( sub_module => 'invoice', cvars_alias => 1, @@ -16,6 +17,8 @@ use SL::DB::Helper::CustomVariables ( __PACKAGE__->meta->make_manager_class; +__PACKAGE__->configure_acts_as_list(group_by => [qw(trans_id)]); + __PACKAGE__->meta->add_relationships( invoice => { type => 'one to one', diff --git a/SL/DB/MetaSetup/InvoiceItem.pm b/SL/DB/MetaSetup/InvoiceItem.pm index 68ed569e4..af759ab17 100644 --- a/SL/DB/MetaSetup/InvoiceItem.pm +++ b/SL/DB/MetaSetup/InvoiceItem.pm @@ -30,6 +30,7 @@ __PACKAGE__->meta->columns( mtime => { type => 'timestamp' }, ordnumber => { type => 'text' }, parts_id => { type => 'integer' }, + position => { type => 'integer', not_null => 1 }, price_factor => { type => 'numeric', default => 1, precision => 15, scale => 5 }, price_factor_id => { type => 'integer' }, pricegroup_id => { type => 'integer' }, diff --git a/sql/Pg-upgrade2/invoice_positions.pl b/sql/Pg-upgrade2/invoice_positions.pl new file mode 100644 index 000000000..854c75fbb --- /dev/null +++ b/sql/Pg-upgrade2/invoice_positions.pl @@ -0,0 +1,46 @@ +# @tag: invoice_positions +# @description: Spalte für Positionen der Einträge in Rechnungen +# @depends: release_3_1_0 +# @encoding: utf-8 +package SL::DBUpgrade2::invoice_positions; + +use strict; +use utf8; + +use parent qw(SL::DBUpgrade2::Base); + +sub run { + my ($self) = @_; + + my $query = qq|ALTER TABLE invoice ADD position INTEGER|; + $self->db_query($query); + + + $query = qq|SELECT * FROM invoice ORDER BY trans_id, id|; + + my $sth = $self->dbh->prepare($query); + $sth->execute || $::form->dberror($query); + + # set new postition field in order of ids, starting by one for each invoice + my $last_invoice_id; + my $position; + while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { + if ($ref->{trans_id} != $last_invoice_id) { + $position = 1; + } else { + $position++; + } + $last_invoice_id = $ref->{trans_id}; + + $query = qq|UPDATE invoice SET position = ? WHERE id = ?|; + $self->db_query($query, bind => [ $position, $ref->{id} ]); + } + $sth->finish; + + $query = qq|ALTER TABLE invoice ALTER COLUMN position SET NOT NULL|; + $self->db_query($query); + + return 1; +} + +1;