<?php

//////////////////////////////////////////////////////////////
//===========================================================
// invoices_theme.php
//===========================================================
// SOFTACULOUS VIRTUALIZOR
// Version : 1.0
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Alons
// Date:       8th Mar 2010
// Time:       23:00 hrs
// Site:       https://www.virtualizor.com/ (SOFTACULOUS VIRTUALIZOR)
// ----------------------------------------------------------
// Please Read the Terms of use at https://www.virtualizor.com
// ----------------------------------------------------------
//===========================================================
// (c)Softaculous Ltd.
//===========================================================
//////////////////////////////////////////////////////////////

if(!defined('VIRTUALIZOR')){

	die('Hacking Attempt');

}

function invoices_theme(){	

global $theme, $globals, $user, $l, $error, $done, $invoices;

softheader($l['<title>']);
										
$buttons = array(
	0 => array(
		'name' => __('Create Invoice'),
		'act'  => 'addinvoice'
	)
);
	
echo page_header(__('Invoices'), 'fas fa-file-invoice', '', 1, 0, $buttons);

$breadcrummbs = array('dashboard', 'lm_billing', 'lms_invoices');

echo get_breadcrumb($breadcrummbs);
	
error_handle($error);

if(empty($globals['inhouse_billing'])){
	show_alert(__('The Billing setting is disabled. Please enable that first from the <a href="index.php?act=billing">Billing Settings</a>.'));
	echo '<div class="mx-3 mb-3 alert alert-danger">'.__('The Billing setting is disabled. Please enable that first from the <a href="index.php?act=billing">Billing Settings</a>.').'</div>';
}

echo '<script language="javascript" type="text/javascript"><!-- // --><![CDATA[
var from_moveips = 0;
function show_confirm(ipid, ele){
	
	ipid = ipid || 0;
	ele = ele || 1;
	ipsids = new Array();
	
	selopts = $("#selected_do_options_"+ele+" option");
	
	selectvals = new Array();

	for(i=0; i<selopts.length; i++){
		val = selopts.eq(i).val();
		if(val != 0){
			selectvals.push(val);
		}
	}	
	
	confirmmsg = ["'.__J('Are you sure, you want to delete the selected item(s) ?').'"];
	donemsg = ["'.__J('The Invoice(s) were deleted successfully. Reloading the Page !').'"];
	selectIndx = selectvals.indexOf($_("selected_do_options_"+ele).value);
	if(ipid < 1){

		if(selectIndx == -1){
			showalert("'.__J('No action selected !').'", "1");
			return false;
		}
	
		$(".ios:checked").each(function() {
			ipsids.push($(this).val());
		});
	
	}else{
		ipsids.push(ipid);
		selectIndx = 0;
	}
	
	if(ipsids.length < 1){
		showalert("'.__J('Nothing selected !').'", "1");
		return false;
	}
	
	var finalips = new Object();
	
	showalert(confirmmsg[selectIndx] ,"3" ,function(confirm){
		if(confirm != true){
			return false;
		}else{
			finalips["do"] = selectvals[selectIndx];
			finalips["ips"] = ipsids.join(",");

			// API backward compatibility
			if(selectvals[selectIndx] == "del"){
				finalips["delete"] = finalips["ips"];
			}

			$("#progress_bar").show();

			$.ajax({
				type: "POST",
				url: "'.$globals['index'].'act=invoices&api=json",
				data: finalips,
				dataType : "json",
				success:function(response){
					$("#progress_bar").hide();
					if("done" in response){
						setTimeout(function(){showalert(donemsg[selectIndx]);
						$("#alert-modal").on("hidden.bs.modal", function (e) {
						location.reload(true);
						});} ,200);
					}
					if("error" in response){
						setTimeout(function(){showalert(response["error"] ,"1");} ,200);
						//location.reload(true);
					}
				},
				error: function(data) {
					$("#progress_bar").hide();
					if(check_logout_session(data)){
						setTimeout(function(){showalert("'.__J('It seems that your session is expired, please login and try again').'" ,"1");} ,200);
						return false;
					}
					setTimeout(function(){showalert("'.__J('It seems that there is some internet connectivity issue, please try to refresh the page and try again').'" ,"1");} ,200);
					return false;
				}
			});
		}
	});
	
	
	
	
	return false;
};

// ]]></script>
<div class="card p-2 mx-3">
	<div id="showsearch" class="mb-3" style="display:'.(optREQ('search') || (!empty($invoices) && !empty($globals['showsearch'])) ? "" : "none").';">
		<form accept-charset="'.$globals['charset'].'" name="invoices" method="GET" action="" class="form-horizontal">
			<input type="hidden" name="act" value="invoices">
					
			<div class="form-group_head">
				<div class="row mx-auto w-100">
					<div class="col-sm-4"><label>'.__('Invoice ID').'</label><input type="text" class="form-control" name="invoid" id="sby_invoid" size="30" value="'.REQval('invoid', '').'"/></div>
					<div class="col-sm-4"><label>'.__('User ID').'</label><input type="text" class="form-control" name="uid" id="sby_uid" size="30" value="'.REQval('uid','').'"/></div>
					<div class="col-sm-4"><label>'.__('User Email').'</label><input type="text" class="form-control" name="email" id="sby_email" size="30" value="'.REQval('email','').'"/></div>
				</div>
					<div class="row mx-auto w-100">
					<div class="col-sm-4"><label>'.__('Item').'</label><input type="text" class="form-control" name="item" id="sby_item" size="30" value="'.REQval('item','').'"/></div>
					<div class="col-sm-4"><label>'.__('Pay Date').'</label><input type="text" class="form-control" name="paydate" id="sby_paydate" size="30" value="'.REQval('paydate','').'"/></div>
					<div class="col-sm-4"><label>'.__('Invoice Date').'</label><input type="text" class="form-control" name="invodate" id="sby_invodate" size="30" value="'.REQval('invodate','').'"/></div>
					<div class="col-sm-4"><label>'.__('Due Date').'</label><input type="text" class="form-control" name="duedate" id="sby_duedate" size="30" value="'.REQval('duedate','').'"/></div>
				</div>
				<div class="row mx-auto w-100 my-2">
					<div class="col-sm-12 text-center"><button type="submit" name="search" class="go_btn mt-2" value="Search">'.__('Search').'</button></div>
				</div>
			</div>
		</form>
	</div>';

if(!empty($done)){
	show_alert(__('The Invoice(s) were deleted successfully. Reloading the Page !'));
}

if(empty($invoices)){
	optREQ('search') ? show_alert(__('No results were found for your search query'), "2") : show_alert(__('There are no Invoices. <a href="index.php?act=addinvoice">Create an Invoice now</a>'), "2");
	echo '<div class="notice"><b>'.__('Note').' : </b> &nbsp; '.(optREQ('search') ? __('No results were found for your search query') : __('There are no Invoices. <a href="index.php?act=addinvoice">Create an Invoice now</a>')).'</div>';
	
}else{
echo'<div class="row mx-auto w-100 mb-3">
		<div class="col-sm-8 col-lg-8 col-md-6 d-flex align-items-center p-0">';
			page_links($globals['num_res'], $globals['cur_page'], $globals['reslen']);
		echo get_num_records($globals['num_res']).'
		</div>
		<div class="col-sm-4 col-lg-4 col-md-6 text-right d-flex align-items-center">
			<div class="bottom-menu float-right w-100">
				<div class="row my-auto">
					<div class="p-0">
						<label>'.__('With Selected').'</label>
					</div>
					<div class="col p-0">
						<select name="selected_do_options" id="selected_do_options_1" class="custom-select">
							<option value="0">---</option>
							<option value="del">'.__('Delete').'</option>
						</select>
					</div>
					<div class="pl-2">
						<input type="submit" class="go_btn" value="'.__('GO').'" onclick="show_confirm(0 ,1); return false;" />
					</div>
				</div>
			</div>
		</div>
	</div>
	<form name="invoices" id="invoices" method="post" action="" accept-charset="'.$globals['charset'].'" class="form-horizontal">
		<div class="w-100 mx-auto sticky">
			<table class="table table-hover tablesorter common-table">
				<tr class="table_heading">
					<th align="center" class="z-index-1 min_width">
						'.get_checkbox('select_all', '', 'select_all', array('chk_it' => 0), '', 'select_all').'
					</th>
					<th align="center" class="min_width">'.__('ID').'</th>
					<th align="center">'.__('User').'</th>
					<th align="center">'.__('Date').'</th>
					<th align="center">'.__('Due Date').'</th>
					<th align="center">'.__('Pay Date').'</th>
					<th align="center">'.__('Item').'</th>
					<th align="center">'.__('Net Amt').'</th>
					<th class="text-right pr-3" align="right" colspan="2">'.__('Manage').'</th>
				</tr>';
				
			foreach($invoices as $k => $v){
				
				echo '<tr>
					<td align="center" width="24" >
						'.get_checkbox('invoids[]', '', 'invoid_'.$k, array('chk_it' => 0), $k).'
					</td>
					<td align="left">'.$k.'</td>
					<td>'.$v['email'].'</td>
					<td>'.datetime($v['invodate']).'</td>
					<td>'.datetime($v['duedate']).'</td>
					<td>'.datetime($v['paydate']).'</td>
					<td>'.$v['item'].'</td>
					<td align="left">'.$v['net'].'</td>
					<td align="center" width="24" >
						<div class="badge_container">
							<a class="badge badge-manage" href="'.$globals['ind'].'act=editinvoice&invoid='.$k.'" title="'.__('Edit Invoice').'"><i class="fas fa-pencil-alt"></i></a>
							<a class="badge badge-trash" href="javascript:void(0);" onclick="return show_confirm('.$k.');"  title="'.__('Delete Invoice').'"><i class="fas fa-trash"></i></a>
						</div>
					</td>
				</tr>';
			}
			echo 
			'</table>
		</div>
		<div class="row mx-auto w-100 mt-5">
			<div class="col-sm-8 col-lg-8 col-md-6 mb-3 p-0">';
				page_links($globals['num_res'], $globals['cur_page'], $globals['reslen']);
			echo'
			</div>
			<div class="col-sm-4 col-lg-4 col-md-6 text-right mb-3">
				<div class="bottom-menu float-right w-100">
					<div class="row my-auto">
						<div class="p-0">
							<label>'.__('With Selected').'</label>
						</div>
						<div class="col p-0">
							<select name="selected_do_options" id="multi_options" class="custom-select">
								<option value="0">---</option>
								<option value="del">'.__('Delete').'</option>
							</select>
						</div>
						<div class="pl-2">
							<input type="submit" class="go_btn" value="'.__('GO').'" onclick="show_confirm(0 ,2); return false;" />
						</div>
					</div>
				</div>
			</div>
		</div>
	</form>';
}
echo '
	<div id="progress_bar" style="display:none;">
		<br />
		<center>
			<font id="progress_txt" size="4">'.__('Please wait, Action in Progress.').'</font>
		</center>
		<div class="my-2" align="center">
			<div class="progress w-50">
				<div class="progress-bar bg-primary progress-bar-striped progress-bar-animated w-100">
				</div>
			</div>
		</div>
		<center>
			'.__('You will be notified once the Action is Completed.').'
		</center>
	</div>
	<div class="text-center my-4">
		<a href="index.php?act=addinvoice" class="go_btn">'.__('Create Invoice').'</a>
	</div>
	<br>
</div>
';

softfooter();

}

