<?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';
$maindb = 'cashrecp';
$keyfld = 'vouchno';

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

browse(mysqli_num_rows($getquery));

while ($fet = mysqli_fetch_array($getquery)) {
    $vouchdt = $fet["vouchdt"];
    $vouchno = $fet["vouchno"];
    $party = $fet["party"];
    $amount = $fet["amount"];
    $qty = $fet["qty"];
    $rate = $fet["rate"];
    $remark1 = $fet["remark1"];
    $remark2 = $fet["remark2"];
    
    
    $bankname = 3;
    $source = "CR";
    $qvouch = $vouchno ;
    $binit = "COH" ;
    $minus_amt = ($amount * -1);


    mysqli_query($conn, "DELETE FROM `$accdb` WHERE `vouchno` = '$vouchno' AND `source`='$source' ") 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'];

    // cash part
    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount)
                    VALUES ('$vouchdt','$qvouch', '$qvouch', '$source', '001', '$binit', '$bankname','002', '$minus_amt')") or die(mysqli_error($conn));

    
    // party part
    mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount)
                    VALUES ('$vouchdt','$qvouch', '$qvouch', '$source', '002', '$binit', '$party', '$pgrp', '$amount')") or die(mysqli_error($conn));
}
echo "Success";
?>
