<?php

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

$formname = 'Sale GST';
$maindb = 'salemain';
$trandb = 'saletran';
$stkdb = 'transacn';
$accdb = 'actran';
$cashdb = 'cashrecp';
$bankdb = 'bankrecp';
$printfile = "salegst_print.php";
$file = 'salegst.php';
$trandbexist = 1;
$keyfld = "vouchno";
$source = "SL";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];

$stkdb1 = 'tran';
$zroot = $_SESSION["zdir"];
$combstkdb = $stkdb1 . $myyear;


$zroot1 = substr($zroot, 0, -4);

$currcono = $compcd;
$curryear = $myyear;

mysqli_select_db($conn, $zdb);
//include("includes/vouchno_generate.php");
//include("includes/prev&next.php");
browse($zdb);

$getbank = mysqli_query($conn, "SELECT * from `bankrecp` WHERE `vouchno`!=''");
browse(mysqli_num_rows($getbank));

mysqli_query($conn, "DELETE FROM `$accdb` WHERE  `source` = 'BR'") or die(mysqli_error($conn));
while ($fetbank = mysqli_fetch_array($getbank)) {

    $mbankvouch = $fetbank[vouchno];
    $vouchdt = $fetbank[vouchdt];
    $party = $fetbank[party];
    $bankname = $fetbank[bankname];
    $actamt = $fetbank[actamt];
    $mbrecpno = $fetbank[recpno];

    $getsale = mysqli_query($conn, "SELECT * FROM `salemain` WHERE `bankvouch` = '$mbankvouch'");
    $bsale = mysqli_fetch_array($getsale);
    $tamt = $bsale[ccamt];
    $prodtype = $bsale[prodtype];

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

    $minus_amt = ($tamt * -1);
    
    if ($prodtype == 'P') {
        $actamt = $tamt * 10;
        $actminus = $tamt * -10;
    } else {
        $actamt = $tamt;
        $actminus = $tamt * -1;
    }

    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount,trnflag,prodtype,actamt)
                    VALUES ('$vouchdt','$mbankvouch', '$mbrecpno', 'BR', '001', '$binit', '$bankname','002', '$minus_amt','S','$prodtype','$actminus')") or die(mysqli_error($conn));


    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount,trnflag,prodtype,actamt)
                    VALUES ('$vouchdt','$mbankvouch', '$mbrecpno', 'BR', '002', '$binit', '$party', '$pbgrp', '$tamt','S','$prodtype','$actamt')") or die(mysqli_error($conn));
}


echo "SU";
include("includes/linkshead.php");
?>

<?php include("includes/linksfoot.php"); ?>
