+ if($form->{reqdatefrom}) {
+ $query .= qq| AND o.reqdate >= ?|;
+ push(@values, conv_date($form->{reqdatefrom}));
+ }
+
+ if($form->{reqdateto}) {
+ $query .= qq| AND o.reqdate <= ?|;
+ push(@values, conv_date($form->{reqdateto}));
+ }
+
+ if($form->{insertdatefrom}) {
+ $query .= qq| AND o.itime::DATE >= ?|;
+ push(@values, conv_date($form->{insertdatefrom}));
+ }
+
+ if($form->{insertdateto}) {
+ $query .= qq| AND o.itime::DATE <= ?|;
+ push(@values, conv_date($form->{insertdateto}));
+ }
+
+ if ($form->{shippingpoint}) {
+ $query .= qq| AND o.shippingpoint ILIKE ?|;
+ push(@values, '%' . $form->{shippingpoint} . '%');
+ }
+
+ if ($form->{taxzone_id} ne '') { # taxzone_id could be 0
+ $query .= qq| AND tz.id = ?|;
+ push(@values, $form->{taxzone_id});
+ }
+
+ if ($form->{transaction_description}) {
+ $query .= qq| AND o.transaction_description ILIKE ?|;
+ push(@values, '%' . $form->{transaction_description} . '%');
+ }
+
+ if ($form->{periodic_invoices_active} ne $form->{periodic_invoices_inactive}) {
+ my $not = $form->{periodic_invoices_inactive} ? 'NOT' : '';
+ $query .= qq| AND ${not} COALESCE(pcfg.active, 'f')|;
+ }
+
+ if ($form->{reqdate_unset_or_old}) {
+ $query .= qq| AND ((o.reqdate IS NULL) OR (o.reqdate < date_trunc('month', current_date)))|;
+ }
+
+ if (($form->{order_probability_value} || '') ne '') {
+ my $op = $form->{order_probability_value} eq 'le' ? '<=' : '>=';
+ $query .= qq| AND (o.order_probability ${op} ?)|;
+ push @values, $form->{order_probability_value};
+ }
+
+ if ($form->{expected_billing_date_from}) {
+ $query .= qq| AND (o.expected_billing_date >= ?)|;
+ push @values, conv_date($form->{expected_billing_date_from});
+ }
+
+ if ($form->{expected_billing_date_to}) {
+ $query .= qq| AND (o.expected_billing_date <= ?)|;
+ push @values, conv_date($form->{expected_billing_date_to});
+ }
+
+ if ($form->{all}) {
+ my @tokens = parse_line('\s+', 0, $form->{all});
+ # ordnumber quonumber customer.name vendor.name transaction_description
+ $query .= qq| AND (
+ o.ordnumber ILIKE ? OR
+ o.quonumber ILIKE ? OR
+ ct.name ILIKE ? OR
+ o.transaction_description ILIKE ?
+ )| for @tokens;
+ push @values, ("%$_%")x4 for @tokens;
+ }
+
+ my ($cvar_where, @cvar_values) = CVar->build_filter_query('module' => 'CT',
+ 'trans_id_field' => 'ct.id',
+ 'filter' => $form,
+ );
+ if ($cvar_where) {
+ $query .= qq| AND ($cvar_where)|;
+ push @values, @cvar_values;
+ }
+
+ my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
+ my $sortorder = join(', ', map { "${_} ${sortdir} " } ("o.id", $form->sort_columns("transdate", $ordnumber, "name"), "o.itime"));
+ my %allowed_sort_columns = (
+ "transdate" => "o.transdate",
+ "reqdate" => "o.reqdate",
+ "id" => "o.id",
+ "ordnumber" => "o.ordnumber",
+ "cusordnumber" => "o.cusordnumber",
+ "quonumber" => "o.quonumber",
+ "name" => "ct.name",
+ "employee" => "e.name",
+ "salesman" => "s.name",
+ "shipvia" => "o.shipvia",
+ "transaction_description" => "o.transaction_description",
+ "shippingpoint" => "o.shippingpoint",
+ "insertdate" => "o.itime",
+ "taxzone" => "tz.description",
+ "payment_terms" => "pt.description",
+ );