How to send email when order status is updated ? prestashop - prestashop

foreach($order_to_convert as $odr_id) {
$objOrder = new Order($odr_id);
$history = new OrderHistory();
$history->id_order = (int)$objOrder->id;
//$history->changeIdOrderState((int)$sts_id, (int)($objOrder->id));
//$history->add(true);
$history->addWithemail(true);
//$history->save();
}
I have used above code to update order status & send email to customers but due to performance issue i have removed the code above & used query to perform the same. but i am not able to send email to customers.
Is their any way to send customer email when the order status is updated ?

Related

How to get $extraVars after creating an order in Prestashop 1.7

i'm creating order manually with extravars like this
$extra_vars = Array();
$extra_vars['slip_id'] = $result['data']['slip_id'];
$extra_vars['language'] = $result['data']['language'];
$extra_vars['expires_at'] = $expires_at;
$extra_vars['barcode'] = $result['data']['barcode'];
$this->module->validateOrder($this->context->cart->id, $status, $total, $checkout_label, NULL, $extra_vars, NULL, false, $customer->secure_key, NULL);
The order is creating successfully, but i cannot retrieve the saved extravars data, i have checked ps_order and ps_order_details table but i could not find extravars, can someone please help me to get the extravars, thanks
$extra_vars is not saved to database but only used so that custom data can be displayed in order confirmation emails.
If you want extra data in order you will need to override the Order class to define extra fields and add these fields in the orders table.

SFDC - Query all contacts shared with a given user?

I'm somewhat of a SFDC novice when it comes to integration, but is there any way I can query just the contacts shared with a given user, taking into account all the ways the share can occur? Essentially just see the same contacts the user would see in within the platform?
I think this is what you are looking for. I added some inline comments to explain what each step is doing. The end result should be all the contacts that can be read by a specified user in your org.
// add a set with all the contact ids in your org
List<contact> contacts = new List<contact>([Select id from Contact]);
Set<ID> contactids = new Set<ID>();
for(Contact c : contacts)
contactids.add(c.id);
// using the user record access you can query all the recordsids and the level of access for a specified user
List<UserRecordAccess> ura = new List<UserRecordAccess>([SELECT RecordId, HasReadAccess, HasTransferAccess, MaxAccessLevel
FROM UserRecordAccess
WHERE UserId = 'theuserid'
AND RecordId in: contactids
] );
// unfortunatelly you cannot agregate your query on hasReadAccess=true so you'd need to add this step
Set<id> readaccessID = new Set<ID>();
for(UserRecordAccess ur : ura)
{
if(ur.HasReadAccess==true)
{
readaccessID.add(ur.RecordID);
}
}
// This is the list of all the Contacts that can be read by the specified user
List<Contact> readAccessContact = new List<Contact>([Select id, name from contact where id in: readaccessID]);
// show the results
system.debug( readAccessContact);

send automatic email alert using stored procedure using sql server

User registered for one plan in website. The plan date is going to expire which means if 30 days, I have to send an automatic alert on the 21 st day to the employer. I don't know stored procedures and all. Now only I am learning.
I don't know how to create a query for that. Using the query how to create in stored procedure and execute automatically to send an e-mail alert?
My linq query:
public bool GetDateExpired(int Id)
{
var Subscribed = from om in _db.Order
join od in _db.OrderDetails on om.OrderId equals od.OrderId
where om.OrganizationId == Id && od.PlanName.Contains("plan") && od.OrderId == om.OrderId && om.PaymentStatus == true && od.RemainingCount > 0
select om;
var count = Subscribed.FirstOrDefault();
if (count != null)
return true;
else
return false;
}
If this condition true means email send to the user. How to do this in a stored procedure? Please help me
SQL Server provides a built-in method of sending emails via SMTP
You need to create a job in sql server agent, for details follow bellow link
Click Here

CodeIgniter Active Records - repeating a reusult from a select query

I have a mails table which is my main tables for manipulating mails. I also have mails_sent and mails_dates which has FK connecting them with mails and contain data as follows mails_dates - a single mail could have more than one date set for sending so I have realtion 1:N where N are all the dates set from the user for the mail to be send and mails_sent - after a mail is sent a new record is added in this table with the id of the mail and the date it was sent. Because as I mentioned a mail could be sent more than once here the relation is also 1:N where N here represents the different dates when a mail was sent.
I need to show this info in a UI. First I had to show only the sending dates and I user this:
$this->db->select("mails.id, mails.subject, mails.dt_created, mails.groups");
$this->db->select("GROUP_CONCAT(DATE_FORMAT( mails_dates.dt_sending_date, '%d-%m-%Y')) AS sending_dates", FALSE );
$this->db->join('mails_dates','mails_dates.mail_id = mails.id', 'left');
$this->db->join('mails_sent','mails_sent.mail_id = mails.id', 'left');
$this->db->from('mails');
$this->db->group_by('mails.id');
$res = $this->db->get()->result_array();
return $res;
When I was using this everything was fine I was getting a string with all the dates in it. But the problem is when I try to get the dates when a mail was sent. I added one more select:
$this->db->select("GROUP_CONCAT(DATE_FORMAT( mails_sent.dt_sent, '%d-%m-%Y')) AS sent_dates", FALSE );
making my query looks like this:
$this->db->select("mails.id, mails.subject, mails.dt_created, mails.groups");
$this->db->select("GROUP_CONCAT(DATE_FORMAT( mails_sent.dt_sent, '%d-%m-%Y')) AS sent_dates", FALSE );
$this->db->select("GROUP_CONCAT(DATE_FORMAT( mails_dates.dt_sending_date, '%d-%m-%Y')) AS sending_dates", FALSE );
$this->db->join('mails_dates','mails_dates.mail_id = mails.id', 'left');
$this->db->join('mails_sent','mails_sent.mail_id = mails.id', 'left');
$this->db->from('mails');
$this->db->group_by('mails.id');
$res = $this->db->get()->result_array();
return $res;
But the problem is that If sending_dates contains let's say - 6 values, and sent_dates has only one I end up with a string containing 6 times the value of sent_dates. This is in production so I don't have a lot of records but the same happens with a query where I have 2 sending_dates and 1 record for sent_dates still I get 2 times the record from sent_dates.
How can I fix this?
Thanks
Leron
Ok, I found the solution, here it is:
$this->db->select("GROUP_CONCAT(DISTINCT(DATE_FORMAT(mails_sent.dt_sent, '%d-%m-%Y'))) AS sent_dates", FALSE );
In other words just use DISTINCT.

Django DB API equivalent of a somewhat complex SQL query

I'm new to Django and still having some problems about simple queries.
Let's assume that I'm writting an email application. This is the Mail
model:
class Mail(models.Model):
to = models.ForeignKey(User, related_name = "to")
sender = models.ForeignKey(User, related_name = "sender")
subject = models.CharField()
conversation_id = models.IntegerField()
read = models.BooleanField()
message = models.TextField()
sent_time = models.DateTimeField(auto_now_add = True)
Each mail has conversation_id which identifies a set of email messages
which are written and replyed. Now, for listing emails in inbox, I
would like as gmail to show only last email per conversation.
I have the SQL equivalent which does the job, but how to construct native Django query for this?
select
*
from
main_intermail
where
id in
(select
max(id)
from
main_intermail
group by conversation_id);
Thank you in advance!
Does this work? It would require Django 1.1.
from django.db.models import Max
mail_list = Mail.objects.values('conversation_id').annotate(Max('id'))
conversation_id_list = mail_list.values_list('id__max',flat=True)
conversation_list = Mail.objects.filter(id__in=conversation_id_list)
So, given a conversation_id you want to retrieve the related record which has the highest id. To do this use order_by to sort the results in descending order (because you want the highest id to come first), and then use array syntax to get the first item, which will be the item with the highest id.
# Get latest message for conversation #42
Mail.objects.filter(conversation_id__exact=42).order_by('-id')[0]
However, this differs from your SQL query. Your query appears to provide the latest message from every conversation. This provides the latest message from one specific conversation. You could always do one query to get the list of conversations for that user, and then follow up with multiple queries to get the latest message from each conversation.