You are here

function invoice_validate in Invoice 7

Same name and namespace in other branches
  1. 6 invoice.module \invoice_validate()

Implements hook_validate()

File

./invoice.module, line 747
Invoice module

Code

function invoice_validate($node, $form, &$form_state) {
  if ($node->op != t('Delete')) {

    // Count invoice items
    $count = db_query("SELECT COUNT(*) FROM {invoice_items} WHERE uid = :uid AND invoice_id = :invoice_id", array(
      'uid' => $GLOBALS['user']->uid,
      'invoice_id' => $node->invoice_number,
    ))
      ->fetchField();

    // Display an error if there are no invoice items
    if ($count == 0) {
      form_set_error('description', t('You have to fill in at least one invoice item!'));
    }
    if (empty($node->company_name) && empty($node->lastname)) {
      form_set_error('company_name', t('Company name and lastname may not both be empty!'));
    }
  }
  if ($node->op == t('Save')) {
    $possible_new_invoice_number = _invoice_get_new_invoice_number(true);

    // If user defined invoice number is higher than the new possible invoice number,
    // use the defined invoice number as the new invoice number
    if (isset($node->user_defined_invoice_number) && intval($node->user_defined_invoice_number) > 0) {
      if ($node->user_defined_invoice_number <= $possible_new_invoice_number) {
        form_set_error('user_defined_invoice_number', t('The user defined invoice number is not greater than the latest invoice number "@invoice_number"!', array(
          '@invoice_number' => $possible_new_invoice_number,
        )));
      }
    }
  }
}