You are here

function invoice_update in Invoice 6

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

Implementation of hook_update()

File

./invoice.module, line 689
Invoice module

Code

function invoice_update($node) {
  $user_id = $node->uid;

  // Only whith the permission "administer invoices" you are allowed to change invoices
  // created by other users.
  if (user_access('administer invoices')) {
    $accessGranted = TRUE;
  }
  else {

    // Make sure that this invoice belongs to this user
    $count = db_result(db_query("SELECT COUNT(*) FROM {invoice_invoices} WHERE iid=%d AND uid=%d", $node->invoice_number, $user_id));
    $accessGranted = $count > 0 ? TRUE : FALSE;
  }
  if ($accessGranted) {

    // Get template ID
    $tid = db_result(db_query("SELECT tid FROM {invoice_templates} WHERE name='%s'", $node->template));

    // Update invoice
    db_query("UPDATE {invoice_invoices} SET leading_zeros=%d, prefix='%s', description='%s', tid='%s', pay_limit=%d, uid=%d WHERE iid=%d", $node->invoice_invoice_number_zerofill, $node->invoice_invoice_number_prefix, $node->invoice_description, $tid, $node->pay_limit, $user_id, $node->invoice_number);

    // Update customers
    db_query("UPDATE {invoice_customers} SET customer_number=%d, company_name='%s', firstname='%s', lastname='%s', street='%s', building_number='%s',\n      zipcode='%s', city='%s', country='%s', coc_number='%s', vat_number='%s', description='%s'  \n      WHERE invoice_id=%d", $node->customer_number, $node->company_name, $node->firstname, $node->lastname, $node->street, $node->building_number, $node->zipcode, $node->city, $node->country, $node->coc_number, $node->vat_number, $node->description, $node->invoice_number);

    // It's not needed to update invoice item data because they are directly updated by every AJAX call.
    // However it's possible for a user whith the "administer invoices" permission to change the author
    db_query("UPDATE {invoice_items} SET uid=%d WHERE invoice_id=%d", $user_id, $node->invoice_number);
  }
  else {
    drupal_set_message(t('You are not the owner of this invoice!'), 'error');
  }
  db_query("UPDATE {node} SET promote=0 WHERE type='invoice' AND nid=%d", $node->nid);
  unset($_SESSION['invoice_template']);
}