<?php
require_once("../config/config.php");
$docid = base64_decode($_REQUEST['id']);
 $po_sql = "SELECT * FROM purchase_order_master where po_no='" . $docid . "'";
$po_res = mysqli_query($link1, $po_sql);
$po_row = mysqli_fetch_assoc($po_res);
///// get parent location details
$parentloc = getLocationDetails($po_row['po_to'], "name,city,state,addrs,disp_addrs,margin", $link1);
$parentlocdet = explode("~", $parentloc);
///// get child location details
$childloc = getLocationDetails($po_row['po_from'], "name,city,state,addrs,disp_addrs", $link1);
$childlocdet = explode("~", $childloc);
@extract($_POST);


////// if we hit process button
if ($_POST) {
    if ($_POST['upd'] == 'Process') {
        if ($total_qty != '' && $total_qty != 0) {
            //// Make System generated Invoice no.//////
            $res_cnt = mysqli_query($link1, "select inv_str,inv_counter from document_counter where location_code='" . $po_row['po_to'] . "'");
            if (mysqli_num_rows($res_cnt)) {
                $row_cnt = mysqli_fetch_array($res_cnt);
                $invcnt = $row_cnt['inv_counter'] + 1;
                $pad = str_pad($invcnt, 4, 0, STR_PAD_LEFT);
                $invno = $row_cnt['inv_str'] . $pad;
                mysqli_autocommit($link1, false);
                $flag = true;
                $err_msg = "";
                ///// Insert Master Data
                $splitcompltetax = explode("~", $complete_tax);
                if ($delivery_address) {
                    $deli_addrs = $delivery_address;
                } else {
                    $deli_addrs = $childlocdet[4];
                }
                 $query1 = "INSERT INTO billing_master set from_location='" . $po_row['po_to'] . "',to_location='" . $po_row['po_from'] . "',challan_no='" . $invno . "',po_no='" . $po_row['po_no'] . "',ref_no='" . $ref_no . "',sale_date='" . $today . "',entry_date='" . $today . "',entry_time='" . $currtime . "',type='CORPORATE',document_type='INVOICE',discountfor='PD',taxfor='" . $taxfor . "',status='Pending',entry_by='" . $_SESSION['userid'] . "',basic_cost='" . $sub_total . "',discount_amt='" . $total_discount . "',tax_cost='" . $tax_amount . "',total_sgst_amt='" . $total_sgst . "',total_cgst_amt='" . $total_cgst . "',total_igst_amt='" . $total_igst . "',total_cost='" . $grand_total . "',tax_type='" . $splitcompltetax[1] . "',tax_header='" . $splitcompltetax[2] . "',tax='" . $splitcompltetax[0] . "',bill_from='" . $po_row['po_to'] . "',bill_topty='" . $po_row['po_from'] . "',from_addrs='" . $parentlocdet[3] . "',disp_addrs='" . $parentlocdet[4] . "',to_addrs='" . $childlocdet[3] . "',deliv_addrs='" . $deli_addrs . "',billing_rmk='" . $remark . "' , margin = '".$margin_amt."' ";			
                $result = mysqli_query($link1, $query1);
                //// check if query is not executed
                if (!$result) {
                    $flag = false;
                    $err_msg = "Error Code1:";
                }
                /// update invoice counter /////
                $result = mysqli_query($link1, "update document_counter set inv_counter=inv_counter+1,update_by='" . $_SESSION['userid'] . "',updatedate='" . $datetime . "' where location_code='" . $po_row['po_to'] . "'");
                //// check if query is not executed
                if (!$result) {
                    $flag = false;
                    $err_msg = "Error Code2:";
                }
                ////// pick purchase order details
                $po_datares = mysqli_query($link1, "select * from purchase_order_data where po_no='" . $po_row['po_no'] . "'");
                while ($po_datarow = mysqli_fetch_array($po_datares)) {
                    $rowprodcut = "prodcode" . $po_datarow['id'];
                    $rowqty = "bill_qty" . $po_datarow['id'];
                    $rowprice = "price" . $po_datarow['id'];
                    $rowvalue = "value" . $po_datarow['id'];
                    $rowtotal = "linetotal" . $po_datarow['id'];
                    $rowmrp = "mrp" . $po_datarow['id'];
                    $rowholdrate = "holdRate" . $po_datarow['id'];
                    $rowsgstPer = "sgst_per" . $po_datarow['id'];
                    $rowsgstAmt = "sgst_amt" . $po_datarow['id'];
                    $rowcgstPer = "cgst_per" . $po_datarow['id'];
                    $rowcgstAmt = "cgst_amt" . $po_datarow['id'];
                    $rowigstPer = "igst_per" . $po_datarow['id'];
                    $rowigstAmt = "igst_amt" . $po_datarow['id'];
                    $rowdisc = "rowdiscount" . $po_datarow['id'];
                    $rowtotalval = "total_val" . $po_datarow['id'];
                    //checking row value of product and qty should not be blank
                    $getstk = getCurrentStock($po_row['po_to'], $_POST[$rowprodcut], "okqty", $link1);
                    //// check stock should be available ////
                    if ($getstk < $_POST[$rowqty]) {
                        $flag = false;
                        $err_msg = "Error Code3: Stock is not available";
                    } else {
                        
                    }
                    if ($_POST[$rowprodcut] != '' && $_POST[$rowqty] != '' && $_POST[$rowqty] != 0) {
                        /////////// insert data
                        $splitrowtax = explode("~", $_POST[$rowtaxtype]);
                        $query2 = "insert into billing_model_data set from_location='" . $po_row['po_to'] . "',challan_no='" . $invno . "', prod_code='" . $_POST[$rowprodcut] . "',prod_cat='" . $po_datarow['prod_cat'] . "', qty='" . $_POST[$rowqty] . "', mrp='" . $_POST[$rowmrp] . "', price='" . $_POST[$rowprice] . "', hold_price='" . $_POST[$rowholdrate] . "', value='" . $_POST[$rowvalue] . "',tax_name='" . $splitrowtax[1] . "',tax_per='" . $splitrowtax[0] . "',tax_amt='" . $_POST[$rowtaxamt] . "',discount='" . $_POST[$rowdisc] . "',sgst_per='" . $_POST[$rowsgstPer] . "',sgst_amt='" . $_POST[$rowsgstAmt] . "',cgst_per='" . $_POST[$rowcgstPer] . "',cgst_amt='" . $_POST[$rowcgstAmt] . "',igst_per='" . $_POST[$rowigstPer] . "',igst_amt='" . $_POST[$rowigstAmt] . "', totalvalue='" . $_POST[$rowtotalval] . "',sale_date='" . $today . "',entry_date='" . $today . "'";
                        $result = mysqli_query($link1, $query2);
                        //// check if query is not executed
                        if (!$result) {
                            $flag = false;
                            $err_msg = "Error Code4:";
                        }
                        //// update stock of from loaction
                       $result = mysqli_query($link1, "update stock_status set okqty=okqty-'" . $_POST[$rowqty] . "',updatedate='" . $datetime . "' where asc_code='" . $po_row['po_to'] . "' and partcode='" . $_POST[$rowprodcut] . "'");
                        //// check if query is not executed
                        if (!$result) {
                            $flag = false;
                            $err_msg = "Error Code5:";
                        }
                        ///// update stock ledger table
                       $flag = stockLedger($invno, $today, $_POST[$rowprodcut], $po_row['po_to'], $po_row['po_from'], $po_row['po_to'], "OUT", "OK", "Corporate Invoice", $_POST[$rowqty], $_POST[$rowprice], $_SESSION['userid'], $today, $currtime, $ip, $link1, $flag);
                        ////// release the PO qty in stock ////
                        $flag = releaseStockQty($po_row['po_to'], $_POST[$rowprodcut], $po_row['req_qty'], $link1, $flag);
                    }// close if loop of checking row value of product and qty should not be blank
                    //// update details in po table
                   $result = mysqli_query($link1, "update purchase_order_data set qty=qty+'" . $_POST[$rowqty] . "' where id='" . $po_datarow['id'] . "'");
                    //// check if query is not executed
                    if (!$result) {
                        $flag = false;
                        $err_msg = "Error Code6:";
                    }
                }/// close while loop
                ///// update invoice and date in po master details
              $result = mysqli_query($link1, "update purchase_order_master set status='Processed',dispatch_challan='" . $invno . "',challan_date='" . $today . "' where po_no='" . $po_row['po_no'] . "'");
                //// check if query is not executed
                if (!$result) {
                    $flag = false;
                    $err_msg = "Error Code7:";
                }
                //// update cr bal of child location
               $result = mysqli_query($link1, "update current_cr_status set cr_abl=cr_abl-'" . $grand_total . "',total_cr_limit=total_cr_limit-'" . $grand_total . "', last_updated='" . $datetime . "' where parent_code='" . $po_row['po_to'] . "' and asc_code='" . $po_row['po_from'] . "'");
                //// check if query is not executed
                if (!$result) {
                    $flag = false;
                    $err_msg = "Error Code8:";
                }
                ////// maintain party ledger////
             $flag = partyLedger($po_row['po_to'], $po_row['po_from'], $invno, $today, $today, $currtime, $_SESSION['userid'], "CORPORATE INVOICE", $grand_total, "DR", $link1, $flag);				
				//////////////
				
				if($_POST['margin_amt'] != '0' || $_POST['margin_amt'] != '0.00') {
				 ////// maintain party ledger for margin////
               $flag = partyLedger($po_row['po_to'], $po_row['po_from'], $invno, $today, $today, $currtime, $_SESSION['userid'], "Margin Against Invoice", $grand_total, "CR", $link1, $flag);	
			  }	
			  ///////////////////////////////////////////////////	
				
				
                ////// insert in activity table////
               $flag = dailyActivity($_SESSION['userid'], $invno, "CORPORATE INVOICE", "CREATE", $ip, $link1, $flag);
                ///// check both master and data query are successfully executed
                if ($flag) {
                    mysqli_commit($link1);
                    $msg = "Invoice is successfully created with ref. no. " . $invno;
                } else {
                    mysqli_rollback($link1);
                    $msg = "Request could not be processed " . $err_msg . ". Please try again.";
                }
                mysqli_close($link1);
            } else {
                $msg = "Request could not be processed invoice series not found. Please try again.";
            }
        } else {
            $msg = "Request could not be processed . Please dispatch some qty.";
        }
        ///// move to parent page
        header("location:corporateInvoice.php?msg=" . $msg . "" . $pagenav);
       exit;
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title><?= siteTitle ?></title>
        <script src="../js/jquery.min.js"></script>
        <link href="../css/font-awesome.min.css" rel="stylesheet">
        <link href="../css/abc.css" rel="stylesheet">
        <script src="../js/bootstrap.min.js"></script>
        <link href="../css/abc2.css" rel="stylesheet">
        <link rel="stylesheet" href="../css/bootstrap.min.css">

        <link rel="stylesheet" href="../css/jquery.dataTables.min.css">
        <script type="text/javascript" src="../js/jquery.dataTables.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#myTable').dataTable();
            });
            $(document).ready(function() {
                $("#frm2").validate();
            });
        </script>
        <script src="../js/frmvalidate.js"></script>
        <script type="text/javascript" src="../js/jquery.validate.js"></script>
        <script type="text/javascript" src="../js/common_js.js"></script>
        <script type="text/javascript">
            /////// calculate line total /////////////
            function rowTotal(ind) {
                var ent_qty = "bill_qty" + ind + "";
                var ent_rate = "price" + ind + "";
                var ent_value = "value" + ind + "";
                var hold_rate = "holdRate" + ind + "";
                var po_price = "poprice" + ind + "";
                var availableQty = "avl_stock" + ind + "";
                var prodmrpField = "mrp" + ind + "";
                var discountField = "rowdiscount" + ind + "";
                var ent_rowdisval = "rowdiscount_val" + ind + "";
                var sgst_per = "sgst_per" + ind + "";
                var cgst_per = "cgst_per" + ind + "";
                var igst_per = "igst_per" + ind + "";
                var sgst_amt = "sgst_amt" + ind + "";
                var cgst_amt = "cgst_amt" + ind + "";
                var igst_amt = "igst_amt" + ind + "";
                var rowtax = "taxType" + ind + "";
                var totalvalField = "total_val" + ind + "";
                var holdRate = document.getElementById(hold_rate).value;
                ////// check if entered qty is something
                if (document.getElementById(ent_qty).value) {
                    var qty = document.getElementById(ent_qty).value;
                } else {
                    var qty = 0;
                }
                /////  check if entered price is somthing
                if (document.getElementById(ent_rate).value) {
                    var price = document.getElementById(ent_rate).value;
                } else {
                    var price = 0.00;
                }
                /////  check if entered value is somthing
                if (document.getElementById(ent_value).value) {
                    var value = document.getElementById(ent_value).value;
                } else {
                    var value = 0.00;
                }
                ///// check if discount value is something
                if (document.getElementById(discountField).value) {
                    var dicountval = document.getElementById(discountField).value;
                } else {
                    var dicountval = 0.00;
                }
                /////  check if entered price is somthing
                if (document.getElementById(sgst_per).value) {
                    var sgst = document.getElementById(sgst_per).value;
                } else {
                    var sgst = 0.00;
                }
                /////  check if entered price is somthing
                if (document.getElementById(cgst_per).value) {
                    var cgst = document.getElementById(cgst_per).value;
                } else {
                    var cgst = 0.00;
                }
                ///// check if discount value is something
                if (document.getElementById(igst_per).value) {
                    var igst = document.getElementById(igst_per).value;
                } else {
                    var igst = 0.00;
                }
                ///// check if row wise tax is something
                //  if(document.getElementById(rowtax).value){ var expldtax=(document.getElementById(rowtax).value).split("~"); var rowtaxval=expldtax[0];}else{ var rowtaxval=0.00; }
                ////// check entered qty should be available
                if (parseFloat(qty) <= parseFloat(document.getElementById(availableQty).value)) {
                    if (parseFloat(price) >= parseFloat(dicountval)) {
                        var value = parseFloat(qty) * parseFloat(price);
                        var discost = parseFloat(value) - (parseFloat(qty) * parseFloat(dicountval));
                        var sgst_amt1 = discost * sgst / 100;
                        var cgst_amt1 = discost * cgst / 100;
                        var igst_amt1 = discost * igst / 100;
                        var linetot = parseFloat(discost + sgst_amt1 + cgst_amt1 + igst_amt1);
                        //var var3="linetotal"+ind+"";    
                        //document.getElementById(var3).value=formatCurrency(total);
                        document.getElementById(ent_value).value = formatCurrency(value);
                        document.getElementById(ent_rowdisval).value = formatCurrency(discost);
                        document.getElementById(sgst_amt).value = formatCurrency(sgst_amt1);
                        document.getElementById(cgst_amt).value = formatCurrency(cgst_amt1);
                        document.getElementById(igst_amt).value = formatCurrency(igst_amt1);
                        document.getElementById(totalvalField).value = formatCurrency(linetot);
                        ///// calculate row wise tax
                        // var taxamount=(parseFloat(totalcost)*parseFloat(rowtaxval))/100;
                        // document.getElementById("rowtaxamount"+ind).value=formatCurrency(taxamount);
                        ///// line total
                        // document.getElementById(totalvalField).value=formatCurrency(parseFloat(totalcost)+parseFloat(taxamount));
                        calculatetotal();
                    } else {
                        alert("Discount is exceeding from price");
                        var total = parseFloat(qty) * parseFloat(price);
                        var var3 = "linetotal" + ind + "";
                        document.getElementById(var3).value = formatCurrency(total);
                        document.getElementById(discountField).value = "0.00";
                        document.getElementById(totalvalField).value = formatCurrency(total);
                        calculatetotal();
                    }
                } else if (parseFloat(document.getElementById(availableQty).value) == '0.00') {
                    alert("Stock is not Available");
                    document.getElementById(ent_qty).value = "";
                    //document.getElementById(availableQty).value="";
                    document.getElementById(ent_rate).value = document.getElementById(po_price).value;
                }
                else {
                    alert("Stock is not Available");
                    document.getElementById(ent_qty).value = "";
                    //document.getElementById(availableQty).value="";
                    document.getElementById(ent_rate).value = document.getElementById(po_price).value;
                }
            }
            ////// calculate final value of form /////
            function calculatetotal() {
                var rowno = (document.getElementById("rowno").value);
                var sum_qty = 0;
                var sum_subtot = 0;
                var sum_discount = 0;
                var sum_grandtot = 0;
                var temp_value = 0.00;
                var sum_discount = 0.00;
                var temp_total_val = 0.00;
                for (var i = 1; i < rowno; i++) {
                    var temp_qty = "bill_qty" + i + "";
                    var temp_value = "value" + i + "";
                    var temp_discount = "rowdiscount" + i + "";
                    var temp_total_val = "total_val" + i + "";
                    var discountvar = 0.00;
                    var totalamt = 0.00;
                    var totalvalue = 0.00;
                    var totqty = 0;
                    ///// check if discount value is something
                    if (document.getElementById(temp_discount).value) {
                        discountvar = document.getElementById(temp_discount).value;
                    } else {
                        discountvar = 0.00;
                    }
                    ///// check if line total value is something
                    if (document.getElementById(temp_value).value) {
                        totalvalue = document.getElementById(temp_value).value;
                    } else {
                        totalvalue = 0.00;
                    }
                    ///// check if line qty is something
                    if (document.getElementById(temp_qty).value) {
                        totqty = document.getElementById(temp_qty).value;
                    } else {
                        totqty = 0;
                    }
                    ///// check if line tax value is something
                    if (document.getElementById(temp_total_val).value) {
                        totalamt = document.getElementById(temp_total_val).value;
                    } else {
                        totalamt = 0.00;
                    }

                    sum_qty += parseFloat(totqty);
                    sum_subtot += parseFloat(totalvalue);
                    sum_discount += parseFloat(discountvar) * parseFloat(totqty);
                    sum_grandtot += parseFloat(totalamt);

                }/// close for loop
               // alert(sum_subtot)
                document.getElementById("total_qty").value = sum_qty;
                document.getElementById("sub_total").value = formatCurrency(sum_subtot);
                document.getElementById("total_discount").value = formatCurrency(sum_discount);
                document.getElementById("grand_total").value = formatCurrency(sum_grandtot);
                
                //    <?php if ($_REQUEST['tax_type'] == "PT") { ?>	
                    //    document.getElementById("tax_amount").value=formatCurrency(sum_tax);
                    //    <?php } ?>
               // document.getElementById("grand_total").value = formatCurrency(parseFloat(sum_total) - parseFloat(sum_discount) + parseFloat(sum_tax));
            }
            ///// check total tax of all selling product
            function check_total_tax() {
                if (document.getElementById("complete_tax").value) {
                    var splittax = (document.getElementById("complete_tax").value).split("~");
                    var completeTax = splittax[0];
                } else {
                    var completeTax = 0.00;
                }
                var calculateTax = (parseFloat(completeTax) * (parseFloat(document.getElementById("sub_total").value) - parseFloat(document.getElementById("total_discount").value))) / 100;
                document.getElementById("tax_amount").value = formatCurrency(calculateTax);
                document.getElementById("grand_total").value = formatCurrency(parseFloat(document.getElementById("sub_total").value) - parseFloat(document.getElementById("total_discount").value) + parseFloat(calculateTax));
            }
        </script>
    </head>
    <body onKeyPress="return keyPressed(event);">
        <div class="container-fluid">
            <div class="row content">
                <?php
                include("../includes/leftnav2.php");
                ?>
                <div class="col-sm-9">
                    <h2 align="center"><i class="fa fa-user-circle-o"></i> Corporate Invoicing against PO</h2><br/>
                    <div class="panel-group">
                        <form id="frm1" name="frm1" class="form-horizontal" action="" method="post">
                            <div class="panel panel-default table-responsive">
                                <div class="panel-heading">Party Information</div>
                                <div class="panel-body">
                                    <table class="table table-bordered" width="100%">
                                        <tbody>
                                            <tr>
                                                <td width="20%"><label class="control-label">Purchase Order To</label></td>
                                                <td width="30%"><?php echo str_replace("~", ",", $parentloc); ?></td>
                                                <td width="20%"><label class="control-label">Purchase Order From</label></td>
                                                <td width="30%"><?php echo str_replace("~", ",", $childloc); ?></td>
                                            </tr>
                                            <tr>
                                                <td><label class="control-label">Purchase Order No.</label></td>
                                                <td><?php echo $po_row['po_no']; ?></td>
                                                <td><label class="control-label">Purchase Order Date</label></td>
                                                <td><?php echo $po_row['requested_date']; ?></td>
                                            </tr>
                                            <tr>
                                                <td><label class="control-label">Entry By</label></td>
                                                <td><?php echo getAdminDetails($po_row['create_by'], "name", $link1); ?></td>
                                                <td><label class="control-label">Status</label></td>
                                                <td><?php echo $po_row['status']; ?></td>
                                            </tr>
                                            <tr>
                                                <td><label class="control-label">Discount Type</label></td>
                                                <td><?php echo getDiscountType($po_row['discount_type']); ?></td>
                                                <td>
                                                    <!--                    <label class="control-label">Tax Type</label>-->
                                                </td>
                                                <td>
                                <!--                    <select name="tax_type" id="tax_type" required class="form-control required" style="width:200px;" onChange="document.frm1.submit();">
                                                  <option value="NONE"<?php if ($_REQUEST['tax_type'] == "NONE") echo "selected"; ?>>NONE</option>
                                                  <option value="PT"<?php if ($_REQUEST['tax_type'] == "PT") echo "selected"; ?>>Productwise Tax</option>
                                                  <option value="TT"<?php if ($_REQUEST['tax_type'] == "TT") echo "selected"; ?>>Total Tax</option>
                                                </select>-->
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div><!--close panel body-->
                            </div><!--close panel-->
                        </form>
                        <form id="frm2" name="frm2" class="form-horizontal" action="" method="post">
                            <div class="panel panel-default table-responsive">
                                <div class="panel-heading">Items Information</div>
                                <div class="panel-body">
                                    <table class="table table-bordered" width="100%">
                                        <thead>
                                            <tr>
                                                <th style="text-align:center" width="5%">#</th>
                                                <th style="text-align:center" width="15%">Product</th>
                                                <th style="text-align:center" width="10%">Req. Qty</th>
                                                <th style="text-align:center" width="10%">Bill Qty</th>
                                                <th style="text-align:center" width="10%">Price</th>
                                                <th style="text-align:center" width="10%">Value</th>
                                                <th style="text-align:center" width="10%">Discount/<br>Unit</th>
                                                <th style="text-align:center" width="10%">After Discount Value</th>
                                                <th style="text-align:center" width="10%">SGST<br>(%)</th>
                                                <th style="text-align:center" width="10%">SGST <br>Amt</th>
                                                <th style="text-align:center" width="10%">CGST<br>(%)</th>
                                                <th style="text-align:center" width="10%">CGST <br>Amt</th>
                                                <th style="text-align:center" width="10%">IGST<br>(%)</th>
                                                <th style="text-align:center" width="10%">IGST <br>Amt</th>
                                                <th style="text-align:center" width="10%">Total</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $i = 1;
                                            $totqty = 0;
                                            $sgst_per = 0;
                                            $cgst_per = 0;
                                            $igst_per = 0;
                                            $sgst_amt = 0;
                                            $cgst_amt = 0;
                                            $igst_amt = 0;
                                            $totsgst = 0;
                                            $totcgst = 0;
                                            $totigst = 0;
                                            $totdiscount = 0;
                                            $subtotal = 0;
                                            $grandtot = 0;
                                            $podata_sql = "SELECT * FROM purchase_order_data where po_no='" . $docid . "'";
                                            $podata_res = mysqli_query($link1, $podata_sql);
                                            while ($podata_row = mysqli_fetch_assoc($podata_res)) {
                                                //// get current available stock
                                                $hsn_code = mysqli_fetch_assoc(mysqli_query($link1, "select hsn_code,productname,productcolor from product_master where productcode='" . $podata_row['prod_code'] . "'"));
                                                $gst_data = mysqli_fetch_assoc(mysqli_query($link1, "select sgst,cgst,igst from tax_hsn_master where hsn_code='" . $hsn_code['hsn_code'] . "'"));
                                              
                                                if ($childlocdet['2'] == $parentlocdet['2']) {
                                                    $sgst_per = $gst_data['sgst'];
                                                    $cgst_per = $gst_data['cgst'];
                                                    $sgst_amt = number_format((($podata_row['totalval'] * $sgst_per) / 100), '2', '.', '');
                                                    $cgst_amt = number_format((($podata_row['totalval'] * $cgst_per) / 100), '2', '.', '');
                                                    $linetotal = $podata_row['totalval'] + $sgst_amt + $cgst_amt;
                                                } else {
                                                    $igst_per = $gst_data['igst'];
                                                    $igst_amt = number_format((($podata_row['totalval'] * $igst_per) / 100), '2', '.', '');
                                                    $linetotal = $podata_row['totalval'] + $igst_amt;
                                                }
                                                $discount_val = number_format(($podata_row['po_value'] - ($podata_row['req_qty'] * $podata_row['discount'])), '2', '.', '');
                                                $avlstock = getCurrentStock($po_row['po_to'], $podata_row['prod_code'], "okqty", $link1);
                                                ?>
                                                <tr>
                                                    <td><?= $i ?></td>
                                                    <td><?=$hsn_code['productname'].' | '.$hsn_code['productcolor'].' | '.$podata_row['prod_code']?><input type="hidden" name="prodcode<?= $podata_row['id'] ?>" id="prodcode<?= $i ?>" value="<?= $podata_row['prod_code'] ?>"></td>
                                                    <td style="text-align:right">
                                                        <input type="hidden" name="avl_stock<?= $podata_row['id'] ?>" id="avl_stock<?= $i ?>" value="<?= $avlstock ?>">
                                                        <input name="mrp<?= $podata_row['id'] ?>" id="mrp<?= $i ?>" type="hidden" value="<?= $podata_row['mrp'] ?>"/>
                                                        <input name="holdRate<?= $podata_row['id'] ?>" id="holdRate<?= $i ?>" type="hidden" value="<?= $podata_row['hold_price'] ?>"/>
                                                        <input type="hidden" name="req_qty<?= $podata_row['id'] ?>" id="req_qty<?= $i ?>" value="<?= $podata_row['req_qty'] ?>">
                                                        <?= $podata_row['req_qty'] ?>
                                                    </td>
                                                    <td><input type="text" class="form-control digits" name="bill_qty<?= $podata_row['id'] ?>" id="bill_qty<?= $i ?>" value="<?=(int)$podata_row['req_qty']?>" autocomplete="off" required onBlur="myFunction(this.value, 'none', 'bill_qty<?= $i ?>');
                                                            rowTotal('<?= $i ?>');" onKeyPress="return onlyNumbers(this.value);" style="width:45px;text-align:right;padding: 5px;"></td>
                                                    <td><input type="text" class="form-control" name="price<?= $podata_row['id'] ?>" id="price<?= $i ?>" onBlur="rowTotal('<?= $i ?>');" autocomplete="off" onKeyPress="return onlyFloatNum(this.value);" required value="<?= $podata_row['po_price'] ?>" style="width:72px;text-align:right;padding: 5px;"><input name="poprice<?= $podata_row['id'] ?>" id="poprice<?= $i ?>" type="hidden" value="<?= $podata_row['po_price'] ?>"/></td>
                                                    <td><input type="text" class="form-control" name="value<?= $podata_row['id'] ?>" id="value<?= $i ?>" autocomplete="off" readonly value="<?= $podata_row['po_value'] ?>" style="width:72px;text-align:right;padding: 5px;"></td>
                                                    <td><input type="text" class="form-control" name="rowdiscount<?= $podata_row['id'] ?>" id="rowdiscount<?= $i ?>" onKeyPress="return onlyFloatNum(this.value);" autocomplete="off"  onblur="rowTotal('<?= $i ?>');" value="<?= $podata_row['discount'] ?>" style="width:72px;text-align:right;padding: 5px;"></td>
                                                    <td><input type="text" class="form-control" name="rowdiscount_val<?= $podata_row['id'] ?>" id="rowdiscount_val<?= $i ?>" autocomplete="off" readonly value="<?= $discount_val ?>" style="width:72px;text-align:right;padding: 5px;"></td>
                                                    <td><input type="text" class="form-control" name="sgst_per<?= $podata_row['id'] ?>" id="sgst_per<?= $i ?>" readonly value="<?= $sgst_per ?>" style="width:50px;text-align:right;padding: 5px;"></td>
                                                    <td><input type="text" class="form-control" name="sgst_amt<?= $podata_row['id'] ?>" id="sgst_amt<?= $i ?>" readonly value="<?= $sgst_amt ?>" style="width:65px;text-align:right;padding: 5px;"></td>
                                                    <td><input type="text" class="form-control" name="cgst_per<?= $podata_row['id'] ?>" id="cgst_per<?= $i ?>" readonly value="<?= $cgst_per ?>" style="width:50px;text-align:right;padding: 5px;"></td>
                                                    <td><input type="text" class="form-control" name="cgst_amt<?= $podata_row['id'] ?>" id="cgst_amt<?= $i ?>" readonly value="<?= $cgst_amt ?>" style="width:65px;text-align:right;padding: 5px;"></td>
                                                    <td><input type="text" class="form-control" name="igst_per<?= $podata_row['id'] ?>" id="igst_per<?= $i ?>" readonly value="<?= $igst_per ?>" style="width:50px;text-align:right;padding: 5px;"></td>
                                                    <td><input type="text" class="form-control" name="igst_amt<?= $podata_row['id'] ?>" id="igst_amt<?= $i ?>" readonly value="<?= $igst_amt ?>" style="width:65px;text-align:right;padding: 5px;"></td>
                                                    <td><input type="text" class="form-control" name="total_val<?= $podata_row['id'] ?>" id="total_val<?= $i ?>" autocomplete="off" readonly value="<?= number_format($linetotal, '2', '.', '') ?>" style="width:75px;text-align:right;padding: 5px;"></td>
                                                </tr>
                                                <?php
                                                $totqty+=$podata_row['req_qty'];
                                                $subtotal+=$podata_row['po_value'];                                                
                                                $grandtot+=$linetotal;
                                                $totsgst+=$sgst_amt;
                                                $totcgst+=$cgst_amt;
                                                $totigst+=$igst_amt;
                                                $totdiscount+=($podata_row['discount']*$podata_row['req_qty']);
                                                $i++;
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                </div><!--close panel body-->
                            </div><!--close panel-->
                            <div class="panel panel-default table-responsive">
                                <div class="panel-heading">Amount Information</div>
                                <div class="panel-body">
                                    <table class="table table-bordered" width="100%">
                                        <tbody>
                                            <tr>
                                                <td width="20%"><label class="control-label">Total Qty</label></td>
                                                <td width="30%"><input type="text" name="total_qty" id="total_qty" class="form-control" value="<?= $totqty ?>" readonly style="width:200px;"/></td>
                                                <td width="20%"><label class="control-label">Sub Total</label></td>
                                                <td width="30%"><input type="text" name="sub_total" id="sub_total" class="form-control" value="<?= currencyFormat($subtotal) ?>" readonly style="width:200px;text-align:right"/></td>
                                            </tr>
                                            <tr>
                                                <td><label class="control-label">Total SGST</label></td>
                                                <td><input type="text" name="total_sgst" id="total_sgst" class="form-control" value="<?= $totsgst ?>" readonly style="width:200px;"/></td>
                                                <td><label class="control-label">Discount</label></td>
                                                <td><input type="text" name="total_discount" id="total_discount" class="form-control" value="<?=$totdiscount?>" style="width:200px;text-align:right" onKeyPress="return onlyFloatNum(this.value);" onKeyUp="check_total_tax();"/></td>
                                            </tr>
                                            <tr>
                                                <td><label class="control-label">Total CGST</label></td>
                                                <td><input type="text" name="total_cgst" id="total_cgst" class="form-control" value="<?= $totcgst ?>" readonly style="width:200px;"/></td>
                                                <td><label class="control-label">Grand Total</label></td>
                                                <td><input type="text" name="grand_total" id="grand_total" class="form-control" value="<?php echo currencyFormat($grandtot); ?>" readonly style="width:200px;text-align:right"/></td>
                                            </tr>
                                            <tr>
                                                <td><label class="control-label">Total IGST</label></td>
                                                <td><input type="text" name="total_igst" id="total_igst" class="form-control" value="<?= $totigst ?>" readonly style="width:200px;"/></td>
                                                <td><label class="control-label"></label></td>
                                                <td></td>
                                            </tr>
                                            <tr>
                                                <td><label class="control-label">Delivery Address</label></td>
                                                <td><textarea name="delivery_address" id="delivery_address" class="form-control required" style="resize:none; width:200px" required><?php echo $po_row['delivery_address']; ?></textarea></td>
                                                <td><label class="control-label">Remark</label></td>
                                                <td><textarea name="remark" id="remark" class="form-control" style="resize:none;width:200px" ></textarea></td>
                                            </tr>
											<tr>
                                                <td><label class="control-label">Margin Amount</label></td>
                                              <td> 
										  <?php  if($parentlocdet[5] != '') {											  
											  $margin = ($subtotal * $parentlocdet[5])/100;
											  } ?>
											  <input type="text" name="margin_amt" id="margin_amt" class="form-control" value="<?=$margin?>" readonly style="width:200px;"/></td>
                                                <td><label class="control-label"></label></td>
                                                
                                            </tr>
                                            <tr>
                                                <td colspan="4" align="center"><input type="submit" class="btn btn-primary" name="upd" id="upd" value="Process" title="Make Invoice">
                                                    <input type="hidden" name="taxfor" id="taxfor" value="<?= $_REQUEST['tax_type'] ?>"/>
                                                    <input type="hidden" name="id" id="id" value="<?= $_REQUEST['id'] ?>"/>
                                                    <input type="hidden" name="rowno" id="rowno" value="<?= $i ?>"/>
                                                    <input title="Back" type="button" class="btn btn-primary" value="Back" onClick="window.location.href = 'corporateInvoice.php?<?= $pagenav ?>'"></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div><!--close panel body-->
                            </div><!--close panel-->
                        </form>
                    </div><!--close panel group-->
                </div><!--close col-sm-9-->
            </div><!--close row content-->
        </div><!--close container-fluid-->
        <?php
        include("../includes/footer.php");
        include("../includes/connection_close.php");
        ?>
    </body>
</html>