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

'; break; } return $o; } function storminvoice_perm() { return array( 'Storm invoice: access', 'Storm invoice: add', 'Storm invoice: delete all', 'Storm invoice: delete own', 'Storm invoice: delete of user organization', 'Storm invoice: edit all', 'Storm invoice: edit own', 'Storm invoice: edit of user organization', 'Storm invoice: view all', 'Storm invoice: view own', 'Storm invoice: view of user organization', ); } function storminvoice_init() { if (function_exists('drupal_set_content')) { $settings = array( 'storm' => array( 'project_invoices_url' => url('storm/project_invoices_js/') ), ); drupal_add_js($settings, 'setting'); drupal_add_js(drupal_get_path('module', 'storminvoice') .'/storminvoice.js', 'module', 'header', FALSE); } } function storminvoice_access($op, $node) { global $user; $account = $user; if ($op == 'create') { return user_access('Storm invoice: add'); } if (is_numeric($node)) $node = node_load($node); if ($op == 'delete') { if (user_access('Storm invoice: delete all')) { return TRUE; } else if (user_access('Storm invoice: delete own') && ($account->uid == $node->uid)) { return TRUE; } else if (user_access('Storm invoice: delete of user organization') && ($account->stormorganization_nid == $node->organization_nid)) { return TRUE; } } if ($op == 'update') { if (user_access('Storm invoice: edit all')) { return TRUE; } else if (user_access('Storm invoice: edit own') && ($account->uid == $node->uid)) { return TRUE; } else if (user_access('Storm invoice: edit of user organization') && ($account->stormorganization_nid == $node->organization_nid)) { return TRUE; } } if ($op == 'view') { if (user_access('Storm invoice: view all')) { return TRUE; } else if (user_access('Storm invoice: view own') && ($account->uid == $node->uid)) { return TRUE; } else if (user_access('Storm invoice: view of user organization') && ($account->stormorganization_nid == $node->organization_nid)) { return TRUE; } } return FALSE; } function storminvoice_access_sql($sql, $where = array()) { if (user_access('Storm invoice: view all')) { $where[] = "'storm_access'='storm_access'"; return storm_rewrite_sql($sql, $where); } global $user; $cond = ''; if (user_access('Storm invoice: view own')) { $cond .= 'n.uid='. $user->uid; } if (user_access('Storm invoice: view of user organization')) { if (!empty($cond)) $cond .= ' OR '; $cond .= 'sin.organization_nid='. $user->stormorganization_nid; } if (empty($cond)) $cond = '0=1'; $where[] = $cond; $where[] = "'storm_access'='storm_access'"; return storm_rewrite_sql($sql, $where); } function storminvoice_storm_rewrite_where_sql($query, $primary_table, $account) { static $conds = array(); if ($conds[$account->uid]) { return $conds[$account->uid]; } if (preg_match("/'storm_access'='storm_access'/", $query)) { $cond = ''; } else { if (user_access('Storm invoice: view all', $account)) { return ''; } $cond = ''; if (user_access('Storm invoice: view own', $account)) { $cond .= "${primary_table}.uid=". $account->uid; } if (user_access('Storm invoice: view of user organization', $account)) { if ($cond) $cond .= ' OR '; $cond .= ' sin1.organization_nid='. $account->stormorganization_nid; } if ($cond) { $cond = " WHEN 'storminvoice' THEN (SELECT IF($cond,1,0) FROM {storminvoice} sin1 WHERE sin1.vid=${primary_table}.vid) "; } else { $cond = " WHEN 'storminvoice' THEN 0 "; } } $conds[$account->uid] = $cond; return $cond; } function storminvoice_menu($may_cache) { $items = array(); if (!$may_cache) { if (arg(0)=='storm' && arg(1)=='invoices') { require_once(drupal_get_path('module', 'storminvoice') .'/storminvoice.admin.inc'); require_once(drupal_get_path('module', 'storminvoice') .'/storminvoice.theme.inc'); } $items[] = array( 'path' => 'storm/invoices', 'title' => t('Invoices'), 'description' => t('Storm invoices'), 'callback' => 'storminvoice_list', 'access' => user_access('Storm invoice: access'), 'type' => MENU_NORMAL_ITEM, ); $items[] = array( 'path' => 'storm/project_invoices_js', 'title' => t('Tasks'), 'callback' => '_storminvoice_project_invoices_js', 'callback arguments' => array(arg(2), arg(3)), 'access' => user_access('Storm invoice: access'), 'type' => MENU_CALLBACK, ); $items[] = array( 'path' => 'storm/invoice/report', 'title' => t('Invoice'), 'description' => t('Storm Invoice'), 'callback' => 'storminvoice_report', 'callback arguments' => array(arg(3), arg(4), arg(5)), 'access arguments' => array('Storm invoice: access'), 'type' => MENU_CALLBACK, ); } return $items; } function storminvoice_node_info() { return array( 'storminvoice' => array( 'name' => t('Invoice'), 'module' => 'storminvoice', 'description' => t("An invoice for Storm."), 'title_label' => t("Description"), 'has_body' => false, ) ); } function storminvoice_storm_indexpage() { $o = array(); $o['storminvoice']['title'] = l(t('Invoices'), 'storm/invoices'); return $o; } function storminvoice_stormorganization_change($organization_nid, $organization_title) { $s = "UPDATE {storminvoice} SET organization_title='%s' WHERE organization_nid=%d AND organization_title <> '%s'"; db_query($s, $organization_title, $organization_nid, $organization_title); } function storminvoice_stormproject_change($project_nid, $project_title) { $s = "UPDATE {storminvoice} SET project_title='%s' WHERE project_nid=%d AND project_title <> '%s'"; db_query($s, $project_title, $project_nid, $project_title); } function storminvoice_form(&$node) { if (arg(1)=='add') { $node->requestdate = time(); $node->duedate = $node->requestdate + (variable_get('storminvoice_payment_days', 30) * 86400); $s = "SELECT MAX(CAST(SUBSTRING_INDEX(sin.number, '/', 1) AS SIGNED)) FROM {node} n INNER JOIN {storminvoice} sin ON n.nid=sin.nid WHERE n.type='storminvoice' AND YEAR(FROM_UNIXTIME(sin.requestdate))=YEAR(FROM_UNIXTIME(%d))"; $date = getdate($node->requestdate); $node->number = (db_result(db_query($s, $node->requestdate)) + 1) .'/'. $date['year']; if (array_key_exists('organization_nid', $_GET) && !$node->organization_nid) { $node->organization_nid = $_GET['organization_nid']; } if (array_key_exists('project_nid', $_GET) && !$node->project_nid) { $node->project_nid = $_GET['project_nid']; $p = node_load($node->project_nid); $node->organization_nid = $p->organization_nid; } if ($_SESSION['storminvoice_list_filter']['organization_nid'] && !$node->organization_nid) { $node->organization_nid = $_SESSION['storminvoice_list_filter']['organization_nid']; } if ($_SESSION['storminvoice_list_filter']['project_nid'] && !$node->project_nid) { $node->project_nid = $_SESSION['storminvoice_list_filter']['project_nid']; } } $type = node_get_types('type', $node); $form['group1'] = array( '#type' => 'markup', '#theme' => 'storm_form_group', '#weight' => -50, ); $form['group1']['number'] = array( '#type' => 'textfield', '#title' => t('Number'), '#required' => TRUE, '#size' => 10, '#default_value' => $node->number, ); $form['group1']['title'] = array( '#type' => 'textfield', '#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => $node->title, ); $form['group2'] = array( '#type' => 'markup', '#theme' => 'storm_form_group', '#weight' => -48, ); $s = "SELECT n.nid, n.title FROM {node} AS n INNER JOIN {stormorganization} AS sor ON sor.vid=n.vid WHERE n.status=1 AND n.type='stormorganization' ORDER BY n.title"; $s = stormorganization_access_sql($s); $s = db_rewrite_sql($s); $r = db_query($s); $organizations = array(); while ($organization = db_fetch_object($r)) { $organizations[$organization->nid] = $organization->title; if (!$node->organization_nid) $node->organization_nid = $organization->nid; } $form['group2']['organization_nid'] = array( '#type' => 'select', '#title' => t('Organization'), '#default_value' => $node->organization_nid, '#options' => $organizations, '#attributes' => array('onchange' => "stormproject_organization_projects(this, 'edit-project-nid', true, '-')"), ); $s = "SELECT n.nid, n.title FROM {node} AS n INNER JOIN {stormproject} AS spr ON spr.vid=n.vid WHERE spr.organization_nid=%d AND n.status=1 AND n.type='stormproject' ORDER BY n.title"; $s = stormproject_access_sql($s); $s = db_rewrite_sql($s); $r = db_query($s, $node->organization_nid); $projects = array(); while ($project = db_fetch_object($r)) { $projects[$project->nid] = $project->title; } $projects = array(0 => '-') + $projects; $form['group2']['project_nid'] = array( '#type' => 'select', '#title' => t('Project'), '#default_value' => $node->project_nid, '#options' => $projects, '#process' => array('storm_dependent_select_process' => array()), ); $form['group2']['reference'] = array( '#type' => 'textfield', '#title' => t('Reference'), '#default_value' => $node->reference, '#size' => 40, ); $form['group3'] = array( '#type' => 'markup', '#theme' => 'storm_form_group', '#weight' => -47, ); $form['group3']['requestdate'] = array( '#type' => 'date', '#title' => t('Request date'), '#default_value' => _storm_gmtimestamp_to_date($node->requestdate), ); $form['group3']['duedate'] = array( '#type' => 'dateext', '#withnull' => 'true', '#title' => t('Due date'), '#default_value' => _storm_gmtimestamp_to_date($node->duedate), ); $form['group3']['paymentdate'] = array( '#type' => 'dateext', '#withnull' => 'true', '#title' => t('Payment date'), '#default_value' => _storm_gmtimestamp_to_date($node->paymentdate), ); $form['group4'] = array( '#type' => 'markup', '#theme' => 'storm_form_group', '#weight' => -46, ); $form['group4']['amount'] = array( '#type' => 'textfield', '#title' => t('Amount'), '#attributes' => array('readonly' => 'readonly'), '#size' => 15, '#default_value' => $node->amount, ); $form['group4']['vat'] = array( '#type' => 'textfield', '#title' => t('VAT'), '#attributes' => array('readonly' => 'readonly'), '#size' => 15, '#default_value' => $node->vat, ); $form['group4']['total'] = array( '#type' => 'textfield', '#title' => t('Total'), '#attributes' => array('readonly' => 'readonly'), '#size' => 15, '#default_value' => $node->total, ); $form['group4']['totalcustomercurr'] = array( '#type' => 'textfield', '#title' => t('Total in customer currency'), '#size' => 15, '#default_value' => $node->totalcustomercurr, ); $form['vatexempt'] = array( '#type' => 'checkbox', '#title' => t('Vat exempt'), '#weight' => -45, '#default_value' => $node->vatexempt, ); return $form; } function storminvoice_insert($node) { _storminvoice_beforesave($node); db_query("INSERT INTO {storminvoice} (vid, nid, number, reference, organization_nid, organization_title, project_nid, project_title, amount, vat, total, totalcustomercurr, requestdate, duedate, paymentdate, vatexempt) VALUES (%d, %d, '%s', '%s', %d, '%s', %d, '%s', %f, %f, %f, %f, %d, %d, %d, %d)", $node->vid, $node->nid, $node->number, $node->reference, $node->organization_nid, $node->organization_title, $node->project_nid, $node->project_title, $node->amount, $node->vat, $node->total, $node->totalcustomercurr, $node->requestdate, $node->duedate, $node->paymentdate, $node->vatexempt); } function storminvoice_update($node) { _storminvoice_beforesave($node); db_query("UPDATE {storminvoice} SET number='%s', reference='%s', organization_nid=%d, organization_title='%s', project_nid=%d, project_title='%s', amount=%f, vat=%f, total=%f, totalcustomercurr=%f, requestdate=%d, duedate=%d, paymentdate=%d, vatexempt=%d WHERE vid = %d", $node->number, $node->reference, $node->organization_nid, $node->organization_title, $node->project_nid, $node->project_title, $node->amount, $node->vat, $node->total, $node->totalcustomercurr, $node->requestdate, $node->duedate, $node->paymentdate, $node->vatexempt, $node->vid); } function _storminvoice_beforesave(&$node) { if (is_array($node->requestdate)) $node->requestdate = _storm_date_to_gmtimestamp($node->requestdate); if (is_array($node->duedate)) $node->duedate = _storm_date_to_gmtimestamp($node->duedate); if (is_array($node->paymentdate)) $node->paymentdate = _storm_date_to_gmtimestamp($node->paymentdate); $s = "SELECT n.title FROM {node} AS n INNER JOIN {stormorganization} AS o ON n.vid=o.vid WHERE type='stormorganization' AND n.nid=%d"; $r = db_query($s, $node->organization_nid); $o = db_fetch_object($r); $node->organization_title = $o->title; $s = "SELECT n.title FROM {node} AS n INNER JOIN {stormproject} AS p ON n.vid=p.vid WHERE type='stormproject' AND n.nid=%d"; $r = db_query($s, $node->project_nid); $p = db_fetch_object($r); $node->project_title = $p->title; if (!$node->totalcustomercurr) { $node->totalcustomercurr = $node->total; } } function storminvoice_delete($node) { db_query('DELETE FROM {storminvoiceitem} WHERE invoice_nid = %d', $node->nid); db_query('DELETE FROM {storminvoice} WHERE nid = %d', $node->nid); } function storminvoice_load($node) { $additions = db_fetch_object(db_query('SELECT * FROM {storminvoice} WHERE vid = %d', $node->vid)); return $additions; } function storminvoice_view($node, $teaser = FALSE, $page = FALSE, $links = TRUE) { require_once(drupal_get_path('module', 'storminvoice') .'/storminvoice.theme.inc'); return theme('storminvoice_view', $node, $teaser, $page, $links); } function storminvoice_form_alter($form_id, &$form) { if ($form_id == 'storm_admin_settings') { $form['storminvoice'] = array( '#type' => 'fieldset', '#title' => t('Invoices'), '#collapsed' => false, '#collapsible' => true, '#weight' => -9, ); $form['storminvoice']['storminvoice_payment_days'] = array( '#type' => 'textfield', '#title' => t('Number of days for invoice payment'), '#default_value' => variable_get('storminvoice_payment_days', 30), '#description' => t('Default number of days for invoice payment'), '#size' => 5, ); $form['storminvoice']['storminvoice_payment_modes'] = array( '#type' => 'textarea', '#title' => t('Modes for invoice payment'), '#default_value' => variable_get('storminvoice_payment_modes', ''), '#description' => t('Modes for invoice payment'), ); $form['storminvoice']['storminvoice_payment_terms'] = array( '#type' => 'textfield', '#title' => t('Payment terms'), '#default_value' => variable_get('storminvoice_payment_terms', 'Due on receipt'), '#description' => t('Payment terms'), '#size' => 50, ); } }