|
Server : Apache/2.4.18 (Ubuntu) System : Linux canvaswebdesign 3.13.0-71-generic #114-Ubuntu SMP Tue Dec 1 02:34:22 UTC 2015 x86_64 User : oppastar ( 1041) PHP Version : 7.0.33-0ubuntu0.16.04.15 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, Directory : /var/www/angkasapuraretail.com/public_html/application/controllers/ |
Upload File : |
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Cronjob extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->model('order_m');
$this->load->model('order_detail_m');
$this->load->model('configuration_m');
$this->load->model('customer_m');
}
public function payment_reminder() {
//get expired configuration
$payment_reminder_time = $this->db->select('time_duration_payment_reminder')->from('configuration')->where('id_configuration',1)->get()->row()->time_duration_payment_reminder;
//get current date
$current_date = strtotime(date('Y-m-d H:i:s')); //get the string of current date
//get all orders from database
$this->db->select('id_orders, customer_id, order_date')->from('orders')->where('payment_status', 1)->where('payment_confirm', 0);
$current_orders = $this->db->get()->result();
foreach ($current_orders as $order) {
//check if the time already pass 12 hours...
// Getting the value of old date + 12 hours, 43200 seconds == 12 hrs
$reminder_date = strtotime($order->order_date) + $payment_reminder_time;
if ($reminder_date < $current_date) {
//means that order date has exceed 12 hours, and must be reminded for payment
//----SEND EMAIL TO CUSTOMER
//get order detail and customer detail
$data['order'] = $this->order_m->get_order($order->id_orders);
$data['order_details'] = $this->db->select('*')->from('orders_detail')->where('orders_id',$order->id_orders)->get()->result();
$data['customer'] = $this->customer_m->get_customer($data['order']->customer_id);
//get website data
$this->db->select('logo, from_email, website_name, email_smtp_host, email_smtp_port, email_smtp_password, email_smtp, bank')->from('configuration')->where('id_configuration', 1);
$website_data = $this->db->get()->row();
$data['logo'] = $website_data->logo;
$data['website_name'] = $website_data->website_name;
$data['bank'] = $website_data->bank;
$data['emails'] = $this->configuration_m->get_emails();
$data['title'] = 'Payment Reminder';
$this->load->library('email');
//get email setting
$config['protocol'] = 'smtp';
$config['smtp_host'] = $website_data->email_smtp_host;
$config['smtp_port'] = $website_data->email_smtp_port;
$config['smtp_user'] = $website_data->email_smtp;
$config['smtp_pass'] = $website_data->email_smtp_password;
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
$this->email->initialize($config);
$this->email->from($data['emails']->from_email, $data['emails']->website_name);
$this->email->to($data['customer']->email);
$this->email->subject('Payment Reminder');
$email = $this->load->view('email/indonesian/payment_reminder', $data, TRUE);
echo $email;die();
$this->email->message($email);
$this->email->send();
//----end send email
}
}
}
public function cancel_pending_order() {
//status dari 0 pending dirubah menjadi 2 cancel...stok balik...
//get expired configuration
$pending_expired_cofiguration = $this->db->select('time_duration_pending_to_cancel')->from('configuration')->where('id_configuration',1)->get()->row()->time_duration_pending_to_cancel;
//get current date
$current_date = strtotime(date('Y-m-d H:i:s')); //get the string of current date
//get all orders from database. status 1 means not paid
$this->db->select('id_orders, customer_id, order_date')->from('orders')->where('payment_status', 0)->where('payment_confirm', 0);
$current_orders = $this->db->get()->result();
foreach ($current_orders as $order) {
//check if the time already pass X hours...
//EVERY 1 HOUR (3600 sec)
$expired_date = strtotime($order->order_date) + $pending_expired_cofiguration; //diganti jadi dinamis, X diambil configuration not paid to cancel
if ($expired_date < $current_date) {
//means that order date has exceed X hours, and must be cancelled
$data = array(
'payment_status' => 2, //cancel order
'cancel_date' => date('Y-m-d H:i:s')
);
$this->db->where('id_orders', $order->id_orders);
$this->db->update('orders', $data);
//get customer_id
$this->db->select('customer_id')->from('orders')->where('id_orders', $order->id_orders);
$customer_id = (int) $this->db->get()->row()->customer_id;
//return the quantity back to stock
//get order details
$order_details = $this->order_detail_m->get_orders_detail($order->id_orders);
foreach ($order_details as $item) {
//get current stock
$this->db->select('stock')->from('stock')->where('id_product', $item->item_id)->where('warehouse_id', $item->warehouse_id);
$current_stock = $this->db->get()->row()->stock;
$qty = 0;
if($item->is_backorder == 'yes'){
$qty = $qty + 0;
}
else{
$qty = $qty + $item->quantity;
}
$data = array(
'stock' => $current_stock + $qty,
);
$this->db->where('id_product', $item->item_id);
$this->db->where('warehouse_id', $item->warehouse_id);
$this->db->update('stock', $data);
if($item->is_flashsale != 0){
//return flashsale counter & customer purchase
/*get purchase qty from flashsale_customer*/
$flashsale_purchase = $this->db->select('purchase_qty')->from('flashsale_customer')->where('customer_id',$customer_id)->where('flashsale_id',$item->is_flashsale)->where('flashsale_product_id',$item->item_id)->get()->row()->purchase_qty;
/*return counter and terjual from flashsale product*/
$current_flashsale_product = $this->db->select('counter,terjual')->from('flashsale_products')->where('flashsale_id',$item->is_flashsale)->where('product_id',$item->item_id)->get()->row();
$return_counter_terjual = array(
'counter' => $current_flashsale_product->counter + $flashsale_purchase,
'terjual' => $current_flashsale_product->terjual - $flashsale_purchase,
);
$this->db->where('flashsale_id',$item->is_flashsale);
$this->db->where('product_id',$item->item_id);
$this->db->update('flashsale_products',$return_counter_terjual);
/*delete flashsale customer record*/
$this->db->where('customer_id',$customer_id);
$this->db->where('flashsale_id',$item->is_flashsale);
$this->db->delete('flashsale_customer');
}
}
//return customer point reward back to customers table..
//get customer current point
$this->db->select('current_pointreward')->from('customers')->where('id_customers', $customer_id);
$current_point = (int) $this->db->get()->row()->current_pointreward;
//get minus point from order
$this->db->select('minus_reward')->from('orders')->where('id_orders', $order->id_orders);
$rewards = $this->db->get()->row();
$minus_point = (int) $rewards->minus_reward;
$updated_point = $current_point + $minus_point;
//update point reward
$data = array(
'current_pointreward' => $updated_point
);
$this->db->where('id_customers', $customer_id);
$this->db->update('customers', $data);
//selain stok, kita harus balikin counter2 untuk flashsale, ..intip di admin Orders (controller)
} //if ($order_date > $current_date)...
} //end foreach ($current_orders as $order)...
}
public function cancel_order_daily() {
//get expired configuration
$daily_expired_cofiguration = $this->db->select('time_duration_not_paid_to_cancel')->from('configuration')->where('id_configuration',1)->get()->row()->time_duration_not_paid_to_cancel;
//get current date
$current_date = strtotime(date('Y-m-d H:i:s')); //get the string of current date
//get all orders from database. status 1 means not paid
$this->db->select('id_orders, customer_id, order_date')->from('orders')->where('payment_status', 1)->where('payment_confirm', 0);
$current_orders = $this->db->get()->result();
foreach ($current_orders as $order) {
//check if the time already pass X hours...
// Getting the value of old date + 24 hours, 86400 seconds == 24 hrs
$expired_date = strtotime($order->order_date) + $daily_expired_cofiguration; //diganti jadi dinamis, X diambil configuration not paid to cancel
if ($expired_date < $current_date) {
//means that order date has exceed X hours, and must be cancelled
$data = array(
'payment_status' => 2, //cancel order
'cancel_date' => date('Y-m-d H:i:s')
);
$this->db->where('id_orders', $order->id_orders);
$this->db->update('orders', $data);
//get customer_id
$this->db->select('customer_id')->from('orders')->where('id_orders', $order->id_orders);
$customer_id = (int) $this->db->get()->row()->customer_id;
//return the quantity back to stock
//get order details
$order_details = $this->order_detail_m->get_orders_detail($order->id_orders);
foreach ($order_details as $item) {
//get current stock
$this->db->select('stock')->from('stock')->where('id_product', $item->item_id)->where('warehouse_id', $item->warehouse_id);
$current_stock = $this->db->get()->row()->stock;
$qty = 0;
if($item->is_backorder == 'yes'){
$qty = $qty + 0;
}
else{
$qty = $qty + $item->quantity;
}
$data = array(
'stock' => $current_stock + $qty,
);
$this->db->where('id_product', $item->item_id);
$this->db->where('warehouse_id', $item->warehouse_id);
$this->db->update('stock', $data);
if($item->is_flashsale != 0){
//return flashsale counter & customer purchase
/*get purchase qty from flashsale_customer*/
$flashsale_purchase = $this->db->select('purchase_qty')->from('flashsale_customer')->where('customer_id',$customer_id)->where('flashsale_id',$item->is_flashsale)->where('flashsale_product_id',$item->item_id)->get()->row()->purchase_qty;
/*return counter and terjual from flashsale product*/
$current_flashsale_product = $this->db->select('counter,terjual')->from('flashsale_products')->where('flashsale_id',$item->is_flashsale)->where('product_id',$item->item_id)->get()->row();
$return_counter_terjual = array(
'counter' => $current_flashsale_product->counter + $flashsale_purchase,
'terjual' => $current_flashsale_product->terjual - $flashsale_purchase,
);
$this->db->where('flashsale_id',$item->is_flashsale);
$this->db->where('product_id',$item->item_id);
$this->db->update('flashsale_products',$return_counter_terjual);
/*delete flashsale customer record*/
$this->db->where('customer_id',$customer_id);
$this->db->where('flashsale_id',$item->is_flashsale);
$this->db->delete('flashsale_customer');
}
}
//return customer point reward back to customers table..
//get customer current point
$this->db->select('current_pointreward')->from('customers')->where('id_customers', $customer_id);
$current_point = (int) $this->db->get()->row()->current_pointreward;
//get minus point from order
$this->db->select('minus_reward')->from('orders')->where('id_orders', $order->id_orders);
$rewards = $this->db->get()->row();
$minus_point = (int) $rewards->minus_reward;
$updated_point = $current_point + $minus_point;
//update point reward
$data = array(
'current_pointreward' => $updated_point
);
$this->db->where('id_customers', $customer_id);
$this->db->update('customers', $data);
//----SEND EMAIL TO CUSTOMER
//get order detail and customer detail
$data['order'] = $this->order_m->get_order($order->id_orders);
$data['customer'] = $this->customer_m->get_customer($data['order']->customer_id);
$data['minus_point'] = $minus_point;
//get website data
$this->db->select('logo, from_email, website_name, email_smtp_host, email_smtp_port, email_smtp_password, email_smtp')->from('configuration')->where('id_configuration', 1);
$website_data = $this->db->get()->row();
$data['logo'] = $website_data->logo;
$data['website_name'] = $website_data->website_name;
$data['emails'] = $this->configuration_m->get_emails();
$data['title'] = 'Order Cancel';
$this->load->library('email');
//get email setting
$config['protocol'] = 'smtp';
$config['smtp_host'] = $website_data->email_smtp_host;
$config['smtp_port'] = $website_data->email_smtp_port;
$config['smtp_user'] = $website_data->email_smtp;
$config['smtp_pass'] = $website_data->email_smtp_password;
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
$this->email->initialize($config);
$this->email->from($data['emails']->from_email, $data['emails']->website_name);
$this->email->to($data['customer']->email);
$this->email->subject('Order Cancel');
$email = $this->load->view('email/indonesian/order_cancel', $data, TRUE);
$this->email->message($email);
$this->email->send();
//----end send email
} //if ($order_date > $current_date)...
} //end foreach ($current_orders as $order)...
}
}