Guest User

Untitled

a guest
Apr 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. --
  2. -- Billig::Economy::Settlement SQL (2.0)
  3. --
  4. -- Changes from 1.0 (UKA-09):
  5. -- - Settlement does not contain information about the physical cash register and terminal.
  6. -- - No 'change' transaction type - handled as one refund and one sale pr ticket affected.
  7. -- - No 'reservation' payment method. Reservation payments are not handled in billig unless
  8. -- they are paid through NetAxcept, which makes it a normal netsale (not handled by this module).
  9. -- - Transactions are now made per ticket and not per purchase, which removes the need for the old
  10. -- ticket_transaction-table and decreases the reporting complexity quite a bit.
  11. -- - Transactions do not have the transaction_sum-field anymore as its redundant and a violation of 3NF,
  12. -- join with the price_group-table instead to find the sum.
  13. -- - Added the settlement_report-view for moving a lot of client-side code (TODO: Analyze performance)
  14. --
  15. -- TODO:
  16. -- - Include muliple seller support from eide
  17.  
  18. CREATE TABLE settlement (
  19. settlement SERIAL PRIMARY KEY NOT NULL,
  20.  
  21. owner VARCHAR NOT NULL,
  22. organisation INTEGER REFERENCES (organisation) NOT NULL,
  23.  
  24. starttime TIMESTAMP NOT NULL DEFAULT NOW(),
  25. endtime TIMESTAMP,
  26.  
  27. cash_in INTEGER, -- Cash contained in the register at endtime
  28. credit_in INTEGER, -- Credit in as reported by the terminal
  29.  
  30. cash_king_id INTEGER, -- Export ID from CashKing (used under UKA) (Renamed from uka_transaction_id in old version)
  31. );
  32.  
  33. CREATE TABLE transaction_type (
  34. transaction_type VARCHAR PRIMARY KEY NOT NULL
  35. );
  36.  
  37. INSERT INTO transaction_type VALUES ('sale');
  38. INSERT INTO transaction_type VALUES ('refund');
  39.  
  40. CREATE TABLE payment_method (
  41. payment_method VARCHAR PRIMARY KEY NOT NULL
  42. );
  43.  
  44. INSERT INTO payment_method VALUES ('cash');
  45. INSERT INTO payment_method VALUES ('credit');
  46.  
  47. CREATE TABLE transaction (
  48. transaction SERIAL PRIMARY KEY NOT NULL,
  49. settlement INTEGER REFERENCES (settlement) NOT NULL,
  50. ticket INTEGER REFERENCES (ticket) NOT NULL,
  51. seller VARCHAR NOT NULL,
  52. transaction_type VARCHAR REFERENCES (transaction_type)
  53. transaction_time TIMESTAMP NOT NULL DEFAULT NOW(),
  54. payment_method VARCHAR REFERENCES (payment_method),
  55. -- Don't allow multiple transactions of the same type on one ticket
  56. UNIQUE (ticket, transaction_type),
  57. -- We only support refunds in cash
  58. CHECK (transaction_type = 'sale' OR (transaction_type = 'refund' AND payment_type = 'cash'))
  59. );
  60.  
  61. CREATE VIEW settlement_report (
  62. SELECT settlement, seller, starttime, endtime, cash_in, credit_in,
  63. (cash_sale.cash_sale - cash_sale.cash_refund) AS cash,
  64. credit_sale.credit_sale AS credit
  65. FROM settlement
  66. NATURAL JOIN transaction
  67. NATURAL JOIN (
  68. SELECT settlement, SUM(price) AS cash_sale
  69. FROM transaction
  70. NATURAL JOIN ticket
  71. NATURAL JOIN price_group
  72. WHERE payment_type = 'cash' AND transaction_type = 'sale'
  73. GROUP BY settlement
  74. ) AS cash_sale,
  75. NATURAL JOIN (
  76. SELECT settlement, SUM(price) AS cash_refund
  77. FROM transaction
  78. NATURAL JOIN ticket
  79. NATURAL JOIN price_group
  80. WHERE payment_type = 'cash' AND transaction_type = 'refund'
  81. GROUP BY settlement
  82. ) AS cash_refund,
  83. NATURAL JOIN (
  84. SELECT settlement, SUM(price) AS credit_sale
  85. FROM transaction
  86. NATURAL JOIN ticket
  87. NATURAL JOIN price_group
  88. WHERE payment_type = 'credit' AND transaction_type = 'sale'
  89. GROUP BY settlement
  90. ) AS credit_sale
  91. );
  92.  
  93. --
  94. -- Returns the sold tickets that were not refunded within the same settlement. Returns the settlement value and the ticket.
  95. -- Ex: SELECT SUM(price) FROM ticket NATURAL JOIN price_group WHERE ticket in (SELECT ticket FROM settlement_tickets WHERE settlement = 100);
  96. --
  97. CREATE VIEW settlement_tickets (
  98. SELECT settlement, ticket FROM (
  99. SELECT settlement, ticket, refunds.ticket
  100. FROM transaction
  101. NATURAL LEFT JOIN (
  102. SELECT settlement, ticket
  103. FROM transaction
  104. WHERE transaction_type = 'refund') AS refunds
  105. WHERE transaction_type = 'sale'
  106. AND refunds.ticket IS NULL) AS collector
  107. ORDER BY settlement, ticket
  108. );
Add Comment
Please, Sign In to add comment