
ALTER TABLE `users`
ADD `group_expiry_date` DATETIME NULL AFTER `resetpass_token_expire_date`,
ADD `mess_expiry_date` DATETIME NULL AFTER `group_expiry_date`,
ADD `family_expiry_date` DATETIME NULL AFTER `mess_expiry_date`;




DROP TABLE IF EXISTS `deposits`;
CREATE TABLE `deposits` (
  `id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `payment_id` varchar(255) CHARACTER SET latin1 DEFAULT NULL COMMENT 'system generated unique id',
  `fee` int(11) NOT NULL DEFAULT '0',
  `gross` int(11) NOT NULL DEFAULT '0',
  `cost` int(11) NOT NULL DEFAULT '0',
  `net` int(11) NOT NULL DEFAULT '0',
  `type` int(11) DEFAULT NULL COMMENT '1 = EFT, 2 = Automatic',
  `status` int(11) DEFAULT NULL COMMENT '0 = Pending, 1= Paid, 2 = Refund, 3 = Payment Fail',
  `user_ip` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
  `created_date` datetime NOT NULL,
  `updated_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
  `id` int(11) NOT NULL,
  `payment_id` varchar(255) CHARACTER SET latin1 DEFAULT NULL COMMENT 'system generated unique id like <expense_type>-<user_id>-<time>-<random_number>',
  `order_unique_id` varchar(255) CHARACTER SET latin1 DEFAULT NULL COMMENT 'Like : <expense_type>-<user_id>-<time>',
  `user_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `expire_date` datetime NOT NULL COMMENT 'Expire date of current purchase',
  `product_price` int(11) NOT NULL DEFAULT '0',
  `fee` int(11) NOT NULL DEFAULT '0',
  `gross` int(11) NOT NULL DEFAULT '0',
  `cost` int(11) NOT NULL DEFAULT '0',
  `net` int(11) NOT NULL DEFAULT '0',
  `currency_id` int(11) NOT NULL DEFAULT '1' COMMENT '1 = USD',
  `platform_id` int(11) NOT NULL,
  `device_id` varchar(255) CHARACTER SET latin1 DEFAULT NULL COMMENT 'session_id for web, device_id for mobile',
  `status` tinyint(4) NOT NULL COMMENT '0 = Pending, 1= Paid, 2 = Refund, 3 = Payment Fail',
  `type` tinyint(4) NOT NULL COMMENT '0 = One Time, 1 = Recurring',
  `user_ip` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
  `payment_type_id` int(11) NOT NULL COMMENT '1= Wallet, 2 = Credit Card, 3=Paypal',
  `created_date` datetime NOT NULL,
  `updated_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

DROP TABLE IF EXISTS `order_billings`;
CREATE TABLE `order_billings` (
  `id` int(11) NOT NULL,
  `order_id` int(11) NOT NULL,
  `first_name` varchar(255) DEFAULT NULL,
  `last_name` varchar(255) DEFAULT NULL,
  `address1` varchar(255) DEFAULT NULL,
  `address2` varchar(255) DEFAULT NULL,
  `city` varchar(255) DEFAULT NULL,
  `state` varchar(255) DEFAULT NULL,
  `zip` varchar(255) DEFAULT NULL,
  `country` varchar(255) DEFAULT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `created_date` datetime NOT NULL,
  `updated_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `payment_types`;
CREATE TABLE `payment_types` (
  `id` int(11) NOT NULL,
  `title` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
  `created_date` datetime NOT NULL,
  `updated_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

DROP TABLE IF EXISTS `products`;
CREATE TABLE `products` (
  `id` int(11) NOT NULL,
  `sku` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
  `title` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
  `description` text CHARACTER SET latin1,
  `price` int(11) NOT NULL DEFAULT '0',
  `currency_id` int(11) NOT NULL DEFAULT '1' COMMENT '1 = USD',
  `photo` varchar(255) CHARACTER SET latin1 DEFAULT NULL,
  `expense_type` tinyint(4) NOT NULL COMMENT '1=Group, 2= Mess, 3 = Family',
  `type` tinyint(4) NOT NULL COMMENT '0 = One Time, 1 = Recurring',
  `payment_number` int(11) NOT NULL DEFAULT '0',
  `payment_interval` int(11) NOT NULL DEFAULT '0',
  `payment_cycle` enum('D','W','M','Y') CHARACTER SET latin1 NOT NULL COMMENT 'D, W, M, Y',
  `status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '0 = Inactive, 1= Active',
  `created_date` datetime NOT NULL,
  `updated_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

DROP TABLE IF EXISTS `recurrings`;
CREATE TABLE `recurrings` (
  `id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `expense_type` int(11) NOT NULL COMMENT '1=Group, 2= Mess, 3 = Family',
  `start_date` datetime NOT NULL,
  `next_action_date` datetime NOT NULL,
  `amount` int(11) NOT NULL DEFAULT '0',
  `total_amount` int(11) NOT NULL DEFAULT '0',
  `payment_number` int(11) NOT NULL DEFAULT '0' COMMENT 'Total number of installments',
  `payment_interval` int(11) NOT NULL DEFAULT '0' COMMENT 'after how many payment cycle',
  `payment_cycle` enum('D','W','M','Y') CHARACTER SET latin1 NOT NULL COMMENT 'D, W, M, Y',
  `created_date` datetime NOT NULL,
  `updated_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

DROP TABLE IF EXISTS `transactions`;
CREATE TABLE `transactions` (
  `id` int(11) NOT NULL,
  `payment_id` varchar(255) CHARACTER SET latin1 NOT NULL COMMENT 'system generated unique id',
  `user_id` int(11) NOT NULL,
  `type` int(11) NOT NULL COMMENT '1 = Purchase, 2 = Deposit',
  `entity_id` int(11) NOT NULL COMMENT 'order_id / deposit_id',
  `description` text CHARACTER SET latin1 NOT NULL,
  `product_price` int(11) NOT NULL DEFAULT '0',
  `fee` int(11) NOT NULL DEFAULT '0',
  `gross` int(11) NOT NULL DEFAULT '0',
  `cost` int(11) NOT NULL DEFAULT '0',
  `net` int(11) NOT NULL DEFAULT '0',
  `currency_id` int(11) NOT NULL DEFAULT '1' COMMENT '1 = USD',
  `money_flow` varchar(255) CHARACTER SET latin1 NOT NULL DEFAULT '+' COMMENT '+ / -',
  `current_balance` int(11) NOT NULL DEFAULT '0',
  `status` tinyint(4) NOT NULL COMMENT '0 = Pending, 1= Paid, 2 = Refund, 3 = Payment Fail',
  `created_date` datetime NOT NULL,
  `updated_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

DROP TABLE IF EXISTS `wallets`;
CREATE TABLE `wallets` (
  `id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `currency_id` int(11) NOT NULL DEFAULT '1' COMMENT '1 = USD',
  `balance` int(11) NOT NULL DEFAULT '0',
  `withdrawable_balance` int(11) NOT NULL DEFAULT '0',
  `non_withdrawable_balance` int(11) NOT NULL DEFAULT '0',
  `status` tinyint(4) NOT NULL DEFAULT '1' COMMENT '0 = Inactive, 1= Active',
  `created_date` datetime NOT NULL,
  `updated_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


ALTER TABLE `deposits`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `orders`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `order_billings`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `payment_types`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `products`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `recurrings`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `transactions`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `wallets`
  ADD PRIMARY KEY (`id`);


ALTER TABLE `deposits`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
ALTER TABLE `orders`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
ALTER TABLE `order_billings`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
ALTER TABLE `payment_types`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
ALTER TABLE `products`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
ALTER TABLE `recurrings`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
ALTER TABLE `transactions`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
ALTER TABLE `wallets`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;COMMIT;


-- 2020-08-28
ALTER TABLE `orders` ADD `purchase_token` VARCHAR(255) NULL AFTER `order_unique_id`;

-- 2020-08-31
ALTER TABLE `user_groups` ADD UNIQUE `user_groups_unique_user_id_unique_url`(`user_id`, `unique_url`);
ALTER TABLE `users` ADD UNIQUE `users_unique_profile_id_profile_type`(`profile_id`, `profile_type`);

-- 2020-09-11
ALTER TABLE user_groups CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE expenses CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE `user_groups`
    CHANGE `unique_url` `unique_url` VARCHAR(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;
ALTER TABLE `products`
    CHANGE `price` `price` FLOAT(11) NOT NULL DEFAULT '0';
ALTER TABLE `orders`
    CHANGE `product_price` `product_price` FLOAT(11) NOT NULL DEFAULT '0',
    CHANGE `fee` `fee` FLOAT(11) NOT NULL DEFAULT '0',
    CHANGE `gross` `gross` FLOAT(11) NOT NULL DEFAULT '0',
    CHANGE `cost` `cost` FLOAT(11) NOT NULL DEFAULT '0',
    CHANGE `net` `net` FLOAT(11) NOT NULL DEFAULT '0';
ALTER TABLE `recurrings`
    CHANGE `amount` `amount` FLOAT(11) NOT NULL DEFAULT '0',
    CHANGE `total_amount` `total_amount` FLOAT(11) NOT NULL DEFAULT '0';
ALTER TABLE `transactions`
    CHANGE `product_price` `product_price` FLOAT(11) NOT NULL DEFAULT '0',
    CHANGE `fee` `fee` FLOAT(11) NOT NULL DEFAULT '0',
    CHANGE `gross` `gross` FLOAT(11) NOT NULL DEFAULT '0',
    CHANGE `cost` `cost` FLOAT(11) NOT NULL DEFAULT '0',
    CHANGE `net` `net` FLOAT(11) NOT NULL DEFAULT '0';
ALTER TABLE `deposits`
    CHANGE `fee` `fee` FLOAT(11) NOT NULL DEFAULT '0',
    CHANGE `gross` `gross` FLOAT(11) NOT NULL DEFAULT '0',
    CHANGE `cost` `cost` FLOAT(11) NOT NULL DEFAULT '0',
    CHANGE `net` `net` FLOAT(11) NOT NULL DEFAULT '0';


-- 21-09-2020
ALTER TABLE `users`
CHANGE `verification_token` `verification_token` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;

-- 25-09-2020
CREATE TABLE `user_subscriptions` (
  `id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL DEFAULT '0',
  `payment_method` enum('paypal') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'paypal',
  `validity` int(5) NOT NULL COMMENT 'in month(s)',
  `valid_from` datetime NOT NULL,
  `valid_to` datetime NOT NULL,
  `item_number` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `txn_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `payment_gross` float(10,2) NOT NULL,
  `currency_code` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
  `subscr_id` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `payer_email` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `payment_status` varchar(50) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `user_subscriptions`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `user_subscriptions`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `users`
ADD `user_subscription_id` INT NULL AFTER `family_expiry_date`;