CREATE PROCEDURE [dbo].[create_fact_payments] AS BEGIN

    DROP TABLE IF EXISTS fact_payments;
	CREATE TABLE fact_payments
	(
		id INT NOT NULL primary key, invoice_id int not null,
	                  payment_method_id int null,
	                  transaction_id int null,
	                  paid_at int null,
	                  amount money null,
	                  tds_amount money default 0,
	                  gst_tds_amount money default 0,
	);
	CREATE INDEX fact_payments_invoice_id_index ON fact_payments (invoice_id);
	                  CREATE INDEX fact_payments_transaction_id_index ON fact_payments (transaction_id);

	INSERT INTO fact_payments
	SELECT id, invoice_id, payment_method_id, transaction_id, dbo.convertDateToInt(paid_at),
	            amount, tds_amount, gst_tds_amount
		from payments;

    UPDATE last_updated_at_history SET last_updated_at = GETDATE() WHERE sp_name = 'create_fact_payments';

END
go

