<?php

    if ((isset($_GET["action"])) && ($_GET["action"] == "paid")) {
	require_once('whm.class.php');
	require_once("config/mail/class.phpmailer.php");
	
	$registerID = base64_decode($_GET['id']);
	$idr = $_GET['request'];
	
	$crud = new crud('invoices'); // Table
    $crud->atualizar("status='confirmed'", "id='$registerID'");
    
    $crud = new crud('requests'); // Table
	$crud->atualizar("status='S'", "id='$idr'");
    
    $query = mysql_query("SELECT * FROM requests_tbl WHERE request = '$idr'");
 	while($result = mysql_fetch_array($query)){ 
	$idres = $result['id'];
	$crud = new crud('accounts'); // Table
	$crud->atualizar("status='S'", "request='$idres'");
	if($result['product'] > '0') {
	$product = $result['product'];
	$tbprds = mysql_query("SELECT * FROM products WHERE id = '$product'");
	$tbproduct = mysql_fetch_array($tbprds);
	
	$tbaccs = mysql_query("SELECT * FROM accounts WHERE request = '$idres'");
	$acc = mysql_fetch_array($tbaccs);
	
	$idsrvc = $tbproduct['server'];
			$dbcs = mysql_query("SELECT * FROM servers WHERE id = '$idsrvc'");
			$srv = mysql_fetch_array($dbcs);
			$whmpwd = $srv['pwd'];
			$whmuser = $srv['user'];
			$whmdomain = $srv['domain'];
			$acctUser = $acc['acctuser'];
			
			$WHM = new WHM( false , $whmdomain, $whmuser, $whmpwd);
			
			$infowhm = $WHM->unsuspend_account($acctUser); 
			if($infowhm) { 
			}
	
	
    }
    }
	
	$invcb = mysql_query("SELECT * FROM invoices WHERE id = '$registerID'");
	$tbinvoice = mysql_fetch_array($invcb);
	$idtbc = $tbinvoice['customer']; 
	$totalinvoice = number_format($tbinvoice['total'],2,'.',''); 
	$paymentinvoive = $tbinvoice['payment']; 
	
    $customs = mysql_query("SELECT * FROM customers WHERE id = '$idtbc'");
	$tbcustomer = mysql_fetch_array($customs);
	
	$emailcustomer = $tbcustomer['email'];
	$namecustomer = $tbcustomer['name'];
	$passcustomer = base64_decode($tbcustomer['salt']);
    
			$query_tpl = mysql_query("SELECT * FROM email_template WHERE id = '12'");
			$template_mail = mysql_fetch_array($query_tpl);
			
			$mail = new PHPMailer();
			$mail->IsSMTP();		
			$mail->SMTPDebug = 0;		
			$mail->SMTPAuth = true;		
			$mail->IsHTML(true);
			$mail->Host = $config['smtp_host'];	
			$mail->Port = $config['smtp_port'];		
			$mail->Username = $config['smtp_user'];	
			$mail->Password = $config['smtp_pass'];	

			$mail->From = $template_mail['from_mail'];  
			$mail->FromName = $template_mail['name_from']; 

			$mail->AddAddress($emailcustomer);
			$mail->IsHTML(true); 
			$mail->CharSet = 'iso-8859-1'; 
			$mail->Subject  = $template_mail['subject']; 
			
			$corpoDoEmail = $template_mail['msg']; 
			$signature = $config['signature'];
			$logo = $config['logo'];
			$company = $config['company'];
			
			// MAIL Variables
			$corpoDoEmail = str_replace( '%EMAIL%', $emailcustomer, $corpoDoEmail );
			$corpoDoEmail = str_replace( '%NOME%', $namecustomer, $corpoDoEmail );
			$corpoDoEmail = str_replace( '%SENHA%', $passcustomer, $corpoDoEmail );
			$corpoDoEmail = str_replace( '%TOTAL%', $totalinvoice, $corpoDoEmail );
			$corpoDoEmail = str_replace( '%FORMA_PAGTO%', $paymentinvoive, $corpoDoEmail );
			$corpoDoEmail = str_replace( '%SIGNATURE%', $signature, $corpoDoEmail );
			$corpoDoEmail = str_replace( '%LOGO%', $logo, $corpoDoEmail );
			$corpoDoEmail = str_replace( '%COMPANY%', $company, $corpoDoEmail );
			
			
			// END Variables
			$mail->Body = $corpoDoEmail;
			$SendOK = $mail->Send();
			if ($SendOK) {
			}
	
	header("Location: index.php?mod=Invoices&y=1");
	}
	
	
	if ((isset($_GET["action"])) && ($_GET["action"] == "cancel")) {
	require_once('whm.class.php');
	
	$registerID = base64_decode($_GET['id']);
	$idr = $_GET['request'];
	
	$crud = new crud('invoices'); // Table
    $crud->atualizar("status='canceled'", "id='$registerID'");
    
    $crud = new crud('requests'); // Table
	$crud->atualizar("status='N'", "id='$idr'");
    
    $query = mysql_query("SELECT * FROM requests_tbl WHERE request = '$idr'");
 	while($result = mysql_fetch_array($query)){ 
	$idres = $result['id'];
	$crud = new crud('accounts'); // Table
	$crud->atualizar("status='N'", "request='$idres'");
	if($result['product'] > '0') {
	$product = $result['product'];
	$tbprds = mysql_query("SELECT * FROM products WHERE id = '$product'");
	$tbproduct = mysql_fetch_array($tbprds);
	
	$tbaccs = mysql_query("SELECT * FROM accounts WHERE request = '$idres'");
	$acc = mysql_fetch_array($tbaccs);
	
	$idsrvc = $tbproduct['server'];
			$dbcs = mysql_query("SELECT * FROM servers WHERE id = '$idsrvc'");
			$srv = mysql_fetch_array($dbcs);
			$whmpwd = $srv['pwd'];
			$whmuser = $srv['user'];
			$whmdomain = $srv['domain'];
			$acctUser = $acc['acctuser'];
			$reson = 'Admin';
			
			$WHM = new WHM( false , $whmdomain, $whmuser, $whmpwd);
			
			$infowhm = $WHM->suspend_account($acctUser,$reson); 
			if($infowhm) { 
			}
	
	
    }
	
	
    }
	
	header("Location: index.php?mod=Invoices&y=2");
	}
	
	
	
?>


    <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            <?php echo $lang['TITLE_LIST_INVOICES']; ?>
          </h1>
          
        </section>

       
       <section class="content">
          <div class="row">
            <div class="col-xs-12">
				
				<!-- ALERTS -->
				<?php if($_GET['y'] == '1') { ?>
				<div class="callout callout-success">
                    <h4><?php echo $lang['ALERT_INVOICE_DUE']; ?></h4>
                    <p><?php echo $lang['ALERT_INVOICE_DUE_TXT']; ?></p>
                  </div>
				<?php } ?>
				<?php if($_GET['y'] == '2') { ?>
				<div class="callout callout-danger">
                    <h4><?php echo $lang['ALERT_INVOICE_CANCEL']; ?></h4>
                    <p><?php echo $lang['ALERT_INVOICE_CANCEL_TXT']; ?></p>
                  </div>
				<?php } ?>
				
				
              <div class="box">
				  
                <div class="box-header">Invoices
                
                </div><!-- /.box-header -->
                <div class="box-body">
                  <table id="tabela" class="table table-striped table-hover">
                    <thead>
                      <tr>
                        <th><?php echo $lang['INVOICE']; ?></th>
                        <th><?php echo $lang['CUSTOMER']; ?></th>
                        <th><?php echo $lang['TOTAL']; ?></th>
                        <th><?php echo $lang['PAYMENT']; ?></th>
                        <th><?php echo $lang['DATE']; ?></th>
                        <th><?php echo $lang['PAYMENT_DATE']; ?></th>
                        <th><?php echo $lang['ACTIONS']; ?></th>
                      </tr>
                    </thead>
                    <tbody>
					<?php
 		  			$query = mysql_query("SELECT * FROM invoices WHERE status = 'open' order by id DESC");
 		  			while($result = mysql_fetch_array($query)){ 
						$idc = $result['customer'];
						$querycs = mysql_query("SELECT * FROM customers WHERE id = '$idc'");
 		  			    $customer = mysql_fetch_array($querycs);
					?>
		            <tr>
					
		            <td>#<?php echo $result['invoice']; ?></td>
		            <td><?php echo $customer['name']; ?></td>
		            
		            <td><?php echo number_format($result['total'],2,'.',''); ?></td>
					<td><?php echo $result['payment']; ?></td>
					<td><?php echo data_en($result['date']); ?></td>
					<td><?php echo data_en($result['payment_date']); ?></td>
					
					<td>
						<a href="javascript:void(0);" onclick="javascript: if (confirm('<?php echo $lang['ALERT_BTN_PAID']; ?>')) { window.location.href='?mod=Invoices&id=<?php echo base64_encode($result['id']); ?>&request=<?php echo $result['request']; ?>&action=paid' } else { void('') };" class="btn btn-success btn-sm"><i class="fa fa-money"></i> Paid</a>
						<a href="javascript:void(0);" onclick="javascript: if (confirm('<?php echo $lang['ALERT_BTN_CANCEL']; ?>')) { window.location.href='?mod=Invoices&id=<?php echo base64_encode($result['id']); ?>&request=<?php echo $result['request']; ?>&action=cancel' } else { void('') };" class="btn btn-danger btn-sm"><i class="fa fa-remove"></i> Cancel</a>
					
					</td>
					
		           
                    
		            </tr>
					<?php } ?>
					
                    </tbody>
                    <tfoot>
                      <tr>
                        <th><?php echo $lang['INVOICE']; ?></th>
                        <th><?php echo $lang['CUSTOMER']; ?></th>
                        <th><?php echo $lang['TOTAL']; ?></th>
                        <th><?php echo $lang['PAYMENT']; ?></th>
                        <th><?php echo $lang['DATE']; ?></th>
                        <th><?php echo $lang['PAYMENT_DATE']; ?></th>
                        <th><?php echo $lang['ACTIONS']; ?></th>
                      </tr>
                    </tfoot>
                  </table>
                </div><!-- /.box-body -->
              </div><!-- /.box -->
       
           </div><!-- /.col -->
          </div><!-- /.row -->
        </section><!-- /.content -->
       
       
        
        
      </div><!-- /.content-wrapper -->
   
   
   
   
