ZGoPmtGwy/assets/php/class-submenu-page.php

587 lines
20 KiB
PHP

<?php
/**
* Creates the submenu page for the plugin.
*
* Provides the functionality necessary for rendering the page corresponding
* to the submenu with which this page is associated.
**/
class Submenu_Page {
/**
* This function renders the contents of the page associated with the Submenu
* that invokes the render method. In the context of this plugin, this is the
* Submenu class.
**/
public $_limit;
public $_offset;
public $_npages;
public $_page;
public $_nrows;
public $_path;
function __construct( ) {
global $wpdb;
$this->_path = $path;
$this->console_log('$_path -> '. $this->_path);
$this->_nrows = $this->getPmtsCount();
//
// Create zgo_pmtlst if does not exist
//
$sql = 'create table if not exists zgo_pmtlst (' .
'pg_ix integer ,' .
'pg_page integer not null default 0,' .
'pg_npages integer not null default 0,' .
'pg_offset integer not null default 0,' .
'pg_limit integer not null default 0,' .
'pg_dt datetime default current_timestamp, '.
'unique lstix (pg_ix) );';
$wpdb->query($sql);
if ( ! $this->pmtLstRdy() ) {
//
// Calculate first time pagination paramters
//
$this->_limit = 10;
$this->_page = 1;
$this->_offset = 0;
$this->_npages = intdiv($this->_nrows,$this->_limit);
if ( $this->_nrows > ($this->_npages*$this->_limit) ) {
$this->_npages++;
}
$sql = 'insert into zgo_pmtlst (pg_ix,pg_page,pg_npages,pg_offset,pg_limit) values (1,' .
$this->_page . ',' .
$this->_npages . ',' .
$this->_offset . ',' .
$this->_limit .')';
$wpdb->query($sql);
} else {
// Load last state
$params = $this->pmtLstParams();
$this->_page = $params->pg_page;
$this->_offset = $params->pg_offset;
$this->_limit = $params->pg_limit;
$this->_npages = $params->pg_npages;
if ( $this->_nrows > ($this->_npages*$this->_limit) ) {
//
// Calc new number of pages
//
$this->_npages = intdiv($this->_nrows,$this->_limit);
if ( $this->_nrows > ($this->_npages*$this->_limit) ) {
$this->_npages++;
}
$sql = 'update zgo_pmtlst set pg_npages=' . $this->_npages . ' where pg_ix=1';
$wpdb->query($sql);
}
}
}
public function getPmtsCount() {
global $wpdb;
$sql = "select count(*) as npmts from zgo_payments";
$result = $wpdb->get_var($sql);
$this->console_log('getPmtsCount() -> ' . $result);
return $result;
}
public function pmtLstRdy() {
global $wpdb;
$sql = 'select count(*) as nrecs from zgo_pmtlst;';
$result = $wpdb->get_var($sql);
$this->console_log("pmtLstRdy() -> " . $result);
return $result;
}
public function pmtLstParams() {
global $wpdb;
$sql = 'select * from zgo_pmtlst where pg_ix=1;';
$result = $wpdb->get_row($sql,OBJECT);
$this->console_log('Params -> ' . json_encode($result) );
$this->console_log('pg_ix -> ' . $result->pg_ix );
$this->console_log('pg_page -> ' . $result->pg_page );
$this->console_log('pg_offset -> ' . $result->pg_offset );
return $result;
}
public function console_log($data) {
$file = $this->_path . 'zgopmtgwy/assets/console.log';
file_put_contents($file, 'LST::' . $data . chr(0x0D) . chr(0x0A), FILE_TEXT | FILE_APPEND | LOCK_EX );
}
public function render() {
global $wpdb;
$currentDate = date('Y-m-d H:i:s');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//
// Navigation button pressed
//
$sql = 'select * from zgo_pmtlst where pg_ix=1;';
$result = $wpdb->get_row($sql,OBJECT);
$this->_page = $result->pg_page;
$this->console_log("_page -> " . $this->_page);
$this->_limit = $result->pg_limit;
$this->console_log("_limit -> " . $this->_limit);
$this->_npages = $result->pg_npages;
$this->console_log("_npages -> " . $this->_npages);
$this->_nrows = $this->getPmtsCount();
$this->console_log("_nrows -> " . $this->_nrows);
if ( $this->_nrows > ($this->_npages*$this->_limit) ) {
$this->console_log('_nrows > ' . ($this->_npages*$this->_limit));
$this->_npages = intdiv($this->_nrows,$this->_limit);
$this->console_log('_npages > ' . $this->_npages );
if ( $this->_nrows > ($this->_npages*$this->_limit) ) {
$this->_npages++;
$this->console_log('Page number increased to ' . $this->_npages);
}
}
if ( isset($_POST['First'])) {
$this->_page = 1;
$this->_offset = 0;
}
if ( isset($_POST['Previous'])) {
if ($this->_page > 1) {
$this->_page--;
}
$this->_offset = $this->_limit*($this->_page-1);
}
if ( isset($_POST['Next'])) {
if ($this->_page < $this->_npages ) {
$this->_page++;
}
$this->_offset = $this->_limit*($this->_page-1);
}
if ( isset($_POST['Last'])) {
$this->_page = $this->_npages;
$this->_offset = $this->_limit*($this->_page-1);
}
//
// Save new list state
//
$sql = 'update zgo_pmtlst set pg_page=' . $this->_page .
', pg_npages=' . $this->_npages .
', pg_offset=' . $this->_offset . ' where pg_ix=1;';
$this->console_log( $sql );
$wpdb->query($sql);
}
?>
<div style="margin-top: 20px;
margin-bottom: 20px;
font-family: Noto Sans;
font-size: 30px;
font-weight: 900;
text-align: center;
color: navy;">
ZGo Payment Gateway - Received Payments
</div>
<!-- now output table header -->
<div width="100%"
style="padding-right: 20px;">
<table width="100%">
<tr style="font-family: Noto Sans;
font-weight: 600;
font-size: 15px;
vertical-align: center;
height: 20px;
color: navy;">
<td width="50%" style="padding-left: 5px;">
<?php
echo 'Page ' . $this->_page . '/' . $this->_npages;
?>
</td>
<td width="50%" style="text-align: right;">
<?php echo $currentDate; ?>
</td>
</tr>
</table>
<table>
<tr style="font-family: Noto Sans;
font-weight: 600;
font-size: 15px;
text-align: center;
vertical-align: center;
height: 20px;
background-color: #ff5722;
color: white;">
<td width="10%">ZGo Order Id</td>
<td width="10%">Shop Order Id</td>
<td width="15%">Customer</td>
<td width="10%">Accepted</td>
<td width="10%">Confirmed</td>
<td width="8%">Order Total</td>
<td width="8%">Rate</td>
<td width="8%">Total ZEC</td>
<td width="5%">Paid?</td>
</tr>
<?php
$sql = "select * from zgo_payments order by pmt_accepted desc LIMIT " . $this->_offset . "," . $this->_limit;
$result = $wpdb->get_results($sql,OBJECT);
$npmts = count($result);
if ( $npmts > 0 ) {
$line = 0;
while ( $line < $npmts ) {
$row = $result[$line];
if ( fmod($line,2) ) {
print '<tr style="background-color: #ffffff;
font-weight: 600;">';
} else {
print '<tr style="background-color: #ebecf0;
font-weight: 600;">';
}
$line++;
print '<td><a href="https://dev.zgo.cash/invoice/' . $row->pmt_orderid . '" target="_blank">' . $row->pmt_orderid . "</a></td>";
print "<td>" . $row->pmt_wc_order . "</td>";
print "<td>" . $row->pmt_wc_custname . "</td>";
print '<td style="text-align:center;">'. $row->pmt_accepted . "</td>";
print '<td style="text-align:center;">'.$row->pmt_confirmed ."</td>";
print '<td style="text-align:right;">'. number_format($row->pmt_amount,2) . "</td>";
print '<td style="text-align:right;">'. number_format($row->pmt_rate,2) . "</td>";
print '<td style="text-align:right;">'. number_format($row->pmt_zec,8) . "</td>";
print '<td style="text-align:center;">'.$row->pmt_wc_paid ."</td></tr>";
}
?>
</table>
</div>
<br>
<form class="" action="" method="POST">
<table width="100%">
<tr>
<td width="10%"></td>
<td width="20%" align="center">
<button type="submit" name="First"
style="border: 2px solid #ff2b00;
border-radius: 8px;
padding-left: 10px;
padding-right: 10px;
padding-top: 5px;
padding-bottom: 5px;
font-weight: 700;
background-color: #ff5722;
color: #ffffff;">First Page</button>
</td>
<td width="20%" align="center">
<button type="submit" name="Previous"
style="border: 2px solid #ff2b00;
border-radius: 8px;
padding-left: 10px;
padding-right: 10px;
padding-top: 5px;
padding-bottom: 5px;
font-weight: 700;
background-color: #ff5722;
color: #ffffff;">Prev Page</button>
</td>
<td width="20%" align="center">
<button type="submit" name="Next"
style="border: 2px solid #ff2b00;
border-radius: 8px;
padding-left: 10px;
padding-right: 10px;
padding-top: 5px;
padding-bottom: 5px;
font-weight: 700;
background-color: #ff5722;
color: #ffffff;">Next Page</button>
</td>
<td width="20%" align="center">
<button type="submit" name="Last"
style="border: 2px solid #ff2b00;
border-radius: 8px;
padding-left: 10px;
padding-right: 10px;
padding-top: 5px;
padding-bottom: 5px;
font-weight: 700;
background-color: #ff5722;
color: #ffffff;">Last Page</button>
</td>
<td width="10%"></td>
</tr>
</table>
</form>
<?php
$this->render_stats();
$this->render_totals();
}
}
public function render_stats() {
global $wpdb;
$sql = "select ifnull(a.dTotalAmount,0) as dTotalAmount, ifnull(a.dTotalZec,0) as dTotalZec, ifnull(a.dTotalPmts,0) as dTotalPmts, ifnull(b.wTotalAmount,0) as wTotalAmount, ifnull(b.wTotalZec,0) as wTotalZec, ifnull(b.wTotalPmts,0) as wTotalPmts, ifnull(c.mTotalAmount,0) as mTotalAmount, ifnull(c.mTotalZec,0) as mTotalZec, ifnull(c.mTotalPmts,0) as mTotalPmts from (select sum(a0.pmt_amount) as dTotalAmount, sum(a0.pmt_zec) as dTotalZec, count(*) as dTotalPmts from zgo_payments a0 where STR_TO_DATE(left(a0.pmt_confirmed,10), '%Y-%m-%d') = CURRENT_DATE ) a join (select sum(aa.pmt_amount) as wTotalAmount, sum(aa.pmt_zec) as wTotalZec, count(*) as wTotalPmts from zgo_payments aa where STR_TO_DATE(aa.pmt_confirmed, '%Y-%m-%d %H:%i:%s') >= DATE_SUB(CURRENT_DATE, INTERVAL 7 DAY)) b join (select sum(bb.pmt_amount) as mTotalAmount, sum(bb.pmt_zec) as mTotalZec, count(*) as mTotalPmts from zgo_payments bb where STR_TO_DATE(bb.pmt_confirmed, '%Y-%m-%d %H:%i:%s') >= DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY)) c where 1;";
$result = $wpdb->get_row($sql,OBJECT);
?>
<div style="margin-top: 25px;
margin-bottom: 10px;
padding-right: 20px;
font-family: 'Noto Sans';
font-size: 20px;
font-weight: 900;
text-align: center;
color: navy;">
ZGo Confirmed Payments Statistics
</div>
<!-- now output table header -->
<table width="100%"
style="padding-right: 20px;">
<tr>
<td width="10%"></td>
<td width="80%">
<table width="100%" >
<tr style="font-family: Noto Sans;
font-weight: 600;
font-size: 15px;
margin: auto;
text-align: center;
vertical-align: center;
height: 20px;
background-color: #ff5722;
color: white;">
<td width="35%">Payments received</td>
<td width="35%">Description</td>
<td width="30%">Value</td>
</tr>
</table>
</td>
<td width="10%"></td>
</tr>
<td width="10%"></td>
<td width="80%" >
<table width="100%">
<tr style="font-family: Noto Sans;
font-weight: 700;
background-color: white;">
<td width="35%" style="padding-left: 10px;
border-top: 1px solid navy;
border-bottom: 1px solid navy;">Received Today</td>
<td width="35%">
<table width="100%"
style="border-top: 1px solid navy;
border-bottom: 1px solid navy;
border-collapse: collapse;">
<tr>
<td style="padding-left: 10px;">Total Amount</td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;">Average Rate</td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;">Total Zec</td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;">Number of payments</td>
</tr>
</table>
</td>
<td width="30%">
<table width="100%"
style="border-top: 1px solid navy;
border-bottom: 1px solid navy;
border-collapse: collapse;">
<tr>
<td style="padding-left: 10px;"> <?php echo number_format($result->dTotalAmount,2);?></td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;"> <?php if ($result->dTotalZec > 0.0) {
echo number_format($result->dTotalZec/$result->dTotalAmount,2);
} else { echo '0.00'; } ?></td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;"><?php echo number_format($result->dTotalZec,8);?></td>
</tr>
<tr >
<td style="border-top: 1px solid navy;
padding-left: 10px;"><?php echo $result->dTotalPmts;?></td>
</tr>
</table>
</td>
</tr>
<tr style="font-family: Noto Sans;
font-weight: 600;">
<td width="35%" style="padding-left: 10px;
border-top: 1px solid navy;
border-bottom: 1px solid navy;">Last 7 days</td>
<td width="35%">
<table width="100%"
style="border-top: 1px solid navy;
border-bottom: 1px solid navy;
border-collapse: collapse;">
<tr>
<td style="padding-left: 10px;">Total Amount</td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;">Average Rate</td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;">Total Zec</td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;">Number of payments</td>
</tr>
</table>
</td>
<td width="30%">
<table width="100%"
style="border-top: 1px solid navy;
border-bottom: 1px solid navy;
border-collapse: collapse;">
<tr>
<td style="padding-left: 10px;"> <?php echo number_format($result->wTotalAmount,2);?></td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;"> <?php if ($result->wTotalZec > 0.0) {
echo number_format($result->wTotalAmount/$result->wTotalZec,2);
} else { echo '0.00'; } ?></td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;"><?php echo number_format($result->wTotalZec,8);?></td>
</tr>
<tr >
<td style="border-top: 1px solid navy;
padding-left: 10px;"><?php echo $result->wTotalPmts;?></td>
</tr>
</table>
</td>
</tr>
<tr style="font-family: Noto Sans;
font-weight: 600;
background-color: white;">
<td width="35%" style="padding-left: 10px;
border-top: 1px solid navy;
border-bottom: 1px solid navy;">Last 30 days</td>
<td width="35%">
<table width="100%"
style="border-top: 1px solid navy;
border-bottom: 1px solid navy;
border-collapse: collapse;">
<tr>
<td style="padding-left: 10px;">Total Amount</td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;">Average Rate</td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;">Total Zec</td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;">Number of payments</td>
</tr>
</table>
</td>
<td width="30%">
<table width="100%"
style="border-top: 1px solid navy;
border-bottom: 1px solid navy;
border-collapse: collapse;">
<tr>
<td style="padding-left: 10px;"> <?php echo number_format($result->mTotalAmount,2);?></td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;"> <?php if ($result->mTotalZec> 0.0) {
echo number_format($result->mTotalAmount/$result->mTotalZec,2);
} else { echo '0.00'; } ?></td>
</tr>
<tr>
<td style="border-top: 1px solid navy;
padding-left: 10px;"><?php echo number_format($result->mTotalZec,8);?></td>
</tr>
<tr >
<td style="border-top: 1px solid navy;
padding-left: 10px;"><?php echo $result->mTotalPmts;?></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td width="10%"></td>
<tr>
</tr>
</table>
<?php
}
public function render_totals() {
global $wpdb;
$sql = 'select ifnull(sum(a.pmt_amount),0) as gTotalAmount, ' .
'ifnull(count(*),0) as gTotalPmts, ' .
'ifnull(sum(a.pmt_zec),0) as gTotalZec ' .
'from zgo_payments a ' .
'where a.pmt_wc_paid = 1;' ;
$result = $wpdb->get_row($sql,OBJECT);
if ( $result ) {
if ($result->gTotalPmts > 0) {
?>
<div style="padding-right: 20px;
font-family: 'Noto Sans';
font-size: 15px;
font-weight: 900;
text-align: center;
color: navy;">
Global Totals
</div>
<table width="100%"
style="padding-right: 20px;">
<tr>
<td width="10%"></td>
<td width="80%">
<table width="100%" >
<tr style="font-family: Noto Sans;
font-weight: 600;
font-size: 15px;
margin: auto;
text-align: center;
vertical-align: center;
height: 20px;
background-color: #ff5722;
color: white;">
<td width="12%">Payments:</td>
<td width="23%"><?php echo $result->gTotalPmts ?></td>
<td width="12%">Total Amount:</td>
<td width="23%"><?php echo number_format($result->gTotalAmount,2) ?></td>
<td width="12%">Total ZEC:</td>
<td width="23%"><?php echo number_format($result->gTotalZec,8) ?></td>
</tr>
</table>
<td width="10%"></td>
</tr>
</table>
<?php
}
}
}
}