+##########################
+# get pricegroups from database
+# build up selected pricegroup
+# if an exchange rate - change price
+# for each part
+#
+sub get_pricegroups_for_parts {
+print STDERR "IS.pm - get_pricegroups_for_parts\n";
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+
+ my $dbh = $form->dbconnect($myconfig);
+
+ my $i = 1;
+ my $id = 0;
+
+ while (($form->{"id_$i"}) or ($form->{"new_id_$i"})) {
+
+ $id = $form->{"id_$i"};
+
+ if (!($form->{"id_$i"}) and $form->{"new_id_$i"}) {
+
+ $id = $form->{"new_id_$i"};
+ }
+
+ ($price, $selectedpricegroup_id) = split /--/, $form->{"sellprice_drag_$i"};
+# print (STDERR "sellprice_drag_$i", Dumper($form->{"sellprice_drag_$i"}));
+
+ $pricegroup_old = $form->{"pricegroup_old_$i"};
+# print (STDERR "pricegroup_old_i-$i", Dumper($pricegroup_old));
+
+ $price_new = $form->{"price_new_$i"};
+
+ $price_old = $form->{"price_old_$i"};
+
+
+ $query = qq|SELECT pricegroup_id, (SELECT p.sellprice from parts p where p.id = $id) as default_sellprice,(SELECT pg.pricegroup FROM pricegroup pg WHERE id=pricegroup_id) AS pricegroup, price, '' AS selected FROM prices WHERE parts_id = $id UNION SELECT 0 as pricegroup_id,(SELECT sellprice FROM parts WHERE id=$id) as default_sellprice,'' as pricegroup, (SELECT DISTINCT sellprice from parts where id=$id) as price, 'selected' AS selected from prices ORDER BY pricegroup|;
+
+ $pkq = $dbh->prepare($query);
+ $pkq->execute || $form->dberror($query);
+ while ($pkr = $pkq->fetchrow_hashref(NAME_lc)) {
+# push @{ $form->{PRICES}{$id} }, $pkr;
+ push @{ $form->{PRICES}{$i} }, $pkr;
+ $pkr->{id} = $id;
+ $pkr->{selected} = '';
+
+ # if there is an exchange rate change price
+ if (($form->{exchangerate} * 1) != 0) {
+# print STDERR "WECHSELKURS?-$form->{exchangerate}\n";
+ $pkr->{price} /= $form->{exchangerate};
+ }
+ $pkr->{price} = $form->format_amount($myconfig,$pkr->{price},5);
+
+
+ if ($selectedpricegroup_id eq undef) {
+ if ($pkr->{pricegroup_id} eq $form->{customer_klass}) {
+print STDERR " INIT ROW \n";
+#print (STDERR " PREIS", Dumper($pkr->{price}));
+ $pkr->{selected} = ' selected';
+ $last->{selected} = '';
+# print (STDERR " SELLPRICE", Dumper($form->{"sellprice_$i"}));
+
+ # no customer pricesgroup set
+ if ($pkr->{price} == $pkr->{default_sellprice}) {
+print (STDERR " PREIS IST DEFAULT-SELLPRICE", Dumper($form->{"sellprice_$i"}));
+ $pkr->{price} = $form->{"sellprice_$i"};
+
+# if ($form->{tradediscount}){
+# print (STDERR "TRADE--", Dumper($pkr->{price}));
+# $pkr->{price} =$pkr->{price} * (1 - $form->{tradediscount});
+# $pkr->{price} = $form->format_amount($myconfig,$pkr->{price},5);
+# print (STDERR "TRADE--", Dumper($pkr->{price}));
+# }
+
+ } else {
+print STDERR " PREIS IST NICHT NULL\n";
+ $form->{"sellprice_$i"} = $pkr->{price};
+ }
+# print (STDERR " PRICE", Dumper($pkr->{price}));
+
+ } else {
+print STDERR " INIT ROW but what\n";
+print (STDERR " PREIS -", Dumper($pkr->{price}), "Default", Dumper($pkr->{default_sellprice}));
+ if ($pkr->{price} == $pkr->{default_sellprice}) {
+print (STDERR " PREIS IST DEFAULT-", Dumper($form->{"sellprice_$i"}));
+ $pkr->{price} = $form->{"sellprice_$i"};
+ $pkr->{selected} = ' selected';
+ }
+ }
+ }
+ if ($selectedpricegroup_id or $selectedpricegroup_id == 0){
+ if ($selectedpricegroup_id ne $pricegroup_old) {
+ if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
+ if ($price_new != $form->{"sellprice_$i"}) {
+print STDERR " MANUELLEN PREIS WÄHLEN\n";
+ } else {
+print STDERR " UPDATE CHANGE PRICEGROUP\n";
+ $pkr->{selected} = ' selected';
+ $last->{selected} = '';
+#$form->{"pricegroup_old_$i"} = $pkr->{$pricegroup_id};
+ }
+ }
+ } else {
+ if (($price_new != $form->{"sellprice_$i"}) and ($price_new ne 0)) {
+ if ($pkr->{pricegroup_id} == 0) {
+print STDERR " UPDATE CHANGE PRICEGROUP with price manuelly\n";
+print (STDERR " SELLPRICE??? ---", Dumper($form->{"sellprice_$i"}));
+print (STDERR " NEWPRICE??? ---", Dumper($price_new));
+ $pkr->{price} = $form->{"sellprice_$i"};
+ $pkr->{selected} = ' selected';
+ $last->{selected} = '';
+ #$form->{"sellprice_$i"} = $form->format_amount($myconfig, $price_new, 2);
+# print (STDERR "----5555---", Dumper($pkr));
+ }
+ } else {
+ if ($pkr->{pricegroup_id} eq $selectedpricegroup_id) {
+print STDERR " UPDATE NO CHANGE\n";
+ $pkr->{selected} = ' selected';
+ $last->{selected} = '';
+print STDERR " DEFAULTPRICE??? ---$pkr->{default_sellprice}\n";
+print (STDERR " SELLPRICE??? ---", Dumper($form->{"sellprice_$i"}));
+# print (STDERR " HIER DER SELLPRICE DEFAULT??? ---", Dumper($form));
+print STDERR " NEWPRICE??? ---$price_new_\n";
+ if (($pkr->{pricegroup_id} == 0) and ($pkr->{price} == $form->{"sellprice_$i"})) {
+print (STDERR " UPDATE NO CHANGE BUT PRICE MANUELLY SET", Dumper($pkr->{price}));
+ # $pkr->{price} = $form->{"sellprice_$i"};
+ } else {
+ $pkr->{price} = $form->{"sellprice_$i"};
+ }
+#print (STDERR " FEHLER", Dumper($form->{"sellprice_$i"}));
+ }
+ }
+ }
+ }
+ }
+ $i++;
+
+ $pkq->finish;
+ }
+
+ $dbh->disconnect;
+
+# print (STDERR "TEST", Dumper($form->{PRICES}));
+# print (STDERR "TEST id_$i", Dumper($form->{"id_$i"}));
+ $main::lxdebug->leave_sub();
+}
+