<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
include ("../navigation/header.php");
$page_name = "Sales-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/sales_sidebar.php");

$quote_no = $_GET['quote_no'];
	
$table_name2 = $_SESSION['db_prefix']."_quotations";
$quote_query = "SELECT * FROM $table_name2 WHERE quote_no ='".$quote_no."' ";
$quote_result = mySQL_query($quote_query);
$quote_num = mysql_num_rows($quote_result);
$cust_id  = mysql_result($quote_result, 0, "cust_id");
$quotation_date  = mysql_result($quote_result, 0, "quotation_date");
$quote_type  = mysql_result($quote_result, 0, "quote_type");
$sales_rep  = mysql_result($quote_result, 0, "seller");
$status  = mysql_result($quote_result, 0, "status");
$check_date  = mysql_result($quote_result, 0, "check_date");
if ($status == 'accepted') { $a=1;} else {$a=0;}

if ($quote_type=='Project') {
$table_name = $_SESSION['db_prefix']."_quotation_lines";
$line_query = "SELECT quote_item_text, quote_item_price  FROM $table_name WHERE quote_number = '".$quote_no."' ";
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);
}

if ($quote_type=='Cash sale') {
$table_name = $_SESSION['db_prefix']."_captured_cash_sale";
$quote_query = "SELECT sales_discount, sales_vat, transport  FROM $table_name WHERE job_no = '".$quote_no."' ";
$quote_result = mySQL_query($quote_query);
$quote_num = mysql_num_rows($quote_result);
$quote_discount = mysql_result($quote_result, 0, "sales_discount");	
$quote_vat = mysql_result($quote_result, 0, "sales_vat");	
$transport = mysql_result($quote_result, 0, "transport");	
	

$table_name = $_SESSION['db_prefix']."_cash_job_lines";
$check_query = "SELECT quote_line_id FROM $table_name WHERE quote_no = '".$quote_no."' ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);

if ($check_num>0) {
	 $table_name = $_SESSION['db_prefix']."_cash_job_lines";
}
else {
	$table_name = $_SESSION['db_prefix']."_cash_job_lines_done";
}
$line_query = "SELECT *  FROM $table_name WHERE quote_no = '".$quote_no."' ";
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);
}

$table_name3 = $_SESSION['db_prefix']."_customer";
$customer_query = "SELECT account_type, comp_name, name_2, title, bag_box, box_no, postal_place, phone, contact_1, email_1, cell_1, status FROM $table_name3 WHERE cust_id = '".$cust_id."' ";
$customer_result = mySQL_query($customer_query);
$c0 = mysql_result($customer_result, 0, "account_type");
if ($c0=="corporate") { $c1 = mysql_result($customer_result, 0, "comp_name"); } else { $c1 = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name"); }

$c2 = mysql_result($customer_result, 0, "bag_box");
$c3 = mysql_result($customer_result, 0, "box_no");
$c4 = mysql_result($customer_result, 0, "postal_place");
$c5 = mysql_result($customer_result, 0, "phone");
$c6 = mysql_result($customer_result, 0, "contact_1");
$c7 = mysql_result($customer_result, 0, "email_1");
$c8 = mysql_result($customer_result, 0, "status");
$c9 = mysql_result($customer_result, 0, "cell_1");

$customerTT = "<label class=tip_text_label>".$c1."<br>".$c2." ".$c3."<br>".$c4."<br>".$c5."<br>".$c6."<br><label class=tip_click_label>Click for details</label>";

$table_name = $_SESSION['db_prefix']."_quotation_notes";	
$notes_query = "SELECT  *   FROM $table_name WHERE quote_number = '".$quote_no."' ORDER BY line_id DESC ";
$notes_result = mySQL_query($notes_query);
$notes_num = mysql_num_rows($notes_result);

$table_name = $_SESSION['db_prefix']."_quotation_accept";
$accept_query = "SELECT accept_value, receipt_no, order_no FROM $table_name WHERE quote_no = '".$quote_no."' ORDER BY accept_id DESC LIMIT 1 ";
$accept_result = mySQL_query($accept_query);
$accept_num = mysql_num_rows($accept_result);
if ($accept_num!=0) {
		$value = mysql_result($accept_result, 0, "accept_value");
		$receipt_no = mysql_result($accept_result, 0, "receipt_no");
		$order_no = mysql_result($accept_result, 0, "order_no");
		
}
else {
	$value = '';
}
?>
<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />

<!--jscript-->
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> 

</head>

<body>
<div class="main_frame" style="height:auto; min-height:555px;">
<div class="page_head_frame">
          <table width="690" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="556"><label class="label_page_head">Quotation <?php echo $quote_type ?></label><label class="label_page_head_2"> <b><?php echo $c1 ?></b></label></td>
              <td width="124"><label style="font-size:24px; color:#FB6808;"><?php echo "Q".$quote_no ?></label></td>
            </tr>
          </table>
        </div>
        <br />
        <br />
        <br />
<br />
<br />

      <div class="page_middle_frame">
       <div class="button_inside_frame"> Quotation details  
              <?php 
		include ('../include/customer_block.php');
		echo' <br>';
		?>
       </div>
      <div class="button_inside_frame">
      <table width="650" id="table_display">
  <tr>
    <td width="138"> Quoatation date:</td>
    <td width="146"><?php echo date("d-m-Y", strtotime($quotation_date)) ?></td>
    <td colspan="2">
    <?php
	if ($status=='quoted') {echo '<font color=orange>This quote is pending. Next check with client: '.date("d-m-Y", strtotime($check_date)).'.</font>';}
	if ($status=='accepted') {echo '<font color=green>This quote is successful.</font>';}
	if ($status=='unsuccessful') {echo '<font color=red>This quote is unsuccessful.</font>';}
	?>
    </td>
    </tr>
  <tr>
    <td>Quotation for:</td>
    <td><?php echo $quote_type ?></td>
    <td width="146">Sales representative:</td>
    <td width="200"><?php echo $sales_rep ?></td>
  </tr>
</table>

     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  <br />
<br />
      </div>
      <?php
	  if ($quote_type=='Project') { ?> 
        <div class="button_inside_frame"> Pricing details<br />
          <table width="650" id="table_details_display">
            <?php 
	  $i=0;
	  $d3=0;
	  while ($i<$line_num) {
		  $d1 = mysql_result($line_result, $i, "quote_item_text");
		  $d2 = mysql_result($line_result, $i, "quote_item_price");
		  $d3 =$d3+$d2
		  ?>
            <tr>
              <td width="331"><?php echo $d1 ?></td>
              <td width="104"><div align="right"><?php echo number_format($d2, 2, '.', ',') ?></div></td>
            </tr>
            <?php
	  $i++;
	  }
	  ?>
            <tr>
              <td align="right">&nbsp;</td>
              <td align="right">&nbsp;</td>
            </tr>
            <tr>
              <td align="right"><b>Total: (excl VAT)</b></td>
              <td align="right"><b>
                <div align="right"><?php echo number_format($d3, 2, '.', ',') ?></div>
                </b></td>
            </tr>
			<?php if ($accept_num!=0) { ?>
            <tr>
              <td align="left">Quotation accepted on: P <?php echo $value ?> </td>
              <td align="right">&nbsp;</td>
            </tr>
            <tr>
              <td align="left"><?php if ($receipt_no!='') {echo 'Receipt no.: '.$receipt_no;}?><?php if ($order_no!='') {echo 'Order no.: '.$order_no;}?> </td>
              <td align="right">&nbsp;</td>
            </tr>
			<?php } ?>
          </table>
        </div>
        <?php } ?>
        
        <?php 
		if ($quote_type=='Cash sale') { ?>
        <div>
        Cash sales
        <table width="670" id="table_details_display" >
                <tr>
                  <td width="144" height="26" align="left" valign="middle">Item</td>
                  <td width="116" align="left" valign="middle">Group</td>
                  <td width="71" align="left" valign="middle">Unit</td>
                  <td width="78" align="left" valign="middle">quantity</td>
                  <td width="89" align="left" valign="middle">price</td>
                  <td width="144" align="left" valign="middle">total</td>
                </tr>
                <?php
				$i=0;
				$sub_tot=0;
				$a5_total=0;
				 while ($i<$line_num){
					 $a0 = mysql_result($line_result, $i, "quote_line_id");
					 $a1 = mysql_result($line_result, $i, "store_item_id");
					 $table_name = $_SESSION['db_prefix']."_store_items";
						$item_query = "SELECT item_name, unit, subCategory FROM $table_name WHERE store_item_id = '".$a1."' ";
						$item_result = mySQL_query($item_query);
						$a1a = mysql_result($item_result, 0, "item_name");
						$a3a = mysql_result($item_result, 0, "unit");
						$a4a = mysql_result($item_result, 0, "subCategory");
					 $a2 = mysql_result($line_result, $i, "quantity");
					 $a3 = mysql_result($line_result, $i, "price_2");
					 
					$a5=$a2*$a3;
					$a5_total=$a5_total+$a5;
					
					
					 
				 ?>
                <tr>
                  <td valign="middle"><?php echo $a1a ?></td>
                  <td><?php echo $a4a ?></td>
                  <td valign="middle"><?php echo $a3a ?></td>
                  <td valign="middle"><?php echo $a2 ?></td>
                  <td align="right" valign="middle"><?php echo $a3. "  /" .$a3a ?>&nbsp;</td>
                  <td align="right" valign="middle"><?php echo number_format($a5, 2, '.', ',')?></td>
                </tr>
                <?php 
			   // $a6_total=$a6_total+$a6;
				$i++;
				 }
				 ?>
                <tr>
                  <td colspan="3" align="left" valign="middle">&nbsp;</td>
                  <td colspan="2" align="left" valign="middle">Sub total:</td>
                  <td align="right" valign="middle"><input type="hidden" name="sub_total" id="sub_total" value="<?php echo $a5_total?>">                    <input type="text" name="sub_total_text" id="sub_total_text" class="text80" style="border:hidden; text-align:right" readonly  value="<?php echo number_format($a5_total, 2, '.', ',') ?>">                    </td>
                </tr>
                <tr>
                  <td colspan="3" align="right" valign="middle">&nbsp;<?php $discount_amount = $a5_total * ($quote_discount / 100 ) ?></td>
                  <td colspan="2" align="left" valign="middle">Discount: 
                    <?php echo $quote_discount ?>
                  %</td>
                  <td align="right" valign="middle"><input type="text" name="discount_text" id="discount_text" class="text80" style="border:hidden; text-align:right" readonly  value="<?php echo number_format($discount_amount, 2, '.', ',') ?>">                    </td>
                </tr>
                <tr>
                  <td colspan="3" align="left" valign="middle">&nbsp;<?php $sub_total = $a5_total - $discount_amount ?></td>
                  <td colspan="2" align="left" valign="middle">Sub total:</td>
                  <td align="right" valign="middle"><input type="text" name="sub_total2_text" id="sub_total2_text" class="text80" style="border:hidden; text-align:right" readonly  value=" <?php echo number_format($sub_total, 2, '.', ',') ?>"></td>
                </tr>
                <tr>
                  <td colspan="3" align="right" valign="middle">&nbsp;</td>
                  <td colspan="2" align="left" valign="middle">Transport: </td>
                  <td align="right" valign="middle"><input type="text" name="transport_text" id="transport_text" class="text60"  autocomplete="off" onfocus = "this.select();" onKeyPress="return isNumberKey(event)" onKeyUp=" this.value = this.value.replace (/(\...).*/, '$1'); document.getElementById('transport').value = document.getElementById('transport_text').value; " onBlur="calc_discount1()" style="border:hidden; text-align:right" readonly value=" <?php echo number_format($transport, 2, '.', ',') ?>" ></td>
                </tr>
                <tr>
                  <td colspan="3" align="left" valign="middle"><?php $sub_total3 = $sub_total + $transport ?></td>
                  <td colspan="2" align="left" valign="middle">Sub total:</td>
                  <td align="right" valign="middle">
                  <input type="text" name="sub_total3_text" id="sub_total3_text" class="text80" style="border:hidden; text-align:right" readonly  value="<?php echo number_format($sub_total3, 2, '.', ',') ?>"></td>
                </tr>
                <tr>
                  <td colspan="3" align="left" valign="middle">&nbsp;<?php $vat_amount = $sub_total3 * ($quote_vat/100) ?></td>
                  <td colspan="2" align="left" valign="middle">VAT @ <?php echo $quote_vat ?>%                  </td>
                  <td align="right" valign="middle"><input type="text" name="vat_text" id="vat_text" class="text80" style="border:hidden; text-align:right" readonly  value="<?php echo number_format($vat_amount, 2, '.', ',') ?>">                    </td>
                </tr>
                <tr>
                  <td colspan="3" align="left" valign="middle">&nbsp;<?php $all_total = $sub_total3 + $vat_amount ?></td>
                  <td colspan="2" align="left" valign="middle">Total:</td>
                  <td align="right" valign="middle"><input type="text" name="all_total_text" id="all_total_text" class="text80" style="border:hidden; text-align:right" readonly  value="<?php echo number_format($all_total, 2, '.', ',') ?>" >
				</td> 
                </tr>
              </table>
            <br />
              <a href="cash_quotation_print.php?quote_no=<?php echo $quote_no ?>&a=<?php echo $a ?>" target="_blank">
              <input name="print_button" id="print_button" type="button" value="Print" class="imagebutton" />
              </a>
</div>
              <?php } ?>
              <br />
        
        <div class="button_inside_frame"> Notes:
          <table width="650" id="table_details_display">
            <tr>
              <td width="81" height="25">Date:</td>
              <td width="86">&nbsp;Entry by:</td>
              <td width="475">Note:&nbsp;</td>
            </tr>
            <?php
			   $i=0;
			   while ($i<$notes_num) {
				   $c1 = mysql_result($notes_result, $i, "note_date");  
				   $c2 = mysql_result($notes_result, $i, "note_text");  
				   $c3 = mysql_result($notes_result, $i, "seller"); 
				   ?>
            <tr>
              <td height="25"><?php echo date("d-m-Y", strtotime($c1))?></td>
              <td><?php echo $c3 ?></td>
              <td><?php echo $c2 ?></td>
            </tr>
            <?php 
				  $i++;
			   }?>
          </table>
        </div>
  </div>
  <!--end of tableMain--> 
  
</div>
<?php include ('../navigation/side_bar.php'); ?>
</body>
<?php } else {
$_SESSION['denied_return'] = "sales/sales_main.php";	
include ("../navigation/sales_sidebar.php");?>
<body onload="accessdenied(); return false">
</body>
<?php }
?>
</html>