| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Payment Left For This Month Widget |
| 4 |
* |
| 5 |
* Displays the Payment Left For This Month. |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
* @version 4.6 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace MainWP\Dashboard\Module\CostTracker; |
| 12 |
|
| 13 |
use MainWP\Dashboard\MainWP_UI; |
| 14 |
use MainWP\Dashboard\MainWP_Utility; |
| 15 |
|
| 16 |
// Exit if accessed directly. |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Class Cost_Tracker_Payment_Left_This_Month |
| 23 |
* |
| 24 |
* Displays the Payment Left For This Month. |
| 25 |
*/ |
| 26 |
class Cost_Tracker_Payment_Left_This_Month { |
| 27 |
|
| 28 |
/** |
| 29 |
* Protected static variable to hold the single instance of the class. |
| 30 |
* |
| 31 |
* @var mixed Default null |
| 32 |
*/ |
| 33 |
protected static $instance = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* Return the single instance of the class. |
| 37 |
* |
| 38 |
* @return mixed $instance The single instance of the class. |
| 39 |
*/ |
| 40 |
public static function instance() { |
| 41 |
if ( is_null( static::$instance ) ) { |
| 42 |
static::$instance = new self(); |
| 43 |
} |
| 44 |
return static::$instance; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Handle render costs widget. |
| 49 |
*/ |
| 50 |
public function render() { |
| 51 |
$this->render_widget(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Render widget. |
| 56 |
*/ |
| 57 |
public function render_widget() { |
| 58 |
?> |
| 59 |
<div class="mainwp-widget-header"> |
| 60 |
<h2 class="ui header handle-drag"> |
| 61 |
<?php esc_html_e( 'Payments Left For the Month', 'mainwp' ); ?> |
| 62 |
<div class="sub header"> |
| 63 |
<?php esc_html_e( 'See upcoming costs left for this month.', 'mainwp' ); ?> |
| 64 |
</div> |
| 65 |
</h2> |
| 66 |
</div> |
| 67 |
|
| 68 |
<div class="mainwp-widget-insights-card"> |
| 69 |
<?php |
| 70 |
/** |
| 71 |
* Action: mainwp_module_cost_tracker_widget_top |
| 72 |
* |
| 73 |
* Fires at the top of the widget. |
| 74 |
* |
| 75 |
* @since 5.0.2 |
| 76 |
*/ |
| 77 |
do_action( 'mainwp_module_cost_tracker_widget_top', 'payment-left-for-this-month' ); |
| 78 |
?> |
| 79 |
<div id="mainwp-message-zone" style="display:none;" class="ui message"></div> |
| 80 |
<?php |
| 81 |
$this->render_widget_content(); |
| 82 |
?> |
| 83 |
<?php |
| 84 |
/** |
| 85 |
* Action: mainwp_module_cost_tracker_widget_bottom |
| 86 |
* |
| 87 |
* Fires at the bottom of the widget. |
| 88 |
* |
| 89 |
* @since 5.0.2 |
| 90 |
*/ |
| 91 |
do_action( 'mainwp_module_cost_tracker_widget_bottom', 'payment-left-for-this-month' ); |
| 92 |
?> |
| 93 |
</div> |
| 94 |
<div class="mainwp-widget-footer ui four columns stackable grid"> |
| 95 |
<div class="column"> |
| 96 |
</div> |
| 97 |
<div class="column"> |
| 98 |
</div> |
| 99 |
</div> |
| 100 |
<?php |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get widget costs data. |
| 105 |
* |
| 106 |
* @param array $cost_data Cost data. |
| 107 |
*/ |
| 108 |
public static function get_costs_widgets_data( $cost_data ) { // phpcs:ignore -- NOSONAR - complex. |
| 109 |
$series_products_price = array(); |
| 110 |
$colors = array(); |
| 111 |
$series_data = array(); |
| 112 |
$product_colors = Cost_Tracker_Admin::get_product_colors(); |
| 113 |
$product_types = Cost_Tracker_Admin::get_product_types(); |
| 114 |
|
| 115 |
if ( is_array( $cost_data ) ) { |
| 116 |
$current_time = time(); |
| 117 |
$upcoming1 = strtotime( gmdate( 'Y-m-01 00:00:00', $current_time ) ); |
| 118 |
$upcoming2 = strtotime( gmdate( 'Y-m-t 23:59:59', $current_time ) ); |
| 119 |
foreach ( $cost_data as $cost ) { |
| 120 |
|
| 121 |
if ( ! isset( $series_data[ $cost->product_type ] ) ) { |
| 122 |
$series_data[ $cost->product_type ] = array( |
| 123 |
'name' => isset( $product_types[ $cost->product_type ] ) ? esc_js( $product_types[ $cost->product_type ] ) : 'N/A', |
| 124 |
); |
| 125 |
$colors[ $cost->product_type ] = isset( $product_colors[ $cost->product_type ] ) ? $product_colors[ $cost->product_type ] : ''; |
| 126 |
$series_products_price[ $cost->product_type ] = 0; |
| 127 |
} |
| 128 |
|
| 129 |
$next_rl = $cost->next_renewal; |
| 130 |
|
| 131 |
if ( $next_rl <= $upcoming1 ) { |
| 132 |
$next_rl = Cost_Tracker_Admin::get_next_renewal( $upcoming1, $cost->renewal_type, false ); |
| 133 |
} |
| 134 |
|
| 135 |
$next_price = 0; |
| 136 |
while ( $next_rl <= $upcoming2 ) { |
| 137 |
if ( $next_rl > $upcoming1 && $next_rl <= $upcoming2 ) { |
| 138 |
$next_price = $cost->price; |
| 139 |
} |
| 140 |
$series_products_price[ $cost->product_type ] += $next_price; |
| 141 |
$next_rl = Cost_Tracker_Admin::get_next_renewal( $next_rl, $cost->renewal_type, false ); |
| 142 |
$next_price = 0; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
$dec = Cost_Tracker_Utility::cost_tracker_format_price( 0, true, array( 'get_decimals' => true ) ); |
| 148 |
foreach ( $series_products_price as $product_type => $price ) { |
| 149 |
$series_data[ $product_type ]['data'][] = round( $price, $dec ); |
| 150 |
} |
| 151 |
|
| 152 |
$categories = array( gmdate( 'M' ) ); |
| 153 |
|
| 154 |
$cur_format = Cost_Tracker_Utility::cost_tracker_format_price( 0, true, array( 'get_currency_format' => true ) ); |
| 155 |
return array( |
| 156 |
'series' => array_values( $series_data ), |
| 157 |
'categories' => array_values( $categories ), |
| 158 |
'colors' => array_values( $colors ), |
| 159 |
'currency_format' => $cur_format['format'], |
| 160 |
'decimals' => $cur_format['decimals'], |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Method render_widget_content() |
| 166 |
*/ |
| 167 |
public function render_widget_content() { |
| 168 |
$costs_data = Cost_Tracker_DB::get_instance()->get_summary_data( array( 'sum_data' => 'all' ) ); |
| 169 |
$chart_data = static::get_costs_widgets_data( $costs_data ); |
| 170 |
?> |
| 171 |
<?php if ( empty( $costs_data ) ) : ?> |
| 172 |
<?php MainWP_UI::render_empty_element_placeholder( __( 'No Upcoming Payment', 'mainwp' ), '<a href="admin.php?page=CostTrackerAdd">' . __( 'Add your costs to see remaining payments for this month.', 'mainwp' ) . '</a>', '<em data-emoji=":bar_chart:" class="medium"></em>' ); ?> |
| 173 |
<?php else : ?> |
| 174 |
<div id="mainwp-module-cost-tracker-payment-left-for-this-month-wrapper"></div> |
| 175 |
<script type="text/javascript"> |
| 176 |
jQuery( document ).ready( function() { |
| 177 |
|
| 178 |
let cost_chart_colors = <?php echo wp_json_encode($chart_data['colors'], true ); //phpcs:ignore -- ok. ?>; |
| 179 |
let cost_chart_currency_format = '<?php echo esc_js($chart_data['currency_format']); //phpcs:ignore -- ok. ?>'; |
| 180 |
|
| 181 |
let options = { |
| 182 |
series: <?php echo wp_json_encode($chart_data['series'], true ); //phpcs:ignore -- ok. ?>, |
| 183 |
chart: { |
| 184 |
type: 'bar', |
| 185 |
height: 350, |
| 186 |
stacked: true, |
| 187 |
}, |
| 188 |
plotOptions: { |
| 189 |
bar: { |
| 190 |
horizontal: true, |
| 191 |
dataLabels: { |
| 192 |
total: { |
| 193 |
enabled: true, |
| 194 |
offsetX: 0, |
| 195 |
style: { |
| 196 |
fontSize: '13px', |
| 197 |
fontWeight: 900 |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
}, |
| 202 |
}, |
| 203 |
xaxis: { |
| 204 |
categories: <?php echo wp_json_encode($chart_data['categories'], true ); //phpcs:ignore -- ok. ?>, |
| 205 |
labels: { |
| 206 |
formatter: function (value) { |
| 207 |
return isNaN(value) ? value : __(cost_chart_currency_format, value ); // to fix month name format. |
| 208 |
}, |
| 209 |
style: { |
| 210 |
colors: '#999999', |
| 211 |
}, |
| 212 |
}, |
| 213 |
}, |
| 214 |
yaxis:{ |
| 215 |
type: 'string', |
| 216 |
labels: { |
| 217 |
formatter: function (value) { |
| 218 |
return isNaN(value) ? value : __(cost_chart_currency_format, value ); // to fix month name format. |
| 219 |
}, |
| 220 |
style: { |
| 221 |
colors: '#999999', |
| 222 |
}, |
| 223 |
}, |
| 224 |
}, |
| 225 |
fill: { |
| 226 |
opacity: 1 |
| 227 |
}, |
| 228 |
legend: { |
| 229 |
show: false |
| 230 |
}, |
| 231 |
tooltip: { |
| 232 |
theme: 'dark' |
| 233 |
}, |
| 234 |
colors: [ |
| 235 |
function ( { value, seriesIndex, dataPointIndex, w } ) { |
| 236 |
if (cost_chart_colors[seriesIndex] !== undefined) { |
| 237 |
return cost_chart_colors[seriesIndex]; |
| 238 |
} else { |
| 239 |
return "#5ec130"; |
| 240 |
} |
| 241 |
} |
| 242 |
], |
| 243 |
}; |
| 244 |
|
| 245 |
let cost_chart = new ApexCharts(document.querySelector("#mainwp-module-cost-tracker-payment-left-for-this-month-wrapper"), options); |
| 246 |
setTimeout(() => { |
| 247 |
cost_chart.render(); |
| 248 |
}, 1000); |
| 249 |
} ); |
| 250 |
</script> |
| 251 |
<?php endif; ?> |
| 252 |
<?php |
| 253 |
} |
| 254 |
} |
| 255 |
|