<?php

include('includes/session.inc');
$Title = _('Create Quotation');
include('includes/SQL_CommonFunctions.inc');

include('includes/header.inc');
echo '<p> </p> <p></p><p class="page_title_text"><img alt="" src="'.$RootPath.'/css/'.$Theme.
		'/images/printer.png" title="' .// Icon image.
		_('Outstanding Approved Job Costing ') . '" /> ' .// Icon title.
		_('Choose below the approved job costing that is yet to be quoted') . '</p>';// Page title.
echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
	echo '<div class="col-sm-10">';// div class=?
	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<table class="table">';

echo '<td>' . _('Approved Job Costing Sheets ') . ':</td>
							<td><select name="Job">';
							
							$SQL = "SELECT pcid,
											projectid,
											name,
				                            contract_value,
											contract_vip
		                            FROM projectcosting
		                             WHERE status = 'approved costing'";

$result=DB_query($SQL);
//echo '<option value="0">0 - ' . _('None Available') . '</option>';
 echo '<option value="0">0 - ' . _('None') . '</option>';
while ($myrow=DB_fetch_array($result)){
	if (isset($_POST['Job']) AND $_POST['Job']==$myrow['pcid']){
	    
		echo '<option selected="selected" value="' . $myrow['projectid'] . '">' . $myrow['projectid'].' - ' .$myrow['name'] . ' - $' .$myrow['contract_value'] . '</option>';
	} else {
			echo '<option value="' . $myrow['projectid'] . '">' . $myrow['projectid'].' - ' .$myrow['name'] . ' -$ ' .$myrow['contract_value'] . '</option>';
	}
	
	
	
}
echo '</select></td>';

echo '<input type="submit" name="quote" value="' ._('Create Quotation') . '" />
		</div>
		';



echo '</div>';

echo '</div>';
$jobno = $_POST['Job'];


	if (isset($_POST['quote']) && ($_POST['Job'] == 0)){


	echo ' <b><font style="color: red">Please there are no approved job costing sheets available to raise quotation. </font></b>';	
		
	}

	elseif (isset($_POST['quote']) && ($_POST['Job'] == $jobno)){
		
		
		//open projectdetail table to start gathering informations which we will first insert at salesorders table  and get pdf headers written. Salesorders will assist in converting quotation to invoice. This below is simply a quote generation.
		
		
			$SQL = "SELECT projectid,
	               project_name,
				   customer,
				   account_number,
				   contact_number,
				   required_date,
				   start_date,
				   comments
			FROM projectdetails 
			WHERE projectid='" . $jobno . "'
			";
	
			$result = DB_query($SQL);
	
			$myrow = DB_fetch_array($result) ;
		$projectname= $myrow['project_name'];
		$ac = $myrow['account_number'];
		
			$debtorno = $myrow['customer'];
					
	$contactnumber = $myrow['contact_number'];
		
	$deliverydate = $myrow['required_date'];
			
	$confirmeddate = $myrow['start_date'];
			$comments = $myrow['comments'];
		
		
		//todays date.This is to show today quote was generated
	$quotedate = date("d/m/Y");
		
		//get debtor headoffice details:
		$SQL100 = "SELECT 	name,
							address1,
						     address2,
				   			 address3,
							 address4,
							 address5,
							 address6
							 
			FROM debtorsmaster
			WHERE debtorno='" . $debtorno . "'
			";
	
	$result100 = DB_query($SQL100);
	
	$myrow100 = DB_fetch_array($result100) ;
		$debtorname = $myrow100['name'];
		
		$address1 = $myrow100['address1'];
		$address2 = $myrow100['address2'];
		$address3 = $myrow100['address3'];
		$address4 = $myrow100['address4'];
		$address5 = $myrow100['address5'];
		$address6=  $myrow100['address6'];
		
		
		//get customer branch code from debtoraccounts table
		
			$SQL1 = "SELECT branchcode,
			                departmentid
				   
			FROM debtoraccount 
			WHERE account_number='" . $ac . "'
			";
	
	$result1 = DB_query($SQL1);
	
	$myrow1 = DB_fetch_array($result1) ;
		
		$branchcode = $myrow1['branchcode'];
		$departmentid = $myrow1['departmentid'];
		
		
		//now get other customer details from custbranch tables
		
				$SQL2 = "SELECT brname,
								braddress1,
								braddress2,
								braddress3,
								phoneno,
								contactname,
								email
				   
			FROM custbranch
			WHERE branchcode='" . $branchcode. "'
			";
	
	$result2 = DB_query($SQL2);
	
	$myrow2 = DB_fetch_array($result2) ;
		
		$branchname = $myrow2['brname'];
		$branchad1 = $myrow2['braddress1'];
		$branchad2 = $myrow2['braddress2'];
		$branchad3 = $myrow2['braddress3'];
		$branchad4 = $myrow2['braddress3'];
		$branchad5 = $myrow2['braddress3'];
		$branchad6 = $myrow2['braddress3'];
		$phoneno = $myrow2['phoneno'];
		$contactname = $myrow2['contactname'];
		$email = $myrow2['email'];
		
		
		
	$username =$_SESSION['UsersRealName'];
		
			
			
		
		
		//insert in salesorders to create new sales order/quote. The system can only add quotation and cant update it.
		$Result3 = DB_Txn_Begin();

	   $OrderNo = GetNextTransNo(30, $db);
		
		$sql3 = "INSERT INTO salesorders (
								orderno,
								debtorno,
								branchcode,
								account_number,
								job_no,
								
								comments,
								orddate,
								ordertype,
								shipvia,
								deliverto,
								deladd1,
								deladd2,
								deladd3,
								deladd4,
								deladd5,
								deladd6,
								contactphone,
								contactemail,
								freightcost,
								fromstkloc,
								deliverydate,
								quotedate,
								confirmeddate,
								quotation,							
								deliverblind,
								preparedby)
							VALUES (
								'$OrderNo',
								'$debtorno ',
								'$branchcode',
								'$ac',
								'$jobno',
								'$comments',
								'$quotedate',
								'DF',
								'1',
								'$debtorname',
								'$branchad1',
								'$branchad2',
								'$branchad3',
								'$branchad4',
								'$branchad5',
								'$branchad6',
								'$phoneno',
								'$email',
								'0',
								'SUVA',
								'$deliverydate',
							     '$quotedate',
								'$confirmeddate',
								'1',
								'1',
								'$username'
								)";

		$ErrMsg = _('This customer could not be added because');
		
			$result3 = DB_query($sql3,$ErrMsg);
	
		
prnMsg( _('Customer updated'),'success');
			echo '<br />';
	
		//create pdf headers
		
		
	}

//echo 'Quotation Created Successfully'. $jobno.'!';
echo '</div>';
echo '</div>';

//include('includes/footer.inc'); 

?>
