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

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

$maindb = 'debrawmain';
$accdb = 'actran';
//$trandb = 'saletran';
$myyear = $_SESSION[$myyear];

$result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `vouchno`!= '' group by vouchno order by vouchno") or die(mysqli_error($conn));
//print table($result);
$getnum = mysqli_num_rows($result);
browse($getnum);
$source = "PRRW";

mysqli_query($conn, "DELETE FROM `$accdb` WHERE `source`='$source' and grpcode = '020' ") or die(mysqli_error($conn));

while ($get = mysqli_fetch_array($result)) {
    $mvouch = $get['vouchno'];
    $vouchdt = $get['vouchdt'];
    $credtype = $get['credtype'];

    $cgst5 = $get['cgst5'];
    $sgst5 = $get['sgst5'];
    $igst5 = $get['igst5'];

    $cgst12 = $get['cgst12'];
    $sgst12 = $get['sgst12'];
    $igst12 = $get['igst12'];

    $cgst18 = $get['cgst18'];
    $sgst18 = $get['sgst18'];
    $igst18 = $get['igst18'];

    $cgst28 = $get['cgst28'];
    $sgst28 = $get['sgst28'];
    $igst28 = $get['igst28'];

    $totgst5 = $get['totgst5'];
    $totgst12 = $get['totgst12'];
    $totgst18 = $get['totgst18'];
    $totgst28 = $get['totgst28'];

    $cgstperc = $get['cgstperc'];
    $igstperc = $get['igstperc'];
    
    $totcgst = $get['totcgst'];
    $totigst = $get['totigst'];

    $oms = $get['oms'];

    $cgst5code = 36;
    $sgst5code = 37;
    $igst5code = 38;

    $cgst12code = 39;
    $sgst12code = 40;
    $igst12code = 41;

    $cgst18code = 42;
    $sgst18code = 43;
    $igst18code = 44;

    $cgst28code = 45;
    $sgst28code = 46;
    $igst28code = 47;


    $gstamount = $totgst5 + $totgst12 + $totgst18 + $totgst28 + $totcgst + $totigst ;

    if ($gstamount > 0) {

        // gst part

        $cgst5min = ($cgst5 * -1);
        $igst5min = ($igst5 * -1);

        $cgst12min = ($cgst12 * -1);
        $igst12min = ($igst12 * -1);

        $cgst18min = ($cgst18 * -1);
        $igst18min = ($igst18 * -1);

        $cgst28min = ($cgst28 * -1);
        $igst28min = ($igst28 * -1);

        if ($credtype == 'O') {
            if ($oms != 1) {
                IF ($cgstperc == 2.5) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$cgst5code','020', '$totcgst')") or die(mysqli_error($conn));
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$sgst5code','020', '$totcgst')") or die(mysqli_error($conn));
                }
                IF ($cgstperc == 6) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$cgst12code','020', '$totcgst')") or die(mysqli_error($conn));
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$sgst12code','020', '$totcgst')") or die(mysqli_error($conn));
                }
                IF ($cgstperc == 9) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$cgst18code','020', '$totcgst')") or die(mysqli_error($conn));
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$sgst18code','020', '$totcgst')") or die(mysqli_error($conn));
                }
                IF ($cgstperc == 14) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$cgst28code','020', '$totcgst')") or die(mysqli_error($conn));
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$sgst28code','020', '$totcgst')") or die(mysqli_error($conn));
                }
            } else {
                IF ($igstperc == 5) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$igst5code','020', '$totigst')") or die(mysqli_error($conn));
                }
                IF ($igstperc == 12) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$igst12code','020', '$totigst')") or die(mysqli_error($conn));
                }
                IF ($igstperc == 18) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$igst18code','020', '$totigst')") or die(mysqli_error($conn));
                }
                IF ($igstperc == 28) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$igst28code','020', '$totigst')") or die(mysqli_error($conn));
                }
            }
        } else {
            if ($oms != 1) {
                IF ($totgst5 > 0) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$cgst5code','020', '$cgst5')") or die(mysqli_error($conn));
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$sgst5code','020', '$cgst5')") or die(mysqli_error($conn));
                }
                IF ($totgst12 > 0) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$cgst12code','020', '$cgst12')") or die(mysqli_error($conn));
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$sgst12code','020', '$cgst12')") or die(mysqli_error($conn));
                }
                IF ($totgst18 > 0) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$cgst18code','020', '$cgst18')") or die(mysqli_error($conn));
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$sgst18code','020', '$cgst18')") or die(mysqli_error($conn));
                }
                IF ($totgst28 > 0) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$cgst28code','020', '$cgst28')") or die(mysqli_error($conn));
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$sgst28code','020', '$cgst28')") or die(mysqli_error($conn));
                }
            } else {
                IF ($totgst5 > 0) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$igst5code','020', '$igst5')") or die(mysqli_error($conn));
                }
                IF ($totgst12 > 0) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$igst12code','020', '$igst12')") or die(mysqli_error($conn));
                }
                IF ($totgst18 > 0) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$igst18code','020', '$igst18')") or die(mysqli_error($conn));
                }
                IF ($totgst28 > 0) {
                    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '$igst28code','020', '$igst28')") or die(mysqli_error($conn));
                }
            }
        }
    }
}
browse("Success");
?>
 