$form->{$number_field} ||= $trans_number->create_unique;
if ($form->{id}) {
- $query = qq|DELETE FROM custom_variables
- WHERE (config_id IN (SELECT id FROM custom_variable_configs WHERE module = 'IC'))
- AND (sub_module = 'orderitems')
- AND (trans_id IN (SELECT id FROM orderitems WHERE trans_id = ?))|;
- do_query($form, $dbh, $query, $form->{id});
-
$query = qq|DELETE FROM shipto | .
qq|WHERE trans_id = ? AND module = 'OE'|;
do_query($form, $dbh, $query, $form->{id});
$pricegroup_id *= 1;
$pricegroup_id = undef if !$pricegroup_id;
+ my $position = $i;
+
# save detail record in orderitems table
+ my $cvars;
if (! $form->{"orderitems_id_$i"}) {
$query = qq|SELECT nextval('orderitemsid')|;
($form->{"orderitems_id_$i"}) = selectrow_query($form, $dbh, $query);
- $query = qq|INSERT INTO orderitems (id) VALUES (?)|;
- do_query($form, $dbh, $query, $form->{"orderitems_id_$i"});
+ $query = qq|INSERT INTO orderitems (id, position) VALUES (?, ?)|;
+ do_query($form, $dbh, $query, $form->{"orderitems_id_$i"}, conv_i($position));
+
+ # get values for CVars from master data for new items
+ $cvars = CVar->get_custom_variables(dbh => $dbh,
+ module => 'IC',
+ trans_id => $form->{"id_$i"},
+ );
+ } else {
+ # get values for CVars from custom_variables for existing items
+ $cvars = CVar->get_custom_variables(dbh => $dbh,
+ module => 'IC',
+ sub_module => 'orderitems',
+ trans_id => $form->{"orderitems_id_$i"},
+ );
}
+ # map only non-editable CVars to form (editable ones are already there)
+ map { $form->{"ic_cvar_$_->{name}_$i"} = $_->{value} unless $_->{flag_editable} } @{ $cvars };
+
my $orderitems_id = $form->{"orderitems_id_$i"};
push @processed_orderitems, $orderitems_id;
$query = <<SQL;
UPDATE orderitems SET
- trans_id = ?, parts_id = ?, description = ?, longdescription = ?, qty = ?, base_qty = ?,
+ trans_id = ?, position = ?, parts_id = ?, description = ?, longdescription = ?, qty = ?, base_qty = ?,
sellprice = ?, discount = ?, unit = ?, reqdate = ?, project_id = ?, serialnumber = ?, ship = ?,
pricegroup_id = ?, ordnumber = ?, transdate = ?, cusordnumber = ?, subtotal = ?,
marge_percent = ?, marge_total = ?, lastcost = ?, price_factor_id = ?,
+ active_price_source = ?, active_discount_source = ?,
price_factor = (SELECT factor FROM price_factors WHERE id = ?), marge_price_factor = ?
WHERE id = ?
SQL
@values = (
- conv_i($form->{id}), conv_i($form->{"id_$i"}),
+ conv_i($form->{id}), conv_i($position), conv_i($form->{"id_$i"}),
$form->{"description_$i"}, $restricter->process($form->{"longdescription_$i"}),
$form->{"qty_$i"}, $baseqty,
$fxsellprice, $form->{"discount_$i"},
$form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
$form->{"cusordnumber_$i"}, $form->{"subtotal_$i"} ? 't' : 'f',
$form->{"marge_percent_$i"}, $form->{"marge_absolut_$i"},
- $form->{"lastcost_$i"},
- conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
- conv_i($form->{"marge_price_factor_$i"}),
+ $form->{"lastcost_$i"}, conv_i($form->{"price_factor_id_$i"}),
+ $form->{"active_price_source_$i"}, $form->{"active_discount_source_$i"},
+ conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"marge_price_factor_$i"}),
conv_i($orderitems_id),
);
name_prefix => 'ic_',
name_postfix => "_$i",
dbh => $dbh);
+ # link previous items with orderitems
+ foreach (qw(quotation_orderitems orderitems invoice)) {
+ if ($form->{"converted_from_${_}_id_$i"}) {
+ my $table = $_;
+ $table = 'orderitems' if $table eq 'quotation_orderitems';
+ RecordLinks->create_links('dbh' => $dbh,
+ 'mode' => 'ids',
+ 'from_table' => $table,
+ 'from_ids' => $form->{"converted_from_${_}_id_$i"},
+ 'to_table' => 'orderitems',
+ 'to_id' => $orderitems_id,
+ );
+ delete $form->{"converted_from_${_}_id_$i"};
+ }
+ }
}
}
# search for orphaned ids
o.sellprice, o.parts_id AS id, o.unit, o.discount, p.notes AS partnotes, p.inventory_accno_id AS part_inventory_accno_id,
o.reqdate, o.project_id, o.serialnumber, o.ship, o.lastcost,
o.ordnumber, o.transdate, o.cusordnumber, o.subtotal, o.longdescription,
- o.price_factor_id, o.price_factor, o.marge_price_factor,
+ o.price_factor_id, o.price_factor, o.marge_price_factor, o.active_price_source, o.active_discount_source,
pr.projectnumber, p.formel,
pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
FROM orderitems o
($form->{id}
? qq|WHERE o.trans_id = ?|
: qq|WHERE o.trans_id IN (| . join(", ", map("?", @ids)) . qq|)|) .
- qq|ORDER BY o.oid|;
+ qq|ORDER BY o.trans_id, o.position|;
@ids = $form->{id} ? ($form->{id}) : @ids;
$sth = prepare_execute_query($form, $dbh, $query, @values);
my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
my $oe_query = qq|SELECT * FROM oe WHERE id = ?|;
- my $oi_query = qq|SELECT * FROM orderitems WHERE trans_id = ?|;
+ my $oi_query = qq|SELECT * FROM orderitems WHERE trans_id = ? ORDER BY position|;
my $order = selectfirst_hashref_query($form, $dbh, $oe_query, conv_i($params{id}));
$order->{orderitems} = selectall_hashref_query( $form, $dbh, $oi_query, conv_i($params{id}));
$sth->finish;
}
+ my $cvars;
+ if (! $form->{"orderitems_id_$i"}) {
+ # get values for CVars from master data for new items
+ $cvars = CVar->get_custom_variables(dbh => $dbh,
+ module => 'IC',
+ trans_id => $form->{"id_$i"},
+ );
+ } else {
+ # get values for CVars from custom_variables for existing items
+ $cvars = CVar->get_custom_variables(dbh => $dbh,
+ module => 'IC',
+ sub_module => 'orderitems',
+ trans_id => $form->{"orderitems_id_$i"},
+ );
+ }
+ # map only non-editable CVars to form (editable ones are already there)
+ map { $form->{"ic_cvar_$_->{name}_$i"} = $_->{value} unless $_->{flag_editable} } @{ $cvars };
+
push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
for @{ $ic_cvar_configs };