SPIDEY has asked for the wisdom of the Perl Monks concerning the following question:
This program will call a sqr program inside it, change the output file to html, and send this via email to a recipient.
The code should be pretty well commented in respects to what it is doing. If there are any questions. feel free to
ask.
Thanks in advance
#!/usr/local/bin/perl # # Programmer: Chris Tantalo # # This program will call eleadsht.sqt and email the eleadsht.lis file # based on the data in the dmg.eleadsht_parameters table # use Mail::Sender; use DBI; # check to see if test machine(cad2) or production(sam) my $node = `hostname`; chomp $node; # establish a connection to the database # change first paramater to the database that you want to connect to read_config($node);
my $filename = ""; $filename = $hash{"LIS_FILE"} if($node eq "sam"); $filename = $hash{"LIS_FILE_TEST"} if($node eq "cad2"); chomp $filename; my $htmfilename = ""; $htmfilename = $hash{"HTM_FILE"} if($node eq "sam"); $htmfilename = $hash{"HTM_FILE_TEST"} if($node eq "cad2"); chomp $htmfilename; my $sqt_path = ""; $sqt_path = $hash{"SQT_PATH"} if($node eq "sam"); $sqt_path = $hash{"SQT_PATH_TEST"} if($node eq "cad2"); chomp $sqt_path; my $database_name = ""; $database_name = $hash{"DB_NAME"} if($node eq "sam"); $database_name = $hash{"DB_NAME_TEST"} if($node eq "cad2"); chop $database_name; my $mail_server_name = ""; $mail_server_name = $hash{"MAIL_SERVER"} if($node eq "sam"); $mail_server_name = $hash{"MAIL_SERVER_TEST"} if($node eq "cad2"); chomp $mail_server_name; $sender = new Mail::Sender {smtp => $mail_server_name}; my @row; my $dbh = DBI->connect("dbi:Oracle:" . $database_name,"","") or die "Cant connect to db: $DBI::errstr\n"; # statement handler to call sql statements my $sth = $dbh->prepare("SELECT ep_call_date,ep_prospect_id,ep_nsr,ep_ +sales_rep,ep_product,ep_leadsht_id,ep_status,ep_branch from dmg.elead +sht_parameters where ep_status in('I','R')") or die "Cant prepare SQL statement: $DBI::errstr\n"; # execute sth above $sth->execute() or die "Cant Execute SQL statement: $DBI::errstr\n"; # this is the loop to do stuff with all the returned data from # the select statement above, if there is any while(@row = $sth->fetchrow_array()){ if($row[6] eq "I") { my $errnow = 0; # run the sqrt report here, which takes a little bit.. # due to sqr runtime `sqrt $sqt_path / $row[0] $row[1] $row[2] $row[4] $row[3]`; error_msg("SR","NG",$row[5],$row[3],"Eleadsht.lis not generated du +e to bad parameters for the lead.","No Email Address.") unless (-e $f +ilename); update_record("N",$row[0],$row[1],$row[2],$row[3],$row[4],$row[5], +$row[6]) unless (-e $filename); next unless (-e $filename); create_html($filename,$htmfilename); $errnow = 1 if($row[3] eq "UNKNOWN" || $row[3] eq "OPEN"); # find out which sales rep to email based on $row[3] error_msg("SR","OE",$row[5],$row[3],$row[3],"No Email Address.") i +f($errnow); # validate in lts table first my $invalid_sr = validate_sales_rep($row[3]); error_msg("SR","OE",$row[5],$row[3],"HR Person Id not set for this + Sales Rep. Could not get email address.","No Email Address.") if($i +nvalid_sr && !$errnow); $errnow = 1 if($invalid_sr && !$errnow); # call sales_rep_email_address() and return the email address my ($sr_email) = sales_rep_email_address($row[3]) unless($errnow); error_msg("SR","OE",$row[5],$row[3],"This person does not have an +email address in the HR system.","No Email Address.") if($sr_email eq + "" && !$errnow); $errnow = 1 if($sr_email eq "" && !$errnow); ##### email to sales rep person right here ##### my $sr_recip_id = error_msg("SR","OS",$row[5],$row[3],"Email Sent +.",$sr_email) unless($errnow); mail_it($htmfilename,$row[1],$sr_recip_id,$sr_email,$row[5],$node) + unless($errnow); # reset errnow here for dsa $errnow = 0; process_dsa_dsm(); } # end of if status is an I elsif ($row[6] eq "R") { my $resend_email_address = ""; # run the sqrt report here, which takes a little bit.. # due to sqr runtime `sqrt $sqt_path / $row[0] $row[1] $row[2] $row[4] $row[3]`; create_html($filename,$htmfilename); my $sth2 = $dbh->prepare("select erl_email_address from dmg.eleads +ht_resend_leadsheet where erl_ep_leadsht_id_fk = ?") or die "Cant prepare SQL statement: $DBI::errstr\n"; $sth2->bind_param(1,$row[5]); # execute sth2 above $sth2->execute() or die "Cant Execute SQL statement: $DBI::errstr\n"; while( $resend_email_address = $sth2->fetchrow_array) { ##### email to resend person right here ##### my $resent_recip_id = error_msg("RES","OS",$row[5],"Resent Leadsh +eet.","Email Sent.",$resend_email_address); mail_it($htmfilename,$row[1],$resent_recip_id,$resend_email_addres +s,$row[5],$node); } } # after emailing the lead sheet, update the record in the database update_record("G",$row[0],$row[1],$row[2],$row[3],$row[4],$row[5], +$row[6]); unlink($htmfilename); unlink($filename); } warn "Data fetching terminated by early error: $DBI::errstr\n" if $DBU::err; # disconnect from the database $dbh->disconnect() or warn "error disconnecting: $DBI::errstr\n"; # exits the program exit; sub check_inclusion_table { my $dsm_person_id = shift @_; my $table_id = ""; my $found = 0; my $sth = $dbh->prepare("SELECT eedi_person_id from dmg.eleadsht_e +mail_dsm_inclusions ") or die "Cant prepare SQL statement: $DBI::errstr\n"; $sth->execute() or die "Cant Execute SQL statement: $DBI::errstr\n"; while ($table_id = $sth->fetchrow_array) { $found = 1 if($table_id == $dsm_person_id); } return $found; } sub validate_sales_rep { my $sr = shift @_; # find sales_rep ##my $quoted_sr = $dbh->quote($sr); my $sth = $dbh->prepare("SELECT sri_person_id from lts.sales_rep_i +nformation where sri_sales_rep = ?") or die "Cant prepare SQL statement: $DBI::errstr\n"; $sth->bind_param(1,$sr); $sth->execute() or die "Cant Execute SQL statement: $DBI::errstr\n"; my $sr_id = $sth->fetchrow_array(); # return the code for being valid or not return 1 if($sr_id eq ""); return 0; } sub error_msg { # get parameters being passed in my $recipient_type = shift @_; my $status_code = shift @_; my $leadsht_id = shift @_; my $recipient_name = shift @_; my $err_msg = shift @_; my $email_address = shift @_; my $quoted_recipient_name = $dbh->quote($recipient_name); my $quoted_recipient_type = $dbh->quote($recipient_type); my $quoted_status = $dbh->quote($status_code); my $quoted_err_msg = $dbh->quote($err_msg); my $quoted_email_address = $dbh->quote($email_address); my $sth = $dbh->prepare("SELECT dmg.eleadsht_recipient_id.nextval +from sys.dual") or die "Cant prepare SQL statement: $DBI::errstr\n"; $sth->execute() or die "Cant Execute SQL statement: $DBI::errstr\n"; my $recipient_id = $sth->fetchrow_array(); my $date = `date`; chomp $date; my ($day,$month,$numday,$fulltime,$zone,$year) = split(/ +/,$d +ate); $numday = "0" . $numday if($numday <10); my $fulldate = $numday . "-".uc($month)."-".$year; my $quoted_date = "to_date('$fulldate','DD-MON-YYYY')"; my ($hour,$min,$sec) = split(/:/,$fulltime); $hour = "0" . $hour if($hour <10); $fulltime = $hour . $min; $dbh->do("insert into dmg.eleadsht_email_log (eel_leadsht_recipient_id,eel_ep_leadsht_id_fk, eel_recipient_name,eel_recipient_type,eel_status, eel_error_message, eel_email_address,eel_process_date, eel_process_time) VALUES ($recipient_id, $leadsht_id,$quoted_recipient_name, $quoted_recipient_type, $quoted_status,$quoted_err_msg, $quoted_email_address,$quoted_date,$fulltime)" ); # return the recipient id, in case it is a good email, so we can u +se # this number for the email, to track who it goes to. return $recipient_id; } sub sales_rep_email_address { # get parameter being passed in my $sales_rep = shift @_; # find sales_reps email address my $sth = $dbh->prepare("SELECT pv_email_address from lts.sales_re +p_information,dmg.papf_view where sri_sales_rep = ? and pv_person_id += sri_person_id") or die "Cant prepare SQL statement: $DBI::errstr\n"; $sth->bind_param(1,$sales_rep); $sth->execute() or die "Cant Execute SQL statement: $DBI::errstr\n"; my $srep_email_address = $sth->fetchrow_array(); # return the sales rep email address return $srep_email_address; } sub mail_it { # name of file to send, should be eleadsht.lis my $filename = shift @_; # prospect_id my $prosp_id = shift @_; # recipient_id my $rec_id = shift @_; # reciever of mail message my $emailto = shift @_; # leadsheet id my $leadsht_id = shift @_; # node my $node = shift @_; my $company_name = get_company_name($prosp_id); # subject my $subject = "Lead Sheet ID:$leadsht_id Company: $company_name Pr +os_id:$prosp_id Rec_id:$rec_id"; my @msg = ""; # open the file to send open(MSG,$filename); push(@msg,$_) while(<MSG>); close(MSG); my $from = ""; $from = "nssmail\@sam.XYZ.com" if($node eq "sam"); $from = "nssmail\@cad2.XYZ.com" if($node eq "cad2"); $sender->Open({ from => $from, to => $emailto, subject => $subject +, headers => "MIME-Version: 1.0\r\nContent-type: text/html\r\nContent +-Transfer-Encoding: 7bit" }) or die $Mail::Sender::Error,"\n"; $sender->Send(@msg); $sender->Close(); } sub find_branch { # get arguments being passed in my $prosid = shift @_; my $prod_code = shift @_; my $sth = $dbh->prepare("SELECT ppi_branch from dmg.prospect_produ +ct_info where ppi_prospect_id = ? and ppi_product = ? ") or die "Cant prepare SQL statement: $DBI::errstr\n"; $sth->bind_param(1,$prosid); $sth->bind_param(2,$prod_code); $sth->execute() or die "Cant Execute SQL statement: $DBI::errstr\n"; my $branchnum = $sth->fetchrow_array(); # if branchnum is empty, check dmg.prospect table if($branchnum eq "") { # search for branch again in dmg.prospect table. my $sth2 = $dbh->prepare("SELECT pro_pr_branch from dmg.prospe +ct where pro_prospect_id = ?") or die "Cant prepare SQL statement: $DBI::errstr\n"; $sth2->bind_param(1,$prosid); $sth2->execute() or die "Cant Execute SQL statement: $DBI::errstr\n"; $branchnum = $sth2->fetchrow_array(); } # convert to a number field to lose the '00' in front of the numbe +r # otherwise sql lookups based on this number will not work $branchnum = $branchnum + 0; return $branchnum; } sub update_record { my $new_status = shift @_; my $scr_date = shift @_; my $pros_id = shift @_; my $tsr_name = shift @_; my $sales_rep_name = shift @_; my $prod_code = shift @_; my $leadsht_id = shift @_; my $curr_status = shift @_; my $quoted_new_status = $dbh->quote($new_status); my $quoted_scr_date = $dbh->quote($scr_date); my $quoted_pros_id = $dbh->quote($pros_id); my $quoted_tsr_name = $dbh->quote($tsr_name); my $quoted_sales_rep_name = $dbh->quote($sales_rep_name); my $quoted_prod_code = $dbh->quote($prod_code); my $quoted_curr_status = $dbh->quote($curr_status); $dbh->do(" update dmg.eleadsht_parameters set ep_status = $quoted_new_status where ep_call_date = to_date($quoted_scr_date,'DD-MON-RR') and ep_prospect_id = $quoted_pros_id and ep_nsr = $quoted_tsr_name and ep_sales_rep = $quoted_sales_rep_name and ep_product = $quoted_prod_code and ep_leadsht_id = $leadsht_id and ep_status = $quoted_curr_status"); } sub get_company_name { my $prospect_id = shift @_; my $comp_name; my $sth = $dbh->prepare("SELECT pro_company from dmg.prospect wher +e pro_prospect_id = ? ") or die "Cant prepare SQL statement: $DBI::errstr\n"; $sth->bind_param(1,$prospect_id); $sth->execute() or die "Cant Execute SQL statement: $DBI::errstr\n"; $comp_name = $sth->fetchrow_array; return $comp_name; } sub read_config() { my $node = shift @_; my $cfg_file = ""; $cfg_file = "/opt/appl/hrstmk/bin/elead.cfg" if($node eq "sam"); $cfg_file = "/usr/appl/cad/prod/hrstmk/bin/elead.cfg" if($node eq +"cad2"); open(CFG_FILE,"$cfg_file") ||die "cannot open $cfg_file for readin +g:$!"; while(<CFG_FILE>) { next if($_ =~ /^#/); ($key,$value) = split(/=/,$_); $hash{$key}=$value; } close(CFG_FILE); } sub create_html { my $lisfilename = shift @_; my $htmfile = shift @_; my $line = ""; open(IN,"$lisfilename"); open(OUT,">$htmfile"); use HTML::FromText; $line = <IN>; while(<IN>){ $line .= $_; } close(IN); print OUT text2html($line, urls => 1, pre => 1); close(OUT); } sub process_dsa_dsm { # find branch number now... my $errnow = 0; my $no_branch = 0; my $branch_number = $row[7]; my @row2 = ""; my $found = 0; my $no_branch = 0; $no_branch = 1 unless($branch_number); error_msg("DSA","OE",$row[5],"DSA","No branch given for this lead, + could not send DSA email.","No Email Address.") if($no_branch); error_msg("DSM","OE",$row[5],"DSM","No branch given for this lead, + could not send DSM email.","No Email Address.") if($no_branch); # DSA here if(!$no_branch) { my $branch = $branch_number +0; my $quoted_dsa_dsm = $dbh->quote("DSA"); my $quoted_prod_code = $dbh->quote($row[4]); my $sth = $dbh->prepare("SELECT decode(pv_email_address,NULL,e +tea_email_address,pv_email_address) ,substr(fse_first_name||' '||fse_ +last_name,1,30), fse_person_id from dmg.field_sales_employees,dmg.fie +ld_sales_assignments, dmg.papf_view, dmg.product, dmg.eleadsht_temp_e +mail_addresses where fsa_off_nbr = $branch and prod_product = $quoted +_prod_code and fsa_fse_id_fk = fse_fse_id and fsa_sales_force = prod_ +product_group and fse_person_id = pv_person_id(+) and fsa_job_type = +$quoted_dsa_dsm and etea_id(+) = fse_fse_id") or die "Cant prepare SQL statement: $DBI::errstr\n"; $sth->execute() or die "Cant Execute SQL statement: $DBI::errstr\n"; while(@row2 = $sth->fetchrow_array()) { my $dsa_email = $row2[0]; my $dsa_name = $row2[1]; my $dsa_pid = $row2[2]; $found++; $errnow = 0; error_msg("DSA","OW",$row[5],$row2[1],"This DSA has not be +en assigned a person id in Field Sales Employee.","No Email Address." +) if($dsa_pid eq ""); error_msg("DSA","OE",$row[5],$dsa_name,"This DSA does not +have an email address in the HR system and there is no temporary addr +ess.","No Email Address.") if($dsa_email eq ""); $errnow = 1 if($dsa_email eq ""); $dsa_name = "No Name" if($dsa_name eq ""); ##### email to dsa person right here ##### my $dsa_recip_id = error_msg("DSA","OS",$row[5],$dsa_name +,"Email Sent.",$dsa_email) unless($errnow); mail_it($htmfilename,$row[1],$dsa_recip_id,$dsa_email,$row +[5],$node) unless($errnow); } # validate dsa exists for this branch error_msg("DSA","OE",$row[5],"Branch $branch_number DSA","No D +SA assigned to this branch in Field Sales Assignments.","No Email Add +ress.") unless($found); } if(!$no_branch) { $found = 0; my $branch2 = $branch_number +0; my $quoted_dsa_dsm2 = $dbh->quote("DSM"); my $quoted_prod_code2 = $dbh->quote($row[4]); @row2 = ""; my $sth2 = $dbh->prepare("SELECT decode(pv_email_address,NULL, +etea_email_address,pv_email_address) ,substr(fse_first_name||' '||fse +_last_name,1,30), fse_person_id from dmg.field_sales_employees,dmg.fi +eld_sales_assignments, dmg.papf_view, dmg.product, dmg.eleadsht_temp_ +email_addresses where fsa_off_nbr = $branch2 and prod_product = $quot +ed_prod_code2 and fsa_fse_id_fk = fse_fse_id and fsa_sales_force = pr +od_product_group and fse_person_id = pv_person_id(+) and fsa_job_type + = $quoted_dsa_dsm2 and etea_id(+) = fse_fse_id") or die "Cant prepare SQL statement: $DBI::errstr\n"; $sth2->execute() or die "Cant Execute SQL statement: $DBI::errstr\n"; while(@row2 = $sth2->fetchrow_array()) { my $dsm_email = $row2[0]; my $dsm_name = $row2[1]; my $dsm_pid = $row2[2]; $found++; $errnow = 0; error_msg("DSM","OW",$row[5],$row2[1],"This DSM has not be +en assigned a person id in Field Sales Employee.","No Email Address." +) if($dsm_pid eq ""); error_msg("DSM","OE",$row[5],$dsm_name,"This DSM does not +have an email address in the HR system and there is no temporary addr +ess.","No Email Address.") if($dsm_email eq ""); $errnow = 1 if($dsm_email eq ""); $dsm_name = "No Name" if($dsm_name eq ""); my $inc_check = 0; $inc_check = check_inclusion_table($dsm_pid) unless ($dsm_ +pid eq "" || $errnow); error_msg("DSM","NS",$row[5],$dsm_name,"This DSM was exclu +ded by NSS.",$dsm_email) if(!$inc_check && !$errnow); $errnow = 1 unless($inc_check); ##### email to dsm person right here ##### my $dsm_recip_id = error_msg("DSM","OS",$row[5],$dsm_name +,"Email Sent.",$dsm_email) unless($errnow); mail_it($htmfilename,$row[1],$dsm_recip_id,$dsm_email,$row +[5],$node) unless($errnow); } # validate dsm exists for this branch error_msg("DSM","OE",$row[5],"Branch $branch_number DSM","No D +SM assigned to this branch in Field Sales Assignments.","No Email Add +ress.") unless($found); } } #### end perl code here
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Review of first real code
by sauoq (Abbot) on Oct 22, 2002 at 19:46 UTC | |
|
Re: Review of first real code
by demerphq (Chancellor) on Oct 22, 2002 at 20:45 UTC | |
|
Re: Review of first real code
by graff (Chancellor) on Oct 23, 2002 at 02:52 UTC | |
|
Re: Review of first real code
by rdfield (Priest) on Oct 23, 2002 at 09:18 UTC | |
|
Re: Review of first real code
by Sifmole (Chaplain) on Oct 23, 2002 at 12:59 UTC | |
|
Re: Review of first real code
by SPIDEY (Initiate) on Oct 23, 2002 at 19:24 UTC |