#!/usr/bin/perl
BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
print "Content-type: text/html\n\n";
require cfg ;
&today ;
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
use DBI;
use CGI::Carp qw(fatalsToBrowser);
use CGI;
@ARGV = split(/\\*\&/, $ENV{'QUERY_STRING'});
$action = $ARGV[0] ;
$selectfield = $ARGV[1] ;
$poc_or_recipient = $ARGV[2] ;
our $ucfirst_poc_or_recipient = ucfirst $poc_or_recipient ;
our $uc_poc_or_recipient = ($poc_or_recipient eq 'poc') ? uc $poc_or_recipient : $ucfirst_poc_or_recipient ;
our $uc_poc_or_recipient_name = ucfirst lc $poc_or_recipient . "_name" ;
our $contact_nr = ($poc_or_recipient eq 'poc') ? "_contact_nr" : "_nr" ;
our $uc_poc_or_recipient_contact_nr = ($poc_or_recipient eq 'poc') ? ucfirst lc $poc_or_recipient . $contact_nr : ucfirst lc $poc_or_recipient . $contact_nr ;
our $only_update_number = ($action eq 'add_number_only') ? 1 : 0 ;
# our $debug = 1 ;
our $table = "event_quotes_$poc_or_recipient" ;
our ($q) = CGI -> new() ;
$iaction = $q -> param('iaction') || $action ;
$iaction = "add" if $iaction eq 'add_number_only' ;
our $dialog = 1 ;
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------
# &db_switch_conn('aisa') ;
if ($iaction eq 'add') {
&common_min_add_screen ;
&screen1 ;
}
if ($iaction eq 'save') {
&common_min_load_params ;
&insert ;
&screen2 ;
}
&common_min_action;
exit ;
#------------------------------------------------------------------------------------------
sub insert {
&add_db_fields ; # $i{last_edited_by} = $userid ; $i{last_updated} = "$now_ccyy_mm_dd $now_hour:$now_min:$now_sec" ;
our $input_name_cnt_in_table = 0 ;
our $input_name_without_number_in_table = 0 ; ;
&validate ;
$ignore{sel_field} = 1 ;
$i{id} = &db_min_get_max($table,'id') ;
$ignore{id} = 0 ;
if ($input_name_cnt_in_table == 1 && $input_name_without_number_in_table) {
my %ii = %i ;
%i = () ;
$i{contact_nr} = $ii{contact_nr} ;
&db_min_upd($table,"`name`='$ii{name}'") ;
%i = %ii ;
} else {
&db_min_insert($table) ;
}
} #------------------------------------------------------------------------------------------
sub validate {
# called from common_min_action
my $exists = '' ;
use utf8;
my $orig_number = $i{contact_nr} ;
$i{name} = &common_shared_fix_str($i{name}) ;
$i{name} = &common_shared_fix_poc_name($i{name}) ;
$i{contact_nr} = &common_shared_fix_poc_numbers($i{contact_nr}) ;
if (length($i{name}) > 50) { $alert = &common_min_alert('danger',"YOUR $uc_poc_or_recipient NAME CONTAINS TOO MANY CHARACTERS!",'warning-sign') ; %col_name = () ; &common_min_add_screen ; &screen1 ; exit ; }
if (length($i{contact_nr}) > 50) { $alert = &common_min_alert('danger',"YOUR $uc_poc_or_recipient CONTACT NUMBER CONTAINS TOO MANY CHARACTERS!",'warning-sign') ; %col_name = () ; &common_min_add_screen ; &screen1 ; exit ; }
if ($orig_number && $i{contact_nr} == "") { $alert = &common_min_alert('danger',"YOUR $uc_poc_or_recipient CONTACT NUMBER IS TOO SHORT/INVALID NUMBER!",'warning-sign') ; %col_name = () ; &common_min_add_screen ; &screen1 ; exit ; }
&db_min_ro($table,'*',"name='$i{name}'",'','') ;
foreach my $id (keys %{$db{$table}}) {
if (lc $db{$table}{$id}{name} eq lc $i{name} && $db{$table}{$id}{contact_nr} eq $i{contact_nr}) {
$exists = 1 ;
last ;
}
if (lc $db{$table}{$id}{name} eq lc $i{name}) {
$input_name_cnt_in_table++ ;
$input_name_without_number_in_table = 1 if !$db{$table}{$id}{contact_nr} ;
}
}
if ($exists) {
$alert = &common_min_alert('danger',"'$i{name}' AS A $uc_poc_or_recipient NAME WITH '$i{contact_nr}' AS $uc_poc_or_recipient NUMBER ALREADY EXISTS!",'warning-sign') ;
%col_name = () ;
&common_min_add_screen;
&screen1;
}
} #------------------------------------------------------------------------------------------
sub add_screen {
$box_header_title = "Add $uc_poc_or_recipient" ;
$hidden_action = 'save' ;
$head_col_width = 2 ;
$trigger_jquery_raw .= qq~
if (parent.\$("#select$uc_poc_or_recipient_name").val() && !parent.\$("#select$uc_poc_or_recipient_contact_nr").val()) {
\$("input[name='name']").val(parent.\$("#select$uc_poc_or_recipient_name").val()) ;
}
~;
} #------------------------------------------------------------------------------------------
sub add_db_fields {
$ignore{iaction} = 1 ;
$ignore{id} = 1 ;
$hidden{sel_field} = 2 ;
$required{name} = 1 ;
# $required{contact_nr} = 4 ;
$required{contact_nr} = 10 ;
$input{name} = 1 ;
$input{contact_nr} = 1 ;
$sort_field{1} = 'name' ;
$sort_field{2} = 'contact_nr' ;
$db{$table}{''}{sel_field} = $selectfield if $selectfield ;
if ($only_update_number) {
$hidden{name} = 1 ;
}
} #------------------------------------------------------------------------------------------
sub thead {
&common_min_thead ;
} #-------------------------------------------------------------------------------
sub screen1 {
# $trigger_jquery_raw .= qq~
# \$('#btn-close-customer').click(function (e) {
# console.log("CLOSING") ;
# parent.BootstrapDialog.closeAll() ;
# });
# ~ ;
&common_min_dialog_load_screen ;
} #------------------------------------------------------------------------------------------
sub screen2 {
$trigger_jquery_raw = qq~
parent.\$("#select$uc_poc_or_recipient_name").append("").trigger("chosen:updated") ;
parent.\$("#select$uc_poc_or_recipient_name").val("$i{name}").trigger("chosen:updated") ;
parent.\$("#select$uc_poc_or_recipient_contact_nr").empty() ;
parent.\$("#select$uc_poc_or_recipient_contact_nr").append("").trigger("chosen:updated") ;
parent.\$("#select$uc_poc_or_recipient_contact_nr").append("") ;
parent.\$("#select$uc_poc_or_recipient_contact_nr").val("$i{contact_nr}").trigger("chosen:updated");
if (!parent.\$("#add_$poc_or_recipient$contact_nr").is(":hidden")) {
parent.\$("#add_$poc_or_recipient$contact_nr").hide() ;
}
parent.BootstrapDialog.closeAll() ;
~;
&common_min_dialog_save_screen ;
} #------------------------------------------------------------------------------------------
use today ;
use dialog ;
use common ;
use common_shared ;
1;