<?php require "_header.php"; ?>
<div id="main-report" align="center">
  <center>
    <h4>Vendor Order Report </h4>
  </center>
  <?php
  $vendor_idhedden = Escape($_REQUEST["vendor_idhedden"]);
  $startdate = Escape($_POST["startdate"]);
  $enddate = Escape($_POST["enddate"]);

  if ($_POST['vendor_idhedden'] == "") {
    ?>
    <script language="javascript" type="text/javascript">
      location.href = 'addedit-customerinvoice.php';
    </script>
    <?php
  } elseif ($_SESSION['cCompany_id'] != "" && isset($_SESSION['cCompany_id'])) {
    $header_row_string = "Vendor Order Report  ";
    $totalproduct = 0;
    $finalgrandtotal = 0;

    ob_start();

    $invoicid = "select invoice_id,invoice_no,invoice_date from lms_invoice where vendor_id='" . $vendor_idhedden . "' ";

    if ($startdate != "From") {
      $startdate = DateConversion($startdate, "Y-m-d", "d/m/Y");
      $invoicid .= " AND invoice_date >= '$startdate' ";
    }

    if ($enddate != "To") {
      $enddate = DateConversion($enddate, "Y-m-d", "d/m/Y");
      $invoicid .= " AND invoice_date <= '$enddate' ";
    }

    $finalinvoiceid = dbQuery($invoicid);
    if (dbRows($finalinvoiceid) > 0) {
      $date = new Date();
      $query = "Select *
			 FROM lms_vendor
			 WHERE
			 vendor_id='" . $vendor_idhedden . "'";


      $out1 = dbQuery($query);
      while ($row1 = dbArray($out1)) {
        ?>
        <table width="80%" class="report">
          <tr>
            <td width="70%" colspan="3" rowspan="2">
              <b>
                <?php echo $row1["vendor_orgname"]; ?>
              </b>
              <br />
              <?php echo $row1["vendor_address"]; ?>
              <br />
              Phone:<?php echo $row1["vendor_phone1"]; ?>
              <br />

            </td>
            <td width="30%" style="text-align:center"><b>TIN No</b><br /><?php echo $row1["vendor_tinno"]; ?></td>
            <td width="30%" style="text-align:center"><b>CST No</b><br /><?php echo $row1["vendor_cstno"]; ?></td>
          </tr>
          <tr>
            <td style="text-align:center"><b>Service Tax No</b><br /><?php echo $row1["vendor_servicetaxno"]; ?></td>
            <td style="text-align:center"><b>PAN No</b><br /><?php echo $row1["vendor_panno"]; ?></td>
          </tr>
          <?php
      }
      $finalinvoiceidrow = [];
      while ($finalinvoiceidrow = dbArray($finalinvoiceid)) {
        $sno = 1;
        $invoice_id = $finalinvoiceidrow['invoice_id'];
        ?>

          <tr>
            <td colspan="2" align="left" style="border-right:none">
            </td>
            <td colspan="3" align="right" style="border-left:none">
            </td>
          </tr>

          <tr>
            <td colspan="2" align="left" style="border-right:none">
              <div style="float:left"><b>Invoice No:</b><?php echo $finalinvoiceidrow['invoice_no']; ?> </div>
            </td>
            <td colspan="3" align="right" style="border-left:none">
              <div style="float:right"><b>Invoice
                  Date:</b><?php echo $date->format("d-M-Y", $finalinvoiceidrow['invoice_date']); ?> </div>
            </td>
          </tr>
          <tr>
            <td width="10%" style="text-align:center">Sr.No</td>
            <td width="60%" style="text-align:center">Description of Goods</td>
            <td width="10%" style="text-align:center">Quantity</td>
            <td width="10%" style="text-align:center">Rate</td>
            <td width="10%" style="text-align:center">Amount</td>
          </tr>
          <?php
          $qry = "Select lcp.product_id,lcp.product_qty,lcp.product_rate,lp.product_name
		from
			lms_invoiceproduct lcp,
			lms_productmaster lp
		Where
			lcp.product_id=lp.product_id
		And
		invoice_id='" . $invoice_id . "'";

          $finalout = dbQuery($qry);
          while ($finalrow = dbArray($finalout)) {
            //print_r($finalrow); 
            $Amount = $finalrow["product_qty"] * $finalrow["product_rate"];
            $totalproduct += $Amount;
            ?>
            <tr>
              <td style="text-align:center"><?php echo $sno; ?></td>
              <td><?php echo $finalrow["product_name"]; ?></td>
              <td style="text-align:center"><?php echo $finalrow["product_qty"]; ?></td>
              <td style="text-align:center"><?php echo $finalrow["product_rate"]; ?></td>
              <td style="text-align:right"><?php echo number_format($Amount, 2); ?></td>
            </tr>
            <?php $sno++;
          } ?>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td style="text-align:right"><?php echo number_format($totalproduct, 2); ?></td>
          </tr>
          <?php

          $qry = "Select lt.tax_name,lct.tax_percentage
	 	FROM
			lms_invoicetax lct,
			lms_taxmaster lt
		WHERE
			lt.tax_id= lct.tax_id
		AND
			invoice_id='" . $invoice_id . "'";

          $finalTax = dbQuery($qry);
          while ($finalTaxrow = dbArray($finalTax)) {
            //print_r($finalTaxrow); 
            $taxamount = (($totalproduct * $finalTaxrow["tax_percentage"]) / 100);
            $totaltax += $taxamount;
            ?>
            <tr>
              <td></td>
              <td style="text-align:right"><?php echo $finalTaxrow["tax_name"]; ?></td>
              <td></td>
              <td style="text-align:right"><?php echo $finalTaxrow["tax_percentage"]; ?>%</td>
              <td style="text-align:right"><?php echo number_format($taxamount, 2); ?></td>
            </tr>
            <?php
          }
          $grandtotal = $totaltax + $totalproduct; ?>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td style="text-align:right"><b>Total</b></td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td style="text-align:right"><?php echo number_format($grandtotal, 2); ?></td>
          </tr>

          <?php $finalgrandtotal += $grandtotal;
      } ?>

        <tr>

        <tr>
          <td colspan="5">&nbsp;</td>
        </tr>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td style="text-align:right;color:#F00"><b>Grand Total</b></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td style="text-align:right;color:#F00"><?php echo number_format($finalgrandtotal, 2); ?></td>
        </tr>
      </table>
      <?php

      $HtmlCode = ob_get_contents();
      ob_end_flush();

      $HtmlUrl = "report-form-customer-invoice" . $_SESSION["cUserId"] . ".html";
      $HtmlPath = "report/" . $HtmlUrl;
      createHTMLReportPage($header_row_string, $HtmlUrl, $HtmlPath, $HtmlCode);

      ?>
      <br />
      <form name="frm" id="frm" action="" method="post">
        <input type="hidden" name="txtFile" id="txtFile" value="<?php echo $HtmlUrl; ?>" />
        <input type="button" name="btnPdf" id="btnPdf" value="Print" onClick="SetPageAction('PRINT');" />
        <input type="button" name="btnPdf" id="btnPdf" value="Create PDF" onclick="SetPageAction('PDF');" />
        <input type="button" name="btnHtml" id="btnHtml" value="Create HTML" onclick="SetPageAction('HTML');" />
        <input type="button" name="btnWord" id="btnWord" value="Create WORD" onclick="SetPageAction('WORD');" />
        <input type="button" name="btnExcel" id="btnExcel" value="Create EXCEL" onclick="SetPageAction('Excel');" />
      </form>
      <script language="javascript" type="text/javascript">
        function SetPageAction(varAction) {
          if (varAction == "PRINT") {
            document.frm.action = "print/create-print.php";
            document.frm.target = "_blank";
            document.frm.submit();
          }
          if (varAction == "PDF") {
            document.frm.action = "convertpdf/create-pdf.php";
            document.frm.target = "_blank";
            document.frm.submit();
          }
          else if (varAction == "HTML") {
            document.frm.action = "<?php echo $HtmlPath; ?>";
            document.frm.target = "_blank";
            document.frm.submit();
          }
          else if (varAction == "WORD") {
            document.frm.action = "convertword/create-word.php";
            document.frm.target = "_blank";
            document.frm.submit();
          }
          else if (varAction == "Excel") {
            document.frm.action = "convertword/create-excel.php";
            document.frm.target = "_blank";
            document.frm.submit();
          }
        }
      </script>
      <br />
      <br />
      <?php
    } else {
      echo "<b>No Data Found </b>";
    }
  } else {
    echo "Session Expired";
  }
  ?>
</div>
<?php
require "_footer.php"; ?>