How to send notfication in three days? - yii

How to write query to send notifications when user package expired after 3 days in Yii?
For example, account expired on 25th Dec, then send expired email on 26th, 27th and 28th Dec.
$expireddate = date('Y-m-d', strtotime('-3 days')); // (Coming from db)
$model = TblPackageUserplan::model()->findAll('expire_date>=:expire_date', array(':expire_date' => $expireddate));
It's not working correct, because its sending mail before 20th Dec, too.
I want to send mail exactly user package expired after 3 days. How to get user details package expired after 3 days?

You may use only criteria.
As of your question
expired on 25th Dec, then send expired email on 26th, 27th and 28th Dec
You need to compare expire_date + 3days.
$criteria = new CDbCriteria;
// With this condition we get only packages on next three days after expire_date.
$criteria->addCondition('DATEDIFF(CURRENT_DATE(),t.expire_date) BETWEEN 1 AND 3');

Related

covering edge cases of dates in Ruby

I have one usecase, where I am doing manipulation based on dates. However there are some edgecases which is causing trouble in my logic and I am not able to figure out a way to cover those edge-cases.
Problem statement:
I want to get all the records which comes under the current month cycle.
Ex: Plan starts at: 5 Oct 2022, then Whenever I query data based on the date of querying, it should return the records count for the current month cycle. Let say, today is 3rd Nov which means current cycle is 5th Oct to 4th Nov. then query should return records from this period.
Based on the below logic, I am able to get the date which I am using in my query to fetch records which are created from date till now.
def get_current_month_contract_start_date(email)
contract_date = User.find_by(email: email).cycle_start_date
contract_day = contract_date.day if contract_date.present?
today = DateTime.now
if DateTime.now.day < contract_day
prev = today - 1.month
upper_bound_date = DateTime.new(prev.year, prev.month, contract_day, 0, 0, 0)
else
upper_bound_date = DateTime.new(today.year, today.month, contract_day, 0, 0, 0)
end
upper_bound_date
end
I am using below query to fetch the data from database:
User.find_by(email: email).where(:created_at.gte => get_current_month_contract_start_date(email)).count
But the problem is edge cases.
Let say contract start date is-> 31 Jan, it should end 27 Feb, next start date should be 28 Feb, but #actual is: 31-01-23 expected: 28-02-2023 when today is #28 Feb
Let say contract start date is -> 28 Feb, it should end 30 March and next start date should be 31 march. Assume, If today's date is 28 March then result is #actual: 28-03-23 expected: 28-02-23, because cycle will end on 30 March(Month end to next month end).
Same if today's date is 30th March then result is #actual: 28-03-23 expected: 28-02-23
If today date is 31th March then result is #actual: 28-03-23 expected: 31-03-23
Can someone please suggest me how to cover these edge cases as well in the current code with necessary modifications. Since I am new to ruby I am not able to figure out the exact solution.

SQL in BigQuery - How to loop through every month in a timeframe and calculate some metrics (i.e. count userID)

I am doing a test to calculate user retention for a bank. While doing so, I can certainly calculate the number of churn users and number of loyal users for a certain date period (for example, from 1 jan to 1 feb). However, the timeframe has every transactions/activities of every unique user from 1 jan 2016 to 1 jun 2018. I know we could do a loop in sql to automatically loop through every month but it is hard to do so with the calculation (with new users joining every month and the new users of month 1 would either be loyal user or churn in month 2).
Could anybody shed a light for me?

SQL Query Select Case

I just want to seek your help regarding the SQL Query below. I'm trying to compare Invoice date and Customer first Purchase Date. Supposedly if the the First Purchase Date is greater then Invoice Date but still within 1 year from the 1st Purchase Date, this will be categorize as New Business
For an example Customer A made a purchase on the following conditions
1st Purchase Feb 2021 -> New Business
2nd Purchase Oct 2021 -> New Business
3rd Purchase Jan 2022 -> New Business
4th Purchase Mar 2022 -> Existing Business (Since is more then 1 year after First Purchase)
Here are the Queries for CASE Scenario that I wrote
SELECT CASE
WHEN [FirstPurchaseCustomer] > [InvoiceDate] BETWEEN
DATEADD(Year,-1,FirstPurchaseCustomer) AND FirstPurchaseCustomer THEN
'New Business'
ELSE 'Existing Business'
END AS [Customer]

MKStoreKit Auto Renewing Subscription Expiry Date Always Returns Current Date

Using MKStoreKit for an auto-renewing subscription. Successful purchase in the sandbox. The issue is the purchase date and expiry date always log as the same, or 1hr behind the current date. With accelerated sandbox times I would expect [[MKStoreKit sharedKit] expiryDateForProduct:#"211112"]; to be around 30mins ahead of [NSDate date] on purchase as its a six month purchase. I get the current date exactly 1hr behind for the transaction.
Log for purchase:
Transaction date is 2021-07-30 4:15:07 pm +0000
Subscription end date is Fri Jul 30 17:15:07 2021
On reloading, the logs print that that subscription expiry date and current date are both exactly the same
Log for reloading:
1. Current time and date: Fri Jul 30 17:02:42 2021
2. Expiry date for subscription 6 months = Fri Jul 30 17:02:42 2021
From the code in MKStoreKit, it would seem the purchase date may be returning NULL and returning the current date
-(NSDate*) expiryDateForProduct:(NSString*) productId {
NSNumber *expiresDateMs = self.purchaseRecord[productId];
if ([expiresDateMs isKindOfClass:NSNull.class]) {
return NSDate.date;
} else {
return [NSDate dateWithTimeIntervalSince1970:[expiresDateMs doubleValue] / 1000.0f];
}
}
[[MKStoreKit sharedKit] valueForKey:#"purchaseRecord"] returns <null> on purchase
It was incorrect app secret in MKStorekitConfigs.plist

To pull out records from an access database multiple time according to the time frame

I have an access database that has some tickets related data. They all have 2 columns - Opening Date and Closing date with a ticket number as primary key!
I want to pull out how many records we open by the end of Jan, how many were open by the end of feb, etc.
So, if a ticket with Ticket ID TKT1, "Opening Date" = Jan 12 and "Closing Date" = March 5 then
if I pull out count of tickets, this TKT1 should be counted for Jan end's and Feb end's tickets as well.
Is there a query with which we could implement this?