-use SL::FU;
-use SL::Notes;
-
-sub get_tuple {
- $main::lxdebug->enter_sub();
-
- my ( $self, $myconfig, $form ) = @_;
-
- my $cv = $form->{db} eq "customer" ? "customer" : "vendor";
-
- my $dbh = $form->dbconnect($myconfig);
- my $query =
- qq|SELECT ct.*, b.id AS business, cp.* | .
- qq|FROM $cv ct | .
- qq|LEFT JOIN business b ON (ct.business_id = b.id) | .
- qq|LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id) | .
- qq|WHERE (ct.id = ?) | .
- qq|ORDER BY cp.cp_id LIMIT 1|;
- my $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
-
- my $ref = $sth->fetchrow_hashref(NAME_lc);
-
- map { $form->{$_} = $ref->{$_} } keys %$ref;
-
- $sth->finish;
- if ( $form->{salesman_id} ) {
- my $query =
- qq|SELECT ct.name AS salesman | .
- qq|FROM $cv ct | .
- qq|WHERE ct.id = ?|;
- ($form->{salesman}) =
- selectrow_query($form, $dbh, $query, $form->{salesman_id});
- }
-
- my ($employee_id) = selectrow_query($form, $dbh, qq|SELECT id FROM employee WHERE login = ?|, $form->{login});
- $query =
- qq|SELECT n.*, n.itime::DATE AS created_on,
- e.name AS created_by_name, e.login AS created_by_login
- FROM notes n
- LEFT JOIN employee e ON (n.created_by = e.id)
- WHERE (n.trans_id = ?) AND (n.trans_module = 'ct')|;
- $form->{NOTES} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
-
- $query =
- qq|SELECT fu.follow_up_date, fu.done AS follow_up_done, e.name AS created_for_name, e.name AS created_for_login
- FROM follow_ups fu
- LEFT JOIN employee e ON (fu.created_for_user = e.id)
- WHERE (fu.note_id = ?)
- AND NOT COALESCE(fu.done, FALSE)
- AND ( (fu.created_by = ?)
- OR (fu.created_by IN (SELECT DISTINCT what FROM follow_up_access WHERE who = ?)))|;
- $sth = prepare_query($form, $dbh, $query);
-
- foreach my $note (@{ $form->{NOTES} }) {
- do_statement($form, $sth, $query, conv_i($note->{id}), conv_i($note->{created_by}), conv_i($employee_id));
- $ref = $sth->fetchrow_hashref();
-
- map { $note->{$_} = $ref->{$_} } keys %{ $ref } if ($ref);
- }
-
- $sth->finish();
-
- if ($form->{edit_note_id}) {
- $query =
- qq|SELECT n.id AS NOTE_id, n.subject AS NOTE_subject, n.body AS NOTE_body,
- fu.id AS FU_id, fu.follow_up_date AS FU_date, fu.done AS FU_done, fu.created_for_user AS FU_created_for_user
- FROM notes n
- LEFT JOIN follow_ups fu ON ((n.id = fu.note_id) AND NOT COALESCE(fu.done, FALSE))
- WHERE n.id = ?|;
- $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{edit_note_id}));
-
- if ($ref) {
- foreach my $key (keys %{ $ref }) {
- my $new_key = $key;
- $new_key =~ s/^([^_]+)/\U\1\E/;
- $form->{$new_key} = $ref->{$key};
- }
- }
- }
-
- # check if it is orphaned
- my $arap = ( $form->{db} eq 'customer' ) ? "ar" : "ap";
- $query =
- qq|SELECT a.id | .
- qq|FROM $arap a | .
- qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
- qq|WHERE ct.id = ? | .
- qq|UNION | .
- qq|SELECT a.id | .
- qq|FROM oe a | .
- qq|JOIN $cv ct ON (a.${cv}_id = ct.id) | .
- qq|WHERE ct.id = ?|;
- my ($dummy) = selectrow_query($form, $dbh, $query, $form->{id}, $form->{id});
- $form->{status} = "orphaned" unless ($dummy);
-
- $dbh->disconnect;
-
- $main::lxdebug->leave_sub();
-}
-
-sub populate_drop_down_boxes {
- $main::lxdebug->enter_sub();
-
- my ($self, $myconfig, $form, $provided_dbh) = @_;
-
- my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect($myconfig);
-
- # get business types
- $query = qq|SELECT id, description FROM business ORDER BY id|;
- $form->{all_business} = selectall_hashref_query($form, $dbh, $query);
-
- # get shipto address
- $query =
- qq|SELECT shipto_id, shiptoname, shiptodepartment_1 | .
- qq|FROM shipto WHERE (trans_id = ?) AND (module = 'CT')|;
- $form->{SHIPTO} = selectall_hashref_query($form, $dbh, $query, $form->{id});
-
- # get contacts
- $query = qq|SELECT cp_id, cp_name, cp_givenname FROM contacts WHERE cp_cv_id = ? ORDER BY cp_name|;
- $form->{CONTACTS} = selectall_hashref_query($form, $dbh, $query, $form->{id});
-
- # get languages
- $query = qq|SELECT id, description FROM language ORDER BY id|;
- $form->{languages} = selectall_hashref_query($form, $dbh, $query);
-
- # get payment terms
- $query = qq|SELECT id, description FROM payment_terms ORDER BY sortkey|;
- $form->{payment_terms} = selectall_hashref_query($form, $dbh, $query);
-
- $dbh->disconnect() unless ($provided_dbh);