|
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/laciasmara.com/public_html/shop/application/controllers/ |
Upload File : |
<?php defined('BASEPATH') or exit('No direct script access allowed');
class Cronjob extends MX_Controller
{
private $cron_token = 'zBnQIXk5Kdi2bPn';
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);
$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, id')->from('stock')->where('id_product', $item->product_id)->where('id_product_detail', $item->item_id)->where('warehouse_id', $item->warehouse_id);
$current_stock = $this->db->get()->row();
$qty = 0;
if ($item->is_backorder == 'yes') {
$qty = $qty + 0;
} else {
$qty = $qty + $item->quantity;
}
$data = array(
'stock' => $current_stock->stock + $qty,
);
$this->db->where('id_product', $item->product_id);
$this->db->where('id_product_detail', $item->item_id);
$this->db->where('warehouse_id', $item->warehouse_id);
$this->db->update('stock', $data);
//get $stock_id
$stock_id = $current_stock->id;
$last_stock = $current_stock->stock + $qty;
//update stock_movement_table
$movement_data = array(
'stock_id' => $stock_id,
'type' => '+',
'stock_change' => (int) $qty,
'remark' => 'Cancel by system Order No: ' . $order->id_orders . ' (' . $last_stock . ')',
'total' => (int) $last_stock,
'name' => 'System'
);
$this->db->insert('stock_movement', $movement_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('current_reward,sisa_reward,minus_reward')->from('orders')->where('id_orders', $order->id_orders);
$rewards = $this->db->get()->row();
$minus_point = (int) $rewards->minus_reward;
$current_reward = (int) $rewards->current_reward;
$sisa_reward = (int) $rewards->sisa_reward;
if (($sisa_reward + $minus_point) == $current_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);
}
} //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, id')->from('stock')->where('id_product', $item->product_id)->where('id_product_detail', $item->item_id)->where('warehouse_id', $item->warehouse_id);
$current_stock = $this->db->get()->row();
$qty = 0;
if ($item->is_backorder == 'yes') {
$qty = $qty + 0;
} else {
$qty = $qty + $item->quantity;
}
$data = array(
'stock' => $current_stock->stock + $qty,
);
$this->db->where('id_product', $item->product_id);
$this->db->where('id_product_detail', $item->item_id);
$this->db->where('warehouse_id', $item->warehouse_id);
$this->db->update('stock', $data);
//get $stock_id
$stock_id = $current_stock->id;
$last_stock = $current_stock->stock + $qty;
//update stock_movement_table
$movement_data = array(
'stock_id' => $stock_id,
'type' => '+',
'stock_change' => (int) $qty,
'remark' => 'Cancel by system Order No: ' . $order->id_orders . ' (' . $last_stock . ')',
'total' => (int) $last_stock,
'name' => 'System'
);
$this->db->insert('stock_movement', $movement_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('current_reward,sisa_reward,minus_reward')->from('orders')->where('id_orders', $order->id_orders);
$rewards = $this->db->get()->row();
$minus_point = (int) $rewards->minus_reward;
$current_reward = (int) $rewards->current_reward;
$sisa_reward = (int) $rewards->sisa_reward;
if (($sisa_reward + $minus_point) == $current_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)...
}
public function cancel_expired_orders() {
// Verifikasi token untuk keamanan
$token = $this->input->get('token');
if ($token !== $this->cron_token) {
show_error('Unauthorized access', 403);
return;
}
// Jalankan fungsi pembatalan
$canceled = $this->order_m->cancel_expired_orders();
// Output hasil
// echo "Berhasil membatalkan $canceled pesanan yang expired.\n";
// Log untuk catatan admin
log_message('info', "Cronjob: $canceled pesanan dibatalkan pada " . date('Y-m-d H:i:s'));
}
}