| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Author: Bernard Szlachta
|
| 5 |
* Company: NobleProg Limited
|
| 6 |
* URL: www.nobleprog.co.uk
|
| 7 |
* bernard.szlachta (at) NobleProg.co.uk
|
| 8 |
*/
|
| 9 |
|
| 10 |
|
| 11 |
//TODO
|
| 12 |
/**
|
| 13 |
* 1. Secure double invoice generation
|
| 14 |
*
|
| 15 |
*/
|
| 16 |
|
| 17 |
|
| 18 |
/**
|
| 19 |
* The hook_menu implementation
|
| 20 |
*/
|
| 21 |
function invoice_pf_menu($may_cache){
|
| 22 |
if(! $may_cache){
|
| 23 |
$items[] = array(
|
| 24 |
'path' => 'node/' . arg(1) . '/gen_invoice' ,
|
| 25 |
'title' => t('Generate invoice from pro forma'),
|
| 26 |
'description' => t('Generate invoice from pro forma'),
|
| 27 |
'position' => 'right',
|
| 28 |
'weight' => -8,
|
| 29 |
'callback' => 'invoice_pf_gen_invoice',
|
| 30 |
'callback arguments' => array(arg(1)),
|
| 31 |
'access' => user_access('generate invoice from pro forma'),
|
| 32 |
'type' => MENU_CALLBACK,
|
| 33 |
);
|
| 34 |
}
|
| 35 |
return $items;
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
| 39 |
* The hook_link_alter implementation
|
| 40 |
*/
|
| 41 |
function invoice_pf_link_alter($node, & $links) {
|
| 42 |
global $user;
|
| 43 |
//We don't want to see any links when printing
|
| 44 |
if ($node->type == 'invoice' and
|
| 45 |
user_access('generate invoice from pro forma') and
|
| 46 |
! is_numeric($node->field_pro_forma_ref[0]['nid'])
|
| 47 |
) {
|
| 48 |
|
| 49 |
if ($user->uid && ($node->field_pro_forma[0]['value'] == 'yes') and
|
| 50 |
user_access('create invoice content')) { // value 'yes' - after optionwidgets patch
|
| 51 |
$links['gen_invoice_from_pf']['title'] = 'Generate invoice from pro-forma';
|
| 52 |
$links['gen_invoice_from_pf']['href'] = 'node/'.$node->nid.'/gen_invoice';
|
| 53 |
}
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
|
| 58 |
/**
|
| 59 |
* The hook_perm implementation
|
| 60 |
*/
|
| 61 |
function invoice_pf_perm(){
|
| 62 |
return array('generate invoice from pro forma');
|
| 63 |
}
|
| 64 |
|
| 65 |
/**
|
| 66 |
* This function generates invoice from pro forma
|
| 67 |
*/
|
| 68 |
function invoice_pf_gen_invoice($nid){
|
| 69 |
$node = node_load($nid);
|
| 70 |
if ($node->type == 'invoice' && $node->field_pro_forma[0]['value'] == 'yes'){
|
| 71 |
$new_node = clone $node;
|
| 72 |
|
| 73 |
unset( $new_node->nid,
|
| 74 |
$new_node->vid,
|
| 75 |
$new_node->title,
|
| 76 |
$new_node->created,
|
| 77 |
$new_node->changed
|
| 78 |
);
|
| 79 |
|
| 80 |
$new_node->field_pro_forma[0]['value'] = 'no';
|
| 81 |
|
| 82 |
$inv_no = variable_get('invoice_next_id', invoices_get_last_id_from_db() + 1);
|
| 83 |
$new_node->title = "Invoice Number " . $inv_no ;
|
| 84 |
|
| 85 |
$new_node->field_invoice_number[0]['value'] = $inv_no;
|
| 86 |
|
| 87 |
$new_node->field_issue_date[0]['value'] = substr(date('c'),0,-6);
|
| 88 |
|
| 89 |
$new_node->field_pro_forma_ref[0]['nid'] = $node->nid;
|
| 90 |
|
| 91 |
node_save($new_node);
|
| 92 |
|
| 93 |
if ($new_node->nid) {
|
| 94 |
variable_set('invoice_next_id',++$inv_no);
|
| 95 |
$link = l($new_node->title,"node/$new_node->nid");
|
| 96 |
drupal_set_message("Invoice has been genereated.($link)");
|
| 97 |
invoice_pf_copy_items($node->nid, $new_node->nid);
|
| 98 |
}
|
| 99 |
|
| 100 |
}
|
| 101 |
|
| 102 |
return '';
|
| 103 |
}
|
| 104 |
|
| 105 |
|
| 106 |
/**
|
| 107 |
* Copy items from invoice pro forma to newly genereated invoice
|
| 108 |
*/
|
| 109 |
function invoice_pf_copy_items($old_invoice_nid, $new_invoice_nid){
|
| 110 |
$items = invoice_pf_get_items($old_invoice_nid);
|
| 111 |
|
| 112 |
foreach($items as $item_nid){
|
| 113 |
$node = node_load($item_nid);
|
| 114 |
unset( $node->nid,
|
| 115 |
$node->vid,
|
| 116 |
$node->created,
|
| 117 |
$node->changed
|
| 118 |
);
|
| 119 |
$node->field_parent_invoice[0]['nid'] = $new_invoice_nid;
|
| 120 |
node_save($node);
|
| 121 |
}
|
| 122 |
}
|
| 123 |
|
| 124 |
/**
|
| 125 |
* Retrivies items of an invoice
|
| 126 |
* @param parent_invoice parent invoice nid
|
| 127 |
* @return array invoice items
|
| 128 |
*/
|
| 129 |
function invoice_pf_get_items($parent_invoice){
|
| 130 |
$result = db_query("SELECT nid FROM {content_type_invoice_item} WHERE field_parent_invoice_nid = $parent_invoice");
|
| 131 |
while($row = db_fetch_array($result)){
|
| 132 |
$items[] = $row['nid'];
|
| 133 |
}
|
| 134 |
return $items;
|
| 135 |
}
|