<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');
require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/config.php');

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "rowid") 
    $su_id = $paramValue;  
  else if ($paramName == "scopeid") 
    $pwo_id = $paramValue;  
}

$dal = new DAL();

$sql = "SELECT id FROM spatialunitlot WHERE spatialunit_id = $su_id ";
$result = $dal->query($sql)->id;

if (null == $result) {
  $sql = "DELETE FROM operationspatialunit " .
         "WHERE spatialunit_id = $su_id AND projectworkdayoperation_id = $pwo_id ";
  $result = $dal->query($sql); 

  $sql = "SELECT id FROM operationspatialunit WHERE spatialunit_id = $su_id";
  $result = $dal->query($sql)->id; 
  
  if (null == $result) {
  
    $sql = "DELETE FROM spatialunit WHERE id = $su_id ";
    $result = $dal->query($sql);       

    $sql = "DELETE FROM part WHERE spatialunit_id = $su_id ";
    $result = $dal->query($sql);
  }
}  
   
echo json_encode($result);

?>

