function invoice_update in Invoice 7
Same name and namespace in other branches
- 6 invoice.module \invoice_update()
Implements hook_update()
File
- ./
invoice.module, line 865 - 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 (isset($GLOBALS['invoice_api']) && true === $GLOBALS['invoice_api'] || user_access('administer invoices')) {
$accessGranted = TRUE;
}
else {
// Make sure that this invoice belongs to this user
$count = db_query("SELECT COUNT(*) FROM {invoice_invoices} WHERE iid = :iid AND uid = :uid", array(
':iid' => $node->invoice_number,
':uid' => $user_id,
))
->fetchField();
$accessGranted = $count > 0 ? TRUE : FALSE;
}
if ($accessGranted) {
// Get template ID
$tid = db_query("SELECT tid FROM {invoice_templates} WHERE name = :name", array(
':name' => $node->template,
))
->fetchField();
// Set pay status
$pay_status = isset($node->pay_status) && 'paid' == $node->pay_status ? 'paid' : 'unpaid';
// Update invoice
db_update('invoice_invoices')
->fields(array(
'leading_zeros' => $node->invoice_invoice_number_zerofill,
'prefix' => $node->invoice_invoice_number_prefix,
'description' => $node->invoice_description,
'tid' => $tid,
'pay_limit' => $node->pay_limit,
'pay_status' => $pay_status,
'uid' => $user_id,
))
->condition('iid', $node->invoice_number)
->execute();
// Update customers
db_update('invoice_customers')
->fields(array(
'customer_number' => $node->customer_number,
'company_name' => $node->company_name,
'firstname' => $node->firstname,
'lastname' => $node->lastname,
'street' => $node->street,
'building_number' => $node->building_number,
'zipcode' => $node->zipcode,
'city' => $node->city,
'state' => $node->state,
'country' => $node->country,
'coc_number' => $node->coc_number,
'vat_number' => $node->vat_number,
'description' => $node->description,
))
->condition('invoice_id', $node->invoice_number)
->execute();
// Invoice items
if (isset($GLOBALS['invoice_api']) && true === $GLOBALS['invoice_api']) {
if (count($node->invoice_items) > 0) {
// Delete existing invoice items
db_delete('invoice_items')
->condition('invoice_id', $node->invoice_number)
->execute();
// Insert new existing invoice items
foreach ($node->invoice_items as $item) {
// Round the price to 3 decimals
$unitcost = round($item['unitcost'], 3);
// Insert invoice item into the invoice items table
$lastInsertId = db_insert('invoice_items')
->fields(array(
'description' => $item['description'],
'vat' => (double) $item['vat'],
'quantity' => (double) $item['quantity'],
'unitcost' => (double) $unitcost,
'weight' => (int) $item['weight'],
'invoice_id' => $node->invoice_number,
'uid' => $user_id,
'created' => time(),
))
->execute();
}
}
}
else {
// 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_update('invoice_items')
->fields(array(
'uid' => $user_id,
))
->condition('invoice_id', $node->invoice_number)
->execute();
}
}
else {
drupal_set_message(t('You are not the owner of this invoice!'), 'error');
}
db_update('node')
->fields(array(
'promote' => 0,
))
->condition('type', 'invoice')
->condition('nid', $node->nid)
->execute();
unset($_SESSION['invoice_template']);
}