| 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 |
|
| 10 |
/**
|
| 11 |
* Sumarizes the invoice statistics
|
| 12 |
*/
|
| 13 |
function invoices_landing_page(){
|
| 14 |
$companies = node_get_nodes('company',1);
|
| 15 |
foreach ($companies as $company){
|
| 16 |
$statistics = invoices_stat_sums($company->nid);
|
| 17 |
$o .= theme('invoice_statistics_companies',$company,$statistics);
|
| 18 |
}
|
| 19 |
|
| 20 |
return $o;
|
| 21 |
}
|
| 22 |
|
| 23 |
function theme_invoice_statistics_companies($company,$statistics){
|
| 24 |
$o .= '<h2>' . $company->title . '</h2>';
|
| 25 |
$o .= theme('invoice_statistics',$statistics);
|
| 26 |
return $o;
|
| 27 |
|
| 28 |
}
|
| 29 |
function theme_invoice_statistics($statistics){
|
| 30 |
return theme('table',array("Name","Value"),$statistics);
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Generats statistics for each company
|
| 35 |
* @par integer company_id
|
| 36 |
* @return array with statists
|
| 37 |
*/
|
| 38 |
function invoices_stat_sums($company_id = 0){
|
| 39 |
$invoice_items = node_get_nodes('invoice_item',1);
|
| 40 |
$invoices = node_get_nodes('invoice',1);
|
| 41 |
|
| 42 |
$sum = 0;
|
| 43 |
$sum_paid =0;
|
| 44 |
foreach($invoice_items as $key => $item ){
|
| 45 |
|
| 46 |
$invoice = $invoices[$item->field_parent_invoice[0]['nid']];
|
| 47 |
if($invoice->field_pro_forma[0]['value'] != 'yes'){
|
| 48 |
if($company_id == 0 or $invoice->field_from[0]['nid'] == $company_id){
|
| 49 |
$sum += $item->field_item_price[0]['value'];
|
| 50 |
|
| 51 |
if($invoice->field_paid[0]['value'] == 'Yes'){
|
| 52 |
|
| 53 |
$sum_paid += $item->field_item_price[0]['value'];
|
| 54 |
}
|
| 55 |
}
|
| 56 |
}
|
| 57 |
}
|
| 58 |
$statistics['item_total'] = array('label' => "Total", 'value' => $sum);
|
| 59 |
$statistics['item_total_paid'] = array('label' => "Total Paid", 'value' => $sum_paid);
|
| 60 |
$statistics['item_total_notpaid'] = array('label' => "Total Not Paid", 'value' => $sum - $sum_paid);;
|
| 61 |
return $statistics;
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* Returns nodes of specific types and status
|
| 66 |
*/
|
| 67 |
function node_get_nodes($type,$status){
|
| 68 |
$result = db_query("SELECT nid FROM {node} WHERE type = '%s' AND status = %d",
|
| 69 |
$type, $status);
|
| 70 |
while($row = db_fetch_object($result)){
|
| 71 |
$node = node_load($row->nid);
|
| 72 |
$nodes[$row->nid] = $node;
|
| 73 |
}
|
| 74 |
return $nodes;
|
| 75 |
}
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
/**
|
| 81 |
* Implementation of hook_block().
|
| 82 |
**/
|
| 83 |
function invoices_block($op = 'list', $delta = 0, $edit = array()) {
|
| 84 |
switch ($op) {
|
| 85 |
case 'list':
|
| 86 |
$blocks[0]['info'] = t('Invoices');
|
| 87 |
return $blocks;
|
| 88 |
|
| 89 |
case 'view':
|
| 90 |
switch ($delta) {
|
| 91 |
case 0:
|
| 92 |
if(user_access('view invoice statistics')){
|
| 93 |
|
| 94 |
$l[] = l('Invoice list','invoices');
|
| 95 |
$l[] = l('Validate invoices','admin/nobleprog/invoices');
|
| 96 |
$l[] = l('Invoice statistcs','invoice_stats');
|
| 97 |
}
|
| 98 |
|
| 99 |
if(user_access('create invoice content')) {
|
| 100 |
$l[] = l('New Invoice','node/add/invoice');
|
| 101 |
}
|
| 102 |
|
| 103 |
if(user_access('create client content')) {
|
| 104 |
$l[] = l('New client','node/add/client');
|
| 105 |
}
|
| 106 |
|
| 107 |
if(user_access('create company content')) {
|
| 108 |
$l[] = l('New company','node/add/company');
|
| 109 |
}
|
| 110 |
|
| 111 |
if(user_access('create bank_account content')) {
|
| 112 |
$l[] = l('New bank account','node/add/bank-account');
|
| 113 |
}
|
| 114 |
|
| 115 |
$block['subject'] = "Invoices";
|
| 116 |
$block['content'] = theme('item_list',$l);
|
| 117 |
}
|
| 118 |
|
| 119 |
return $block;
|
| 120 |
}
|
| 121 |
}
|