You are here

function invoice_nodeapi in Invoice 6

Implementation of hook_nodeapi()

Parameters

object $node:

string $op:

string $teaser:

string $page:

File

./invoice.module, line 349
Invoice module

Code

function invoice_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case "presave":
      if ($node->type == 'invoice') {

        // If true we are creating a new invoice
        if (intval($node->invoice_number) == 0) {

          // Get new invoice number
          if (intval($node->user_defined_invoice_number) > 0) {
            $node->invoice_number = $node->user_defined_invoice_number;
          }
          else {
            $node->invoice_number = _invoice_get_new_invoice_number();
          }
        }

        // Save the title, this must happen when creating AND editing a node because otherwise
        // the pathauto module will give an error
        if (intval($node->invoice_number) > 0) {
          $node->title = t('Invoice') . ' #' . _invoice_get_formatted_invoice_number($node->invoice_number, $node);
        }

        // Get customer number
        if (!empty($node->company_name)) {
          $customer_number = db_result(db_query("SELECT customer_number FROM {invoice_customers}\n            WHERE company_name='%s' AND country='%s' LIMIT 1", $node->company_name, $node->country));
        }
        elseif (!empty($node->lastname)) {
          $customer_number = db_result(db_query("SELECT customer_number FROM {invoice_customers}\n            WHERE lastname='%s' AND zipcode='%s' AND building_number='%s' LIMIT 1", $node->lastname, $node->zipcode, $node->building_number));
        }

        // If customer number is still empty, get a new one
        if (empty($customer_number)) {
          $customer_number = 1 + db_result(db_query("SELECT MAX(customer_number) FROM {invoice_customers}"));
        }

        // Add customer number to the node object
        $node->customer_number = $customer_number;
      }
      break;
  }
}