#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
    
require cfg ;

use CGI::Carp qw(fatalsToBrowser);
use Date::Calc qw(:all);
# use DBI;

# https://itvadmin.co.za/cgi-bin/scripts/admin/oneoff/event_quotes_fix_db.pl?1

#-------------------------------------------------------------------------------

my  @ARGV     = split(/\&/, $ENV{'QUERY_STRING'});
our $debug    = $ARGV[0] ;
# our $username = 'rory' ;

if ($debug) { print "Content-type: text/html\n\n"; }

#------------------------------------------------------------------------------------------

&today ;

&load_hash ;

&db_open_upd ;
# &db_open_ro ;
    our $db_ignore_open_close = 1 ; # do one open and one close instead of repeating it
	&load_vars ;
	$db_ignore_open_close = 0 ; # do one open and one close instead of repeating it
&db_close_conn ;

if ($debug) { print "Done."; }

exit;

#------------------------------------------------------------------------------------------

sub load_vars {

	&db_min_ro('cities','*','','','') ;
	foreach my $_id (sort {$a <=> $b} keys %{$db{cities}}) {
		$city_id{lc $db{cities}{$_id}{city}} = $_id ;
	}
	
	&db_min_ro('event_quotes','id,city',"id",'','') ;
	
	foreach my $_id (sort {$a <=> $b} keys %{$db{event_quotes}}) {
		next unless $db{event_quotes}{$_id}{city} ;
		
		my $lc_city         = lc $db{event_quotes}{$_id}{city} ;
		   $lc_city         =~ s/^\s+|\s+$//g;
		
		$i{city_id}        = ($city_id{$lc_city}) ? $city_id{$lc_city} : 0   ;
		
		&db_min_upd(event_quotes,"id='$_id'") ;
		# &common_min_debug("UPDATE event_quotes SET city_id = '$i{city_id}' WHERE id = '$_id' [$lc_city]") ;
		
	}

} #-------------------------------------------------------------------------------

sub load_hash {

	$school_id_hash{"jeppe's high"}                               = 45 ;
	$school_id_hash{"jeppe"}                                      = 45 ;
	# $school_id_hash{"st stithians college"}                     = 56 ;
	$school_id_hash{"st  stithians"}                              = 56 ;
	$school_id_hash{"st stithians"}                               = 56 ;
	$school_id_hash{"st stithian's"}                              = 56 ;
	$school_id_hash{"st stithians + st andrew's"}                 = '56,53' ;
	$school_id_hash{"cornwall college"}                           = 61 ;
	$school_id_hash{"cornwall hill"}                              = 61 ;
	$school_id_hash{"cornwall"}                                   = 61 ;
	$school_id_hash{"cornwall hill college"}                      = 61 ;
	$school_id_hash{"st andrew's school for girls"}               = 53 ;
	$school_id_hash{"st andrew's college"}                        = 53 ;
	$school_id_hash{"st andrew's"}                                = 53 ;
	$school_id_hash{"st alban's college"}                         = 52 ;
	$school_id_hash{"st albans"}                                  = 52 ;
	$school_id_hash{"st albans college"}                          = 52 ;
	$school_id_hash{"st alban's rugby"}                           = 52 ;
	$school_id_hash{"parktown high"}                              = 47 ;
	$school_id_hash{"parktown"}                                   = 47 ;
	$school_id_hash{"st anne's college"}                          = 13 ;
	$school_id_hash{"kes"}                                        = 46 ;
	$school_id_hash{"kes + parktown"}                             = '46,47' ;
	$school_id_hash{"st joh's"}                                   = 54 ;
	$school_id_hash{"st john's"}                                  = 54 ;
	$school_id_hash{"st john's college"}                          = 54 ;
	$school_id_hash{"pretoria boys high school"}                  = 49 ;
	$school_id_hash{"clarendon all girls"}                        = 97 ;
	$school_id_hash{"clarendon school for girls"}                 = 97 ;
	$school_id_hash{"kimberley boys high"}                        = 37 ;
	$school_id_hash{"selbourne college"}                          = 108 ;
	$school_id_hash{"selborne"}                                   = 108 ;
	$school_id_hash{"dsg"}                                        = 100 ;
	$school_id_hash{"stellenbosch"}                               = 109 ;
	$school_id_hash{"riverside"}                                  = 12 ;
	$school_id_hash{"waterkloof hs"}                              = 58 ;
	$school_id_hash{"waterkloof + cornwall"}                      = '58,61' ;
	$school_id_hash{"hs alberton"}                                = 41 ;
	$school_id_hash{"st john's dsg"}                              = 15 ;
	$school_id_hash{"keps"}                                       = 154 ;
	$school_id_hash{"grey high"}                                  = 101 ;
	$school_id_hash{"grey hs"}                                    = 101 ;
	$school_id_hash{"grey + eunice"}                              = 101 ;
	$school_id_hash{"woodbridge"}                                 = 155 ;
	$school_id_hash{"saccs & wpcc"}                               = '107,111' ;
	$school_id_hash{"wpcc"}                                       = 111 ;
	$school_id_hash{"northwood high school"}                      = 10 ;
	$school_id_hash{"heron bridge"}                               = 38 ;
	$school_id_hash{"herronbridge"}                               = 38 ;
	$school_id_hash{"heronbridge college"}                        = 38 ;
	$school_id_hash{"danville"}                                   = 156 ;
	$school_id_hash{"christ church"}                              = 57 ;
	$school_id_hash{"christ church soccer"}                       = 57 ;
	$school_id_hash{"st davids"}                                  = 73 ;
	$school_id_hash{"st david's"}                                 = 73 ;
	$school_id_hash{"st dunstans"}                                = 158 ;
	$school_id_hash{"maties"}                                     = 109 ;
	$school_id_hash{"maties + bloemhof"}                          = '109,95' ;
	$school_id_hash{"hs bloemhof"}                                = 95 ;
	$school_id_hash{"bloemhof"}                                   = 95 ;
	$school_id_hash{"bloemhof + rhenish"}                         = '95,134' ;
	$school_id_hash{"paul roos gimnasium"}                        = 104 ;
	$school_id_hash{"alexandra"}                                  = 17 ;
	$school_id_hash{"alexander hs"}                               = 17 ;
	$school_id_hash{"alex high school"}                           = 17 ;
	$school_id_hash{"pbhs"}                                       = 102 ;
	$school_id_hash{"roedean"}                                    = 51 ;
	$school_id_hash{"roedean school"}                             = 51 ;
	$school_id_hash{"roedean senior high"}                        = 51 ;
	$school_id_hash{"roedean school (sa)"}                        = 51 ;
	$school_id_hash{"maritzburg college + pmbgh"}                 = '8,11' ;
	$school_id_hash{"eldoraigne"}                                 = 43 ;
	$school_id_hash{"hs waterkloof"}                              = 58 ;
	$school_id_hash{"st mary's waverly"}                          = 55 ;
	$school_id_hash{"st mary's waverley"}                         = 55 ;
	$school_id_hash{"pretoria girls"}                             = 70 ;
	$school_id_hash{"pretoria girls high school"}                 = 70 ;
	$school_id_hash{"hs oosterlig"}                               = 40 ;
	$school_id_hash{"pretoria high school for boys"}              = 49 ;
	$school_id_hash{"pretoria boys"}                              = 49 ;
	$school_id_hash{"rondebosch high school"}                     = 106 ;
	$school_id_hash{"rondebosh boys"}                             = 106 ;
	$school_id_hash{"rondebosh"}                                  = 106 ;
	$school_id_hash{"rondebosch"}                                 = 106 ;
	$school_id_hash{"rbhs"}                                       = 106 ;
	$school_id_hash{"glenwood"}                                   = 5 ;
	$school_id_hash{"dhs"}                                        = 4 ;
	$school_id_hash{"beaulieu college"}                           = 59 ;
	$school_id_hash{"st mary's"}                                  = 15 ;
	$school_id_hash{"st mary's kloof"}                            = 15 ;
	$school_id_hash{"rhenish"}                                    = 134 ;
	$school_id_hash{"rhenish hs"}                                 = 134 ;
	$school_id_hash{"klerksdorp"}                                 = 88 ;
	$school_id_hash{"hs menlopark"}                               = 44 ;
	$school_id_hash{"menlo park"}                                 = 44 ;
	$school_id_hash{"menlo + cornwall"}                           = '44,61' ;
	$school_id_hash{"menlo"}                                      = 44 ;
	$school_id_hash{"benoni hs"}                                  = 159 ;
	$school_id_hash{"kloof hs"}                                   = 161 ;
	$school_id_hash{"bosmansdam"}                                 = 112 ;
	$school_id_hash{"lyttleton high"}                             = 162 ;
	$school_id_hash{"hs transvalia"}                              = 75 ;
	$school_id_hash{"noordheuwel"}                                = 66 ;
	$school_id_hash{"westville"}                                  = 16 ;
	$school_id_hash{"westville hs"}                               = 16 ;
	$school_id_hash{"stelleberg"}                                 = 138 ;
	$school_id_hash{"stellenberg"}                                = 138 ;
	$school_id_hash{"reddam waterfall"}                           = 163 ;
	$school_id_hash{"monnas hs"}                                  = 42 ;
	$school_id_hash{"hs noordheuwel + hs monument"}               = '66,42' ;
	$school_id_hash{"noordheuwel + monnas"}                       = '66,42' ;
	$school_id_hash{"noordheuwel + monument"}                     = '66,42' ;
	$school_id_hash{"st martins"}                                 = 164 ;
	$school_id_hash{"worcester gym"}                              = 141 ;
	$school_id_hash{"st anne's"}                                  = 13 ;
	$school_id_hash{"paark boys high"}                            = 47 ;
	$school_id_hash{"kearsney + st mary's kloof"}                 = '7,15' ;
	$school_id_hash{"la rochelle + paarl girls + paarl boys"}     = '128,131,102' ;
	$school_id_hash{"pghs"}                                       = 131 ;
	$school_id_hash{"pmbgh"}                                      = 11 ;
	$school_id_hash{"curro thatchfield"}                          = 165 ;
	$school_id_hash{"dphs"}                                       = 2 ;
	$school_id_hash{"waterstone college"}                         = 166 ;
	$school_id_hash{"glenwood hs"}                                = 5 ;
	$school_id_hash{"hugenote hs"}                                = 167 ;
	$school_id_hash{"cloetesville"}                               = 168 ;
	$school_id_hash{"thomas moore"}                               = 170 ;
	$school_id_hash{"eg jansens"}                                 = 62 ;
	$school_id_hash{"hs hans moore"}                              = 63 ;
	$school_id_hash{"crawford la lucia"}                          = 172 ;
	$school_id_hash{"riverside + crawford la lucia"}              = '12,172' ;
	$school_id_hash{"riverside rugby"}                            = 12 ;
	$school_id_hash{"randburg astro"}                             = 71 ;
	$school_id_hash{"crawford north coast"}                       = 174 ;
	$school_id_hash{"panorama sports ground"}                     = 175 ;
	$school_id_hash{"uj"}                                         = 177 ;
	$school_id_hash{"tut + tuks"}                                 = '178,179' ;
	$school_id_hash{"tut + nwu"}                                  = '178,180' ;
	$school_id_hash{"hartleyvale + paarl"}                        = '181,102' ;
	$school_id_hash{"trinity house"}                              = 76 ;
	$school_id_hash{"garsfontein"}                                = 39 ;
	$school_id_hash{"crawford sandton"}                           = 182 ;
	$school_id_hash{"sol plaaitjie university"}                   = 185 ;
	$school_id_hash{"st charles"}                                 = 14 ;
	$school_id_hash{"saheti bedfordview"}                         = 187 ;
	$school_id_hash{"epworth"}                                    = 20 ;
	$school_id_hash{"sutherland"}                                 = 189 ;
	$school_id_hash{"alberton"}                                   = 41 ;
	
	$region_id{"cape town"}                                       = 9 ;
	$region_id{"langa"}                                           = 9 ;
	$region_id{"grahamstown"}                                     = 1 ;
	$region_id{"mitchells plein"}                                 = 9 ;
	$region_id{"durban"}                                          = 4 ;
	$region_id{"pretoria"}                                        = 3 ;
	$region_id{"johannesburg"}                                    = 3 ;
	$region_id{"bloemfontein"}                                    = 2 ;
	
	$sport_types_id_hash{'hockey & netball'}                      = '2,3' ;
	$sport_types_id_hash{'netball + hockey'}                      = '2,3' ;
	$sport_types_id_hash{'hockey + netball'}                      = '2,3' ;
	$sport_types_id_hash{'hockey + rugby'}                        = '1,2' ;
	$sport_types_id_hash{'rugby + hockey'}                        = '1,2' ;
	$sport_types_id_hash{'hockey & rugby'}                        = '1,2' ;
	$sport_types_id_hash{'rugby + netball'}                       = '1,3' ;
	$sport_types_id_hash{'netball + rugby'}                       = '1,3' ;
	$sport_types_id_hash{'football'}                              = 7 ;
	$sport_types_id_hash{'rugby + rugby'}                         = 1 ;
	$sport_types_id_hash{'hokcey'}                                = 2 ;
	$sport_types_id_hash{'waterpolo + basketball'}                = '4,5' ;
	$sport_types_id_hash{'basketball + waterpolo'}                = '4,5' ;
	$sport_types_id_hash{'hockey + football + waterpolo'}         = '2,7,4' ;
	$sport_types_id_hash{'football + netball'}                    = '7,3' ;

} #-------------------------------------------------------------------------------

# use db ;
use today ;
use common ;
use common_min ;
