| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* Author: Bernard Szlachta
|
| 4 |
* Company: NobleProg Limited
|
| 5 |
* URL: www.nobleprog.co.uk
|
| 6 |
* bernard.szlachta (at) NobleProg.co.uk
|
| 7 |
*/
|
| 8 |
|
| 9 |
require_once('invoice_statistics.inc');
|
| 10 |
require_once('invoices_views.inc');
|
| 11 |
require_once('func.inc.php');
|
| 12 |
/**
|
| 13 |
* Implementation of hook_menu()
|
| 14 |
*
|
| 15 |
* @param $may_cache A boolean indicating whether cacheable menu items should be returned.
|
| 16 |
*/
|
| 17 |
function invoices_menu($may_cache) {
|
| 18 |
$items = array();
|
| 19 |
if ($may_cache) {
|
| 20 |
$access = user_access('administer site configuration');
|
| 21 |
$items[] = array(
|
| 22 |
'path' => 'admin/nobleprog/invoices',
|
| 23 |
'title' => t('Validate Invoices'),
|
| 24 |
'callback' => 'drupal_get_form',
|
| 25 |
'callback arguments' => Array('invoices_settings_get_form'),
|
| 26 |
'description' => t('Validation of invoices'),
|
| 27 |
'type' => MENU_NORMAL_ITEM,
|
| 28 |
'access' => $access
|
| 29 |
);
|
| 30 |
$items[] = array(
|
| 31 |
'path' => 'invoice_stats',
|
| 32 |
'title' => t('Invoices Information'),
|
| 33 |
'callback' => 'invoices_landing_page',
|
| 34 |
'description' => t('Invoices Information, summarises, etc.'),
|
| 35 |
'type' => MENU_CALLBACK,
|
| 36 |
'access' => user_access('view invoice statistics'),
|
| 37 |
);
|
| 38 |
|
| 39 |
}
|
| 40 |
|
| 41 |
return $items;
|
| 42 |
}
|
| 43 |
|
| 44 |
function invoices_perm(){
|
| 45 |
return array('view invoice statistics');
|
| 46 |
}
|
| 47 |
/**
|
| 48 |
* Menu callback; handles pages for creating and editing URL aliases.
|
| 49 |
*/
|
| 50 |
function invoices_settings_get_form() {
|
| 51 |
$form['invoices_validation'] = array (
|
| 52 |
'#type' => 'fieldset',
|
| 53 |
'#title' => t("Invoices validation")
|
| 54 |
);
|
| 55 |
$form['invoices_validation']['check_validation'] = array (
|
| 56 |
'#type' => 'submit',
|
| 57 |
'#value' => t("Validate Invoices")
|
| 58 |
);
|
| 59 |
return $form;
|
| 60 |
}
|
| 61 |
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Implementation of submit form or something:)
|
| 65 |
*/
|
| 66 |
function invoices_settings_get_form_submit($form_id, $form_values) {
|
| 67 |
switch ($form_values['op']) {
|
| 68 |
case t("Validate Invoices"): {
|
| 69 |
invoices_validate();
|
| 70 |
break;
|
| 71 |
}
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
| 75 |
/**
|
| 76 |
* Implementation of hook_form_alter().
|
| 77 |
*/
|
| 78 |
function invoices_form_alter($form_id, &$form) {
|
| 79 |
if ($form_id == 'invoice_node_form') {
|
| 80 |
$nid = $form['#node']->nid;
|
| 81 |
if (empty($nid) || !is_numeric($nid)) { // insert new number of invoice if nid of node doesn't exist
|
| 82 |
|
| 83 |
$inv_no = variable_get('invoice_next_id',invoices_get_last_id_from_db()+1);
|
| 84 |
// get last invoice id from variable (or from db if var dosen't exist)
|
| 85 |
|
| 86 |
$inv_pf_no = variable_get('invoice_next_pf_id',1);
|
| 87 |
// get last pro-forma invoice id from variable (or from db if var dosen't exist)
|
| 88 |
|
| 89 |
if($form['#post']['field_pro_forma']['keys'] == 'yes'){
|
| 90 |
$form['field_invoice_number'][0]['value']['#value'] = $inv_pf_no;
|
| 91 |
} else {
|
| 92 |
$form['field_invoice_number'][0]['value']['#value'] = $inv_no;
|
| 93 |
}
|
| 94 |
|
| 95 |
|
| 96 |
//$form['field_invoice_number'][0]['value']['#attributes']['disabled'] = 'true'; // dissallow editing this component
|
| 97 |
|
| 98 |
$form['field_invoice_number'][0]['value']['#suffix'] = t('Invoice number auto-generated successfully.'); // add suffix description
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
| 102 |
|
| 103 |
if ($form_id == 'invoice_item_node_form') {
|
| 104 |
//Set the parent invoice
|
| 105 |
if(!isset($form['field_parent_invoice']['nids']['#default_value']['0'])){
|
| 106 |
$form['field_parent_invoice']['nids']['#default_value']['0'] = arg(3);
|
| 107 |
}
|
| 108 |
}
|
| 109 |
|
| 110 |
}
|
| 111 |
|
| 112 |
/**
|
| 113 |
* hook_nodeapi Implementation
|
| 114 |
*/
|
| 115 |
function invoices_nodeapi(&$node, $op, $teaser = null, $page = null) {
|
| 116 |
global $user;
|
| 117 |
|
| 118 |
/**
|
| 119 |
* Injects extra stuff to an invoice
|
| 120 |
*/
|
| 121 |
if ($node->type == 'invoice' and $op == 'view') {
|
| 122 |
$path = drupal_get_path('module', 'invoices').'/'.'invoices.css';
|
| 123 |
drupal_add_css($path);
|
| 124 |
|
| 125 |
//Inject bank details
|
| 126 |
if (!empty($node->field_bank_details[0]['nid']) && is_numeric($node->field_bank_details[0]['nid'])) {
|
| 127 |
$node_bank_account = node_load($node->field_bank_details[0]['nid']);
|
| 128 |
$node->content['bank_account'] = array (
|
| 129 |
'#value' => theme('bank_account',$node_bank_account ),
|
| 130 |
'#title' => 'Bank details',
|
| 131 |
'#type' => 'fieldset',
|
| 132 |
'#weight' => 9,
|
| 133 |
'#attributes' => array( 'class' => 'bank-details'),
|
| 134 |
);
|
| 135 |
}
|
| 136 |
|
| 137 |
//Inject invoice items
|
| 138 |
$view = views_get_view('invoice_items');
|
| 139 |
$invoice_items = views_build_view('embed', $view, array($node->nid), false, 0, false, 0);
|
| 140 |
$node->content['invoice_items']['#value'] = $invoice_items;
|
| 141 |
$node->content['invoice_items']['#weight'] = -7;
|
| 142 |
|
| 143 |
|
| 144 |
//Inject client details
|
| 145 |
if (!empty($node->field_to[0][nid]) && is_numeric($node->field_to[0][nid])) {
|
| 146 |
$node_client = node_load($node->field_to[0][nid]);
|
| 147 |
$node->content['client'] = array (
|
| 148 |
'#value' => theme('client_details',$node_client ),
|
| 149 |
'#title' => 'To',
|
| 150 |
'#type' => 'fieldset',
|
| 151 |
'#weight' => -8,
|
| 152 |
'#attributes' => array( 'class' => 'client-details'),
|
| 153 |
);
|
| 154 |
}
|
| 155 |
|
| 156 |
if (!empty($node->field_from[0][nid]) && is_numeric($node->field_from[0][nid])) {
|
| 157 |
$node_from = node_load($node->field_from[0][nid]);
|
| 158 |
$node->content['issuer'] = array (
|
| 159 |
'#value' => theme('invoice_issuer',$node_from ),
|
| 160 |
'#title' => 'From',
|
| 161 |
'#type' => 'fieldset',
|
| 162 |
'#weight' => -9,
|
| 163 |
'#attributes' => array( 'class' => 'invoice-issuer'),
|
| 164 |
);
|
| 165 |
}
|
| 166 |
|
| 167 |
}
|
| 168 |
|
| 169 |
// Autogenerate number
|
| 170 |
if ($node->form_id == 'invoice_node_form' and $node->type == 'invoice') {
|
| 171 |
switch ($op) {
|
| 172 |
case 'insert': // after inserting invoice, increase id
|
| 173 |
if($node->field_pro_forma[0]['value'] == 'yes'){
|
| 174 |
$inv_no = variable_get('invoice_next_pf_id',1);
|
| 175 |
variable_set('invoice_next_pf_id',++$inv_no);
|
| 176 |
|
| 177 |
} else {
|
| 178 |
$inv_no = variable_get('invoice_next_id',invoices_get_last_id_from_db());
|
| 179 |
variable_set('invoice_next_id',++$inv_no);
|
| 180 |
}
|
| 181 |
break;
|
| 182 |
}
|
| 183 |
}
|
| 184 |
|
| 185 |
if ($node->type == 'invoice') {
|
| 186 |
switch ($op) {
|
| 187 |
case 'insert':
|
| 188 |
case 'update':
|
| 189 |
case 'submit':
|
| 190 |
|
| 191 |
if($node->field_pro_forma[0]['value'] == 'yes') { // value 'yes' - after optionwidgets patch
|
| 192 |
$node->title = "Pro Forma Invoice Number " .$node->field_invoice_number[0]['value'];
|
| 193 |
} else {
|
| 194 |
$node->title = "Invoice Number " .$node->field_invoice_number[0]['value'];
|
| 195 |
}
|
| 196 |
|
| 197 |
break;
|
| 198 |
}
|
| 199 |
}
|
| 200 |
|
| 201 |
}
|
| 202 |
|
| 203 |
function theme_bank_account($node_bank_account){
|
| 204 |
|
| 205 |
$node = node_build_content($node_bank_account);
|
| 206 |
$content = drupal_render($node->content);
|
| 207 |
$o .= $content ;
|
| 208 |
return $o;
|
| 209 |
}
|
| 210 |
|
| 211 |
function theme_client_details($node){
|
| 212 |
$node = node_build_content($node);
|
| 213 |
$content = drupal_render($node->content);
|
| 214 |
$o .= $content ;
|
| 215 |
return $o;
|
| 216 |
}
|
| 217 |
|
| 218 |
|
| 219 |
function theme_invoice_issuer($node){
|
| 220 |
$node = node_build_content($node);
|
| 221 |
$content = drupal_render($node->content);
|
| 222 |
$o .= $content ;
|
| 223 |
return $o;
|
| 224 |
}
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
/**
|
| 229 |
* hook_link implementation
|
| 230 |
*/
|
| 231 |
function invoices_link($type, &$node){
|
| 232 |
if ($type == 'node' and $node->type == 'invoice') {
|
| 233 |
if (user_access('create invoice_item content')){
|
| 234 |
$links['new_invoice_item']['title'] = 'New invoice item';
|
| 235 |
$links['new_invoice_item']['href'] = 'node/add/invoice-item/'. $node->nid;
|
| 236 |
// $links['new_invoice_item']['query'] = 'parent_invoice=' . $node->nid;
|
| 237 |
}
|
| 238 |
}
|
| 239 |
return $links;
|
| 240 |
}
|
| 241 |
|