-- +--------------------------------------------------------------------+
-- | Copyright CiviCRM LLC. All rights reserved.                        |
-- |                                                                    |
-- | This work is published under the GNU AGPLv3 license with some      |
-- | permitted exceptions and without any warranty. For full license    |
-- | and copyright information, see https://civicrm.org/licensing       |
-- +--------------------------------------------------------------------+
--
-- Generated from schema.tpl
-- DO NOT EDIT.  Generated by CRM_Core_CodeGen
--


-- +--------------------------------------------------------------------+
-- | Copyright CiviCRM LLC. All rights reserved.                        |
-- |                                                                    |
-- | This work is published under the GNU AGPLv3 license with some      |
-- | permitted exceptions and without any warranty. For full license    |
-- | and copyright information, see https://civicrm.org/licensing       |
-- +--------------------------------------------------------------------+
--
-- Generated from drop.tpl
-- DO NOT EDIT.  Generated by CRM_Core_CodeGen
--
-- /*******************************************************
-- *
-- * Clean up the exisiting tables
-- *
-- *******************************************************/



-- /*******************************************************
-- *
-- * Create new tables
-- *
-- *******************************************************/

-- /*******************************************************
-- *
-- * cividiscount_item
-- *
-- * A discount item
-- *
-- *******************************************************/
CREATE TABLE `cividiscount_item` (


     `id` int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Discount Item ID',
     `code` varchar(255) NOT NULL   COMMENT 'Discount Code',
     `description` varchar(255) NOT NULL   COMMENT 'Discount Description',
     `amount` varchar(255) NOT NULL   COMMENT 'Amount of discount, actual or percentage',
     `amount_type` varchar(4) NOT NULL   COMMENT 'Type of discount, actual or percentage',
     `count_max` int NOT NULL   COMMENT 'Max number of times this code can be used',
     `count_use` int NOT NULL  DEFAULT 0 COMMENT 'Number of times this code has been used',
     `events` text    COMMENT 'Serialized list of events for which this code can be used',
     `pricesets` text    COMMENT 'Serialized list of pricesets for which this code can be used',
     `memberships` text    COMMENT 'Serialized list of memberships for which this code can be used',
     `autodiscount` text    COMMENT 'Serialized list of entity-based attributes. A user with one of them has the discount automatically applied',
     `organization_id` int unsigned    COMMENT 'FK to Contact ID for the organization that originated this discount',
     `active_on` datetime    COMMENT 'Date this discount activated',
     `expire_on` datetime    COMMENT 'Date this discount expires',
     `is_active` tinyint    COMMENT 'Is this discount active?',
     `discount_msg_enabled` tinyint    ,
     `discount_msg` varchar(255)    ,
     `filters` text    COMMENT 'What filters apply to this discount (json array)' 
,
        PRIMARY KEY (`id`)
 
 
,          CONSTRAINT FK_cividiscount_item_organization_id FOREIGN KEY (`organization_id`) REFERENCES `civicrm_contact`(`id`) ON DELETE SET NULL  
)    ;

-- /*******************************************************
-- *
-- * cividiscount_track
-- *
-- * Record an instance of a discount use
-- *
-- *******************************************************/
CREATE TABLE `cividiscount_track` (


     `id` int unsigned NOT NULL AUTO_INCREMENT  COMMENT 'Discount Item ID',
     `item_id` int unsigned    COMMENT 'FK to Item ID of the discount code',
     `contact_id` int unsigned    COMMENT 'FK to Contact ID for the contact that used this discount',
     `used_date` datetime    COMMENT 'Date of use',
     `track` text    COMMENT 'Tracking code information',
     `contribution_id` int unsigned    COMMENT 'FK to contribution table',
     `event_id` int unsigned    COMMENT 'FK to event table',
     `entity_table` varchar(64) NOT NULL   COMMENT 'Table where referenced item is stored',
     `entity_id` int unsigned NOT NULL   COMMENT 'Foreign key to the referenced item' 
,
        PRIMARY KEY (`id`)
 
 
,          CONSTRAINT FK_cividiscount_track_item_id FOREIGN KEY (`item_id`) REFERENCES `cividiscount_item`(`id`) ON DELETE SET NULL,          CONSTRAINT FK_cividiscount_track_contact_id FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact`(`id`) ON DELETE SET NULL,          CONSTRAINT FK_cividiscount_track_contribution_id FOREIGN KEY (`contribution_id`) REFERENCES `civicrm_contribution`(`id`) ON DELETE CASCADE,          CONSTRAINT FK_cividiscount_track_event_id FOREIGN KEY (`event_id`) REFERENCES `civicrm_event`(`id`) ON DELETE CASCADE  
)    ;

 