<?php

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

$maindb = 'salemain';
$trandb = 'saletran';
$accdb = 'actran';
$stkdb = 'transacn';
$printfile = "sale_ti_print.php";
$file = 'sale_ti.php';
$trandbexist = 1;
$source = "SL  ";
$keyfld = "vouchno";
$myyear = $_SESSION["tit_" . $dbname];

date_default_timezone_set('Asia/Kolkata');
$time = date('H:i:s');

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

//browse($username);
$formname = 'Sale';

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

mysqli_select_db($conn, $zdb);


mysqli_query($conn, "DELETE FROM `$accdb` WHERE  `source` = '$source' ") OR die(mysqli_error($conn));

$getmain = mysqli_query($conn, "Select * from `$maindb` order by vouchno") or die(mysqli_error($conn));
while ($fetmain = mysqli_fetch_array($getmain)) {
    $voucher = $fetmain["voucher"];
    $taxableamt = $fetmain["taxableamt"];
    $totbillamt = $fetmain["billamt"];
    $paidby = $fetmain["paidby"];
    $godown = $fetmain["godown"];
    $mvouch = $fetmain["vouchno"];
    $vouchdt = $fetmain["vouchdt"];
    $mono = $fetmain["mono"];
    $accode = $fetmain["accode"];
    $gstamount = $totbillamt - $taxableamt;

    switch ($paidby) {
        case "R";
            $purcode = '30';
            $party = $_POST["party"];

            $mpart = mysqli_query($conn, "SELECT accode, grpcode, name  FROM `accmast` WHERE accode='$party'") or die(mysqli_error($conn));
            $ipart = mysqli_fetch_array($mpart);
//            $accode = $ipart['accode'];
            $grpcode = $ipart['grpcode'];
            $partyname = $ipart['name'];
            break;

        case "C";
            $purcode = '29';
            $party = $_POST["party"];
            $mpart = mysqli_query($conn, "SELECT accode, grpcode, name FROM `accmast` WHERE accode='$party'") or die(mysqli_error($conn));
            $ipart = mysqli_fetch_array($mpart);
//            $accode = $ipart['accode'];
            $grpcode = $ipart['grpcode'];
            $partyname = $ipart['name'];
            break;
    }

    $minus_amt = ($totbillamt * -1);

    // sale part
    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,`godown`, source, trnno, accode, grpcode, amount, monno)
                            VALUES ('$vouchdt','$mvouch','$godown', '$source', '001', '$purcode','005', '$taxableamt','$mono')") or die(mysqli_error($conn));

    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,`godown`, source, trnno, accode, grpcode, amount, monno)
                            VALUES ('$vouchdt','$mvouch','$godown', '$source', '003', '5','020', '$gstamount','$mono')") or die(mysqli_error($conn));

    // party part
    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, `godown`, source, trnno, accode, grpcode, amount, monno)
                                VALUES ('$vouchdt','$mvouch','$godown', '$source', '002', '$accode','010', '$minus_amt','$mono')") or die(mysqli_error($conn));
}
echo "Success";
