<?php

ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = "";
$action = "";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

$myyear = $_SESSION[$myyear];
$zroot = $_SESSION["zdbroot"];
$vouchno = $_REQUEST["vouchno"];
$invdate = $_REQUEST["invdate"];

$result = mysqli_query($conn, "SELECT * FROM `pax` WHERE vouchno='$vouchno'") or die(mysqli_error($conn));
$get = mysqli_fetch_array($result);

$hotelaprov = $get['hotelaprov'];
$transaprov = $get['transaprov'];
$foodaprov = $get['foodaprov'];
$tiktaprov = $get['tiktaprov'];
$visaaprov = $get['visaaprov'];
$laundaprov = $get['laundaprov'];

if ($foodaprov == 1) {
    $foodamt = $get['foodamt'];
} else {
    $foodamt = 0;
}
if ($laundaprov == 1) {
    $laundamt = $get['laundamt'];
} else {
    $laundamt = 0;
}
if ($visaaprov == 1) {
    $visaamt = $get['visaamt'];
} else {
    $visaamt = 0;
}
if ($transaprov == 1) {
    $transamt = $get['transamt'];
} else {
    $transamt = 0;
}
if ($tiktaprov == 1) {
    $tiktamt = $get['tiktamt'];
} else {
    $tiktamt = 0;
}
if ($hotelaprov == 1) {
    $hotelamt = $get['hotelamt'];
} else {
    $hotelamt = 0;
}

$netamt = $foodamt + $laundamt + $visaamt + $transamt + $tiktamt + $hotelamt;

if ($netamt > 0) {
    $salecode = 15;
    $minus_amt = $netamt * -1;
    mysqli_query($conn, "DELETE FROM `actran` WHERE `vouchno` = '$vouchno' AND `source`='SL'") OR die(mysqli_error($conn));

    // sale part
    mysqli_query($conn, "INSERT INTO `actran`(date, vouchno, source, trnno, accode, grpcode, amount)
                    VALUES ('$invdate','$vouchno', 'SL', '001', '$salecode','005', '$netamt')") OR die(mysqli_error($conn));
// party part
    mysqli_query($conn, "INSERT INTO `actran`(date, vouchno, source, trnno, accode, grpcode, amount)
                     VALUES ('$invdate','$vouchno', 'SL', '002', '$get[partycode]', '010', '$minus_amt')") or die(mysqli_error($conn));
}

$gen1 = mysqli_query($conn, "SELECT MAX(invno) AS maxcode FROM `pax` LIMIT 0,1 ") or die(mysqli_error($conn));
if (mysqli_num_rows($gen1) > 0) {
    $vaccode = mysqli_fetch_assoc($gen1);
    $qinv = $vaccode["maxcode"] + 1;
} else {
    $qinv = '1';
}

mysqli_query($conn, "UPDATE `pax` SET `invno`='$qinv',invdate='$invdate' where vouchno = '$vouchno' ") or die(mysqli_error($conn));

echo $qinv;
?>