+ $query = qq|SELECT COUNT(shipto_id) AS used FROM (
+ SELECT shipto_id FROM oe UNION
+ SELECT shipto_id FROM ar UNION
+ SELECT shipto_id FROM delivery_orders
+ ) AS stid WHERE shipto_id = ? OR ? = 0|;
+ ($form->{shiptoused}) = selectfirst_array_query($form, $dbh, $query, ($form->{shipto_id})x2);
+
+ $sth->finish;
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub get_delivery {
+ $main::lxdebug->enter_sub();
+
+ my ( $self, $myconfig, $form ) = @_;
+ my $dbh = $form->dbconnect($myconfig);
+
+ my $arap = $form->{db} eq "vendor" ? "ap" : "ar";
+ my $db = $form->{db} eq "customer" ? "customer" : "vendor";
+ my $qty_sign = $form->{db} eq 'vendor' ? ' * -1 AS qty' : '';
+
+ my $where = " WHERE 1=1 ";
+ my @values;
+
+ if ($form->{shipto_id} && ($arap eq "ar")) {
+ $where .= "AND ${arap}.shipto_id = ?";
+ push(@values, $form->{shipto_id});
+ } else {
+ $where .= "AND ${arap}.${db}_id = ?";
+ push(@values, $form->{id});
+ }
+
+ if ($form->{from}) {
+ $where .= "AND ${arap}.transdate >= ?";
+ push(@values, conv_date($form->{from}));
+ }
+ if ($form->{to}) {
+ $where .= "AND ${arap}.transdate <= ?";
+ push(@values, conv_date($form->{to}));
+ }
+ my $query =
+ qq|SELECT s.shiptoname, i.qty $qty_sign, | .
+ qq| ${arap}.id, ${arap}.transdate, ${arap}.invnumber, ${arap}.ordnumber, | .
+ qq| i.description, i.unit, i.sellprice, | .
+ qq| oe.id AS oe_id, invoice | .
+ qq|FROM $arap | .
+ qq|LEFT JOIN shipto s ON | .
+ ($arap eq "ar"
+ ? qq|(ar.shipto_id = s.shipto_id) |
+ : qq|(ap.id = s.trans_id) |) .
+ qq|LEFT JOIN invoice i ON (${arap}.id = i.trans_id) | .
+ qq|LEFT join parts p ON (p.id = i.parts_id) | .
+ qq|LEFT JOIN oe ON (oe.ordnumber = ${arap}.ordnumber AND NOT ${arap}.ordnumber = '') | .
+ $where .
+ qq|ORDER BY ${arap}.transdate DESC LIMIT 15|;
+
+ $form->{DELIVERY} = selectall_hashref_query($form, $dbh, $query, @values);
+
+ $dbh->disconnect;
+
+ $main::lxdebug->leave_sub();
+}
+
+sub _save_note {
+ $main::lxdebug->enter_sub();
+
+ my $self = shift;
+ my %params = @_;
+
+ my $form = $main::form;
+
+ Common::check_params(\%params, 'dbh');
+
+ if (!$form->{NOTE_subject}) {
+ $main::lxdebug->leave_sub();
+ return;
+ }
+
+ my $dbh = $params{dbh};
+
+ my %follow_up;
+ my %note = (
+ 'id' => $form->{NOTE_id},
+ 'subject' => $form->{NOTE_subject},
+ 'body' => $form->{NOTE_body},
+ 'trans_id' => $form->{id},
+ 'trans_module' => 'ct',
+ );
+
+ $note{id} = Notes->save(%note);
+
+ if ($form->{FU_date}) {
+ %follow_up = (
+ 'id' => $form->{FU_id},
+ 'note_id' => $note{id},
+ 'follow_up_date' => $form->{FU_date},
+ 'created_for_user' => $form->{FU_created_for_user},
+ 'done' => $form->{FU_done} ? 1 : 0,
+ 'subject' => $form->{NOTE_subject},
+ 'body' => $form->{NOTE_body},
+ 'LINKS' => [
+ {
+ 'trans_id' => $form->{id},
+ 'trans_type' => $form->{db} eq 'customer' ? 'customer' : 'vendor',
+ 'trans_info' => $form->{name},
+ },
+ ],
+ );
+
+ $follow_up{id} = FU->save(%follow_up);
+
+ } elsif ($form->{FU_id}) {
+ do_query($form, $dbh, qq|DELETE FROM follow_up_links WHERE follow_up_id = ?|, conv_i($form->{FU_id}));
+ do_query($form, $dbh, qq|DELETE FROM follow_ups WHERE id = ?|, conv_i($form->{FU_id}));
+ }
+
+ delete @{$form}{grep { /^NOTE_|^FU_/ } keys %{ $form }};
+
+ $main::lxdebug->leave_sub();
+}
+
+sub _delete_selected_notes {
+ $main::lxdebug->enter_sub();
+
+ my $self = shift;
+ my %params = @_;
+
+ Common::check_params(\%params, 'dbh');
+
+ my $form = $main::form;
+ my $dbh = $params{dbh};
+
+ foreach my $i (1 .. $form->{NOTES_rowcount}) {
+ next unless ($form->{"NOTE_delete_$i"} && $form->{"NOTE_id_$i"});
+
+ Notes->delete('dbh' => $params{dbh},
+ 'id' => $form->{"NOTE_id_$i"});
+ }
+
+ $main::lxdebug->leave_sub();
+}
+
+# TODO: remove in 2.7.0 stable
+sub delete_shipto {
+ $main::lxdebug->enter_sub();
+
+ my $self = shift;
+ my $shipto_id = shift;
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $dbh = $form->get_standard_dbh(\%myconfig);
+
+ do_query($form, $dbh, qq|UPDATE shipto SET trans_id = NULL WHERE shipto_id = ?|, $shipto_id);
+
+ $dbh->commit();
+
+ $main::lxdebug->leave_sub();
+}
+
+# TODO: remove in 2.7.0 stable
+sub delete_contact {
+ $main::lxdebug->enter_sub();
+
+ my $self = shift;
+ my $cp_id = shift;
+
+ my $form = $main::form;
+ my %myconfig = %main::myconfig;
+ my $dbh = $form->get_standard_dbh(\%myconfig);
+
+ do_query($form, $dbh, qq|UPDATE contacts SET cp_cv_id = NULL WHERE cp_id = ?|, $cp_id);
+
+ $dbh->commit();
+
+ $main::lxdebug->leave_sub();
+}
+
+sub get_bank_info {
+ $main::lxdebug->enter_sub();
+
+ my $self = shift;
+ my %params = @_;
+
+ Common::check_params(\%params, qw(vc id));
+
+ my $myconfig = \%main::myconfig;
+ my $form = $main::form;
+
+ my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
+
+ my $table = $params{vc} eq 'customer' ? 'customer' : 'vendor';
+ my @ids = ref $params{id} eq 'ARRAY' ? @{ $params{id} } : ($params{id});
+ my $placeholders = join ", ", ('?') x scalar @ids;
+ my $query = qq|SELECT id, name, account_number, bank, bank_code, iban, bic
+ FROM ${table}
+ WHERE id IN (${placeholders})|;
+
+ my $result = selectall_hashref_query($form, $dbh, $query, map { conv_i($_) } @ids);
+
+ if (ref $params{id} eq 'ARRAY') {
+ $result = { map { $_->{id} => $_ } @{ $result } };
+ } else {
+ $result = $result->[0] || { 'id' => $params{id} };
+ }
+
+ $main::lxdebug->leave_sub();
+
+ return $result;
+}
+
+sub parse_excel_file {
+ $main::lxdebug->enter_sub();
+
+ my ($self, $myconfig, $form) = @_;
+ my $locale = $main::locale;
+
+ $form->{formname} = 'sales_quotation';
+ $form->{type} = 'sales_quotation';
+ $form->{format} = 'excel';
+ $form->{media} = 'screen';
+ $form->{quonumber} = 1;
+
+
+ # $form->{"notes"} will be overridden by the customer's/vendor's "notes" field. So save it here.
+ $form->{ $form->{"formname"} . "notes" } = $form->{"notes"};
+
+ my $inv = "quo";
+ my $due = "req";
+ $form->{"${inv}date"} = $form->{transdate};
+ $form->{label} = $locale->text('Quotation');
+ my $numberfld = "sqnumber";
+ my $order = 1;
+
+ # assign number
+ $form->{what_done} = $form->{formname};
+
+ map({ delete($form->{$_}); } grep(/^cp_/, keys(%{ $form })));
+
+ my $output_dateformat = $myconfig->{"dateformat"};
+ my $output_numberformat = $myconfig->{"numberformat"};
+ my $output_longdates = 1;
+
+ # map login user variables
+ map { $form->{"login_$_"} = $myconfig->{$_} } ("name", "email", "fax", "tel", "company");
+
+ # format item dates
+ for my $field (qw(transdate_oe deliverydate_oe)) {
+ map {
+ $form->{$field}[$_] = $locale->date($myconfig, $form->{$field}[$_], 1);
+ } 0 .. $#{ $form->{$field} };
+ }
+
+ if ($form->{shipto_id}) {
+ $form->get_shipto($myconfig);
+ }
+
+ $form->{notes} =~ s/^\s+//g;
+
+ $form->{templates} = $myconfig->{templates};
+
+ delete $form->{printer_command};
+
+ $form->get_employee_info($myconfig);
+
+ my ($cvar_date_fields, $cvar_number_fields) = CVar->get_field_format_list('module' => 'CT', 'prefix' => 'vc_');
+
+ if (scalar @{ $cvar_date_fields }) {
+ format_dates($output_dateformat, $output_longdates, @{ $cvar_date_fields });
+ }
+
+ while (my ($precision, $field_list) = each %{ $cvar_number_fields }) {
+ reformat_numbers($output_numberformat, $precision, @{ $field_list });
+ }
+
+ $form->{excel} = 1;
+ my $extension = 'xls';
+
+ $form->{IN} = "$form->{formname}.${extension}";
+
+ delete $form->{OUT};
+
+ $form->parse_template($myconfig);
+
+ $main::lxdebug->leave_sub();
+}
+
+sub search_contacts {
+ $::lxdebug->enter_sub;
+
+ my $self = shift;
+ my %params = @_;
+
+ my $dbh = $params{dbh} || $::form->get_standard_dbh;
+ my $vc = $params{db} eq 'customer' ? 'customer' : 'vendor';
+
+ my %sortspecs = (
+ 'cp_name' => 'cp_name, cp_givenname',
+ 'vcname' => 'vcname, cp_name, cp_givenname',
+ 'vcnumber' => 'vcnumber, cp_name, cp_givenname',
+ );
+
+ my %sortcols = map { $_ => 1 } qw(cp_name cp_givenname cp_phone1 cp_phone2 cp_mobile1 cp_email cp_street cp_zipcode cp_city vcname vcnumber);
+
+ my $order_by = $sortcols{$::form->{sort}} ? $::form->{sort} : 'cp_name';
+ $::form->{sort} = $order_by;
+ $order_by = $sortspecs{$order_by} if ($sortspecs{$order_by});
+
+ my $sortdir = $::form->{sortdir} ? 'ASC' : 'DESC';
+ $order_by =~ s/,/ ${sortdir},/g;
+ $order_by .= " $sortdir";
+
+ my @where_tokens = ();
+ my @values;
+
+ if ($params{search_term}) {
+ my @tokens;
+ push @tokens,
+ 'cp.cp_name ILIKE ?',
+ 'cp.cp_givenname ILIKE ?',
+ 'cp.cp_email ILIKE ?';
+ push @values, ('%' . $params{search_term} . '%') x 3;
+
+ if (($params{search_term} =~ m/\d/) && ($params{search_term} !~ m/[^\d \(\)+\-]/)) {
+ my $number = $params{search_term};
+ $number =~ s/[^\d]//g;
+ $number = join '[ /\(\)+\-]*', split(m//, $number);
+
+ push @tokens, map { "($_ ~ '$number')" } qw(cp_phone1 cp_phone2 cp_mobile1 cp_mobile2);
+ }
+
+ push @where_tokens, map { "($_)" } join ' OR ', @tokens;
+ }
+
+ my ($cvar_where, @cvar_values) = CVar->build_filter_query('module' => 'Contacts',
+ 'trans_id_field' => 'cp.cp_id',
+ 'filter' => $params{filter});
+
+ if ($cvar_where) {
+ push @where_tokens, $cvar_where;
+ push @values, @cvar_values;
+ }
+
+ if (my $filter = $params{filter}) {
+ for (qw(name title givenname email project abteilung)) {
+ next unless $filter->{"cp_$_"};
+ add_token(\@where_tokens, \@values, col => "cp.cp_$_", val => $filter->{"cp_$_"}, method => 'ILIKE', esc => 'substr');
+ }
+
+ push @where_tokens, 'cp.cp_cv_id IS NOT NULL' if $filter->{status} eq 'active';
+ push @where_tokens, 'cp.cp_cv_id IS NULL' if $filter->{status} eq 'orphaned';
+ }
+
+ my $where = @where_tokens ? 'WHERE ' . join ' AND ', @where_tokens : '';
+
+ my $query = qq|SELECT cp.*,
+ COALESCE(c.id, v.id) AS vcid,
+ COALESCE(c.name, v.name) AS vcname,
+ COALESCE(c.customernumber, v.vendornumber) AS vcnumber,
+ CASE WHEN c.name IS NULL THEN 'vendor' ELSE 'customer' END AS db
+ FROM contacts cp
+ LEFT JOIN customer c ON (cp.cp_cv_id = c.id)
+ LEFT JOIN vendor v ON (cp.cp_cv_id = v.id)
+ $where
+ ORDER BY $order_by|;
+
+ my $contacts = selectall_hashref_query($::form, $dbh, $query, @values);
+
+ $::lxdebug->leave_sub;
+
+ return @{ $contacts };
+}
+
+
+1;