+sub get_shipto {
+ $main::lxdebug->enter_sub();
+
+ my ( $self, $myconfig, $form ) = @_;
+ my $dbh = $form->dbconnect($myconfig);
+ my $query = qq|SELECT * FROM shipto WHERE shipto_id = ?|;
+ my $sth = prepare_execute_query($form, $dbh, $query, $form->{shipto_id});
+
+ my $ref = $sth->fetchrow_hashref(NAME_lc);
+
+ map { $form->{$_} = $ref->{$_} } keys %$ref;
+
+ $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 | .
+ 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 }};