| 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 |
* Convert date from ISO (yyyy-mm-ddT00:00:00) to unixtime (used date module)
|
| 12 |
* @param date in ISO format
|
| 13 |
* @return date in unix format
|
| 14 |
*/
|
| 15 |
function conv_date_iso2unix($date) {
|
| 16 |
if (module_exists('date') && file_exists(drupal_get_path('module', 'date_api').'/date.inc')) {
|
| 17 |
include_once(drupal_get_path('module', 'date_api') .'/date.inc');
|
| 18 |
return date_iso2unix($date);
|
| 19 |
} else {
|
| 20 |
drupal_set_message(__FUNCTION__.'(): Error! Check your date module!','error');
|
| 21 |
return NULL;
|
| 22 |
}
|
| 23 |
}
|
| 24 |
|
| 25 |
function invoices_validate() {
|
| 26 |
$result = db_query("SELECT nid FROM {node} WHERE type = 'invoice'");
|
| 27 |
while ( ($nid = db_fetch_object($result)) ? $nids[] = $nid : ''); // save selected nodes to array
|
| 28 |
|
| 29 |
// FIRST STEP VALIDATION (check if numbers and dates are not empty)
|
| 30 |
$invoices = array();
|
| 31 |
$maxnr = 0;
|
| 32 |
foreach ($nids as $nid) {
|
| 33 |
if (is_numeric($nid->nid)) {
|
| 34 |
$node = node_load($nid->nid);
|
| 35 |
$nr = isset($node->field_invoice_number[0]) ? implode('',$node->field_invoice_number[0]) : NULL;
|
| 36 |
$issue_date = isset($node->field_issue_date[0]) ? conv_date_iso2unix($node->field_issue_date[0]['value']) : NULL;
|
| 37 |
$nlink = l('node/'.$node->nid,'node/'.$node->nid).' '.l('(edit)','node/'.$node->nid.'/edit');
|
| 38 |
if (empty($nr)) {
|
| 39 |
drupal_set_message(t('Empty invoice number! - ').$nlink);
|
| 40 |
} else if (!is_numeric($nr)) {
|
| 41 |
drupal_set_message(t('Invoice number is not numeric! - ')." is '$nr' - ".$nlink);
|
| 42 |
} else {
|
| 43 |
if (!isset($invoices[$nr])) {
|
| 44 |
$invoices[$nr] = Array($node->nid, $issue_date); // add correct invoices to the list for the second validation
|
| 45 |
} else {
|
| 46 |
$nid = $invoices[$nr][0]; // get duplicated nid
|
| 47 |
$nlink2 = l('node/'.$nid,'node/'.$nid).' '.l('(edit)','node/'.$nid.'/edit');
|
| 48 |
drupal_set_message(t('Duplicated invoices! - ')." no:'$nr' - ".$nlink.' conflicting with '.$nlink2);
|
| 49 |
}
|
| 50 |
if ($nr>$maxnr) $maxnr = $nr; // update nr of last invoice
|
| 51 |
}
|
| 52 |
if (empty($date)) {
|
| 53 |
drupal_set_message(t('Empty issue date in invoice! - ').$nlink);
|
| 54 |
} else if (!is_string($nr)) {
|
| 55 |
drupal_set_message(t('Invalid date of issue in invoice! - ')." is '$nr' - ".$nlink);
|
| 56 |
}
|
| 57 |
}
|
| 58 |
}
|
| 59 |
|
| 60 |
// SECOND STEP VALIDATION (check order of invoices and correct order of dates)
|
| 61 |
$currdate = $invoices[1][1]; // starting unix time
|
| 62 |
$lastnr = 0; // last checked invoice
|
| 63 |
for ($nr = 1; $nr<$maxnr+1; $nr++) { // check from number 1 to last invoice
|
| 64 |
if (!isset($invoices[$nr])) { // if there is no such invoice
|
| 65 |
drupal_set_message(t('There is no invoice nr')." = $nr");
|
| 66 |
} else {
|
| 67 |
$nid = $invoices[$nr][0];
|
| 68 |
$issue_date = !empty($invoices[$nr][1]) ? $invoices[$nr][1] : $currdate;
|
| 69 |
if ($issue_date<$currdate) { // check if date time is after than date of last checked invoice
|
| 70 |
$nlink = l('node/'.$nid,'node/'.$nid).' '.l('(edit)','node/'.$nid.'/edit');
|
| 71 |
drupal_set_message("Invoice nr $nr has date before than invoice nr $lastnr! - ".$nlink);
|
| 72 |
$lastnr = $nr; // save this invoice as last
|
| 73 |
}
|
| 74 |
}
|
| 75 |
}
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Return last number of invoice from database
|
| 80 |
*
|
| 81 |
* @param
|
| 82 |
* @return integer Last id number of invoice
|
| 83 |
*/
|
| 84 |
|
| 85 |
function invoices_get_last_id_from_db() {
|
| 86 |
$res = db_result(db_query("SELECT MAX(field_invoice_number_value) FROM {content_type_invoice}"));
|
| 87 |
if (!is_numeric($res)) {
|
| 88 |
$res = 0;
|
| 89 |
drupal_set_message(__FUNCTION__."(): Can't find last invoice number!",'error');
|
| 90 |
}
|
| 91 |
return $res;
|
| 92 |
}
|
| 93 |
|