<?php
//this scripts updates pos system gl

//error_reporting(E_ALL & ~E_NOTICE);

//include('includes/session.inc');
//$Title=_('ERP Home');
//include('includes/newheader.php');
//include('includes/SQL_CommonFunctions.inc');

$userid = $_SESSION['UserID'];

//nadi main 
//counter 1
$shop1 = new mysqli("localhost", "sooggoo", "4Compassion*", "bhairava_pos"); 
//post pos sales GL



$SQL = "SELECT 	*
							 
			FROM ospos_sales
			WHERE gl_posted ='0'
			AND mode = 'sale'
			";
	
	$result = mysqli_query($shop1,$SQL);
	while ($myrow =  mysqli_fetch_array($result) ){
		
		
	
		//get periods
				  $sqldate = $myrow['sale_time'];
	
				$yeardate = date('Y',strtotime($sqldate));
				$monthdate =date('m',strtotime($sqldate));
				$monthname =date('M',strtotime($sqldate)); 
				$query22 = "SELECT 
						periodno,
		             lastdate_in_period
		                 
					  
		          FROM periods
				    WHERE YEAR(lastdate_in_period) = '" . $yeardate. "'
					AND MONTH(lastdate_in_period)  = '" . $monthdate. "'";
				
        			$result22=DB_query($query22);
					$myrow22=DB_fetch_array($result22);
      
					$PeriodNo = $myrow22['periodno'];
					$data = $myrow22['lastdate_in_period'];	
		
		
		//update sales GL and tax then enter each item and reduce inventory gl. Then account for each items cost price and qty sold to reflect cost of goods sold
	$vepamount = $myrow['rounded_amount'] - $myrow['totaltax'];
	
		//update tax gl
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
											'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'2300',
											'POS VAT-".$myrow['loccode'].$myrow['terminal'].$myrow['sale_id']."',
											'" . -$myrow['vat']. "',
											'" . $myrow['department']  . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Sales Gl ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		
		
		
			if ($myrow['plastic'] !== '0') {
		
		//plastic
		$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
											'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'2311',
											'POS PlasticLevy-".$myrow['loccode'].$myrow['terminal'].$myrow['sale_id']."',
											'" . -$myrow['plastic']. "',
											'" . $myrow['department']  . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Sales Gl ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		
			}
		
		
		//update cash funds gl , undposited funds nd bank and mpaisa
		
		
		    $SQLpayments = "SELECT  payment_type
							 
			FROM ospos_sales_payments where sale_id ='" . $myrow['sale_id'] . "'
			";
	
	       $resultpayments= mysqli_query($shop1,$SQLpayments);
		
		    $myrow1payments = mysqli_fetch_array($resultpayments);
				  
		if ( $myrow1payments['payment_type'] == 'Cash' || $myrow1payments['payment_type'] == 'Cheque'){
			
			 $undepositedfunds =  $myrow['rounded_amount'];
			
			 $bankfunds = 0;
			
		}else{
			
			$undepositedfunds=0;
			$bankfunds = $myrow['totalamount'];
			
		}
				  
				
				  
				  
				  
			
		
		//undeposited funds
		if ($undepositedfunds > 0){
		$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
											'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'1020',
											'POS Cash-undepositedfunds-".$myrow['loccode'].$myrow['terminal'].$myrow['sale_id']."',
											'" . $undepositedfunds . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Bank Cash And Chq ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		}
		//bank account 
		if ($bankfunds > 0){
		$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
										'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'1030',
											'POS banktransfer-".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . $bankfunds . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Bank Cash And Chq ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		}
		//mpaisa
			if ($myrow1payments['payment_type'] == 'Mpaisa Or QR Pay'){
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
											'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'1021',
											'POS Mpaisa-".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . $myrow['totalamount'] . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Bank Cash And Chq ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		
			}
		
		
		//mycash
			if ($myrow1payments['payment_type'] == 'MyCash'){
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
											'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'1022',
											'POS MyCash -".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . $myrow['totalamount'] . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Bank Cash And Chq ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
			}
		
		
		
		    $SQL1 = "SELECT 	*
							 
			FROM ospos_sales_items where sale_id ='" . $myrow['sale_id'] . "'
			";
	
	       $result1 = mysqli_query($shop1,$SQL1);
		
		      while ($myrow1 = mysqli_fetch_array($result1) ){
				 $qty = $myrow1['quantity'] ;
				  //get stockmoves no:
		
			if ($myrow1['tax_label'] == 'D'){
				
				$vatrate = 0.15;
				
			}
		if ($myrow1['tax_label'] == 'A'){
				
				$vatrate = 0.09;
				
			}
			
			$vip = $myrow1['item_unit_price'] * $myrow1['quantity_purchased'];
			
			//cost price
			$totalcostvep = $myrow1['item_cost_price'] * $myrow1['quantity_purchased'];
				  
				 
				  
		    $SQLpayments1 = "SELECT *
							 
			FROM ospos_sales_payments where sale_id ='" . $myrow['sale_id'] . "'
			";
	
	       $resultpayments1= mysqli_query($shop1,$SQLpayments1);
		
		      $myrow1payments1 = mysqli_fetch_array($resultpayments1) ;
				  
		if ( $myrow1payments1['payment_type'] == 'Cash'){
			
			
			$grossamount=$vip;
					
						$valueInString = strval(round($grossamount,2));
if (strpos($valueInString, ".") == 0)
$valueInString = $valueInString.".00";
$valueArray = explode(".", $valueInString);
$substringValue = substr($valueArray[1], 1);

if ($substringValue >= 3 && $substringValue <= 7) {
$tempValue = str_replace(substr($valueArray[1], 1), 5, substr($valueArray[1], 1));
$tempValue = substr($valueArray[1],0,1).$tempValue;
$newvalue = floatval($valueArray[0].".".$tempValue);
} elseif ($substringValue == 1 ) {
$newvalue = $grossamount-0.01;
	} elseif ($substringValue == 2 ) {
	
	$newvalue = $grossamount-0.02;
	} elseif($substringValue == 0) {
$newvalue = floatval($grossamount);
} else {
$newFloat = floatval($valueArray[0].".".substr($valueArray[1],0,1));
$newvalue = ($newFloat+0.1);
}
		
		$rounded_amount = $newvalue;
		$rounded_amount =round($rounded_amount,2);
		$roundings = round(($grossamount - $rounded_amount),2);
		
			$vep = $rounded_amount/(1 + $vatrate);
			
			$vep = round($vep,2);
		
		}else{
			
			 $vep = $vip/(1 + $vatrate);
			
			$vep = round($vep,2);
				  
		}
				  
				  
			//we now know vep amount
	
			
			
			
		
		
		
		
				 //determine which stock category this item is
				
		    $SQL111 = "SELECT categoryid
							 
			FROM  stockmaster
			
			
			where stockid ='" . $myrow1['item_id'] . "'
			";
	
	       $result111 = DB_query($SQL111);
			
			$myrow111 = DB_fetch_array($result111);
				  //sales gl
			
	 $SQL1112 = "SELECT salesglcode
							 
			FROM  salesglpostings
			
			
			where stkcat ='" . $myrow111['categoryid'] . "'
			";
	
	       $result1112 = DB_query($SQL1112);
			
			$myrow1112 = DB_fetch_array($result1112);
				  
				  $salesgl = $myrow1112['salesglcode'];
				  
				  //cogsold
				  
				  $SQL11121 = "SELECT glcode
							 
			FROM  cogsglpostings
			
			
			where stkcat ='" . $myrow111['categoryid'] . "'
			";
	
	       $result11121 = DB_query($SQL11121);
			
			$myrow11121 = DB_fetch_array($result11121);
				  
				  $cogsgl = $myrow11121['glcode'];
				  
				  //get stock gl to hit
				  
				  $SQLstockgl = "SELECT stockact
							 
			FROM  stockcategory
			
			
			where categoryid ='" . $myrow111['categoryid'] . "'
			";
	
	       $resultstockgl = DB_query($SQLstockgl);
			
			$myrowstockgl = DB_fetch_array($resultstockgl);
				  
				  $stockgl = $myrowstockgl['stockact'];
	//$sdctime =  date("Y-m-d h:i:s a",$quotedated );
	    	//refund
  
			  
			
			//sales
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
											'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'" . $salesgl. "',
											'POS Sales GL".$myrow['sale_id']." -".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . -$vep . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Sales Gl ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		//debtors
			//" . $_SESSION['CompanyRecord']['debtorsact'] . "
			//this one is cash sales so check if cash payment or efpos,mpaisa. Cheq will be treated as undeposited funds
			
			
			
		
			
			
			
			//cost of goos sold
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
											'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'" . $cogsgl. "',
											'POS COGS".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . $totalcostvep . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Sales Gl ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		//stock
			
	//$cash= $_SESSION['InventoryCashSales'];
			
			
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
												'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'" . $stockgl . "',
											'POS Stock".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . -$totalcostvep . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Sales Gl ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
			
		
		
				  
		
		
		}//end sales_items table
	
		
		//update status
		
			
				$SQLS = "UPDATE ospos_sales SET gl_posted = '1'
									WHERE sale_id ='" . $myrow['sale_id'] . "'";


			$ResultS = mysqli_query($shop1,$SQLS); 
		
		
	}



//refund


$SQL = "SELECT 	*
							 
			FROM ospos_sales
			WHERE gl_posted ='0'
			AND mode = 'return'
			";
	
	$result = mysqli_query($shop1,$SQL);
		while ($myrow = mysqli_fetch_array($result) ){
		
		
	
		//get periods
				  $sqldate = $myrow['sale_time'];
	
				$yeardate = date('Y',strtotime($sqldate));
				$monthdate =date('m',strtotime($sqldate));
				$monthname =date('M',strtotime($sqldate)); 
				$query22 = "SELECT 
						periodno,
		             lastdate_in_period
		                 
					  
		          FROM periods
				    WHERE YEAR(lastdate_in_period) = '" . $yeardate. "'
					AND MONTH(lastdate_in_period)  = '" . $monthdate. "'";
				
        			$result22=DB_query($query22);
					$myrow22=DB_fetch_array($result22);
      
					$PeriodNo = $myrow22['periodno'];
					$data = $myrow22['lastdate_in_period'];	
		
		
		//update sales GL and tax then enter each item and reduce inventory gl. Then account for each items cost price and qty sold to reflect cost of goods sold
	$vepamount = $myrow['rounded_amount'] - $myrow['total_tax'];
	
		//update tax gl
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
												'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'2300',
											'POS Refund VAT-".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . -$myrow['vat']. "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Sales Gl ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		
		
		
			if ($myrow['plastic'] !=='0'){
		
		//plastic
		$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
												'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'2311',
											'POS Refund PlasticLevy-".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . -$myrow['plastic']. "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Sales Gl ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		
		
			}
		
		//update cash funds gl , undposited funds nd bank and mpaisa
				
		    $SQLpayments = "SELECT  payment_type
							 
			FROM ospos_sales_payments where sale_id ='" . $myrow['sale_id'] . "'
			";
	
	       $resultpayments= mysqli_query($shop1,$SQLpayments);
		
		    $myrow1payments = mysqli_fetch_array($resultpayments);
				  
		if ( $myrow1payments['payment_type'] == 'Cash' || $myrow1payments['payment_type'] == 'Cheque'){
			
			 $undepositedfunds =  $myrow['rounded_amount'];
			
			 $bankfunds = 0;
			
		}else{
			
			$undepositedfunds=0;
			$bankfunds = $myrow['totalamount'];
			
		}
			
		
		//undeposited funds
		if ($undepositedfunds < 0){
		$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
												'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'1020',
											'POS Refund Cash-undepositedfunds-".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . $undepositedfunds . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Bank Cash And Chq ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		}
		//bank account 
		if ($bankfunds < 0){
		$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
												'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'1030',
											'POS banktransfer-".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . $bankfunds . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Bank Cash And Chq ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		}
		//mpaisa
		if ($myrow1payments['payment_type'] == 'Mpaisa Or QR Pay'){
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
												'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'1021',
											'POS Refund Mpaisa-".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . $myrow['totalamount'] . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Bank Cash And Chq ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		
			}
		
			if ($myrow1payments['payment_type'] == 'MyCash'){
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
												'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'1022',
											'POS Refund MyCash -".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . $myrow['totalamount'] . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Bank Cash And Chq ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
			}
		
		
		
		    $SQL1 = "SELECT 	*
							 
			FROM ospos_sales_items where sale_id ='" . $myrow['sale_id'] . "'
			";
	
	       $result1 = mysqli_query($shop1,$SQL1);
		
		      while ($myrow1 = mysqli_fetch_array($result1) ){
				 $qty = $myrow1['quantity'] ;
				  //get stockmoves no:
		
			if ($myrow1['tax_label'] == 'D'){
				
				$vatrate = 0.15;
				
			}
		if ($myrow1['tax_label'] == 'A'){
				
				$vatrate = 0.09;
				
			}
			
			$vip = $myrow1['item_unit_price'] * $myrow1['quantity_purchased'];
			
			//cost price
			$totalcostvep = $myrow1['item_cost_price'] * $myrow1['quantity_purchased'];
			//we now know vep amount
	$SQLpayments1 = "SELECT *
							 
			FROM ospos_sales_payments where sale_id ='" . $myrow['sale_id'] . "'
			";
	
	       $resultpayments1= mysqli_query($shop1,$SQLpayments1);
		
		      $myrow1payments1 = mysqli_fetch_array($resultpayments1) ;
				  
		if ( $myrow1payments1['payment_type'] == 'Cash'){
			
			
			$grossamount=$vip;
					
						$valueInString = strval(round($grossamount,2));
if (strpos($valueInString, ".") == 0)
$valueInString = $valueInString.".00";
$valueArray = explode(".", $valueInString);
$substringValue = substr($valueArray[1], 1);

if ($substringValue >= 3 && $substringValue <= 7) {
$tempValue = str_replace(substr($valueArray[1], 1), 5, substr($valueArray[1], 1));
$tempValue = substr($valueArray[1],0,1).$tempValue;
$newvalue = floatval($valueArray[0].".".$tempValue);
} elseif ($substringValue == 1 ) {
$newvalue = $grossamount-0.01;
	} elseif ($substringValue == 2 ) {
	
	$newvalue = $grossamount-0.02;
	} elseif($substringValue == 0) {
$newvalue = floatval($grossamount);
} else {
$newFloat = floatval($valueArray[0].".".substr($valueArray[1],0,1));
$newvalue = ($newFloat+0.1);
}
		
		$rounded_amount = $newvalue;
		$rounded_amount =round($rounded_amount,2);
		$roundings = round(($grossamount - $rounded_amount),2);
		
			$vep = $rounded_amount/(1 + $vatrate);
			
			$vep = round($vep,2);
		
		}else{
			
			 $vep = $vip/(1 + $vatrate);
			
			$vep = round($vep,2);
				  
		}
				  
				  
		
			
		
		
		
		
		
				 //determine which stock category this item is
				
		    $SQL111 = "SELECT categoryid
							 
			FROM  stockmaster
			
			
			where stockid ='" . $myrow1['item_id'] . "'
			";
	
	       $result111 = DB_query($SQL111);
			
			$myrow111 = DB_fetch_array($result111);
				  //sales gl
			
	 $SQL1112 = "SELECT salesglcode
							 
			FROM  salesglpostings
			
			
			where stkcat ='" . $myrow111['categoryid'] . "'
			";
	
	       $result1112 = DB_query($SQL1112);
			
			$myrow1112 = DB_fetch_array($result1112);
				  
				  $salesgl = $myrow1112['salesglcode'];
				  
				  //cogsold
				  
				  $SQL11121 = "SELECT glcode
							 
			FROM  cogsglpostings
			
			
			where stkcat ='" . $myrow111['categoryid'] . "'
			";
	
	       $result11121 = DB_query($SQL11121);
			
			$myrow11121 = DB_fetch_array($result11121);
				  
				  $cogsgl = $myrow11121['glcode'];
				  
				  //get stock gl to hit
				  
				  $SQLstockgl = "SELECT stockact
							 
			FROM  stockcategory
			
			
			where categoryid ='" . $myrow111['categoryid'] . "'
			";
	
	       $resultstockgl = DB_query($SQLstockgl);
			
			$myrowstockgl = DB_fetch_array($resultstockgl);
				  
				  $stockgl = $myrowstockgl['stockact'];
	//$sdctime =  date("Y-m-d h:i:s a",$quotedated );
	    	//refund
  
			  
			
			//sales
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
											'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'" . $salesgl. "',
											'POS Refund Sales GL".$myrow['sale_id']." -".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . -$vep . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Sales Gl ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		//debtors
			//" . $_SESSION['CompanyRecord']['debtorsact'] . "
			//this one is cash sales so check if cash payment or efpos,mpaisa. Cheq will be treated as undeposited funds
			
			
			
		
			
			
			
			//cost of goos sold
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
												'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'" . $cogsgl. "',
											'POS Refund COGS".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . -$totalcostvep . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Sales Gl ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
		//stock
			
	//$cash= $_SESSION['InventoryCashSales'];
			
			
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											69,
												'" . $myrow['sale_id']  . "',
											'" . $myrow['sale_time'] . "',
											'" . $PeriodNo . "',
											'" . $stockgl . "',
											'POS Refund Stock".$myrow['loccode']." -".$myrow['terminal']."-".$myrow['sale_id']."',
											'" . -$totalcostvep . "',
											'" . $myrow['department'] . "')";

					$ErrMsg = _('CRITICAL ERROR') . '! ' . _('NOTE DOWN THIS ERROR AND SEEK ASSISTANCE') . ': ' . _('Sales Gl ');
					$DbgMsg = _('The following SQL to insert the GLTrans record was used');
					$Result = DB_query($SQL,$ErrMsg,$DbgMsg,true);
			
		
		
				  
		
		
		}
	
		
		//update status
		
			
				$SQLS = "UPDATE ospos_sales SET gl_posted = '1'
									WHERE sale_id ='" . $myrow['sale_id'] . "'";


			$ResultS = mysqli_query($shop1,$SQLS); 
		
		
	}


mysqli_close($shop1);



	