How to create a discount coupon programmaticaly in opencart 2.1.0.2 - opencart

I have created a newsletter subscription pop-up using acumbamail and what i want is to programatically create a -10% discount coupon, which will last for 7 days, everytime someone clicks subscribe, then i will email the coupon code to the customer. I know that in opencart 2.1.0.2 you can go to marketing->coupons but i dont have the choice to create a coupon that lasts for 7 days from the day that the customer subscribed to the newsletter.

You have to create coupon code (coupon code must be unique) and you have to store in the [db_prefix]coupon table then send it via email to customer. Did you analyze coupon table structure?
Here is an example with description (DB_PREFIX = oc_), I used INSERT INTO SET instead of INSERT INTO () VALUES (), because it's compact
INSERT INTO oc_coupon
SET `name` = "New coupon", -- coupon description
`code` = "COUPON", -- unique coupon code
`type` = "P", -- P means percentage, F means flat
`discount` = 10, -- 10% discount
`logged` = 1, -- 1 means customer must be logged in, 0 means coupon can be used as guest
`shipping` = 0, -- free shipping when coupon used
`total` = 0, -- coupon valid from cart subtotal
`date_start` = CURRENT_DATE(), -- today
`date_end` = DATE_ADD(CURRENT_DATE(), INTERVAL 6 DAY), -- today +6 days
`uses_total` = 1, -- how many times the coupon can be used
`uses_customer` = 1, -- how many times the coupon can be used per customer
`status` = 1, -- coupon status
`date_added` = NOW()

Related

Dynamically insert data into an APEX page item when a page item is entered a value

I have a form in APEX that is used to add data into a table. Most of the items in the form are free form texts but there few items that are needed to auto filled when you enter a value in on of the page item.
For example.
There is form with Page Items. ID, Name, TitleLevel, Title_prefix, Salalry,
There is a table XYZ with Title_prefix and salary info which
ID Title_prefix Salaray
1 Junior 80000
2 Mid-Level 95000
3 Senior 115000
So you enter ID= 1, Name = ABCD, Titlelevel = 6 is entered and Title_prefix and salary should be populated based on the value that is entered in titlelevel. In this case it will be
Title_Prefix = (SELECT Title_Prefix FROM XYZ WHERE ID = (Case WHEN :P1_TitleLevel > 5 THEN 3 CASE WHEN :P1_TitleLevel = 5 THEN 2 ELSE 1 END))
Same thing for Salary as well.
Immediately after entering Titlelevel page item to 6 the Title_prefix and Salary should get refreshed and populate this items. Then the user will submit the form so that the information can be entered into the table.
Add a Dynamic Action on titlelevel (Key Release if Text Field, onChange if dropdown etc.)
Add a PL/SQL Action to the dynamic_action, using Items to Submit to pass fields in and Items to Return for the fields you modify.
Dynamic Action:
OR
Action:

Using SELECTEDVALUE in PowerBI

I have a table that contains the sample data from the attached image.
The sample table can be interpreted as follows: I have a list of customers with Customer_id, Customer_name and Email that have an account on 1 or more e-commerce sites. Every e-commerce site can be identified by the EcommerceSite_Id column.
If a customer has more than one account (eg: on EcommerceSite_Id = 111 and also on EcommerceSite_Id = 112) the GlobalClient_Id will have the same value (e.g. John has an account on the following EcommerceSite_Id: 111, 113 and 114. Therefore, he has the same GlobalClient_Id – “11” which is attributed based on some automatic criteria – in this example, email address).
What I want to achieve:
By using a slicer with the EcommerceSite_Id column, when selecting the EcommerceSite_Id 114, it should display all customers with a unique email address, that do not have an account in 114, by taking into account the GlobalClient_Id.
Therefore, the output should be
:
Therefore, as you can see I excluded the following customer_ids: 5 and 9. They do not have an account on 114, but I excluded them because they have the same ClientGlobal_Id with customers_ids 10 and 8, customers that have an account on 114.
I cannot find a solution. I tried to use selected values, but I don’t know if I am using it correctly.
Can you please give me an idea on how to solve it?
Welcome to SO. Here's one solution that might work for you:
Step 1. EcommerceSite_Id needs to be added to a separate table - it will contain distinct IDs:
Here's how the data model should look like:
Step 2. Create a measure
For each client, the measure will count the number of rows where a chosen EcommerceSite_Id appeared:
mExclude =
var EcommerceSite = SELECTEDVALUE(ES[EcommerceSite_Id])
var ThisCustomer = SELECTEDVALUE(CustomerData[Customer_name])
var Check = COUNTROWS(FILTER(ALL(CustomerData), CustomerData[Customer_name] = ThisCustomer && CustomerData[EcommerceSite_Id] = EcommerceSite))
return
Check + 0
When added to a table, and having a single EcommerceSite_Id selected (use your new table as a slicer), you get the following results:
Step 3. Finishing up
Finally, remove mExclude form the table and add it to the table filters. Set it up to filter values that are equal to zero.
The final result:

How to count active customers using start and end date in Power-BI?

Newbie here to Power-BI. I have a project where I need to count the number of active customers per month based on the Start_date and exit_date. There are some customers that do not yet have an exit date as they are still active/ haven't yet exited.
If all you want to do is count the customers that have no exit date (active customers), this worked for me:
CountActive = COUNTBLANK('your table'[EXIT_DATE])
If you want to show the active customers based on the start date you can simply use a slicer with START_DATE. The measure will automatically update the count of active customers.
If you want to show the customers that already exited (non active customers) you could do the following:
CountNonBlank = COUNTROWS('your table') - COUNTBLANK('your table'[END_DATE])
Again, just simply add a slicer to show count depending on start date.
Hope this helps

Power BI: Find Last Inactive Period of 365 Days

I'd like to find the reactivation date after someone had last been inactive for 365 days.
There are two tables:
A Login Table with non-distinct users and their multiple login dates.
A User Table with distinct users and their last login dates. I would also like this Reactivation Date to be placed here.
I used the following Dax formula to get the "last login date"
Last Login Date = MAXX(RELATEDTABLE('Login Table'), 'Login Table'[Login Date])
Any help would be greatly appreciated.
One way to solve this is by using multiple measures and columns.
For your Login Table, add a new calculated column with the following definition:
Previous Login = CALCULATE(MAX(LoginTable[Login]), FILTER(LoginTable, LoginTable[User] = EARLIER(LoginTable[User]) && LoginTable[Login] < EARLIER(LoginTable[Login])))
This will create a new calculated column in your Login Table which adds the date of the previous login to your table. (My Login Date column is called Login, therefore you might need to change this)
Now, simply calculate the DAY difference between the login date and the previous login date:
Day Difference = DATEDIFF(LoginTable[Previous Login], LoginTable[Login], DAY)
Using a simple IF statement, you can now add a new calculated column returning 1 if this login is a reactivation or not:
Is Reactivated = IF(DATEDIFF(LoginTable[Previous Login],LoginTable[Login], DAY) > 360, 1, 0)
To make things easier, also create a calculated column that returns the login date, if this is a reactivation:
Reactivate = IF(LoginTable[Is Reactivated] == 1, LoginTable[Login], BLANK())
Now you simply have to create a calculated table which groups by user and gets the max login date and max reactivated date like this:
Table = GROUPBY(LoginTable, LoginTable[User], "Last Login", MAXX(CURRENTGROUP(), LoginTable[Login]), "Last Reactivation", MAXX(CURRENTGROUP(), LoginTable[Reactivate]))
A lot of steps could be put into one steps but this way, it is simpler to understand and troubleshoot.
Hope this helps!

Top user of day, week, all time - best way to implement?

Suppose each user on my site has a score which increases as they use the site (like Stackoverflow) and I have a field score stored in the user profile table for each user.
Getting the top 10 users of all time is easy, just order by the score column.
I want to have "top 10 today", "top 10 this week", "top 10 of all time".
What's the best way to implement this? Do I need to store every single score change with a timestamp?
You would have to have a table that stored the increments and use a timestamp. I.E.
CREATE TABLE ScoreIncreases (
PrimaryKey UNIQUEIDENTIFIER,
UserId UNIQUEIDENTIFIER,
ScoreIncrease INT,
CreatedDate DATETIME)
Your query would then be something like
SELECT TOP 1 u.PrimaryKey, SUM(ScoreIncrease)
FROM Users u
INNER JOIN ScoreIncreases si ON si.Userid=u.PrimaryKey
WHERE DATEDIFF(day,si.CreatedDate,GETDATE()) = 0
GROUP BY u.PrimaryKey
ORDER BY SUM(ScoreIncrease) DESC