WIP - work on invoice types add edit
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 29 Nov 2017 21:52:32 +0000 (16:52 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 29 Nov 2017 21:52:32 +0000 (16:52 -0500)
Working currently on how to order it like the categories by hierarchy.

classes/data/dataInvoiceTypes.php
models/admin/settings/invoiceTypes.php
views/admin/settings/invoiceTypes.html

index d1527fa..cb74304 100644 (file)
@@ -181,6 +181,56 @@ class GlmDataInvoiceTypes extends GlmDataAbstract
         return $r;
     }
 
+    /*
+     * Sort a database result array (from DataAbstract.php) by parent, child relationships
+     *
+     * @param array $array Array to be sorted
+     * @param string $name Name of array element that represents the text to sort on
+     * @param string $parent Name of the array element that represents the parent ID to sort on
+     *
+     * @return array Sorted array
+     */
+    public static function sortParentChild($array, $name = 'name', $parent = 'parent')
+    {
+
+        if (!is_array($array) || count($array) == 0) {
+            return false;
+        }
+
+        // Do a sort by custom function with it included in the call
+        // This lets me directly use $name and $parent in the sort function
+        uasort($array, function ($a, $b) use ($name, $parent) {
+
+            // If there's a parent, append the name of this entry to the parent
+            // The '~~~' simply keeps appended strings from any chance of a match with something else
+            if ($a[$parent]['value']) {
+            $aVal = $a[$parent]['name'].'~~~'.$a['name'];
+            } else {
+            // Otheriwse just use the name.
+            $aVal = $a['name'].'~~~';
+            }
+
+            // Same for b value
+            if ($b[$parent]['value']) {
+            $bVal = $b[$parent]['name'].'~~~'.$b['name'];
+            } else {
+            $bVal = $b['name'].'~~~';
+            }
+
+            if ($aVal > $bVal) {
+            return 1;
+            }
+
+            if ($aVal < $bVal) {
+            return -1;
+            }
+
+            return 0;
+
+        });
+
+        return $array;
+    }
 }
 
 ?>
index bee1344..39ca9da 100644 (file)
@@ -137,9 +137,9 @@ class GlmMembersAdmin_settings_invoiceTypes extends GlmDataInvoiceTypes
         }
 
         // Get a current list of members
-        $invoiceTypes = $this->getList('', 'name');
+        $invoiceTypes = $this->getList();
 
-        // echo '<pre>$invoiceTypes: ' . print_r( $invoiceTypes, true ) . '</pre>';
+        echo '<pre>$invoiceTypes: ' . print_r( $invoiceTypes, true ) . '</pre>';
 
         // If we have list entries - even if it's an empty list
         $success          = true;
@@ -154,6 +154,11 @@ class GlmMembersAdmin_settings_invoiceTypes extends GlmDataInvoiceTypes
             }
         }
 
+        // Sort results by higherarchy (Parent/Child and Alpha)
+        $invoiceTypes = $this->sortParentChild($invoiceTypes);
+        echo '<pre>$invoiceTypes: ' . print_r( $invoiceTypes, true ) . '</pre>';
+
+
         // If we had a fatal error, redirect to the error page
         if ($error) {
             return array(
@@ -170,7 +175,8 @@ class GlmMembersAdmin_settings_invoiceTypes extends GlmDataInvoiceTypes
             'action'           => $_REQUEST['glm_action'],
             'enable_members'   => $enable_members,
             'haveInvoiceTypes' => $haveInvoiceTypes,
-            'invoiceTypes'     => $invoiceTypes
+            'invoiceTypes'     => $invoiceTypes,
+            'recurrenceTypes'  => $this->config['recurrence'],
         );
 
         // Return status, suggested view, and data to controller
index 2c033f7..0c9bd7b 100644 (file)
                     </td>
                 </tr>
                 <tr>
-                    <th>Code:</th>
+                    <th class="glm-required">Parent:</th>
                     <td>
-                        <input type="text" name="qcode" class="glm-form-text-input">
-                    </td>
-                </tr>
-                <tr>
-                    <th class="glm-required">Category:</th>
-                    <td>
-                        <input type="text" name="category" class="glm-form-text-input" required>
+                        <select name="parent">
+                            <option value="0">(none)</option>
+                            {if $haveInvoiceTypes}
+                                {foreach $invoiceTypes as $type}
+                                    <option value="{$type.id}">{$type.name}</option>
+                                {/foreach}
+                            {/if}
+                        </select>
                     </td>
                 </tr>
                 <tr>
                     </td>
                 </tr>
                 <tr>
-                    <th>Dynamic Amount:</th>
+                    <th>Recurring:</th>
                     <td>
-                        <input type="hidden" name="dynamic_amount" class="glm-form-text-input" value="0">
-                        <input type="checkbox" name="dynamic_amount" class="glm-form-text-input" value="1">
+                        <input type="hidden" name="recurring" class="glm-form-text-input" value="0">
+                        <input type="checkbox" name="recurring" class="glm-form-text-input" value="1">
                     </td>
                 </tr>
                 <tr>
-                    <th>Notes:</th>
+                    <th>Recurrence:</th>
                     <td>
-                        <textarea name="notes" class="glm-form-textarea"></textarea>
+                        <select name="recurrence">
+                            <option value="">(none)</option>
+                                {foreach $recurrenceTypes as $typeId => $typeLabel}
+                                    <option value="{$typeId}">{$typeLabel}</option>
+                                {/foreach}
+                        </select>
                     </td>
                 </tr>
             </table>
@@ -56,8 +62,8 @@
     <!-- Add InvoiceTypes Button -->
     <div id="deleteInvoiceTypeDialog" class="glm-dialog-box" title="Delete InvoiceType">
         <center>
-            <p>Are you sure you want to delete this inoiceType?</p>
-            <p><div id="deleteInvoiceTypeConfirm" class="button button-primary">Yes, delete this inoiceType</div></p>
+            <p>Are you sure you want to delete this invoice Type?</p>
+            <p><div id="deleteInvoiceTypeConfirm" class="button button-primary">Yes, delete this invoice Type</div></p>
             <p><div id="deleteInvoiceTypeCancel" class="button button-primary">Cancel</div></p>
         </center>
     </div>
         <form action="{$thisUrl}?page={$thisPage}" method="post" enctype="multipart/form-data">
             <input type="hidden" name="glm_action" value="invoiceTypes">
             <input type="hidden" name="option" value="update">
-            <input id="editInvoiceTypeID" type="hidden" name="id" value="">
+            <input id="edit-id" type="hidden" name="id" value="">
             <table class="glm-admin-table">
                 <tr>
                     <th class="glm-required">Invoice Type Name:</th>
                     <td>
-                        <input id="editInvoiceTypeName" type="text" name="name" class="glm-form-text-input" required>
+                        <input id="edit-name" type="text" name="name" class="glm-form-text-input" required>
                     </td>
                 </tr>
                 <tr>
-                    <th>Code:</th>
+                    <th class="glm-required">Parent:</th>
                     <td>
-                        <input id="editInvoiceTypeQCode" type="text" name="qcode" class="glm-form-text-input">
+                        <select id="edit-parent" name="parent">
+                            <option value="0">(none)</option>
+                                {if $haveInvoiceTypes}
+                                    {foreach $invoiceTypes as $type}
+                                        {if !$type.parent.value} <!-- don't show child categories -->
+                                            <option value="{$type.id}">{$type.name}</option>
+                                        {/if}
+                                    {/foreach}
+                                {/if}
+                        </select>
                     </td>
                 </tr>
                 <tr>
-                    <th class="glm-required">Category:</th>
-                    <td>
-                        <input id="editInvoiceTypeCategory" type="text" name="category" class="glm-form-text-input" required>
-                    </td>
-                </tr>
-                <tr>
-                    <th class="glm-required">Amount:</th>
+                    <th class="glm-required">Amount:Numbers only (999.99)</th>
                     <td>
-                        <input id="editInvoiceTypeAmount" type="text" name="amount" class="glm-form-text-input" required>
+                        <input id="edit-amount" type="text" name="amount" class="glm-form-text-input" required>
                     </td>
                 </tr>
                 <tr>
-                    <th>Dynamic Amount:</th>
+                    <th>Recurring:</th>
                     <td>
-                        <input type="hidden" name="dynamic_amount" class="glm-form-text-input" value="0">
-                        <input id="editInvoiceTypeDynamicAmount" type="checkbox" name="dynamic_amount" class="glm-form-text-input" value="1">
+                        <input type="hidden" name="recurring" class="glm-form-text-input" value="0">
+                        <input id="edit-recurring" type="checkbox" name="recurring" class="glm-form-text-input" value="1">
                     </td>
                 </tr>
                 <tr>
-                    <th>Notes:</th>
+                    <th>Recurrence:</th>
                     <td>
-                        <textarea id="editInvoiceTypeNotes" name="notes" class="glm-form-textarea"></textarea>
+                        <select id="edit-recurrence" name="recurrence">
+                            <option value="">(none)</option>
+                                {foreach $recurrenceTypes as $typeId => $typeLabel}
+                                    <option value="{$typeId}">{$typeLabel}</option>
+                                {/foreach}
+                        </select>
                     </td>
                 </tr>
             </table>
                 <th>ID</th>
                 <th>InvoiceType</th>
                 <th>Amount</th>
+                <th>Recurring</th>
+                <th>Recurrence</th>
                 <th>&nbsp;</th>
             </tr>
         </thead>
                     {/if}
                         <td>{$t.id}</td>
                         <td>
-                            <a class="editInvoiceType"
-                                data-inoiceTypeID="{$t.id}"
-                                data-inoiceTypeName="{$t.name}"
-                                data-inoiceTypeQCode="{$t.qcode}"
-                                data-inoiceTypeCategory="{$t.category}"
-                                data-inoiceTypeAmount="{$t.amount}"
-                                data-inoiceTypeDynamicAmount="{$t.dynamic_amount}"
-                                data-inoiceTypeNotes="{$t.notes}">{$t.name}</a>
-                        </td>
-                        <td>
-                            {$t.amount}
+                            <div{if $t.parent.value} class="glm-indent"{/if}>
+                                <a class="editInvoiceType"
+                                    data-invoice-id="{$t.id}"
+                                    data-invoice-name="{$t.name}"
+                                    data-invoice-parent="{$t.parent}"
+                                    data-invoice-amount="{$t.amount}"
+                                    data-invoice-recurring="{$t.recurring.value}"
+                                    data-invoice-recurrence="{$t.recurrence}">{$t.name}</a>
+                            </div>
                         </td>
+                        <td>{$t.amount}</td>
+                        <td>{if $t.recurring.value}Yes{else}No{/if}</td>
+                        <td>{if $t.recurrence}{$recurrenceTypes[$t.recurrence]}{/if}</td>
                         <td>
-                            <div class="deleteInvoiceTypeButton button button-secondary glm-button-small glm-right" data-inoiceTypeID="{$t.id}">Delete</div>
+                            <div class="deleteInvoiceTypeButton button button-secondary glm-button-small glm-right" data-invoiceTypeID="{$t.id}">Delete</div>
                         </td>
                     </tr>
                 {/foreach}
                 $("#newInvoiceTypeDialog").dialog("open");
             });
             $('.editInvoiceType').click( function() {
-                $('#editInvoiceTypeDynamicAmount').prop( 'checked', false );
+                $('#editRecurring').prop( 'checked', false );
 
-                var inoiceTypeID = $(this).data('inoicetypeid');
-                var inoiceTypeName = $(this).data('inoicetypename');
-                var inoiceTypeQCode = $(this).data('inoicetypeqcode');
-                var inoiceTypeCategory = $(this).data('inoicetypecategory');
-                var inoiceTypeAmount = $(this).data('inoicetypeamount');
-                var inoiceTypeDynamicAmount = $(this).data('inoicetypedynamicamount');
-                var inoiceTypeNotes = $(this).data('inoicetypenotes');
+                var invoiceID         = $(this).data('invoice-id');
+                var invoiceName       = $(this).data('invoice-name');
+                var invoiceParent     = $(this).data('invoice-parent');
+                var invoiceAmount     = $(this).data('invoice-amount');
+                var invoiceRecurring  = $(this).data('invoice-recurring');
+                var invoiceRecurrence = $(this).data('invoice-recurrence');
 
-                // Set the values of the edit form for the selected inoiceType
-                $('#editInvoiceTypeID').val( inoiceTypeID );
-                $('#editInvoiceTypeName').val( inoiceTypeName );
-                $('#editInvoiceTypeQCode').val( inoiceTypeQCode );
-                $('#editInvoiceTypeCategory').val( inoiceTypeCategory );
-                $('#editInvoiceTypeAmount').val( inoiceTypeAmount );
-                if ( inoiceTypeDynamicAmount === 1 ) {
-                    $('#editInvoiceTypeDynamicAmount').prop( 'checked', true );
+                // Set the values of the edit form for the selected invoiceType
+                $('#edit-id').val( invoiceID );
+                $('#edit-name').val( invoiceName );
+                $('#edit-parent').val( invoiceParent );
+                $('#edit-amount').val( invoiceAmount );
+                if ( invoiceRecurring === 1 ) {
+                    $('#edit-recurring').prop( 'checked', true );
                 }
-                $('#editInvoiceTypeNotes').val( inoiceTypeNotes );
+                console.log(invoiceRecurring);
+                $('#edit-recurrence').val( invoiceRecurrence );
 
                 $("#editInvoiceTypeDialog").dialog("open");
             });
 
             var id = false;
             $('.deleteInvoiceTypeButton').click( function() {
-                id = $(this).attr('data-inoiceTypeID');
+                id = $(this).attr('data-invoiceTypeID');
                 $("#deleteInvoiceTypeDialog").dialog("open");
             });
             $('#deleteInvoiceTypeConfirm').click( function() {