<?php
/* $Id: index.php 6941 2014-10-26 23:18:08Z daintree $*/
error_reporting(E_ALL & ~E_NOTICE);
ob_start();
$PageSecurity=0;

include('includes/session.inc');
$Title=_('ERP Home');
include('includes/newheader.php');
include('includes/SQL_CommonFunctions.inc');
   
	//echo $request->BuyerCostCenterId;
	
$trimmed = str_replace("\\n", "", $_SESSION['results1']);
	
	$finaldata = str_replace("\\r", "", $trimmed);
	$finaldata1 = str_replace("\\", "", $finaldata);
	$response = json_decode($finaldata1);
	 
		$trimmed1 = str_replace("\\n", "", $_SESSION['invoiceRequest1']);
	
	$finaldata1 = str_replace("\\r", "", $trimmed1);
	$finaldata2 = str_replace("\\", "", $finaldata1);
	$request = json_decode($finaldata2);
	
	$trimmedother = str_replace("\\n", "", $_SESSION['otherRequest1']);
	
	$finaldataother = str_replace("\\r", "", $trimmedother);
	$finaldata2other = str_replace("\\", "", $finaldataother);
	$otherrequest = json_decode($finaldata2other);

	   
	
		$userid = $_SESSION['UserID'];
		
$SQLuser = "SELECT email,realname
	               
			FROM www_users
			WHERE userid='" . $userid. "'
			";
	
			$resultuser = DB_query($SQLuser);
	
			$myrowuser = DB_fetch_array($resultuser) ;
		$emailm= $myrowuser['email'];
$realname= $myrowuser['realname'];
	
	
	
	   
	   $SQL77 = "SELECT 	departmentname
							 
			FROM department
			WHERE departmentid='" . $otherrequest->department. "'
			";
	
	$result77 = DB_query($SQL77);
	
	$myrow77 = DB_fetch_array($result77) ;
		
	$departmentname = $myrow77['departmentname'];	
//get debtor headoffice details:
		$SQL100 = "SELECT 	debtorno,
							name,
							address1,
						     address2,
				   			 address3,
							 address4,
							 address5,
							 address6
							 
			FROM debtorsmaster
			WHERE debtorno='" . $otherrequest->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'];
		
		
		
		
				$SQL21 = "SELECT brname,
								braddress1,
								braddress2,
								braddress3,
								phoneno,
								contactname,
								email
				   
			FROM custbranch
			WHERE debtorno='" . $myrow100['debtorno'] . "'
			";
	
	$result21 = DB_query($SQL21);
	
	$myrow21 = DB_fetch_array($result2) ;
		
		$branchname = $myrow21['brname'];
		$branchad1 = $myrow21['braddress1'];
		$branchad2 = $myrow21['braddress2'];
		$branchad3 = $myrow21['braddress3'];
		$branchad4 = $myrow21['braddress3'];
		$branchad5 = $myrow21['braddress3'];
		$branchad6 = $myrow21['braddress3'];
		$phoneno = $myrow21['phoneno'];
		$contactname = $myrow21['contactname'];
		$email = $myrow21['email'];
		
		

		$SQLpaymentterms = "SELECT 	paymentterms
							 
			FROM debtorsmaster
			WHERE debtorno='" . $myrow100['debtorno'] . "'
			";
	
	$resultpaymentterms = DB_query($SQLpaymentterms);
	$myrowpaymentterms = DB_fetch_array($resultpaymentterms) ;
	
	
$InvoiceNo = $request->InvoiceNumber;
			//get periodno 
	
	
	
	$quotedated = $otherrequest->Date;
	
	$sqldate = date("Y-m-d");
	$erptime =  date("d-m-Y h:i:s a");
$yeardate = date('Y',strtotime($sqldate));
$monthdate =date('m',strtotime($sqldate));
$monthname =date('M',strtotime($sqldate)); 
	
	
	
	//$sdctime =  date("Y-m-d h:i:s",$quotedate);

	$sdctime = $response->DT;

$formatedsdctime= date("Y/m/d h:i:s a",strtotime(($sdctime)));
	
	
$PaperSize = 'A4_Landscape';// PDFStarter.php: 
	
$Page_Width=842; $Page_Height=595; $Top_Margin=30; $Bottom_Margin=30; $Left_Margin=40; $Right_Margin=30;

include('includes/PDFStarter.php');
$pdf->addInfo('Title', _('Customer Credit Note') );
$pdf->addInfo('Subject', _('Credit Note'));
$FontSize = 12;
$FirstPage = true;
	$line_height=16;
	
		$PageNumber = 1;
		include('includes/PDFCreditNoteHeader.inc');
	$FirstPage = False;

	
//get line items from salesorderdetails


	 $items = $otherrequest->Items;

 foreach($items as $key => $lineitems) {
	  
    
  

		  
        $LeftOvers = $pdf->addTextWrap($Left_Margin+3,$YPos,95,$FontSize,$lineitems->Stockid);
        $LeftOvers = $pdf->addTextWrap($Left_Margin+100,$YPos,251,$FontSize,$lineitems->Name);
	
	  $lines=1;
					while($LeftOvers!='') {
						$LeftOvers = $pdf->addTextWrap($Left_Margin+100,$YPos,251,$FontSize,$LeftOvers);
						$lines++;
					}
		 $SQL = "SELECT 	units
							 
			FROM stockmaster 
			WHERE stockid ='" . $lineitems->Stockid . "'
			";
	
	
	$result = DB_query($SQL);
	$myrow = DB_fetch_array($result) ;
	 $uom= $myrow['units'];
	$LeftOvers = $pdf->addTextWrap($Left_Margin+353,$YPos,96,$FontSize,locale_number_format($lineitems->UnitPrice,2),'right');
					$LeftOvers = $pdf->addTextWrap($Left_Margin+453,$YPos,95,$FontSize,-($lineitems->Quantity),'right');
					$LeftOvers = $pdf->addTextWrap($Left_Margin+553,$YPos,25,$FontSize,$uom,'centre');
	 
	 $itemstaxes = $items1->Labels;
	 $counter2=0;

	 
	 
	 $SQL= "SELECT  sales_tax
	               
			FROM item_tax
			WHERE stockid='" . $lineitems->Stockid. "'
			";
	
			$result = DB_query($SQL);
	
	while ($myrow = DB_fetch_array($result)){
	  
    
  $SQLlabel= "SELECT  tax_label
	               
			FROM  new_sales_tax
			WHERE id='" . $myrow['sales_tax']. "'
			";
	
			$resultlabel = DB_query($SQLlabel);
$myrowlabel = DB_fetch_array($resultlabel);
		  
$LeftOvers = $pdf->addTextWrap($Left_Margin+575+$counter2,$YPos,50,$FontSize,$myrowlabel['tax_label'],'center');


					

			$counter2= $counter2 +7;
		  
	  }
	 
					
					$LeftOvers = $pdf->addTextWrap($Left_Margin+642,$YPos,120,$FontSize,locale_number_format(-($lineitems->Quantity * $lineitems->UnitPrice),2),'right');
  

		$YPos -= ($line_height);

			
					if ($YPos <= $Bottom_Margin) {
						/* head up a new invoice/credit note page */
						/*draw the vertical column lines right to the bottom */
						PrintLinesToBottom ();
						include ('includes/PDFCreditNoteHeader.inc');
					} //end if need a new page headed up
		

			
  }		



$YPos -= ($line_height);

				


				

			$YPos -= $line_height;

			
			/* check to see enough space left to print the 4 lines for the totals/footer */
			if (($YPos-$Bottom_Margin)<(4*$line_height)) {
				PrintLinesToBottom ();
				include ('includes/PDFCreditNoteHeader.inc');
			}
			/* Print a column vertical line  with enough space for the footer */
			/* draw the vertical column lines to 4 lines shy of the bottom
			to leave space for invoice footer info ie totals etc */
			$pdf->line($Left_Margin+97, $TopOfColHeadings+12,$Left_Margin+97,$Bottom_Margin+(6*$line_height));

			/* Print a column vertical line */
			$pdf->line($Left_Margin+350, $TopOfColHeadings+12,$Left_Margin+350,$Bottom_Margin+(6*$line_height));

			/* Print a column vertical line */
			$pdf->line($Left_Margin+450, $TopOfColHeadings+12,$Left_Margin+450,$Bottom_Margin+(6*$line_height));

			/* Print a column vertical line */
			$pdf->line($Left_Margin+550, $TopOfColHeadings+12,$Left_Margin+550,$Bottom_Margin+(6*$line_height));

			/* Print a column vertical line */
			$pdf->line($Left_Margin+587, $TopOfColHeadings+12,$Left_Margin+587,$Bottom_Margin+(6*$line_height));

			$pdf->line($Left_Margin+640, $TopOfColHeadings+12,$Left_Margin+640,$Bottom_Margin+(6*$line_height));

			/* Rule off at bottom of the vertical lines */
			$pdf->line($Left_Margin, $Bottom_Margin+(6*$line_height),$Page_Width-$Right_Margin,$Bottom_Margin+(6*$line_height));
	
	
	/* Now print out the footer and totals */

		

			$YPos = $Bottom_Margin+(5*$line_height);

			/* Print out the invoice text entered */
			$FontSize =10;
				$LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos+5,280,$FontSize, _('SDC Time') . ': ' . $formatedsdctime);
		    $LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-5,280,$FontSize, _('SDC Invoice No:') . $response->IN);
	
	//$qrcode = base64_decode($response->VerificationQRCode);
$qrcode = base64_decode($response->VerificationQRCode);
	
	$pdf->Image('@'.$qrcode, 290, 474, 65, 65,'GIF', '', '', true, 2000, '', false, false, 0, false, false,false);
	
//	$pdf->Image('@'.$qrcode, 280, 500, 75, 75,'GIF', '', '', true, 600, '', false, false, 0, false, false,false);
	
	// $LeftOvers = $pdf->addTextWrap($Left_Margin+285,$YPos-5,65,$FontSize, $qrcode);
	//$pdf->write2DBarcode($qrcode, 'QRCODE,H', 20, 210, 50, 50, $style, 'N');

	
	         $LeftOvers = $pdf->addTextWrap($Left_Margin+5,$YPos-15,280,$FontSize,_('Invoice Counter:') . $response->IC);
	
	$v =$response->VerificationUrl;
$space=" ";
	 $html = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="'.$v.'" style="color:blue;"><span style="font-weight: bold;">SDC Verification Link(For electronic Use Only)</span></a>';
 $link = $pdf->writeHTML($html, true, false, true, false, '');
	
			$FontSize = 10;

				$pdf->addText($Page_Width-$Right_Margin-220, $YPos+15,$FontSize, _('Total Purchase'));
			$LeftOvers = $pdf->addTextWrap($Left_Margin+642,$YPos+5,120,$FontSize,locale_number_format($response->TotalAmount,2), 'right');
	
			$pdf->addText($Page_Width-$Right_Margin-220, $YPos-2,$FontSize, _('Payment Method'));
		   
            	$LeftOvers = $pdf->addTextWrap($Left_Margin+642,$YPos-10,120,$FontSize,$request->PaymentType, 'right');
			/*rule off for total */
			$pdf->line($Page_Width-$Right_Margin-222, $YPos-(2*$line_height),$Page_Width-$Right_Margin,$YPos-(2*$line_height));

			/*vertical to separate totals from comments and ROMALPA */
			$pdf->line($Page_Width-$Right_Margin-222, $YPos+$line_height,$Page_Width-$Right_Margin-222,$YPos-55);

			$YPos+=10;
			
				/* Print out the payment terms */
			//	$pdf->addTextWrap($Left_Margin+5,$YPos-5,280,$FontSize,_('SDC Time') . ': ' . $response->DT);

	
				$pdf->addText($Page_Width-$Right_Margin-220, $YPos - ($line_height*2)-10,$FontSize, _('TOTAL CREDIT'));
		$pdf->addText($Page_Width-$Right_Margin-750, $YPos - ($line_height*2)-15,8, _('Powered by E-Business ERP SYSTEM'));
				$FontSize=9;
				$YPos-=4;

/*vertical to sperate tax labels */
                  $pdf->line($Left_Margin+350, $YPos+$line_height,$Left_Margin+350,$YPos-60);


	            $LeftOvers = $pdf->addTextWrap($Left_Margin+350,$YPos,260,$FontSize,_('Label') );
			      $LeftOvers = $pdf->addTextWrap($Left_Margin+400,$YPos,260,$FontSize,_('Name') );
			        $LeftOvers = $pdf->addTextWrap($Left_Margin+450,$YPos,260,$FontSize,_('Rate') );
	  $LeftOvers = $pdf->addTextWrap($Left_Margin+500,$YPos,260,$FontSize,_('Tax') );
	
//horizontal line
$LeftOvers = $pdf->line($Left_Margin+350, $YPos+$line_height-20,$Left_Margin+550,$YPos+$line_height-20);

$itemtaxes = $response->TaxItems;
//$LeftOvers = $pdf->addTextWrap($Left_Margin+350,$YPos-15,260,$FontSize,_('Testing') );
	//loop through tax items
	
//get tax





 $itemtaxes = $response->TaxItems; // Replace ... with your PHP Object
	
	$count= 15;
$totaltax= 0;
		foreach($itemtaxes as $key => $lineitemtax) {
			
			if ($lineitemtax->Label =='P'){
				
				$type = "$";
				
			}
			else{
				
				$type = "%";
			}
			
			
			$pdf->addTextWrap($Left_Margin+350,$YPos-$count,260,8,$lineitemtax->Label);
		 $pdf->addTextWrap($Left_Margin+400,$YPos-$count,260,8,$lineitemtax->CategoryName);
		 $pdf->addTextWrap($Left_Margin+450,$YPos-$count,260,8,locale_number_format($lineitemtax->Rate,2).$type);
		 $pdf->addTextWrap($Left_Margin+500,$YPos-$count,260,8,locale_number_format($lineitemtax->Amount,2));
		
		$totaltax= $totaltax + $lineitemtax->Amount ;
 $count = $count + 8;
			
			
		}
		$pdf->line($Left_Margin+350, $YPos-40,$Left_Margin+550,$YPos-40);		
	
			$pdf->addTextWrap($Left_Margin+350,$YPos-50,260,$FontSize,_('Total Tax'));

				$pdf->addTextWrap($Left_Margin+500,$YPos-50,260,$FontSize,locale_number_format($totaltax,2));

// Rule horizontal line
$pdf->line($Left_Margin, $YPos-60,$Left_Margin+770,$YPos-60);
				
				$FontSize=10;
		
			$LeftOvers = $pdf->addTextWrap($Left_Margin+642,$YPos-50,120, $FontSize,locale_number_format($response->TotalAmount,2), 'right');
		
//$pdf->AddPage();
	
//$pdf->Image('@'.$qrcode, 315, 200, 200, 200,'GIF', '', '', true, 300, '', false, false, 0, false, false,false);
	

				
	
		
		ob_end_clean();
	
		$pdf->OutputD('ebusinessCredit_Note_:' . $request->InvoiceNumber. '.pdf');

		
		
	
	$pdf->__destruct();
	
	
	

	exit ();

	
	

function PrintLinesToBottom () {

	global $Bottom_Margin;
	global $Left_Margin;
	global $line_height;
	global $PageNumber;
	global $pdf;
	global $TopOfColHeadings;

	/* draw the vertical column lines right to the bottom */
	$pdf->line($Left_Margin+97, $TopOfColHeadings+12,$Left_Margin+97,$Bottom_Margin);

	/* Print a column vertical line */
	$pdf->line($Left_Margin+350, $TopOfColHeadings+12,$Left_Margin+350,$Bottom_Margin);

	/* Print a column vertical line */
	$pdf->line($Left_Margin+450, $TopOfColHeadings+12,$Left_Margin+450,$Bottom_Margin);

	/* Print a column vertical line */
	$pdf->line($Left_Margin+550, $TopOfColHeadings+12,$Left_Margin+550,$Bottom_Margin);

	/* Print a column vertical line */
	$pdf->line($Left_Margin+587, $TopOfColHeadings+12,$Left_Margin+587,$Bottom_Margin);

	$pdf->line($Left_Margin+640, $TopOfColHeadings+12,$Left_Margin+640,$Bottom_Margin);

	$PageNumber++;

}

	



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





			  
 	echo '</div >';
	?>
						

		
					</div>
				</div>
			</div>
					
					

						<!--end:: Widgets/Stats-->

						<!--Begin::Section-->
						

		<!-- end::Quick Sidebar -->

		<!-- begin::Scroll Top -->
		<div id="m_scroll_top" class="m-scroll-top">
			<i class="la la-arrow-up"></i>
		</div>

		<!-- end::Scroll Top -->

		<!-- begin::Quick Nav -->
		

		<!-- begin::Quick Nav -->
<script src="includes/vendors/owl.carousel/dist/owl.carousel.js" type="text/javascript"></script>

		<!--end::Page Scripts -->
	</body>

	<!-- end::Body -->
</html>
 
      
<?php



/*The module link codes are hard coded in a switch statement below to determine the options to show for each tab */
include('includes/MainMenuLinksArray.php');

if (isset($SupplierLogin) AND $SupplierLogin==1){
	echo '<table class="table_index">
			<tr>
			<td class="menu_group_item">
				<p>&bull; <a href="' . $RootPath . '/SupplierTenders.php?TenderType=1">' . _('View or Amend outstanding offers') . '</a></p>
			</td>
			</tr>
			<tr>
			<td class="menu_group_item">
				<p>&bull; <a href="' . $RootPath . '/SupplierTenders.php?TenderType=2">' . _('Create a new offer') . '</a></p>
			</td>
			</tr>
			<tr>
			<td class="menu_group_item">
				<p>&bull; <a href="' . $RootPath . '/SupplierTenders.php?TenderType=3">' . _('View any open tenders without an offer') . '</a></p>
			</td>
			</tr>
		</table>';
	include('includes/footer.inc');
	exit;
} elseif (isset($CustomerLogin) AND $CustomerLogin==1){
	echo '<table class="table_index">
			<tr>
			<td class="menu_group_item">
				<p>&bull; <a href="' . $RootPath . '/CustomerInquiry.php?CustomerID=' . $_SESSION['CustomerID'] . '">' . _('Account Status') . '</a></p>
			</td>
			</tr>
			<tr>
			<td class="menu_group_item">
				<p>&bull; <a href="' . $RootPath . '/SelectOrderItems.php?NewOrder=Yes">' . _('Place An Order') . '</a></p>
			</td>
			</tr>
			<tr>
			<td class="menu_group_item">
				<p>&bull; <a href="' . $RootPath . '/SelectCompletedOrder.php?SelectedCustomer=' . $_SESSION['CustomerID'] . '">' . _('Order Status') . '</a></p>
			</td>
			</tr>
		</table>';

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

if (isset($_GET['Application'])){ /*This is sent by this page (to itself) when the user clicks on a tab */
	$_SESSION['Module'] = $_GET['Application'];
}








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

function GetRptLinks($GroupID) {
/*
This function retrieves the reports given a certain group id as defined in /reports/admin/defaults.php
in the acssociative array $ReportGroups[]. It will fetch the reports belonging solely to the group
specified to create a list of links for insertion into a table to choose a report. Two table sections will
be generated, one for standard reports and the other for custom reports.
*/
	global $db, $RootPath, $ReportList;
	require_once('reportwriter/languages/en_US/reports.php');
	require_once('reportwriter/admin/defaults.php');
	$GroupID=$ReportList[$GroupID];
	$Title= array(_('Custom Reports'), _('Standard Reports and Forms'));

	$sql= "SELECT id,
				reporttype,
				defaultreport,
				groupname,
				reportname
			FROM reports
			ORDER BY groupname,
					reportname";
	$Result=DB_query($sql,'','',false,true);
	$ReportList = '';
	while ($Temp = DB_fetch_array($Result)) $ReportList[] = $Temp;

	$RptLinks = '';
	for ($Def=1; $Def>=0; $Def--) {
        $RptLinks .= '<li class="menu_group_headers">';
        $RptLinks .= '<b>' .  $Title[$Def] . '</b>';
        $RptLinks .= '</li>';
		$NoEntries = true;
		if ($ReportList) { // then there are reports to show, show by grouping
			foreach ($ReportList as $Report) {
				if ($Report['groupname']==$GroupID AND $Report['defaultreport']==$Def) {
                    $RptLinks .= '<li class="menu_group_item">';
					$RptLinks .= '<p>&bull; <a href="' . $RootPath . '/reportwriter/ReportMaker.php?action=go&amp;reportid=' . $Report['id'] . '">' . _($Report['reportname']) . '</a></p>';
					$RptLinks .= '</li>';
					$NoEntries = false;
				}
			}
			// now fetch the form groups that are a part of this group (List after reports)
			$NoForms = true;
			foreach ($ReportList as $Report) {
				$Group=explode(':',$Report['groupname']); // break into main group and form group array
				if ($NoForms AND $Group[0]==$GroupID AND $Report['reporttype']=='frm' AND $Report['defaultreport']==$Def) {
                    $RptLinks .= '<li class="menu_group_item">';
					$RptLinks .= '<img src="' . $RootPath . '/css/' . $_SESSION['Theme'] . '/images/folders.gif" width="16" height="13" alt="" />&nbsp;';
					$RptLinks .= '<p>&bull; <a href="' . $RootPath . '/reportwriter/FormMaker.php?id=' . $Report['groupname'] . '"></p>';
					$RptLinks .= $FormGroups[$Report['groupname']] . '</a>';
					$RptLinks .= '</li>';
					$NoForms = false;
					$NoEntries = false;
				}
			}
		}
		if ($NoEntries) $RptLinks .= '<li class="menu_group_item">' . _('There are no reports to show!') . '</li>';
	}
	return $RptLinks;
}
	
?>
<!-- Modal HTML embedded directly into document -->
<div id="ex1" class="modal">
  <p>Thanks for clicking. That felt good.</p>
  <a href="#" rel="modal:close">Close</a>
</div>


<!-- Modal -->
 <div class="modal fade" id="getCodeModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
   <div class="modal-dialog modal-lg">
      <div class="modal-content">
       <div class="modal-header">
         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
         <h4 class="modal-title" id="myModalLabel"> API CODE </h4>
       </div>
       <div class="modal-body" id="getCode" style="overflow-x: scroll;">
       
          
          
       </div>
    </div>
   </div>
 </div>
<script>
jQuery(document).ready(function($){

   // event must be delegated from .xcrud
	
	function disableF5(e) {
		
if ((e.which || e.keyCode) == 116) 
	
	e.preventDefault(); 
	
	};
// To disable f5
    /* jQuery < 1.7 */
$(document).bind("keydown", disableF5);
/* OR jQuery >= 1.7 */
$(document).on("keydown", disableF5);

// To re-enable f5


	
	
	 });
	
	
	
	

	
	
	
	
</script>
