Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 462287ad

Von Sven Schöling vor mehr als 8 Jahren hinzugefügt

  • ID 462287add299747f9b4c50ea7b6e3b2e8802d93c
  • Vorgänger 297a3b70
  • Nachfolger 87ef5e00

TransNumber: single-dbh

Unterschiede anzeigen:

SL/TransNumber.pm
8 8
use List::MoreUtils qw(any none);
9 9
use SL::DBUtils;
10 10
use SL::PrefixedNumber;
11
use SL::DB;
11 12

  
12 13
use Rose::Object::MakeMethods::Generic
13 14
(
......
23 24
  croak "Invalid type " . $self->type if none { $_ eq $self->type } @SUPPORTED_TYPES;
24 25

  
25 26
  $self->dbh_provided($self->dbh);
26
  $self->dbh($::form->get_standard_dbh) if !$self->dbh;
27
  $self->dbh(SL::DB->client->dbh) if !$self->dbh;
27 28
  $self->save(1) unless defined $self->save;
28 29
  $self->business_id(undef) if $self->type ne 'customer';
29 30

  
......
115 116

  
116 117
  my $form    = $main::form;
117 118
  my %filters = $self->_get_filters();
118

  
119
  $self->dbh->begin_work if $self->dbh->{AutoCommit};
120

  
121
  my $where = $filters{where} ? ' WHERE ' . $filters{where} : '';
122
  my $query = <<SQL;
123
    SELECT DISTINCT $filters{trans_number}, 1 AS in_use
124
    FROM $filters{table}
125
    $where
119
  my $number;
120

  
121
  SL::DB->client->with_transaction(sub {
122
    my $where = $filters{where} ? ' WHERE ' . $filters{where} : '';
123
    my $query = <<SQL;
124
      SELECT DISTINCT $filters{trans_number}, 1 AS in_use
125
      FROM $filters{table}
126
      $where
126 127
SQL
127 128

  
128
  do_query($form, $self->dbh, "LOCK TABLE " . $filters{table}) || die $self->dbh->errstr;
129
  my %numbers_in_use = selectall_as_map($form, $self->dbh, $query, $filters{trans_number}, 'in_use');
129
    do_query($form, $self->dbh, "LOCK TABLE " . $filters{table}) || die $self->dbh->errstr;
130
    my %numbers_in_use = selectall_as_map($form, $self->dbh, $query, $filters{trans_number}, 'in_use');
130 131

  
131
  my $business_number;
132
  ($business_number) = selectfirst_array_query($form, $self->dbh, qq|SELECT customernumberinit FROM business WHERE id = ? FOR UPDATE|, $self->business_id) if $self->business_id;
133
  my $number         = $business_number;
134
  ($number)          = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults FOR UPDATE|)                               if !$number;
135
  if ($filters{numberfield} eq 'assemblynumber' and length($number) < 1) {
136
    $filters{numberfield} = 'articlenumber';
137
    ($number)        = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults FOR UPDATE|)                               if !$number;
138
  }
139
  $number          ||= '';
140
  my $sequence       = SL::PrefixedNumber->new(number => $number);
141

  
142
  do {
143
    $number = $sequence->get_next;
144
  } while ($numbers_in_use{$number});
145

  
146
  if ($self->save) {
147
    if ($self->business_id && $business_number) {
148
      do_query($form, $self->dbh, qq|UPDATE business SET customernumberinit = ? WHERE id = ?|, $number, $self->business_id);
149
    } else {
150
      do_query($form, $self->dbh, qq|UPDATE defaults SET $filters{numberfield} = ?|, $number);
132
    my $business_number;
133
    ($business_number) = selectfirst_array_query($form, $self->dbh, qq|SELECT customernumberinit FROM business WHERE id = ? FOR UPDATE|, $self->business_id) if $self->business_id;
134
    $number         = $business_number;
135
    ($number)          = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults FOR UPDATE|)                               if !$number;
136
    if ($filters{numberfield} eq 'assemblynumber' and length($number) < 1) {
137
      $filters{numberfield} = 'articlenumber';
138
      ($number)        = selectfirst_array_query($form, $self->dbh, qq|SELECT $filters{numberfield} FROM defaults FOR UPDATE|)                               if !$number;
151 139
    }
152
    $self->dbh->commit if !$self->dbh_provided;
153
  }
140
    $number          ||= '';
141
    my $sequence       = SL::PrefixedNumber->new(number => $number);
142

  
143
    do {
144
      $number = $sequence->get_next;
145
    } while ($numbers_in_use{$number});
146

  
147
    if ($self->save) {
148
      if ($self->business_id && $business_number) {
149
        do_query($form, $self->dbh, qq|UPDATE business SET customernumberinit = ? WHERE id = ?|, $number, $self->business_id);
150
      } else {
151
        do_query($form, $self->dbh, qq|UPDATE defaults SET $filters{numberfield} = ?|, $number);
152
      }
153
    }
154

  
155
    1;
156
  }) or do { die SL::DB->client->error };
154 157

  
155 158
  return $number;
156 159
}

Auch abrufbar als: Unified diff