From defddac19df6731433bff27fe67fd2975a44c033 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 20 Mar 2018 15:54:10 -0400 Subject: [PATCH] Start for create invoice scripting. This will be for the ajax cron task. --- models/admin/ajax/createNewInvoices.php | 144 +++++++++++++++++++++ models/admin/management/createInvoices.php | 2 +- setup/validActions.php | 9 +- 3 files changed, 150 insertions(+), 5 deletions(-) create mode 100644 models/admin/ajax/createNewInvoices.php diff --git a/models/admin/ajax/createNewInvoices.php b/models/admin/ajax/createNewInvoices.php new file mode 100644 index 0000000..7edd88d --- /dev/null +++ b/models/admin/ajax/createNewInvoices.php @@ -0,0 +1,144 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/notifications.php'; +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/billingSupport.php'; +// require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/data/dataInvoiceTypes.php'; +// require_once GLM_MEMBERS_PLUGIN_CLASS_PATH . '/data/dataMemberTypes.php'; + +/** + * Steve Note + * + * You can get to this using the following URL. + * + * + {host}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=runQueue + * + * You should be able to do this as POST or GET and should be able to add and read additional parameters. + * I added a "mystuff" parameter to the URL above and it does output from the code in the + * modelAction() function below. + * + * To add another model under models/admin/ajax all you need to do is create it and add it to the + * setup/validActions.php file. + * + */ + +/** + * This class handles the work of creating new invoices based on. + * 1) Member Type of member matching a paid invoiceType + * 2) Member renewal date past + * 3) Member has Billing Account + * 4) Member has no active Invoice + * 5) Renewal date is within the next 30 Days + * + */ +class GlmMembersAdmin_ajax_createNewInvoices +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + public function __construct ($wpdb, $config) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + } + + public function modelAction( $actionData = false ) { + // Get list of member billing accounts based on memberTypes that match invoiceTypes + // InvoiceTypes + $invoiceTypes = $this->getInvoiceTypes(); + + // MemberTypes + $memberTypes = $this->getMemberTypes(); + + // echo '
$invoiceTypes: ' . print_r( $invoiceTypes, true ) . '
'; + + // echo '
$memberTypes: ' . print_r( $memberTypes, true ) . '
'; + + $accounts = $this->getListOfAccountsPayable(); + + echo '
$accounts: ' . print_r( $accounts, true ) . '
'; + + wp_die(); + } + + public function getInvoiceTypes() + { + return $this->wpdb->get_results( + "SELECT * + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoice_types + ORDER BY name", + ARRAY_A + ); + } + + public function getMemberTypes() + { + return $this->wpdb->get_results( + "SELECT IT.id,MT.id as member_type,IT.name,IT.amount,IT.recurrence + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_type MT + LEFT OUTER JOIN " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoice_types IT + ON (MT.id = IT.member_type) + WHERE IT.amount > 0 + AND IT.recurring = true + AND IT.recurrence = 20", + ARRAY_A + ); + } + + public function getListOfAccountsPayable() + { + $types = $this->getMemberTypes(); + $memberTypes = array(); + foreach ( $types as $type ) { + $memberTypes[] = $type['member_type']; + } + return $this->wpdb->get_results( + "SELECT * + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts + WHERE ref_dest IN ( + SELECT id + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members + WHERE member_type IN ( " . implode( ',', $memberTypes ) . " ) + ) + AND renewal_date >= '2017-01-01' + AND renewal_date <= '2018-04-19' + AND id NOT IN ( + SELECT distinct account + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices + )", + ARRAY_A + ); + } + +} diff --git a/models/admin/management/createInvoices.php b/models/admin/management/createInvoices.php index 22cad8d..f79e00e 100644 --- a/models/admin/management/createInvoices.php +++ b/models/admin/management/createInvoices.php @@ -150,7 +150,7 @@ foreach ( $members as $member ) { 'amount' => $invoiceTypes[$invoiceTypeId]['amount'], 'quantity' => 1, 'total' => $invoiceTypes[$invoiceTypeId]['amount'], - 'created' => date('Y-m-d'), + 'created' => date( 'Y-m-d' ), 'first_due_date' => date( 'Y-m-d', strtotime( $next_anniversary_date) ), 'next_due_date' => date( 'Y-m-d', strtotime( $next_anniversary_date) ), 'recurring' => $invoiceTypes[$invoiceTypeId]['recurring']['value'], diff --git a/setup/validActions.php b/setup/validActions.php index c83fa54..79eaaa4 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -60,10 +60,11 @@ $glmMembersBillingAddOnValidActions = array( 'adminActions' => array( 'ajax' => array( - 'setupQueue' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, - 'runQueue' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, - 'account' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, - 'invoiceTypes' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + 'setupQueue' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + 'runQueue' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + 'account' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + 'invoiceTypes' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + 'createNewInvoices' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, ), 'management' => array( 'billing' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, -- 2.17.1