/packages/intranet-timesheet2-invoices/sql/postgresql/upgrade/upgrade-3.4.0.1.0-3.4.0.2.0.sql

https://github.com/cognovis/project-open · SQL · 91 lines · 80 code · 9 blank · 2 comment · 0 complexity · 81f328d94b6bfeb1c73fb77a1ca2c688 MD5 · raw file

  1. -- upgrade-3.4.0.1.0-3.4.0.2.0.sql
  2. SELECT acs_log__debug('/packages/intranet-timesheet2-invoices/sql/postgresql/upgrade/upgrade-3.4.0.1.0-3.4.0.2.0.sql','');
  3. create or replace function inline_0 ()
  4. returns integer as '
  5. DECLARE
  6. v_count integer;
  7. BEGIN
  8. select count(*) into v_count from user_tab_columns
  9. where table_name = ''IM_TIMESHEET_INVOICES'' and column_name = ''INVOICE_PERIOD_START'';
  10. IF v_count > 0 THEN return 0; END IF;
  11. -- Start and end date of invoicing period
  12. alter table im_timesheet_invoices add invoice_period_start timestamptz;
  13. alter table im_timesheet_invoices add invoice_period_end timestamptz;
  14. return 0;
  15. end;' language 'plpgsql';
  16. select inline_0();
  17. drop function inline_0();
  18. create or replace function inline_0 ()
  19. returns integer as '
  20. DECLARE
  21. v_count integer;
  22. BEGIN
  23. select count(*) into v_count from user_tab_columns
  24. where lower(table_name) = ''im_hours'' and lower(column_name) = ''invoice_id'';
  25. IF v_count > 0 THEN return 0; END IF;
  26. alter table im_hours add invoice_id integer
  27. constraint im_hours_invoice_fk references im_costs;
  28. -- copy the cost_id values to invoice_id
  29. -- for all real invoices
  30. update im_hours set invoice_id = cost_id
  31. where cost_id in (
  32. select cost_id
  33. from im_costs
  34. where cost_type_id = 3700
  35. );
  36. update im_hours set cost_id = null
  37. where cost_id in (
  38. select cost_id
  39. from im_costs
  40. where cost_type_id = 3700
  41. );
  42. return 0;
  43. end;' language 'plpgsql';
  44. select inline_0();
  45. drop function inline_0();
  46. -- Delete a single invoice (if we know its ID...)
  47. create or replace function im_timesheet_invoice__delete (integer)
  48. returns integer as '
  49. DECLARE
  50. p_invoice_id alias for $1;
  51. BEGIN
  52. -- Reset the invoiced-flag of all invoiced tasks
  53. update im_timesheet_tasks
  54. set invoice_id = null
  55. where invoice_id = p_invoice_id;
  56. -- Reset the invoiced-flag of all included hours
  57. update im_hours
  58. set invoice_id = null
  59. where invoice_id = p_invoice_id;
  60. -- Compatibility for old invoices where cost_id
  61. -- indicated that hours belong to invoice
  62. update im_hours
  63. set cost_id = null
  64. where cost_id = p_invoice_id;
  65. -- Erase the invoice itself
  66. delete from im_timesheet_invoices
  67. where invoice_id = p_invoice_id;
  68. -- Erase the CostItem
  69. PERFORM im_invoice__delete(p_invoice_id);
  70. return 0;
  71. end;' language 'plpgsql';