WIP billing invoice template
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 18 Dec 2017 21:54:16 +0000 (16:54 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 18 Dec 2017 21:54:16 +0000 (16:54 -0500)
Working on the billing invoice template.
Creating method in support class to generate the html.

classes/billingSupport.php
classes/data/dataInvoices.php
classes/data/dataPayments.php
models/admin/billing/invoices.php
views/admin/billing/invoiceStore.html [new file with mode: 0644]
views/admin/billing/invoices.html
views/admin/billing/payments.html

index a81a8d4..9c2a15c 100644 (file)
@@ -202,7 +202,7 @@ class GlmBillingSupport
     }
 
     /**
-     * getStatements
+     * getStatementsByRefDest
      *
      * Create array that has the set of invoices and payments for an account.
      * Returns false if nothing is found or array if it finds records.
@@ -367,6 +367,16 @@ class GlmBillingSupport
         );
     }
 
+    /**
+     * getUnPaidInvoicesByAccount
+     *
+     * Get all unpaid invoices given an account id.
+     *
+     * @param mixed $account Id of the account.
+ *
+     * @access public
+     * @return void
+     */
     public function getUnPaidInvoicesByAccount( $account )
     {
         return $this->wpdb->get_results(
@@ -405,6 +415,16 @@ class GlmBillingSupport
     }
 
 
+    /**
+     * getBalanceDueByAccount
+     *
+     * Get the balance due for an account.
+     *
+     * @param mixed $account Id of the account
+     *
+     * @access public
+     * @return void
+     */
     public function getBalanceDueByAccount( $account )
     {
         // Find all invoices for this account that aren't marked as paid.
@@ -417,5 +437,134 @@ class GlmBillingSupport
         return $balance_due;
     }
 
+
+    /**
+     * viewInvoice
+     *
+     * 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 viewInvoice( $invoice_id )
+    {
+        $invoice = $this->getInvoiceById( $invoice_id );
+        // echo '<pre>$invoice: ' . print_r( $invoice, true ) . '</pre>';
+        $line_items = $this->getLineItemsForInvoice( $invoice_id );
+        // echo '<pre>$line_items: ' . print_r( $line_items, true ) . '</pre>';
+        // echo '<pre>$this->config: ' . print_r( $this->config['settings'], true ) . '</pre>';
+
+        $templateData = array(
+            'settings'   => $this->config['settings'],
+            'invoice'    => $invoice,
+            'line_items' => $line_items,
+            'member'     => array( 'member_name' => 'Member Name here' )
+        );
+
+        $invoiceHtml = $this->generateInvoiceHtml( $templateData, 'admin/billing/invoiceStore.html' );
+        echo '<pre>$invoiceHtml: ' . print_r( $invoiceHtml, true ) . '</pre>';
+    }
+
+    /**
+     * getLineItemsForInvoice
+     *
+     * Grab all the line_items records for a given invoice.
+     *
+     * @param mixed $invoice_id
+     *
+     * @access public
+     * @return void
+     */
+    public function getLineItemsForInvoice( $invoice_id )
+    {
+        $line_items = $this->wpdb->get_results(
+            $this->wpdb->prepare(
+                "SELECT *
+                   FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "line_items
+                  WHERE invoice = %d",
+                $invoice_id
+            ),
+            ARRAY_A
+        );
+        return $line_items;
+    }
+
+
+    /**
+     * Merge template and data to produce HTML
+     *
+     * Checks the theme's view directories and the view directories for
+     * this plugin for a matching view file.
+     *
+     * Note that $viewFile needs to have the proper view directory path
+     * includes. (i.e. "/views/admin/billing/invoiceStore.html")
+     *
+     * @param $data array Array of data to merge with the template
+     * @param $view string Path added to
+     *
+     * @access public
+     * @return void
+     */
+    function generateInvoiceHtml( $data, $viewFile )
+    {
+
+        // If a view file is specified
+        if ($viewFile) {
+
+            // Get the specified view file - check theme first
+            $viewPath = GLM_MEMBERS_PLUGIN_CURRENT_THEME_DIR."/views";
+            $viewPath2 = GLM_MEMBERS_WORDPRESS_PLUGIN_PATH . "views";       // Save default
+
+            // If the view is not found in the theme, fall back to views in the plugin
+            if (!is_file($viewPath.'/'.$viewFile)) {
+
+                // Next try the plugin/add-on
+                $viewPath = GLM_MEMBERS_BILLING_PLUGIN_PATH . "/views";
+
+                if (!is_file($viewPath.'/'.$viewFile)) {
+
+                    if (GLM_MEMBERS_PLUGIN_FRONT_DEBUG) {
+                        trigger_error("Bad or missing view file when generating checkout HTML: $viewPath/$viewFile", E_USER_NOTICE);
+                    }
+
+                }
+
+            }
+
+        }
+
+        // Load Smarty Template support
+        $smarty = new smartyTemplateSupport();
+
+        // Add standard parameters
+        require GLM_MEMBERS_PLUGIN_SETUP_PATH.'/standardTemplateParams.php';
+
+        // Add data from model to Smarty template
+        if (is_array($data) && count($data) > 0) {
+            foreach ($data as $k => $d) {
+                $smarty->templateAssign($k, $d);
+            }
+        }
+
+        // Update the Smarty view path
+        $smarty->template->setTemplateDir($viewPath);
+
+        // If the view path doesn't match the default, add the default (using theme view)
+        if ($viewPath2 != $viewPath) {
+            $smarty->template->addTemplateDir($viewPath2);
+        }
+
+        // Generate output from model data and view
+        $out = $smarty->template->fetch($viewFile);
+
+        return $out;
+
+    }
+
 }
 
index cf342ef..07b21f3 100644 (file)
@@ -207,12 +207,15 @@ class GlmDataInvoices extends GlmDataAbstract
      */
     public function entryPostProcessing($r, $a)
     {
-        $sql = "
-        SELECT A.ref_name
-          FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices I
-          LEFT OUTER JOIN " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts A ON ( I.account = A.id )
-         WHERE I.id = " . $r['id'];
-        $r['member_name'] = $this->wpdb->get_var( $sql );
+        $r['member_name'] = $this->wpdb->get_var(
+            $this->wpdb->prepare(
+                "SELECT A.ref_name
+                   FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices I
+                   LEFT OUTER JOIN " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts A ON ( I.account = A.id )
+                  WHERE I.id = %d",
+                $r['id']
+            )
+        );
         return $r;
     }
 
index 5746ea8..cb0d548 100644 (file)
@@ -190,6 +190,15 @@ class GlmDataPayments extends GlmDataAbstract
      */
     public function entryPostProcessing($r, $a)
     {
+        $r['member_name'] = $this->wpdb->get_var(
+            $this->wpdb->prepare(
+                "SELECT A.ref_name
+                   FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "payments P
+                   LEFT OUTER JOIN " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "accounts A ON ( P.account = A.id )
+                  WHERE P.id = %d",
+                $r['id']
+            )
+        );
         return $r;
     }
 
index 9b8cc03..861cb30 100644 (file)
@@ -129,6 +129,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
 
         case 'add':
             $invoices        = $this->newEntry();
+            // Set the view file to editInvoice
             $view            = 'editInvoice';
             $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config );
             $invoiceTypes    = $InvoiceTypesObj->getList();
@@ -183,11 +184,14 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
                     }
                 }
             }
+            // Here we're going to generate the invoice total.
             $totals = $this->generateInvoiceTotal( $this->invoice_id );
             // Now we have a total for the invoice we can record the transaction
             $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config );
+            // Calling the support class to save the invoice to transaction table.
             $BillingSupport->recordInvoice( $this->invoice_id, $_REQUEST['account'], $totals['amount_total'] );
 
+            // Set the view file to editInvoice
             $view = 'editInvoice';
             $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config );
             $invoiceTypes    = $InvoiceTypesObj->getList();
@@ -218,6 +222,7 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
                 $invoices = false;
             }
 
+            // Set the view file to editInvoice
             $view = 'editInvoice';
             break;
 
@@ -235,9 +240,20 @@ class GlmMembersAdmin_billing_invoices extends GlmDataInvoices
                 $invoiceUpdateError = true;
             }
 
+            // Set the view file to editInvoice
             $view = 'editInvoice';
 
             break;
+        case 'view':
+            // Now we have a total for the invoice we can record the transaction
+            $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config );
+
+            // echo '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
+
+            $BillingSupport->viewInvoice( $_REQUEST['id'] );
+
+            wp_die( 'fun' );
+            break;
 
         case 'delete':
             // Need to remove any line items for the invoice alse
diff --git a/views/admin/billing/invoiceStore.html b/views/admin/billing/invoiceStore.html
new file mode 100644 (file)
index 0000000..11e946e
--- /dev/null
@@ -0,0 +1,44 @@
+<html>
+    <head>
+        <style>
+
+        </style>
+    </head>
+    <body>
+        <table id="container-table">
+            <tr>
+                <td><!-- Logo --></td>
+                <td><!-- Invoice date/memberbilling# --></td>
+            </tr>
+            <tr>
+                <td colspan="2"> <!-- Billing Invoice Settings --> </td>
+            </tr>
+            <tr>
+                <td colspan="2">
+                    Bill To:
+                    <!-- Billing Settings -->
+                </td>
+            </tr>
+            <tr>
+                <table>
+                    <thead>
+                        <tr>
+                            <th>Date</th>
+                            <th>Description</th>
+                            <th>Amount</th>
+                            <th>Balance</th>
+                        </tr>
+                    </thead>
+                    <tbody>
+                        <tr>
+                            <td><!-- date --></td>
+                            <td><!-- description --></td>
+                            <td><!-- amount --></td>
+                            <td><!-- balance --></td>
+                        </tr>
+                    </tbody>
+                </table>
+            </tr>
+        </table>
+    </body>
+</html>
index 02112e4..d08d59e 100644 (file)
@@ -13,6 +13,7 @@
             <th>Time</th>
             <th>Due Date</th>
             <th>Balance</th>
+            <th>View</th>
         </tr>
     </thead>
     <tbody>
@@ -29,6 +30,7 @@
                     <td> {$t.transaction_time.datetime} </td>
                     <td> {$t.due_date.date} </td>
                     <td> {$t.balance} </td>
+                    <td> <a href="{$thisUrl}?page={$thisPage}&glm_action=invoices&option=view&id={$t.id}">View</a> </td>
                 </tr>
             {/foreach}
         {else}
index 4624e6e..3779a6f 100644 (file)
@@ -24,7 +24,7 @@
                     <tr class="alternate">
                 {/if}
                     <td> {$t.id} </td>
-                    <td>  </td>
+                    <td> {$t.member_name} </td>
                     <td> {$t.transaction_time.datetime} </td>
                     <td> {$t.amount|string_format:"%.2f"} </td>
                 </tr>