stormexpense.module

Tracking 6.x-2.x branch
  1. drupal
    1. 6 contributions/storm/stormexpense/stormexpense.module

Functions & methods

NameDescription
stormexpense_access
stormexpense_access_sql
stormexpense_content_extra_fields
stormexpense_delete
stormexpense_form
stormexpense_help
stormexpense_insert
stormexpense_load
stormexpense_menu
stormexpense_nodeapi
stormexpense_node_info
stormexpense_perm
stormexpense_storminvoice_auto_add
stormexpense_stormorganization_change
stormexpense_stormproject_change
stormexpense_stormproject_change_hierarchy
stormexpense_stormtask_change
stormexpense_stormtask_change_hierarchy
stormexpense_stormticket_change
stormexpense_stormticket_change_hierarchy
stormexpense_storm_dashboard_links
stormexpense_storm_rewrite_where_sql
stormexpense_theme
stormexpense_update
stormexpense_view
stormexpense_views_api
_stormexpense_beforesave

File

View source
  1. <?php
  2. /**
  3. * @file
  4. */
  5. function stormexpense_help($path, $arg) {
  6. $o = '';
  7. switch ($path) {
  8. case "admin/help#stormexpense":
  9. $o = '<p>'. t("Provides expense support for Storm") .'</p>';
  10. break;
  11. }
  12. return $o;
  13. }
  14. function stormexpense_perm() {
  15. return array(
  16. 'Storm expense: access',
  17. 'Storm expense: add',
  18. 'Storm expense: delete all',
  19. 'Storm expense: delete own',
  20. 'Storm expense: delete of user organization',
  21. 'Storm expense: edit all',
  22. 'Storm expense: edit own',
  23. 'Storm expense: edit of user organization',
  24. 'Storm expense: view all',
  25. 'Storm expense: view own',
  26. 'Storm expense: view of user organization',
  27. );
  28. }
  29. function stormexpense_access($op, $node, $account=NULL) {
  30. if (empty($account)) {
  31. global $user;
  32. $account = $user;
  33. }
  34. if ($op == 'create') {
  35. return user_access('Storm expense: add');
  36. }
  37. if (is_numeric($node)) $node = node_load($node);
  38. if ($op == 'delete') {
  39. if (user_access('Storm expense: delete all')) {
  40. return TRUE;
  41. }
  42. elseif (user_access('Storm expense: delete own') && ($account->uid == $node->uid)) {
  43. return TRUE;
  44. }
  45. elseif (user_access('Storm expense: delete of user organization') && ($account->stormorganization_nid == $node->organization_nid)) {
  46. return TRUE;
  47. }
  48. }
  49. if ($op == 'update') {
  50. if (user_access('Storm expense: edit all')) {
  51. return TRUE;
  52. }
  53. elseif (user_access('Storm expense: edit own') && ($account->uid == $node->uid)) {
  54. return TRUE;
  55. }
  56. elseif (user_access('Storm expense: edit of user organization') && ($account->stormorganization_nid == $node->organization_nid)) {
  57. return TRUE;
  58. }
  59. }
  60. if ($op == 'view') {
  61. if (user_access('Storm expense: view all')) {
  62. return TRUE;
  63. }
  64. elseif (user_access('Storm expense: view own') && ($account->uid == $node->uid)) {
  65. return TRUE;
  66. }
  67. elseif (user_access('Storm expense: view of user organization') && ($account->stormorganization_nid == $node->organization_nid)) {
  68. return TRUE;
  69. }
  70. }
  71. return FALSE;
  72. }
  73. function stormexpense_access_sql($sql, $where = array()) {
  74. if (user_access('Storm expense: view all')) {
  75. $where[] = "'storm_access'='storm_access'";
  76. return storm_rewrite_sql($sql, $where);
  77. }
  78. global $user;
  79. $cond = '';
  80. if (user_access('Storm expense: view own')) {
  81. $cond .= 'n.uid='. $user->uid;
  82. }
  83. if (user_access('Storm expense: view of user organization')) {
  84. if (!empty($cond)) $cond .= ' OR ';
  85. $cond .= 'sex.organization_nid='. $user->stormorganization_nid;
  86. }
  87. if (empty($cond)) $cond = '0=1';
  88. $where[] = $cond;
  89. $where[] = "'storm_access'='storm_access'";
  90. return storm_rewrite_sql($sql, $where);
  91. }
  92. function stormexpense_storm_rewrite_where_sql($query, $primary_table, $account) {
  93. static $conds = array();
  94. if (isset($conds[$primary_table][$account->uid])) {
  95. return $conds[$primary_table][$account->uid];
  96. }
  97. if (preg_match("/'storm_access'='storm_access'/", $query)) {
  98. $cond = '';
  99. }
  100. else {
  101. if (user_access('Storm expense: view all', $account)) {
  102. return '';
  103. }
  104. $cond = '';
  105. if (user_access('Storm expense: view own', $account)) {
  106. $cond .= "${primary_table}.uid=". $account->uid;
  107. }
  108. if (user_access('Storm expense view of user organization', $account)) {
  109. if ($cond) {
  110. $cond .= ' OR ';
  111. }
  112. // If function is called without viewing an expense, this variable may not be set.
  113. // These lines check for that and set the organization node id to zero if not otherwise set.
  114. if (!isset($account->stormorganization_nid)) {
  115. $account->stormorganization_nid = 0;
  116. }
  117. $cond .= ' sex1.organization_nid='. $account->stormorganization_nid;
  118. }
  119. if ($cond) {
  120. $cond = " WHEN 'stormexpense' THEN (SELECT IF($cond,1,0) FROM {stormexpense} sex1 WHERE sex1.vid=${primary_table}.vid) ";
  121. }
  122. else {
  123. $cond = " WHEN 'stormexpense' THEN 0 ";
  124. }
  125. }
  126. $conds[$primary_table][$account->uid] = $cond;
  127. return $cond;
  128. }
  129. function stormexpense_menu() {
  130. $items = array();
  131. $items['storm/expenses'] = array(
  132. 'title' => 'Expenses',
  133. 'description' => 'Storm expenses',
  134. 'page callback' => 'stormexpense_list',
  135. 'access arguments' => array('Storm expense: access'),
  136. 'type' => MENU_NORMAL_ITEM,
  137. 'file' => 'stormexpense.admin.inc',
  138. 'weight' => 9,
  139. );
  140. $items['storm/expenses/report/%/%'] = array(
  141. 'title' => 'Expenses',
  142. 'description' => 'Storm expenses',
  143. 'page arguments' => array(3, 4),
  144. 'page callback' => 'stormexpense_list_report',
  145. 'access arguments' => array('Storm expense: access'),
  146. 'type' => MENU_CALLBACK,
  147. 'file' => 'stormexpense.admin.inc',
  148. );
  149. $items['storm/expenses/provider_autocomplete'] = array(
  150. 'title' => 'Provider autocomplete',
  151. 'page callback' => 'stormexpense_autocomplete',
  152. 'access arguments' => array('Storm expense: access'),
  153. 'type' => MENU_CALLBACK,
  154. 'file' => 'stormexpense.admin.inc',
  155. );
  156. return $items;
  157. }
  158. function stormexpense_theme() {
  159. return array(
  160. 'stormexpense_list' => array(
  161. 'file' => 'stormexpense.theme.inc',
  162. 'arguments' => array('header', 'tasks', 'duration'),
  163. ),
  164. 'stormexpense_view' => array(
  165. 'file' => 'stormexpense.theme.inc',
  166. 'arguments' => array('node', 'teaser', 'page'),
  167. ),
  168. 'stormexpense_list_form_report_reports' => array(
  169. 'file' => 'stormexpense.theme.inc',
  170. ),
  171. 'stormexpense_list_report' => array(
  172. 'file' => 'stormexpense.theme.inc',
  173. 'arguments' => array('report', 'language', 'timetrackings'),
  174. ),
  175. );
  176. }
  177. function stormexpense_node_info() {
  178. return array(
  179. 'stormexpense' => array(
  180. 'name' => t('Expense'),
  181. 'module' => 'stormexpense',
  182. 'description' => t("An expense for Storm."),
  183. 'title_label' => t("Title"),
  184. 'body_label' => t("Description"),
  185. )
  186. );
  187. }
  188. function stormexpense_content_extra_fields($type_name) {
  189. if ($type_name == 'stormexpense') {
  190. return array(
  191. 'group1' => array('label' => 'Organization/Project/Task/Ticket Group', 'weight' => -20),
  192. 'group2' => array('label' => 'Date/Provider Group', 'weight' => -19),
  193. 'group3' => array('label' => 'Amount', 'weight' => -18),
  194. 'group4' => array('label' => 'Tax Group', 'weight' => -17),
  195. );
  196. }
  197. }
  198. function stormexpense_stormorganization_change($organization_nid, $organization_title) {
  199. $s = "UPDATE {stormexpense} SET organization_title='%s' WHERE organization_nid=%d AND organization_title <> '%s'";
  200. db_query($s, $organization_title, $organization_nid, $organization_title);
  201. }
  202. function stormexpense_stormproject_change($project_nid, $project_title) {
  203. $s = "UPDATE {stormexpense} SET project_title='%s' WHERE project_nid=%d AND project_title <> '%s'";
  204. db_query($s, $project_title, $project_nid, $project_title);
  205. }
  206. function stormexpense_stormtask_change($task_nid, $task_title, $task_stepno) {
  207. $s = "UPDATE {stormexpense} SET task_title='%s', task_stepno='%s' WHERE task_nid=%d AND
  208. (task_title<>'%s' OR task_stepno<>'%s')";
  209. db_query($s, $task_title, $task_stepno, $task_nid, $task_title, $task_stepno);
  210. }
  211. function stormexpense_stormticket_change($ticket_nid, $ticket_title) {
  212. $s = "UPDATE {stormexpense} SET ticket_title='%s' WHERE ticket_nid=%d AND ticket_title <> '%s'";
  213. db_query($s, $ticket_title, $ticket_nid, $ticket_title);
  214. }
  215. function stormexpense_stormproject_change_hierarchy($project_nid, $organization_nid, $organization_title) {
  216. $s = "UPDATE {stormexpense} SET organization_nid=%d, organization_title='%s' WHERE project_nid=%d";
  217. db_query($s, $organization_nid, $organization_title, $project_nid);
  218. }
  219. function stormexpense_stormtask_change_hierarchy($task_nid, $organization_nid, $organization_title, $project_nid, $project_title) {
  220. $s = "UPDATE {stormexpense} SET organization_nid=%d, organization_title='%s', project_nid=%d, project_title='%s' WHERE task_nid=%d";
  221. db_query($s, $organization_nid, $organization_title, $project_nid, $project_title, $task_nid);
  222. }
  223. function stormexpense_stormticket_change_hierarchy($ticket_nid, $organization_nid, $organization_title, $project_nid, $project_title, $task_nid, $task_title) {
  224. $s = "UPDATE {stormexpense} SET organization_nid=%d, organization_title='%s', project_nid=%d, project_title='%s', task_nid=%d, task_title='%s' WHERE ticket_nid=%d";
  225. db_query($s, $organization_nid, $organization_title, $project_nid, $project_title, $task_nid, $task_title, $ticket_nid);
  226. }
  227. function stormexpense_form(&$node) {
  228. $breadcrumb = array();
  229. $breadcrumb[] = l(t('Storm'), 'storm');
  230. $breadcrumb[] = l(t('Expenses'), 'storm/expenses');
  231. drupal_set_breadcrumb($breadcrumb);
  232. if (arg(1)=='add') {
  233. if (array_key_exists('organization_nid', $_GET) && !$node->organization_nid) {
  234. $node->organization_nid = $_GET['organization_nid'];
  235. }
  236. if (array_key_exists('project_nid', $_GET) && !$node->project_nid) {
  237. $node->project_nid = $_GET['project_nid'];
  238. $p = node_load($node->project_nid);
  239. $node->organization_nid = $p->organization_nid;
  240. if (!stormorganization_access('view', $node->organization_nid)) {
  241. drupal_set_message(t("You cannot add an expense for this project, as you do not have access to view the organization's profile"));
  242. drupal_goto('node/'. $node->project_nid);
  243. }
  244. }
  245. if (array_key_exists('task_nid', $_GET) && !$node->task_nid) {
  246. $node->task_nid = $_GET['task_nid'];
  247. $t = node_load($node->task_nid);
  248. $node->organization_nid = $t->organization_nid;
  249. $node->project_nid = $t->project_nid;
  250. // $project_access deals with the case whereby the project could be blank, hence access rules not required
  251. $project_access = $node->project_nid ? stormproject_access('view', $node->project_nid) : TRUE;
  252. if (!stormorganization_access('view', $node->organization_nid) || !$project_access) {
  253. drupal_set_message(t("You cannot add an expense for this task, as you do not have access to view the both the organization and project's profile"));
  254. drupal_goto('node/'. $node->task_nid);
  255. }
  256. }
  257. if (array_key_exists('ticket_nid', $_GET) && !$node->ticket_nid) {
  258. $node->ticket_nid = $_GET['ticket_nid'];
  259. $t = node_load($node->ticket_nid);
  260. $node->organization_nid = $t->organization_nid;
  261. $node->project_nid = $t->project_nid;
  262. $node->task_nid = $t->task_nid;
  263. // $project_access deals with the case whereby the project could be blank, hence access rules not required
  264. $project_access = $node->project_nid ? stormproject('view', $node->project_nid) : TRUE;
  265. $task_access = $node->task_nid ? stormtask('view', $node->task_nid) : TRUE;
  266. if (!stormorganization_access('view', $node->organization_nid) || !project_access || !task_access) {
  267. drupal_set_message(t("You cannot add an expense for this ticket, as you do not have access to view all of the organization, project and task's profile"));
  268. drupal_goto('node/'. $node->ticket_nid);
  269. }
  270. }
  271. if (isset($_SESSION['stormexpense_list_filter']['organization_nid']) && (!isset($node->organization_nid))) {
  272. $node->organization_nid = $_SESSION['stormexpense_list_filter']['organization_nid'];
  273. }
  274. if (isset($_SESSION['stormexpense_list_filter']['project_nid']) && (!isset($node->project_nid))) {
  275. $node->project_nid = $_SESSION['stormexpense_list_filter']['project_nid'];
  276. }
  277. if (isset($_SESSION['stormexpense_list_filter']['task_nid']) && (!isset($node->task_nid))) {
  278. $node->task_nid = $_SESSION['stormexpense_list_filter']['task_nid'];
  279. }
  280. if (isset($_SESSION['stormexpense_list_filter']['ticket_nid']) && (!isset($node->ticket_nid))) {
  281. $node->ticket_nid = $_SESSION['stormexpense_list_filter']['ticket_nid'];
  282. }
  283. if (array_key_exists('organization_nid', $_GET)) $node->organization_nid = $_GET['organization_nid'];
  284. if (array_key_exists('project_nid', $_GET)) $node->project_nid = $_GET['project_nid'];
  285. if (array_key_exists('task_nid', $_GET)) $node->task_nid = $_GET['task_nid'];
  286. if (array_key_exists('ticket_nid', $_GET)) $node->ticket_nid = $_GET['ticket_nid'];
  287. $node->expensedate = time();
  288. $s_org = "SELECT n.nid, n.title FROM {stormorganization} so INNER JOIN {node} n
  289. ON so.nid=n.nid WHERE n.status=1 AND so.isactive=1 AND n.type='stormorganization' ORDER BY n.title";
  290. // Load tax defaults
  291. $node->tax1app = variable_get('storm_tax1_app', 1);
  292. $node->tax1percent = variable_get('storm_tax1_percent', 20);
  293. $node->tax2app = variable_get('storm_tax2_app', 0);
  294. $node->tax2percent = variable_get('storm_tax2_percent', 20);
  295. }
  296. else {
  297. $s_org = "SELECT n.nid, n.title FROM {stormorganization} so INNER JOIN {node} n
  298. ON so.nid=n.nid WHERE n.status=1 AND n.type='stormorganization' ORDER BY n.title";
  299. }
  300. // Transition to compound tax - allow for nodes which may have been saved in the past without a tax percentage
  301. if ((arg(2) == 'edit') && ($node->tax1percent == NULL)) {
  302. $node->tax1percent = $node->tax1 / $node->amount * 100;
  303. }
  304. $type = node_get_types('type', $node);
  305. $w = -100;
  306. $form['#attributes']['class'] = 'stormcomponent_node_form';
  307. $form['group1'] = array(
  308. '#type' => 'markup',
  309. '#theme' => 'storm_form_group',
  310. '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group1') : -20,
  311. );
  312. $s_org = stormorganization_access_sql($s_org);
  313. $s_org = db_rewrite_sql($s_org);
  314. $r = db_query($s_org);
  315. $organizations = array();
  316. if ($r->num_rows > 0) {
  317. while ($organization = db_fetch_object($r)) {
  318. $organizations[$organization->nid] = $organization->title;
  319. if (!isset($node->organization_nid)) {
  320. $node->organization_nid = $organization->nid;
  321. }
  322. }
  323. }
  324. $form['group1']['organization_nid'] = array(
  325. '#type' => 'select',
  326. '#title' => t('Organization'),
  327. '#default_value' => $node->organization_nid,
  328. '#options' => $organizations,
  329. '#required' => TRUE,
  330. '#attributes' => array('onchange' => "stormticket_organization_project_task_tickets(this, 'edit-project-nid', 'edit-task-nid', 'edit-ticket-nid', true, '-')"),
  331. );
  332. $s = "SELECT n.nid, n.title FROM {node} AS n INNER JOIN {stormproject} AS spr ON spr.vid=n.vid
  333. WHERE spr.organization_nid=%d AND n.status=1 AND n.type='stormproject' ORDER BY n.title";
  334. $s = stormproject_access_sql($s);
  335. $s = db_rewrite_sql($s);
  336. $r = db_query($s, $node->organization_nid);
  337. $projects = array();
  338. while ($project = db_fetch_object($r)) {
  339. $projects[$project->nid] = $project->title;
  340. }
  341. $projects = array(0 => '-') + $projects;
  342. $form['group1']['project_nid'] = array(
  343. '#type' => 'select',
  344. '#title' => t('Project'),
  345. '#default_value' => (isset($node->project_nid)) ? $node->project_nid : NULL,
  346. '#options' => $projects,
  347. '#process' => array('storm_dependent_select_process'),
  348. '#attributes' => array('onchange' => "stormticket_project_task_tickets(this, 'edit-organization-nid', 'edit-task-nid', 'edit-ticket-nid', true, '-')"),
  349. );
  350. $tree = _stormtask_get_tree((isset($node->project_nid)) ? $node->project_nid : NULL);
  351. $tasks = _stormtask_plain_tree($tree);
  352. $tasks = array(0 => '-') + $tasks;
  353. $form['group1']['task_nid'] = array(
  354. '#type' => 'select',
  355. '#title' => t('Task'),
  356. '#default_value' => (isset($node->task_nid)) ? $node->task_nid : NULL,
  357. '#options' => (isset($tasks)) ? $tasks : NULL,
  358. '#process' => array('storm_dependent_select_process'),
  359. '#attributes' => array('onchange' => "stormticket_task_tickets(this, 'edit-organization-nid', 'edit-project-nid', 'edit-ticket-nid', true, '-')"),
  360. );
  361. $tickets = array();
  362. $s = "SELECT n.nid, n.title FROM {node} AS n INNER JOIN {stormticket} AS sti ON sti.vid=n.vid WHERE n.status=1 AND n.type='stormticket'
  363. AND sti.organization_nid=%d AND sti.project_nid=%d AND sti.task_nid=%d ORDER BY n.title";
  364. $s = stormticket_access_sql($s);
  365. $s = db_rewrite_sql($s);
  366. if (isset($node->organization_nid) && (isset($node->project_nid))) {
  367. $r = db_query($s, $node->organization_nid, $node->project_nid, $node->task_nid);
  368. }
  369. while ($ticket = db_fetch_object($r)) {
  370. $tickets[$ticket->nid] = $ticket->title;
  371. }
  372. $form['group1']['ticket_nid'] = array(
  373. '#type' => 'select',
  374. '#title' => t('Ticket'),
  375. '#default_value' => (isset($node->ticket_nid)) ? $node->ticket_nid : NULL,
  376. '#options' => array(0 => '-') + $tickets,
  377. '#process' => array('storm_dependent_select_process'),
  378. );
  379. $form['group2'] = array(
  380. '#type' => 'markup',
  381. '#theme' => 'storm_form_group',
  382. '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group2') : -19,
  383. );
  384. $form['group2']['expensedate'] = array(
  385. '#type' => 'dateext',
  386. '#title' => t('Date'),
  387. '#default_value' => $node->expensedate,
  388. );
  389. $form['group2']['provider_title'] = array(
  390. '#type' => 'textfield',
  391. '#title' => t('Provider'),
  392. '#size' => 50,
  393. '#default_value' => (isset($node->provider_title)) ? $node->provider_title : NULL,
  394. '#autocomplete_path' => 'storm/expenses/provider_autocomplete',
  395. );
  396. $form['group3'] = array(
  397. '#type' => 'markup',
  398. '#theme' => 'storm_form_group',
  399. '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group3') : -18,
  400. );
  401. $form['group3']['amount'] = array(
  402. '#type' => 'textfield',
  403. '#title' => t('Amount'),
  404. '#default_value' => (isset($node->amount)) ? $node->amount : NULL,
  405. '#size' => 15,
  406. );
  407. $form['group4'] = array(
  408. '#type' => 'markup',
  409. '#theme' => 'storm_form_group',
  410. '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group4') : -17,
  411. );
  412. $form['group4']['tax1app'] = array(
  413. '#type' => 'select',
  414. '#title' => t('Tax 1 Application'),
  415. '#options' => array(
  416. 1 => t('Apply to item amount'),
  417. 0 => t('Do not apply tax'),
  418. ),
  419. '#default_value' => $node->tax1app,
  420. );
  421. $form['group4']['tax1percent'] = array(
  422. '#type' => 'textfield',
  423. '#title' => t('Tax 1 Percentage'),
  424. '#default_value' => $node->tax1percent,
  425. '#size' => 20,
  426. );
  427. $form['group4']['tax2app'] = array(
  428. '#type' => 'select',
  429. '#title' => t('Tax 2 Application'),
  430. '#options' => array(
  431. 2 => t('Apply to total of item amount plus previous tax'),
  432. 1 => t('Apply to item amount'),
  433. 0 => t('Do not apply tax'),
  434. ),
  435. '#default_value' => $node->tax2app,
  436. );
  437. $form['group4']['tax2percent'] = array(
  438. '#type' => 'textfield',
  439. '#title' => t('Tax 2 Percentage'),
  440. '#default_value' => $node->tax2percent,
  441. '#size' => 20,
  442. );
  443. if (!variable_get('storm_tax_display', TRUE)) {
  444. $form['group4']['#type'] = 'hidden';
  445. }
  446. if (!variable_get('storm_tax2_display', TRUE)) {
  447. $form['group4']['tax2app']['#type'] = 'hidden';
  448. $form['group4']['tax2percent']['#type'] = 'hidden';
  449. }
  450. $form['taxnotes'] = array(
  451. '#type' => 'markup',
  452. '#value' => t('Totals will be calculated automatically according to your tax selections.'),
  453. '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'group4') : -17,
  454. );
  455. $form['title'] = array(
  456. '#type' => 'textfield',
  457. '#title' => check_plain($type->title_label),
  458. '#required' => TRUE,
  459. '#default_value' => $node->title,
  460. '#weight' => module_exists('content') ? content_extra_field_weight($node->type, 'title') : -16,
  461. );
  462. if ($type->has_body) {
  463. $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
  464. }
  465. return $form;
  466. }
  467. // NODE MANIPULATION FUNCTIONS
  468. function stormexpense_insert($node) {
  469. _stormexpense_beforesave($node);
  470. db_query("INSERT INTO {stormexpense}
  471. (vid, nid, organization_nid, organization_title, project_nid, project_title,
  472. task_nid, task_title, task_stepno, ticket_nid, ticket_title,
  473. provider_nid, provider_title, expensedate, amount, tax1app, tax1percent,
  474. tax1, subtotal, tax2app, tax2percent, tax2, total) VALUES
  475. (%d, %d, %d, '%s', %d, '%s',
  476. %d, '%s', '%s', %d, '%s',
  477. %d, '%s', %d, %f, %d, %f,
  478. %f, %f, %d, %f, %f, %f)",
  479. $node->vid, $node->nid, $node->organization_nid, $node->organization_title, $node->project_nid, (isset($node->project_title)) ? $node->project_title : NULL,
  480. (isset($node->task_nid)) ? $node->task_nid : NULL, (isset($node->task_title)) ? $node->task_title : NULL, (isset($node->task_stepno)) ? $node->task_stepno : NULL, $node->ticket_nid, (isset($node->ticket_title)) ? $node->ticket_title : NULL,
  481. (isset($node->provider_nid)) ? $node->provider_nid : NULL, $node->provider_title, $node->expensedate, $node->amount, $node->tax1app, $node->tax1percent,
  482. $node->tax1, $node->subtotal, $node->tax2app, $node->tax2percent, $node->tax2, $node->total);
  483. }
  484. function stormexpense_update($node) {
  485. _stormexpense_beforesave($node);
  486. if ($node->revision) {
  487. stormexpense_insert($node);
  488. }
  489. else {
  490. db_query("UPDATE {stormexpense} SET
  491. organization_nid=%d, organization_title='%s', project_nid=%d, project_title='%s',
  492. task_nid=%d, task_title='%s', task_stepno='%s', ticket_nid=%d, ticket_title='%s',
  493. provider_nid=%d, provider_title='%s', expensedate=%d, amount=%f, tax1app=%d, tax1percent=%f,
  494. tax1=%f, subtotal=%f, tax2app=%d, tax2percent=%f, tax2=%f, total=%f WHERE vid = %d",
  495. $node->organization_nid, $node->organization_title, $node->project_nid, $node->project_title,
  496. $node->task_nid, $node->task_title, $node->task_stepno, $node->ticket_nid, $node->ticket_title,
  497. $node->provider_nid, $node->provider_title, $node->expensedate, $node->amount, $node->tax1app, $node->tax1percent,
  498. $node->tax1, $node->subtotal, $node->tax2app, $node->tax2percent, $node->tax2, $node->total, $node->vid);
  499. }
  500. }
  501. function _stormexpense_beforesave(&$node) {
  502. // Allow use of comma when inputting numerical values - str_replace with period decimal
  503. $node->amount = str_replace(',', '.', $node->amount);
  504. $node->tax1percent = str_replace(',', '.', $node->tax1percent);
  505. $node->tax2percent = str_replace(',', '.', $node->tax2percent);
  506. $node->expensedate = _storm_date_to_gmtimestamp($node->expensedate);
  507. storm_taxation($node);
  508. $s = "SELECT n.title FROM {node} AS n
  509. INNER JOIN {stormorganization} AS o ON n.nid=o.nid
  510. WHERE type='stormorganization' AND n.nid=%d";
  511. $r = db_query($s, $node->organization_nid);
  512. $o = db_fetch_object($r);
  513. $o ? $node->organization_title = $o->title : NULL;
  514. $s = "SELECT n.title, p.organization_title
  515. FROM {node} n INNER JOIN {stormproject} p ON n.nid=p.nid
  516. WHERE type='stormproject' AND n.nid=%d";
  517. $r = db_query($s, $node->project_nid);
  518. $p = db_fetch_object($r);
  519. $p ? $node->project_title = $p->title : NULL;
  520. $s = "SELECT title, stepno FROM {node} AS n INNER JOIN {stormtask} AS t ON n.vid=t.vid WHERE n.type='stormtask' AND n.nid=%d";
  521. (isset($node->task_nid)) ? $r = db_query($s, $node->task_nid) : NULL;
  522. $ta = db_fetch_object($r);
  523. (isset($ta->title)) ? $node->task_title = $ta->title : NULL;
  524. (isset($ta->stepno)) ? $node->task_stepno = $ta->stepno : NULL;
  525. $s = "SELECT title FROM {node} AS n INNER JOIN {stormticket} AS t ON n.vid=t.vid WHERE n.type='stormticket' AND n.nid=%d";
  526. (isset($node->ticket_nid)) ? $r = db_query($s, $node->ticket_nid) : NULL;
  527. $ti = db_fetch_object($r);
  528. (isset($ti->title)) ? $node->ticket_title = $ti->title : NULL;
  529. }
  530. function stormexpense_nodeapi(&$node, $op, $teaser, $page) {
  531. switch ($op) {
  532. case 'delete revision':
  533. // Notice that we're matching a single revision based on the node's vid.
  534. db_query('DELETE FROM {stormexpense} WHERE vid = %d', $node->vid);
  535. break;
  536. }
  537. }
  538. function stormexpense_delete($node) {
  539. db_query('DELETE FROM {stormexpense} WHERE nid = %d', $node->nid);
  540. }
  541. function stormexpense_load($node) {
  542. $additions = db_fetch_object(db_query('SELECT * FROM {stormexpense} WHERE vid = %d', $node->vid));
  543. return $additions;
  544. }
  545. function stormexpense_view($node, $teaser = FALSE, $page = FALSE) {
  546. return theme('stormexpense_view', $node, $teaser, $page);
  547. }
  548. // INVOICE AUTO ADD HANDLER
  549. function stormexpense_storminvoice_auto_add($node, $invoice_nid = NULL) {
  550. if (!module_exists('storminvoice')) {
  551. drupal_set_message(t('This function should only be called from within Storm Invoice'));
  552. return;
  553. }
  554. else {
  555. global $user;
  556. if (!$invoice_nid) {
  557. $new_invoice = new StdClass;
  558. // Code copied with edits from node form
  559. $new_invoice->requestdate = time();
  560. $new_invoice->duedate = $new_invoice->requestdate + (variable_get('storminvoice_payment_days', 30) * 86400);
  561. $s = "SELECT MAX(CAST(SUBSTRING_INDEX(sin.number, '/', 1) AS SIGNED)) FROM {node} n INNER JOIN {storminvoice} sin ON n.nid=sin.nid
  562. WHERE n.type='storminvoice' AND YEAR(FROM_UNIXTIME(sin.requestdate))=YEAR(FROM_UNIXTIME(%d))";
  563. $date = getdate($new_invoice->requestdate);
  564. $new_invoice->number = (db_result(db_query($s, $new_invoice->requestdate)) + 1) .'/'. $date['year'];
  565. $new_invoice->title = $node->title;
  566. $new_invoice->uid = $user->uid;
  567. $new_invoice->type = 'storminvoice';
  568. // $new_invoice->reference
  569. $new_invoice->organization_nid = $node->organization_nid;
  570. $new_invoice->organization_title = $node->organization_title;
  571. $new_invoice->project_nid = $node->project_nid;
  572. $new_invoice->project_title = $node->project_title;
  573. // $new_invoice->amount
  574. // $new_invoice->tax
  575. // $new_invoice->total
  576. // $new_invoice->totalcustomercurr
  577. // $new_invoice->taxexempt
  578. $new_invoice->src_nid = $node->nid;
  579. $new_invoice->src_vid = $node->vid;
  580. node_save($new_invoice);
  581. $invoice_nid = $new_invoice->nid;
  582. }
  583. else {
  584. $new_invoice = node_load($invoice_nid);
  585. }
  586. if ($node->ticket_nid) {
  587. $parent_ticket = node_load($node->ticket_nid);
  588. }
  589. elseif ($node->task_nid) {
  590. $parent_task = node_load($node->task_nid);
  591. }
  592. elseif ($node->project_nid) {
  593. $parent_project = node_load($node->project_nid);
  594. }
  595. else {
  596. $parent_organization = node_load($node->organization_nid);
  597. }
  598. $count = count($new_invoice->items);
  599. $new_invoice->items[$count]->description = $node->title;
  600. $new_invoice->items[$count]->amount = $node->amount;
  601. // Tax percent uses the values set on the expense.
  602. $new_invoice->items[$count]->tax1app = $node->tax1app;
  603. $new_invoice->items[$count]->tax1percent = $node->tax1percent;
  604. $new_invoice->items[$count]->tax1 = $node-> tax1;
  605. $new_invoice->items[$count]->tax2app = $node->tax2app;
  606. $new_invoice->items[$count]->tax2percent = $node->tax2percent;
  607. $new_invoice->items[$count]->tax2 = $node->tax2;
  608. $new_invoice->items[$count]->total = $node->total;
  609. $new_invoice->items[$count]->src_nid = $node->nid;
  610. $new_invoice->items[$count]->src_vid = $node->vid;
  611. // storm_taxation($new_invoice->items[$count]);
  612. storminvoice_update($new_invoice);
  613. }
  614. return $invoice_nid;
  615. }
  616. function stormexpense_views_api() {
  617. return array(
  618. 'api' => 2,
  619. 'path' => drupal_get_path('module', 'stormexpense'),
  620. );
  621. }
  622. function stormexpense_storm_dashboard_links($type) {
  623. $links = array();
  624. if ($type == 'page' || $type == 'block') {
  625. $links[] = array(
  626. 'theme' => 'storm_dashboard_link',
  627. 'title' => t('Expenses'),
  628. 'icon' => 'stormexpenses',
  629. 'path' => 'storm/expenses',
  630. 'params' => array(),
  631. 'access_arguments' => 'Storm expense: access',
  632. 'node_type' => 'stormexpense',
  633. 'add_type' => 'stormexpense',
  634. 'map' => array(),
  635. 'weight' => 11,
  636. );
  637. }
  638. return $links;
  639. }