-- Payment-page payments: the billing address and email in TransactionLog, like the
-- website's payments (Address, City, State, Country, Email). Before, only the name,
-- phone and ZIP were filled; the address went to afd_BillingAddress (006) only.
--
-- A NEW procedure, so pay's code and this script can be deployed in any order: pay uses
-- afd_TransactionLog_InsertCard when it exists and afd_InsertTransactionLogCard (004)
-- until then. Never writes Account or ANI fields.
--
-- Run as a DB admin after 013. Safe to run again.

DELIMITER ;;

DROP PROCEDURE IF EXISTS `afd_TransactionLog_InsertCard`;;
CREATE PROCEDURE `afd_TransactionLog_InsertCard`(
  IN p_AccountID bigint, IN p_Amount double, IN p_Fee double, IN p_SourceID tinyint,
  IN p_FirstName varchar(50), IN p_LastName varchar(50), IN p_Phone varchar(25),
  IN p_Address varchar(60), IN p_City varchar(45), IN p_State varchar(40),
  IN p_ZipCode varchar(20), IN p_Country varchar(60), IN p_Email varchar(255),
  IN p_IPAddress varchar(15))
BEGIN
  INSERT INTO TransactionLog (AccountID, TransactionSourceID, FirstName, LastName, Phone,
                              Address, City, State, ZipCode, Country, Email,
                              AmountToCharge, Fee, IPAddress, Description)
  VALUES (p_AccountID, p_SourceID, p_FirstName, p_LastName, p_Phone,
          p_Address, p_City, p_State, p_ZipCode, p_Country, p_Email,
          p_Amount, p_Fee, p_IPAddress, 'AI Front Desk payment link');

  SELECT LAST_INSERT_ID() AS TransactionLogID;
END ;;

DELIMITER ;

-- With per-procedure grants:
-- GRANT EXECUTE ON PROCEDURE `prepaid`.`afd_TransactionLog_InsertCard` TO 'your_user'@'your_host';
