Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision bce08af4

Von Sven Schöling vor fast 8 Jahren hinzugefügt

  • ID bce08af4ca2b6a160505dedd3a28edb455e1de7a
  • Vorgänger 46b1d1ce
  • Nachfolger 660c7e53

Preisupdate in eigenen controller verlagert

...und dabei das völlig kaputte Exceptionhandling gefixt

Unterschiede anzeigen:

SL/IC.pm
49 49

  
50 50
use strict;
51 51

  
52
sub get_pricegroups {
53
  $main::lxdebug->enter_sub();
54

  
55
  my ($self, $myconfig, $form) = @_;
56

  
57
  my $dbh = $form->get_standard_dbh;
58

  
59
  # get pricegroups
60
  my $query = qq|SELECT id, pricegroup FROM pricegroup ORDER BY lower(pricegroup)|;
61
  my $pricegroups = selectall_hashref_query($form, $dbh, $query);
62

  
63
  my $i = 1;
64
  foreach my $pg (@{ $pricegroups }) {
65
    $form->{"price_$i"}         = $form->format_amount($myconfig, $form->{"price_$i"}, -2);
66
    $form->{"pricegroup_id_$i"} = "$pg->{id}";
67
    $form->{"pricegroup_$i"}    = "$pg->{pricegroup}";
68
    $i++;
69
  }
70

  
71
  #correct rows
72
  $form->{price_rows} = $i - 1;
73

  
74
  $main::lxdebug->leave_sub();
75

  
76
  return $pricegroups;
77
}
78

  
79 52
sub retrieve_buchungsgruppen {
80 53
  $main::lxdebug->enter_sub();
81 54

  
......
92 65
  $main::lxdebug->leave_sub();
93 66
}
94 67

  
95

  
96 68
sub assembly_item {
97 69
  $main::lxdebug->enter_sub();
98 70

  
......
605 577
  return $form->{parts};
606 578
}
607 579

  
608
sub _create_filter_for_priceupdate {
609
  $main::lxdebug->enter_sub();
610

  
611
  my $self     = shift;
612
  my $myconfig = \%main::myconfig;
613
  my $form     = $main::form;
614

  
615
  my @where_values;
616
  my $where = '1 = 1';
617

  
618
  foreach my $item (qw(partnumber drawing microfiche make model pg.partsgroup)) {
619
    my $column = $item;
620
    $column =~ s/.*\.//;
621
    next unless ($form->{$column});
622

  
623
    $where .= qq| AND $item ILIKE ?|;
624
    push(@where_values, like($form->{$column}));
625
  }
626

  
627
  foreach my $item (qw(description serialnumber)) {
628
    next unless ($form->{$item});
629

  
630
    $where .= qq| AND (${item} ILIKE ?)|;
631
    push(@where_values, like($form->{$item}));
632
  }
633

  
634

  
635
  # items which were never bought, sold or on an order
636
  if ($form->{itemstatus} eq 'orphaned') {
637
    $where .=
638
      qq| AND (p.onhand = 0)
639
          AND p.id NOT IN
640
            (
641
              SELECT DISTINCT parts_id FROM invoice
642
              UNION
643
              SELECT DISTINCT parts_id FROM assembly
644
              UNION
645
              SELECT DISTINCT parts_id FROM orderitems
646
            )|;
647

  
648
  } elsif ($form->{itemstatus} eq 'active') {
649
    $where .= qq| AND p.obsolete = '0'|;
650

  
651
  } elsif ($form->{itemstatus} eq 'obsolete') {
652
    $where .= qq| AND p.obsolete = '1'|;
653

  
654
  } elsif ($form->{itemstatus} eq 'onhand') {
655
    $where .= qq| AND p.onhand > 0|;
656

  
657
  } elsif ($form->{itemstatus} eq 'short') {
658
    $where .= qq| AND p.onhand < p.rop|;
659

  
660
  }
661

  
662
  foreach my $column (qw(make model)) {
663
    next unless ($form->{$column});
664
    $where .= qq| AND p.id IN (SELECT DISTINCT parts_id FROM makemodel WHERE $column ILIKE ?|;
665
    push(@where_values, like($form->{$column}));
666
  }
667

  
668
  $main::lxdebug->leave_sub();
669

  
670
  return ($where, @where_values);
671
}
672

  
673
sub get_num_matches_for_priceupdate {
674
  $main::lxdebug->enter_sub();
675

  
676
  my $self     = shift;
677

  
678
  my $myconfig = \%main::myconfig;
679
  my $form     = $main::form;
680

  
681
  my $dbh      = $form->get_standard_dbh($myconfig);
682

  
683
  my ($where, @where_values) = $self->_create_filter_for_priceupdate();
684

  
685
  my $num_updated = 0;
686
  my $query;
687

  
688
  for my $column (qw(sellprice listprice)) {
689
    next if ($form->{$column} eq "");
690

  
691
    $query =
692
      qq|SELECT COUNT(*)
693
         FROM parts
694
         WHERE id IN
695
           (SELECT p.id
696
            FROM parts p
697
            LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
698
            WHERE $where)|;
699
    my ($result)  = selectfirst_array_query($form, $dbh, $query, @where_values);
700
    $num_updated += $result if (0 <= $result);
701
  }
702

  
703
  $query =
704
    qq|SELECT COUNT(*)
705
       FROM prices
706
       WHERE parts_id IN
707
         (SELECT p.id
708
          FROM parts p
709
          LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
710
          WHERE $where) AND (pricegroup_id = ?)|;
711
  my $sth = prepare_query($form, $dbh, $query);
712

  
713
  for my $i (1 .. $form->{price_rows}) {
714
    next if ($form->{"price_$i"} eq "");
715

  
716
    my ($result)  = do_statement($form, $sth, $query, @where_values, conv_i($form->{"pricegroup_id_$i"}));
717
    $num_updated += $result if (0 <= $result);
718
  }
719
  $sth->finish();
720

  
721
  $main::lxdebug->leave_sub();
722

  
723
  return $num_updated;
724
}
725

  
726
sub update_prices {
727
  my ($self, $myconfig, $form) = @_;
728
  $main::lxdebug->enter_sub();
729

  
730
  my $num_updated = SL::DB->client->with_transaction(\&_update_prices, $self, $myconfig, $form);
731

  
732
  $main::lxdebug->leave_sub();
733
  return $num_updated;
734
}
735

  
736
sub _update_prices {
737
  my ($self, $myconfig, $form) = @_;
738

  
739
  my ($where, @where_values) = $self->_create_filter_for_priceupdate();
740
  my $num_updated = 0;
741

  
742
  # connect to database
743
  my $dbh = SL::DB->client->dbh;
744

  
745
  for my $column (qw(sellprice listprice)) {
746
    next if ($form->{$column} eq "");
747

  
748
    my $value = $form->parse_amount($myconfig, $form->{$column});
749
    my $operator = '+';
750

  
751
    if ($form->{"${column}_type"} eq "percent") {
752
      $value = ($value / 100) + 1;
753
      $operator = '*';
754
    }
755

  
756
    my $query =
757
      qq|UPDATE parts SET $column = $column $operator ?
758
         WHERE id IN
759
           (SELECT p.id
760
            FROM parts p
761
            LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
762
            WHERE $where)|;
763
    my $result    = do_query($form, $dbh, $query, $value, @where_values);
764
    $num_updated += $result if (0 <= $result);
765
  }
766

  
767
  my $q_add =
768
    qq|UPDATE prices SET price = price + ?
769
       WHERE parts_id IN
770
         (SELECT p.id
771
          FROM parts p
772
          LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
773
          WHERE $where) AND (pricegroup_id = ?)|;
774
  my $sth_add = prepare_query($form, $dbh, $q_add);
775

  
776
  my $q_multiply =
777
    qq|UPDATE prices SET price = price * ?
778
       WHERE parts_id IN
779
         (SELECT p.id
780
          FROM parts p
781
          LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
782
          WHERE $where) AND (pricegroup_id = ?)|;
783
  my $sth_multiply = prepare_query($form, $dbh, $q_multiply);
784

  
785
  for my $i (1 .. $form->{price_rows}) {
786
    next if ($form->{"price_$i"} eq "");
787

  
788
    my $value = $form->parse_amount($myconfig, $form->{"price_$i"});
789
    my $result;
790

  
791
    if ($form->{"pricegroup_type_$i"} eq "percent") {
792
      $result = do_statement($form, $sth_multiply, $q_multiply, ($value / 100) + 1, @where_values, conv_i($form->{"pricegroup_id_$i"}));
793
    } else {
794
      $result = do_statement($form, $sth_add, $q_add, $value, @where_values, conv_i($form->{"pricegroup_id_$i"}));
795
    }
796

  
797
    $num_updated += $result if (0 <= $result);
798
  }
799

  
800
  $sth_add->finish();
801
  $sth_multiply->finish();
802

  
803
  return $num_updated;
804
}
805

  
806 580
# get partnumber, description, unit, sellprice and soldtotal with choice through $sortorder for Top100
807 581
sub get_parts {
808 582
  $main::lxdebug->enter_sub();

Auch abrufbar als: Unified diff