'. t("Provides invoice item support for Storm") .'

'; break; } return $o; } function storminvoiceitem_perm() { return array( 'Storm invoice item: access', 'Storm invoice item: add', 'Storm invoice item: delete all', 'Storm invoice item: delete own', 'Storm invoice item: edit all', 'Storm invoice item: edit own', 'Storm invoice item: view all', 'Storm invoice item: view own', ); } function storminvoiceitem_init() { if (function_exists('drupal_set_content')) { drupal_add_js(drupal_get_path('module', 'storminvoiceitem') .'/storminvoiceitem.js', 'module', 'header', FALSE); } } function storminvoiceitem_access($op, $node) { global $user; $account= $user; if (is_numeric($node)) $node = node_load($node); if ($op == 'create') { return user_access('Storm invoice item: add'); } if ($op == 'delete') { if (user_access('Storm invoice item: delete all')) { return TRUE; } else if (user_access('Storm invoice item: delete own') && ($account->uid == $node->uid)) { return TRUE; } } if ($op == 'update') { if (user_access('Storm invoice item: edit all')) { return TRUE; } else if (user_access('Storm invoice item: edit own') && ($account->uid == $node->uid)) { return TRUE; } } if ($op == 'view') { if (user_access('Storm invoice item: view all')) { return TRUE; } else if (user_access('Storm invoice item: view own') && ($account->uid == $node->uid)) { return TRUE; } } return FALSE; } function storminvoiceitem_access_sql($sql, $where = array()) { if (user_access('Storm invoice item: view all')) { $where[] = "'storm_access'='storm_access'"; return storm_rewrite_sql($sql, $where); } global $user; $cond = ''; if (user_access('Storm invoice item: view own')) { $cond .= 'n.uid='. $user->uid; } if (empty($cond)) $cond = '0=1'; $where[] = $cond; $where[] = "'storm_access'='storm_access'"; return storm_rewrite_sql($sql, $where); } function storminvoiceitem_storm_rewrite_where_sql($query, $primary_table, $account) { static $conds = array(); if (!$account) { global $user; $account = $user; } if ($conds[$account->uid]) { return $conds[$account->uid]; } if (preg_match("/'storm_access'='storm_access'/", $query)) { $cond = ''; } else { if (user_access('Storm invoice item: view all', $account)) { return ''; } $cond = ''; if (user_access('Storm invoice item: view own', $account)) { $cond .= "${primary_table}.uid=". $account->uid; } if ($cond) { $cond = "WHEN 'storminvoiceitem' THEN (SELECT IF($cond,1,0) FROM {storminvoiceitem} sit1 WHERE sit1.vid=${primary_table}.vid) "; } else { $cond = "WHEN 'storminvoiceitem' THEN 0 "; } } $conds[$account->uid] = $cond; return $cond; } function storminvoiceitem_menu($may_cache) { $items = array(); if (!$may_cache) { if (arg(0)=='node' && is_numeric(arg(1)) && arg(2) == 'invoiceitems') { require_once(drupal_get_path('module', 'storminvoiceitem') .'/storminvoiceitem.admin.inc'); require_once(drupal_get_path('module', 'storminvoiceitem') .'/storminvoiceitem.theme.inc'); } $items[] = array( 'path' => 'node/'. arg(1) .'/invoiceitems', 'title' => t('Invoice items'), 'callback' => 'storminvoiceitem_invoiceitems', 'callback arguments' => array(arg(1)), 'access' => _storminvoiceitem_invoiceitems_access(arg(1)), 'type' => MENU_LOCAL_TASK, 'weight' => 3 ); } return $items; } function storminvoiceitem_node_info() { return array( 'storminvoiceitem' => array( 'name' => t('Invoice item'), 'module' => 'storminvoiceitem', 'description' => t("An invoice item for Storm."), 'title_label' => t("Description"), 'has_body' => false, ) ); } function storminvoiceitem_form(&$node) { if (arg(1)=='add') { if (array_key_exists('invoice_nid', $_GET) && !$node->invoice_nid) { $node->invoice_nid = $_GET['invoice_nid']; } else { drupal_set_message(t("It's impossible to create an item without an invoice. You need to select an invoice before.")); drupal_goto('storm/invoices'); } } $type = node_get_types('type', $node); $form['group1'] = array( '#type' => 'markup', '#theme' => 'storm_form_group', '#weight' => -50, ); $form['group1']['invoice_nid'] = array( '#type' => 'hidden', '#default_value' => $node->invoice_nid, ); $form['group2'] = array( '#type' => 'markup', '#theme' => 'storm_form_group', '#weight' => -48, ); $form['group2']['title'] = array( '#type' => 'textfield', '#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => $node->title, '#size' => 80, ); $form['group2']['weight'] = array( '#type' => 'weight', '#title' => t('Weight'), '#default_value' => $node->weight, ); $form['group3'] = array( '#type' => 'markup', '#theme' => 'storm_form_group', '#weight' => -48, ); $form['group3']['amount'] = array( '#type' => 'textfield', '#withnull' => 'true', '#title' => t('Amount'), '#size' => 15, '#default_value' => $node->amount, ); $form['group3']['vatpercent'] = array( '#type' => 'textfield', '#withnull' => 'true', '#title' => t('VAT percent'), '#size' => 15, '#default_value' => $node->vatpercent, ); $form['group3']['vat'] = array( '#type' => 'textfield', '#withnull' => 'true', '#title' => t('VAT'), '#size' => 15, '#default_value' => $node->vat, ); $form['group3']['total'] = array( '#type' => 'textfield', '#withnull' => 'true', '#title' => t('Total'), '#size' => 15, '#default_value' => $node->total, ); return $form; } function storminvoiceitem_insert($node) { _storminvoiceitem_beforesave($node); db_query("INSERT INTO {storminvoiceitem} (vid, nid, invoice_nid, amount, taxpercent, tax, total, weight) VALUES (%d, %d, %d, %f, %f, %f, %f, %d)", $node->vid, $node->nid, $node->invoice_nid, $node->amount, $node->taxpercent, $node->tax, $node->total, $node->weight); _storminvoiceitem_aftersave($node); } function storminvoiceitem_update($node) { _storminvoiceitem_beforesave($node); db_query("UPDATE {storminvoiceitem} SET invoice_nid=%d, amount=%f, vatpercent=%f, vat=%f, total=%f, weight=%d WHERE vid = %d", $node->invoice_nid, $node->amount, $node->vatpercent, $node->vat, $node->total, $node->weight, $node->vid); _storminvoiceitem_aftersave($node); } function _storminvoiceitem_beforesave(&$node) { if (!$node->vat) { $node->vat = $node->amount * $node->vatpercent * .01; } if (!$node->total) { $node->total = $node->amount + $node->vat; } } function _storminvoiceitem_aftersave(&$node) { $s = "SELECT SUM(amount) tamount, SUM(vat) tvat, SUM(total) ttotal FROM {storminvoiceitem} WHERE invoice_nid=%d"; $r = db_query($s, $node->invoice_nid); $t = db_fetch_object($r); $invoice = node_load($node->invoice_nid); $invoice->amount = $t->tamount; $invoice->vat = $t->tvat; $invoice->total = $t->ttotal; storminvoice_update($invoice); } function storminvoiceitem_delete($node) { // Notice that we're matching all revision, by using the node's nid. db_query('DELETE FROM {storminvoiceitem} WHERE nid = %d', $node->nid); _storminvoiceitem_aftersave($node); } function storminvoiceitem_load($node) { $additions = db_fetch_object(db_query('SELECT * FROM {storminvoiceitem} WHERE vid = %d', $node->vid)); $additions->stepno_old = $node->stepno; $additions->title_old = $node->title; return $additions; } function storminvoiceitem_validate(&$node) { if (($node->nid == $node->parent_nid) && $node->parent_nid) { form_set_error('parent_nid', t('Impossible to assign itself as parent.')); } } function storminvoiceitem_view($node, $teaser = FALSE, $page = FALSE) { $node = node_prepare($node, $teaser); return $node; } function _storminvoiceitem_invoiceitems_access($nid) { $node = NULL; if ($nid && is_numeric($nid)) $node = node_load($nid); if ($node == NULL) return FALSE; if ($node->type == 'storminvoice' && user_access('Storm invoice item: access')) return TRUE; return FALSE; } function storminvoiceitem_getitems($invoice_nid) { $s = "SELECT n.*, sit.* FROM {node} AS n INNER JOIN {storminvoiceitem} AS sit ON n.vid=sit.vid WHERE n.status=1 AND n.type='storminvoiceitem' AND sit.invoice_nid = %d"; $s = storminvoiceitem_access_sql($s); $s = db_rewrite_sql($s); $r = db_query($s, $invoice_nid); $items = array(); while ($i = db_fetch_object($r)) { $items[] = $i; } return $items; }