From b63a7a5dd1f92c81a6175595e16f879a75c8e9a8 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Tue, 4 Dec 2018 16:33:54 -0500 Subject: [PATCH] Add Labels for printing (WIP) and new contact name field. Adding contact name to billing account. WIP for the labels. --- classes/billingSupport.php | 44 +++-- classes/data/dataSettings.php | 7 + index.php | 2 +- models/admin/ajax/createPDFInvoice.php | 151 ++++++++++++----- models/admin/ajax/createPDFLabels.php | 152 ++++++++++++++++++ ...0.0.29.sql => create_database_V0.0.30.sql} | 2 + setup/databaseScripts/dbVersions.php | 1 + .../update_database_V0.0.30.sql | 13 ++ setup/validActions.php | 1 + views/admin/settings/billing.html | 9 ++ 10 files changed, 327 insertions(+), 55 deletions(-) create mode 100644 models/admin/ajax/createPDFLabels.php rename setup/databaseScripts/{create_database_V0.0.29.sql => create_database_V0.0.30.sql} (98%) create mode 100644 setup/databaseScripts/update_database_V0.0.30.sql diff --git a/classes/billingSupport.php b/classes/billingSupport.php index 920d055..0853ffc 100644 --- a/classes/billingSupport.php +++ b/classes/billingSupport.php @@ -692,10 +692,7 @@ class GlmBillingSupport /** * viewInvoice * - * Need to get everything here for creating the invoice. - * Need billing settings. - * Invoice data. - * All line items for that invoice. + * Return the HTML for the invoice * * @param mixed $invoice_id * @@ -704,25 +701,42 @@ class GlmBillingSupport */ public function viewInvoice( $invoice_id ) { - $invoice = $this->getInvoiceById( $invoice_id ); - $line_items = $this->getLineItemsForInvoice( $invoice_id ); - $account = $this->getAccountById( $invoice['account'] ); - $payments = $this->getInvoicePaymentsByInvoiceId( $invoice_id ); + $fullInvoice = $this->getFullInvoiceData( $invoice_id ); $templateData = array( 'settings' => $this->config['settings'], - 'invoice' => $invoice, - 'line_items' => $line_items, - 'account' => $account, - 'payments' => $payments, + 'invoice' => $fullInvoice['invoice'], + 'line_items' => $fullInvoice['line_items'], + 'account' => $fullInvoice['account'], + 'payments' => $fullInvoice['payments'], ); - // echo '
$templateData: ' . print_r( $templateData, true ) . '
'; - $invoiceHtml = $this->generateInvoiceHtml( $templateData, 'admin/billing/invoiceStore.html' ); return $invoiceHtml; } - + /** + * getFullInvoiceData + * + * Need to get everything here for creating the invoice. + * Need billing settings. + * Invoice data. + * All line items for that invoice. + * + * @param mixed $invoice_id + * + * @access public + * @return void + */ + public function getFullInvoiceData( $invoice_id ) + { + $invoice = $this->getInvoiceById( $invoice_id ); + return array( + 'invoice' => $invoice, + 'line_items' => $this->getLineItemsForInvoice( $invoice_id ), + 'account' => $this->getAccountById( $invoice['account'] ), + 'payments' => $this->getInvoicePaymentsByInvoiceId( $invoice_id ), + ); + } /** * getLineItemsForInvoice * diff --git a/classes/data/dataSettings.php b/classes/data/dataSettings.php index 7022bd1..8ff272f 100644 --- a/classes/data/dataSettings.php +++ b/classes/data/dataSettings.php @@ -307,6 +307,13 @@ class GlmDataBillingSettings extends GlmDataAbstract 'use' => 'a', ), + // Use billing_contact_name instead of fname lname + 'billing_contact_name_enabled' => array( + 'field' => 'billing_contact_name_enabled', + 'type' => 'checkbox', + 'use' => 'a', + ), + ); } diff --git a/index.php b/index.php index 1d49158..92114a1 100644 --- a/index.php +++ b/index.php @@ -38,7 +38,7 @@ * version from this plugin. */ define('GLM_MEMBERS_BILLING_PLUGIN_VERSION', '1.0.16'); -define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.29'); +define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.30'); // This is the minimum version of the GLM Members DB plugin require for this plugin. define('GLM_MEMBERS_BILLING_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '2.8.0'); diff --git a/models/admin/ajax/createPDFInvoice.php b/models/admin/ajax/createPDFInvoice.php index 77b4f91..7bb20b0 100644 --- a/models/admin/ajax/createPDFInvoice.php +++ b/models/admin/ajax/createPDFInvoice.php @@ -73,9 +73,6 @@ class GlmMembersAdmin_ajax_createPDFInvoice public function modelAction( $actionData = false ) { - trigger_error( print_r( $_REQUEST, true ), E_USER_NOTICE ); - trigger_error( print_r( $actionData, true ), E_USER_NOTICE ); - if ( isset( $_REQUEST['id'] ) ) { $id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ); } @@ -83,34 +80,36 @@ class GlmMembersAdmin_ajax_createPDFInvoice if ( $id ) { // Create Billing Support Object. $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); + $fullInvoice = $BillingSupport->getFullInvoiceData( $id ); + // Get the Invoice. - $invoice = $BillingSupport->getInvoiceById( $id ); + $invoice = $fullInvoice['invoice']; if ( !$invoice ) { echo 'failed to get invoice'; return false; } // Get the account information. - $account = $BillingSupport->getAccountById( $invoice['account'] ); + $account = $fullInvoice['account']; // PDF settings - $this->pdf_top_y = 725; - $this->pdf_width = 570; - $this->line_height = 10; - $this->pdf_lft_col_x = 20; - $this->pdf_rgt_col_x = 380; - $this->pdf_bottom_y = 130; - $this->pdf_font_size = 12; - $this->pdf_header_font_1 = 24; - $this->pdf_header_font_2 = 20; - $this->color_white = array( 1, 1, 1 ); - $this->color_red = array( 1, 0, 0 ); - $this->color_black = array( 0, 0, 0 ); + $this->pdf_top_y = 725; + $this->pdf_width = 570; + $this->line_height = 10; + $this->pdf_lft_col_x = 20; + $this->pdf_rgt_col_x = 380; + $this->pdf_bottom_y = 130; + $this->pdf_font_size = 12; + $this->pdf_header_font_1 = 24; + $this->pdf_header_font_2 = 20; + $this->color_white = array( 1, 1, 1 ); + $this->color_red = array( 1, 0, 0 ); + $this->color_black = array( 0, 0, 0 ); // Create pdf object $pdf = new GlmPDFInvoice( 'a4', 'portrait' ); $pdf->allowedTags .= '|rect:.*?'; $pdf->setLineStyle( 1 ); - $pdf->ezSetMargins( 20, 20, 20, 20); + $pdf->ezSetMargins( 20, 20, 20, 20 ); $pdf->selectFont( 'Helvetica' ); // Set header/footer of all pages @@ -120,12 +119,12 @@ class GlmMembersAdmin_ajax_createPDFInvoice if ( $this->config['settings']['company_logo'] ) { $pdf->ezImage( GLM_MEMBERS_PLUGIN_MEDIA_PATH . '/images/large/' . $this->config['settings']['company_logo'], // image - 0, // padding - 260, // width + 0, // padding + 260, // width 'none', // resize 'left', // justification - '', // angle - '' // array border + '', // angle + '' // array border ); } $pdf->restoreState(); @@ -194,17 +193,32 @@ class GlmMembersAdmin_ajax_createPDFInvoice $this->pdf_font_size ); - // Line Items. - $lineItems = $BillingSupport->getLineItemsForInvoice( $id ); + $invoiceLineItems = array(); + // Line Items (charges). + $lineItems = $fullInvoice['line_items']; + $item = false; foreach ( $lineItems as &$item ) { - $item['created'] = date( 'm/d/Y', strtotime( $item['created'] ) ); + $invoiceLineItems[] = array( + 'created' => date( 'm/d/Y', strtotime( $item['created'] ) ), + 'name' => $item['name'], + 'amount' => $item['amount'], + ); + } + // Payments. + $payments = $fullInvoice['payments']; + $item = false; + foreach ( $payments as &$item ) { + $invoiceLineItems[] = array( + 'created' => date( 'm/d/Y', strtotime( $item['transaction_time'] ) ), + 'name' => 'Payment', + 'amount' => '-'.$item['amount'], + ); } - $lineItemData = array(); $pdf->ezText( '', $this->pdf_font_size ); $pdf->ezText( '', $this->pdf_font_size ); $pdf->ezTable( - $lineItems, - array( 'created' => 'Date', 'name' => 'Description', 'amount' => 'Amount', 'total' => 'Balance' ), + $invoiceLineItems, + array( 'created' => 'Date', 'name' => 'Description', 'amount' => 'Rate' ), '', array( 'showHeadings' => 1, @@ -214,9 +228,32 @@ class GlmMembersAdmin_ajax_createPDFInvoice 'xOrientation' => 'right', 'cols' => array( 'created' => array( 'width' => 100 ), - 'name' => array( 'width' => 330 ), + 'name' => array( 'width' => 380 ), + 'amount' => array( 'width' => 50, 'justification' => 'right' ), + ), + ) + ); + $invoiceTotal = array( + array( + 'created' => '', + 'name' => 'Total Amount Due', + 'amount' => $invoice['balance'] + ) + ); + $pdf->ezTable( + $invoiceTotal, + array( 'created' => 'Date', 'name' => 'Description', 'amount' => 'Rate' ), + '', + array( + 'showHeadings' => 0, + 'showLines' => 4, + 'width' => 560, + 'xPos' => 0, + 'xOrientation' => 'right', + 'cols' => array( + 'created' => array( 'width' => 100 ), + 'name' => array( 'width' => 380, 'justification' => 'right' ), 'amount' => array( 'width' => 50, 'justification' => 'right' ), - 'total' => array( 'width' => 50, 'justification' => 'right' ), ), ) ); @@ -316,19 +353,55 @@ class GlmMembersAdmin_ajax_createPDFInvoice ) ); $pdf->ezText( '', $this->pdf_font_size ); - $paymentForm = array( + $paymentForm1 = array( array( 'header' => 'Please Pay:', 'value' => $invoice['balance'] ), + ); + $newY = $pdf->ezTable( + $paymentForm1, + array( 'header' => 'Header', 'value' => 'Value' ), + '', array( - 'header' => 'Payment Amount:', - 'value' => '___________', - 'headerFill' => $this->color_white, - 'valueFill' => $this->color_white, - - ), + 'fontSize' => 12, + 'showHeadings' => 0, + 'showLines' => 0, + 'width' => 230, + 'xPos' => 320, + 'xOrientation' => 'right', + 'cols' => array( 'header' => array( 'width' => 120,'justification' => 'right' ), 'value' => array( 'justification' => 'right' ) ), + ) + ); + if ( $invoice['balance'] != '0.00' ) { + $paymentForm2 = array( + array( + 'header' => 'Payment Amount:', + 'value' => '___________', + 'headerFill' => $this->color_white, + 'valueFill' => $this->color_white, + ) + ); + $newY = $pdf->ezTable( + $paymentForm2, + array( 'header' => 'Header', 'value' => 'Value' ), + '', + array( + 'fontSize' => 12, + 'showHeadings' => 0, + 'showLines' => 0, + 'width' => 230, + 'xPos' => 320, + 'xOrientation' => 'right', + 'cols' => array( 'header' => array( 'width' => 120,'justification' => 'right' ), 'value' => array( 'justification' => 'right' ) ), + ) + ); + } else { + $newY = $pdf->ezText( '', $this->pdf_font_size ); + $newY = $pdf->addText( 375, $newY, 14, 'NO PAYMENT REQUIRED' ); + } + $paymentForm3 = array( array( 'header' => 'Member Billing #:', 'value' => $account['account_number'] ), ); $pdf->ezTable( - $paymentForm, + $paymentForm3, array( 'header' => 'Header', 'value' => 'Value' ), '', array( @@ -338,7 +411,7 @@ class GlmMembersAdmin_ajax_createPDFInvoice 'width' => 230, 'xPos' => 320, 'xOrientation' => 'right', - 'cols' => array( 'header' => array( 'justification' => 'right' ), 'value' => array( 'justification' => 'right' ) ), + 'cols' => array( 'header' => array( 'width' => 120, 'justification' => 'right' ), 'value' => array( 'justification' => 'right' ) ), ) ); diff --git a/models/admin/ajax/createPDFLabels.php b/models/admin/ajax/createPDFLabels.php new file mode 100644 index 0000000..8eb36bc --- /dev/null +++ b/models/admin/ajax/createPDFLabels.php @@ -0,0 +1,152 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/billingSupport.php'; +require_once GLM_MEMBERS_BILLING_PLUGIN_PATH . '/lib/GlmPDFInvoice.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_createPDFLabels +{ + + /** + * 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 ) { + + $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); + + // Expect an array of member data with addresses. + + // PDF settings + $this->pdf_top_y = 725; + $this->pdf_width = 570; + $this->line_height = 10; + $this->pdf_lft_col_x = 20; + $this->pdf_rgt_col_x = 380; + $this->pdf_bottom_y = 130; + $this->pdf_font_size = 12; + $this->pdf_header_font_1 = 24; + $this->pdf_header_font_2 = 20; + $this->color_white = array( 1, 1, 1 ); + $this->color_red = array( 1, 0, 0 ); + $this->color_black = array( 0, 0, 0 ); + + // Create pdf object + $pdf = new GlmPDFInvoice( 'a4', 'portrait' ); + $pdf->allowedTags .= '|rect:.*?'; + $pdf->setLineStyle( 1 ); + $pdf->ezSetMargins( 20, 20, 20, 20 ); + $pdf->selectFont( 'Helvetica' ); + + $Invoice = new GlmDataInvoices( $this->wpdb, $this->config ); + $invoices = $Invoice->getList(); + + // echo '
$invoices: ' . print_r( $invoices, true ) . '
'; + + $y = 830; + $pdf->ezSetY( $y ); + + $counter = 0; + foreach ( $invoices as $invoice ) { + $counter++; + if ( $counter % 2 == 0 ) { + $y = $y += 40; + $pdf->ezSetY( $y ); + $option = array( + 'aleft' => 330, + ); + } else { + $y = $y -= 30; + $pdf->ezSetY( $y ); + $option = ''; + } + + // echo '
$invoice: ' . print_r( $invoice['account']['value'], true ) . '
'; + // Get account + $account = $BillingSupport->getAccountById( $invoice['account']['value'] ); + // echo '
$account: ' . print_r( $account, true ) . '
'; + $y = $pdf->ezText( + sprintf( + "%s\n%s\n%s, %s %s", + $account['ref_name'], + $account['billing_addr1'], + $account['billing_city'], + $account['billing_state'], + $account['billing_zip'] + ), + 12, + $option + ); + if ( $y <= 80 && $counter % 2 == 0 ) { + $pdf->ezNewPage(); + $y = 830; + $pdf->ezSetY( $y ); + } + } + + $pdf->ezStream(); + + wp_die(); + } + +} diff --git a/setup/databaseScripts/create_database_V0.0.29.sql b/setup/databaseScripts/create_database_V0.0.30.sql similarity index 98% rename from setup/databaseScripts/create_database_V0.0.29.sql rename to setup/databaseScripts/create_database_V0.0.30.sql index 81c6d61..c443285 100644 --- a/setup/databaseScripts/create_database_V0.0.29.sql +++ b/setup/databaseScripts/create_database_V0.0.30.sql @@ -16,6 +16,7 @@ CREATE TABLE {prefix}accounts ( ref_dest INT NOT NULL, -- reference to member id ref_name TINYTEXT NOT NULL, -- Name of reference member invoice_type INT NOT NULL DEFAULT '0', -- Ref to Invoice Type id + billing_contact_name TINYTEXT NULL, -- Billing Contact Name billing_fname TINYTEXT NULL, -- Billing First Name billing_lname TINYTEXT NULL, -- Billing Last Name billing_company TINYTEXT NULL, -- Billing Company Name @@ -259,6 +260,7 @@ CREATE TABLE {prefix}settings ( renewal_month INT NULL, -- Month of year for renewals billing_county_enabled BOOLEAN DEFAULT '0', -- Billing uses county field invoice_pdf_enabled BOOLEAN DEFAULT '0', -- Create PDF invoices + billing_contact_name_enabled BOOLEAN DEFAULT '0', -- Use billing_contact_name instead of fname lname PRIMARY KEY (id) ); diff --git a/setup/databaseScripts/dbVersions.php b/setup/databaseScripts/dbVersions.php index b3f0ccd..5276c44 100644 --- a/setup/databaseScripts/dbVersions.php +++ b/setup/databaseScripts/dbVersions.php @@ -43,5 +43,6 @@ $glmMembersBillingDbVersions = array( '0.0.27' => array('version' => '0.0.27', 'tables' => 15, 'date' => '11/19/2018'), '0.0.28' => array('version' => '0.0.28', 'tables' => 15, 'date' => '11/27/2018'), '0.0.29' => array('version' => '0.0.29', 'tables' => 15, 'date' => '11/29/2018'), + '0.0.30' => array('version' => '0.0.30', 'tables' => 15, 'date' => '12/04/2018'), ); diff --git a/setup/databaseScripts/update_database_V0.0.30.sql b/setup/databaseScripts/update_database_V0.0.30.sql new file mode 100644 index 0000000..b5874be --- /dev/null +++ b/setup/databaseScripts/update_database_V0.0.30.sql @@ -0,0 +1,13 @@ +-- Gaslight Media Billing Database +-- File Created: 12/04/2018 +-- Database Version: 0.0.30 +-- +-- To permit each query below to be executed separately, +-- all queries must be separated by a line with four dashes + +-- Update the accounts table +ALTER TABLE {prefix}accounts ADD billing_contact_name TINYTEXT NULL; + +---- + +ALTER TABLE {prefix}settings ADD billing_contact_name_enabled BOOLEAN DEFAULT '0'; diff --git a/setup/validActions.php b/setup/validActions.php index 4dac4ae..4238fa3 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -67,6 +67,7 @@ $glmMembersBillingAddOnValidActions = array( 'invoiceTypes' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'createNewInvoices' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'createPDFInvoice' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + 'createPDFLabels' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'accountsListExport' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'paymentsListExport' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, 'billingFlagExpiredUsers' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, diff --git a/views/admin/settings/billing.html b/views/admin/settings/billing.html index 8607368..64fa14a 100644 --- a/views/admin/settings/billing.html +++ b/views/admin/settings/billing.html @@ -210,6 +210,15 @@ + + + + + + Enable Billing Contact Name + + + -- 2.17.1