-
- $main::lxdebug->leave_sub();
-}
-
-##########################
-# get pricegroups from database
-# build up selected pricegroup
-# if an exchange rate - change price
-# for each part
-#
-sub get_pricegroups_for_parts {
-
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form) = @_;
-
- my $dbh = $form->get_standard_dbh;
-
- $form->{"PRICES"} = {};
-
- my $i = 1;
- my $id = 0;
- my $all_units = AM->retrieve_units($myconfig, $form);
- while (($form->{"id_$i"}) or ($form->{"new_id_$i"})) {
- $form->{"PRICES"}{$i} = [];
-
- $id = $form->{"id_$i"};
-
- if (!($form->{"id_$i"}) and $form->{"new_id_$i"}) {
- $id = $form->{"new_id_$i"};
- }
-
- my ($price, $selectedpricegroup_id) = split(/--/, $form->{"sellprice_pg_$i"});
-
- my $pricegroup_old = $form->{"pricegroup_old_$i"};
-
- # sellprice has format 13,0000 or 0,00000, can't check for 0 numerically
- my $sellprice = $form->{"sellprice_$i"};
- my $pricegroup_id = $form->{"pricegroup_id_$i"};
- $form->{"new_pricegroup_$i"} = $selectedpricegroup_id;
- $form->{"old_pricegroup_$i"} = $pricegroup_old;
-
- my $price_new = $form->{"price_new_$i"};
- my $price_old = $form->{"price_old_$i"};
-
- if (!$form->{"unit_old_$i"}) {
- # Neue Ware aus der Datenbank. In diesem Fall ist unit_$i die
- # Einheit, wie sie in den Stammdaten hinterlegt wurde.
- # Es sollte also angenommen werden, dass diese ausgewaehlt war.
- $form->{"unit_old_$i"} = $form->{"unit_$i"};
- }
-
- # Die zuletzt ausgewaehlte mit der aktuell ausgewaehlten Einheit
- # vergleichen und bei Unterschied den Preis entsprechend umrechnen.
- $form->{"selected_unit_$i"} = $form->{"unit_$i"} unless ($form->{"selected_unit_$i"});
-
- if (!$all_units->{$form->{"selected_unit_$i"}} ||
- ($all_units->{$form->{"selected_unit_$i"}}->{"base_unit"} ne
- $all_units->{$form->{"unit_old_$i"}}->{"base_unit"})) {
- # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
- # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
- # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
- $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"};
- }
-
- my $basefactor = 1;
-
- if ($form->{"unit_old_$i"} ne $form->{"selected_unit_$i"}) {
- if (defined($all_units->{$form->{"unit_old_$i"}}->{"factor"}) &&
- $all_units->{$form->{"unit_old_$i"}}->{"factor"}) {
- $basefactor = $all_units->{$form->{"selected_unit_$i"}}->{"factor"} /
- $all_units->{$form->{"unit_old_$i"}}->{"factor"};
- }
- }
-
- if (!$form->{"basefactor_$i"}) {
- $form->{"basefactor_$i"} = 1;
- }
-
- my $query =
- qq|SELECT
- 0 as pricegroup_id,
- sellprice AS default_sellprice,
- '' AS pricegroup,
- sellprice AS price,
- 'selected' AS selected
- FROM parts
- WHERE id = ?
- UNION ALL
- SELECT
- pricegroup_id,
- parts.sellprice AS default_sellprice,
- pricegroup.pricegroup,
- price,
- '' AS selected
- FROM prices
- LEFT JOIN parts ON parts.id = parts_id
- LEFT JOIN pricegroup ON pricegroup.id = pricegroup_id
- WHERE parts_id = ?
- ORDER BY pricegroup|;
- my @values = (conv_i($id), conv_i($id));
- my $pkq = prepare_execute_query($form, $dbh, $query, @values);
-
- while (my $pkr = $pkq->fetchrow_hashref('NAME_lc')) {
- $pkr->{id} = $id;
- $pkr->{selected} = '';
-
- # if there is an exchange rate change price
- if (($form->{exchangerate} * 1) != 0) {
- $pkr->{price} /= $form->{exchangerate};
- }
-
- $pkr->{price} *= $form->{"basefactor_$i"};
- $pkr->{price} *= $basefactor;
- $pkr->{price_ufmt} = $pkr->{price};
- $pkr->{price} = $form->format_amount($myconfig, $pkr->{price}, 5);
-
- if (!defined $selectedpricegroup_id) {
- # new entries in article list, either old invoice was loaded (edit) or a new article was added
- # Case A: open old invoice, no pricegroup selected
- # Case B: add new article to invoice, no pricegroup selected
-
- # to distinguish case A and B the variable pricegroup_id_$i is used
- # for new articles this variable isn't defined, for loaded articles it is
- # sellprice can't be used, as it already has 0,00 set
-
- if ($pkr->{pricegroup_id} eq $form->{"pricegroup_id_$i"} and defined $form->{"pricegroup_id_$i"}) {
- # Case A
- $pkr->{selected} = ' selected';
- } elsif ($pkr->{pricegroup_id} eq $form->{customer_klass}
- and not defined $form->{"pricegroup_id_$i"}
- and $pkr->{price_ufmt} != 0 # only use customer pricegroup price if it has a value, else use default_sellprice
- # for the case where pricegroup prices haven't been set
- ) {
- # Case B: use default pricegroup of customer
-
- $pkr->{selected} = ' selected'; # unless $form->{selected};
- # no customer pricesgroup set
- if ($pkr->{price_ufmt} == $pkr->{default_sellprice}) {
-
- $pkr->{price} = $form->{"sellprice_$i"};
-
- } else {
-
-# this sub should not set anything and only return. --sschoeling, 20090506
-# is this correct? put in again... -- grichardson 20110119
- $form->{"sellprice_$i"} = $pkr->{price};
- }
-
- } elsif ($pkr->{price_ufmt} == $pkr->{default_sellprice} and $pkr->{default_sellprice} != 0) {
- $pkr->{price} = $form->{"sellprice_$i"};
- $pkr->{selected} = ' selected';
- }
- }
-
- # existing article: pricegroup or price changed
- if ($selectedpricegroup_id or $selectedpricegroup_id == 0) {
- if ($selectedpricegroup_id ne $pricegroup_old) {
- # pricegroup has changed
- if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
- $pkr->{selected} = ' selected';
- }
- } elsif ( ($form->parse_amount($myconfig, $price_new)
- != $form->parse_amount($myconfig, $form->{"sellprice_$i"}))
- and ($price_new ne 0) and defined $price_new) {
- # sellprice has changed
- # when loading existing invoices $price_new is NULL
- if ($pkr->{pricegroup_id} == 0) {
- $pkr->{price} = $form->{"sellprice_$i"};
- $pkr->{selected} = ' selected';
- }
- } elsif ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
- # neither sellprice nor pricegroup changed
- $pkr->{selected} = ' selected';
- if ( ($pkr->{pricegroup_id} == 0) and ($pkr->{price} == $form->{"sellprice_$i"})) {
- # $pkr->{price} = $form->{"sellprice_$i"};
- } else {
- $pkr->{price} = $form->{"sellprice_$i"};
- }
- }
- }
- push @{ $form->{PRICES}{$i} }, $pkr;
-
- }
- $form->{"basefactor_$i"} *= $basefactor;
-
- $i++;
-
- $pkq->finish;
- }
-