$kid = $_GET["kid"];
if ($sortType == NULL || $sortType == '') {
    $sortType = 'id DESC';
}
if ($numberPage === NULL || $numberPage < 0) {
    echo "<script src='../JS/functions.js'></script><script>nextPage('set', '0')</script>";
}
//Getting all registries
if ($simpleKeyword !== NULL) {
    $defuntionRegistries = DefuntionManager::simpleSearchDefuntion($simpleKeyword, $sortType, $numberPage);
} else {
    if ($kid !== NULL) {
        $defuntionSearch = new Defuntion();
        $kcelebration = DatabaseManager::singleDateToDatabaseDate($_GET["kcelebration"]);
        $kbornp = $_GET["kbornp"];
        $kbornd = DatabaseManager::singleDateToDatabaseDate($_GET["kbornd"]);
        $knamec = $_GET["knamec"];
        $klastname1c = $_GET["klastname1c"];
        $klastname2c = $_GET["klastname2c"];
        $knamef = $_GET["knamef"];
        $kchurch = $_GET["kchurch"];
        $defuntionSearch->setId($kid);
        $defuntionSearch->setDeadDate($kcelebration);
        $posibleNames[0] = PersonManager::searchPersonsByNames($knamec, $klastname1c, $klastname2c, false);
        $defuntionSearch->setIdOwner($posibleNames);
        $posibleChurch = ChurchManager::simpleSearchChurch($kchurch, 'id', -1);
        $defuntionSearch->setIdChurch($posibleChurch);
        $defuntionRegistries = DefuntionManager::advancedSearchDefuntion($defuntionSearch, 'AND', $sortType, $numberPage);
    } else {
        $defuntionRegistries = DefuntionManager::getAllDefuntions($sortType, $numberPage);
    }
}
$girlfriend = new Person();
if ($_POST["idGirlfriend"] !== '0') {
    $girlfriend = PersonManager::getSinglePerson('id', $_POST["idGirlfriend"]);
}
$boyfriend->setId($_POST["idBoyfriend"]);
$boyfriend->setNames($_POST["boyfriendName"]);
$boyfriend->setLastname1($_POST["lastname1Boyfriend"]);
$boyfriend->setLastname2($_POST["lastname2Boyfriend"]);
$girlfriend->setId($_POST["idGirlfriend"]);
$girlfriend->setNames($_POST["girlfriendName"]);
$girlfriend->setLastname1($_POST["lastname1Girlfriend"]);
$girlfriend->setLastname2($_POST["lastname2Girlfriend"]);
$marriage = new Marriage();
$marriage->setId($_POST["idMarriage"]);
$celb = DatabaseManager::singleDateToDatabaseDate($_POST["celebrationDate"]);
$marriage->setCelebrationDate($celb);
$marriage->setIdChurchProcess($churchProcess->getId());
$marriage->setIdChurchMarriage($churchMarriage->getId());
$marriage->setIdRector($_POST["rectorId"]);
if ($boyfriend->getId() === '0') {
    PersonManager::addPerson($boyfriend, 'true');
    $boyfriend = PersonManager::getSinglePerson('id', PersonManager::getLastID());
} else {
    PersonManager::updatePerson($boyfriend);
}
if ($girlfriend->getId() === '0') {
    PersonManager::addPerson($girlfriend, 'true');
    $girlfriend = PersonManager::getSinglePerson('id', PersonManager::getLastID());
} else {
    PersonManager::updatePerson($girlfriend);
$sortType = $_GET["sort"];
$simpleKeyword = $_GET["keyword"];
$kid = $_GET["kid"];
if ($sortType == NULL || $sortType == '') {
    $sortType = 'id';
}
if ($numberPage === NULL || $numberPage < 0) {
    echo "<script src='../JS/functions.js'></script><script>nextPage('set', '0')</script>";
}
//Getting all registries
if ($simpleKeyword !== NULL) {
    $marriageRegistries = MarriageManager::simpleSearchMarriage($simpleKeyword, $sortType, $numberPage);
} else {
    if ($kid !== NULL) {
        $marriageSearch = new Marriage();
        $kcelebration = DatabaseManager::singleDateToDatabaseDate($_GET["kcelebration"]);
        $knameb = $_GET["knameb"];
        $klastname1b = $_GET["klastname1b"];
        $klastname2b = $_GET["klastname2b"];
        $knameg = $_GET["knameg"];
        $klastname1g = $_GET["klastname1g"];
        $klastname2g = $_GET["klastname2g"];
        $kchurchc = $_GET["kchurch"];
        $kbook = $_GET["kbook"];
        $knumber = $_GET["knumber"];
        $kpape = $_GET["kpape"];
        $marriageSearch->setId($kid);
        $marriageSearch->setCelebrationDate($kcelebration);
        $posibleNames[0] = PersonManager::searchPersonsByNames($knameb, $klastname1b, $klastname2b, false);
        $posibleNames[1] = PersonManager::searchPersonsByNames($knameg, $klastname1g, $klastname2g, false);
        $marriageSearch->setIdBoyfriend($posibleNames[0]);
                 $baptism->setIdCivilRegistry('1');
                 $celb = DatabaseManager::singleDateToDatabaseDate($_POST["baptismDate"]);
                 $baptism->setCelebrationDate($celb);
                 $church = ChurchManager::getSingleChurch('name', $_POST["baptismChurch"]);
                 if ($church !== NULL) {
                     $baptism->setIdChurch($church->getId());
                     $baptism->setIdRector(RectorManager::getSingleRector('idActualChurch', $church->getId())->getId());
                 }
                 $baptism->setIdOwner($child->getId());
                 $baptism->setIdBookRegistry($bookRegistry->getId());
                 BaptismManager::addBaptism($baptism);
             }
         } else {
             $baptism = BaptismManager::getSingleBaptism('id', $_POST["baptismId"]);
             if ($baptism !== NULL) {
                 $celb = DatabaseManager::singleDateToDatabaseDate($_POST["baptismDate"]);
                 $baptism->setCelebrationDate($celb);
                 $church = ChurchManager::getSingleChurch('name', $_POST["baptismChurch"]);
                 if ($church !== NULL) {
                     $baptism->setIdChurch($church->getId());
                 }
                 BaptismManager::updateBaptism($baptism);
                 $baptismReg = BaptismManager::getSingleBaptismRegistry('id', $baptism->getIdBookRegistry());
                 $baptismReg->setBook($_POST["bookBookRegistryB"]);
                 $baptismReg->setPage($_POST["pageBookRegistryB"]);
                 $baptismReg->setNumber($_POST["numBookRegistryB"]);
                 BaptismManager::updateBaptismRegistry($baptismReg);
             }
         }
     }
 }
if (!isset($_POST) || $_POST["idChild"] === NULL) {
    echo "KO";
    die;
}
$church = ChurchManager::getSingleChurch('name', $_POST["celebrationChurch"]);
$child = new Person();
if ($_POST["idChild"] !== '0') {
    $child = PersonManager::getSinglePerson('id', $_POST["idChild"]);
}
$child->setId($_POST["idChild"]);
$child->setNames($_POST["nameChild"]);
$child->setLastname1($_POST["lastname1Child"]);
$child->setLastname2($_POST["lastname2Child"]);
$defuntion = new Defuntion();
$defuntion->setId($_POST["idDefuntion"]);
$defuntion->setDeadDate(DatabaseManager::singleDateToDatabaseDate($_POST["celebrationDate"]));
$defuntion->setIdChurch($church->getId());
$defuntion->setIdOwner($child->getId());
//Get the defuntion Crypt
if ($_POST["idCrypt"] == '0') {
    if ($_POST["inCrypt"] == "true") {
        $myCrypt = new Crypt();
        $myCrypt->setIdNiche($church->getIdNiche());
        $myCrypt->setCol($_POST["cryptColumn"]);
        $myCrypt->setRow($_POST["cryptRow"]);
        $myCrypt->setNumber($_POST["cryptNumber"]);
        DefuntionManager::addCrypt($myCrypt);
        $singleCrypt = DefuntionManager::getSingleCrypt('col', $myCrypt->getCol(), 'row', $myCrypt->getRow(), 'idNiche', $myCrypt->getIdNiche(), 'number', $myCrypt->getNumber());
        $defuntion->setIdCrypt($singleCrypt->getId());
    } else {
        $defuntion->setIdCrypt(NULL);
}
$church = ChurchManager::getSingleChurch('name', $_POST["celebrationChurch"]);
$child = new Person();
if ($_POST["idChild"] !== '0') {
    $child = PersonManager::getSinglePerson('id', $_POST["idChild"]);
}
$child->setId($_POST["idChild"]);
$child->setNames($_POST["nameChild"]);
$child->setLastname1($_POST["lastname1Child"]);
$child->setLastname2($_POST["lastname2Child"]);
$child->setGender(substr($_POST["genderChild"], 0, 1));
$baptism = new Baptism();
$baptism->setId($_POST["idBaptism"]);
$celb = DatabaseManager::singleDateToDatabaseDate($_POST["celebrationDate"]);
$baptism->setCelebrationDate($celb);
$baptism->setBornDate(DatabaseManager::singleDateToDatabaseDate($_POST["bornDateChild"]));
$baptism->setBornPlace($_POST["bornPlaceChild"]);
$baptism->setIdChurch($church->getId());
$baptism->setIdRector($_POST["rectorId"]);
$legitimate = substr($_POST["legitimateChild"], 0, 1);
if ($legitimate === 'S' || $legitimate === 'Y') {
    $baptism->setLegitimate('Y');
} else {
    $baptism->setLegitimate('N');
}
//Data Process for the Father
$father = new Person();
if ($_POST["idFather"] !== '0') {
    $father = PersonManager::getSinglePerson('id', $_POST["idFather"]);
    $father->setId($_POST["idFather"]);
    $father->setNames($_POST["nameFather"]);