<?php
$tdataloandata = array();
$tdataloandata[".searchableFields"] = array();
$tdataloandata[".ShortName"] = "loandata";
$tdataloandata[".OwnerID"] = "";
$tdataloandata[".OriginalTable"] = "loans";


$tdataloandata[".pagesByType"] = my_json_decode( "{\"list\":[\"list\"],\"search\":[\"search\"]}" );
$tdataloandata[".originalPagesByType"] = $tdataloandata[".pagesByType"];
$tdataloandata[".pages"] = types2pages( my_json_decode( "{\"list\":[\"list\"],\"search\":[\"search\"]}" ) );
$tdataloandata[".originalPages"] = $tdataloandata[".pages"];
$tdataloandata[".defaultPages"] = my_json_decode( "{\"list\":\"list\",\"search\":\"search\"}" );
$tdataloandata[".originalDefaultPages"] = $tdataloandata[".defaultPages"];

//	field labels
$fieldLabelsloandata = array();
$fieldToolTipsloandata = array();
$pageTitlesloandata = array();
$placeHoldersloandata = array();

if(mlang_getcurrentlang()=="English")
{
	$fieldLabelsloandata["English"] = array();
	$fieldToolTipsloandata["English"] = array();
	$placeHoldersloandata["English"] = array();
	$pageTitlesloandata["English"] = array();
	$fieldLabelsloandata["English"]["loan_id"] = "Loan Id";
	$fieldToolTipsloandata["English"]["loan_id"] = "";
	$placeHoldersloandata["English"]["loan_id"] = "";
	$fieldLabelsloandata["English"]["name"] = "Name";
	$fieldToolTipsloandata["English"]["name"] = "";
	$placeHoldersloandata["English"]["name"] = "";
	$fieldLabelsloandata["English"]["details"] = "Details";
	$fieldToolTipsloandata["English"]["details"] = "";
	$placeHoldersloandata["English"]["details"] = "";
	$fieldLabelsloandata["English"]["arrears_amount"] = "Arrears Amount";
	$fieldToolTipsloandata["English"]["arrears_amount"] = "";
	$placeHoldersloandata["English"]["arrears_amount"] = "";
	if (count($fieldToolTipsloandata["English"]))
		$tdataloandata[".isUseToolTips"] = true;
}


	$tdataloandata[".NCSearch"] = true;



$tdataloandata[".shortTableName"] = "loandata";
$tdataloandata[".nSecOptions"] = 0;

$tdataloandata[".mainTableOwnerID"] = "";
$tdataloandata[".entityType"] = 1;
$tdataloandata[".connId"] = "easy_cash_at_localhost";


$tdataloandata[".strOriginalTableName"] = "loans";

	



$tdataloandata[".showAddInPopup"] = false;

$tdataloandata[".showEditInPopup"] = false;

$tdataloandata[".showViewInPopup"] = false;

//page's base css files names
$popupPagesLayoutNames = array();
$tdataloandata[".popupPagesLayoutNames"] = $popupPagesLayoutNames;


	$tdataloandata[".listAjax"] = true;
//	temporary
$tdataloandata[".listAjax"] = false;

	$tdataloandata[".audit"] = true;

	$tdataloandata[".locking"] = false;


$pages = $tdataloandata[".defaultPages"];

if( $pages[PAGE_EDIT] ) {
	$tdataloandata[".edit"] = true;
	$tdataloandata[".afterEditAction"] = 1;
	$tdataloandata[".closePopupAfterEdit"] = 1;
	$tdataloandata[".afterEditActionDetTable"] = "";
}

if( $pages[PAGE_ADD] ) {
$tdataloandata[".add"] = true;
$tdataloandata[".afterAddAction"] = 2;
$tdataloandata[".closePopupAfterAdd"] = 1;
$tdataloandata[".afterAddActionDetTable"] = "Detail tables not found!";
}

if( $pages[PAGE_LIST] ) {
	$tdataloandata[".list"] = true;
}



$tdataloandata[".strSortControlSettingsJSON"] = "";




if( $pages[PAGE_VIEW] ) {
$tdataloandata[".view"] = true;
}

if( $pages[PAGE_IMPORT] ) {
$tdataloandata[".import"] = true;
}

if( $pages[PAGE_EXPORT] ) {
$tdataloandata[".exportTo"] = true;
}

if( $pages[PAGE_PRINT] ) {
$tdataloandata[".printFriendly"] = true;
}



$tdataloandata[".showSimpleSearchOptions"] = true; // temp fix #13449

// Allow Show/Hide Fields in GRID
$tdataloandata[".allowShowHideFields"] = true; // temp fix #13449
//

// Allow Fields Reordering in GRID
$tdataloandata[".allowFieldsReordering"] = true; // temp fix #13449
//

$tdataloandata[".isUseAjaxSuggest"] = true;

$tdataloandata[".rowHighlite"] = true;



																								

$tdataloandata[".ajaxCodeSnippetAdded"] = false;

$tdataloandata[".buttonsAdded"] = false;

$tdataloandata[".addPageEvents"] = false;

// use timepicker for search panel
$tdataloandata[".isUseTimeForSearch"] = false;


$tdataloandata[".badgeColor"] = "E67349";


$tdataloandata[".allSearchFields"] = array();
$tdataloandata[".filterFields"] = array();
$tdataloandata[".requiredSearchFields"] = array();

$tdataloandata[".googleLikeFields"] = array();
$tdataloandata[".googleLikeFields"][] = "loan_id";
$tdataloandata[".googleLikeFields"][] = "name";
$tdataloandata[".googleLikeFields"][] = "details";
$tdataloandata[".googleLikeFields"][] = "arrears_amount";



$tdataloandata[".tableType"] = "list";

$tdataloandata[".printerPageOrientation"] = 0;
$tdataloandata[".nPrinterPageScale"] = 100;

$tdataloandata[".nPrinterSplitRecords"] = 40;

$tdataloandata[".geocodingEnabled"] = false;





$tdataloandata[".isResizeColumns"] = true;





$tdataloandata[".pageSize"] = 20;

$tdataloandata[".warnLeavingPages"] = true;



$tstrOrderBy = "ORDER BY t.loan_id DESC";
if(strlen($tstrOrderBy) && strtolower(substr($tstrOrderBy,0,8))!="order by")
	$tstrOrderBy = "order by ".$tstrOrderBy;
$tdataloandata[".strOrderBy"] = $tstrOrderBy;

$tdataloandata[".orderindexes"] = array();
	$tdataloandata[".orderindexes"][] = array(1, (0 ? "ASC" : "DESC"), "loan_id");



$tdataloandata[".sqlHead"] = "SELECT t.loan_id,      CONCAT(          t.full_name, ' / ',          t.nic, ' / ',          t.phone, ' / ',          t.address      ) AS name,      CONCAT(  					\"ණය අංකය: \", t.loan_id,\", \", \"\\n\",          \"ණය: \", t.loan_amount,\", \", \"\\n\",           \"සේවා: \", t.profit_amount,\", \", \"\\n\",          \"මුළු ගෙවීම: \", t.total_payable,\", \", \"\\n\",          \"වාරිකය: \", t.installment_amount,\", \", \"\\n\",          \"කාලය: \", t.payment_interval_days,\", \", \"\\n\",          \"අරම්භය: \", t.start_date,\", \", \"\\n\",          \"අවසානය: \", t.end_date,\", \", \"\\n\",          \"තත්ත්වය: \", t.status,\", \", \"\\n\",           \"තව දින: \", t.remaining_days,\", \", \"\\n\",          \"තව වාරික: \", t.remaining_installments,\", \", \"\\n\",          \"ගෙවූ මුදල: \", t.total_collected,\", \", \"\\n\",          \"අදට ගෙවිය යුතු: \", t.should_pay,\", \", \"\\n\",          \"හිඟ: \", t.arrears_amount, \"\\n\"      ) AS details, t.arrears_amount";
$tdataloandata[".sqlFrom"] = "FROM (      SELECT          l.loan_id,          l.customer_id,          l.horno,          l.guarantor_1,          l.guarantor_2,            /* Customer fields */          cus.full_name AS full_name,          cus.nic AS nic,          cus.phone AS phone,          cus.address AS address,            l.loan_amount,          l.total_payable,          l.installment_amount,          l.payment_interval_days,          l.start_date,          l.end_date,          l.status,          l.Old_Loan,          l.profit_amount,            IFNULL(SUM(c.amount), 0) AS total_collected,            /* Calculations – capped to total_payable */          LEAST(              (FLOOR(DATEDIFF(CURDATE(), l.start_date) / l.payment_interval_days) + 1)              * l.installment_amount,              l.total_payable          ) AS should_pay,            (l.total_payable - IFNULL(SUM(c.amount), 0)) AS remaining_balance,            GREATEST(              LEAST(                  (FLOOR(DATEDIFF(CURDATE(), l.start_date) / l.payment_interval_days) + 1)                  * l.installment_amount,                  l.total_payable              ) - IFNULL(SUM(c.amount), 0),              0          ) AS arrears_amount,            DATEDIFF(l.end_date, CURDATE()) AS remaining_days,            GREATEST(              ROUND((l.total_payable / l.installment_amount)                  - (IFNULL(SUM(c.amount), 0) / l.installment_amount), 0),              0          ) AS remaining_installments        FROM loans l      LEFT JOIN customers cus ON l.customer_id = cus.customer_id      LEFT JOIN collections c ON l.loan_id = c.loan_id      WHERE l.status = 'active' OR l.status = 'Recovered'      GROUP BY l.loan_id  ) AS t";
$tdataloandata[".sqlWhereExpr"] = "";
$tdataloandata[".sqlTail"] = "";

//fill array of tabs for list page
$arrGridTabs = array();
$arrGridTabs[] = array(
	'tabId' => "",
	'name' => "All data",
	'nameType' => 'Text',
	'where' => "",
	'showRowCount' => 1,
	'hideEmpty' => 0,
);
$arrGridTabs[] = array(
	'tabId' => "10",
	'name' => "Avtive",
	'nameType' => 'Text',
	'where' => "status = 'active'",
	'showRowCount' => 1,
	'hideEmpty' => 0,
);
$arrGridTabs[] = array(
	'tabId' => "11",
	'name' => "Closed",
	'nameType' => 'Text',
	'where' => "status = 'closed'",
	'showRowCount' => 1,
	'hideEmpty' => 0,
);
$arrGridTabs[] = array(
	'tabId' => "12",
	'name' => "Defaulted",
	'nameType' => 'Text',
	'where' => "status = 'defaulted'",
	'showRowCount' => 1,
	'hideEmpty' => 0,
);
$arrGridTabs[] = array(
	'tabId' => "13",
	'name' => "Recovered",
	'nameType' => 'Text',
	'where' => "status = 'Recovered'",
	'showRowCount' => 1,
	'hideEmpty' => 0,
);
$tdataloandata[".arrGridTabs"] = $arrGridTabs;









//fill array of records per page for list and report without group fields
$arrRPP = array();
$arrRPP[] = 10;
$arrRPP[] = 20;
$arrRPP[] = 30;
$arrRPP[] = 50;
$arrRPP[] = 100;
$arrRPP[] = 500;
$arrRPP[] = -1;
$tdataloandata[".arrRecsPerPage"] = $arrRPP;

//fill array of groups per page for report with group fields
$arrGPP = array();
$arrGPP[] = 1;
$arrGPP[] = 3;
$arrGPP[] = 5;
$arrGPP[] = 10;
$arrGPP[] = 50;
$arrGPP[] = 100;
$arrGPP[] = -1;
$tdataloandata[".arrGroupsPerPage"] = $arrGPP;

$tdataloandata[".highlightSearchResults"] = true;

$tableKeysloandata = array();
$tableKeysloandata[] = "loan_id";
$tdataloandata[".Keys"] = $tableKeysloandata;


$tdataloandata[".hideMobileList"] = array();




//	loan_id
//	Custom field settings
	$fdata = array();
	$fdata["Index"] = 1;
	$fdata["strName"] = "loan_id";
	$fdata["GoodName"] = "loan_id";
	$fdata["ownerTable"] = "";
	$fdata["Label"] = GetFieldLabel("loandata","loan_id");
	$fdata["FieldType"] = 3;

	
		$fdata["AutoInc"] = true;

	
			

		$fdata["strField"] = "loan_id";

	
		$fdata["isSQLExpression"] = true;
	$fdata["FullName"] = "t.loan_id";

	
	
				$fdata["UploadFolder"] = "files";

//  Begin View Formats
	$fdata["ViewFormats"] = array();

	$vdata = array("ViewFormat" => "");

	
	
	
	
	
	
	
	
	
	
	
		
	
		$vdata["NeedEncode"] = true;

	
		$vdata["truncateText"] = true;
	$vdata["NumberOfChars"] = 80;

	$fdata["ViewFormats"]["view"] = $vdata;
//  End View Formats

//	Begin Edit Formats
	$fdata["EditFormats"] = array();

	$edata = array("EditFormat" => "Text field");

	
		$edata["weekdayMessage"] = array("message" => "", "messageType" => "Text");
	$edata["weekdays"] = "[]";


	
	



		$edata["IsRequired"] = true;

	
	
	
			$edata["acceptFileTypes"] = ".+$";
		$edata["acceptFileTypesHtml"] = "";

		$edata["maxNumberOfFiles"] = 1;

	
	
	
	
			$edata["HTML5InuptType"] = "text";

		$edata["EditParams"] = "";
		
		$edata["controlWidth"] = 200;

//	Begin validation
	$edata["validateAs"] = array();
	$edata["validateAs"]["basicValidate"] = array();
	$edata["validateAs"]["customMessages"] = array();
				$edata["validateAs"]["basicValidate"][] = getJsValidatorName("Number");
						$edata["validateAs"]["basicValidate"][] = "IsRequired";
		
	
	//	End validation

	
			
	
	
	
	$fdata["EditFormats"]["edit"] = $edata;
//	End Edit Formats


	$fdata["isSeparate"] = false;




// the field's search options settings
		$fdata["defaultSearchOption"] = "Contains";

			// the default search options list
				$fdata["searchOptionsList"] = array("Contains", "Equals", "Starts with", "More than", "Less than", "Between", "Empty", NOT_EMPTY);
// the end of search options settings


//Filters settings
	$fdata["filterTotals"] = 0;
		$fdata["filterMultiSelect"] = 0;
			$fdata["filterFormat"] = "Values list";
		$fdata["showCollapsed"] = false;

		$fdata["sortValueType"] = 0;
		$fdata["numberOfVisibleItems"] = 10;

		$fdata["filterBy"] = 0;

	

	
	
//end of Filters settings


	$tdataloandata["loan_id"] = $fdata;
		$tdataloandata[".searchableFields"][] = "loan_id";
//	name
//	Custom field settings
	$fdata = array();
	$fdata["Index"] = 2;
	$fdata["strName"] = "name";
	$fdata["GoodName"] = "name";
	$fdata["ownerTable"] = "";
	$fdata["Label"] = GetFieldLabel("loandata","name");
	$fdata["FieldType"] = 200;

	
	
	
			

		$fdata["strField"] = "name";

	
		$fdata["isSQLExpression"] = true;
	$fdata["FullName"] = "CONCAT(          t.full_name, ' / ',          t.nic, ' / ',          t.phone, ' / ',          t.address      )";

	
	
				$fdata["UploadFolder"] = "files";

//  Begin View Formats
	$fdata["ViewFormats"] = array();

	$vdata = array("ViewFormat" => "");

	
	
	
	
	
	
	
	
	
	
	
		
	
		$vdata["NeedEncode"] = true;

	
		$vdata["truncateText"] = true;
	$vdata["NumberOfChars"] = 80;

	$fdata["ViewFormats"]["view"] = $vdata;
//  End View Formats

//	Begin Edit Formats
	$fdata["EditFormats"] = array();

	$edata = array("EditFormat" => "Text field");

	
		$edata["weekdayMessage"] = array("message" => "", "messageType" => "Text");
	$edata["weekdays"] = "[]";


	
	



	
	
	
	
			$edata["acceptFileTypes"] = ".+$";
		$edata["acceptFileTypesHtml"] = "";

		$edata["maxNumberOfFiles"] = 1;

	
	
	
	
			$edata["HTML5InuptType"] = "text";

		$edata["EditParams"] = "";
		
		$edata["controlWidth"] = 200;

//	Begin validation
	$edata["validateAs"] = array();
	$edata["validateAs"]["basicValidate"] = array();
	$edata["validateAs"]["customMessages"] = array();
	
	
	//	End validation

	
			
	
	
	
	$fdata["EditFormats"]["edit"] = $edata;
//	End Edit Formats


	$fdata["isSeparate"] = false;




// the field's search options settings
		$fdata["defaultSearchOption"] = "Contains";

			// the default search options list
				$fdata["searchOptionsList"] = array("Contains", "Equals", "Starts with", "More than", "Less than", "Between", "Empty", NOT_EMPTY);
// the end of search options settings


//Filters settings
	$fdata["filterTotals"] = 0;
		$fdata["filterMultiSelect"] = 0;
			$fdata["filterFormat"] = "Values list";
		$fdata["showCollapsed"] = false;

		$fdata["sortValueType"] = 0;
		$fdata["numberOfVisibleItems"] = 10;

		$fdata["filterBy"] = 0;

	

	
	
//end of Filters settings


	$tdataloandata["name"] = $fdata;
		$tdataloandata[".searchableFields"][] = "name";
//	details
//	Custom field settings
	$fdata = array();
	$fdata["Index"] = 3;
	$fdata["strName"] = "details";
	$fdata["GoodName"] = "details";
	$fdata["ownerTable"] = "";
	$fdata["Label"] = GetFieldLabel("loandata","details");
	$fdata["FieldType"] = 200;

	
	
	
			

		$fdata["strField"] = "details";

	
		$fdata["isSQLExpression"] = true;
	$fdata["FullName"] = "CONCAT(  					\"ණය අංකය: \", t.loan_id,\", \", \"\\n\",          \"ණය: \", t.loan_amount,\", \", \"\\n\",           \"සේවා: \", t.profit_amount,\", \", \"\\n\",          \"මුළු ගෙවීම: \", t.total_payable,\", \", \"\\n\",          \"වාරිකය: \", t.installment_amount,\", \", \"\\n\",          \"කාලය: \", t.payment_interval_days,\", \", \"\\n\",          \"අරම්භය: \", t.start_date,\", \", \"\\n\",          \"අවසානය: \", t.end_date,\", \", \"\\n\",          \"තත්ත්වය: \", t.status,\", \", \"\\n\",           \"තව දින: \", t.remaining_days,\", \", \"\\n\",          \"තව වාරික: \", t.remaining_installments,\", \", \"\\n\",          \"ගෙවූ මුදල: \", t.total_collected,\", \", \"\\n\",          \"අදට ගෙවිය යුතු: \", t.should_pay,\", \", \"\\n\",          \"හිඟ: \", t.arrears_amount, \"\\n\"      )";

	
	
				$fdata["UploadFolder"] = "files";

//  Begin View Formats
	$fdata["ViewFormats"] = array();

	$vdata = array("ViewFormat" => "");

	
	
	
	
	
	
	
	
	
	
	
		
	
		$vdata["NeedEncode"] = true;

	
		$vdata["truncateText"] = true;
	$vdata["NumberOfChars"] = 80;

	$fdata["ViewFormats"]["view"] = $vdata;
//  End View Formats

//	Begin Edit Formats
	$fdata["EditFormats"] = array();

	$edata = array("EditFormat" => "Text field");

	
		$edata["weekdayMessage"] = array("message" => "", "messageType" => "Text");
	$edata["weekdays"] = "[]";


	
	



	
	
	
	
			$edata["acceptFileTypes"] = ".+$";
		$edata["acceptFileTypesHtml"] = "";

		$edata["maxNumberOfFiles"] = 1;

	
	
	
	
			$edata["HTML5InuptType"] = "text";

		$edata["EditParams"] = "";
		
		$edata["controlWidth"] = 200;

//	Begin validation
	$edata["validateAs"] = array();
	$edata["validateAs"]["basicValidate"] = array();
	$edata["validateAs"]["customMessages"] = array();
	
	
	//	End validation

	
			
	
	
	
	$fdata["EditFormats"]["edit"] = $edata;
//	End Edit Formats


	$fdata["isSeparate"] = false;




// the field's search options settings
		$fdata["defaultSearchOption"] = "Contains";

			// the default search options list
				$fdata["searchOptionsList"] = array("Contains", "Equals", "Starts with", "More than", "Less than", "Between", "Empty", NOT_EMPTY);
// the end of search options settings


//Filters settings
	$fdata["filterTotals"] = 0;
		$fdata["filterMultiSelect"] = 0;
			$fdata["filterFormat"] = "Values list";
		$fdata["showCollapsed"] = false;

		$fdata["sortValueType"] = 0;
		$fdata["numberOfVisibleItems"] = 10;

		$fdata["filterBy"] = 0;

	

	
	
//end of Filters settings


	$tdataloandata["details"] = $fdata;
		$tdataloandata[".searchableFields"][] = "details";
//	arrears_amount
//	Custom field settings
	$fdata = array();
	$fdata["Index"] = 4;
	$fdata["strName"] = "arrears_amount";
	$fdata["GoodName"] = "arrears_amount";
	$fdata["ownerTable"] = "";
	$fdata["Label"] = GetFieldLabel("loandata","arrears_amount");
	$fdata["FieldType"] = 14;

	
	
	
			

		$fdata["strField"] = "arrears_amount";

	
		$fdata["isSQLExpression"] = true;
	$fdata["FullName"] = "t.arrears_amount";

	
	
				$fdata["UploadFolder"] = "files";

//  Begin View Formats
	$fdata["ViewFormats"] = array();

	$vdata = array("ViewFormat" => "Number");

	
	
	
	
	
	
		$vdata["DecimalDigits"] = 0;

	
	
	
	
		
	
		$vdata["NeedEncode"] = true;

	
		$vdata["truncateText"] = true;
	$vdata["NumberOfChars"] = 80;

	$fdata["ViewFormats"]["view"] = $vdata;
//  End View Formats

//	Begin Edit Formats
	$fdata["EditFormats"] = array();

	$edata = array("EditFormat" => "Text field");

	
		$edata["weekdayMessage"] = array("message" => "", "messageType" => "Text");
	$edata["weekdays"] = "[]";


	
	



		$edata["IsRequired"] = true;

	
	
	
			$edata["acceptFileTypes"] = ".+$";
		$edata["acceptFileTypesHtml"] = "";

		$edata["maxNumberOfFiles"] = 1;

	
	
	
	
			$edata["HTML5InuptType"] = "text";

		$edata["EditParams"] = "";
		
		$edata["controlWidth"] = 200;

//	Begin validation
	$edata["validateAs"] = array();
	$edata["validateAs"]["basicValidate"] = array();
	$edata["validateAs"]["customMessages"] = array();
				$edata["validateAs"]["basicValidate"][] = getJsValidatorName("Number");
						$edata["validateAs"]["basicValidate"][] = "IsRequired";
		
	
	//	End validation

	
			
	
	
	
	$fdata["EditFormats"]["edit"] = $edata;
//	End Edit Formats


	$fdata["isSeparate"] = false;




// the field's search options settings
		$fdata["defaultSearchOption"] = "Contains";

			// the default search options list
				$fdata["searchOptionsList"] = array("Contains", "Equals", "Starts with", "More than", "Less than", "Between", "Empty", NOT_EMPTY);
// the end of search options settings


//Filters settings
	$fdata["filterTotals"] = 0;
		$fdata["filterMultiSelect"] = 0;
			$fdata["filterFormat"] = "Values list";
		$fdata["showCollapsed"] = false;

		$fdata["sortValueType"] = 0;
		$fdata["numberOfVisibleItems"] = 10;

		$fdata["filterBy"] = 0;

	

	
	
//end of Filters settings


	$tdataloandata["arrears_amount"] = $fdata;
		$tdataloandata[".searchableFields"][] = "arrears_amount";


$tables_data["loandata"]=&$tdataloandata;
$field_labels["loandata"] = &$fieldLabelsloandata;
$fieldToolTips["loandata"] = &$fieldToolTipsloandata;
$placeHolders["loandata"] = &$placeHoldersloandata;
$page_titles["loandata"] = &$pageTitlesloandata;

// -----------------start  prepare master-details data arrays ------------------------------//
// tables which are detail tables for current table (master)
$detailsTablesData["loandata"] = array();

// tables which are master tables for current table (detail)
$masterTablesData["loandata"] = array();



// -----------------end  prepare master-details data arrays ------------------------------//



require_once(getabspath("classes/sql.php"));











function createSqlQuery_loandata()
{
$proto0=array();
$proto0["m_strHead"] = "SELECT";
$proto0["m_strFieldList"] = "t.loan_id,      CONCAT(          t.full_name, ' / ',          t.nic, ' / ',          t.phone, ' / ',          t.address      ) AS name,      CONCAT(  					\"ණය අංකය: \", t.loan_id,\", \", \"\\n\",          \"ණය: \", t.loan_amount,\", \", \"\\n\",           \"සේවා: \", t.profit_amount,\", \", \"\\n\",          \"මුළු ගෙවීම: \", t.total_payable,\", \", \"\\n\",          \"වාරිකය: \", t.installment_amount,\", \", \"\\n\",          \"කාලය: \", t.payment_interval_days,\", \", \"\\n\",          \"අරම්භය: \", t.start_date,\", \", \"\\n\",          \"අවසානය: \", t.end_date,\", \", \"\\n\",          \"තත්ත්වය: \", t.status,\", \", \"\\n\",           \"තව දින: \", t.remaining_days,\", \", \"\\n\",          \"තව වාරික: \", t.remaining_installments,\", \", \"\\n\",          \"ගෙවූ මුදල: \", t.total_collected,\", \", \"\\n\",          \"අදට ගෙවිය යුතු: \", t.should_pay,\", \", \"\\n\",          \"හිඟ: \", t.arrears_amount, \"\\n\"      ) AS details, t.arrears_amount";
$proto0["m_strFrom"] = "FROM (      SELECT          l.loan_id,          l.customer_id,          l.horno,          l.guarantor_1,          l.guarantor_2,            /* Customer fields */          cus.full_name AS full_name,          cus.nic AS nic,          cus.phone AS phone,          cus.address AS address,            l.loan_amount,          l.total_payable,          l.installment_amount,          l.payment_interval_days,          l.start_date,          l.end_date,          l.status,          l.Old_Loan,          l.profit_amount,            IFNULL(SUM(c.amount), 0) AS total_collected,            /* Calculations – capped to total_payable */          LEAST(              (FLOOR(DATEDIFF(CURDATE(), l.start_date) / l.payment_interval_days) + 1)              * l.installment_amount,              l.total_payable          ) AS should_pay,            (l.total_payable - IFNULL(SUM(c.amount), 0)) AS remaining_balance,            GREATEST(              LEAST(                  (FLOOR(DATEDIFF(CURDATE(), l.start_date) / l.payment_interval_days) + 1)                  * l.installment_amount,                  l.total_payable              ) - IFNULL(SUM(c.amount), 0),              0          ) AS arrears_amount,            DATEDIFF(l.end_date, CURDATE()) AS remaining_days,            GREATEST(              ROUND((l.total_payable / l.installment_amount)                  - (IFNULL(SUM(c.amount), 0) / l.installment_amount), 0),              0          ) AS remaining_installments        FROM loans l      LEFT JOIN customers cus ON l.customer_id = cus.customer_id      LEFT JOIN collections c ON l.loan_id = c.loan_id      WHERE l.status = 'active' OR l.status = 'Recovered'      GROUP BY l.loan_id  ) AS t";
$proto0["m_strWhere"] = "";
$proto0["m_strOrderBy"] = "ORDER BY t.loan_id DESC";
	
		;
			$proto0["cipherer"] = null;
$proto2=array();
$proto2["m_sql"] = "";
$proto2["m_uniontype"] = "SQLL_UNKNOWN";
	$obj = new SQLNonParsed(array(
	"m_sql" => ""
));

$proto2["m_column"]=$obj;
$proto2["m_contained"] = array();
$proto2["m_strCase"] = "";
$proto2["m_havingmode"] = false;
$proto2["m_inBrackets"] = false;
$proto2["m_useAlias"] = false;
$obj = new SQLLogicalExpr($proto2);

$proto0["m_where"] = $obj;
$proto4=array();
$proto4["m_sql"] = "";
$proto4["m_uniontype"] = "SQLL_UNKNOWN";
	$obj = new SQLNonParsed(array(
	"m_sql" => ""
));

$proto4["m_column"]=$obj;
$proto4["m_contained"] = array();
$proto4["m_strCase"] = "";
$proto4["m_havingmode"] = false;
$proto4["m_inBrackets"] = false;
$proto4["m_useAlias"] = false;
$obj = new SQLLogicalExpr($proto4);

$proto0["m_having"] = $obj;
$proto0["m_fieldlist"] = array();
						$proto6=array();
			$obj = new SQLField(array(
	"m_strName" => "loan_id",
	"m_strTable" => "t",
	"m_srcTableName" => "loandata"
));

$proto6["m_sql"] = "t.loan_id";
$proto6["m_srcTableName"] = "loandata";
$proto6["m_expr"]=$obj;
$proto6["m_alias"] = "";
$obj = new SQLFieldListItem($proto6);

$proto0["m_fieldlist"][]=$obj;
						$proto8=array();
			$proto9=array();
$proto9["m_functiontype"] = "SQLF_CUSTOM";
$proto9["m_arguments"] = array();
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.full_name"
));

$proto9["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "' / '"
));

$proto9["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.nic"
));

$proto9["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "' / '"
));

$proto9["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.phone"
));

$proto9["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "' / '"
));

$proto9["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.address"
));

$proto9["m_arguments"][]=$obj;
$proto9["m_strFunctionName"] = "CONCAT";
$obj = new SQLFunctionCall($proto9);

$proto8["m_sql"] = "CONCAT(          t.full_name, ' / ',          t.nic, ' / ',          t.phone, ' / ',          t.address      )";
$proto8["m_srcTableName"] = "loandata";
$proto8["m_expr"]=$obj;
$proto8["m_alias"] = "name";
$obj = new SQLFieldListItem($proto8);

$proto0["m_fieldlist"][]=$obj;
						$proto17=array();
			$proto18=array();
$proto18["m_functiontype"] = "SQLF_CUSTOM";
$proto18["m_arguments"] = array();
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"ණය අංකය: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.loan_id"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"ණය: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.loan_amount"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"සේවා: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.profit_amount"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"මුළු ගෙවීම: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.total_payable"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"වාරිකය: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.installment_amount"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"කාලය: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.payment_interval_days"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"අරම්භය: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.start_date"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"අවසානය: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.end_date"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"තත්ත්වය: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.status"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"තව දින: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.remaining_days"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"තව වාරික: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.remaining_installments"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"ගෙවූ මුදල: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.total_collected"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"අදට ගෙවිය යුතු: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.should_pay"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\", \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"හිඟ: \""
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "t.arrears_amount"
));

$proto18["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "\"\\n\""
));

$proto18["m_arguments"][]=$obj;
$proto18["m_strFunctionName"] = "CONCAT";
$obj = new SQLFunctionCall($proto18);

$proto17["m_sql"] = "CONCAT(  					\"ණය අංකය: \", t.loan_id,\", \", \"\\n\",          \"ණය: \", t.loan_amount,\", \", \"\\n\",           \"සේවා: \", t.profit_amount,\", \", \"\\n\",          \"මුළු ගෙවීම: \", t.total_payable,\", \", \"\\n\",          \"වාරිකය: \", t.installment_amount,\", \", \"\\n\",          \"කාලය: \", t.payment_interval_days,\", \", \"\\n\",          \"අරම්භය: \", t.start_date,\", \", \"\\n\",          \"අවසානය: \", t.end_date,\", \", \"\\n\",          \"තත්ත්වය: \", t.status,\", \", \"\\n\",           \"තව දින: \", t.remaining_days,\", \", \"\\n\",          \"තව වාරික: \", t.remaining_installments,\", \", \"\\n\",          \"ගෙවූ මුදල: \", t.total_collected,\", \", \"\\n\",          \"අදට ගෙවිය යුතු: \", t.should_pay,\", \", \"\\n\",          \"හිඟ: \", t.arrears_amount, \"\\n\"      )";
$proto17["m_srcTableName"] = "loandata";
$proto17["m_expr"]=$obj;
$proto17["m_alias"] = "details";
$obj = new SQLFieldListItem($proto17);

$proto0["m_fieldlist"][]=$obj;
						$proto74=array();
			$obj = new SQLField(array(
	"m_strName" => "arrears_amount",
	"m_strTable" => "t",
	"m_srcTableName" => "loandata"
));

$proto74["m_sql"] = "t.arrears_amount";
$proto74["m_srcTableName"] = "loandata";
$proto74["m_expr"]=$obj;
$proto74["m_alias"] = "";
$obj = new SQLFieldListItem($proto74);

$proto0["m_fieldlist"][]=$obj;
$proto0["m_fromlist"] = array();
												$proto76=array();
$proto76["m_link"] = "SQLL_MAIN";
			$proto77=array();
$proto77["m_strHead"] = "      SELECT";
$proto77["m_strFieldList"] = "l.loan_id,          l.customer_id,          l.horno,          l.guarantor_1,          l.guarantor_2,            /* Customer fields */          cus.full_name AS full_name,          cus.nic AS nic,          cus.phone AS phone,          cus.address AS address,            l.loan_amount,          l.total_payable,          l.installment_amount,          l.payment_interval_days,          l.start_date,          l.end_date,          l.status,          l.Old_Loan,          l.profit_amount,            IFNULL(SUM(c.amount), 0) AS total_collected,            /* Calculations – capped to total_payable */          LEAST(              (FLOOR(DATEDIFF(CURDATE(), l.start_date) / l.payment_interval_days) + 1)              * l.installment_amount,              l.total_payable          ) AS should_pay,            (l.total_payable - IFNULL(SUM(c.amount), 0)) AS remaining_balance,            GREATEST(              LEAST(                  (FLOOR(DATEDIFF(CURDATE(), l.start_date) / l.payment_interval_days) + 1)                  * l.installment_amount,                  l.total_payable              ) - IFNULL(SUM(c.amount), 0),              0          ) AS arrears_amount,            DATEDIFF(l.end_date, CURDATE()) AS remaining_days,            GREATEST(              ROUND((l.total_payable / l.installment_amount)                  - (IFNULL(SUM(c.amount), 0) / l.installment_amount), 0),              0          ) AS remaining_installments";
$proto77["m_strFrom"] = "FROM loans l      LEFT JOIN customers cus ON l.customer_id = cus.customer_id      LEFT JOIN collections c ON l.loan_id = c.loan_id";
$proto77["m_strWhere"] = "l.status = 'active' OR l.status = 'Recovered'";
$proto77["m_strOrderBy"] = "";
	
		;
			$proto77["cipherer"] = null;
$proto79=array();
$proto79["m_sql"] = "l.status = 'active' OR l.status = 'Recovered'";
$proto79["m_uniontype"] = "SQLL_OR";
	$obj = new SQLNonParsed(array(
	"m_sql" => "l.status = 'active' OR l.status = 'Recovered'"
));

$proto79["m_column"]=$obj;
$proto79["m_contained"] = array();
						$proto81=array();
$proto81["m_sql"] = "l.status = 'active'";
$proto81["m_uniontype"] = "SQLL_UNKNOWN";
						$obj = new SQLField(array(
	"m_strName" => "status",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto81["m_column"]=$obj;
$proto81["m_contained"] = array();
$proto81["m_strCase"] = "= 'active'";
$proto81["m_havingmode"] = false;
$proto81["m_inBrackets"] = false;
$proto81["m_useAlias"] = false;
$obj = new SQLLogicalExpr($proto81);

			$proto79["m_contained"][]=$obj;
						$proto83=array();
$proto83["m_sql"] = "l.status = 'Recovered'";
$proto83["m_uniontype"] = "SQLL_UNKNOWN";
						$obj = new SQLField(array(
	"m_strName" => "status",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto83["m_column"]=$obj;
$proto83["m_contained"] = array();
$proto83["m_strCase"] = "= 'Recovered'";
$proto83["m_havingmode"] = false;
$proto83["m_inBrackets"] = false;
$proto83["m_useAlias"] = false;
$obj = new SQLLogicalExpr($proto83);

			$proto79["m_contained"][]=$obj;
$proto79["m_strCase"] = "";
$proto79["m_havingmode"] = false;
$proto79["m_inBrackets"] = false;
$proto79["m_useAlias"] = false;
$obj = new SQLLogicalExpr($proto79);

$proto77["m_where"] = $obj;
$proto85=array();
$proto85["m_sql"] = "";
$proto85["m_uniontype"] = "SQLL_UNKNOWN";
	$obj = new SQLNonParsed(array(
	"m_sql" => ""
));

$proto85["m_column"]=$obj;
$proto85["m_contained"] = array();
$proto85["m_strCase"] = "";
$proto85["m_havingmode"] = false;
$proto85["m_inBrackets"] = false;
$proto85["m_useAlias"] = false;
$obj = new SQLLogicalExpr($proto85);

$proto77["m_having"] = $obj;
$proto77["m_fieldlist"] = array();
						$proto87=array();
			$obj = new SQLField(array(
	"m_strName" => "loan_id",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto87["m_sql"] = "l.loan_id";
$proto87["m_srcTableName"] = "loandata";
$proto87["m_expr"]=$obj;
$proto87["m_alias"] = "";
$obj = new SQLFieldListItem($proto87);

$proto77["m_fieldlist"][]=$obj;
						$proto89=array();
			$obj = new SQLField(array(
	"m_strName" => "customer_id",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto89["m_sql"] = "l.customer_id";
$proto89["m_srcTableName"] = "loandata";
$proto89["m_expr"]=$obj;
$proto89["m_alias"] = "";
$obj = new SQLFieldListItem($proto89);

$proto77["m_fieldlist"][]=$obj;
						$proto91=array();
			$obj = new SQLField(array(
	"m_strName" => "horno",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto91["m_sql"] = "l.horno";
$proto91["m_srcTableName"] = "loandata";
$proto91["m_expr"]=$obj;
$proto91["m_alias"] = "";
$obj = new SQLFieldListItem($proto91);

$proto77["m_fieldlist"][]=$obj;
						$proto93=array();
			$obj = new SQLField(array(
	"m_strName" => "guarantor_1",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto93["m_sql"] = "l.guarantor_1";
$proto93["m_srcTableName"] = "loandata";
$proto93["m_expr"]=$obj;
$proto93["m_alias"] = "";
$obj = new SQLFieldListItem($proto93);

$proto77["m_fieldlist"][]=$obj;
						$proto95=array();
			$obj = new SQLField(array(
	"m_strName" => "guarantor_2",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto95["m_sql"] = "l.guarantor_2";
$proto95["m_srcTableName"] = "loandata";
$proto95["m_expr"]=$obj;
$proto95["m_alias"] = "";
$obj = new SQLFieldListItem($proto95);

$proto77["m_fieldlist"][]=$obj;
						$proto97=array();
			$obj = new SQLField(array(
	"m_strName" => "full_name",
	"m_strTable" => "cus",
	"m_srcTableName" => "loandata"
));

$proto97["m_sql"] = "cus.full_name";
$proto97["m_srcTableName"] = "loandata";
$proto97["m_expr"]=$obj;
$proto97["m_alias"] = "full_name";
$obj = new SQLFieldListItem($proto97);

$proto77["m_fieldlist"][]=$obj;
						$proto99=array();
			$obj = new SQLField(array(
	"m_strName" => "nic",
	"m_strTable" => "cus",
	"m_srcTableName" => "loandata"
));

$proto99["m_sql"] = "cus.nic";
$proto99["m_srcTableName"] = "loandata";
$proto99["m_expr"]=$obj;
$proto99["m_alias"] = "nic";
$obj = new SQLFieldListItem($proto99);

$proto77["m_fieldlist"][]=$obj;
						$proto101=array();
			$obj = new SQLField(array(
	"m_strName" => "phone",
	"m_strTable" => "cus",
	"m_srcTableName" => "loandata"
));

$proto101["m_sql"] = "cus.phone";
$proto101["m_srcTableName"] = "loandata";
$proto101["m_expr"]=$obj;
$proto101["m_alias"] = "phone";
$obj = new SQLFieldListItem($proto101);

$proto77["m_fieldlist"][]=$obj;
						$proto103=array();
			$obj = new SQLField(array(
	"m_strName" => "address",
	"m_strTable" => "cus",
	"m_srcTableName" => "loandata"
));

$proto103["m_sql"] = "cus.address";
$proto103["m_srcTableName"] = "loandata";
$proto103["m_expr"]=$obj;
$proto103["m_alias"] = "address";
$obj = new SQLFieldListItem($proto103);

$proto77["m_fieldlist"][]=$obj;
						$proto105=array();
			$obj = new SQLField(array(
	"m_strName" => "loan_amount",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto105["m_sql"] = "l.loan_amount";
$proto105["m_srcTableName"] = "loandata";
$proto105["m_expr"]=$obj;
$proto105["m_alias"] = "";
$obj = new SQLFieldListItem($proto105);

$proto77["m_fieldlist"][]=$obj;
						$proto107=array();
			$obj = new SQLField(array(
	"m_strName" => "total_payable",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto107["m_sql"] = "l.total_payable";
$proto107["m_srcTableName"] = "loandata";
$proto107["m_expr"]=$obj;
$proto107["m_alias"] = "";
$obj = new SQLFieldListItem($proto107);

$proto77["m_fieldlist"][]=$obj;
						$proto109=array();
			$obj = new SQLField(array(
	"m_strName" => "installment_amount",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto109["m_sql"] = "l.installment_amount";
$proto109["m_srcTableName"] = "loandata";
$proto109["m_expr"]=$obj;
$proto109["m_alias"] = "";
$obj = new SQLFieldListItem($proto109);

$proto77["m_fieldlist"][]=$obj;
						$proto111=array();
			$obj = new SQLField(array(
	"m_strName" => "payment_interval_days",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto111["m_sql"] = "l.payment_interval_days";
$proto111["m_srcTableName"] = "loandata";
$proto111["m_expr"]=$obj;
$proto111["m_alias"] = "";
$obj = new SQLFieldListItem($proto111);

$proto77["m_fieldlist"][]=$obj;
						$proto113=array();
			$obj = new SQLField(array(
	"m_strName" => "start_date",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto113["m_sql"] = "l.start_date";
$proto113["m_srcTableName"] = "loandata";
$proto113["m_expr"]=$obj;
$proto113["m_alias"] = "";
$obj = new SQLFieldListItem($proto113);

$proto77["m_fieldlist"][]=$obj;
						$proto115=array();
			$obj = new SQLField(array(
	"m_strName" => "end_date",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto115["m_sql"] = "l.end_date";
$proto115["m_srcTableName"] = "loandata";
$proto115["m_expr"]=$obj;
$proto115["m_alias"] = "";
$obj = new SQLFieldListItem($proto115);

$proto77["m_fieldlist"][]=$obj;
						$proto117=array();
			$obj = new SQLField(array(
	"m_strName" => "status",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto117["m_sql"] = "l.status";
$proto117["m_srcTableName"] = "loandata";
$proto117["m_expr"]=$obj;
$proto117["m_alias"] = "";
$obj = new SQLFieldListItem($proto117);

$proto77["m_fieldlist"][]=$obj;
						$proto119=array();
			$obj = new SQLField(array(
	"m_strName" => "Old_Loan",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto119["m_sql"] = "l.Old_Loan";
$proto119["m_srcTableName"] = "loandata";
$proto119["m_expr"]=$obj;
$proto119["m_alias"] = "";
$obj = new SQLFieldListItem($proto119);

$proto77["m_fieldlist"][]=$obj;
						$proto121=array();
			$obj = new SQLField(array(
	"m_strName" => "profit_amount",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto121["m_sql"] = "l.profit_amount";
$proto121["m_srcTableName"] = "loandata";
$proto121["m_expr"]=$obj;
$proto121["m_alias"] = "";
$obj = new SQLFieldListItem($proto121);

$proto77["m_fieldlist"][]=$obj;
						$proto123=array();
			$proto124=array();
$proto124["m_functiontype"] = "SQLF_CUSTOM";
$proto124["m_arguments"] = array();
						$obj = new SQLNonParsed(array(
	"m_sql" => "SUM(c.amount)"
));

$proto124["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "0"
));

$proto124["m_arguments"][]=$obj;
$proto124["m_strFunctionName"] = "IFNULL";
$obj = new SQLFunctionCall($proto124);

$proto123["m_sql"] = "IFNULL(SUM(c.amount), 0)";
$proto123["m_srcTableName"] = "loandata";
$proto123["m_expr"]=$obj;
$proto123["m_alias"] = "total_collected";
$obj = new SQLFieldListItem($proto123);

$proto77["m_fieldlist"][]=$obj;
						$proto127=array();
			$proto128=array();
$proto128["m_functiontype"] = "SQLF_CUSTOM";
$proto128["m_arguments"] = array();
						$obj = new SQLNonParsed(array(
	"m_sql" => "(FLOOR(DATEDIFF(CURDATE(), l.start_date) / l.payment_interval_days) + 1)              * l.installment_amount"
));

$proto128["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "l.total_payable"
));

$proto128["m_arguments"][]=$obj;
$proto128["m_strFunctionName"] = "LEAST";
$obj = new SQLFunctionCall($proto128);

$proto127["m_sql"] = "LEAST(              (FLOOR(DATEDIFF(CURDATE(), l.start_date) / l.payment_interval_days) + 1)              * l.installment_amount,              l.total_payable          )";
$proto127["m_srcTableName"] = "loandata";
$proto127["m_expr"]=$obj;
$proto127["m_alias"] = "should_pay";
$obj = new SQLFieldListItem($proto127);

$proto77["m_fieldlist"][]=$obj;
						$proto131=array();
			$obj = new SQLNonParsed(array(
	"m_sql" => "(l.total_payable - IFNULL(SUM(c.amount), 0))"
));

$proto131["m_sql"] = "(l.total_payable - IFNULL(SUM(c.amount), 0))";
$proto131["m_srcTableName"] = "loandata";
$proto131["m_expr"]=$obj;
$proto131["m_alias"] = "remaining_balance";
$obj = new SQLFieldListItem($proto131);

$proto77["m_fieldlist"][]=$obj;
						$proto133=array();
			$proto134=array();
$proto134["m_functiontype"] = "SQLF_CUSTOM";
$proto134["m_arguments"] = array();
						$obj = new SQLNonParsed(array(
	"m_sql" => "LEAST(                  (FLOOR(DATEDIFF(CURDATE(), l.start_date) / l.payment_interval_days) + 1)                  * l.installment_amount,                  l.total_payable              ) - IFNULL(SUM(c.amount), 0)"
));

$proto134["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "0"
));

$proto134["m_arguments"][]=$obj;
$proto134["m_strFunctionName"] = "GREATEST";
$obj = new SQLFunctionCall($proto134);

$proto133["m_sql"] = "GREATEST(              LEAST(                  (FLOOR(DATEDIFF(CURDATE(), l.start_date) / l.payment_interval_days) + 1)                  * l.installment_amount,                  l.total_payable              ) - IFNULL(SUM(c.amount), 0),              0          )";
$proto133["m_srcTableName"] = "loandata";
$proto133["m_expr"]=$obj;
$proto133["m_alias"] = "arrears_amount";
$obj = new SQLFieldListItem($proto133);

$proto77["m_fieldlist"][]=$obj;
						$proto137=array();
			$proto138=array();
$proto138["m_functiontype"] = "SQLF_CUSTOM";
$proto138["m_arguments"] = array();
						$obj = new SQLNonParsed(array(
	"m_sql" => "l.end_date"
));

$proto138["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "CURDATE()"
));

$proto138["m_arguments"][]=$obj;
$proto138["m_strFunctionName"] = "DATEDIFF";
$obj = new SQLFunctionCall($proto138);

$proto137["m_sql"] = "DATEDIFF(l.end_date, CURDATE())";
$proto137["m_srcTableName"] = "loandata";
$proto137["m_expr"]=$obj;
$proto137["m_alias"] = "remaining_days";
$obj = new SQLFieldListItem($proto137);

$proto77["m_fieldlist"][]=$obj;
						$proto141=array();
			$proto142=array();
$proto142["m_functiontype"] = "SQLF_CUSTOM";
$proto142["m_arguments"] = array();
						$obj = new SQLNonParsed(array(
	"m_sql" => "ROUND((l.total_payable / l.installment_amount)                  - (IFNULL(SUM(c.amount), 0) / l.installment_amount), 0)"
));

$proto142["m_arguments"][]=$obj;
						$obj = new SQLNonParsed(array(
	"m_sql" => "0"
));

$proto142["m_arguments"][]=$obj;
$proto142["m_strFunctionName"] = "GREATEST";
$obj = new SQLFunctionCall($proto142);

$proto141["m_sql"] = "GREATEST(              ROUND((l.total_payable / l.installment_amount)                  - (IFNULL(SUM(c.amount), 0) / l.installment_amount), 0),              0          )";
$proto141["m_srcTableName"] = "loandata";
$proto141["m_expr"]=$obj;
$proto141["m_alias"] = "remaining_installments";
$obj = new SQLFieldListItem($proto141);

$proto77["m_fieldlist"][]=$obj;
$proto77["m_fromlist"] = array();
												$proto145=array();
$proto145["m_link"] = "SQLL_MAIN";
			$proto146=array();
$proto146["m_strName"] = "loans";
$proto146["m_srcTableName"] = "loandata";
$proto146["m_columns"] = array();
$proto146["m_columns"][] = "loan_id";
$proto146["m_columns"][] = "customer_id";
$proto146["m_columns"][] = "loan_amount";
$proto146["m_columns"][] = "total_payable";
$proto146["m_columns"][] = "profit_amount";
$proto146["m_columns"][] = "payment_interval_days";
$proto146["m_columns"][] = "installment_amount";
$proto146["m_columns"][] = "start_date";
$proto146["m_columns"][] = "end_date";
$proto146["m_columns"][] = "status";
$proto146["m_columns"][] = "created_at";
$proto146["m_columns"][] = "call_record";
$proto146["m_columns"][] = "loan_period";
$proto146["m_columns"][] = "Loan";
$proto146["m_columns"][] = "video";
$proto146["m_columns"][] = "files";
$proto146["m_columns"][] = "image";
$proto146["m_columns"][] = "horno";
$proto146["m_columns"][] = "user";
$proto146["m_columns"][] = "guarantor_1";
$proto146["m_columns"][] = "guarantor_2";
$proto146["m_columns"][] = "Old_Loan";
$proto146["m_columns"][] = "new_loan";
$proto146["m_columns"][] = "route_id";
$proto146["m_columns"][] = "route_order";
$obj = new SQLTable($proto146);

$proto145["m_table"] = $obj;
$proto145["m_sql"] = "loans l";
$proto145["m_alias"] = "l";
$proto145["m_srcTableName"] = "loandata";
$proto147=array();
$proto147["m_sql"] = "";
$proto147["m_uniontype"] = "SQLL_UNKNOWN";
	$obj = new SQLNonParsed(array(
	"m_sql" => ""
));

$proto147["m_column"]=$obj;
$proto147["m_contained"] = array();
$proto147["m_strCase"] = "";
$proto147["m_havingmode"] = false;
$proto147["m_inBrackets"] = false;
$proto147["m_useAlias"] = false;
$obj = new SQLLogicalExpr($proto147);

$proto145["m_joinon"] = $obj;
$obj = new SQLFromListItem($proto145);

$proto77["m_fromlist"][]=$obj;
												$proto149=array();
$proto149["m_link"] = "SQLL_LEFTJOIN";
			$proto150=array();
$proto150["m_strName"] = "customers";
$proto150["m_srcTableName"] = "loandata";
$proto150["m_columns"] = array();
$proto150["m_columns"][] = "customer_id";
$proto150["m_columns"][] = "full_name";
$proto150["m_columns"][] = "nic";
$proto150["m_columns"][] = "phone";
$proto150["m_columns"][] = "address";
$proto150["m_columns"][] = "photo";
$proto150["m_columns"][] = "created_at";
$proto150["m_columns"][] = "user";
$obj = new SQLTable($proto150);

$proto149["m_table"] = $obj;
$proto149["m_sql"] = "LEFT JOIN customers cus ON l.customer_id = cus.customer_id";
$proto149["m_alias"] = "cus";
$proto149["m_srcTableName"] = "loandata";
$proto151=array();
$proto151["m_sql"] = "l.customer_id = cus.customer_id";
$proto151["m_uniontype"] = "SQLL_UNKNOWN";
						$obj = new SQLField(array(
	"m_strName" => "customer_id",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto151["m_column"]=$obj;
$proto151["m_contained"] = array();
$proto151["m_strCase"] = "= cus.customer_id";
$proto151["m_havingmode"] = false;
$proto151["m_inBrackets"] = false;
$proto151["m_useAlias"] = false;
$obj = new SQLLogicalExpr($proto151);

$proto149["m_joinon"] = $obj;
$obj = new SQLFromListItem($proto149);

$proto77["m_fromlist"][]=$obj;
												$proto153=array();
$proto153["m_link"] = "SQLL_LEFTJOIN";
			$proto154=array();
$proto154["m_strName"] = "collections";
$proto154["m_srcTableName"] = "loandata";
$proto154["m_columns"] = array();
$proto154["m_columns"][] = "collection_id";
$proto154["m_columns"][] = "loan_id";
$proto154["m_columns"][] = "pay_date";
$proto154["m_columns"][] = "amount";
$proto154["m_columns"][] = "customer_id";
$proto154["m_columns"][] = "details";
$proto154["m_columns"][] = "horno";
$proto154["m_columns"][] = "user";
$proto154["m_columns"][] = "creat_at";
$proto154["m_columns"][] = "remaining_balance";
$obj = new SQLTable($proto154);

$proto153["m_table"] = $obj;
$proto153["m_sql"] = "LEFT JOIN collections c ON l.loan_id = c.loan_id";
$proto153["m_alias"] = "c";
$proto153["m_srcTableName"] = "loandata";
$proto155=array();
$proto155["m_sql"] = "l.loan_id = c.loan_id";
$proto155["m_uniontype"] = "SQLL_UNKNOWN";
						$obj = new SQLField(array(
	"m_strName" => "loan_id",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto155["m_column"]=$obj;
$proto155["m_contained"] = array();
$proto155["m_strCase"] = "= c.loan_id";
$proto155["m_havingmode"] = false;
$proto155["m_inBrackets"] = false;
$proto155["m_useAlias"] = false;
$obj = new SQLLogicalExpr($proto155);

$proto153["m_joinon"] = $obj;
$obj = new SQLFromListItem($proto153);

$proto77["m_fromlist"][]=$obj;
$proto77["m_groupby"] = array();
												$proto157=array();
						$obj = new SQLField(array(
	"m_strName" => "loan_id",
	"m_strTable" => "l",
	"m_srcTableName" => "loandata"
));

$proto157["m_column"]=$obj;
$obj = new SQLGroupByItem($proto157);

$proto77["m_groupby"][]=$obj;
$proto77["m_orderby"] = array();
$proto77["m_srcTableName"]="loandata";		
$obj = new SQLQuery($proto77);

$proto76["m_table"] = $obj;
$proto76["m_sql"] = "(      SELECT          l.loan_id,          l.customer_id,          l.horno,          l.guarantor_1,          l.guarantor_2,            /* Customer fields */          cus.full_name AS full_name,          cus.nic AS nic,          cus.phone AS phone,          cus.address AS address,            l.loan_amount,          l.total_payable,          l.installment_amount,          l.payment_interval_days,          l.start_date,          l.end_date,          l.status,          l.Old_Loan,          l.profit_amount,            IFNULL(SUM(c.amount), 0) AS total_collected,            /* Calculations – capped to total_payable */          LEAST(              (FLOOR(DATEDIFF(CURDATE(), l.start_date) / l.payment_interval_days) + 1)              * l.installment_amount,              l.total_payable          ) AS should_pay,            (l.total_payable - IFNULL(SUM(c.amount), 0)) AS remaining_balance,            GREATEST(              LEAST(                  (FLOOR(DATEDIFF(CURDATE(), l.start_date) / l.payment_interval_days) + 1)                  * l.installment_amount,                  l.total_payable              ) - IFNULL(SUM(c.amount), 0),              0          ) AS arrears_amount,            DATEDIFF(l.end_date, CURDATE()) AS remaining_days,            GREATEST(              ROUND((l.total_payable / l.installment_amount)                  - (IFNULL(SUM(c.amount), 0) / l.installment_amount), 0),              0          ) AS remaining_installments        FROM loans l      LEFT JOIN customers cus ON l.customer_id = cus.customer_id      LEFT JOIN collections c ON l.loan_id = c.loan_id      WHERE l.status = 'active' OR l.status = 'Recovered'      GROUP BY l.loan_id  ) AS t";
$proto76["m_alias"] = "t";
$proto76["m_srcTableName"] = "loandata";
$proto159=array();
$proto159["m_sql"] = "";
$proto159["m_uniontype"] = "SQLL_UNKNOWN";
	$obj = new SQLNonParsed(array(
	"m_sql" => ""
));

$proto159["m_column"]=$obj;
$proto159["m_contained"] = array();
$proto159["m_strCase"] = "";
$proto159["m_havingmode"] = false;
$proto159["m_inBrackets"] = false;
$proto159["m_useAlias"] = false;
$obj = new SQLLogicalExpr($proto159);

$proto76["m_joinon"] = $obj;
$obj = new SQLFromListItem($proto76);

$proto0["m_fromlist"][]=$obj;
$proto0["m_groupby"] = array();
$proto0["m_orderby"] = array();
												$proto161=array();
						$obj = new SQLField(array(
	"m_strName" => "loan_id",
	"m_strTable" => "t",
	"m_srcTableName" => "loandata"
));

$proto161["m_column"]=$obj;
$proto161["m_bAsc"] = 0;
$proto161["m_nColumn"] = 0;
$obj = new SQLOrderByItem($proto161);

$proto0["m_orderby"][]=$obj;					
$proto0["m_srcTableName"]="loandata";		
$obj = new SQLQuery($proto0);

	return $obj;
}
$queryData_loandata = createSqlQuery_loandata();


	
		;

				

$tdataloandata[".sqlquery"] = $queryData_loandata;



$tableEvents["loandata"] = new eventsBase;
$tdataloandata[".hasEvents"] = false;

?>