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

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

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

$zroot = $_SESSION["zdbroot"];

$formname = ' Register';
$file = 'hsndtl.php';
$regtype = $_POST["regtype"];

if (isset($_POST["excel"])) {
    $a = 1;
    $hsncode = $_POST["hsncode"];
    if (!empty($hsncode)) {
        $hsncond = " AND a.`hsncode` = '$hsncode' ";
    } else {
        browse('HSN Code Cannot Be Empty');
        $a = 0;
    }

    if ($a = 1) {
        if (($_GET["fdt"]) != '') {
            $mfdt = $_GET["fdt"];
            $mtdt = $_GET["tdt"];
            $mfrmdate = date("y-m-d", strtotime($mfdt));
            $mtodate = date("y-m-d", strtotime($mtdt));
            $dtype = $_GET["rpt"];
        } else {

            $mfrmdate = trim($_POST["frmdt"]);
            $mtodate = trim($_POST["todt"]);

            if (empty($_POST["frmdt"])) {
                browse("From Date Cannot Be Empty");
                header("refresh:1; saleitemgrp.php");
            }
            if (empty($_POST["todt"])) {
                browse("To Date Cannot Be Empty");
                header("refresh:1; saleitemgrp.php");
            }
            $mfdt = date("d-m-Y", strtotime($mfrmdate));
            $mtdt = date("d-m-Y", strtotime($mtodate));
        }

        $getmain = mysqli_query($conn, "SELECT a.*, b.oms from saletran a, salemain b WHERE a.vouchno = b.vouchno and a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $hsncond  ") or die(mysqli_error($conn));
        
        $getslo = mysqli_query($conn, "SELECT a.*, b.oms from slonltran a, slonlmain b  WHERE a.vouchno = b.vouchno  and a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $hsncond  ") or die(mysqli_error($conn));

        $getdex = mysqli_query($conn, "SELECT a.*, b.oms from deextran a, deexmain b  WHERE a.vouchno = b.vouchno  and a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $hsncond  ") or die(mysqli_error($conn));

        $getcred = mysqli_query($conn, "SELECT a.*, b.oms from credtran a, credmain b WHERE a.vouchno = b.vouchno and a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $hsncond  ") or die(mysqli_error($conn));
//        print table($getcred);
//        die();
        
        $contents .= "Sr., Voucher, Date, Product Code, Product Name, Gram, Qty., Rate, Amount, ";
        $contents .= "Discount, Taxable Amt, CGST 0.05%, SGST 0.05%,  CGST 2.5%, SGST 2.5%, ";
        $contents .= "CGST 6%, SGST 6%, CGST 9%, SGST 9%, CGST 14%, SGST 14%,";
        $contents .= " IGST 0.1%, IGST 5%, IGST 12%, ";
        $contents .= " IGST 18%,IGST 28%\n";

        $ctr = 0;
        $tqty = 0;
        $trate = 0;
        $tamt = 0;
        $tdisc = 0;
        $ttax = 0;
        $tcgst1 = 0;
        $tsgst1 = 0;
        $tcgst5 = 0;
        $tcgst5 = 0;
        $tsgst5 = 0;
        $tsgst5 = 0;
        $tsgst12 = 0;
        $tsgst12 = 0;
        $tcgst12 = 0;
        $tcgst12 = 0;
        $tsgst18 = 0;
        $tsgst18 = 0;
        $tcgst18 = 0;
        $tcgst18 = 0;
        $tcgst28 = 0;
        $tcgst28 = 0;
        $tsgst28 = 0;
        $tsgst28 = 0;
        $tigst1 = 0;
        $tigst5 = 0;
        $tigst12 = 0;
        $tigst18 = 0;
        $tigst28 = 0;

        while ($pmain = mysqli_fetch_array($getmain)) {
//        browse(2);
            $getitem = mysqli_query($conn, "SELECT name from product WHERE prodcode = '$pmain[itemcode]' ") or die(mysqli_error($conn));
            $geti = mysqli_fetch_array($getitem);
            $itemname = $geti['name'];

            $voucher = $pmain['vouchno'];
            $vouchdt = $pmain['vouchdt'];
            $itemcode = $pmain['itemcode'];
            $gram = $pmain['gram'];
            $qty = $pmain['qty'];
            $rate = $pmain['rate'];
            $amount = $pmain['amount'];
            $discount = $pmain['discamt'];
            $total = $pmain['subtot'];
            $oms = $pmain['oms'];
            
            $igstrate = $pmain['igstrate'];
            $cgstrate = $pmain['cgstrate'];
            
            $igst1 = $cgst1 = $sgst1 = $igst5 = $cgst5 = $sgst5 = $igst12 = $cgst12 = $sgst12 = 0 ;
            $igst18 = $cgst18 = $sgst18 = $igst28 = $cgst28 = $sgst28 = 0 ;
            
            if ($oms == 1) {
                switch ($igstrate) {
                case 5 ;
                    $igst5 = $pmain['igstamt'];
                    break ;
                case 12 ;
                    $igst12 = $pmain['igstamt'];
                    break ;
                case 18 ;
                    $igst18 = $pmain['igstamt'];
                    break ;
                case 28 ;
                    $igst28 = $pmain['igstamt'];
                    break ;
                }
            } else {
                $gstrate = $cgstrate * 2 ;
                switch ($gstrate) {
                case 5 ;
                    $cgst5 = $pmain['cgstamt'];
                    $sgst5 = $pmain['sgstamt'];
                    break ;
                case 12 ;
                    $cgst12 = $pmain['cgstamt'];
                    $sgst12 = $pmain['sgstamt'];
                    break ;
                case 18 ;
                    $cgst18 = $pmain['cgstamt'];
                    $sgst18 = $pmain['sgstamt'];
                    break ;
                case 28 ;
                    $cgst28 = $pmain['cgstamt'];
                    $sgst28 = $pmain['sgstamt'];
                    break ;
                }
            }
            
            $tqty = $qty + $tqty;
            $trate = $rate + $trate;
            $tamt = $amount + $tamt;
            $tdisc = $discounr + $tdisc;
            $ttax = $ttax + $total;
            $tcgst1 = $tcgst1 + $cgst1;
            $tcgst5 = $tcgst5 + $cgst5;
            $tcgst12 = $tcgst12 + $cgst12;
            $tcgst18 = $tcgst18 + $cgst18;
            $tcgst28 = $tcgst28 + $cgst28;
            $tsgst1 = $tsgst1 + $sgst1;
            $tsgst5 = $tsgst5 + $sgst5;
            $tsgst12 = $tsgst12 + $sgst12;
            $tsgst18 = $tsgst18 + $sgst18;
            $tsgst28 = $tsgst28 + $sgst28;
            $tigst1 = $tigst1 + $igst1;
            $tigst5 = $tigst5 + $igst5;
            $tigst12 = $tigst12 + $igst12;
            $tigst18 = $tigst18 + $igst18;
            $tigst28 = $tigst28 + $igst28;

            $ctr++;
            $contents .= $ctr . ",";
            $contents .= $voucher . ",";
            $contents .= $vouchdt . ",";
            $contents .= $itemcode . ",";
            $contents .= $itemname . ",";
            $contents .= $gram . ",";
            $contents .= $qty . ",";
            $contents .= $rate . ",";
            $contents .= $amount . ",";
            $contents .= $discount . ",";
            $contents .= $total . ",";
            $contents .= $cgst1 . ",";
            $contents .= $sgst1 . ",";
            $contents .= $cgst5 . ",";
            $contents .= $sgst5 . ",";
            $contents .= $cgst12 . ",";
            $contents .= $sgst12 . ",";
            $contents .= $cgst18 . ",";
            $contents .= $sgst18 . ",";
            $contents .= $cgst28 . ",";
            $contents .= $sgst28 . ",";
            $contents .= $igst1 . ",";
            $contents .= $igst5 . ",";
            $contents .= $igst12 . ",";
            $contents .= $igst18 . ",";
            $contents .= $igst28 . "\n";
        }


        $ctr = 0;

        while ($pmain2 = mysqli_fetch_array($getslo)) {

            $getitem = mysqli_query($conn, "SELECT name from product WHERE prodcode = '$pmain2[itemcode]' ") or die(mysqli_error($conn));
            $geti2 = mysqli_fetch_array($getitem);


            $voucher = $pmain2['vouchno'];
            $vouchdt = $pmain2['vouchdt'];
            $itemcode = $pmain2['itemcode'];
            $itemname = $geti2['name'];
            $gram = $pmain2['gram'];
            $qty = $pmain2['qty'];
            $rate = $pmain2['rate'];
            $amount = $pmain2['amount'];
            $discount = $pmain2['discamt'];
            $total = $pmain2['subtot'];
            $oms = $pmain2['oms'];
            
            $igstrate = $pmain2['igstrate'];
            $cgstrate = $pmain2['cgstrate'];
            
            $igst1 = $cgst1 = $sgst1 = $igst5 = $cgst5 = $sgst5 = $igst12 = $cgst12 = $sgst12 = 0 ;
            $igst18 = $cgst18 = $sgst18 = $igst28 = $cgst28 = $sgst28 = 0 ;
            
            if ($oms == 1) {
                switch ($igstrate) {
                case 5 ;
                    $igst5 = $pmain2['igstamt'];
                    break ;
                case 12 ;
                    $igst12 = $pmain2['igstamt'];
                    break ;
                case 18 ;
                    $igst18 = $pmain2['igstamt'];
                    break ;
                case 28 ;
                    $igst28 = $pmain2['igstamt'];
                    break ;
                }
            } else {
                $gstrate = $cgstrate * 2 ;
                switch ($gstrate) {
                case 5 ;
                    $cgst5 = $pmain2['cgstamt'];
                    $sgst5 = $pmain2['sgstamt'];
                    break ;
                case 12 ;
                    $cgst12 = $pmain2['cgstamt'];
                    $sgst12 = $pmain2['sgstamt'];
                    break ;
                case 18 ;
                    $cgst18 = $pmain2['cgstamt'];
                    $sgst18 = $pmain2['sgstamt'];
                    break ;
                case 28 ;
                    $cgst28 = $pmain2['cgstamt'];
                    $sgst28 = $pmain2['sgstamt'];
                    break ;
                }
            }
 
            $tqty = $qty + $tqty;
            $trate = $rate + $trate;
            $tamt = $amount + $tamt;
            $tdisc = $discounr + $tdisc;
            $ttax = $ttax + $total;
            $tcgst1 = $tcgst1 + $cgst1;
            $tcgst5 = $tcgst5 + $cgst5;
            $tcgst12 = $tcgst12 + $cgst12;
            $tcgst18 = $tcgst18 + $cgst18;
            $tcgst28 = $tcgst28 + $cgst28;
            $tsgst1 = $tsgst1 + $sgst1;
            $tsgst5 = $tsgst5 + $sgst5;
            $tsgst12 = $tsgst12 + $sgst12;
            $tsgst18 = $tsgst18 + $sgst18;
            $tsgst28 = $tsgst28 + $sgst28;
            $tigst1 = $tigst1 + $igst1;
            $tigst5 = $tigst5 + $igst5;
            $tigst12 = $tigst12 + $igst12;
            $tigst18 = $tigst18 + $igst18;
            $tigst28 = $tigst28 + $igst28;

            $ctr++;
            $contents .= $ctr . ",";
            $contents .= $voucher . ",";
            $contents .= $vouchdt . ",";
            $contents .= $itemcode . ",";
            $contents .= $itemname . ",";
            $contents .= $gram . ",";
            $contents .= $qty . ",";
            $contents .= $rate . ",";
            $contents .= $amount . ",";
            $contents .= $discount . ",";
            $contents .= $total . ",";
            $contents .= $cgst1 . ",";
            $contents .= $sgst1 . ",";
            $contents .= $cgst5 . ",";
            $contents .= $sgst5 . ",";
            $contents .= $cgst12 . ",";
            $contents .= $sgst12 . ",";
            $contents .= $cgst18 . ",";
            $contents .= $sgst18 . ",";
            $contents .= $cgst28 . ",";
            $contents .= $sgst28 . ",";
            $contents .= $igst1 . ",";
            $contents .= $igst5 . ",";
            $contents .= $igst12 . ",";
            $contents .= $igst18 . ",";
            $contents .= $igst28 . "\n";
        }
        
        $ctr = 0;
        while ($pmain3 = mysqli_fetch_array($getdex)) {

            $getitem = mysqli_query($conn, "SELECT name from product WHERE prodcode = '$pmain3[itemcode]' ") or die(mysqli_error($conn));
            $geti3 = mysqli_fetch_array($getitem);


            $voucher = $pmain3['vouchno'];
            $vouchdt = $pmain3['vouchdt'];
            $itemcode = $pmain3['itemcode'];
            $itemname = $geti3['name'];
            $gram = $pmain3['gram'];
            $qty = $pmain3['qty'];
            $rate = $pmain3['rate'];
            $amount = $pmain3['amount'];
            $discount = $pmain3['discamt'];
            $total = $pmain3['subtot'];
            $oms = $pmain3['oms'];
            
            $igstrate = $pmain3['igstrate'];
            $cgstrate = $pmain3['cgstrate'];
            
            $igst1 = $cgst1 = $sgst1 = $igst5 = $cgst5 = $sgst5 = $igst12 = $cgst12 = $sgst12 = 0 ;
            $igst18 = $cgst18 = $sgst18 = $igst28 = $cgst28 = $sgst28 = 0 ;
            
            if ($oms == 1) {
                switch ($igstrate) {
                case 5 ;
                    $igst5 = $pmain3['igstamt'];
                    break ;
                case 12 ;
                    $igst12 = $pmain3['igstamt'];
                    break ;
                case 18 ;
                    $igst18 = $pmain3['igstamt'];
                    break ;
                case 28 ;
                    $igst28 = $pmain3['igstamt'];
                    break ;
                }
            } else {
                $gstrate = $cgstrate * 2 ;
                switch ($gstrate) {
                case 5 ;
                    $cgst5 = $pmain3['cgstamt'];
                    $sgst5 = $pmain3['sgstamt'];
                    break ;
                case 12 ;
                    $cgst12 = $pmain3['cgstamt'];
                    $sgst12 = $pmain3['sgstamt'];
                    break ;
                case 18 ;
                    $cgst18 = $pmain3['cgstamt'];
                    $sgst18 = $pmain3['sgstamt'];
                    break ;
                case 28 ;
                    $cgst28 = $pmain3['cgstamt'];
                    $sgst28 = $pmain3['sgstamt'];
                    break ;
                }
            }
 
            $tqty = $qty + $tqty;
            $trate = $rate + $trate;
            $tamt = $amount + $tamt;
            $tdisc = $discounr + $tdisc;
            $ttax = $ttax + $total;
            $tcgst1 = $tcgst1 + $cgst1;
            $tcgst5 = $tcgst5 + $cgst5;
            $tcgst12 = $tcgst12 + $cgst12;
            $tcgst18 = $tcgst18 + $cgst18;
            $tcgst28 = $tcgst28 + $cgst28;
            $tsgst1 = $tsgst1 + $sgst1;
            $tsgst5 = $tsgst5 + $sgst5;
            $tsgst12 = $tsgst12 + $sgst12;
            $tsgst18 = $tsgst18 + $sgst18;
            $tsgst28 = $tsgst28 + $sgst28;
            $tigst1 = $tigst1 + $igst1;
            $tigst5 = $tigst5 + $igst5;
            $tigst12 = $tigst12 + $igst12;
            $tigst18 = $tigst18 + $igst18;
            $tigst28 = $tigst28 + $igst28;

            $ctr++;
            $contents .= $ctr . ",";
            $contents .= $voucher . ",";
            $contents .= $vouchdt . ",";
            $contents .= $itemcode . ",";
            $contents .= $itemname . ",";
            $contents .= $gram . ",";
            $contents .= $qty . ",";
            $contents .= $rate . ",";
            $contents .= $amount . ",";
            $contents .= $discount . ",";
            $contents .= $total . ",";
            $contents .= $cgst1 . ",";
            $contents .= $sgst1 . ",";
            $contents .= $cgst5 . ",";
            $contents .= $sgst5 . ",";
            $contents .= $cgst12 . ",";
            $contents .= $sgst12 . ",";
            $contents .= $cgst18 . ",";
            $contents .= $sgst18 . ",";
            $contents .= $cgst28 . ",";
            $contents .= $sgst28 . ",";
            $contents .= $igst1 . ",";
            $contents .= $igst5 . ",";
            $contents .= $igst12 . ",";
            $contents .= $igst18 . ",";
            $contents .= $igst28 . "\n";

//<!--end deem register-->  
        }


        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= "Total" . ",";
        $contents .= $tqty . ",";
        $contents .= $trate . ",";
        $contents .= $tamt . ",";
        $contents .= $tdisc . ",";
        $contents .= $ttax . ",";
        $contents .= $tcgst1 . ",";
        $contents .= $tsgst1 . ",";
        $contents .= $tcgst5 . ",";
        $contents .= $tsgst5 . ",";
        $contents .= $tcgst12 . ",";
        $contents .= $tsgst12 . ",";
        $contents .= $tcgst18 . ",";
        $contents .= $tsgst18 . ",";
        $contents .= $tcgst28 . ",";
        $contents .= $tsgst28 . ",";
        $contents .= $tigst1 . ",";
        $contents .= $tigst5 . ",";
        $contents .= $tigst12 . ",";
        $contents .= $tigst18 . ",";
        $contents .= $tigst28 . "\n";
        
        $contents .= "\n";
        $contents .= "\n";
        
//////////////// credit note
        
        $ctr = 0;
        $ctqty = 0;
        $ctrate = 0;
        $ctamt = 0;
        $ctdisc = 0;
        $cttax = 0;
        $ctcgst1 = 0;
        $ctsgst1 = 0;
        $ctcgst5 = 0;
        $ctcgst5 = 0;
        $ctsgst5 = 0;
        $ctsgst5 = 0;
        $ctsgst12 = 0;
        $ctsgst12 = 0;
        $ctcgst12 = 0;
        $ctcgst12 = 0;
        $ctsgst18 = 0;
        $ctsgst18 = 0;
        $ctcgst18 = 0;
        $ctcgst18 = 0;
        $ctcgst28 = 0;
        $ctcgst28 = 0;
        $ctsgst28 = 0;
        $ctsgst28 = 0;
        $ctigst1 = 0;
        $ctigst5 = 0;
        $ctigst12 = 0;
        $ctigst18 = 0;
        $ctigst28 = 0;

        while ($pmainc = mysqli_fetch_array($getcred)) {
            $getitem = mysqli_query($conn, "SELECT name from product WHERE prodcode = '$pmainc[itemcode]' ") or die(mysqli_error($conn));
            $geti = mysqli_fetch_array($getitem);
            $itemname = $geti['name'];

            $voucher = $pmainc['vouchno'];
            $vouchdt = $pmainc['vouchdt'];
            $itemcode = $pmainc['itemcode'];
            $gram = $pmainc['gram'];
            $qty = $pmainc['qty'];
            $rate = $pmainc['rate'];
            $amount = $pmainc['amount'];
            $discount = $pmainc['discamt'];
            $total = $pmainc['subtot'];
            $oms = $pmainc['oms'];
            
            $igstrate = $pmainc['igstrate'];
            $cgstrate = $pmainc['cgstrate'];
            
            $igst1 = $cgst1 = $sgst1 = $igst5 = $cgst5 = $sgst5 = $igst12 = $cgst12 = $sgst12 = 0 ;
            $igst18 = $cgst18 = $sgst18 = $igst28 = $cgst28 = $sgst28 = 0 ;
            
            if ($oms == 1) {
                switch ($igstrate) {
                case 5 ;
                    $igst5 = $pmainc['igstamt'];
                    break ;
                case 12 ;
                    $igst12 = $pmainc['igstamt'];
                    break ;
                case 18 ;
                    $igst18 = $pmainc['igstamt'];
                    break ;
                case 28 ;
                    $igst28 = $pmainc['igstamt'];
                    break ;
                }
            } else {
                $gstrate = $cgstrate * 2 ;
                switch ($gstrate) {
                case 5 ;
                    $cgst5 = $pmainc['cgstamt'];
                    $sgst5 = $pmainc['sgstamt'];
                    break ;
                case 12 ;
                    $cgst12 = $pmainc['cgstamt'];
                    $sgst12 = $pmainc['sgstamt'];
                    break ;
                case 18 ;
                    $cgst18 = $pmainc['cgstamt'];
                    $sgst18 = $pmainc['sgstamt'];
                    break ;
                case 28 ;
                    $cgst28 = $pmainc['cgstamt'];
                    $sgst28 = $pmainc['sgstamt'];
                    break ;
                }
            }
            
            $ctqty = $ctqty + ($qty );
            $ctrate = $ctrate + $rate;
            $ctamt = $ctamount + ($amt);
            $ctdisc = $ctdiscount + ($disc);
            $cttax = $cttax + ($total);
            $ctcgst1 = $ctcgst1 + ($cgst1);
            $ctcgst5 = $ctcgst5 + ($cgst5);
            $ctcgst12 = $ctcgst12 + ($cgst12);
            $ctcgst18 = $ctcgst18 + ($cgst18);
            $ctcgst28 = $ctcgst28 + ($cgst28);
            $ctsgst1 = $ctsgst1 + ($sgst1);
            $ctsgst5 = $ctsgst5 + ($sgst5);
            $ctsgst12 = $ctsgst12 + ($sgst12);
            $tsgst18 = $tsgst18 + ($sgst18);
            $ctsgst28 = $ctsgst28 + ($sgst28);
            $ctigst1 = $ctigst1 + ($igst1);
            $ctigst5 = $ctigst5 + ($igst5);
            $ctigst12 = $ctigst12 + ($igst12);
            $ctigst18 = $ctigst18 + ($igst18);
            $ctigst28 = $ctigst28 + ($igst28);
            
            $ctr++;
//            $contents .= $ctr . ",";
//            $contents .= $voucher . ",";
//            $contents .= $vouchdt . ",";
//            $contents .= $itemcode . ",";
//            $contents .= $itemname . ",";
//            $contents .= $gram . ",";
//            $contents .= $qty . ",";
//            $contents .= $rate . ",";
//            $contents .= $amount . ",";
//            $contents .= $discount . ",";
//            $contents .= $total . ",";
//            $contents .= $cgst1 . ",";
//            $contents .= $sgst1 . ",";
//            $contents .= $cgst5 . ",";
//            $contents .= $sgst5 . ",";
//            $contents .= $cgst12 . ",";
//            $contents .= $sgst12 . ",";
//            $contents .= $cgst18 . ",";
//            $contents .= $sgst18 . ",";
//            $contents .= $cgst28 . ",";
//            $contents .= $sgst28 . ",";
//            $contents .= $igst1 . ",";
//            $contents .= $igst5 . ",";
//            $contents .= $igst12 . ",";
//            $contents .= $igst18 . ",";
//            $contents .= $igst28 . "\n";
        }
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= "Total Credit " . ",";
        $contents .= $ctqty . ",";
        $contents .= $ctrate . ",";
        $contents .= $ctamt . ",";
        $contents .= $ctdisc . ",";
        $contents .= $cttax . ",";
        $contents .= $ctcgst1 . ",";
        $contents .= $ctsgst1 . ",";
        $contents .= $ctcgst5 . ",";
        $contents .= $ctsgst5 . ",";
        $contents .= $ctcgst12 . ",";
        $contents .= $ctsgst12 . ",";
        $contents .= $ctcgst18 . ",";
        $contents .= $ctsgst18 . ",";
        $contents .= $ctcgst28 . ",";
        $contents .= $ctsgst28 . ",";
        $contents .= $ctigst1 . ",";
        $contents .= $ctigst5 . ",";
        $contents .= $ctigst12 . ",";
        $contents .= $ctigst18 . ",";
        $contents .= $ctigst28 . "\n";
        $contents .=  "\n";
            
            $tqty = $tqty - $ctqty;
            $trate = $trate - $ctrate;
            $tamt =  $tamt - $ctamt;
            $tdisc = $tdisc - $ctdisc;
            $ttax = $ttax - $cttax;
            $tcgst1 = $tcgst1 - $ctcgst1;
            $tcgst5 = $tcgst5 - $ctcgst5;
            $tcgst12 = $tcgst12 - $ctcgst12;
            $tcgst18 = $tcgst18 - $ctcgst18;
            $tcgst28 = $tcgst28 - $ctcgst28;
            $tsgst1 = $tsgst1 - $ctsgst1;
            $tsgst5 = $tsgst5 - $ctsgst5;
            $tsgst12 = $tsgst12 - $ctsgst12;
            $tsgst18 = $tsgst18 - $ctsgst18;
            $tsgst28 = $tsgst28 - $ctsgst28;
            $tigst1 = $tigst1 - $ctigst1;
            $tigst5 = $tigst5 - $ctigst5;
            $tigst12 = $tigst12 - $ctigst12;
            $tigst18 = $tigst18 - $ctigst18;
            $tigst28 = $tigst28 - $ctigst28;
        
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= " " . ",";
        $contents .= "Total " . ",";
        $contents .= $tqty . ",";
        $contents .= $trate . ",";
        $contents .= $tamt . ",";
        $contents .= $tdisc . ",";
        $contents .= $ttax . ",";
        $contents .= $tcgst1 . ",";
        $contents .= $tsgst1 . ",";
        $contents .= $tcgst5 . ",";
        $contents .= $tsgst5 . ",";
        $contents .= $tcgst12 . ",";
        $contents .= $tsgst12 . ",";
        $contents .= $tcgst18 . ",";
        $contents .= $tsgst18 . ",";
        $contents .= $tcgst28 . ",";
        $contents .= $tsgst28 . ",";
        $contents .= $tigst1 . ",";
        $contents .= $tigst5 . ",";
        $contents .= $tigst12 . ",";
        $contents .= $tigst18 . ",";
        $contents .= $tigst28 . "\n";
        $contents .=  "\n";

        $contents .= "Sr., Voucher, Date, Product Code, Product Name, Gram, Qty., Rate, Amount, ";
        $contents .= "Discount, Taxable Amt, CGST 0.05%, SGST 0.05%,  CGST 2.5%, SGST 2.5%, ";
        $contents .= "CGST 6%, SGST 6%, CGST 9%, SGST 9%, CGST 14%, SGST 14%,";
        $contents .= " IGST 0.1%, IGST 5%, IGST 12%, ";
        $contents .= " IGST 18%,IGST 28%\n";
        
        $contents = strip_tags($contents);

        if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
            header("Content-Disposition: attachment;filename = $formname" . "_Summary" . date('d-m-Y') . ".csv");
            print $contents;
        } else {
            header("Content-Disposition: attachment;filename = $formname" . "_Detail" . date('d-m-Y') . ".csv");
            print $contents;
        }
    }
}
?>