Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 0022ca37

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

  • ID 0022ca3734db21b55ff8a11c0f4d8fdc909ef5b4
  • Vorgänger 31155ae5
  • Nachfolger 9fdd2fb1

AM: single-dbh und disconnects

Unterschiede anzeigen:

SL/AM.pm
46 46
use SL::DB::Default;
47 47
use SL::DB::Employee;
48 48
use SL::DB::Chart;
49
use SL::DB;
49 50
use SL::GenericTranslations;
50 51

  
51 52
use strict;
......
122 123
}
123 124

  
124 125
sub save_account {
126
  my ($self, $myconfig, $form) = @_;
125 127
  $main::lxdebug->enter_sub();
126 128

  
129
  my $rc = SL::DB->client->with_transaction(\&_save_account, $self, $myconfig, $form);
130

  
131
  $::lxdebug->leave_sub;
132
  return $rc;
133
}
134

  
135
sub _save_account {
127 136
  # TODO: it should be forbidden to change an account to a heading if there
128 137
  # have been bookings to this account in the past
129 138

  
130 139
  my ($self, $myconfig, $form) = @_;
131 140

  
132
  # connect to database, turn off AutoCommit
133
  my $dbh = $form->dbconnect_noauto($myconfig);
141
  my $dbh = SL::DB->client->dbh;
134 142

  
135 143
  for (qw(AR_include_in_dropdown AP_include_in_dropdown summary_account)) {
136 144
    $form->{$form->{$_}} = $form->{$_} if $form->{$_};
......
356 364

  
357 365
  do_query($form, $dbh, $query, $form->{id});
358 366

  
359
  # commit
360
  my $rc = $dbh->commit;
361
  $dbh->disconnect;
362

  
363
  $main::lxdebug->leave_sub();
364

  
365
  return $rc;
367
  return 1;
366 368
}
367 369

  
368 370
sub delete_account {
371
  my ($self, $myconfig, $form) = @_;
369 372
  $main::lxdebug->enter_sub();
370 373

  
374
  my $rc = SL::DB->client->with_transaction(\&_delete_account, $self, $myconfig, $form);
375

  
376
  $::lxdebug->leave_sub;
377
  return $rc;
378
}
379

  
380
sub _delete_account {
371 381
  my ($self, $myconfig, $form) = @_;
372 382

  
373
  # connect to database, turn off AutoCommit
374
  my $dbh = $form->dbconnect_noauto($myconfig);
383
  my $dbh = SL::DB->client->dbh;
375 384

  
376 385
  my $query = qq|SELECT count(*) FROM acc_trans a
377 386
                 WHERE a.chart_id = ?|;
378 387
  my ($count) = selectrow_query($form, $dbh, $query, $form->{id});
379 388

  
380 389
  if ($count) {
381
    $dbh->disconnect;
382
    $main::lxdebug->leave_sub();
383 390
    return;
384 391
  }
385 392

  
......
408 415
              WHERE id = ?|;
409 416
  do_query($form, $dbh, $query, $form->{id});
410 417

  
411
  # commit and redirect
412
  my $rc = $dbh->commit;
413
  $dbh->disconnect;
414

  
415
  $main::lxdebug->leave_sub();
416

  
417
  return $rc;
418
  return 1;
418 419
}
419 420

  
420 421
sub lead {
......
422 423

  
423 424
  my ($self, $myconfig, $form) = @_;
424 425

  
425
  # connect to database
426
  my $dbh = $form->dbconnect($myconfig);
426
  my $dbh = SL::DB->client->dbh;
427 427

  
428 428
  my $query = qq|SELECT id, lead
429 429
                 FROM leads
......
437 437
  }
438 438

  
439 439
  $sth->finish;
440
  $dbh->disconnect;
441 440

  
442 441
  $main::lxdebug->leave_sub();
443 442
}
......
447 446

  
448 447
  my ($self, $myconfig, $form) = @_;
449 448

  
450
  # connect to database
451
  my $dbh = $form->dbconnect($myconfig);
449
  my $dbh = SL::DB->client->dbh;
452 450

  
453 451
  my $query =
454 452
    qq|SELECT l.id, l.lead | .
......
463 461

  
464 462
  $sth->finish;
465 463

  
466
  $dbh->disconnect;
467

  
468 464
  $main::lxdebug->leave_sub();
469 465
}
470 466

  
......
474 470
  my ($self, $myconfig, $form) = @_;
475 471
  my ($query);
476 472

  
477
  # connect to database
478
  my $dbh = $form->dbconnect($myconfig);
473
  my $dbh = SL::DB->client->dbh;
479 474

  
480 475
  my @values = ($form->{description});
481 476
  # id is the old record
......
491 486
  }
492 487
  do_query($form, $dbh, $query, @values);
493 488

  
494
  $dbh->disconnect;
495

  
496 489
  $main::lxdebug->leave_sub();
497 490
}
498 491

  
......
502 495
  my ($self, $myconfig, $form) = @_;
503 496
  my ($query);
504 497

  
505
  # connect to database
506
  my $dbh = $form->dbconnect($myconfig);
507

  
508
  $query = qq|DELETE FROM leads
509
              WHERE id = ?|;
510
  do_query($form, $dbh, $query, $form->{id});
511

  
512
  $dbh->disconnect;
498
  SL::DB->client->with_transaction(sub {
499
    $query = qq|DELETE FROM leads WHERE id = ?|;
500
    do_query($form, SL::DB->client->dbh, $query, $form->{id});
501
  });
513 502

  
514 503
  $main::lxdebug->leave_sub();
515 504
}
......
519 508

  
520 509
  my ($self, $myconfig, $form, $return_list) = @_;
521 510

  
522
  # connect to database
523
  my $dbh = $form->dbconnect($myconfig);
511
  my $dbh = SL::DB->client->dbh;
524 512

  
525 513
  my $query =
526 514
    "SELECT id, description, template_code, article_code, " .
......
537 525
  }
538 526

  
539 527
  $sth->finish;
540
  $dbh->disconnect;
541 528

  
542 529
  $main::lxdebug->leave_sub();
543 530

  
......
553 540

  
554 541
  my ($self, $myconfig, $form) = @_;
555 542

  
556
  # connect to database
557
  my $dbh = $form->dbconnect($myconfig);
543
  my $dbh = SL::DB->client->dbh;
558 544

  
559 545
  my $query =
560 546
    "SELECT description, template_code, article_code, " .
......
569 555

  
570 556
  $sth->finish;
571 557

  
572
  $dbh->disconnect;
573

  
574 558
  $main::lxdebug->leave_sub();
575 559
}
576 560

  
......
579 563

  
580 564
  my ($self, $myconfig, $form, $id) = @_;
581 565

  
582
  # connect to database
583
  my $dbh = $form->dbconnect($myconfig);
566
  my $dbh = SL::DB->client->dbh;
584 567

  
585 568
  my $query =
586 569
    "SELECT template_code, " .
587 570
    "  output_numberformat, output_dateformat, output_longdates " .
588 571
    "FROM language WHERE id = ?";
589 572
  my @res = selectrow_query($form, $dbh, $query, $id);
590
  $dbh->disconnect;
591 573

  
592 574
  $main::lxdebug->leave_sub();
593 575

  
......
599 581

  
600 582
  my ($self, $myconfig, $form) = @_;
601 583

  
602
  # connect to database
603
  my $dbh = $form->dbconnect($myconfig);
604
  my (@values, $query);
605

  
606
  map({ push(@values, $form->{$_}); }
607
      qw(description template_code article_code
608
         output_numberformat output_dateformat output_longdates));
609

  
610
  # id is the old record
611
  if ($form->{id}) {
612
    $query =
613
      "UPDATE language SET " .
614
      "  description = ?, template_code = ?, article_code = ?, " .
615
      "  output_numberformat = ?, output_dateformat = ?, " .
616
      "  output_longdates = ? " .
617
      "WHERE id = ?";
618
    push(@values, $form->{id});
619
  } else {
620
    $query =
621
      "INSERT INTO language (" .
622
      "  description, template_code, article_code, " .
623
      "  output_numberformat, output_dateformat, output_longdates" .
624
      ") VALUES (?, ?, ?, ?, ?, ?)";
625
  }
626
  do_query($form, $dbh, $query, @values);
627

  
628
  $dbh->disconnect;
584
  SL::DB->client->with_transaction(sub {
585
    my $dbh = SL::DB->client->dbh;
586
    my (@values, $query);
587

  
588
    map({ push(@values, $form->{$_}); }
589
        qw(description template_code article_code
590
           output_numberformat output_dateformat output_longdates));
591

  
592
    # id is the old record
593
    if ($form->{id}) {
594
      $query =
595
        "UPDATE language SET " .
596
        "  description = ?, template_code = ?, article_code = ?, " .
597
        "  output_numberformat = ?, output_dateformat = ?, " .
598
        "  output_longdates = ? " .
599
        "WHERE id = ?";
600
      push(@values, $form->{id});
601
    } else {
602
      $query =
603
        "INSERT INTO language (" .
604
        "  description, template_code, article_code, " .
605
        "  output_numberformat, output_dateformat, output_longdates" .
606
        ") VALUES (?, ?, ?, ?, ?, ?)";
607
    }
608
    do_query($form, $dbh, $query, @values);
609
  });
629 610

  
630 611
  $main::lxdebug->leave_sub();
631 612
}
......
636 617
  my ($self, $myconfig, $form) = @_;
637 618
  my $query;
638 619

  
639
  # connect to database
640
  my $dbh = $form->dbconnect_noauto($myconfig);
620
  SL::DB->client->with_transaction(sub {
621
    my $dbh = SL::DB->client->dbh;
641 622

  
642
  foreach my $table (qw(generic_translations units_language)) {
643
    $query = qq|DELETE FROM $table WHERE language_id = ?|;
644
    do_query($form, $dbh, $query, $form->{"id"});
645
  }
646

  
647
  $query = "DELETE FROM language WHERE id = ?";
648
  do_query($form, $dbh, $query, $form->{"id"});
623
    foreach my $table (qw(generic_translations units_language)) {
624
      $query = qq|DELETE FROM $table WHERE language_id = ?|;
625
      do_query($form, $dbh, $query, $form->{"id"});
626
    }
649 627

  
650
  $dbh->commit();
651
  $dbh->disconnect;
628
    $query = "DELETE FROM language WHERE id = ?";
629
    do_query($form, $dbh, $query, $form->{"id"});
630
  });
652 631

  
653 632
  $main::lxdebug->leave_sub();
654 633
}
......
767 746
  my $myconfig = \%main::myconfig;
768 747
  my $form     = $main::form;
769 748

  
770
  my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
749
  my $dbh      = $params{dbh} || SL::DB->client->dbh;
771 750

  
772 751
  my $defaults = selectfirst_hashref_query($form, $dbh, qq|SELECT * FROM defaults|) || {};
773 752

  
......
783 762

  
784 763
  my ($self, $myconfig, $form) = @_;
785 764

  
786
  my $dbh = $form->dbconnect($myconfig);
765
  my $dbh = SL::DB->client->dbh;
787 766

  
788 767
  my $query = qq|SELECT closedto, max_future_booking_interval, revtrans FROM defaults|;
789 768
  my $sth   = $dbh->prepare($query);
......
793 772

  
794 773
  $sth->finish;
795 774

  
796
  $dbh->disconnect;
797

  
798 775
  $main::lxdebug->leave_sub();
799 776
}
800 777

  
......
803 780

  
804 781
  my ($self, $myconfig, $form) = @_;
805 782

  
806
  my $dbh = $form->dbconnect($myconfig);
783
  SL::DB->client->with_transaction(sub {
784
    my $dbh = SL::DB->client->dbh;
807 785

  
808
  my ($query, @values);
786
    my ($query, @values);
809 787

  
810
  # is currently NEVER trueish (no more hidden revtrans in $form)
811
  # if ($form->{revtrans}) {
812
  #   $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
813
  # -> therefore you can only set this to false (which is already the default)
814
  # and this flag is currently only checked in gl.pl. TOOD Can probably be removed
788
    # is currently NEVER trueish (no more hidden revtrans in $form)
789
    # if ($form->{revtrans}) {
790
    #   $query = qq|UPDATE defaults SET closedto = NULL, revtrans = '1'|;
791
    # -> therefore you can only set this to false (which is already the default)
792
    # and this flag is currently only checked in gl.pl. TOOD Can probably be removed
815 793

  
816
    $query = qq|UPDATE defaults SET closedto = ?, max_future_booking_interval = ?, revtrans = '0'|;
817
    @values = (conv_date($form->{closedto}), conv_i($form->{max_future_booking_interval}));
794
      $query = qq|UPDATE defaults SET closedto = ?, max_future_booking_interval = ?, revtrans = '0'|;
795
      @values = (conv_date($form->{closedto}), conv_i($form->{max_future_booking_interval}));
818 796

  
819
  # set close in defaults
820
  do_query($form, $dbh, $query, @values);
821

  
822
  $dbh->disconnect;
797
    # set close in defaults
798
    do_query($form, $dbh, $query, @values);
799
  });
823 800

  
824 801
  $main::lxdebug->leave_sub();
825 802
}
......
845 822
  my ($self, $myconfig, $form, $prefix) = @_;
846 823
  $prefix ||= '';
847 824

  
848
  my $dbh = $form->get_standard_dbh;
825
  my $dbh = SL::DB->client->dbh;
849 826

  
850 827
  my $query = "SELECT *, base_unit AS original_base_unit FROM units";
851 828

  
......
934 911

  
935 912
  my ($self, $myconfig, $form, $units) = @_;
936 913

  
937
  my $dbh = $form->dbconnect($myconfig);
914
  my $dbh = SL::DB->client->dbh;
938 915

  
939 916
  map({ $_->{"in_use"} = 0; } values(%{$units}));
940 917

  
......
971 948
    }
972 949
  }
973 950

  
974
  $dbh->disconnect();
975

  
976 951
  $main::lxdebug->leave_sub();
977 952
}
978 953

  
......
1103 1078

  
1104 1079
  my ($self, $myconfig, $form, $name, $base_unit, $factor, $languages) = @_;
1105 1080

  
1106
  my $dbh = $form->dbconnect_noauto($myconfig);
1081
  SL::DB->client->with_transaction(sub {
1082
    my $dbh = SL::DB->client->dbh;
1107 1083

  
1108
  my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1109
  my ($sortkey) = selectrow_query($form, $dbh, $query);
1084
    my $query = qq|SELECT COALESCE(MAX(sortkey), 0) + 1 FROM units|;
1085
    my ($sortkey) = selectrow_query($form, $dbh, $query);
1110 1086

  
1111
  $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1112
    "VALUES (?, ?, ?, ?)";
1113
  do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1087
    $query = "INSERT INTO units (name, base_unit, factor, sortkey) " .
1088
      "VALUES (?, ?, ?, ?)";
1089
    do_query($form, $dbh, $query, $name, $base_unit, $factor, $sortkey);
1114 1090

  
1115
  if ($languages) {
1116
    $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1117
    my $sth = $dbh->prepare($query);
1118
    foreach my $lang (@{$languages}) {
1119
      my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1120
      $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1091
    if ($languages) {
1092
      $query = "INSERT INTO units_language (unit, language_id, localized, localized_plural) VALUES (?, ?, ?, ?)";
1093
      my $sth = $dbh->prepare($query);
1094
      foreach my $lang (@{$languages}) {
1095
        my @values = ($name, $lang->{"id"}, $lang->{"localized"}, $lang->{"localized_plural"});
1096
        $sth->execute(@values) || $form->dberror($query . " (" . join(", ", @values) . ")");
1097
      }
1098
      $sth->finish();
1121 1099
    }
1122
    $sth->finish();
1123
  }
1124

  
1125
  $dbh->commit();
1126
  $dbh->disconnect();
1100
  });
1127 1101

  
1128 1102
  $main::lxdebug->leave_sub();
1129 1103
}
1130 1104

  
1131 1105
sub save_units {
1106
  my ($self, $myconfig, $form, $units, $delete_units) = @_;
1132 1107
  $main::lxdebug->enter_sub();
1133 1108

  
1109
  my $rc = SL::DB->client->with_transaction(\&_save_units, $self, $myconfig, $form, $units, $delete_units);
1110

  
1111
  $::lxdebug->leave_sub;
1112
  return $rc;
1113
}
1114

  
1115
sub _save_units {
1134 1116
  my ($self, $myconfig, $form, $units, $delete_units) = @_;
1135 1117

  
1136
  my $dbh = $form->dbconnect_noauto($myconfig);
1118
  my $dbh = SL::DB->client->dbh;
1137 1119

  
1138 1120
  my ($base_unit, $unit, $sth, $query);
1139 1121

  
......
1180 1162

  
1181 1163
  $sth->finish();
1182 1164
  $sth_lang->finish();
1183
  $dbh->commit();
1184
  $dbh->disconnect();
1185 1165

  
1186
  $main::lxdebug->leave_sub();
1166
  return 1;
1187 1167
}
1188 1168

  
1189 1169
sub taxes {
......
1191 1171

  
1192 1172
  my ($self, $myconfig, $form) = @_;
1193 1173

  
1194
  # connect to database
1195
  my $dbh = $form->dbconnect($myconfig);
1174
  my $dbh = SL::DB->client->dbh;
1196 1175

  
1197 1176
  my $query = qq|SELECT
1198 1177
                   t.id,
......
1217 1196
  }
1218 1197

  
1219 1198
  $sth->finish;
1220
  $dbh->disconnect;
1221 1199

  
1222 1200
  $main::lxdebug->leave_sub();
1223 1201
}
......
1227 1205

  
1228 1206
  my ($self, $myconfig, $form) = @_;
1229 1207

  
1230
  my $dbh = $form->dbconnect($myconfig);
1208
  my $dbh = SL::DB->client->dbh;
1231 1209

  
1232 1210
  # get Accounts from chart
1233 1211
  my $query = qq{ SELECT
......
1259 1237

  
1260 1238
  $sth->finish;
1261 1239

  
1262
  $dbh->disconnect;
1263

  
1264 1240
  $main::lxdebug->leave_sub();
1265 1241
}
1266 1242

  
......
1269 1245

  
1270 1246
  my ($self, $myconfig, $form) = @_;
1271 1247

  
1272
  # connect to database
1273
  my $dbh = $form->dbconnect($myconfig);
1248
  my $dbh = SL::DB->client->dbh;
1274 1249

  
1275 1250
  my $query = qq|SELECT
1276 1251
                   taxkey,
......
1321 1296
    $sth->finish;
1322 1297
  }
1323 1298

  
1324
  $dbh->disconnect;
1325

  
1326 1299
  $main::lxdebug->leave_sub();
1327 1300
}
1328 1301

  
1329 1302
sub save_tax {
1303
  my ($self, $myconfig, $form) = @_;
1330 1304
  $main::lxdebug->enter_sub();
1331 1305

  
1306
  my $rc = SL::DB->client->with_transaction(\&_save_tax, $self, $myconfig, $form);
1307

  
1308
  $::lxdebug->leave_sub;
1309
  return $rc;
1310
}
1311

  
1312
sub _save_tax {
1332 1313
  my ($self, $myconfig, $form) = @_;
1333 1314
  my $query;
1334 1315

  
1335
  # connect to database
1336
  my $dbh = $form->get_standard_dbh($myconfig);
1316
  my $dbh = SL::DB->client->dbh;
1337 1317

  
1338 1318
  $form->{rate} = $form->{rate} / 100;
1339 1319

  
......
1384 1364
                              'language_id'      => $language_id,
1385 1365
                              'translation'      => $form->{translations}->{$language_id});
1386 1366
  }
1387

  
1388
  $dbh->commit();
1389

  
1390
  $main::lxdebug->leave_sub();
1391 1367
}
1392 1368

  
1393 1369
sub delete_tax {
......
1396 1372
  my ($self, $myconfig, $form) = @_;
1397 1373
  my $query;
1398 1374

  
1399
  # connect to database
1400
  my $dbh = $form->get_standard_dbh($myconfig);
1401

  
1402
  $query = qq|DELETE FROM tax
1403
              WHERE id = ?|;
1404
  do_query($form, $dbh, $query, $form->{id});
1405

  
1406
  $dbh->commit();
1375
  SL::DB->client->with_transaction(sub {
1376
    $query = qq|DELETE FROM tax WHERE id = ?|;
1377
    do_query($form, SL::DB->client->dbh, $query, $form->{id});
1378
  });
1407 1379

  
1408 1380
  $main::lxdebug->leave_sub();
1409 1381
}
......
1413 1385

  
1414 1386
  my ($self, $myconfig, $form) = @_;
1415 1387

  
1416
  # connect to database
1417
  my $dbh = $form->get_standard_dbh($myconfig);
1418

  
1419
  my $query;
1420
  my @values = ($form->{description}, conv_i($form->{factor}));
1388
  SL::DB->client->with_transaction(sub {
1389
    my $dbh = SL::DB->client->dbh;
1421 1390

  
1422
  if ($form->{id}) {
1423
    $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1424
    push @values, conv_i($form->{id});
1391
    my $query;
1392
    my @values = ($form->{description}, conv_i($form->{factor}));
1425 1393

  
1426
  } else {
1427
    $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1428
  }
1394
    if ($form->{id}) {
1395
      $query = qq|UPDATE price_factors SET description = ?, factor = ? WHERE id = ?|;
1396
      push @values, conv_i($form->{id});
1429 1397

  
1430
  do_query($form, $dbh, $query, @values);
1398
    } else {
1399
      $query = qq|INSERT INTO price_factors (description, factor, sortkey) VALUES (?, ?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM price_factors))|;
1400
    }
1431 1401

  
1432
  $dbh->commit();
1402
    do_query($form, $dbh, $query, @values);
1403
  });
1433 1404

  
1434 1405
  $main::lxdebug->leave_sub();
1435 1406
}
......
1439 1410

  
1440 1411
  my ($self, $myconfig, $form) = @_;
1441 1412

  
1442
  # connect to database
1443
  my $dbh = $form->get_standard_dbh($myconfig);
1413
  my $dbh = SL::DB->client->dbh;
1444 1414

  
1445 1415
  $form->{PRICE_FACTORS} = selectall_hashref_query($form, $dbh, qq|SELECT * FROM price_factors ORDER BY sortkey|);
1446 1416

  
......
1453 1423
  my ($self, $myconfig, $form) = @_;
1454 1424

  
1455 1425
  # connect to database
1456
  my $dbh = $form->get_standard_dbh($myconfig);
1426
  my $dbh = SL::DB->client->dbh;
1457 1427

  
1458 1428
  my $query = qq|SELECT description, factor,
1459 1429
                   ((SELECT COUNT(*) FROM parts      WHERE price_factor_id = ?) +
......
1471 1441

  
1472 1442
  my ($self, $myconfig, $form) = @_;
1473 1443

  
1474
  # connect to database
1475
  my $dbh = $form->get_standard_dbh($myconfig);
1476

  
1477
  do_query($form, $dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1478
  $dbh->commit();
1444
  SL::DB->client->with_transaction(sub {
1445
    do_query($form, SL::DB->client->dbh, qq|DELETE FROM price_factors WHERE id = ?|, conv_i($form->{id}));
1446
  });
1479 1447

  
1480 1448
  $main::lxdebug->leave_sub();
1481 1449
}
......
1485 1453

  
1486 1454
  my ($self, $myconfig, $form) = @_;
1487 1455

  
1488
  # connect to database
1489
  my $dbh = $form->get_standard_dbh($myconfig);
1490

  
1491
  my ($query, @values, $sth);
1456
  SL::DB->client->with_transaction(sub {
1457
    my $dbh = SL::DB->client->dbh;
1492 1458

  
1493
  if (!$form->{id}) {
1494
    $query        = qq|SELECT nextval('id')|;
1495
    ($form->{id}) = selectrow_query($form, $dbh, $query);
1459
    my ($query, @values, $sth);
1496 1460

  
1497
    $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1498
    do_query($form, $dbh, $query, $form->{id});
1499
  }
1461
    if (!$form->{id}) {
1462
      $query        = qq|SELECT nextval('id')|;
1463
      ($form->{id}) = selectrow_query($form, $dbh, $query);
1500 1464

  
1501
  do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
1502
           $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
1465
      $query        = qq|INSERT INTO warehouse (id, sortkey) VALUES (?, (SELECT COALESCE(MAX(sortkey), 0) + 1 FROM warehouse))|;
1466
      do_query($form, $dbh, $query, $form->{id});
1467
    }
1503 1468

  
1504
  if (0 < $form->{number_of_new_bins}) {
1505
    my ($num_existing_bins) = selectfirst_array_query($form, $dbh, qq|SELECT COUNT(*) FROM bin WHERE warehouse_id = ?|, $form->{id});
1506
    $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
1507
    $sth   = prepare_query($form, $dbh, $query);
1469
    do_query($form, $dbh, qq|UPDATE warehouse SET description = ?, invalid = ? WHERE id = ?|,
1470
             $form->{description}, $form->{invalid} ? 't' : 'f', conv_i($form->{id}));
1508 1471

  
1509
    foreach my $i (1..$form->{number_of_new_bins}) {
1510
      do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}" . ($i + $num_existing_bins));
1511
    }
1472
    if (0 < $form->{number_of_new_bins}) {
1473
      my ($num_existing_bins) = selectfirst_array_query($form, $dbh, qq|SELECT COUNT(*) FROM bin WHERE warehouse_id = ?|, $form->{id});
1474
      $query = qq|INSERT INTO bin (warehouse_id, description) VALUES (?, ?)|;
1475
      $sth   = prepare_query($form, $dbh, $query);
1512 1476

  
1513
    $sth->finish();
1514
  }
1477
      foreach my $i (1..$form->{number_of_new_bins}) {
1478
        do_statement($form, $sth, $query, conv_i($form->{id}), "$form->{prefix}" . ($i + $num_existing_bins));
1479
      }
1515 1480

  
1516
  $dbh->commit();
1481
      $sth->finish();
1482
    }
1483
  });
1517 1484

  
1518 1485
  $main::lxdebug->leave_sub();
1519 1486
}
......
1523 1490

  
1524 1491
  my ($self, $myconfig, $form) = @_;
1525 1492

  
1526
  # connect to database
1527
  my $dbh = $form->get_standard_dbh($myconfig);
1528

  
1529
  my ($query, @values, $commit_necessary, $sth);
1530

  
1531
  @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
1493
  SL::DB->client->with_transaction(sub {
1494
    my $dbh = SL::DB->client->dbh;
1532 1495

  
1533
  if (@values) {
1534
    $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
1535
    do_query($form, $dbh, $query, @values);
1496
    my ($query, @values, $sth);
1536 1497

  
1537
    $commit_necessary = 1;
1538
  }
1498
    @values = map { $form->{"id_${_}"} } grep { $form->{"delete_${_}"} } (1..$form->{rowcount});
1539 1499

  
1540
  $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
1541
  $sth   = prepare_query($form, $dbh, $query);
1542

  
1543
  foreach my $row (1..$form->{rowcount}) {
1544
    next if ($form->{"delete_${row}"});
1500
    if (@values) {
1501
      $query = qq|DELETE FROM bin WHERE id IN (| . join(', ', ('?') x scalar(@values)) . qq|)|;
1502
      do_query($form, $dbh, $query, @values);
1503
    }
1545 1504

  
1546
    do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
1505
    $query = qq|UPDATE bin SET description = ? WHERE id = ?|;
1506
    $sth   = prepare_query($form, $dbh, $query);
1547 1507

  
1548
    $commit_necessary = 1;
1549
  }
1508
    foreach my $row (1..$form->{rowcount}) {
1509
      next if ($form->{"delete_${row}"});
1550 1510

  
1551
  $sth->finish();
1511
      do_statement($form, $sth, $query, $form->{"description_${row}"}, conv_i($form->{"id_${row}"}));
1512
    }
1552 1513

  
1553
  $dbh->commit() if ($commit_necessary);
1514
    $sth->finish();
1515
  });
1554 1516

  
1555 1517
  $main::lxdebug->leave_sub();
1556 1518
}
......
1560 1522

  
1561 1523
  my ($self, $myconfig, $form) = @_;
1562 1524

  
1563
  # connect to database
1564
  my $dbh = $form->get_standard_dbh($myconfig);
1525
  my $rc = SL::DB->client->with_transaction(sub {
1526
    my $dbh = SL::DB->client->dbh;
1565 1527

  
1566
  my $id      = conv_i($form->{id});
1567
  my $query   = qq|SELECT i.bin_id FROM inventory i WHERE i.bin_id IN (SELECT b.id FROM bin b WHERE b.warehouse_id = ?) LIMIT 1|;
1568
  my ($count) = selectrow_query($form, $dbh, $query, $id);
1528
    my $id      = conv_i($form->{id});
1529
    my $query   = qq|SELECT i.bin_id FROM inventory i WHERE i.bin_id IN (SELECT b.id FROM bin b WHERE b.warehouse_id = ?) LIMIT 1|;
1530
    my ($count) = selectrow_query($form, $dbh, $query, $id);
1569 1531

  
1570
  if ($count) {
1571
    $main::lxdebug->leave_sub();
1572
    return 0;
1573
  }
1532
    if ($count) {
1533
      return 0;
1534
    }
1574 1535

  
1575
  do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
1576
  do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
1536
    do_query($form, $dbh, qq|DELETE FROM bin       WHERE warehouse_id = ?|, conv_i($form->{id}));
1537
    do_query($form, $dbh, qq|DELETE FROM warehouse WHERE id           = ?|, conv_i($form->{id}));
1577 1538

  
1578
  $dbh->commit();
1539
    return 1;
1540
  });
1579 1541

  
1580 1542
  $main::lxdebug->leave_sub();
1581 1543

  
1582
  return 1;
1544
  return $rc;
1583 1545
}
1584 1546

  
1585 1547
sub get_all_warehouses {
......
1587 1549

  
1588 1550
  my ($self, $myconfig, $form) = @_;
1589 1551

  
1590
  # connect to database
1591
  my $dbh = $form->get_standard_dbh($myconfig);
1552
  my $dbh = SL::DB->client->dbh;
1592 1553

  
1593 1554
  my $query = qq|SELECT w.id, w.description, w.invalid,
1594 1555
                   (SELECT COUNT(b.description) FROM bin b WHERE b.warehouse_id = w.id) AS number_of_bins
......
1605 1566

  
1606 1567
  my ($self, $myconfig, $form) = @_;
1607 1568

  
1608
  # connect to database
1609
  my $dbh = $form->get_standard_dbh($myconfig);
1569
  my $dbh = SL::DB->client->dbh;
1610 1570

  
1611 1571
  my $id    = conv_i($form->{id});
1612 1572
  my $query = qq|SELECT w.description, w.invalid

Auch abrufbar als: Unified diff