$form->{amount}{IC_expense} = $form->{expense_accno};
$form->{amount}{IC_cogs} = $form->{expense_accno};
- my @pricegroups = ();
- my @pricegroups_not_used = ();
-
# get prices
- $query =
- qq|SELECT p.parts_id, p.pricegroup_id, p.price,
- (SELECT pg.pricegroup
- FROM pricegroup pg
- WHERE pg.id = p.pricegroup_id) AS pricegroup
- FROM prices p
- WHERE (parts_id = ?)
- ORDER BY pricegroup|;
- $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
-
- #for pricegroups
- my $i = 1;
- while (($form->{"klass_$i"}, $form->{"pricegroup_id_$i"},
- $form->{"price_$i"}, $form->{"pricegroup_$i"})
- = $sth->fetchrow_array()) {
- push @pricegroups, $form->{"pricegroup_id_$i"};
- $i++;
- }
-
- $sth->finish;
-
- # get pricegroups
- $query = qq|SELECT id, pricegroup FROM pricegroup|;
- $form->{PRICEGROUPS} = selectall_hashref_query($form, $dbh, $query);
-
- #find not used pricegroups
- while (my $tmp = pop(@{ $form->{PRICEGROUPS} })) {
- my $in_use = 0;
- foreach my $item (@pricegroups) {
- if ($item eq $tmp->{id}) {
- $in_use = 1;
- last;
- }
- }
- push(@pricegroups_not_used, $tmp) unless ($in_use);
- }
-
- # if not used pricegroups are avaible
- if (@pricegroups_not_used) {
+ $query = <<SQL;
+ SELECT pg.pricegroup, pg.id AS pricegroup_id, COALESCE(pr.price, 0) AS price
+ FROM pricegroup pg
+ LEFT JOIN prices pr ON (pr.pricegroup_id = pg.id) AND (pr.parts_id = ?)
+ ORDER BY lower(pg.pricegroup)
+SQL
- foreach my $name (@pricegroups_not_used) {
- $form->{"klass_$i"} = "$name->{id}";
- $form->{"pricegroup_id_$i"} = "$name->{id}";
- $form->{"pricegroup_$i"} = "$name->{pricegroup}";
- $i++;
- }
+ my $row = 1;
+ foreach $ref (selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}))) {
+ $form->{"${_}_${row}"} = $ref->{$_} for qw(pricegroup_id pricegroup price);
+ $row++;
}
-
- #correct rows
- $form->{price_rows} = $i - 1;
+ $form->{price_rows} = $row - 1;
# get makes
if ($form->{makemodel}) {
my $dbh = $form->dbconnect($myconfig);
# get pricegroups
- my $query = qq|SELECT id, pricegroup FROM pricegroup|;
+ my $query = qq|SELECT id, pricegroup FROM pricegroup ORDER BY lower(pricegroup)|;
my $pricegroups = selectall_hashref_query($form, $dbh, $query);
my $i = 1;