<?php

#echo str_replace("'","","6576'8767");
#exit;
  function prx($arr) {
        echo "<hr>";
        echo "<pre>";
        echo print_r($arr);
        echo "</pre>";
        echo "<hr>";
    }
    function CDatex($date){
        $date1=explode("-",$date);

        $tgl=$date1[0];
        if((int)$tgl<=9) {
                $tgl="0".$tgl;
        }

        $arr1["Jan"]="01";
        $arr1["Feb"]="02";
        $arr1["Mar"]="03";
        $arr1["Apr"]="04";
        $arr1["May"]="05";
        $arr1["Jun"]="06";
        $arr1["Jul"]="07";
        $arr1["Aug"]="08";
        $arr1["Sep"]="09";
        $arr1["Oct"]="10";
        $arr1["Nov"]="11";
        $arr1["Dec"]="12";

        $bln=$arr1[$date1[1]];
        if((int)($date1[2]<=9)) {
            $th="20";
        }
        else {
            $th="19";
        }
        $thn=$th.$date1[2];
        $datam=$thn."-".$bln."-".$tgl;
        return $datam;

    }

     function CDatex2($date){
        $date1=explode(" ",$date);

        $tgl=$date1[0];
//        if((int)$tgl<=9) {
//                $tgl="0".$tgl;
//        }

        $arr1["Jan"]="01";
        $arr1["Feb"]="02";
        $arr1["Mar"]="03";
        $arr1["Apr"]="04";
        $arr1["May"]="05";
        $arr1["Jun"]="06";
        $arr1["Jul"]="07";
        $arr1["Aug"]="08";
        $arr1["Sep"]="09";
        $arr1["Oct"]="10";
        $arr1["Nov"]="11";
        $arr1["Dec"]="12";

        $arr1["Januari"]="01";
        $arr1["Februari"]="02";
        $arr1["Maret"]="03";
        $arr1["April"]="04";
        $arr1["Mei"]="05";
        $arr1["Juni"]="06";
        $arr1["Juli"]="07";
        $arr1["Agustus"]="08";
        $arr1["September"]="09";
        $arr1["Septeber"]="09";
        
        $arr1["Oktober"]="10";
        $arr1["Nopember"]="11";
        $arr1["Desember"]="12";
        

        $bln=$arr1[$date1[1]];
//        if((int)($date1[2]<=9)) {
//            $th="20";
//        }
//        else {
//            $th="19";
//        }
        $thn=$th.$date1[2];
        $datam=$thn."-".$bln."-".$tgl;
        return $datam;

    }
    
        session_cache_expire(1440);
        session_start();
        session_name("insys");
        include "include/config.php";
        include "include/functions.php";
        include "class/UctVw.php";



        $ssql="select * from tlemployee_temp order by fnid ";

        $rs=mysql_query($ssql) or die($ssql);

        $ssql1=array();
         mysql_query("BEGIN");
        while($r=mysql_fetch_assoc($rs)) {
            $r['tgl_masuk']=CDatex($r['tgl_masuk']);
            $_REQUEST['fnstatus']="1";
            $fttrans_no=getAutoNumber("hrd008","tlemployee","ftemployee_code",CDateRev($r['tgl_masuk']));

//            $r['tgl_lahir']=CDatex($r['tgl_lahir']);
            #$r['tgl_masuk']=CDatex($r['tgl_masuk']);
            $arr=explode("-",$r['pendidikan']);
            $arr[0]=str_replace(" ","",$arr[0]);

            switch($arr[0]) {
                case "SMA":
                case "SMU":
                case "SMK":
                case "STM":
                    $m="SMA";
                    break;
                case "MTS":
                case "SMP":
                case "SLTP":
                    $m="SMP";
                   break;
                case "SD";
                    $m="SD";
                    break;
                case "S1":
                    $m="S1";
                    break;
                case "D3":
                    $m="D3";
                    break;
            }
            $r['pendidikan']=$m;
            if($r['status_nikah']=="L") {
                $r['status_nikah']="1";
            }
            else {
                $r['status_nikah']="0";
            }

            if($r['jenis_kelamin']=="Wanita") {
                $r['jenis_kelamin']="1";
            }
            else {
                $r['jenis_kelamin']="0";
            }

            $r['ftpayroll_type_code']="";

//             $r['ftdivision_code']="ASS";
            $r['ftdepartment_code']="D01";

            

           if($r['divisi']=="Manager Produksi") {
                $r['ftdivision_code']="MGP";
            }
            elseif($r['divisi']=="PPIC") {
                $r['ftdivision_code']="PPC";
            }
            elseif($r['divisi']=="Painting") {
                $r['ftdivision_code']="PTG";
            }
            elseif($r['divisi']=="sewing") {
                $r['ftdivision_code']="SEW";
            }
            elseif($r['divisi']=="Assembling") {
                $r['ftdivision_code']="ASS";
            }
            elseif($r['divisi']=="Injection") {
                $r['ftdivision_code']="INJ";
            }
            elseif($r['divisi']=="Purchasing") {
                $r['ftdivision_code']="PCH";
            }
              elseif($r['divisi']=="IC") {
                $r['ftdivision_code']="ICL";
            }
              elseif($r['divisi']=="Personalia") {
                $r['ftdivision_code']="HRD";
            }
              elseif($r['divisi']=="Finance") {
                $r['ftdivision_code']="FNC";
            }
              elseif($r['divisi']=="Marketing") {
                $r['ftdivision_code']="MKT";
            }
           
           
            if($r['jabatan']=="Adm.") {
                $r['ftposition_code']="J04";
            }
            elseif($r['jabatan']=="Receiving") {
                $r['ftposition_code']="J11";
            }
            elseif($r['jabatan']=="Operator") {
                $r['ftposition_code']="J05";
            }
             elseif($r['jabatan']=="Kenek") {
                $r['ftposition_code']="J05";
            }
             elseif($r['jabatan']=="Supir") {
                $r['ftposition_code']="J05";
            }
           elseif($r['jabatan']=="Leader") {
                $r['ftposition_code']="J03";
           }
           elseif($r['jabatan']=="OPERATOR") {
                $r['ftposition_code']="J05";
           }
           elseif($r['jabatan']=="Operator") {
                $r['ftposition_code']="J05";
           }
           elseif($r['jabatan']=="TEKNISI") {
                $r['ftposition_code']="J06";
           }
           elseif($r['jabatan']=="Teknisi") {
                $r['ftposition_code']="J06";
           }
           elseif($r['jabatan']=="Security") {
                $r['ftposition_code']="J12";
           }
           elseif($r['jabatan']=="Supir") {
                $r['ftposition_code']="J07";
           }
           elseif($r['jabatan']=="Manager Produksi") {
                $r['ftposition_code']="J06";
           }
           
            elseif($r['jabatan']=="Ka. Bag.") {
                $r['ftposition_code']="J01";
           }
            elseif($r['jabatan']=="ka. Bag") {
                $r['ftposition_code']="J01";
           }
             elseif($r['jabatan']=="Ass.ka.Bag.") {
                $r['ftposition_code']="J02";
           }
             elseif($r['jabatan']=="Ass.Ka.Bag") {
                $r['ftposition_code']="J02";
           }


             elseif($r['jabatan']=="QC") {
                $r['ftposition_code']="J06";
           }
         
           
           
          
           # echo   $r['ftposition_code']."<br>";
            #$r['ftposition_code']="";
            $r['fnstatus']="1";
            $r['fdappointed_date']="";
            $r['fdstart_contract_date']="";
            $r['fdend_contract_date']="";
            $r['ftpph21_code']="TK";
            $r['nama_ortu']=str_replace("'","",$r['nama_ortu']);
 
             $r['nama']=str_replace("'","",$r['nama']);
              $r['nama_panggilan']=str_replace("'","",$r['nama_panggilan']);

              $r['ftpayroll_type_code']="2";
            $r['ftshift_code']="S06";
            $r['fnpayment_status_code']="0";
            unset($arr);
            $arr=explode('*',$r['tempat_lahir']);
            $r['tempat_lahir']=$arr[0];
            unset($arr);
            $arr=explode('*',$r['tgl_lahir']);
           $r['tgl_lahir']=CDatex2($arr[1]);

                     
            
            //echo $tgl_lahir."|";
            $ssql="


                    insert into tlemployee
                    (
                     ftemployee_code, ftemployee_name, ftnickname, fiphoto, fdbirth_date, ftbirth_place,
                        ftreligion, fnsex, fttelephone, fthandphone, ftktp_no, ftaddress,
                        ftcity, ftzip_code, ftstate, ftcountry, fteducation_code, ftjurusan, ftphoto, fdstart_date,
                        fdleave_date, ftdivision_code,ftdepartment_code, ftposition_code ,fnstatus, fdappointed_date, fdstart_contract_date,fdend_contract_date, ftpph21_code,
                        fnmarried_status, ftspouse_name,ftparent_name, ftnotes, ftemergency_contact, ftemergency_affiliation, ftemergency_telephone,
                        ftpermanent_address, ftpermanent_city, ftpermanent_state, ftpermanent_zip_code, ftpermanent_telephone, ftpayroll_type_code,
                        fcbasic_salary, fcfood_money, fctransport_money, fcovertime_money, fcmaximum_loan, ftshift_code,
                        fnpayment_status_code, ftbank_name, ftbank_account, ftpayroll_notes,
                        ftcreated_userx
                    )

                    select '{$fttrans_no}' as ftemployee_code,'{$r['nama']}' as ftemployee_name,'{$r['nama_panggilan']}' as ftnickname,'' as  fiphoto,'{$r['tgl_lahir']}' as  fdbirth_date, '{$r['tempat_lahir']}' as ftbirth_place,
                        'ISLAM' AS ftreligion,'{$r['status_nikah']}' as  fnsex,'{$r['telp']}' AS fttelephone,'{$r['hp']}' AS fthandphone,'{$r['ktp']}' AS  ftktp_no,'{$r['alamat_sekarang']}' AS ftaddress,
                        '{$r['alamat_sekarang']}' AS ftcity,'' AS  ftzip_code,'' AS  ftstate, 'Indonesia' as ftcountry,'{$r['pendidikan']}' as  fteducation_code,'{$r['jurusan']}' as  ftjurusan,'' as  ftphoto, '{$r['tgl_masuk']}' as fdstart_date,
                        '' as fdleave_date, '{$r['ftdivision_code']}' as ftdivision_code,'{$r['ftdepartment_code']}' as ftdepartment_code, '{$r['ftposition_code']}' as ftposition_code ,'{$r['fnstatus']}' as fnstatus,'{$r['fdappointed_date']}' as  fdappointed_date,'{$r['fdstart_contract_date']}' as  fdstart_contract_date,'{$r['fdend_contract_date']}' as fdend_contract_date,'{$r['ftpph21_code']}' as  ftpph21_code,
                        '{$r['status_nikah']}' AS fnmarried_status,  '{$r['nama_pasangan']}'  AS ftspouse_name, '{$r['nama_ortu']}'  AS ftparent_name,'' AS  ftnotes,'' AS ftemergency_contact,'' AS ftemergency_affiliation,'' AS ftemergency_telephone,
                       '{$r['alamat_asal']}' AS ftpermanent_address, '' AS ftpermanent_city,'' AS ftpermanent_state, '' AS ftpermanent_zip_code,'' AS  ftpermanent_telephone,'{$r['ftpayroll_type_code']}' as  ftpayroll_type_code,
                        '' as fcbasic_salary,'' as fcfood_money,'' as fctransport_money,'' as  fcovertime_money, '' as fcmaximum_loan,'{$r['ftshift_code']}' as ftshift_code,
                        '{$r['fnpayment_status_code']}' as fnpayment_status_code,'' as ftbank_name, '' as ftbank_account, '' as ftpayroll_notes,
                        'xxx7' as ftcreated_user

                    ";
                mysql_query($ssql) or die($ssql);
                $ssql1[]=$ssql;

                $ssql="insert into tlemployee_salary_inc
                        (
                            ftemployee_code, fnindex, ftsalary_code, fcamount,fnsalary_period,fnpayroll_slip,
                            ftcreated_userx
                        )
                        SELECT '{$fttrans_no}' as  ftemployee_code, fnindex, ftsalary_code, fcamount,fnsalary_period,fnpayroll_slip,
                            'xxx7' as ftcreated_userx
                        FROM tlemployee_salary_inc_temp
                 ";
                 mysql_query($ssql) or die($ssql);
                $ssql1[]=$ssql;

                  $ssql="insert into tlemployee_salary_dec
                        (
                            ftemployee_code, fnindex, ftsalary_code, fcamount,fnsalary_period,fnpayroll_slip,
                            ftcreated_userx
                        )
                        SELECT '{$fttrans_no}' as  ftemployee_code, fnindex, ftsalary_code, fcamount,fnsalary_period,fnpayroll_slip,
                             'xxx7' as ftcreated_user
                        FROM tlemployee_salary_dec_temp
                 ";
                 mysql_query($ssql) or die($ssql);
                $ssql1[]=$ssql;


        }
        $ssql="update tlemployee set ftaddress=ftpermanent_address,fdstart_contract_date=date_add(fdstart_date,INTERVAL 1 MONTH),
            ftpermanent_address='' 
         where ftcreated_userx='xxx7' ";
         $ssql1[]=$ssql;
          mysql_query($ssql) or die($ssql);
          mysql_query("COMMIT");
          prx($ssql1);