<?xml version="1.0"?>
<!--
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_GiftCard
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */
-->
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="mageplaza_giftcard_pool" resource="default" engine="innodb" comment="Gift Card Pool Table">
        <column xsi:type="int" name="pool_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Code Pool Id"/>
        <column xsi:type="varchar" name="name" length="255" comment="Name"/>
        <column xsi:type="smallint" name="status" nullable="false" default="0" comment="Status"/>
        <column xsi:type="smallint" name="can_inherit" nullable="false" default="1" comment="Can Inherit"/>
        <column xsi:type="varchar" name="pattern" length="255" comment="Code Pattern"/>
        <column xsi:type="decimal" name="balance" default="0.0000" precision="12" scale="4" comment="Balance"/>
        <column xsi:type="smallint" name="can_redeem" nullable="false" default="1" comment="Can Redeem"/>
        <column xsi:type="smallint" name="store_id" padding="5" unsigned="true" nullable="false" default="0" comment="Website Id"/>
        <column xsi:type="int" name="template_id" padding="10" unsigned="true" comment="Template Id"/>
        <column xsi:type="varchar" name="image" length="255" comment="Template Image"/>
        <column xsi:type="mediumtext" name="template_fields" comment="Template Fields"/>
        <column xsi:type="mediumtext" name="conditions_serialized"/>
        <column xsi:type="smallint" name="is_can_refund" nullable="false" default="0" comment="Refund Balance"/>
        <column xsi:type="varchar" name="count_giftcard_use" length="255" comment="Count Gift Card Use"/>
        <column xsi:type="smallint" name="is_config_refund" comment="Is Use System Refund" />
        <column xsi:type="datetime" name="expired_at" on_update="false" nullable="true" comment="Expired At"/>
        <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP" comment="Created At"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="pool_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="MPGIFTCARD_POOL_ID_STORE_ID"
                    table="mageplaza_giftcard_pool" column="store_id" referenceTable="store"
                    referenceColumn="store_id" onDelete="NO ACTION"/>
        <index referenceId="MPGIFTCARD_POOL_ID" indexType="btree">
            <column name="pool_id"/>
        </index>
    </table>

    <table name="mageplaza_giftcard_template" resource="default" engine="innodb" comment="Gift Card Template Table">
        <column xsi:type="int" name="template_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Gift Card Template Id"/>
        <column xsi:type="varchar" name="name" length="255" comment="Name"/>
        <column xsi:type="smallint" name="status" nullable="false" default="1" comment="Status"/>
        <column xsi:type="smallint" name="can_upload" nullable="false" default="1" comment="Can Upload Image"/>
        <column xsi:type="varchar" name="title" length="255" comment="Title"/>
        <column xsi:type="varchar" name="font_family" length="255" default="Arial" comment="Font Family"/>
        <column xsi:type="varchar" name="background_image" length="255" comment="Background Image"/>
        <column xsi:type="mediumtext" name="design" comment="Design For Template (Json)"/>
        <column xsi:type="mediumtext" name="note" comment="Note"/>
        <column xsi:type="mediumtext" name="images" comment="Images (Json)"/>
        <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="template_id"/>
        </constraint>
        <index referenceId="MPGIFTCARD_TEMPLATE_ID" indexType="btree">
            <column name="template_id"/>
        </index>
    </table>

    <table name="mageplaza_giftcard" resource="default" engine="innodb" comment="Gift Card Code Table">
        <column xsi:type="int" name="giftcard_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Gift Card Id"/>
        <column xsi:type="varchar" name="code" length="255" nullable="false" comment="Code"/>
        <column xsi:type="decimal" name="init_balance" default="0.0000" precision="12" scale="4" comment="Initial Balance"/>
        <column xsi:type="decimal" name="balance" default="0.0000" precision="12" scale="4" comment="Balance"/>
        <column xsi:type="smallint" name="status" nullable="false" default="0" comment="Status"/>
        <column xsi:type="smallint" name="can_redeem" nullable="false" default="1" comment="Can Redeem"/>
        <column xsi:type="smallint" name="store_id" padding="5" unsigned="true" nullable="false" default="0" comment="Store Id"/>
        <column xsi:type="int" name="pool_id" padding="10" unsigned="true" comment="Pool Id"/>
        <column xsi:type="int" name="template_id" padding="10" unsigned="true" comment="Template Id"/>
        <column xsi:type="varchar" name="image" length="255" comment="Template Image"/>
        <column xsi:type="mediumtext" name="template_fields" comment="Template Fields"/>
        <column xsi:type="mediumtext" name="conditions_serialized"/>
        <column xsi:type="varchar" name="customer_ids" length="255" comment="Customer Save Gift Card"/>
        <column xsi:type="int" name="order_item_id" padding="10" unsigned="true" comment="Order Item Id"/>
        <column xsi:type="varchar" name="order_increment_id" length="32" comment="Order Increment Id"/>
        <column xsi:type="smallint" name="delivery_method" comment="Delivery Method"/>
        <column xsi:type="mediumtext" name="delivery_address" comment="Delivery Address"/>
        <column xsi:type="smallint" name="is_sent" padding="5" nullable="false" default="0" comment="Is Sent Gift Card"/>
        <column xsi:type="date" name="delivery_date" comment="Delivery Date"/>
        <column xsi:type="varchar" name="timezone" length="31" comment="Timezone"/>
        <column xsi:type="mediumtext" name="extra_content" comment="Extra Content"/>
        <column xsi:type="datetime" name="expired_at" on_update="false" nullable="true" comment="Expired At"/>
        <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP" comment="Created At"/>
        <column xsi:type="mediumtext" name="description" comment="Description"/>
        <column xsi:type="smallint" name="is_can_refund" nullable="false" default="0" comment="Refund Balance"/>
        <column xsi:type="varchar" name="count_giftcard_use" length="255" comment="Count Gift Card Use"/>
        <column xsi:type="decimal" name="amount_used" default="0.0000" precision="12" scale="4" comment="Amount Used"/>
        <column xsi:type="int" name="product_id" padding="10" unsigned="true" comment="Product Id Create Gift Code"/>
        <column xsi:type="decimal" name="row_total" default="0.0000" precision="12" scale="4" comment="Raw Total Order"/>
        <column xsi:type="smallint" name="is_config_refund" comment="Is Use System Refund" />
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="giftcard_id"/>
        </constraint>
        <constraint xsi:type="unique" referenceId="UNIQUE_CODE">
            <column name="giftcard_id"/>
            <column name="code"/>
            <column name="store_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="MPGIFTCARD_CODE_ID_STORE_ID"
                    table="mageplaza_giftcard" column="store_id" referenceTable="store"
                    referenceColumn="store_id" onDelete="NO ACTION"/>
        <constraint xsi:type="foreign" referenceId="MPGIFTCARD_CODE_POOL_ID_POOL_ID"
                    table="mageplaza_giftcard" column="pool_id" referenceTable="mageplaza_giftcard_pool"
                    referenceColumn="pool_id" onDelete="NO ACTION"/>
        <index referenceId="MPGIFTCARD_CODE_ID" indexType="btree">
            <column name="giftcard_id"/>
        </index>
        <index referenceId="MPGIFTCARD_CODE_POOL_ID" indexType="btree">
            <column name="pool_id"/>
        </index>
    </table>

    <table name="mageplaza_giftcard_history" resource="default" engine="innodb" comment="Gift Card History Table">
        <column xsi:type="int" name="history_id" padding="10" unsigned="true" nullable="false" identity="true" comment="History Id"/>
        <column xsi:type="int" name="giftcard_id" padding="10" unsigned="true" comment="Code Id"/>
        <column xsi:type="varchar" name="code" length="255" nullable="false" comment="Code"/>
        <column xsi:type="varchar" name="action" length="64" comment="Action"/>
        <column xsi:type="decimal" name="balance" default="0.0000" precision="12" scale="4" comment="Initial Balance"/>
        <column xsi:type="decimal" name="amount" default="0.0000" precision="12" scale="4" comment="Amount change"/>
        <column xsi:type="smallint" name="status" comment="Status"/>
        <column xsi:type="smallint" name="store_id" nullable="false" default="0" comment="Store Id"/>
        <column xsi:type="mediumtext" name="extra_content" comment="Extra Content"/>
        <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP" comment="Created At"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="history_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="MPGIFTCARD_HISTORY_ID_GIFTCARD_ID"
                    table="mageplaza_giftcard_history" column="giftcard_id" referenceTable="mageplaza_giftcard"
                    referenceColumn="giftcard_id" onDelete="CASCADE"/>
        <index referenceId="MPGIFTCARD_HISTORY_ID" indexType="btree">
            <column name="history_id"/>
        </index>
    </table>

    <table name="mageplaza_giftcard_credit" resource="default" engine="innodb" comment="Gift Card Customer Credit">
        <column xsi:type="int" name="credit_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Credit Id"/>
        <column xsi:type="int" name="customer_id" padding="10" unsigned="true" comment="Customer Id"/>
        <column xsi:type="decimal" name="balance" default="0.0000" precision="12" scale="4" comment="Balance"/>
        <column xsi:type="smallint" name="credit_notification" comment="Email Update Balance Notification"/>
        <column xsi:type="smallint" name="giftcard_notification" comment="Email Gift Card Update Notification"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="credit_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="MPGIFTCARD_CREDIT_ID_CUSTOMER_ID"
                    table="mageplaza_giftcard_credit" column="customer_id" referenceTable="customer_entity"
                    referenceColumn="entity_id" onDelete="CASCADE"/>
        <index referenceId="MPGIFTCARD_CREDIT_ID" indexType="btree">
            <column name="credit_id"/>
        </index>
    </table>

    <table name="mageplaza_giftcard_transaction" resource="default" engine="innodb" comment="Gift Card Customer Credit Transaction">
        <column xsi:type="int" name="transaction_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Transaction Id"/>
        <column xsi:type="int" name="credit_id" padding="10" unsigned="true" comment="Customer Id"/>
        <column xsi:type="smallint" name="action" nullable="false" default="0" comment="Action"/>
        <column xsi:type="decimal" name="balance" default="0.0000" precision="12" scale="4" comment="Balance"/>
        <column xsi:type="decimal" name="amount" default="0.0000" precision="12" scale="4" comment="Amount"/>
        <column xsi:type="smallint" name="website_id" padding="5" unsigned="true" comment="Website Id"/>
        <column xsi:type="mediumtext" name="extra_content" comment="Extra Content"/>
        <column xsi:type="timestamp" name="created_at" on_update="false" nullable="false" default="CURRENT_TIMESTAMP" comment="Created At"/>
        <constraint xsi:type="primary" referenceId="PRIMARY">
            <column name="transaction_id"/>
        </constraint>
        <constraint xsi:type="foreign" referenceId="MPGIFTCARD_TRANS_ID_CREDIT_ID"
                    table="mageplaza_giftcard_transaction" column="credit_id" referenceTable="mageplaza_giftcard_credit"
                    referenceColumn="credit_id" onDelete="CASCADE"/>
        <constraint xsi:type="foreign" referenceId="MPGIFTCARD_CREDIT_ID_WEBSITE_ID"
                    table="mageplaza_giftcard_transaction" column="website_id" referenceTable="store_website"
                    referenceColumn="website_id" onDelete="CASCADE"/>
        <index referenceId="MPGIFTCARD_TRANS_ID" indexType="btree">
            <column name="transaction_id"/>
        </index>
    </table>
    <table name="sales_order" resource="default">
        <column xsi:type="text" name="gift_cards" comment="Mp Gift Cards"/>
        <column xsi:type="decimal" name="base_gift_card_amount" scale="2" precision="9" unsigned="false" nullable="true" comment="Mp Base Gift Card Amount"/>
        <column xsi:type="decimal" name="gift_card_amount" scale="2" precision="9" unsigned="false" nullable="true" comment="Mp Gift Card Amount"/>
        <column xsi:type="decimal" name="base_gift_credit_amount" scale="2" precision="9" unsigned="false" nullable="true" comment="Mp Gift Credit Amount"/>
        <column xsi:type="decimal" name="gift_credit_amount" scale="2" precision="9" unsigned="false" nullable="true" comment="Mp Gift Credit Amount"/>
        <column xsi:type="text" name="mp_gift_cards" comment="Mp Gift Cards"/>
        <column xsi:type="text" name="give_gift_cards" comment="Mp Gift Card Give"/>
    </table>
    <table name="sales_invoice" resource="default">
        <column xsi:type="decimal" name="base_gift_card_amount" scale="2" precision="9" unsigned="false" nullable="true" comment="Mp Base Gift Card Amount"/>
        <column xsi:type="decimal" name="gift_card_amount" scale="2" precision="9" unsigned="false" nullable="true" comment="Mp Gift Card Amount"/>
        <column xsi:type="decimal" name="base_gift_credit_amount" scale="2" precision="9" unsigned="false" nullable="true" comment="Mp Gift Credit Amount"/>
        <column xsi:type="decimal" name="gift_credit_amount" scale="2" precision="9" unsigned="false" nullable="true" comment="Mp Gift Credit Amount"/>
    </table>
    <table name="sales_creditmemo" resource="default">
        <column xsi:type="decimal" name="base_gift_card_amount" scale="2" precision="9" unsigned="false" nullable="true" comment="Mp Base Gift Card Amount"/>
        <column xsi:type="decimal" name="gift_card_amount" scale="2" precision="9" unsigned="false" nullable="true" comment="Mp Gift Card Amount"/>
        <column xsi:type="decimal" name="base_gift_credit_amount" scale="2" precision="9" unsigned="false" nullable="true" comment="Mp Gift Credit Amount"/>
        <column xsi:type="decimal" name="gift_credit_amount" scale="2" precision="9" unsigned="false" nullable="true" comment="Mp Gift Credit Amount"/>
    </table>
    <table name="quote" resource="default">
        <column xsi:type="text" name="gift_cards" comment="Mp Gift Cards"/>
        <column xsi:type="text" name="gc_credit" comment="Mp Gift Scredit"/>
        <column xsi:type="text" name="mp_gift_cards" comment="Mp Gift Cards"/>
        <column xsi:type="text" name="mp_base_credit" comment="Mp Base Gift Scredit"/>
    </table>
</schema>
