break;
case 'insert':
+ // Get Data Class for Invoice Types
+ $InvoiceTypesObj = new GlmDataInvoiceTypes( $this->wpdb, $this->config );
// Set transaction_time to current time.
$_REQUEST['transaction_time'] = date('Y-m-d H:i:s');
$_REQUEST['due_date'] = date('Y-m-d', strtotime($_REQUEST['due_date']));
$line_items = $_REQUEST['line_items'];
if ( is_array( $line_items ) && !empty( $line_items ) ) {
foreach ( $line_items as $key => $line_item ) {
+ // Get the invoice type
+ $invoiceType = $InvoiceTypesObj->getEntry( $line_item );
+ //
// Add individual line item record
$this->wpdb->insert(
GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'line_items',
'amount' => $_REQUEST['line_item_amount'][$line_item],
'quantity' => $_REQUEST['line_item_qty'][$line_item],
'total' => (float)$_REQUEST['line_item_qty'][$line_item] * (float)$_REQUEST['line_item_amount'][$line_item],
+ 'created' => date('Y-m-d'),
+ 'first_due_date' => $_REQUEST['due_date'],
+ 'next_due_date' => $_REQUEST['due_date'],
),
array(
'%d',
'%s',
'%s',
'%d',
- '%d'
+ '%d',
+ '%s',
+ '%s',
+ '%s',
)
);
}
{if $invoiceAdded}<span class="glm-notice glm-flash-updated">Notification Added</span>{/if}
<div id="billing-invoice-form">
- <form action="{$thisUrl}?page={$thisPage}&glm_action=invoices" method="post">
+ <form id="create-invoice-form" action="{$thisUrl}?page={$thisPage}&glm_action=invoices" method="post">
{if $invoice_id}
<input type="hidden" name="option" value="update">
<input type="hidden" name="id" value="{$invoice_id}">
</div>
<div class="glm-columns glm-small-12 glm-large-10">
<div class="glm-row">
- <div class="glm-columns glm-small-8" style="text-align: right;"> Total Amount: </div>
+ <div class="glm-columns glm-small-8" style="text-align: right;">
+ Total Amount:
+ <input type="hidden" id="invoice-total-amount" value="" />
+ </div>
<div class="glm-columns glm-small-4" id="invoice-total"> $0.00 </div>
</div>
</div>
<tr>
<th class="glm-required">Invoice Type</th>
<td>
- <select name="line_item_type">
+ <select id="line_item_type" name="line_item_type">
<option value="">Select Invoice Type</option>
{$parent = 0}
{foreach $invoiceTypes as $inv}
<optgroup label="{$inv.name}">
{/if}
{if $inv.amount != '0.00'}
- <option value="{$inv.id}">{$inv.name}</option>
+ <option value="{$inv.id}" data-price="{$inv.amount}">{$inv.name}</option>
{/if}
{if $inv.parent.value == 0}
</optgroup>
{/foreach}
</select>
</td>
+ <td><p id="line_item_price"></p></td>
</tr>
</table>
</form>
// List of Billing Accounts.
var availableAccounts = [
{foreach $accounts as $m}
- { label: "{$m.ref_name|unescape:'html'|replace:'"':''}", value: "{$m.ref_name|unescape:'html'|replace:'"':''}", id: '{$m.id}' },
+ {
+ label: "{$m.ref_name|unescape:'html'|replace:'"':''}",
+ value: "{$m.ref_name|unescape:'html'|replace:'"':''}",
+ id: '{$m.id}',
+ anniversary: '{$m.next_anniversary_date}'
+ },
{/foreach}
];
change: function( event, ui ) {
// If not in select option then empty the field
$(this).val((ui.item ? ui.item.label : ""));
+ // Update the anniversary date
+ $('#invoice_due_date').val( ui.item.anniversary );
},
});
// Setup autocomplete for the non member input.
totalAmount += parseFloat( line_items[i].amount ) * line_items[i].qty;
}
$('#invoice-total').html('$' + totalAmount.toFixed( 2 ) );
+ $('#invoice-total-amount').val( totalAmount.toFixed( 2 ) );
}
/**
selectedLineItem.qty = parseInt(1);
line_items.push( selectedLineItem );
$( '#invoice-line-items' ). append( '<div class="glm-row" id="line-item-type-' + selectedLineItem.id + '">' +
- '<div class="glm-columns glm-small-7">' + selectedLineItem.name + '</div>' +
+ '<div class="glm-columns glm-small-1 line-item-delete dashicons dashicons-trash" data-id="' + selectedLineItem.id + '"></div>' +
+ '<div class="glm-columns glm-small-6">' + selectedLineItem.name + '</div>' +
'<div class="glm-columns glm-small-1">' +
- '<input type="number" min="0" data-id="' + selectedLineItem.id + '" name="line_item_qty[' + selectedLineItem.id + ']" value="1">' +
+ '<input type="number" min="1" data-id="' + selectedLineItem.id + '" name="line_item_qty[' + selectedLineItem.id + ']" value="1">' +
'</div><div class="glm-columns glm-small-4">' +
'<input type="hidden" name="line_items[]" value="' + selectedLineItem.id + '">' +
'<input type="hidden" name="line_item_amount[' + selectedLineItem.id + ']" value="' + selectedLineItem.amount + '">' +
selectedLineItem.qty++;
$('input[name="line_item_qty\\[' + selectedLineItem.id + '\\]').val(selectedLineItem.qty);
}
+ $('#line_item_price').html( '' );
dialog.dialog( 'close' );
}
totalInvoice();
return valid;
}
+ // Add the price to the right of the new selection for add line item.
+ $('#line_item_type').change(function(){
+ $('#line_item_price').html( '$' + $('#line_item_type option:selected').data( 'price') );
+ });
+
+ // Here we delete a line_item from the form.
+ $('body').on( 'click', '.line-item-delete', function(){
+ $('#line-item-type-' + $(this).data( 'id' ) ).remove();
+ // Need to remove from line_items also.
+ // Loop through the line_items and remove the one with matching id.
+ for ( i = 0; i < line_items.length; i++ ) {
+ if ( line_items[i].id == $(this).data('id') ) {
+ line_items.splice( i, 1 );
+ }
+ }
+ totalInvoice();
+ } );
+
/**
* addNewAccount
*
}, 500);
}
- /**
- * Ajax call to get the list of billing accounts and update the select
- * for the Billing account drop down.
- */
- // function updateBillingAccountSelect(selected_id = null) {
- // $.ajax({
- // url: '{$ajaxUrl}',
- // cache: false,
- // type: 'GET',
- // data: 'action=glm_members_admin_ajax&glm_action=account&option=list',
- // encode: true,
- // dataType: 'json'
- // }).done(function(msg){
- // // console.log( msg.length );
- // var memberAccount = document.getElementById('member-account');
- // memberAccount.options.length = 0;
- // memberAccount.options[memberAccount.options.length] = new Option( 'Select an Account', '' );
- // for ( index in msg ) {
- // var obj = msg[index];
- // memberAccount.options[memberAccount.options.length] = new Option( obj.ref_name, obj.id );
- // }
- // memberAccount.value = selected_id;
- // });
- // }
-
/**
* Ajax call to get the list of members with accounts.
* Updating the account list used for the autocomplete.
buttons: {
"Add Line Item": addLineItem,
Cancel: function(){
+ $('#line_item_price').html( '' );
dialog.dialog( 'close' );
},
},
addLineItem();
});
+ // Need to make sure something is added to the form.
+ // Should not be making empty invoices.
+ $('#create-invoice-form').submit(function(e){
+ // Check if there's a total amount.
+ // If it is empty then don't submit form.
+ if ( !$('#invoice-total-amount').val() || $('#invoice-total-amount').val() == '0.00' ) {
+ alert( 'Empty invoice. Please add something!' );
+ return false;
+ } else {
+ return true;
+ }
+ return false;
+ });
+
/**
* Click event for the id of #newLineItemButton.
*
$('#invoice_due_date').val( $(this).find('option:selected').data('anniversary') );
});
- /**
- * Update the member name field when you select a member from the add account form.
- */
- $('#non-member-account').change(function(){
- $('#glm_member_name').val( $(this).find('option:selected').text() );
- });
-
// Flash certain elements for a short time after display
$(".glm-flash-updated").fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500);