<?php
ini_set ("display_errors", "0");
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
require("../connect.php");
include './funcConvertToSecond.php';
date_default_timezone_set('Asia/Jakarta');

if ($_GET['rkhtype'] == 'division') { 
  
  	// IF RKH DIVISION
    $sqlcheck  = " SELECT * from app_entity_72 where ";
    $sqlcheck .= " field_1165 = '".$_POST['company']."'";
    $sqlcheck .= " and field_1166 = '".$_POST['site']."'";
    $sqlcheck .= " and field_1167 = '".$_POST['estate']."'";
    $sqlcheck .= " and field_1161 = '".$_POST['divisioncode']."'";
    $sqlcheck .= " and field_1175 = '".$_POST['nodoc']."';";
    
    $checkresult = mysqli_query($conn, $sqlcheck);
    $rowcount=mysqli_num_rows($checkresult);
    
    if ($rowcount == 0) {
        
        $harvestDateDivRKH = strtotime($_POST['harvestdate']);
        $pointMapDivRKH = "[map]".$_POST['latrkh'].",".$_POST['longrkh']."(".$_POST['nodoc'].")[/map]";
        
        $queryInsertDivRKH = "INSERT INTO app_entity_72 (parent_item_id, 
        date_added, created_by, field_1174, field_1165, field_1166, field_1167, 
        field_1161, field_1162, field_1175, field_1163, field_1170, field_1256, field_1171, 
        field_1172, field_1176, field_1177, field_1178) VALUES (?, 
        UNIX_TIMESTAMP(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
        
        $stmtInsertDivRKH = mysqli_prepare($conn, $queryInsertDivRKH);
        mysqli_stmt_bind_param($stmtInsertDivRKH, 'iiissssissisiisss', 
        $_POST['divisionid'], $_POST['uploadby'], $_POST['addedby'], 
        $_POST['company'], $_POST['site'], $_POST['estate'], 
        $_POST['divisioncode'], $_POST['divisionid'], $_POST['nodoc'], 
        $harvestDateDivRKH, $_POST['harvesters'], $_POST['detailharvesters'], $_POST['totalunit'], 
        $_POST['totaltrip'], $_POST['latrkh'], $_POST['longrkh'], $pointMapDivRKH);
        
        if (mysqli_stmt_execute($stmtInsertDivRKH)) {
            echo json_encode(array(
            	"UPLOADRKH"=>"SUCCESS",
            	"RKHHEADERID" => mysqli_insert_id($conn),
            	"NODOCRKH" => $_POST['nodoc']
        	));
        	mysqli_query($conn, "INSERT INTO app_entity_72_values (items_id, fields_id, 
        	value) VALUES (".mysqli_insert_id($conn).", 1162, ".$_POST['divisionid'].");");
        	mysqli_close($conn);
        }
        
    } else if ($rowcount > 0) {
        echo json_encode(array("UPLOADRKH"=>"SUCCESS"));
        mysqli_close($conn);
    }
}

if ($_GET['rkhtype'] == 'block') { // IF RKH BLOK

    $harvestDateBlokRKH = strtotime($_POST['harvestdate']);
    $dotPercentAKP = str_replace(",", ".", $_POST['percentakp']);
    $dotHarvestWeight = str_replace(",", ".", $_POST['targetpanen']);

    $stmtSeksiPanen = mysqli_query($conn, "SELECT id FROM app_global_lists_choices 
    WHERE name = '".$_POST['seksipanen']."' AND lists_id = 10;");
    $rowSeksiPanen = mysqli_fetch_assoc($stmtSeksiPanen);
    
    $stmtDivisiRKH = mysqli_query($conn, "SELECT id FROM app_entity_72 WHERE field_1175 = '".$_POST['nodoc']."';");
    $rowDivisiRKH = mysqli_fetch_assoc($stmtDivisiRKH);
    
    $sqlcheck  = " SELECT * FROM app_entity_45 WHERE ";
    $sqlcheck .= " field_756 = '".$_POST['company']."'";
    $sqlcheck .= " and field_757 = '".$_POST['site']."'";
    $sqlcheck .= " and field_747 = '".$_POST['divisioncode']."'";
    $sqlcheck .= " and field_748 = '".$_POST['blockcode']."'";
    $sqlcheck .= " and field_1179 = '".$rowDivisiRKH['id']."';";
    
    $checkresult = mysqli_query($conn,$sqlcheck);
    $rowcount=mysqli_num_rows($checkresult);
    
    if ($rowcount == 0) {
        
        $queryInsertBlokRKH = "INSERT INTO app_entity_45 (parent_item_id, 
        date_added, created_by, field_858, field_1179, field_756, 
        field_757, field_747, field_749, field_750, field_748, 
        field_751, field_812, field_752, field_809, field_810, 
        field_811, field_813, field_1180, field_1181) 
        VALUES (?, UNIX_TIMESTAMP(), ?, ?, ?, ?, ?, ?, ?, ?, ?, 
        ?, ?, ?, ?, ?, ?, ?, ?, ?);";
        
        $stmtInsertBlokRKH = mysqli_prepare($conn, $queryInsertBlokRKH);
        mysqli_stmt_bind_param($stmtInsertBlokRKH, 'iiiisssiissississss', 
        $_POST['blockid'], $_POST['uploadby'], $_POST['addedby'], $rowDivisiRKH['id'], $_POST['company'], 
        $_POST['site'], $_POST['divisioncode'], $_POST['divisionid'], $_POST['blockid'], $_POST['blockcode'], 
        $harvestDateBlokRKH, $rowSeksiPanen['id'], $_POST['harvesthectare'], $_POST['pokoksampling'], $_POST['jjgmatang'], 
        $dotPercentAKP, $dotHarvestWeight, $_POST['latrkh'], $_POST['longrkh']);
        
        if (mysqli_stmt_execute($stmtInsertBlokRKH)) {
            echo json_encode(array("UPLOADRKH"=>"SUCCESS"));
            
            $lastInsertID = mysqli_insert_id($conn);
            
            mysqli_query($conn, "INSERT INTO app_entity_45_values (items_id, fields_id, 
        	value) VALUES (".$lastInsertID.", 749, ".$_POST['divisionid'].");");
        	mysqli_query($conn, "INSERT INTO app_entity_45_values (items_id, fields_id, 
        	value) VALUES (".$lastInsertID.", 750, ".$_POST['blockid'].");");
        }

    } else {
     	echo json_encode(array("UPLOADRKH"=>"SUCCESS"));
     	mysqli_close($conn);
    }
}

?>