<?php 
set_time_limit(10000);
ini_set('memory_limit', '-1');
session_start(); 
require_once('../Connections/courier.php');
require_once("../assets/PHPExcel/Classes/PHPExcel.php");
$column = array(
    "A" => 1,
    "B" => 2,
    "C" => 3,
    "D" => 4,
    "E" => 5,
    "F" => 6,
    "G" => 7,
    "H" => 8,
    "I" => 9,
    "J" => 10,
    "K" => 11,
    "L" => 12,
    "M" => 13,
    "N" => 14,
    "O" => 15,
    "P" => 16,
    "Q" => 17,
    "R" => 18,
    "S" => 19,
    "T" => 20,
    "U" => 21,
    "V" => 22,
    "W" => 23,
    "X" => 24,
    "Y" => 25,
    "Z" => 26,
    "AA" => 27,
    "AB" => 28,
    "AC" => 29,
    "AD" => 30,
    "AE" => 31,
    "AF" => 32,
    "AG" => 33,
    "AH" => 34,
    "AI" => 35,
    "AJ" => 36,
    "AK" => 37,
    "AL" => 38,
    "AM" => 39,
    "AN" => 40,
    "AO" => 41,
    "AP" => 42,
    "AQ" => 43,
    "AR" => 44,
    "AS" => 45,
    "AT" => 46,
    "AU" => 47,
    "AV" => 48,
    "AW" => 49,
    "AX" => 50,
    "AY" => 51,
    "AZ" => 52
); 

$lowestRow = $_POST['startRow'];
$highestRow = $_POST['endRow'];
$sheet_no = $_POST['sheetNumber'];
$comp=$_POST['comp'];
$pplan=$_POST['pplan'];
$destination=$_POST['destination'];
$update_option = $_POST['update_option'];
$icount = 1;

$item_id = $column[strtoupper($_POST['item_id'])]-1;
$ins_item_code = $column[strtoupper($_POST['ins_item_code'])]-1;
$item = $column[strtoupper($_POST['item'])]-1;
$price = $column[strtoupper($_POST['price'])]-1;
$service = $column[strtoupper($_POST['service'])]-1;
$service_type = $column[strtoupper($_POST['service_type'])]-1;
$sub_service_type = $column[strtoupper($_POST['sub_service_type'])]-1;
$quantity_hand = $column[strtoupper($_POST['quantity_hand'])]-1;
$quantity_adabraka = $column[strtoupper($_POST['quantity_adabraka'])]-1;
$quantity_ksi = $column[strtoupper($_POST['quantity_ksi'])]-1;
$quantity_adenta = $column[strtoupper($_POST['quantity_adenta'])]-1;
$quantity_danquah = $column[strtoupper($_POST['quantity_danquah'])]-1;
$quantity_weija = $column[strtoupper($_POST['quantity_weija'])]-1;
$quantity_tema = $column[strtoupper($_POST['quantity_tema'])]-1;
$quantity_mobile_unit = $column[strtoupper($_POST['quantity_mobile_unit'])]-1;
$quantity_east_legon = $column[strtoupper($_POST['quantity_east_legon'])]-1;
$quantity_ahodwo = $column[strtoupper($_POST['quantity_ahodwo'])]-1;


 if (isset($_POST) && ($_SERVER['REQUEST_METHOD'] == "POST")) {
    $name = $_FILES['Eventimageupload']['name'];
    if (strlen($name)) {
        $tmp = $_FILES['Eventimageupload']['tmp_name'];
        $path_info = pathinfo($name);
        $ext = $path_info['extension'];
        $valid_formats = array("xlsx", "xls");
        if (in_array(strtolower($ext), $valid_formats)) {  


            $inputFileName = $tmp;
            $objPHPExcel = new PHPExcel();

            try {
                $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
                $objReader = PHPExcel_IOFactory::createReader($inputFileType);
                $objPHPExcel = $objReader->load($inputFileName);
            } catch (Exception $e) {
                die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
                    . '": ' . $e->getMessage());
            }

            $sheet = $objPHPExcel->getSheet($sheet_no-1);
            $highestColumn = $sheet->getHighestColumn();
            for ($row = $lowestRow; $row <= $highestRow; $row++) {     

                $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
                $item_id_import = empty($rowData[0][$item_id]) ? "" : $rowData[0][$item_id];
                $ins_item_code_import = empty($rowData[0][$ins_item_code]) ? "" : $rowData[0][$ins_item_code];
                $item_import = empty($rowData[0][$item]) ? "" : $rowData[0][$item];
                $service_import = empty($rowData[0][$service]) ? "" : $rowData[0][$service];
                $price_import = empty($rowData[0][$price]) ? "" : $rowData[0][$price];
                $service_type_import = empty($rowData[0][$service_type]) ? "" : $rowData[0][$service_type];
                $sub_service_type_import = empty($rowData[0][$sub_service_type]) ? "" : $rowData[0][$sub_service_type];
                $quantity_hand_import = empty($rowData[0][$quantity_hand]) ? "" : $rowData[0][$quantity_hand];
                $quantity_adabraka_import = empty($rowData[0][$quantity_adabraka]) ? "" : $rowData[0][$quantity_adabraka];
                $quantity_ksi_import = empty($rowData[0][$quantity_ksi]) ? "" : $rowData[0][$quantity_ksi];
                $quantity_adenta_import = empty($rowData[0][$quantity_adenta]) ? "" : $rowData[0][$quantity_adenta];
                $quantity_danquah_import = empty($rowData[0][$quantity_danquah]) ? "" : $rowData[0][$quantity_danquah];
                $quantity_weija_import = empty($rowData[0][$quantity_weija]) ? "" : $rowData[0][$quantity_weija];
                $quantity_tema_import = empty($rowData[0][$quantity_tema]) ? "" : $rowData[0][$quantity_tema];
                $quantity_mobile_unit_import = empty($rowData[0][$quantity_mobile_unit]) ? "" : $rowData[0][$quantity_mobile_unit];
                $quantity_east_legon_import = empty($rowData[0][$quantity_east_legon]) ? "" : $rowData[0][$quantity_east_legon];
                $quantity_ahodwo_import = empty($rowData[0][$quantity_ahodwo]) ? "" : $rowData[0][$quantity_ahodwo];



                if ($update_option == "insert_item"){
                    if ($destination == 'service_items') {
                        //$insert = "INSERT INTO `service_items`(`item`, `service_type`, `service`, `sub_service_type`, `quantity_onhand`, `quantity_available_adabraka`, `quantity_available_kumasi`, `quantity_available_adenta`, `quantity_available_danquah`, `quantity_available_weija`, `quantity_available_tema`, `quantity_available_mobile_unit`, `quantity_available_east_legon`, `quantity_available_ahodwo`) VALUES ('$item_import', '$service_type_import', '$service_import', '$sub_service_type_import', '$quantity_hand_import', '$quantity_adabraka_import', '$quantity_ksi_import', '$quantity_adenta_import', '$quantity_danquah_import', '$quantity_weija_import', '$quantity_tema_import', '$quantity_mobile_unit_import', '$quantity_east_legon_import', '$quantity_ahodwo_import')";
                    }else{
                        $insert = "INSERT INTO `stock_items`(`ins_item_code`,`item`,`price`,`service_type`, `service`, `quantity_available`,`status`) VALUES ('$ins_item_code_import','$item_import', '$price_import' ,'$service_type_import', '$service_import', '$quantity_hand_import','Approve')";
                        $insertExecute = mysql_query($insert, $courier);

                        $Last_id = mysql_insert_id();
                        $update = mysql_query("UPDATE stock_items SET item_id = '$Last_id' WHERE id = '$Last_id'");


                        $insert1 = "INSERT INTO `service_items`(`id`,`ins_item_code`,`item`, `service_type`, `service`,`sub_service_type`) VALUES ('$Last_id','$ins_item_code_import','$item_import', '$service_type_import', '$service_import', '$service_type_import')";
                        mysql_query($insert1, $courier);

                    }
                }else if ($update_option == "update_item"){
                    echo $updateRecord = "UPDATE `stock_items` SET `ins_item_code` = '$ins_item_code_import',`item` = '$item_import',`price`='$price_import',`service_type` = '$service_type_import',`service`='$service_import',`quantity_available`='$quantity_hand_import' WHERE  id ='$item_id_import'";
                    $insertExecute = mysql_query($updateRecord, $courier);
                }

                
            }
            header("Location: http://localhost/optical/rns");
        } else {
            echo "failed";
        }
    } else {
        echo "Please select Excel File..!";
    }
}

