// JavaScript Document

/* Created By Mahmoud Badawi */
var table_Free = 'Free_check';
var _base_name = 'Commercial';
var table2 = 'sentence_transaction';
var count = 0 ;
// Clear Form 
function clear_form_internal(){
    //clearForm($('#Free_check_form'));
	$('#TXT_SUBSCRIBER_Free').val('');
	$('#TXT_ID_Free').val('');
	$('#TXT_NAME_Free_FIRST').val('');
	$('#TXT_NAME_Free_TWO').val('');
	$('#TXT_NAME_Free_THREE').val('');
	$('#TXT_NAME_Free_FOUR').val('');
}
/// Search Function
function searchsCheck(){
	GeneralShowloading(); 
	var SUBSCRIBER = $('#Free_check_form').find('input[name="TXT_SUBSCRIBER_Free"]').val();
    var ID = $('#Free_check_form').find('input[name="TXT_ID_Free"]').val();
    var NAME = $('#TXT_NAME_Free').val();
    var NAME_BEN = $('#TXT_NAME_BEN_Free').val();
    var TYPE_CHECK = $('#DL_TYPE_CHECK').val();
    var REQUEST = 0;//$('#TXT_REQUEST_HIDDEN').val();
    //
    var FIRST = $('#Free_check_form').find('input[name="TXT_NAME_Free_FIRST"]').val();
    var TWO = $('#Free_check_form').find('input[name="TXT_NAME_Free_TWO"]').val();
    var THREE = $('#Free_check_form').find('input[name="TXT_NAME_Free_THREE"]').val();
    var FOUR = $('#Free_check_form').find('input[name="TXT_NAME_Free_FOUR"]').val();
    //
    var PKG_INTERNAL = 'EBLLC_PKG';
    var PROCEDURE_INTERNAL = 'SUBSCRIBERS_FIND_TB_LIST2';
	if(SUBSCRIBER == null && ID == null && FIRST == null && TWO == null && THREE == null && FOUR == null && NAME_BEN == null){
	   GeneralHideloading();
       danger_msg('تحذير..','لم يتم تحديد أي محدد للبحث');
   }else if(FIRST != null || TWO != null || THREE != null || FOUR != null || SUBSCRIBER != null || ID != null ){
        if(SUBSCRIBER != ''){
			/// Check If the Subscriber exist in request app
			$.ajax({
                url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_twoDetService2/SUB_CHECK_TB_GET2/'+REQUEST+'/'+SUBSCRIBER,
                type: 'POST',
                datatype: 'json',
                success: function (datas) { 
                      if(datas.length ==0){
						   var newArr_INTERNAL = {PKG_INTERNAL:PKG_INTERNAL,PROCEDURE_INTERNAL:PROCEDURE_INTERNAL,FIRST:FIRST,TWO:TWO,THREE:THREE,FOUR:FOUR,SUBSCRIBER:SUBSCRIBER,ID:ID}
						   // Remove Tr
						   $("."+table_Free+"_ListIns_INTERNAL table tbody tr").remove();
						   get_data('/'+_base_name+'/freecheck/Free_check/public_get_detaild_issues/',newArr_INTERNAL ,function(data){
								 // Fetch Data in the Table
								 $(data).each(function(index, element) {
										if(element.FLAG == 2){
											$("."+table_Free+"_ListIns_INTERNAL tbody").append(GetTableRowsWithCheckBoxHidden([],[element.SER+"/"+element.FLAG,element.SER,element.ID,
											element.FULL_NAME,element.B_NAME,element.BRANCH,element.ISSUE_TYPE]));
										}else if(element.FLAG == 1){
											$("."+table_Free+"_ListIns_INTERNAL tbody").append(GetTableRowsWithCheckBoxHidden([],[element.SUB_NO,element.SUB_NO,element.ID,
											element.FULL_NAME,element.B_NAME,element.BRANCH,element.ISSUE_TYPE]));
										}else if(element.FLAG == 3){
											$("."+table_Free+"_ListIns_INTERNAL tbody").append(GetTableRowsWithCheckBoxHidden([],[element.SER+"/"+element.FLAG,element.SER,element.ID,
											element.FULL_NAME,element.B_NAME,element.BRANCH,element.ISSUE_TYPE]));
										}
								  })
								  GeneralHideloading();
								 /// Show Modal
								 $("."+table_Free+"Modal52_Free").modal('show');
						   },'json');
					   }else{
							    danger_msg('تحذير..','رقم الإشتراك المراد البحث عنه , تم البحث عنه مسبقا لهذه المعاملة');
                       }
                 },
                error: function (jqXHR, textStatus, errorThrown) {  }
            });
		}else{
		   var newArr_INTERNAL = {PKG_INTERNAL:PKG_INTERNAL,PROCEDURE_INTERNAL:PROCEDURE_INTERNAL,FIRST:FIRST,TWO:TWO,THREE:THREE,FOUR:FOUR,SUBSCRIBER:SUBSCRIBER,ID:ID}
		   // Remove Tr
		   $("."+table_Free+"_ListIns_INTERNAL table tbody tr").remove();
		   get_data('/'+_base_name+'/freecheck/Free_check/public_get_detaild_issues/',newArr_INTERNAL ,function(data){
				 // Fetch Data in the Table
				 $(data).each(function(index, element) {
						if(element.FLAG == 2){
							$("."+table_Free+"_ListIns_INTERNAL tbody").append(GetTableRowsWithCheckBoxHidden([],[element.SER+"/"+element.FLAG,element.SER,element.ID,
							element.FULL_NAME,element.B_NAME,element.BRANCH,element.ISSUE_TYPE]));
						}else if(element.FLAG == 1){
							$("."+table_Free+"_ListIns_INTERNAL tbody").append(GetTableRowsWithCheckBoxHidden([],[element.SUB_NO,element.SUB_NO,element.ID,
							element.FULL_NAME,element.B_NAME,element.BRANCH,element.ISSUE_TYPE]));
						}else if(element.FLAG == 3){
							$("."+table_Free+"_ListIns_INTERNAL tbody").append(GetTableRowsWithCheckBoxHidden([],[element.SER+"/"+element.FLAG,element.SER,element.ID,
							element.FULL_NAME,element.B_NAME,element.BRANCH,element.ISSUE_TYPE]));
						}
				  })
				  GeneralHideloading();
				 /// Show Modal
				 $("."+table_Free+"Modal52_Free").modal('show');
		   },'json');
		}
   }
}
///
$("#select_checked").click(function(){
	GeneralShowloading();
    var REQUEST = $('#Free_check_form').find("input[name='TXT_REQUEST_HIDDEN']").val();
    $("."+table_Free+"_Lists3 tbody tr").remove();
    $("."+table_Free+"_Lists tbody tr").remove();
    $("."+table_Free+"_Lists2 tbody tr").remove();
    $("."+table_Free+"_Lists10x tbody tr").remove();
    $("."+table_Free+"_ListsLaw tbody tr").remove();
    //showLoading();
    setTimeout(function() {
    		//HideLoading();
		    var values = new Array();
			$.each($("input[name='SubCheck[]']:checked"), function() {
			   values.push($(this).val());
			   var request = $(this).val();
       			  <!----------------------->
                  var PKG_SUBSC1 = 'DB_LINK_PKG';
				  var PROCEDURE_SUBSC1 = 'SUBSCRIBERSS_TB_LIST';
				  var toals = 0 ;
				  var newArr_SUBSC1 = {PKG:PKG_SUBSC1,PROCEDURE:PROCEDURE_SUBSC1,SUBSCRIBER:request}
				   get_data('/'+_base_name+'/freecheck/Free_check/public_get_detailff',newArr_SUBSC1 ,function(data){
						 $(data).each(function(index, value) {
							   $("."+table_Free+"_Lists10x tbody").append(GetTableRow([],[value.BRANCH_ID_NAME,value.NO,value.NAME,
									   value.BENIFICARY_NAME,value.CUT_NUMBERS,value.RETURN_NUMBERS]));
								  })
	
				   },'json');
				  <!----------------------------->
				  var PKG_SUBSC = 'EBLLC_PKG';
				  var PROCEDURE_SUBSC = 'BILLS_TB_LIST_P';
				  var toals = 0 ;
				  var counter=0;
				  var newArr_SUBSC = {PKG:PKG_SUBSC,PROCEDURE:PROCEDURE_SUBSC,SUBSCRIBER:request}
				   get_data('/'+_base_name+'/freecheck/Free_check/public_get_detailf',newArr_SUBSC ,function(data){
						 $(data).each(function(index, element) {
	
							   if(element.IS_ACTIVE==1 && element.CONSUMES_VALUE==0) counter=counter+1;
							   var ddssss = element.NET_TO_PAY - element.REMAINDER ;
							   toals = parseInt(toals) + parseInt(element.KW_CONSUMES);
							   $("."+table_Free+"_Lists3 tbody").append(GetTableRow([],
							   [element.SUBSCRIBER,element.NAME,element.FOR_MONTH,ddssss,element.KW_CONSUMES,element.IS_ACTIVE_NAME]));
						 })
	
						  if (toals>5)  $('.Qraa').css('display','block');
						  else  $('.Qraa').css('display','none');
	
				   },'json');
				   <!----------// Insert Into DataBase //----------------->
				   $.post('/'+_base_name+'/requests/Requests/subCheckSelect/'+request+'/'+REQUEST,$("#Form_Table_"+table).serializeArray(),'','json');
			});
            //Call Function Subsc
			getAllsSelected(values,REQUEST);

            //Call Function Subsc
            getAllMotSelect(values);
			
            /// Hide Modal
            $("."+table_Free+"Modal52_Free").modal('hide');
    }, 500);
});
//
function getAllsSelected(values,REQUEST){
	var PKG = 'PROCESS_PKG';
    var PROCEDURE = 'V_SENTENCE_TRANSACTION_PROCESS';

    var myJsonString = JSON.stringify(values);
    var newArr = {PKG:PKG,PROCEDURE:PROCEDURE,SUBSCRIBER:myJsonString}
    get_data('/'+_base_name+'/freecheck/Free_check/public_get_detaild_new',newArr ,function(data){
         $(data.result_process).each(function(index, element) {
               var totl = parseInt(element.DEMURAGE_VALUE) + parseInt(element.CONSUMES_VALUE);
               $("."+table_Free+"_Lists2 tbody").append(GetTableRowViewProcess([],[element.SER,element.P_SER_NO_OLD,element.SUBSCRIBER,
               element.NAME,element.B_NAME,'بيانات الإدخال',element.DEMURAGE_VALUE !== null ? element.DEMURAGE_VALUE : '',element.CONSUMES_VALUE !== null ? element.CONSUMES_VALUE : '',element.TO_DEMURAGE_TOTAL !== null ? element.TO_DEMURAGE_TOTAL : '',element.TO_TO_CONSUMES_TOTAL !== null ? element.TO_TO_CONSUMES_TOTAL : '', isNaN(totl) ? '' : totl,element.V_PROCESS_CASE_NAME,element.SENTENCE_DATE, '' ,element.V_PROCESS_CASE]));
          })
         $(data.result_law).each(function(index, element) {
             $("."+table_Free+"_ListsLaw tbody").append(GetTableRowAdoptTaft([],[element.SER,element.BRANCH_NAME,element.SUB_NO,
                 element.SUB_NAME,element.ID,element.FOR_MONTH,element.NET_PAY,element.LAW_TYPE_NAME,
                 element.D_NAME,element.STATUS_NAME]));
				 
				 <!----------// Insert Into DataBase //----------------->
				 $.post('/'+_base_name+'/requests/Requests/subCheckSelectOther/'+element.SER+'/'+REQUEST,$("#Form_Table_"+table).serializeArray(),'','json');
         })
    },'json');
}
//
function getAllMotSelect(values){
	var PKG_TAHSEL = 'DB_LINK_PKG';
    var PROCEDURE_TAHSEL = 'MONTH_BILLS_TB_LIST';
    var myJsonString = JSON.stringify(values);
    
    var newArr_TAHSEL = {PKG:PKG_TAHSEL,PROCEDURE:PROCEDURE_TAHSEL,SUBSCRIBER:myJsonString}
    get_data('/'+_base_name+'/freecheck/Free_check/public_get_detailRs',newArr_TAHSEL ,function(data){
         $(data).each(function(index, element) {
               $("."+table_Free+"_Lists tbody").append(GetTableRowLast3([],[element.BRANCH_NAME,element.SUBSCRIBER,element.NAME,
               element.BENIFICARY_NAME,element.TYPE_NAME,element.USE_TYPE_NAME,element.FOR_MONTH,element.REMAINDER,element.NET_TO_PAY,
			   element.PAYMENT_WAY_NO_NAME]));
          })
		  GeneralHideloading();
    },'json');
}
///
function InspectionShow(ser){
	GeneralShowloading();
	$(".ListIns_INTERNAL322 tbody tr").remove();
	$.ajax({
        url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/SERVICES_PKG/REQUEST_INSPECTION_TB_GET_SER/'+ser,
        type: 'POST',
        datatype: 'json',
        success: function (data) { 
            if(data.length ==0){  
                GeneralHideloading();
            }else{ 
                $(data).each(function(index, element) {
                	$(".ListIns_INTERNAL322 tbody").append(GetTableRowAdoptTaft([],
                    [element.REQUEST_APP_SERIAL,element.P_SER_NO,element.SUBSCRIBER,element.NAME,element.INSPECTION_AGREE_NAME,
					element.INSPECTION_AGREE_NAME,element.NOTE]));
                });
                GeneralHideloading();
                /// Show Modal
                $(".Modal322_Free").modal('show');
            }

         },
        error: function (jqXHR, textStatus, errorThrown) {  }
    }); 
}
///
function DeletePaidSearch(NoFind){
		if (isDoubleClicked($(this))) return;
        swal.fire({
            title: 'هل أنت متأكد من حذف السجل',
            text: "عزيزي المستخدم, في حال قمت بالضغط على زر التأكيد ,سيقوم النظام بحذف السجل",
            type: 'warning',
            showCancelButton: true,
            confirmButtonText: 'تأكيد'
        }).then(function(result) {
            if(result.value){
                $.post('/'+_base_name+'/freecheck/Free_check/DeleteFunc/'+NoFind,$('#'+table+'_form').serialize(), function(r){
					$(r).each(function(index, element) { 
						if(element['o_success'] == 0){
							 danger_msg('تحذير..',element['o_msgtxt']);
						}
						if(element['o_success'] == 1){
							 ShowSuccessMessage();
							 get_to_link(window.location.href);
						}
					});
				},'json');
				return false;
            }
        });
}
///
function DeleteSearchRising(NoFind){
	    if (isDoubleClicked($(this))) return;
        swal.fire({
            title: 'هل أنت متأكد من حذف السجل',
            text: "عزيزي المستخدم, في حال قمت بالضغط على زر التأكيد ,سيقوم النظام بحذف السجل",
            type: 'warning',
            showCancelButton: true,
            confirmButtonText: 'تأكيد'
        }).then(function(result) {
            if(result.value){
                $.post('/'+_base_name+'/freecheck/Free_check/DeleteFunc/'+NoFind,$('#'+table+'_form').serialize(), function(r){
					$(r).each(function(index, element) { 
						if(element['o_success'] == 0){
							 danger_msg('تحذير..',element['o_msgtxt']);
						}
						if(element['o_success'] == 1){
							 ShowSuccessMessage();
							 get_to_link(window.location.href);
						}
					});
				},'json');
				return false;
            }
        });
}
///
function LoadTaftish(){
   GeneralShowloading();
   var SUBSCRIBER = $('#TXT_SUBSCRIBER_Free').val();
   var ID = $('#TXT_ID_Free').val();
   var NAME = $('#TXT_NAME_Free').val();
   var NAME_BEN = $('#TXT_NAME_BEN_Free').val();
   var TYPE_CHECK = $('#DL_TYPE_CHECK').val();
   var REQUEST = $('#TXT_REQUEST_HIDDEN').val();
   var PKG = 'DB_LINK_PKG';
   var PROCEDURE = 'MONTH_BILLS_TB_LIST';
   if(SUBSCRIBER == '' && ID == '' && NAME == '' && NAME_BEN == ''){
	   danger_msg('تحذير..','لم يتم تحديد أي محدد للبحث');
   }else{
		if(SUBSCRIBER != ''){
			  /// Check If the Subscriber exist in request app
			  $.ajax({
                url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_twoDetService/REQUEST_RAISING_TB_GET_SUB/'+SUBSCRIBER+'/'+REQUEST,
                type: 'POST',
                datatype: 'json',
                success: function (datas) { 
                          if(datas.length ==0){
                               var newArr = {PKG:PKG,PROCEDURE:PROCEDURE,SUBSCRIBER:SUBSCRIBER,ID:ID,NAME:NAME,NAME_BEN:NAME_BEN}
							   get_data('/'+_base_name+'/freecheck/Free_check/public_get_detailRs',newArr ,function(data){
									 $("."+table_Free+"_Lists tbody tr").remove();
									 $(data).each(function(index, element) {
										   $("."+table_Free+"_Lists tbody").append(GetTableRowLast([],[element.BRANCH_NAME,element.SUBSCRIBER,element.NAME,
										   element.BENIFICARY_NAME,element.TYPE_NAME,element.FOR_MONTH,element.REMAINDER,element.PRE_PAID_REMAINDER,element.NET_TO_PAY]));
									  })
							   },'json');
                           }else{
							      // يوجد داتا
								  var NoFound = datas[0];
								  $("."+table_Free+"_Lists tbody tr").remove();
								  
								  if(NoFound.ADOPT == 1){
										  $(datas).each(function(key, value) {
											   $("."+table_Free+"_Lists tbody").append(GetTableRowAdoptEmp([],[value.BRANCH_ID_NAME,value.FOLLOW_SUBSCRIBER,value.NAME,
											   value.NAME,value.TYPE_NAME,value.FOR_MONTH,value.REMAINDER,value.REMAINDER_PREPAID,value.NET_TO_PAY]));
										  })
								  }else if(NoFound.ADOPT == 2){
										  $(datas).each(function(keys, values) {
											   $("."+table_Free+"_Lists tbody").append(GetTableRowAdoptManager([],[values.BRANCH_ID_NAME,values.FOLLOW_SUBSCRIBER,values.NAME,
											   values.NAME,values.TYPE_NAME,values.FOR_MONTH,values.REMAINDER,values.REMAINDER_PREPAID,values.NET_TO_PAY]));
										  })
								  }else if(NoFound.ADOPT == 3){
										  $(datas).each(function(keye, valuee) {
											   $("."+table_Free+"_Lists tbody").append(GetTableRowAdoptManReturn([],[valuee.BRANCH_ID_NAME,valuee.FOLLOW_SUBSCRIBER,valuee.NAME,
											   valuee.NAME,valuee.TYPE_NAME,valuee.FOR_MONTH,valuee.REMAINDER,valuee.REMAINDER_PREPAID,valuee.NET_TO_PAY]));
										  })
								  }
                           }
                 },
                error: function (jqXHR, textStatus, errorThrown) {  }
            });
		}else{
		   var newArr = {PKG:PKG,PROCEDURE:PROCEDURE,SUBSCRIBER:SUBSCRIBER,ID:ID,NAME:NAME,NAME_BEN:NAME_BEN}
		   get_data('/'+_base_name+'/freecheck/Free_check/public_get_detailRs',newArr ,function(data){
				 $("."+table_Free+"_Lists tbody tr").remove();
				 $(data).each(function(index, element) {
					   $("."+table_Free+"_Lists tbody").append(GetTableRowLast([],[element.BRANCH_NAME,element.SUBSCRIBER,element.NAME,
					   element.BENIFICARY_NAME,element.TYPE_NAME,element.FOR_MONTH,element.REMAINDER,element.PRE_PAID_REMAINDER,element.NET_TO_PAY]));
				  })
		   },'json');
	   }
   }
   GeneralHideloading();
}
//
$("#DL_BANKS_Free").change(function(){
	 GeneralShowloading();
     var BANKS = $(this).val();
	 GeneralShowloading();
	 setTimeout(function() {
            $.ajax({
                url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_twoDet/CONSTANTS_DETAIL_BRANCH_GET/100/'+BANKS,
                type: 'POST',
                datatype: 'json',
                success: function (data) { 
                          if(data.length ==0){
                              $('#DL_BANK_BRANCH_Free option').remove();
							  $('#DL_BANK_BRANCH_Free').append($('<option/>').attr("value", '').text(''));
							  $('#DL_BANK_BRANCH_Free').prop("selected",true);
							  $("#DL_BANK_BRANCH_Free").change();
                           }else{
							    $('#DL_BANK_BRANCH_Free option').remove();
								$('#DL_BANK_BRANCH_Free').append($('<option/>').attr("value", '').text('---اختر---'));
								$.each(data,function (i,option) {
									$('#DL_BANK_BRANCH_Free').append($('<option/>').attr("value", option.VID).text(option.VNAME));
									if(Bank_Br_Free == option.VID){
										$('#DL_BANK_BRANCH_Free').val(option.VID).prop('selected',true);
										$('#DL_BANK_BRANCH_Free').change();
									}
								})
                                //$('#'+param_four).change();
								GeneralHideloading();
                           }
                 },
                error: function (jqXHR, textStatus, errorThrown) {  }
            });
     }, 500);
});
//
function LoadSubsc(){
	GeneralShowloading();
    var SUBSCRIBER = $('#TXT_SUBSCRIBER_Free').val();
    var ID = $('#TXT_ID_Free').val();
    var NAME = $('#TXT_NAME_Free').val();
    var NAME_BEN = $('#TXT_NAME_BEN_Free').val();
    var TYPE_CHECK = $('#DL_TYPE_CHECK').val();
    
	  var PKG = 'EBLLC_PKG';
      var PROCEDURE = 'BILLS_TB_LIST_P';
      var toals = 0 ;
      if(SUBSCRIBER == '' && ID == '' && NAME == '' && NAME_BEN == ''){
           danger_msg('تحذير..','لم يتم تحديد أي محدد للبحث');
       }else{
          var newArr = {PKG:PKG,PROCEDURE:PROCEDURE,SUBSCRIBER:SUBSCRIBER,ID:ID,NAME:NAME,NAME_BEN:NAME_BEN}
           get_data('/'+_base_name+'/freecheck/Free_check/public_get_detailf',newArr ,function(data){
                 $("."+table_Free+"_Lists3 tbody tr").remove();
                 $(data).each(function(index, element) {
                       var ddssss = element.NET_TO_PAY - element.REMAINDER ;
                       toals = parseInt(toals) + parseInt(element.KW_CONSUMES);
                       $("."+table_Free+"_Lists3 tbody").append(GetTableRow([],
                       [element.SUBSCRIBER,element.NAME,element.FOR_MONTH,ddssss,element.KW_CONSUMES]));
                  })
                  if(toals == 0){
                      $('.Qraa').css('display','block');
                   }else{
                      $('.Qraa').css('display','none');
                   }
           },'json');
        }
        GeneralHideloading();
}
function SelectProcessForElement(NoFind){
	GeneralShowloading();
    Req = $('#Free_check_form').find("input[name='TXT_REQUEST_HIDDEN']").val();
    setTimeout(function() {
		  $.ajax({
                url: '/'+_base_name+'/freecheck/Free_check/create1/'+Req+'/'+NoFind,
                type: 'POST',
                datatype: 'json',
                success: function (data) {
					GeneralHideloading(); 
					if(data <= 0){
						 danger_msg('تحذير..','لم يتم الإضافة');
					}else{
						success_msg('رسالة','تم حفظ البيانات بنجاح ..');
					}
                 },
                error: function (jqXHR, textStatus, errorThrown) {  }
            });
    }, 500);
}
///
function PaidModals(NoFind , Request_app){
	var max = 0;
      var totals=0;
	  var total_paid=0;
	  $.ajax({
		url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/WEB_TRAD_PKG/SENTENCE_SER_GET/'+NoFind,
		type: 'POST',
		datatype: 'json',
		success: function (data) { 
		      max = data[0].SENTENCE_SER;
			  $.ajax({
				url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/PROCESS_PKG/V_SENTENCE_TB_GET/'+max,
				type: 'POST',
				datatype: 'json',
				success: function (data) { 
					if(data.length ==0){}else{
						  $('#TXT_CONST_NO').val(data[0].SENTENCE_SER);
						  $('#TXT_SER_P').val(data[0].P_SER_NO);
						  totals = parseInt(data[0].DEMURAGE_VALUE) + parseInt(data[0].CONSUMES_VALUE);
						  total_paid=parseInt(data[0].SUM_PAID);
						  $('#TXT_CONST_NAME').val(data[0].DEMURAGE_VALUE);
						  $('#TXT_CONST_NOTE').val(data[0].CONSUMES_VALUE);
						  var DV =  data[0].DEMURAGE_VALUE ;
						  var CV =  data[0].CONSUMES_VALUE ;
						  var l1 = 0 ;
						  var l2 = 0 ;
						  var l3 = 0 ;
						  $.ajax({
								url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/PROCESS_PKG/V_PAID_TB_GET/'+data[0].P_SER_NO,
								type: 'POST',
								datatype: 'json',
								success: function (data) { 
									if(data.length ==0){}else{
										    $('.'+table2+'_List1 table tbody tr').remove();
											$(data).each(function(key, value) {
													  count = count + 1;
													  l1 = parseInt(l1) + parseInt(value.TO_DEMURAGE);
													  l2 = parseInt(l2) + parseInt(value.TO_CONSUMES) ;
													  l3 = parseInt(l3) + parseInt(value.PAID_VALUE) ;
							  var html = '<tr><td><div><input type="text" value="'+value.PAID_SER+'" class="form-control TXT_PAID_SER" id="TXT_PAID_SER'+count+'" name="TXT_PAID_SER[]" readonly /><input type="hidden" name="CONST_FOLLOW_NO_SER[]" id="CONST_FOLLOW_NO_SER'+count+'" value="'+value.PAID_SER+'" /></div></td><td><div><input type="text" value="'+value.PAID_VALUE+'" class="form-control TXT_PAID_VALUE" id="TXT_PAID_VALUE'+count+'" name="TXT_PAID_VALUE[]" readonly /></div></td><td><div><select name="DP_FEELS_CURRENCY[]" class="form-control" id="DP_FEELS_CURRENCY'+count+'" data-val="'+value.CURRENCY+'"></select></div></td><td><div><a href="https://'+window.location.hostname+'/gfc/treasury/income_voucher/get/'+value.VOUCHER_NO+'" target="_blank">'+value.VOUCHER_NO+'</a> </div></td><td><div><input type="text" value="'+value.PAID_DATE+'" readonly class="form-control date-input" id="TXT_PAID_DATE'+count+'" name="TXT_PAID_DATE[]" /></div></td><td class="TO_DEMURAGE_TD"><div><input type="text" value="'+value.TO_DEMURAGE+'" class="form-control TXT_TO_DEMURAGE" id="TXT_TO_DEMURAGE'+count+'" readonly name="TXT_TO_DEMURAGE[]" /></div></td><td class="TO_CONSUMES_TD"><div><input type="text" value="'+value.TO_CONSUMES+'" class="form-control TXT_TO_CONSUMES" id="TXT_TO_CONSUMES'+count+'" readonly name="TXT_TO_CONSUMES[]" /></div></td><td><div><input type="text" value="'+value.SOURCE_TYPE_NAME+'" class="form-control TXT_SOURCE_TYPE" id="TXT_SOURCE_TYPE'+count+'" readonly name="TXT_SOURCE_TYPE[]" /></div></td><td><div><input type="text" value="'+value.SOURCE_ID+'" class="form-control TXT_SOURCE_ID" id="TXT_SOURCE_ID'+count+'" readonly name="TXT_SOURCE_ID[]" /></div></td></tr>';
													  $('.'+table2+'_List1 table tbody').append(html);
													  reBindA2(1);
											})
									}
									  var res1 = DV - l1;
									  var res2 = CV - l2;
									  $('#sum_demurge').text(l1);
									  $('#sum_consume').text(l2);
									  $('#sum_value').text(l3);
								
									  $('#TXT_CONST_NO_R').val(res1);
									  $('#TXT_CONST_NAME_R').val(res2);
									  $('#TXT_TOTALSAX').val(totals-total_paid);
								 },
								error: function (jqXHR, textStatus, errorThrown) {  }
						 });
					}
					  
				 },
				error: function (jqXHR, textStatus, errorThrown) {  }
			 });
		 },
		error: function (jqXHR, textStatus, errorThrown) {  }
	 });
	 //////One
     GeneralShowloading();
      setTimeout(function() {
		  GeneralHideloading();
		  /// Show Modal
		  $(".FormModal"+table2).modal('show');
      }, 500);
}
///
function reBindA2(s){
  if(s==undefined){s=0;}
  if(s){
	  $('select#DP_FEELS_CURRENCY'+count).append('<option></option>'+select_currency).val( $('select#DP_FEELS_CURRENCY'+count).attr('data-val') );
	  $('select#DL_FILE_TYPE'+count).append('<option></option>'+select_ttt).val( $('select#DL_FILE_TYPE'+count).attr('data-val') );
	  $('select#DL_FILE_TYPES'+count).append('<option></option>'+select_legal).val( $('select#DL_FILE_TYPES'+count).attr('data-val') );
  }
  $('select[id^="DL_FILE_TYPE"]').select2();
  $('select[id^="DL_FILE_TYPES"]').select2();
}
///
function openModalVisitationCash(NoFind , REQUEST_APP_SERIAL){
	  $("."+table2+"_ListFees tbody tr").remove();
	  var l1 = 0 ;
	  var l2 = 0 ;
	  ////
	  $.ajax({
		url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/SERVICE_NEW/V_PROCESS_TB_GET/'+NoFind,
		type: 'POST',
		datatype: 'json',
		success: function (data) { 
			if(data.length ==0){}else{
			  $(".Form"+table2+"ModalCollect").find("input[name='TXT_CONST_NO_FEES']").val(data[0].SENTENCE_SER);
			  $(".Form"+table2+"ModalCollect").find("input[name='TXT_SER_P_FEES']").val(data[0].P_SER_NO);
			  $(".Form"+table2+"ModalCollect").find("input[name='TXT_CONST_NAME_FEES']").val(data[0].DEMURAGE_VALUE);
			  $(".Form"+table2+"ModalCollect").find("input[name='TXT_CONST_NOTE_FEES']").val(data[0].CONSUMES_VALUE);
			  var DV =  data[0].DEMURAGE_VALUE ;
			  var CV =  data[0].CONSUMES_VALUE ;
			  ////		  
			  $(data).each(function(key, value) {
				  l1 = parseInt(l1) + parseInt(value.TO_DEMURAGE);
				  l2 = parseInt(l2) + parseInt(value.TO_CONSUMES) ;
			  });
			  ///
			  var res1 = DV - l1;
			  var res2 = CV - l2;
			  $(".Form"+table2+"ModalCollect").find("input[name='TXT_CONST_NO_R_FEES']").val(res1);
			  $(".Form"+table2+"ModalCollect").find("input[name='TXT_CONST_NAME_R_FEES']").val(res2);
			}
		 }		  
	  });
	  /////*********/////
	  $.ajax({
			url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/SERVICES_PKG/FEES_REQUEST_P_APP_SERIAL_GET/'+NoFind,
			type: 'POST',
			datatype: 'json',
			success: function (datas) { 
			   if(datas.length ==0){ }else{
					var req_app_ser = datas[0].REQUEST_APP_SERIAL;
					if(req_app_ser <= 0){
						var count = 1;
						var html = '<tr><td></td><td><input name="TXT_FEELS_NO[]" class="form-control" id="TXT_FEELS_NO" value="838" readonly /></td><td><input name="TXT_FEELS_NAME[]" class="form-control" id="TXT_FEELS_NAME1" value="رسم الغرامة" readonly /></td><td><input type="text" class="form-control"  id="TXT_FEELS_VALUE1" name="TXT_FEELS_VALUE[]"  /></td><td><select name="DP_FEELS_CURRENCY[]" class="form-control" id="DP_FEELS_CURRENCY'+count+'" data-val="1"><option value="0">----اختر----</option><option value="1" selected>شيكل</option><option value="2">دولار</option><option value="3">دينار</option></select></td><td><select name="DL_PAID_TYPE[]" class="form-control" id="DL_PAID_TYPE'+count+'" data-val="1"><option value="0">----اختر----</option><option value="1" selected>نقدا</option><option value="2">إضافة</option><option value="3">أقساط</option></select></td><td><input type="text" class="form-control" id="TXT_PAID_NO'+count+'" name="TXT_PAID_NO[]" readonly /></td><td><input type="text" class="form-control" id="TXT_PAID_DATE'+count+'" name="TXT_PAID_DATE[]" readonly /> </td><td><select name="DL_IS_PAID[]" class="form-control" id="DL_IS_PAID'+count+'" data-val="1"><option value="0">----اختر----</option><option value="1">لا</option><option value="2">نعم</option></select></td></tr>';
						html+= '<tr><td></td><td><input name="TXT_FEELS_NO[]" class="form-control" id="TXT_FEELS_NO2" value="837" readonly /></td><td><input name="TXT_FEELS_NAME[]" class="form-control" id="TXT_FEELS_NAME2" value="رسم فرق الاستهلاك" readonly /></td><td><input type="text" class="form-control"  id="TXT_FEELS_VALUE2" name="TXT_FEELS_VALUE[]"  /></td><td><select name="DP_FEELS_CURRENCY[]" class="form-control" id="DP_FEELS_CURRENCY'+(count+1)+'" data-val="1"><option value="0">----اختر----</option><option value="1" selected>شيكل</option><option value="2">دولار</option><option value="3">دينار</option></select></td><td><select name="DL_PAID_TYPE[]" class="form-control" id="DL_PAID_TYPE'+(count+1)+'" data-val="1"><option value="0">----اختر----</option><option value="1" selected>نقدا</option><option value="2">إضافة</option><option value="3">أقساط</option></select></td><td><input type="text" class="form-control" id="TXT_PAID_NO'+(count+1)+'" name="TXT_PAID_NO[]" readonly /></td><td><input type="text" class="form-control" id="TXT_PAID_DATE'+(count+1)+'" name="TXT_PAID_DATE[]" readonly /> </td><td><select name="DL_IS_PAID[]" class="form-control" id="DL_IS_PAID'+(count+1)+'" data-val="1"><option value="0">----اختر----</option><option value="1">لا</option><option value="2">نعم</option></select></td></tr>';
						$('.'+table2+'_ListFees table tbody').append(html);
						reBindA3(1);
					}else{
						$.ajax({
							url: '/'+_base_name+'/freecheck/Free_check/public_get_details_three/SERVICES_PKG/FEES_REQUEST_TB_PAID_ST2/115/'+req_app_ser+'/0',
							type: 'POST',
							datatype: 'json',
							success: function (data_details) { 
									var value_consume=0;
									var value_demurge=0;
									var count_i = 0;
									var count = 1;
									var nameList = [];
									  if(data_details.length ==0){ }else{
										 	$.each(JSON.parse(data_details), function( index, value ) {
												var PAID_NOS = value.PAID_NO != null ? value.PAID_NO : '' ;
												var PAID_DATES = value.PIAD_DATE != null ? value.PIAD_DATE : '' ;
											    ////
												var html = '<tr><td><input name="TXT_REQ_SERIALS[]" class="form-control" id="TXT_REQ_SERIALS'+(count+1)+'" value="'+value.REQUEST_APP_SERIAL+'" readonly /></td><td><input name="TXT_FEELS_NO[]" class="form-control" id="TXT_FEELS_NO'+(count+1)+'" value="'+value.FEES_NO+'" readonly /></td><td><input name="TXT_FEELS_NAME[]" class="form-control" id="TXT_FEELS_NAME'+(count+1)+'" value="'+value.FEES_NAME+'" readonly /></td><td><input type="text" class="form-control"  id="TXT_FEELS_VALUE'+(count+1)+'" name="TXT_FEELS_VALUE[]" value="'+value.THE_VALUE+'"  /></td><td><select name="DP_FEELS_CURRENCY[]" class="form-control" id="DP_FEELS_CURRENCY'+(count+1)+'" data-val="'+value.CURRENCYS+'"><option value="0">----اختر----</option><option value="1" selected>شيكل</option><option value="2">دولار</option><option value="3">دينار</option></select></td><td><select name="DL_PAID_TYPE[]" class="form-control" id="DL_PAID_TYPE'+(count+1)+'" data-val="'+value.PAID_TYPE+'"><option value="0">----اختر----</option><option value="1" selected>نقدا</option><option value="2">إضافة</option><option value="3">أقساط</option></select></td><td><input type="text" class="form-control" id="TXT_PAID_NO'+(count+1)+'" name="TXT_PAID_NO[]" readonly value="'+PAID_NOS+'" /></td><td><input type="text" class="form-control" id="TXT_PAID_DATE'+(count+1)+'" name="TXT_PAID_DATE[]" readonly value="'+PAID_DATES+'" /> </td><td><select name="DL_IS_PAID[]" class="form-control" id="DL_IS_PAID'+(count+1)+'" data-val="'+value.IS_PAID+'"><option value="0">----اختر----</option><option value="1" >لا</option><option value="2">نعم</option></select></td></tr>';
												///
												$('.'+table2+'_ListFees table tbody').append(html);
												reBindA3(1);
												////
											});  
									  }
							 },
							error: function (jqXHR, textStatus, errorThrown) {  }
						});
					}
			   }
			 },
			error: function (jqXHR, textStatus, errorThrown) {  }
	  });
	  /////*********/////
      $(".FormModalCollect"+table2).modal('show');
}
//
$('#btn_collect_amount'+table2).click(function(){
	  var NoFind = $(".Form"+table2+"Modal").find("input[name='TXT_SER_P']").val();
	  $("."+table2+"_ListFees tbody tr").remove();
	  $.ajax({
			url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/WEB_TRAD_PKG/SENTENCE_SER_GET/'+NoFind,
			type: 'POST',
			datatype: 'json',
			success: function (data) { 
					  var sentSer = data[0].SENTENCE_SER;
					  $.ajax({
						url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/PROCESS_PKG/V_SENTENCE_TB_GET/'+sentSer,
						type: 'POST',
						datatype: 'json',
						success: function (data) { 
						   if(data.length ==0){}else{
							  $(".Form"+table2+"ModalCollect").find("input[name='TXT_CONST_NO_FEES']").val(data[0].SENTENCE_SER);
							  $(".Form"+table2+"ModalCollect").find("input[name='TXT_SER_P_FEES']").val(data[0].P_SER_NO);
							  $(".Form"+table2+"ModalCollect").find("input[name='TXT_CONST_NAME_FEES']").val(data[0].DEMURAGE_VALUE);
							  $(".Form"+table2+"ModalCollect").find("input[name='TXT_CONST_NOTE_FEES']").val(data[0].CONSUMES_VALUE);
							  var DV =  data[0].DEMURAGE_VALUE ;
							  var CV =  data[0].CONSUMES_VALUE ;
							  var l1 = 0 ;
							  var l2 = 0 ;
							  /////
							  $.ajax({
									url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/PROCESS_PKG/V_PAID_TB_GET/'+data[0].P_SER_NO,
									type: 'POST',
									datatype: 'json',
									success: function (datas) { 
										  if(datas.length ==0){ }else{
												  $(datas).each(function(key, value) {
													  l1 = parseInt(l1) + parseInt(value.TO_DEMURAGE);
													  l2 = parseInt(l2) + parseInt(value.TO_CONSUMES) ;
												  });
												  var res1 = DV - l1;
												  var res2 = CV - l2;
												  $(".Form"+table2+"ModalCollect").find("input[name='TXT_CONST_NO_R_FEES']").val(res1);
												  $(".Form"+table2+"ModalCollect").find("input[name='TXT_CONST_NAME_R_FEES']").val(res2);
										   }
									 },
									error: function (jqXHR, textStatus, errorThrown) {  }
							  });
							  /////*********/////
							  $.ajax({
									url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/SERVICES_PKG/FEES_REQUEST_P_APP_SERIAL_GET/'+data[0].P_SER_NO,
									type: 'POST',
									datatype: 'json',
									success: function (datas) { 
									   if(datas.length ==0){ }else{
											var req_app_ser = datas[0].REQUEST_APP_SERIAL;
											if(req_app_ser <= 0){
												var count = 1;
												var html = '<tr><td><input name="TXT_FEELS_NO[]" class="form-control" id="TXT_FEELS_NO" value="838" readonly /></td><td><input name="TXT_FEELS_NAME[]" class="form-control" id="TXT_FEELS_NAME1" value="رسم الغرامة" readonly /></td><td><input type="text" class="form-control"  id="TXT_FEELS_VALUE1" name="TXT_FEELS_VALUE[]"  /></td><td><select name="DP_FEELS_CURRENCY[]" class="form-control" id="DP_FEELS_CURRENCY'+count+'" data-val="1"><option value="0">----اختر----</option><option value="1" selected>شيكل</option><option value="2">دولار</option><option value="3">دينار</option></select></td><td><select name="DL_PAID_TYPE[]" class="form-control" id="DL_PAID_TYPE'+count+'" data-val="1"><option value="0">----اختر----</option><option value="1" selected>نقدا</option><option value="2">إضافة</option><option value="3">أقساط</option></select></td><td><input type="text" class="form-control" id="TXT_PAID_NO'+count+'" name="TXT_PAID_NO[]" readonly /></td><td><input type="text" class="form-control" id="TXT_PAID_DATE'+count+'" name="TXT_PAID_DATE[]" readonly /> </td><td><select name="DL_IS_PAID[]" class="form-control" id="DL_IS_PAID'+count+'" data-val="1"><option value="0">----اختر----</option><option value="1">لا</option><option value="2">نعم</option></select></td></tr>';
												html+= '<tr><td><input name="TXT_FEELS_NO[]" class="form-control" id="TXT_FEELS_NO2" value="837" readonly /></td><td><input name="TXT_FEELS_NAME[]" class="form-control" id="TXT_FEELS_NAME2" value="رسم فرق الاستهلاك" readonly /></td><td><input type="text" class="form-control"  id="TXT_FEELS_VALUE2" name="TXT_FEELS_VALUE[]"  /></td><td><select name="DP_FEELS_CURRENCY[]" class="form-control" id="DP_FEELS_CURRENCY'+(count+1)+'" data-val="1"><option value="0">----اختر----</option><option value="1" selected>شيكل</option><option value="2">دولار</option><option value="3">دينار</option></select></td><td><select name="DL_PAID_TYPE[]" class="form-control" id="DL_PAID_TYPE'+(count+1)+'" data-val="1"><option value="0">----اختر----</option><option value="1" selected>نقدا</option><option value="2">إضافة</option><option value="3">أقساط</option></select></td><td><input type="text" class="form-control" id="TXT_PAID_NO'+(count+1)+'" name="TXT_PAID_NO[]" readonly /></td><td><input type="text" class="form-control" id="TXT_PAID_DATE'+(count+1)+'" name="TXT_PAID_DATE[]" readonly /> </td><td><select name="DL_IS_PAID[]" class="form-control" id="DL_IS_PAID'+(count+1)+'" data-val="1"><option value="0">----اختر----</option><option value="1">لا</option><option value="2">نعم</option></select></td></tr>';
												$('.'+table2+'_ListFees table tbody').append(html);
												reBindA3(1);
											}else{
												$.ajax({
													url: '/'+_base_name+'/freecheck/Free_check/public_get_details_three/SERVICES_PKG/FEES_REQUEST_TB_PAID_ST2/115/'+req_app_ser+'/0',
													type: 'POST',
													datatype: 'json',
													success: function (data_details) { 
															var value_consume=0;
															var value_demurge=0;
															var count_i = 0;
															  if(data_details.length ==0){ }else{
																	$(data_details).each(function(key, value) {
																		if(parseInt(value.FEES_NO) == 837){ value_consume = value.THE_VALUE}
																		if(parseInt(value.FEES_NO) == 838){ value_demurge = value.THE_VALUE}
																		count_i = count_i+1;
																	});
									
																	var count = 1;
																	var html = '<tr><td><input name="TXT_FEELS_NO[]" class="form-control" id="TXT_FEELS_NO" value="838" readonly /></td><td><input name="TXT_FEELS_NAME[]" class="form-control" id="TXT_FEELS_NAME1" value="رسم الغرامة" readonly /></td><td><input type="text" class="form-control"  id="TXT_FEELS_VALUE1" name="TXT_FEELS_VALUE[]" value="'+value_consume+'"  /></td><td><select name="DP_FEELS_CURRENCY[]" class="form-control" id="DP_FEELS_CURRENCY'+count+'" data-val="1"><option value="0">----اختر----</option><option value="1" selected>شيكل</option><option value="2">دولار</option><option value="3">دينار</option></select></td><td><select name="DL_PAID_TYPE[]" class="form-control" id="DL_PAID_TYPE'+count+'" data-val="1"><option value="0">----اختر----</option><option value="1" selected>نقدا</option><option value="2">إضافة</option><option value="3">أقساط</option></select></td><td><input type="text" class="form-control" id="TXT_PAID_NO'+count+'" name="TXT_PAID_NO[]" readonly /></td><td><input type="text" class="form-control" id="TXT_PAID_DATE'+count+'" name="TXT_PAID_DATE[]" readonly /> </td><td><select name="DL_IS_PAID[]" class="form-control" id="DL_IS_PAID'+count+'" data-val="1"><option value="0">----اختر----</option><option value="1" >لا</option><option value="2">نعم</option></select></td></tr>';
																	html+= '<tr><td><input name="TXT_FEELS_NO[]" class="form-control" id="TXT_FEELS_NO2" value="837" readonly /></td><td><input name="TXT_FEELS_NAME[]" class="form-control" id="TXT_FEELS_NAME2" value="رسم فرق الاستهلاك" readonly /></td><td><input type="text" class="form-control"  id="TXT_FEELS_VALUE2" name="TXT_FEELS_VALUE[]" value="'+value_demurge+'"  /></td><td><select name="DP_FEELS_CURRENCY[]" class="form-control" id="DP_FEELS_CURRENCY'+(count+1)+'" data-val="1"><option value="0">----اختر----</option><option value="1" selected>شيكل</option><option value="2">دولار</option><option value="3">دينار</option></select></td><td><select name="DL_PAID_TYPE[]" class="form-control" id="DL_PAID_TYPE'+(count+1)+'" data-val="1"><option value="0">----اختر----</option><option value="1" selected>نقدا</option><option value="2">إضافة</option><option value="3">أقساط</option></select></td><td><input type="text" class="form-control" id="TXT_PAID_NO'+(count+1)+'" name="TXT_PAID_NO[]" readonly /></td><td><input type="text" class="form-control" id="TXT_PAID_DATE'+(count+1)+'" name="TXT_PAID_DATE[]" readonly /> </td><td><select name="DL_IS_PAID[]" class="form-control" id="DL_IS_PAID'+(count+1)+'" data-val="1"><option value="0">----اختر----</option><option value="1">لا</option><option value="2">نعم</option></select></td></tr>';
																	$('.'+table2+'_ListFees table tbody').append(html);
																	reBindA3(1);
															   }
													 },
													error: function (jqXHR, textStatus, errorThrown) {  }
												});
											}
									   }
									 },
									error: function (jqXHR, textStatus, errorThrown) {  }
							  });
							  /////*********/////
						   }
						 },
						error: function (jqXHR, textStatus, errorThrown) {  }
					});
			 },
			error: function (jqXHR, textStatus, errorThrown) {  }
	  });
	  $(".FormModal"+table2).modal('hide');
      $(".FormModalCollect"+table2).modal('show');
});
///
function reBindA3(s){
  
}
///
$('#btn_push_fees'+table2).click(function(){
  $.post($('#Form'+table2+'ModalCollect').attr('action'),$('#Form'+table2+'ModalCollect').serialize(), function(r){
	  $(r).each(function(index, element) {
		  if(element['o_success'] == 0){
			danger_msg('تحذير..',element['o_msgtxt']);
		  }
		  if(element['o_success'] == 1){
			success_msg('رسالة','تم حفظ البيانات بنجاح ..');
			//get_to_link(window.location.href);
		  }
	  })
	  },'json');
  return false;
});
////
function SubscriberBenifit(Request , Subsc){
	window.location.href = '/'+_base_name+'/EditQuotasGeneral/'+Request+'/'+Subsc;
	/*GeneralShowloading();
    ///
    $(".Free_check_ListIns table tbody tr").remove();
    $('.demRet').text(0); 
    $('.consRet').text(0);
    $('.valRet').text(0);
    $(".valBill").text(0);
    $('.valBiRet').text(0);
    $('.valBaqi').text(0);
    ///
    var total1 = 0 ;
    var total2 = 0;
    var total3 = 0;
    var TotalBill = 0 ;
    var toalss = 0 ;
    var Last = 0 ;
    var totals = 0 ;
    var demarge = 0;
    var consume = 0;
    setTimeout(function() {
		    $.ajax({
                url: '/'+_base_name+'/freecheck/Free_check/public_get_details/PROCESS_PKG/V_PAID_TB_GET/'+NoFind,
                type: 'POST',
                datatype: 'json',
                success: function (data) { 
                        $("."+table+"_ListIns tbody tr").remove();
						$(data).each(function(index, element) {
                            if(element.TO_DEMURAGE == null){
								demarge = 0 ;
							}else{
								demarge = element.TO_DEMURAGE ;
							}
							if(element.TO_CONSUMES == null){
								consume = 0 ;
							}else{
								consume = element.TO_CONSUMES ;
							}
							total1 = parseInt(total1) + parseInt(demarge);
							total2 = parseInt(total2) + parseInt(consume);
							total3 = parseInt(total3) + parseInt(element.PAID_VALUE);
							$("."+table_Free+"_ListIns tbody").append(GetTableR([],
								   [element.P_SER_NO,element.PAID_VALUE,element.VOUCHER_NO,element.PAID_DATE,demarge,consume]));
					    });
					    $('.demRet').text(total1); 
					    $('.consRet').text(total2);
					    $('.valRet').text(total3);
						///// بطلع الإضافة ع الفاتورة  
						$.ajax({
							url: '/'+_base_name+'/freecheck/Free_check/public_get_twoDets/V_ADD_BILLS_TB_BRANCH_GET/'+NoFind+'/'+session_users,
							type: 'POST',
							datatype: 'json',
							success: function (dataS) { 
									   if(dataS.length == 0){ }else{
										  $(dataS).each(function(index, element) {
											  TotalBill = parseInt(TotalBill) + parseInt(element.BILL_ADD);
										  });
									   }
									   $(".valBill").text(TotalBill);
									   totals = parseInt(TotalBill) + parseInt(total3) ;
									   $('.valBiRet').text(totals);
									   ///// بطلع الغرامة وفرق الاستهلاك
									   $.ajax({
										url: '/'+_base_name+'/freecheck/Free_check/public_get_twoDets/V_SENTENCE_TRANSACTION_BRANCH/'+NoFind+'/'+session_users,
										type: 'POST',
										datatype: 'json',
										success: function (dataF) { 
												  if(dataF.length == 0){ }else{
													  $(dataF).each(function(index, element) {
														   toalss = parseInt(element.DEMURAGE_VALUE) + parseInt(element.CONSUMES_VALUE);
														   Last = parseInt(toalss) - parseInt(totals) ;
														   $('.valBaqi').text(Last);
													  })
												  }
										 },
										error: function (jqXHR, textStatus, errorThrown) {  }
									});
							 },
							error: function (jqXHR, textStatus, errorThrown) {  }
						});
						//
                 },
                error: function (jqXHR, textStatus, errorThrown) {  }
            });
			GeneralHideloading();
			/// Show Modal
            $("."+table_Free+"Modal1_Free").modal('show');
	}, 500);*/
}
/////// اظهار مودال بيانات التحصيل
function SubscriberActual(NoFind , Sub_Tab){
	Req = $('#Free_check_form').find("input[name='TXT_REQUEST_HIDDEN']").val();
	subsss = $('form').find("input[name='TXT_SUBSCRIBER']").val();
	$('#TXT_REQUEST_APP_SERIAL_Free').val(Req);
    $('.SUBSCRIBER_LATE_Free').val(Sub_Tab);
    $('.MOTA5ERAT_Free').val(NoFind);
    $('#TXT_SUBSCRIBERS_Free').val(subsss);
    ///////
	GeneralShowloading();
    setTimeout(function() {
		  GeneralHideloading();
		  	//
			$.ajax({
                url: '/'+_base_name+'/freecheck/Free_check/public_get_details/EBLLC_PKG/SUBSCRIBERS_TB_GET/'+Sub_Tab,
                type: 'POST',
                datatype: 'json',
                success: function (dataA) { 
					if(dataA.length==0){ alert(); }else{
							 var INFOA = dataA[0];
							 $('#TXT_NO_ID_Free').val(INFOA.ID);
							 $('#TXT_SUBSCRIBER_NAME_Free').val(INFOA.NAME);
							 ///
							 $('#BR_NAME_Free').val(INFOA.BRANCH_NAME);
							 $('#BR_ID_Free').val(INFOA.BRANCH);
							 ///
							 $('#DL_REQ_FE_TYPE_Free').val(INFOA.PAYMENT_WAY_NO).prop('selected',true);
							 $('#DL_REQ_FE_TYPE_Free').change();
							 ///
							 if(INFOA.PAYMENT_WAY_NO == 1){
								  $.ajax({
									url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/EBLLC_PKG/BANK_SUBSCRIBERS_GET/'+Sub_Tab,
									type: 'POST',
									datatype: 'json',
									success: function (datasAs) { 
											  if(datasAs.length ==0){ }else{ 
													 var insfo = datasAs[0];
													 $('#TXT_BANK_NO_Free').val(insfo.ACCOUNT_NO);
													 $('#DL_BANKS_Free').val(insfo.B_NO).prop('selected',true);
													 $('#DL_BANKS_Free').change();
													 $('#DL_BANK_BRANCH_Free').val(insfo.B_BR_NO).prop('selected',true);
													 $('#DL_BANK_BRANCH_Free').change();
													 Bank_Br_Free = insfo.B_BR_NO;
											   }
									 },
									 error: function (jqXHR, textStatus, errorThrown) {  }
								  });
							 }else if(INFOA.PAYMENT_WAY_NO == 2){
								  $.ajax({
									url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/SERVICE_NEW/STRUCTURED_BOND_TB_GET/'+Sub_Tab,
									type: 'POST',
									datatype: 'json',
									success: function (datasAs) { 
										   if(datasAs.length ==0){ }else{ 
												 var insfo = datasAs[0];
												 $('#TXT_SEND_NO_Free').val(insfo.BOND_NO);
												 $('#TXT_THE_YEAR_Free').val(insfo.BOND_YEAR);
												 $('#TXT_BOND_VALUE_Free').val(insfo.BOND_VALUE);
												 $('#TXT_BOND_DATE_Free').val(insfo.BOND_DATE);
												 $('#TXT_FIRST_DATE_PAID_Free').val(insfo.FROM_INSTALLMENT_DATE);
										   }
									 },
									 error: function (jqXHR, textStatus, errorThrown) {  }
								  });
							 }else{
			
							 }
					 }
                 },
                error: function (jqXHR, textStatus, errorThrown) {  }
            });
			//
		    $.ajax({
                url: '/'+_base_name+'/freecheck/Free_check/public_get_details/TSG_SETTING_PKG/SERVSYSTEM_TB_GET/'+1,
                type: 'POST',
                datatype: 'json',
                success: function (data) { 
                         var INFO = data[0];
						 if(NoFind > parseInt(INFO.MLEVEL)){
							$('.NOTES_Free').val(INFO.MLVH);
							$('.PERCENTS_Free').val((INFO.MVALUEH)+'%');
							var perc = parseInt(INFO.MVALUEH) / 100 ;
							$('.VALUEC_Free').val((perc * parseInt(NoFind)).toFixed(2));
						 } else{
							$('.NOTES_Free').val(INFO.MLVL);
							$('.PERCENTS_Free').val((INFO.MVALUEL)+'%');
							var perc = parseInt(INFO.MVALUEL) / 100 ;
							$('.VALUEC_Free').val((perc * parseInt(NoFind)).toFixed(2));
						 } 
                 },
                error: function (jqXHR, textStatus, errorThrown) {  }
            });
			/// Show Modal
            $("."+table_Free+"Modal2_Free").modal('show'); 
	}, 500);
}
/////// اظهار مودال بيانات التحصيل
function SubscriberActualQuats(NoFind , Sub_Tab){
	Req = $('#Quotas_form').find("input[name='TXT_REQUEST_APP_SERIAL']").val();
	subsss = $('#Quotas_form').find("input[name='TXT_SUBSCRIBER']").val();
	$('#TXT_REQUEST_APP_SERIAL_Free').val(Req);
    $('.SUBSCRIBER_LATE_Free').val(Sub_Tab);
    $('.MOTA5ERAT_Free').val(NoFind);
    $('#TXT_SUBSCRIBERS_Free').val(subsss);
    ///////
	GeneralShowloading();
    setTimeout(function() {
		  GeneralHideloading();
		  	//
			$.ajax({
                url: '/'+_base_name+'/freecheck/Free_check/public_get_details/EBLLC_PKG/SUBSCRIBERS_TB_GET/'+Sub_Tab,
                type: 'POST',
                datatype: 'json',
                success: function (dataA) { 
					if(dataA.length==0){ alert(); }else{
							 var INFOA = dataA[0];
							 $('#TXT_NO_ID_Free').val(INFOA.ID);
							 $('#TXT_SUBSCRIBER_NAME_Free').val(INFOA.NAME);
							 ///
							 $('#BR_NAME_Free').val(INFOA.BRANCH_NAME);
							 $('#BR_ID_Free').val(INFOA.BRANCH);
							 ///
							 $('#DL_REQ_FE_TYPE_Free').val(INFOA.PAYMENT_WAY_NO).prop('selected',true);
							 $('#DL_REQ_FE_TYPE_Free').change();
							 ///
							 if(INFOA.PAYMENT_WAY_NO == 1){
								  $.ajax({
									url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/EBLLC_PKG/BANK_SUBSCRIBERS_GET/'+Sub_Tab,
									type: 'POST',
									datatype: 'json',
									success: function (datasAs) { 
											  if(datasAs.length ==0){ }else{ 
													 var insfo = datasAs[0];
													 $('#TXT_BANK_NO_Free').val(insfo.ACCOUNT_NO);
													 $('#DL_BANKS_Free').val(insfo.B_NO).prop('selected',true);
													 $('#DL_BANKS_Free').change();
													 $('#DL_BANK_BRANCH_Free').val(insfo.B_BR_NO).prop('selected',true);
													 $('#DL_BANK_BRANCH_Free').change();
													 Bank_Br_Free = insfo.B_BR_NO;
											   }
									 },
									 error: function (jqXHR, textStatus, errorThrown) {  }
								  });
							 }else if(INFOA.PAYMENT_WAY_NO == 2){
								  $.ajax({
									url: '/'+_base_name+'/requestsclearence/Requests_clearence/public_get_details/SERVICE_NEW/STRUCTURED_BOND_TB_GET/'+Sub_Tab,
									type: 'POST',
									datatype: 'json',
									success: function (datasAs) { 
										   if(datasAs.length ==0){ }else{ 
												 var insfo = datasAs[0];
												 $('#TXT_SEND_NO_Free').val(insfo.BOND_NO);
												 $('#TXT_THE_YEAR_Free').val(insfo.BOND_YEAR);
												 $('#TXT_BOND_VALUE_Free').val(insfo.BOND_VALUE);
												 $('#TXT_BOND_DATE_Free').val(insfo.BOND_DATE);
												 $('#TXT_FIRST_DATE_PAID_Free').val(insfo.FROM_INSTALLMENT_DATE);
										   }
									 },
									 error: function (jqXHR, textStatus, errorThrown) {  }
								  });
							 }else{
			
							 }
					 }
                 },
                error: function (jqXHR, textStatus, errorThrown) {  }
            });
			//
		    $.ajax({
                url: '/'+_base_name+'/freecheck/Free_check/public_get_details/TSG_SETTING_PKG/SERVSYSTEM_TB_GET/'+1,
                type: 'POST',
                datatype: 'json',
                success: function (data) { 
                         var INFO = data[0];
						 if(NoFind > parseInt(INFO.MLEVEL)){
							$('.NOTES_Free').val(INFO.MLVH);
							$('.PERCENTS_Free').val((INFO.MVALUEH)+'%');
							var perc = parseInt(INFO.MVALUEH) / 100 ;
							$('.VALUEC_Free').val((perc * parseInt(NoFind)).toFixed(2));
						 } else{
							$('.NOTES_Free').val(INFO.MLVL);
							$('.PERCENTS_Free').val((INFO.MVALUEL)+'%');
							var perc = parseInt(INFO.MVALUEL) / 100 ;
							$('.VALUEC_Free').val((perc * parseInt(NoFind)).toFixed(2));
						 } 
                 },
                error: function (jqXHR, textStatus, errorThrown) {  }
            });
			/// Show Modal
            $("."+table_Free+"Modal2_Free").modal('show'); 
	}, 500);
}
///
$('#DL_REQ_FE_TYPE_Free').change(function(){
     NOF = $(this).val();
     if(NOF == 1){
        $('.WAYPAYSADAD').css('display','block');
        $('.WAYPAYMOSBAQ').css('display','none');
        $('.Ethtenthna').css('display','none');
     }else if(NOF == 2){
        $('.WAYPAYMOSBAQ').css('display','block');
        $('.WAYPAYSADAD').css('display','none');
        $('.Ethtenthna').css('display','none');
     }else{
        $('.WAYPAYSADAD').css('display','none');
        $('.WAYPAYMOSBAQ').css('display','none');
        $('.Ethtenthna').css('display','block');
     }
});
/// عرض فواتير اشتراك عادي
function showBills(NoFind){
	GeneralShowloading();
	$("."+table_Free+"_ListIns89 tbody tr").remove();
	$.ajax({
		url: '/'+_base_name+'/freecheck/Free_check/public_get_details/EBLLC_PKG/BILLS_TB_12_LIST/'+NoFind,
		type: 'POST',
		datatype: 'json',
		success: function (data) { 
			if(data.length==0){ }else{
				$(data).each(function(index, element) {
						$("."+table_Free+"_ListIns89 tbody").append(GetTableRow([],[element.SUBSCRIBER,
						element.FOR_MONTH,element.PAID,element.VALUEE,element.DATE_,element.SUM_VALUE,
						element.BANK_NAME]));
				});
				GeneralHideloading();
				/// Show Modal
   				$("."+table_Free+"Modal89_Free").modal('show');
			}
		 },
		error: function (jqXHR, textStatus, errorThrown) {  }
	});
}
/// عرض شحنات مسبق الدفع
function ShowPrPaidCharge(NoFind){
	GeneralShowloading();
	$(".Free_check_ListIns89 tbody tr").remove();
	$.ajax({
		url: '/'+_base_name+'/freecheck/Free_check/public_get_details/PRE_PAID_PKG/PRE_PAID_POWER_GET/'+NoFind,
		type: 'POST',
		datatype: 'json',
		success: function (data) { 
			if(data.length==0){ 
				danger_msg('للإفادة..','لا يوجد شحنات لهذا الإشتراك');
				GeneralHideloading(); 
			}else{
                $(data).each(function(index, element) {
                        $(".Free_check_ListIns89 tbody").append(GetTableRow([],
                       [element.SUB_NO,element.BUYDATE,element.ACCEPT]));
                });
				GeneralHideloading();
				/// Show Modal
       			$("."+table_Free+"ModalPrepaid_Free").modal('show');
            }
		 },
		error: function (jqXHR, textStatus, errorThrown) {  }
	});
}
/// اظهار مودال ادخال ملاحظات على رقم الاشتراك المبحوث عنه
function AddNotes(NoFind){
   var Request_app_serial = $('#'+table_Free+'_form').find('input[name="HIDDURL"]').val();
   $('#TXT_SUBSCRIBER_NOTE').val(NoFind);
   $('#TXT_REQUEST_APP_SERIAL_NOTE').val(Request_app_serial);
   //
   $.ajax({
		url: '/'+_base_name+'/freecheck/Free_check/public_get_details/SERVICES_T_PKG/REQUEST_APP_NOTES1_TB_GET/'+Request_app_serial,
		type: 'POST',
		datatype: 'json',
		success: function (data) { 
			if(data.length==0){ }else{
                $('#TXT_NOTE_LAST').val(data[0].NOTE);
            }
		 },
		error: function (jqXHR, textStatus, errorThrown) {  }
   });
   /// Show Modal
   $("."+table_Free+"ModalNote").modal('show');
}
///// Add In Database
/// ادخال الملاحظات
$('#SAVE_NOTE'+table_Free).click(function(){
    $.post($('#Note_Table_'+table_Free).attr('action'),$('#Note_Table_'+table_Free).serialize(), function(r){
        $(r).each(function(index, element) {
            if(element['o_success'] == 0){
                 danger_msg('تحذير..',element['o_msgtxt']);
            }
            if(element['o_success'] == 1){
                 success_msg('رسالة','تم حفظ البيانات بنجاح ..');
            }
        })
    },'json');
    return false;
});
///// Add In Database
/// ادخال بيانات التحصيل
$('#SAVE'+table_Free).click(function(){
    $.post($('#_Form_Table_'+table_Free).attr('action'),$('#_Form_Table_'+table_Free).serialize(), function(r){
        $(r).each(function(index, element) {
            if(element['o_success'] == 0){
                 danger_msg('تحذير..',element['o_msgtxt']);
            }
            if(element['o_success'] == 1){
                 success_msg('رسالة','تم حفظ البيانات بنجاح ..');
                 //get_to_link(window.location.href);
            }
        })
    },'json');
    return false;
});
///اظهار مودال الإرجاع
function showModaleReturnRequest(NoFind){
	GeneralShowloading();
	 
	$.ajax({
		url: '/'+_base_name+'/freecheck/Free_check/public_get_details/SERVICE_NEW/SERVICES_TRACTOR_DEF_REQ1_GET/'+NoFind,
		type: 'POST',
		datatype: 'json',
		success: function (data) { 
			if(data.length ==0){
			  $('#DL_STAGE_BACK_ID option').remove();
			  $('#DL_STAGE_BACK_ID').append($('<option/>').attr("value", '').text(''));
			  $('#DL_STAGE_BACK_ID').prop("selected",true);
			  $("#DL_STAGE_BACK_ID").change();
		   }else{
				$('#DL_STAGE_BACK_ID option').remove();
				$('#DL_STAGE_BACK_ID').append($('<option/>').attr("value", '').text('---اختر---'));
				$.each(data,function (i,option) {
				   $('#DL_STAGE_BACK_ID').append($('<option/>').attr("value", option.STAGE_DEFINITION_ID+','+option.STAGE_DEFINITION_ORDER).text(option.STAGE_DEFINITION_ID_NAME));
				   $("#DL_STAGE_BACK_ID").change();
				});
				$('.Return_Table_Modals').find("input[name='TXT_REQUEST_APP_SERIAL_RETURN']").val(NoFind);
		   }
		   GeneralHideloading();
		   /// Show Modal
           $(".ModalReturns").modal('show');
		 },
		error: function (jqXHR, textStatus, errorThrown) {  }
	});
}
///اظهار مودال الإرجاع لطلبات مسبق الدفع
function showModaleReturnPmeter(NoFind){
	GeneralShowloading();
	 
	$.ajax({
		url: '/'+_base_name+'/freecheck/Free_check/public_get_details/PMETER_PKG/PMETER_TRACTOR_DEF_REQ1_GET/'+NoFind,
		type: 'POST',
		datatype: 'json',
		success: function (data) { 
			if(data.length ==0){
			  $('#DL_STAGE_BACK_ID1 option').remove();
			  $('#DL_STAGE_BACK_ID1').append($('<option/>').attr("value", '').text(''));
			  $('#DL_STAGE_BACK_ID1').prop("selected",true);
			  $("#DL_STAGE_BACK_ID1").change();
		   }else{
				$('#DL_STAGE_BACK_ID1 option').remove();
				$('#DL_STAGE_BACK_ID1').append($('<option/>').attr("value", '').text('---اختر---'));
				$.each(data,function (i,option) {
				   $('#DL_STAGE_BACK_ID1').append($('<option/>').attr("value", option.STAGE_DEFINITION_ID+','+option.STAGE_DEFINITION_ORDER).text(option.STAGE_DEFINITION_ID_NAME));
				   $("#DL_STAGE_BACK_ID1").change();
				});
				$('.Return_Table_Modals_Pmeter').find("input[name='TXT_REQUEST_APP_SERIAL_RETURN1']").val(NoFind);
		   }
		   GeneralHideloading();
		   /// Show Modal
           $(".ModalReturnsPmeter").modal('show');
		 },
		error: function (jqXHR, textStatus, errorThrown) {  }
	});
}