<?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 = 'gstmain';
$accdb = 'actran';
//$trandb = 'saletran';
$myyear = $_SESSION[$myyear];

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

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'];

    $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'];
    $packgtperc = $get['packgtperc'];
    $packgtamt = $get['packgtamt'];
    $oms = $get['oms'];
    $gstamount = $totgst5 + $totgst12 + $totgst18 + $totgst28 + $packgtamt;

    if ($gstamount > 0) {

        // gst part

        if ($packgtamt > 0) {
            if ($oms != 1) {
                $halfgt = round(($packgtamt / 2), 2);
                if ($packgtperc == 5) {
                    $cgst5 = $cgst5 + $halfgt;
                }
                if ($packgtperc == 12) {
                    $cgst12 = $cgst12 + $halfgt;
                }
                if ($packgtperc == 18) {
                    $cgst18 = $cgst18 + $halfgt;
                }
                if ($packgtperc == 28) {
                    $cgst28 = $cgst28 + $halfgt;
                }
            } else {
                if ($packgtperc == 5) {
                    $igst5 = $igst5 + $packgtamt;
                }
                if ($packgtperc == 12) {
                    $igst12 = $igst12 + $packgtamt;
                }
                if ($packgtperc == 18) {
                    $igst18 = $igst18 + $packgtamt;
                }
                if ($packgtperc == 28) {
                    $igst28 = $igst28 + $packgtamt;
                }
            }
        }

        if ($oms != 1) {
            IF ($totgst5 > 0) {
                mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,source, trnno, accode, grpcode, amount)
                                           VALUES ('$vouchdt','$mvouch', '$source', '003', '5','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', '6','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', '8','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', '9','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', '16','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', '17','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', '19','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', '20','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', '7','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', '10','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', '18','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', '21','020', '$igst28')") or die(mysqli_error($conn));
            }
        }
    }
}
browse("Success");
?>
 