<?php
//this scripts updates pos system gl
//include ('includes/session.inc');
//include('includes/SQL_CommonFunctions.inc');

	//	$credit = $_SESSION['InventoryCreditSales'];

$userid = $_SESSION['UserID'];

//post pos sales GL
	if ($_SESSION['CompanyRecord']['gllink_debtors'] == 1) {

$SQL = "SELECT 	*
							 
			FROM debtortrans
			WHERE gl_status ='0'
			AND type = '11'
			AND cancelled = 0
			AND trandate >='2024-01-01'
			";
	
	$result = DB_query($SQL);
	while ($myrow = DB_fetch_array($result) ){
		
		$reference = $myrow['reference'];
		
		$erpinvoicenumber = mb_substr($reference,24,5);
		
		
$SQLc = "SELECT 	*
							 
			FROM debtortrans
			WHERE transno ='".$erpinvoicenumber."'
			";
	
	$resultc = DB_query($SQLc);
		$myrowc = DB_fetch_array($resultc);
	if (!isset($myrowc['projectid']) && $myrowc['projectid'] == '0' ){
		//get periods
				  $sqldate = $myrow['trandate'];
	
				$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['totalamount'] - $myrow['total_tax'];
	
		//update tax gl
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											10,
											'" . $myrow['transno']  . "',
											'" . $myrow['trandate'] . "',
											'" . $PeriodNo . "',
											'G421',
											'Credit Sales',
											'" . -$myrow['ovgst']. "',
											'" . $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 takes vip amount as this what its owed by accounting
		
		$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											10,
											'" . $myrow['transno']  . "',
											'" . $myrow['trandate'] . "',
											'" . $PeriodNo . "',
											' " .$_SESSION['CompanyRecord']['debtorsact']. "',
											'Credit Sales',
											'" . ($myrow['ovamount'] + $myrow['ovgst']) . "',
											'" . $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);
		
		//plastic
	
		
				  //get stockmoves no:
			//	  $AdjustmentNumber  = GetNextTransNo(17, $db);
			
				  	//get sales,debtors tax gl out
				  
		
		
		//sales

			
			
			//check stockid
		
		
	
	    
			  
			
			//sales
			$SQL = "INSERT INTO gltrans (type,
												typeno,
												trandate,
												periodno,
												account,
												narrative,
												amount,
												department)
										VALUES (
											10,
											'" . $myrow['transno']  . "',
											'" . $myrow['trandate'] . "',
											'" . $PeriodNo . "',
											'" . $myrowc['oldglcode']. "',
											'Credit Sales',
											'" . -$myrow['ovamount']. "',
											'" . $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
			
			
		
		}
	
		
		//update status
		
			
				$SQLS = "UPDATE debtortrans SET gl_status = '1'
									WHERE id ='" . $myrow['id'] . "'";


			$ResultS = DB_query($SQLS); 
		
		
	}


	}
		
		
		

	