<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
$zdb = $_SESSION["zdbyear_" . $dbname];

mysqli_select_db($conn,$zdb);
$accdb = 'actran';
$cashdb = 'cashrecp';
$maindb = 'challan';

$keyfld = 'vouchno';
$getquery = mysqli_query($conn,"select * from `$maindb` where vouchno != '' ") or die(mysqli_error($conn));

browse(mysqli_num_rows($getquery));

while ($fetchquery = mysqli_fetch_array($getquery)) {
    $mcashvouch = $fetchquery['cashvouch'];
    $type = $fetchquery['type'];

    $vouchdt = $fetchquery['vouchdt'];
    $party = $fetchquery['party'];
    $vouchno = $fetchquery['vouchno'];
    $amount = $fetchquery['amount'];
    $total = $fetchquery['total'];
    $lessdisc = $fetchquery['lessdisc'];

//    auto cash receipt entry start

//    if ($type == 'C' || $type == 'c') {
//        if ($mcashvouch == '0' || $mcashvouch == '') {
//            $gen1 = mysqli_query($conn,"SELECT MAX(vouchno) AS maxcode FROM `$cashdb`  LIMIT 0,1 ") or die(mysqli_error($conn));
//            if (mysqli_num_rows($gen1) > 0) {
//                $vvouch = mysqli_fetch_assoc($gen1);
//                $mcashvouch = $vvouch["maxcode"] + 1;
//            } else {
//                $mcashvouch = '1';
//            }
//        }
//
////            browse($mcashvouch);
//
//
//        $genrecp = mysqli_query($conn,"SELECT * FROM `$cashdb` where `vouchno` = '$mcashvouch'") or die(mysqli_error($conn));
//        if (mysqli_num_rows($genrecp) > 0) {
//            $fetrecp = mysqli_fetch_array($genrecp);
//            $mrecpno = $fetrecp[recpno];
//            $mcashvouch = $fetrecp[vouchno];
//        } else {
//
//            $gen2 = mysqli_query($conn,"SELECT MAX(recpno) AS maxrecp FROM `$cashdb` where bankname = '3'  LIMIT 0,1 ") or die(mysqli_error($conn));
//            if (mysqli_num_rows($gen2) > 0) {
//                $gvouch = mysqli_fetch_assoc($gen2);
//                $mrecpno = $gvouch["maxrecp"] + 1;
//            } else {
//                $mrecpno = '1';
//            }
//        }
//
//        $tamt = $billamt;
//        $adv = "Cash Received For";
//
//        mysqli_query($conn,"DELETE FROM `$cashdb` WHERE `vouchno`='$mcashvouch'") or die(mysqli_error($conn));
//
//        $mtype = '';
//        $weight = $fetchquery['weight'];
//        $straight = $fetchquery['straight'];
//        $totfeet = $fetchquery['totfeet'];
//        $nag = $fetchquery['nag'];
//
//        if ($weight == 0) {
//            if ($straight == 'Y' || $straight == 'y') {
//                $qty = $totfeet;
//            } else {
//                $qty = $nag;
//            }
//            $type = "F";
//        } else {
//            $qty = $weight;
//            $type = "W";
//        }
//
////            browse($totfeet);
////            browse($nag);
//
//        mysqli_query($conn,"INSERT INTO `$cashdb`( vouchno ,`vouchdt`, `bankname`, `bankinit`, 
//                `recpno`, `party`,`amount`,`remark1`,`remark2`,`trnflag`, `rate`, `type`, `qty`)
//                Values ('$mcashvouch', '$vouchdt', '3', 'COH', '$fetchquery[recpno]','$party','$amount',
//                        '$fetchquery[descrip]','Challan No.$vouchno','S', '$fetchquery[rate]', '$mtype', '$qty')") or die(mysqli_error($conn));
//
//// cash part     
//
//        $minus_amt = ($amount * -1);
//
//
//        mysqli_query($conn,"DELETE FROM `$accdb` WHERE `$keyfld`='$mcashvouch' and `source` = 'CR'") or die(mysqli_error($conn));
//
//        $qrygrp = mysqli_query($conn,"select `grpcode` from accmast where `accode` = '$party'") or die(mysqli_error($conn));
//        $fetgrp = mysqli_fetch_array($qrygrp);
//        $pgrp = $fetgrp['grpcode'];
//
//
//        mysqli_query($conn,"INSERT INTO `$accdb`(date, vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount, trnflag)
//                    VALUES ('$vouchdt','$mcashvouch', '$fetchquery[recpno]', 'CR', '001', 'COH','3','002', '$minus_amt','S')") or die(mysqli_error($conn));
//
//
//// party part
//        mysqli_query($conn,"INSERT INTO `$accdb`(date, vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount, trnflag)
//                    VALUES ('$vouchdt','$mcashvouch', '$fetchquery[recpno]', 'CR', '002', 'COH', '$party', '$pgrp', '$amount','S')") or die(mysqli_error($conn));
//    } else {
//        mysqli_query($conn,"DELETE FROM `$cashdb` WHERE `$keyfld`='$mcashvouch'") or die(mysqli_error($conn));
//        mysqli_query($conn,"DELETE FROM `$accdb` WHERE `$keyfld`='$mcashvouch' AND `source`='CR'")or die(mysqli_error($conn));
//        $mcashvouch = "";
//    }

    if ($type == 'C' || $type == 'c') {
        $salecode = 14;
    } else {
        $salecode = 15;
    }


//   auto cash receipt entry end
############################### Accounts Part ##################################
    
    $source = "CH";

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


    $delvdate = $fetchquery['delvdate'];
    $gstflag = $fetchquery['gstflag'];


    if ($amount > 0 && $delvdate != '0000-00-00' && $delvdate != '' && $gstflag == 'N') {

        $sminus_amt = $amount * -1;

// sale part
        mysqli_query($conn,"INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                    VALUES ('$vouchdt','$vouchno', '$source', '001', '$salecode','005', '$amount')") OR die(mysqli_error($conn));
// party part
        mysqli_query($conn,"INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                     VALUES ('$vouchdt','$vouchno', '$source', '002', '$party', '010', '$sminus_amt')") or die(mysqli_error($conn));
    }
}
echo "Success";
?>
