<?php

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

$zdb = $_SESSION["zdbyear_$dbname"];
mysqli_select_db($conn, $zdb);
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("H:i:sa");

$username = $_SESSION["username_" . $dbname];

if (isset($_REQUEST['buttonsk'])) {

    $savevalue = $_REQUEST['buttonsk'];
    $accode = $_REQUEST['accode'];
    $vouchdt = $_REQUEST['vouchdt'];
    $pairs = $_REQUEST['pairs'];
    $amount = $_REQUEST['amount'];
    $type1 = $_REQUEST['type1'];
    $type2 = $_REQUEST['type2'];
    $bankname = $_REQUEST['bankname'];
    $remark = $_REQUEST['remark'];

    if ($savevalue == "S") {
        $type = $type1;
    } else if ($savevalue == "SC") {
        $type = $type1;
    } else if ($savevalue == "PU") {
        $type = $type1;
    } else if ($savevalue == "PC") {
        $type = $type1;
    } else {
        $type = $type2;
    }

    // SALE //

    if ($savevalue == "S") {

        $maindbf = "sale";
        $cashdbf = "cashrecp";
        $accdb = "actran";
        $source = 'SL';
        $cshsource = 'CR';

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

        mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$qvouch' AND `source`='$source' ") or die(mysqli_error($conn));

        if ($type == "R") {
            $purcode = 15;
        } else {
            $purcode = 14;
        }
        if ($type == "C") {
            $bankname = 3;
        }

        $getbankinit = mysqli_query($conn, "SELECT grpcode, bankinit FROM `accmast` WHERE `accode` = '$bankname'") or die(mysqli_error($conn));
        $bankinit = mysqli_fetch_array($getbankinit);
        $binit = $bankinit['bankinit'];
        $bgrp = $bankinit['grpcode'];

        $minus_amt = ($amount * -1);

        // sale part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, bankinit, accode, grpcode, amount)
                                            VALUES ('$vouchdt', '$qvouch', '$source', '001', '$binit', '$purcode', '005', '$amount')") or die(mysqli_error($conn));

        // party part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                                            VALUES ('$vouchdt', '$qvouch', '$source', '002', '$accode', '010', '$minus_amt')") or die(mysqli_error($conn));

        if ($type != 'R') {

            $genc = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$cashdbf` LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($genc) > 0) {
                $cvouch = mysqli_fetch_assoc($genc);
                $cqvouch = $cvouch["maxcode"] + 1;
            } else {
                $cqvouch = '1';
            }


            mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$cqvouch' AND `source`='$cshsource' ") or die(mysqli_error($conn));

            // cash part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, bankinit, accode, grpcode, amount)
                                                VALUES ('$vouchdt', '$cqvouch', '$cshsource', '001', '$binit', '$bankname', '$bgrp', '$minus_amt')") or die(mysqli_error($conn));

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                                                VALUES ('$vouchdt', '$cqvouch', '$cshsource', '002', '$accode', '010', '$amount')") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `$cashdbf`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `party`, `type`, `amount`, trnflag)
                                           Values ('$cqvouch', '$vouchdt', '$bankname', '$binit', '$accode', '$type', '$amount', 'S')") or die(mysqli_error($conn));

        }
            mysqli_query($conn, "INSERT INTO `$maindbf`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `party`, `type`, `pairs`, `amount`, `remark`, cashvouch, `adduser`)
                                           VALUES ('$qvouch', '$vouchdt', '$bankname', '$binit', '$accode', '$type', '$pairs', '$amount', '$remark', '$cqvouch',  '$username')") or die(mysqli_error($conn));

        echo "$qvouch";
    }


    // RECEIPT //

    if ($savevalue == "R") {

        $maindbf = "cashrecp";
        $accdb = "actran";
        $source = 'CR';

        $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindbf` LIMIT 0,1 ") or die(mysqli_error($conn));

        if (mysqli_num_rows($gen1) > 0) {
            $vvouch = mysqli_fetch_assoc($gen1);
            $qvouch = $vvouch["maxcode"] + 1;
        } else {
            $qvouch = '1';
        }

        mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$qvouch' AND `source`='$source' ") or die(mysqli_error($conn));

        if ($type == 'U') {
            
        } else {
            $bankname = '3';
        }

        $getbankinit = mysqli_query($conn, "SELECT bankinit FROM `accmast` WHERE `accode` = '$bankname'") or die(mysqli_error($conn));
        $bankinit = mysqli_fetch_array($getbankinit);
        $binit = $bankinit['bankinit'];

        $minus_amt = ($amount * -1);

        // cash part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, bankinit, accode, grpcode, amount)
                                                VALUES ('$vouchdt', '$qvouch', '$source', '001', '$binit', '$bankname', '002', '$minus_amt')") or die(mysqli_error($conn));

        // party part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                                                VALUES ('$vouchdt', '$qvouch', '$source', '002', '$accode', '010', '$amount')") or die(mysqli_error($conn));

        mysqli_query($conn, "INSERT INTO `$maindbf`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `party`, `type`, `amount`, `remark1`)
                                           Values ('$qvouch', '$vouchdt', '$bankname', '$binit', '$accode', '$type', '$amount', '$remark')") or die(mysqli_error($conn));

        echo "$qvouch";
    }

    // SALE CLAIM //

    if ($savevalue == "SC") {

        $maindbf = "slclaim";
        $accdb = "actran";
        $source = 'SCL';
        $cashdbf = "cashpay";
        $cshsource = 'CP';

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

        mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$qvouch' AND `source`='$source' ") or die(mysqli_error($conn));

        if ($type == "R") {
            $purcode = 15;
        } else {
            $purcode = 14;
        }

        if ($type == "C") {
            $bankname = 3;
        }

        $getbankinit = mysqli_query($conn, "SELECT grpcode, bankinit FROM `accmast` WHERE `accode` = '$bankname'") or die(mysqli_error($conn));
        $bankinit = mysqli_fetch_array($getbankinit);
        $binit = $bankinit['bankinit'];
        $bgrp = $bankinit['grpcode'];

        $minus_amt = ($amount * -1);

        // sale part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, bankinit, accode, grpcode, amount)
                                            VALUES ('$vouchdt', '$qvouch', '$source', '001', '$binit', '$purcode', '005', '$minus_amt')") or die(mysqli_error($conn));

        // party part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                                            VALUES ('$vouchdt', '$qvouch', '$source', '002', '$accode', '010', '$amount')") or die(mysqli_error($conn));

        if ($type != 'R') {

            $genc = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$cashdbf` LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($genc) > 0) {
                $cvouch = mysqli_fetch_assoc($genc);
                $cqvouch = $cvouch["maxcode"] + 1;
            } else {
                $cqvouch = '1';
            }

            mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$cqvouch' AND `source`='$cshsource' ") or die(mysqli_error($conn));

            // cash part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, bankinit, accode, grpcode, amount)
                                                VALUES ('$vouchdt', '$cqvouch', '$cshsource', '001', '$binit', '$bankname', '$bgrp', '$amount')") or die(mysqli_error($conn));

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                                                VALUES ('$vouchdt', '$cqvouch', '$cshsource', '002', '$accode', '010', '$minus_amt')") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `$cashdbf`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `party`, `type`, `amount`, trnflag)
                                           Values ('$cqvouch', '$vouchdt', '$bankname', '$binit', '$accode', '$type', '$amount', 'S')") or die(mysqli_error($conn));
        }

         mysqli_query($conn, "INSERT INTO `$maindbf`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `party`, `type`, `amount`, `remark`, cashvouch, `adduser`)
                                           VALUES ('$qvouch', '$vouchdt', '$bankname', '$binit', '$accode', '$type', '$amount', '$remark', '$cqvouch', '$username')") or die(mysqli_error($conn));

        echo "$qvouch";
    }

    // PURCHASE //

    if ($savevalue == "PU") {

        $maindbf = "purchase";
        $cashdbf = "cashpay";
        $accdb = "actran";
        $source = 'PU';
        $cshsource = 'CP';
        if ($type == "C") {
            $bankname = 3;
        }
        $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindbf` LIMIT 0,1 ") or die(mysqli_error($conn));
        if (mysqli_num_rows($gen1) > 0) {
            $vvouch = mysqli_fetch_assoc($gen1);
            $qvouch = $vvouch["maxcode"] + 1;
        } else {
            $qvouch = '1';
        }

        mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$qvouch' AND `source`='$source' ") or die(mysqli_error($conn));

        if ($type == "R") {
            $purcode = '12';
        } else {
            $purcode = '11';
        }

        $getbankinit = mysqli_query($conn, "SELECT bankinit,grpcode FROM `accmast` WHERE `accode` = '$bankname'") or die(mysqli_error($conn));
        $bankinit = mysqli_fetch_array($getbankinit);
        $binit = $bankinit['bankinit'];
        $bgrp = $bankinit['grpcode'];

        $minus_amt = ($amount * -1);

        // sale part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, bankinit, accode, grpcode, amount)
                                         VALUES ('$vouchdt', '$qvouch', '$source', '001', '$binit', '$purcode', '004', '$minus_amt')") or die(mysqli_error($conn));

        // party part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                                         VALUES ('$vouchdt', '$qvouch', '$source', '002', '$accode', '009', '$amount')") or die(mysqli_error($conn));

        if ($type != 'R') {

            $genc = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$cashdbf` LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($genc) > 0) {
                $cvouch = mysqli_fetch_assoc($genc);
                $cqvouch = $cvouch["maxcode"] + 1;
            } else {
                $cqvouch = '1';
            }


            mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$cqvouch' AND `source`='$cshsource' ") or die(mysqli_error($conn));

            // cash part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, bankinit, accode, grpcode, amount)
                                                VALUES ('$vouchdt', '$cqvouch', '$cshsource', '001', '$binit', '$bankname', '$bgrp', '$amount')") or die(mysqli_error($conn));

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                                                VALUES ('$vouchdt', '$cqvouch', '$cshsource', '002', '$accode', '009', '$minus_amt')") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `$cashdbf`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `party`, `type`, `amount`, trnflag)
                                           Values ('$cqvouch', '$vouchdt', '$bankname', '$binit', '$accode', '$type', '$amount', 'S')") or die(mysqli_error($conn));
        }
        
        mysqli_query($conn, "INSERT INTO `$maindbf`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `party`, `type`, `pairs`, `amount`, `remark`, cashvouch,`adduser`)
                                        VALUES ('$qvouch', '$vouchdt', '$bankname', '$binit', '$accode', '$type', '$pairs', '$amount', '$remark', '$cqvouch', '$username')") or die(mysqli_error($conn));

        echo "$qvouch";
    }

    // PAYMENT //

    if ($savevalue == "PY") {

        $maindbf = "cashpay";
        $accdb = "actran";
        $source = 'CP';

        $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindbf` LIMIT 0,1 ") or die(mysqli_error($conn));

        if (mysqli_num_rows($gen1) > 0) {
            $vvouch = mysqli_fetch_assoc($gen1);
            $qvouch = $vvouch["maxcode"] + 1;
        } else {
            $qvouch = '1';
        }

        mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$qvouch' AND `source`='$source' ") or die(mysqli_error($conn));

        if ($type == 'U') {
            
        } else {
            $bankname = '3';
        }

        $getbankinit = mysqli_query($conn, "SELECT bankinit FROM `accmast` WHERE `accode` = '$bankname'") or die(mysqli_error($conn));
        $bankinit = mysqli_fetch_array($getbankinit);
        $binit = $bankinit['bankinit'];

        $minus_amt = ($amount * -1);

        // cash part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, bankinit, accode, grpcode, amount)
                                                VALUES ('$vouchdt', '$qvouch', '$source', '001', '$binit', '$bankname', '002', '$amount')") or die(mysqli_error($conn));

        // party part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                                                VALUES ('$vouchdt', '$qvouch', '$source', '002', '$accode', '010', '$minus_amt')") or die(mysqli_error($conn));

        mysqli_query($conn, "INSERT INTO `$maindbf`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `party`, `type`, `amount`, `remark1`)
                                           Values ('$qvouch', '$vouchdt', '$bankname', '$binit', '$accode', '$type', '$amount', '$remark')") or die(mysqli_error($conn));

        echo "$qvouch";
    }

    // PURCHASE CLAIM //

    if ($savevalue == "PC") {

        $maindbf = "puclaim";
        $cashdbf = "cashrecp";
        $accdb = "actran";
        $source = 'PCL';
        $cshsource = 'CR';
        if ($type == "C") {
            $bankname = 3;
        }

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

        mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$qvouch' AND `source`='$source' ") or die(mysqli_error($conn));

        if ($type == "R") {
            $purcode = 15;
        } else {
            $purcode = 14;
        }

        $getbankinit = mysqli_query($conn, "SELECT bankinit,grpcode FROM `accmast` WHERE `accode` = '$bankname'") or die(mysqli_error($conn));
        $bankinit = mysqli_fetch_array($getbankinit);
        $binit = $bankinit['bankinit'];
        $bgrp = $bankinit['grpcode'];

        $minus_amt = ($amount * -1);

        // sale part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, bankinit, accode, grpcode, amount)
                                            VALUES ('$vouchdt', '$qvouch', '$source', '001', '$binit', '$purcode', '005', '$amount')") or die(mysqli_error($conn));

        // party part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                                            VALUES ('$vouchdt', '$qvouch', '$source', '002', '$accode', '010', '$minus_amt')") or die(mysqli_error($conn));

       

        if ($type != 'R') {

            $genc = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$cashdbf` LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($genc) > 0) {
                $cvouch = mysqli_fetch_assoc($genc);
                $cqvouch = $cvouch["maxcode"] + 1;
            } else {
                $cqvouch = '1';
            }


            mysqli_query($conn, "DELETE FROM `$accdb` WHERE vouchno='$cqvouch' AND `source`='$cshsource' ") or die(mysqli_error($conn));

            // party part
            // cash part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, bankinit, accode, grpcode, amount)
                                                VALUES ('$vouchdt', '$cqvouch', '$cshsource', '001', '$binit', '$bankname', '$bgrp', '$minus_amt')") or die(mysqli_error($conn));

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                                                VALUES ('$vouchdt', '$cqvouch', '$cshsource', '002', '$accode', '009', '$amount')") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `$cashdbf`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `party`, `type`, `amount`, trnflag)
                                           Values ('$cqvouch', '$vouchdt', '$bankname', '$binit', '$accode', '$type', '$amount', 'S')") or die(mysqli_error($conn));
        }

         mysqli_query($conn, "INSERT INTO `$maindbf`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `party`, `type`, `amount`, `remark`, cashvouch,`adduser`)
                                           VALUES ('$qvouch', '$vouchdt', '$bankname', '$binit', '$accode', '$type', '$amount', '$remark', '$cqvouch','$username')") or die(mysqli_error($conn));

        echo "$qvouch";
    }
}
