You are here

function invoice_validate in Invoice 6

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

Implementation of node_validate()

File

./invoice.module, line 604
Invoice module

Code

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

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

    // 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 (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,
        )));
      }
    }
  }
}