<?php
session_start();
ob_start();
  if(!isset($_SESSION['LOGIN_STATUS']))
  {
      header('location:../index.php');
  }
require "../model/configuration.php";
include("../model/words_class.php");
/* @var $sion type */
$sion = $_POST['sion'];

//$specdisc=$_POST['specdisc'];
//$taxprice=$_POST['taxprice'];
//$expence=$_POST['expence'];
$advamt=$_POST['advamt'];
$binding=$_POST['additionalctp'];
$discount=$_POST['additionalimpressioncharge'];
$additionaldtpcharge=$_POST['additionaldtpcharge'];
if (isset($_POST['save_estimate'])) {
    if (isset($_SESSION['estimate']) && $_SESSION['estimate'] == 'Estimate') {
        $result = $db->execute("SELECT * FROM transaction_number");
        $row = $result->fetch_assoc();
        $trno = $row['trnno'];
        $nwtnno = $trno + 1;
        $db->execute("update transaction_number set trnno='$nwtnno'");



        $nwinvno = $db->execute("SELECT * FROM estimation_no");
        $rowinno = $nwinvno->fetch_assoc();
        $ninno = $rowinno['estimateno'];
        $nwinno = $ninno + 1;
        $db->execute("update estimation_no set estimateno='$nwinno'");
        $date = date("Y-m-d");


        $prdqty = $db->execute("select * from estimate_temp_table where permission='".$sion."'");
        while ($tqty = $prdqty->fetch_assoc()) 
        {
$db->execute("INSERT INTO estimate_table(estimation_no,customer,product_code,product_name,machine_id,print_size,nopages,inkcolor,copies,numberofbooks,numberofsetsperbooks,startingno,bookno_frm,typeofbndboard,plate,ctp,lamination,backprint,same_matter,folding,creasing,binding,stapling,stitching,pasting,prntlastcopy,typeofwork,workdescription,amount,laminat_amount,bckprnt_amt,fold_amt,creas_amt,tax,advamt,date,dummypaper,printamount,papercoast,platecoast,permission,user,trnno) 
            VALUES('".$ninno."','".$tqty['customer']."','".$tqty['product_code']."','".$tqty['product_name']."','".$tqty['machine_id']."','".$tqty['print_size']."','".$tqty['nopages']."','".$tqty['inkcolor']."','".$tqty['copies']."','".$tqty['numberofbooks']."','".$tqty['numberofsetsperbooks']."','".$tqty['startingno']."','".$tqty['bookno_frm']."','".$tqty['typeofbndboard']."','".$tqty['plate']."','".$tqty['ctp']."','".$tqty['lamination']."','".$tqty['backprint']."','".$tqty['same_matter']."','".$tqty['folding']."','".$tqty['creasing']."','".$tqty['binding']."','".$tqty['stapling']."','".$tqty['stitching']."','".$tqty['pasting']."','".$tqty['prntlastcopy']."','".$tqty['typeofwork']."','".$tqty['workdescription']."','".$tqty['amount']."','".$tqty['laminat_amount']."','".$tqty['bckprnt_amt']."','".$tqty['fold_amt']."','".$tqty['creas_amt']."','".$tqty['tax']."','$advamt','".$tqty['date']."','".$tqty['dummypaper']."','".$tqty['printamount']."','".$tqty['papercoast']."','".$tqty['platecoast']."','".$sion."','".$tqty['user']."','$trno')");
        }
      $db->execute("delete from estimate_temp_table where permission='$sion'");
    }
    unset($_SESSION['estimate']);
}
?>
<!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" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><?php echo $_SESSION['title'];?></title>
<link rel='stylesheet' type='text/css' href='../css/fonts.googleapis.com/css_cd14c140.css' />
<!-- Bootstrap CSS -->
<link rel="stylesheet" type="text/css" href="../vendor/bootstrap/css/bootstrap.min.css" />
<!-- Theme CSS -->
<link rel="stylesheet" type="text/css" href="../css/vendor.css" />
<link rel="stylesheet" type="text/css" href="../css/theme.css" />
<link rel="stylesheet" type="text/css" href="../css/utility.css" />
<link rel="stylesheet" type="text/css" href="../css/custom.css" />
<link rel="stylesheet" type="text/css" href="../css/Quotation/quotstyle.css" />
</head>

<body class="dashboard-page">
<script> var boxtest = localStorage.getItem('boxed'); if (boxtest === 'true') {document.body.className+=' boxed-layout';} </script> 
<!-- Start: Theme Preview Pane -->


<!-- Start: Header -->
<header class="navbar navbar-fixed-top">
  
   <?php include_once("../include/top_menu.php");?> 
</header>

<!-- Start: Main -->
<div id="main"> 
  
  <!-- Start: Sidebar -->
  <?php include_once("../include/left_menu.php");?>

  <!-- Start: Content -->
  <section id="content_wrapper">
    <div id="topbar">
      <div class="topbar-left">
        <ol class="breadcrumb">
          <li class="crumb-icon"><a href="#"><span class="glyphicon glyphicon-home"></span></a></li>
          <li class="crumb-link"><a href="#">Estimate</a></li>
          <li class="crumb-trail">Estimate Add</li>
        </ol>
      </div>
      
    </div>
    
    
    <div id="content">
      <div class="row">
        <div class="col-md-12">
          <div class="panel">
          
          
          
          
          <div id="divToPrint">
          
          
          
          
          <div class="quot_wraper">
          <?php
	$salquery = $db->execute("SELECT * FROM estimate_table where permission='" . $sion . "'");
	$salero = $salquery->fetch_assoc();
?>
          <?php 
	$vqry=$db->execute("select * from clients where client_id='" . $salero['customer'] . "' and delet='0'");
	$vqrow=$vqry->fetch_assoc();
    
    
    $mchqry=$db->execute("select * from machines_table where id='" . $salero['machine_id'] . "'");
       $mqro=$mchqry->fetch_assoc();
?>
          
<div class="quot_head">Quotation</div>
<div class="quot_head_main"><div class="hd_right"><div class="hdrgtcnt">Date : <?php echo $salero['date'];?></div><div class="hdrgtcnt">Ref :</div></div></div>
<div class="quot_head_main"><div class="hd_left"><div class="hdlftcnt"><label class="cladrslabl">To : </label><span class="cladrs"><?php echo $vqrow['client_name'];?><br /><?php echo $vqrow['clientaddress'];?><br /><?php echo $vqrow['place'];?><br /><?php echo $vqrow['phone'];?></span></div></div></div>
<div class="quot_head_main"><span class="spn">Dear Sir,<br /> We are pleased to quote our best possible rates as below:</span></div>
<div class="btm_cnt">

<table class="dtls_table" cellpadding="0" cellspacing="0">

<tr><th>Sl.#</th><th>Description</th><th>Print Size</th><th>Pages</th><th>Copys</th><th>CTP</th><th>DTP</th><th>Dummy Paper</th><th>Paper Coast</th><th>Plate Coast + Impresiion</th><th class="brdrnone">Amount</th></tr>
<?php
$c=0;
$gtotal=0;
$cppy=0;
$prntamt=0;
$ttamt=0;
$lamnatamt=0;
$bckprintamt=0;
$foldamt=0;
$cresamt=0;
$othrtot=0;
$nettotal=0;
$ttamta=0;
$prdqty = $db->execute("select * from estimate_table where customer='" . $salero['customer'] . "' and estimation_no='" . $salero['estimation_no'] . "' and date='" . $salero['date'] . "' and trnno='" . $salero['trnno'] . "' and permission='" . $sion . "' and delet='0'");
while ($tqty = $prdqty->fetch_assoc())
{
    $c++;
    
    $mchprice=$db->execute("select * from other_settings where id='5'");
    $mchqro=$mchprice->fetch_assoc();
    $mchpriceo=$db->execute("select * from other_settings where id='6'");
    $mchqroo=$mchpriceo->fetch_assoc();
	$prdselquery=$db->execute("select * from products where productcode='".$tqty['product_code']."'");
    $prdselrow=$prdselquery->fetch_assoc();
	
list($power,$exp) = explode("/", $tqty['print_size']);
$pow=$power;
$expon=$exp-1;
$prntcopy=pow(2,$expon);
	
$dummypaper=($tqty['copies']/$prntcopy);

$papercost=	$prdselrow['sellprice']*ceil($dummypaper);
	
    
    if($tqty['copies']>1000)
    {
       $cppy=$tqty['copies']/1000;  
       $cppy=ceil($cppy)-1;
       
       $ttamt=$cppy*$mqro['impression_charge'];   
       
       $prices=  $ttamt+($tqty['amount']*$tqty['copies']);
       
       
       $ttpr=$tqty['ctp']*$mchqro['amount'];
       
       
       $ttprice=$prices+$ttpr+$mchqroo['amount'];
  
    }
    else
    {
       $prices=$ttamt+($tqty['amount']*$tqty['copies']); 
       $ttpr=$tqty['ctp']*$mchqro['amount'];
       
       
       $ttprice=$prices+$ttpr+$mchqroo['amount'];
    }
    
    
    
    if($tqty['same_matter']=='No')
    {
        if($tqty['copies']>1000)
    {
       $cppya=$tqty['copies']/1000;  
       $cppya=ceil($cppya)-1;
       
       $ttamta=$cppya*$mqro['impression_charge'];   
       
       $pricesa=  $ttamta+($tqty['amount']*$tqty['copies']);
       
       
       $ttpra=$tqty['add_plate']*$mchqro['amount'];
       
       
       $additamt=$pricesa+$ttpra+$mchqroo['amount'];
  
    }
    else
    {
       $pricesa=$ttamta+($tqty['amount']*$tqty['copies']); 
       $ttpra=$tqty['add_plate']*$mchqro['amount'];
       
       
       $additamt=$pricesa+$ttpra+$mchqroo['amount'];
    }
    }
    else
    {
        $cppya=$tqty['copies']/1000;  
        $cppya=ceil($cppya);
        $additamt=$cppya*$mqro['impression_charge'];
    }
    
    
    
    
    
    $lamnatamt=$lamnatamt+($tqty['laminat_amount']*$tqty['copies']);
    //$bckprintamt=$bckprintamt+($tqty['bckprnt_amt']*$tqty['copies']);
    $foldamt=$foldamt+($tqty['fold_amt']*$tqty['copies']);
    $cresamt=$cresamt+($tqty['creas_amt']*$tqty['copies']);
    

$gtotal=$gtotal+$ttprice+$additamt;  
$tott= $tqty['printamount']+ $tqty['platecoast'] + $tqty['papercoast'];  
?>
<tr><td><?php echo $c;?></td><td><?php echo $tqty['product_name'];?></td><td><?php echo $tqty['print_size'];?></td><td><?php echo $tqty['nopages'];?></td><td><?php echo $tqty['copies'];?></td>  <td><?php echo $tqty['ctp'];?></td> <td><?php ?></td> <td><?php echo $tqty['dummypaper'];?></td> <td><?php echo $tqty['papercoast'];?></td><td><?php echo $tqty['platecoast']."+".$tqty['printamount'];?></td> <td class="brdrnone"><?php echo $tott;?></td></tr>
<?php
 
$prntamt=$prntamt + $tott ;
}
?>

<tr><td></td><td>Advance+Dtp</td><td colspan="8"></td><td><?php echo $advamt."+".$additionaldtpcharge; //$othrtot=$lamnatamt+$bckprintamt+$foldamt+$cresamt; 
$nettotal = $prntamt - $advamt;//$gtotal;//+$othrtot;
$nettotal = $nettotal-$discount;
$nettotal = $nettotal+$binding;
?></td></tr>

<tr><td></td><td>Binding/Additional</td><td colspan="8"></td><td><?php echo $binding;
?></td></tr>
<tr><td></td><td>Less discount</td><td colspan="8"></td><td><?php echo $discount;
?></td></tr>



<tr class="btmrow"><td colspan="9">&nbsp;Rs: <?php $re= new in_words; echo $re->show_words(number_format($nettotal))."/ Only";?></td><th>Total</th><th><?php $nettotal=$nettotal+$additionaldtpcharge;
				echo $nettotal;
				$nettotal=$nettotal+$advamt;
       ?></th></tr>

<?php
  $totalamt= ($prntamt+$binding)-$discount;
$db->execute("update estimate_table set total = '".$nettotal."' where customer='" . $salero['customer'] . "' and estimation_no='" . $salero['estimation_no'] . "' and date='" . $salero['date'] . "' and trnno='" . $salero['trnno'] . "' and permission='" . $sion . "' and delet='0'");


?>
</table>

</div>
</div>
          
          
          
          
          
          </div>
          
          
  <div class="row"><br />
    <div class="col-xs-8 invoice-block pull-right">
    <a href="estimate.php" class="btn btn-info">Back</a>
                <input type="button" class="btn btn-success" onclick="PrintDiv();" value="Print"/><br />
    </div>
    <br />
    </div>        
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          
          </div>
          </div>
          
         
          </div>
          
    
    
    
    
    
    
    
    
    
      <?php //include_once("dashboard_content.php");?>
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    </div>
    
  </section>
  
  <!-- Start: Right Sidebar -->
  
</div>
<!-- End #Main --> 


<!-- jQuery --> 
<script type="text/javascript" src="../vendor/jquery/jquery-1.11.1.min.js"></script> 
<script type="text/javascript" src="../vendor/jquery/jquery_ui/jquery-ui.min.js"></script> <!-- Bootstrap --> 
<script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script> 

<!-- Page Plugins --> 
<script type="text/javascript" src="../vendor/plugins/validate/jquery.validate.js"></script>  
<script type="text/javascript" src="../vendor/plugins/morris/morris.js"></script> 
<script type="text/javascript" src="../vendor/plugins/datatables/js/jquery.dataTables.min.js"></script> 
<script type="text/javascript" src="../vendor/plugins/datatables/js/datatables.js"></script> 
<script type="text/javascript" src="../vendor/plugins/multiselect/bootstrap-multiselect.js"></script> 
<script type="text/javascript" src="../vendor/plugins/jvectormap/jquery-jvectormap.min.js"></script> 
<script type="text/javascript" src="../vendor/plugins/jvectormap/assets/jquery-jvectormap-us-lcc-en.js"></script> 
<script type="text/javascript" src="../vendor/plugins/clndr/clndr.js"></script> 
<script type="text/javascript" src="../vendor/plugins/clndr/moment.js"></script> 
<script type="text/javascript" src="../vendor/plugins/datepicker/bootstrap-datepicker.js"></script> 
<script type="text/javascript" src="../vendor/plugins/jquerymask/jquery.maskedinput.min.js"></script> 
<!-- Flot Plugins --> 
<script type="text/javascript" src="../vendor/plugins/jqueryflot/jquery.flot.min.js"></script> 

<!-- Theme Javascript --> 
<script type="text/javascript" src="../js/utility/spin.min.js"></script> 
<script type="text/javascript" src="../js/utility/underscore-min.js"></script> 
<script type="text/javascript" src="../js/main.js"></script> 
<script type="text/javascript" src="../js/ajax.js"></script> 
<script type="text/javascript" src="../js/custom.js"></script> 

<script type="text/javascript">

                                                                function PrintDiv() {

                                                                    var divToPrint = document.getElementById('divToPrint');
                                                                    var popupWin = window.open('', '_blank', 'width=595,height=842');
                                                                    popupWin.document.open();
                                                                    popupWin.document.write('<html><link href="../css/Quotation/quotstyle.css" rel="stylesheet" type="text/css" /><body onload="window.print()">' + divToPrint.innerHTML + '</html>');
                                                                    popupWin.document.close();
                                                                }
        </script>


</body>
</html>