Пример #1
0
 /**
  * After purchased order mail function
  *
  * @return void
  */
 public function after_purchased_order_mail()
 {
     $redshopMail = new redshopMail();
     $redconfig = new Redconfiguration();
     $stockroomhelper = new rsstockroomhelper();
     $db = JFactory::getDbo();
     $date = JFactory::getDate();
     $fdate = $date->toFormat('%Y-%m-%d');
     $query = "SELECT * FROM #__redshop_orders where order_payment_status ='Paid' and order_status = 'C'";
     $db->setQuery($query);
     $data = $db->loadObjectList();
     JTable::addIncludePath(JPATH_SITE . '/administrator/components/com_redshop/tables');
     foreach ($data as $mail_detail) {
         $bodytmp = "";
         $subject = "";
         $order_id = $mail_detail->order_id;
         $mailbcc = null;
         $config = JFactory::getConfig();
         $from = $config->getValue('mailfrom');
         $fromname = $config->getValue('fromname');
         $start_date = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
         $end_date = $start_date + DISCOUPON_DURATION * 23 * 59 * 59;
         $valid_end_date = $redconfig->convertDateFormat($end_date);
         $discoupon_value = DISCOUPON_PERCENT_OR_TOTAL == 0 ? REDCURRENCY_SYMBOL . " " . number_format(DISCOUPON_VALUE, 2, PRICE_SEPERATOR, THOUSAND_SEPERATOR) : ($discoupon_value = DISCOUPON_VALUE . " %");
         $sql = "SELECT CONCAT(firstname,' ',lastname) as name,user_email as email FROM  `#__redshop_order_users_info` WHERE `order_id` =  " . (int) $mail_detail->order_id . " AND `address_type` = 'BT' limit 0,1";
         $db->setQuery($sql);
         $orderuserarr = $db->loadObject();
         $sql = "SELECT coupon_left as total,coupon_code,end_date FROM  `#__redshop_coupons` WHERE `order_id` =  " . (int) $order_id . " AND coupon_left != 0 limit 0,1";
         $db->setQuery($sql);
         $couponeArr = $db->loadObject();
         if (count($couponeArr) <= 0) {
             continue;
         }
         $total = $couponeArr->total;
         $coupon_code = $couponeArr->coupon_code;
         $cend_date = $couponeArr->end_date;
         $name = "";
         $recipient = "";
         if (isset($orderuserarr)) {
             $recipient = $orderuserarr->email;
             $name = $orderuserarr->name;
         }
         if ($mail_detail->mail1_status == 0 && (DAYS_MAIL1 != 0 || DAYS_MAIL1 != '')) {
             $send_date = date("Y-m-d", $mail_detail->cdate + DAYS_MAIL1 * (60 * 60 * 24));
             $firstmail_data = $redshopMail->getMailtemplate(0, "first_mail_after_order_purchased");
             if (count($firstmail_data) > 0) {
                 $bodytmp = $firstmail_data[0]->mail_body;
                 $subject = $firstmail_data[0]->mail_subject;
                 if (trim($firstmail_data[0]->mail_bcc) != "") {
                     $mailbcc = explode(",", $firstmail_data[0]->mail_bcc);
                 }
             }
             $jpathurl = '<a href="' . JURI::root() . '">' . JURI::root() . '</a>';
             $body = str_replace("{name}", $name, $bodytmp);
             $body = str_replace("{url}", $jpathurl, $body);
             $body = str_replace("{coupon_amount}", $discoupon_value, $body);
             if ($fdate == $send_date) {
                 $better_token = md5(uniqid(mt_rand(), true));
                 $token = substr($better_token, 0, 10);
                 $body = str_replace("{coupon_code}", $token, $body);
                 $body = str_replace("{coupon_duration}", $valid_end_date, $body);
                 $sent = JUtility::sendMail($from, $fromname, $recipient, $subject, $body, $mode = 1, null, $mailbcc);
                 if ($sent == 1) {
                     $couponItems = JTable::getInstance('coupon_detail', 'Table');
                     $couponItems->coupon_code = $token;
                     $couponItems->percent_or_total = DISCOUPON_PERCENT_OR_TOTAL;
                     $couponItems->coupon_value = DISCOUPON_VALUE;
                     $couponItems->start_date = $start_date;
                     $couponItems->end_date = $end_date;
                     $couponItems->coupon_type = 1;
                     $couponItems->userid = $mail_detail->user_id;
                     $couponItems->coupon_left = 1;
                     $couponItems->published = 1;
                     $couponItems->order_id = $order_id;
                     $couponItems->store();
                     $q_update = "UPDATE #__redshop_orders SET mail1_status = 1 WHERE order_id = " . $order_id;
                     $db->setQuery($q_update);
                     $db->query();
                 }
             }
         } elseif ($mail_detail->mail2_status == 0 && (DAYS_MAIL2 != 0 || DAYS_MAIL2 != '') && $total != 0) {
             $send_date = date("Y-m-d", $mail_detail->cdate + DAYS_MAIL2 * (59 * 59 * 23));
             $secmail_data = $redshopMail->getMailtemplate(0, "second_mail_after_order_purchased");
             if (count($secmail_data) > 0) {
                 $bodytmp = $secmail_data[0]->mail_body;
                 $subject = $secmail_data[0]->mail_subject;
                 if (trim($secmail_data[0]->mail_bcc) != "") {
                     $mailbcc = explode(",", $secmail_data[0]->mail_bcc);
                 }
             }
             $days = $stockroomhelper->getdatediff($cend_date, $start_date);
             $jpathurl = '<a href="' . JURI::root() . '">' . JURI::root() . '</a>';
             $body = str_replace("{name}", $name, $bodytmp);
             $body = str_replace("{url}", $jpathurl, $body);
             $body = str_replace("{coupon_amount}", $discoupon_value, $body);
             if ($days && $fdate == $send_date) {
                 $valid_end_date = $redconfig->convertDateFormat($cend_date);
                 $body = str_replace("{coupon_code}", $coupon_code, $body);
                 $body = str_replace("{coupon_duration}", $valid_end_date, $body);
                 $sent = JUtility::sendMail($from, $fromname, $recipient, $subject, $body, $mode = 1, null, $mailbcc);
                 $q_update = "UPDATE #__redshop_orders SET mail2_status = 1 WHERE order_id = " . $order_id;
                 $db->setQuery($q_update);
                 $db->query();
             }
         } elseif ($mail_detail->mail3_status == 0 && (DAYS_MAIL3 != 0 || DAYS_MAIL3 != '') && $total != 0) {
             // Coupon reminder
             $thrdmail_data = $redshopMail->getMailtemplate(0, "third_mail_after_order_purchased");
             if (count($thrdmail_data) > 0) {
                 $bodytmp = $thrdmail_data[0]->mail_body;
                 $subject = $thrdmail_data[0]->mail_subject;
                 if (trim($thrdmail_data[0]->mail_bcc) != "") {
                     $mailbcc = explode(",", $thrdmail_data[0]->mail_bcc);
                 }
             }
             $send_date = date("Y-m-d", $mail_detail->cdate + DAYS_MAIL3 * (60 * 60 * 24));
             $days = $stockroomhelper->getdatediff($cend_date, $start_date);
             $jpathurl = '<a href="' . JURI::root() . '">' . JURI::root() . '</a>';
             $body = str_replace("{name}", $name, $bodytmp);
             $body = str_replace("{url}", $jpathurl, $body);
             $body = str_replace("{coupon_amount}", $discoupon_value, $body);
             if ($days && $fdate == $send_date) {
                 $valid_end_date = $redconfig->convertDateFormat($cend_date);
                 $body = str_replace("{coupon_code}", $coupon_code, $body);
                 $body = str_replace("{coupon_duration}", $valid_end_date, $body);
                 $sent = JUtility::sendMail($from, $fromname, $recipient, $subject, $body, $mode = 1, null, $mailbcc);
                 $q_update = "UPDATE #__redshop_orders SET mail3_status = 1 WHERE order_id = " . $order_id;
                 $db->setQuery($q_update);
                 $db->query();
             }
         }
     }
 }