<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');
// include ('delinvoice.php');

mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$zyear = $myyear;

$zcomp = $_SESSION["comp_" . $dbname];

$maindb = 'salemain';
$trandb = 'saletran';

$zroot = $_SESSION["zdbroot"];

// $para = $_GET['para'];
// if ($para == "M") {
//     $formname = "Invoice - Multiple";
// } else {
//     $formname = "Invoice - Single";
// }

$zroot = $_SESSION["zdir"];
$file = 'invoice_fr_2.php';

if (isset($_POST["view"])) {

//     $dbillno = "";
//     $imon = "";
//     $iyear = "";

//     $stdate = '2012-07-01';      // date - service tax applicable
//     $rtit1 = '';
//     $rtit2 = '';

//     $mcono = $zcomp;

//     $qrycomp = mysqli_query($conn, "SELECT * from company WHERE cono = '$mcono' ") or die(mysqli_error($conn));
//     $fetcomp = mysqli_fetch_array($qrycomp);

//     $mgstno = $fetcomp['gstno'];
//     $mtdsperc = $fetcomp['tdsperc'];

//     $zadd1 = trim($fetcomp['hoadd1']) . ' ' . trim($fetcomp['hoadd2']);
//     $zadd2 = trim($fetcomp['hoadd3']) . ' ' . trim($fetcomp['email']);

//     $mrepprin = trim($fetcomp['invprin']);
//     $mbankdtl = trim($fetcomp['bankdtl']);
//     $mpaymode = trim($fetcomp['paymode']);
//     $mlatepay = trim($fetcomp['latepay']);

//     $zbankname = trim($fetcomp['bankname']);
//     $zbranch = trim($fetcomp['branchname']);
//     $zbankcode = trim($fetcomp['ifccode']);
//     $zacno = trim($fetcomp['acno']);

//     $qryyear = mysqli_query($conn, "SELECT * from compyear WHERE cono = '$mcono' and finyear = '$zyear' ") or die(mysqli_error($conn));
//     $fetyear = mysqli_fetch_array($qryyear);
//     $mstopperc = trim($fetcomp['stonop']);

//     $vmon = $_POST["month"];
//     IF (EMPTY($vmon)) {
//         browse('Month Cannot Be Empty');
//         echo "<script>
//           window.close();
//           </script>";
//     } else {
//         $rtit1 = 'Specific Month    ' . vmon;
//     }

//     $period = $_POST["period"];

//     IF (EMPTY($period)) {
//         browse('Period Cannot Be Empty');
//         echo "<script>
//           window.close();
//           </script>";
//     } ELSE {
//         $rtit1 = 'Specific Period   ' . $period;
//     }

//     if (empty($_POST["todate"])) {
//         browse("Invoice Date Cannot Be Empty");
//         echo "<script>
//           window.close();
//           </script>";
//     }

//     $mtodate = $_POST["todate"];
//     $mtdt = date("d-m-Y", strtotime($mtodate));

//     $ddate = $mtodate;

//     $mmonth = $vmon;
//     $mperiod = $period;

//     $rtit1 = '';
//     $cucond = "";

//     $iname = trim($_POST["account"]);

//     if (!empty($iname)) {
//         $rtit1 = 'Specific Party     ' . $iname;

//         $qryacc = mysqli_query($conn, "select * from accmast where name = '$iname' ") or die(mysqli_error($conn));
//         $noacc = mysqli_num_rows($qryacc);

//         if ($noacc == 0) {
//             browse('Party Does Not Exist');
//             echo "<script>
//           window.close();
//           </script>";
//         } else {
//             $fetacc = mysqli_fetch_array('$qryacc');
//             $cucode = $fetacc['custcode'];
//         }

//         $frmaccode = $cucode;
//         $toaccode = $cucode;
//     }

//     $frmcode = $_POST["frmcode"];

//     IF (!EMPTY($frmcode)) {
//         $qryacc = mysqli_query($conn, "select * from accmast where custcode = '$frmcode' ") or die(mysqli_error($conn));
//         $noacc = mysqli_num_rows($qryacc);

//         if ($noacc == 0) {
//             browse('Invalid From Customer Code');
//             echo "<script>
//           window.close();
//           </script>";
//         } else {
//             $fetacc = mysqli_fetch_array('$qryacc');
//             $frmaccode = $fetacc['accode'];
//         }
//     }

//     $tocode = $_POST["tocode"];

//     IF (!EMPTY($tocode)) {
//         $qryacc = mysqli_query($conn, "select * from accmast where custcode = '$tocode' ") or die(mysqli_error($conn));
//         $noacc = mysqli_num_rows($qryacc);

//         if ($noacc == 0) {
//             browse('Invalid From Customer Code');
//             echo "<script>
//           window.close();
//           </script>";
//         } else {
//             $fetacc = mysqli_fetch_array('$qryacc');
//             $toaccode = $fetacc['accode'];
//         }
//     }

//     $frmcust = $frmcode;
//     $tocust = $tocode;

//     IF (!EMPTY($frmcust) && !EMPTY($tocust)) {
//         $cucond = " and custcode >= '$frmcust' and custcode <= '$tocust' ";
//     }

//     $vcond = '';
//     $mvehno = $_POST["vehno"];
//     if (!empty($mvehno)) {
//         $rtit1 = 'Specific Vehicle     ' . $mvehno;
//         $vcond = " and vehno == '$mvehno' ";
//     }

//     $mdaydate = $_POST["daydate"];
//     IF (!EMPTY($daydate)) {
//         if ($mdaydate >= $zsdate && $mdaydate <= $zedate) {
            
//         } else {
//             browse('Date Does Not Belong To Financial Year');
//             echo "<script>
//           window.close();
//           </script>";
//         }
//     } ELSE {
//         IF ($mperiod == 8) {
//             browse('Date Cannot Be Empty');
//             echo "<script>
//           window.close();
//           </script>";
//         }
//     }

// //  build date condition string and period string

//     $period = $datecond = $puccond = '';
//     $mfromdate = '0000-00-00';
//     $mtodate = '0000-00-00';

//     if ($mmonth > 3) {
//         $myear = year($zsdate);
//     } else {
//         $myear = year($zedate);
//     }

//     $arrdate = $myear . '-' . $mmonth . '-01';

//     $qrymon = mysqli_query($conn, "SELECT * from month WHERE moncode = '$mmonth' ") or die(mysqli_error($conn));
//     $fetmon = mysqli_fetch_array($qrymon);
//     $mper = $fetmon['monname'];

//     $mdate = $arrdate;
//     $mondate1 = date('Y-m-d', strtotime($mdate . ' . 1 months'));
//     $mondate = date('Y-m-d', strtotime($mondate1 . ' - 1 days'));

//     $chkdate = $mondate;

//     $dmonth = date('F', strtotime($mdate));

//     browse($dmonth);

//     $mhf = $myear . '-' . $mmonth . '-01';

//     $ehf1 = date('Y-m-d', strtotime($mhf . ' . 1 months'));
//     $ehf = date('Y-m-d', strtotime($ehf1 . ' - 1 days'));

//     $mmd = date("d", $ehf);

//     $stdate1 = left($mondate, 8) . '01';
//     $stdate2 = left($mondate, 8) . '08';
//     $stdate3 = left($mondate, 8) . '16';
//     $stdate4 = left($mondate, 8) . '23';

//     $onedate = left($mondate, 8) . '07';
//     $twodate = left($mondate, 8) . '15';
//     $thredate = left($mondate, 8) . '22';

//     $oneten = left($mondate, 8) . '10';
//     $twoten = left($mondate, 8) . '20';


//     $arrdate = $myear . '-' . $mmonth . '-01';
//     $mper = date('F', strtotime($arrdate));


//     switch ($mperiod) {
//         case "1":
//             $datecond = " and month(vouchdt)  = '$mmonth' ";
//             $puccond = " and month(trandate) = '$mmonth' ";
//             $period = $mper;
//             $dispmon = $dmonth;
//             $bperiod = 1;
//             $chkdate = $mondate;
//             $mtodate = $todate;
//             $mbillcycle = "M";
//             break;

//         case "2";

//             $mfromdate = $myear . '-' . $mmonth . '-01';
//             $mtodate = $myear . '-' . $mmonth . '-15';

//             $period = '1st Half  ( ' . $mfromdate . ' - ' . $mtodate . ' )';
//             $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
//             $puccond = " and trandate >= '$mfromdate' and trandate <= '$mtodate' ";
//             $dispmon = left($dmonth, 3) . ' 01-15';
//             $bperiod = 2;
//             $chkdate = $twodate;
//             $mbillcycle = "H";
//             break;

//         case "3";

//             $mfromdate = $myear . '-' . $mmonth . '-16';
//             $to1 = date('Y-m-d', strtotime($mmonth . ' . 1 months'));
//             $mtodate = date('Y-m-d', strtotime($to1 . ' - 1 days'));

//             $period = '2nd Half  ( ' . $mfromdate . ' - ' . $mtodate . ' )';
//             $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
//             $puccond = " and trandate >= '$mfromdate' and trandate <= '$mtodate' ";
//             $dispmon = left($dmonth, 3) . ' 16-' . $mmd;
//             $bperiod = 2;
//             $chkdate = $mondate;
//             $mbillcycle = "H";
//             break;

//         case "4";
//             $mfromdate = $myear . '-' . $mmonth . '-01';
//             $mtodate = $myear . '-' . $mmonth . '-07';
//             $period = '1st Wk  ( ' . $mfromdate . ' - ' . $mtodate . ' )';
//             $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
//             $puccond = " and trandate >= '$mfromdate' and trandate <= '$mtodate' ";
//             $dispmon = left($dmonth, 3) . ' 01-07';
//             $bperiod = 3;
//             $chkdate = $onedate;
//             $mbillcycle = "W";
//             break;

//         case "5";
//             $mfromdate = $myear . '-' . $mmonth . '-08';
//             $mtodate = $myear . '-' . $mmonth . '-15';
//             $period = '2nd Wk  ( ' . $mfromdate . ' - ' . $mtodate . ' )';
//             $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
//             $puccond = "";
//             $dispmon = left($dmonth, 3) . ' 08-15';
//             $bperiod = 3;
//             $chkdate = $twodate;
//             $mbillcycle = "W";
//             break;

//         case "6";
//             $mfromdate = $myear . '-' . $mmonth . '-16';
//             $mtodate = $myear . '-' . $mmonth . '-22';
//             $period = '3rd Wk  ( ' . $mfromdate . ' - ' . $mtodate . ' )';
//             $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
//             $puccond = "";
//             $dispmon = left($dmonth, 3) . ' 16-22';
//             $bperiod = 3;
//             $chkdate = $thredate;
//             $mbillcycle = "W";
//             break;

//         case "7";

//             $mfromdate = $myear . '-' . $mmonth . '-23';
//             $mhalf = $myear . '-' . $mmonth . '-01';

//             $ehf1 = date('Y-m-d', strtotime($mhalf . ' . 1 months'));
//             $ehalf = date('Y-m-d', strtotime($ehf1 . ' - 1 days'));
//             $mtodate = $ehalf;
//             $period = '4th Wk  ( ' . $mfromdate . ' - ' . $mtodate . ' )';
//             $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
//             $puccond = "";
//             $dispmon = left($dmonth, 3) . ' 23-' . $mmd;
//             $bperiod = 3;
//             $chkdate = $mondate;
//             $mbillcycle = "W";
//             break;

//         case "8";

//             $cdatef = date('Y-m-d', strtotime($mdate));
//             $cdatet = date('Y-m-d', strtotime($chkdate));

//             if (($mdaydate >= $cdatef) && ($mdaydate <= $cdatet)) {
                
//             } else {
//                 browse('Date Does Not Belong To The Month Selected');
//                 echo "<script>
//           window.close();
//           </script>";
//             }

//             $mfromdate = $mdaydate;
//             $mtodate = $mdaydate;
//             $period = '( Daily ) - ' . $mtodate;

//             $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
//             $puccond = "";
//             $dispmon = $mdaydate;
//             $bperiod = 4;
//             $chkdate = $mdaydate;
//             $mbillcycle = "D";
//             break;

//         case "9";
//             $mfromdate = $myear . '-' . $mmonth . '-01';
//             $mtodate = $myear . '-' . $mmonth . '-10';

//             $period = 'First Ten Days ( ' . $mfromdate . ' - ' . $mtodate . ' )';
//             $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
//             $puccond = " and trandate >= '$mfromdate' and trandate <= '$mtodate' ";

//             $dispmon = left($dmonth, 3) . ' 01-10';
//             $bperiod = 5;
//             $chkdate = $mondate;
//             $mbillcycle = "T";
//             break;

//         case "10";
//             $mfromdate = $myear . '-' . $mmonth . '-11';
//             $mtodate = $myear . '-' . $mmonth . '-20';

//             $period = 'Mid Ten Days ( ' . $mfromdate . ' - ' . $mtodate . ' )';
//             $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
//             $puccond = " and trandate >= '$mfromdate' and trandate <= '$mtodate' ";

//             $dispmon = left($dmonth, 3) . ' 11-20';
//             $bperiod = 5;
//             $chkdate = $oneten;
//             $mbillcycle = "T";
//             break;

//         case "11";
//             $mfromdate = $myear . '-' . $mmonth . '-21';
//             $mhalf = $myear . '-' . $mmonth . '-01';

//             $ehf1 = date('Y-m-d', strtotime($mhalf . ' . 1 months'));
//             $ehalf = date('Y-m-d', strtotime($ehf1 . ' - 1 days'));
//             $mtodate = $ehalf;

//             $period = 'Last Ten Days ( ' . dtoc(mfromdate) . ' - ' . dtoc(mtodate) . ' )';
//             $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
//             $puccond = " and trandate >= '$mfromdate' and trandate <= '$mtodate' ";

//             $dispmon = left($dmonth, 3) . ' 16-' . $mmd;
//             $bperiod = 5;
//             $chkdate = $towten;
//             $mbillcycle = "T";
//             break;
//     }


//     IF ($mtodate > date("Y-m-d")) {
//         browse('Last Date Of Invoice Period Is Greater Than Current Date');
//         echo "<script>
//           window.close();
//           </script>";
//     }

//     $minvdt = $myear . '-' . $mmonth . '-01';

//     switch ($mperiod) {
//         case "3":
//             $minvdt = $myear . '-' . $mmonth . '-16';
//         case "5":
//             $minvdt = $myear . '-' . $mmonth . '-08';
//         case "6":
//             $minvdt = $myear . '-' . $mmonth . '-16';
//         case "7":
//             $minvdt = $myear . '-' . $mmonth . '-23';
//         case "8":
//             $minvdt = $mdaydate;
//         case "9":
//             $minvdt = $myear . '-' . $mmonth . '-01';
//         case "10":
//             $minvdt = $myear . '-' . $mmonth . '-11';
//         case "11":
//             $minvdt = $myear . '-' . $mmonth . '-21';
//     }

//     $mcond = "";

// // ********* end of date condition string

//     $cctr = 0;

//     $qrymon = mysqli_query($conn, "SELECT * from accmast WHERE custcode >= '$frmcust' AND custcode <= '$tocust' AND billcycle != '$mbillcycle' ") or die(mysqli_error($conn));
//     IF (mysqli_num_rows($qrymon) == 0) {
//         $fetmon = mysqli_fetch_array($qrymon);
//         $mcust = $fetmon['custcode'];

//         browse('Customer Code : ' . $mcust . '  Mismatch Billing Cycle Selected');
//         echo "<script>
//           window.close();
//           </script>";
//     }

//     $ccyc = mysqli_query($conn, "SELECT * from accmast WHERE billcycle = '$mbillcycle' AND dailyprint != 1 ") or die(mysqli_error($conn));

//     $cmain = mysqli_query($conn, "SELECT * from salemain WHERE accode != ''  $datecond $cucond $vcond and accode in 
//                                      (select accode from accmast WHERE billcycle = '$mbillcycle' AND dailyprint != 1 ) ") or die(mysqli_error($conn));
//     IF (mysqli_num_rows($cmain) == 0) {
//         browse('Record Not Found');

//         while ($rw = mysqli_fetch_array($ccyc)) {
//             $maccode = $rw['accode'];
//             $acode = $maccode;

//             // ********* invoice part

//             switch ($mperiod) {
//                 case "1":
//                     mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' ") or die(mysqli_error($conn));

//                     delro(13, 20, $acode, " month(date) = '$mmonth' ");
//                 case "2":
//                     mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
//                                          half != 2 and half != 5 and half != 6  and half != 10 ") or die(mysqli_error($conn));

//                     delinv($mmonth, $acode, 7, " and day(invdate) <= 15 ");

//                 case "3":
//                     mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
//                                          half != 1 and half != 3 and half != 4  and half != 8 ") or die(mysqli_error($conn));

//                     delinv($mmonth, $acode, 7, " and day(invdate) > 15 ");

//                 case "4":
//                     mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
//                                          half != 2 and half != 4 and half != 5  and half != 9 and half != 10 ") or die(mysqli_error($conn));

//                     delinv($mmonth, $acode, 7, " and day(invdate) <= 7 ");

//                 case "5":
//                     mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
//                                          half != 2 and half != 4 and half != 5  and half != 9 and half != 10 ") or die(mysqli_error($conn));

//                     delinv($mmonth, $acode, 7, " and day(invdate) > 7 and day(invdate) < 16  ");

//                 case "6":
//                     mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
//                                          half != 1 and half != 3 and half != 4  and half != 6 and half != 8 ") or die(mysqli_error($conn));

//                     delinv($mmonth, $acode, 7, " and day(invdate) > 15 and day(invdate) < 23  ");

//                 case "7":
//                     mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
//                                          half != 1 and half != 3 and half != 4  and half != 5 and half != 8 and half != 9 ") or die(mysqli_error($conn));

//                     delinv($mmonth, $acode, 7, " and day(invdate) > 22  ");

//                 case "8":
//                     delinv($mmonth, $acode, 0, "");

//                     IF ((mdaydate >= stdate1) && ($mdaydate <= onedate)) {
//                         delinv($mmonth, $acode, 1, "");
//                         delinv($mmonth, $acode, 3, "");
//                     } elseif (($mdaydate >= $stdate3) && ($mdaydate <= $twodate)) {
//                         delinv($mmonth, $acode, 1, "");
//                         delinv($mmonth, $acode, 4, "");
//                     } elseif (($mdaydate >= $stdate3) && ($mdaydate <= $thredate)) {
//                         delinv($mmonth, $acode, 2, "");
//                         delinv($mmonth, $acode, 5, "");
//                     } else {
//                         delinv($mmonth, $acode, 2, "");
//                         delinv($mmonth, $acode, 6, "");
//                     }

//                     delinv($mmonth, $acode, 7, " and invdate = '$mdaydate' ");

//                 case "9":
//                     mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
//                                          half != 2 and half != 5 and half != 6  and half != 9 and half != 10 ") or die(mysqli_error($conn));

//                     delinv($mmonth, $acode, 7, " and day(invdate) <= 11  ");

//                 case "10":
//                     mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
//                                          half != 3 and half != 6 and half != 8  and half != 10 ") or die(mysqli_error($conn));

//                     delinv($mmonth, $acode, 7, " and day(invdate) > 11 and DAY(invdate) < 20 ");

//                 case "11":
//                     mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
//                                          half != 1 and half != 3 and half != 4 and half != 8  and half != 9 ") or die(mysqli_error($conn));

//                     delinv($mmonth, $acode, 7, " and day(invdate) < 21 ");
//             }

//             // ********* actran part

//             switch ($mperiod) {
//                 case "1":
//                     delro(13, 20, $acode, " month(date) = '$mmonth' ");
//                 case "2":
//                     delro(13, 14, $acode, " month(date) = '$mmonth' ");
//                     delro(16, 17, $acode, " month(date) = '$mmonth' ");
//                     delro(20, 20, $acode, "  day(date) <= 15 ");
//                 case "3":
//                     delro(13, 14, $acode, " month(date) = '$mmonth' ");
//                     delro(15, 15, $acode, " month(date) = '$mmonth' ");
//                     delro(18, 19, $acode, " month(date) = '$mmonth' ");
//                     delro(20, 20, $acode, "  day(date) >= 16 ");
//                 case "4":
//                     delro(13, 14, $acode, " month(date) = '$mmonth' ");
//                     delro(16, 16, $acode, " month(date) = '$mmonth' ");
//                     delro(20, 20, $acode, "  day(date) <= 7 ");
//                 case "5":
//                     delro(13, 14, $acode, " month(date) = '$mmonth' ");
//                     delro(17, 17, $acode, " month(date) = '$mmonth' ");
//                     delro(20, 20, $acode, " day(date) >= 8 and day(date) <= 15  ");

//                 case "6":
//                     delro(13, 13, $acode, " month(date) = '$mmonth' ");
//                     delro(15, 15, $acode, " month(date) = '$mmonth' ");
//                     delro(18, 18, $acode, " month(date) = '$mmonth' ");
//                     delro(20, 20, $acode, " day(date) >= 16 and day(date) <= 22 ");

//                 case "7":
//                     delro(13, 13, $acode, " month(date) = '$mmonth' ");
//                     delro(15, 15, $acode, " month(date) = '$mmonth' ");
//                     delro(19, 19, $acode, " month(date) = '$mmonth' ");
//                     delro(20, 20, $acode, " day(date) >= 23 ");

//                 case "8":
//                     delro(13, 13, $acode, " month(date) = '$mmonth' ");
//                     IF ((mdaydate >= stdate1) && ($mdaydate <= onedate)) {
//                         delro(14, 14, $acode, " month(date) = '$mmonth' ");
//                         delro(16, 16, $acode, " month(date) = '$mmonth' ");
//                     } elseif (($mdaydate >= $stdate3) && ($mdaydate <= $twodate)) {
//                         delro(14, 14, $acode, " month(date) = '$mmonth' ");
//                         delro(17, 17, $acode, " month(date) = '$mmonth' ");
//                     } elseif (($mdaydate >= $stdate3) && ($mdaydate <= $thredate)) {
//                         delro(15, 15, $acode, " month(date) = '$mmonth' ");
//                         delro(18, 18, $acode, " month(date) = '$mmonth' ");
//                     } else {
//                         delro(15, 15, $acode, " month(date) = '$mmonth' ");
//                         delro(19, 19, $acode, " month(date) = '$mmonth' ");
//                     }

//                     delro(20, 20, $acode, " date = '$mdaydate' ");
//                 case "9":

//                 case "10":

//                 case "11":
//             }
//         }


//         echo "<script>
//           window.close();
//           </script>";
//     }

    $qrysale = mysqli_query($conn, "SELECT a.*, b.srno, b.prodcode, b.qty, b.unit, b.rate, b.amount as amt, 
                                           (b.qty * b.discdies) as tdiscdies, c.name as prodname
                                           from cmain a, saletran b, product c WHERE a.vouchno = b.vouchno and b.prodcode = c.prodcode ") or die(mysqli_error($conn));
    $fetyear = mysqli_fetch_array($qryyear);
    // $mstopperc = trim($fetcomp['stonop']);


// ********************
    

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #6082BF;
        }

    </style>
    <script src="MyScript/item.js" type="text/javascript"></script>
    </head>
    <body>
        <div class="container">
    <?php include("includes/head_rep.php"); ?>
            <div class="container">
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:15%">Sr</th>
                                    <th style="width:15%">Voucher</th>
                                    <th style="width:20%">Date</th>
                                    <th style="width:20%">Godown</th>
                                    <th style="width:30%; text-align:right;">Total Qty.</th>
                                </tr>
                            </thead>
                            <tbody>
    <?php
    $ctr = 0;
    $totqty = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {

        $ggodwn = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code` = '$pmain[godown]'")or die(mysqli_error($conn));
        $godwn = mysqli_fetch_array($ggodwn);

        $ctr++;
        Echo "<tr id='boldfont'>";
        Echo "<td>$ctr</td>";
        Echo "<td>$pmain[vouchno]</td>";
        Echo "<td>$pmain[mydt]</td>";
        Echo "<td>$godwn[gcode]</td>";
        Echo "<td style='text-align:right;'>$pmain[totqty]</td>";
        Echo "</tr>";

        $totqty = $totqty . $pmain["totqty"];

        if ($_POST["dtype"] == 2) {

            Echo "<tr style='height:10px;'>";
            Echo "<td colspan='25'>";
            Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
            Echo "<th style='width:5%;'>Sr</th>";
            Echo "<th style='width:10%;'>Barcode</th>";
            Echo "<th style='width:10%;' class='text-right'>Item Code</th>";
            Echo "<th style='width:15%;' class='text-right'>Item Name</th>";
            Echo "<th style='width:10%;' class='text-right'>HSN Code</th>";
            Echo "<th style='width:8%;' class='text-right'>Gram</th>";
            Echo "<th style='width:8%;' class='text-right'>Unit</th>";
            Echo "<th style='width:8%;' class='text-right'>Batch</th>";
            Echo "<th style='width:8%;' class='text-right'>Qty.</th>";
            Echo "<th style='width:20%;' class='text-right'>Remark</th>";
            Echo "<tr>";


            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
            $ctr2 = 0;
            while ($tran = mysqli_fetch_array($gettran)) {

                $ctr2++;
                Echo "<tr>";
                Echo "<td>$ctr2</td>";
                Echo "<td>$tran[barcode]</td>";
                Echo "<td>$tran[itemcode]</td>";
                Echo "<td>$tran[itemname]</td>";
                Echo "<td>$tran[hsncode]</td>";
                Echo "<td>$tran[gram]</td>";
                Echo "<td>$tran[unit]</td>";
                Echo "<td>$tran[batch]</td>";
                Echo "<td>$tran[qty]</td>";
                Echo "<td>$tran[remark]</td>";
                Echo "</tr>";

                $trtotqty = $trtotqty . $tran['qty'];
            }
            Echo "<tr>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "<td></td>";
            Echo "<td>Total</td>";
            Echo "<td>$trtotqty</td>";
            Echo "<td></td>";
            Echo "</tr>";

            Echo "</table>";
            Echo "</td>";
        }
    }

    Echo "<tr>";
    Echo "<td></td>";
    Echo "<td></td>";
    Echo "<td></td>";
    Echo "<td><b>Total</b></td>";
    Echo "<td style='text-align:right'><b>$totqty</b></td>";
    Echo "</tr>";
    ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
    <?php include("includes/linksfoot.php");
    ?>
    </body>
    </html>
            <?php
        }
        ?>