1003 lines
36 KiB
PHP
1003 lines
36 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Backend;
|
|
|
|
use App\Models\Orders;
|
|
use App\Models\Customer;
|
|
use App\Models\TopupSettings;
|
|
use App\Models\VoucherSchedule;
|
|
use App\Models\VoucherSettings;
|
|
use CodeIgniter\Database\Config;
|
|
use App\Models\VoucherRedemptions;
|
|
use App\Models\CustomerVoucherList;
|
|
use App\Models\PromoSetting;
|
|
use App\Models\SettingCustomerType;
|
|
use App\Models\SettingMembershipTier;
|
|
use App\Models\VoucherPoints;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use CodeIgniter\RESTful\ResourceController;
|
|
use PDO;
|
|
|
|
class VoucherController extends ResourceController
|
|
{
|
|
|
|
private $voucher_points;
|
|
private $voucher_schedules;
|
|
private $customer_voucher_lists;
|
|
private $voucher_redemptions;
|
|
private $promo_setting;
|
|
|
|
private $customers;
|
|
private $orders;
|
|
private $topups;
|
|
|
|
private $membership_tier;
|
|
private $customer_type;
|
|
|
|
public function __construct()
|
|
{
|
|
helper('voucher');
|
|
helper('order');
|
|
$this->voucher_points = new VoucherPoints();
|
|
$this->voucher_schedules = new VoucherSchedule();
|
|
$this->customer_voucher_lists = new CustomerVoucherList();
|
|
$this->voucher_redemptions = new VoucherRedemptions();
|
|
$this->promo_setting = new PromoSetting();
|
|
|
|
$this->customers = new Customer();
|
|
$this->orders = new Orders();
|
|
$this->topups = new TopupSettings();
|
|
|
|
$this->membership_tier = new SettingMembershipTier();
|
|
$this->customer_type = new SettingCustomerType();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
|
|
$voucherName = $this->request->getGet('voucher_name');
|
|
$dateFrom = $this->request->getGet('date_from');
|
|
$dateTo = $this->request->getGet('date_to');
|
|
|
|
$builder = $this->voucher_points->builder(); // 👈 this is the fix
|
|
|
|
$builder->where('voucher_status !=', 'trash');
|
|
|
|
if ($voucherName) {
|
|
$builder->like('voucher_name', $voucherName);
|
|
}
|
|
if ($dateFrom && $dateTo) {
|
|
$builder->where('voucher_expired_date >=', $dateFrom);
|
|
$builder->where('voucher_expired_date <=', $dateTo);
|
|
}
|
|
|
|
// ✅ Print the generated SQL
|
|
// echo $builder->getCompiledSelect();
|
|
// exit;
|
|
|
|
// All transactions
|
|
$voucherSettings = $builder->get()->getResult();
|
|
|
|
if (empty($voucherSettings)) {
|
|
return $this->respond(["status" => 400, "message" => "No Voucher Found", "data" => []]);
|
|
}
|
|
|
|
return $this->respond(["status" => 200, "message" => "Successfully retrive data!", "data" => $voucherSettings]);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$validation = $this->validate([
|
|
'voucher_name' => 'required',
|
|
'voucher_total_count' => 'required',
|
|
'voucher_point_redeem' => 'required',
|
|
'promo_setting_id' => 'required',
|
|
'voucher_expired_date' => 'permit_empty',
|
|
'voucher_image' => 'permit_empty|uploaded[voucher_image]',
|
|
'voucher_details' => 'required',
|
|
'voucher_tnc' => 'permit_empty',
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$promo_setting_id = $this->request->getVar('promo_setting_id');
|
|
$promo_exist = $this->promo_setting->find($promo_setting_id);
|
|
if(empty($promo_exist)){
|
|
return $this->fail('Promo setting is not valid!' , 400);
|
|
}
|
|
|
|
// Handle Image Upload
|
|
$image = $this->request->getFile('voucher_image');
|
|
$imagePath = null;
|
|
if ($image && $image->isValid() && !$image->hasMoved()) {
|
|
$uploadPath = $_SERVER['DOCUMENT_ROOT'] . '/backend/uploads/vouchers/';
|
|
if (!is_dir($uploadPath)) {
|
|
mkdir($uploadPath, 0755, true);
|
|
}
|
|
|
|
$newImageName = time() . '_' . uniqid() . '.' . $image->getClientExtension();
|
|
|
|
try {
|
|
$image->move($uploadPath, $newImageName);
|
|
$imagePath = $newImageName;
|
|
} catch (\Exception $e) {
|
|
log_message('error', 'Voucher image upload failed: ' . $e->getMessage());
|
|
return $this->failServerError('Voucher image upload failed.');
|
|
}
|
|
}
|
|
|
|
// Get POST values
|
|
$data = [
|
|
'voucher_name' => $this->request->getVar('voucher_name'),
|
|
'voucher_total_count' => $this->request->getVar('voucher_total_count'),
|
|
'voucher_point_redeem' => $this->request->getVar('voucher_point_redeem'),
|
|
'voucher_redeem_count' => $this->request->getVar('voucher_redeem_count'),
|
|
'voucher_count_customer' => $this->request->getVar('voucher_count_customer'),
|
|
'promo_setting_id' => $promo_setting_id,
|
|
'voucher_details' => $this->request->getVar('voucher_details'),
|
|
'voucher_tnc' => $this->request->getVar('voucher_tnc'),
|
|
'voucher_expired_date' => $this->request->getVar('voucher_expired_date'),
|
|
];
|
|
|
|
if ($imagePath) {
|
|
$data['voucher_image'] = $imagePath;
|
|
}
|
|
|
|
// Insert into DB
|
|
$id = $this->voucher_points->insert($data);
|
|
|
|
if (!$id) {
|
|
return $this->fail('Fail to create voucher!', 400);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'message' => 'Voucher successfully created!'
|
|
]);
|
|
}
|
|
|
|
public function update($id = null)
|
|
{
|
|
// Find the voucher setting first
|
|
$voucher = $this->voucher_points->find($id);
|
|
|
|
if (!$voucher) {
|
|
return $this->fail('No voucher found!', 400);
|
|
}
|
|
|
|
// Run validation
|
|
$validation = $this->validate([
|
|
'voucher_name' => 'required',
|
|
'voucher_total_count' => 'required',
|
|
'voucher_redeem_count' => 'required',
|
|
'voucher_count_customer' => 'required',
|
|
'voucher_expiry_type' => 'permit_empty',
|
|
'voucher_expiry_value' => 'permit_empty',
|
|
'voucher_expired_date' => 'permit_empty',
|
|
'voucher_point_redeem' => 'required',
|
|
'promo_setting_id' => 'required',
|
|
'voucher_image' => 'permit_empty|uploaded[voucher_image]',
|
|
'voucher_details' => 'permit_empty',
|
|
'voucher_tnc' => 'permit_empty',
|
|
'voucher_status' => 'required',
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$promo_setting_id = $this->request->getVar('promo_setting_id');
|
|
$promo_exist = $this->promo_setting->find($promo_setting_id);
|
|
if(empty($promo_exist)){
|
|
return $this->fail('Promo setting is not valid!' , 400);
|
|
}
|
|
|
|
|
|
// Handle Image Upload
|
|
$image = $this->request->getFile('voucher_image');
|
|
$imagePath = null;
|
|
if ($image && $image->isValid() && !$image->hasMoved()) {
|
|
$uploadPath = $_SERVER['DOCUMENT_ROOT'] . '/backend/uploads/vouchers/';
|
|
if (!is_dir($uploadPath)) {
|
|
mkdir($uploadPath, 0755, true);
|
|
}
|
|
|
|
$newImageName = time() . '_' . uniqid() . '.' . $image->getClientExtension();
|
|
|
|
try {
|
|
$image->move($uploadPath, $newImageName);
|
|
$imagePath = $newImageName;
|
|
} catch (\Exception $e) {
|
|
log_message('error', 'Voucher image upload failed: ' . $e->getMessage());
|
|
return $this->failServerError('Voucher image upload failed.');
|
|
}
|
|
}
|
|
|
|
// Prepare update data
|
|
$data = [
|
|
'voucher_name' => $this->request->getVar('voucher_name'),
|
|
'voucher_total_count' => $this->request->getVar('voucher_total_count'),
|
|
'voucher_redeem_count' => $this->request->getVar('voucher_redeem_count'),
|
|
'voucher_count_customer' => $this->request->getVar('voucher_count_customer'),
|
|
'voucher_expiry_type' => $this->request->getVar('voucher_expiry_type'),
|
|
'voucher_expiry_value' => $this->request->getVar('voucher_expiry_value'),
|
|
'voucher_expired_date' => $this->request->getVar('voucher_expired_date'),
|
|
'voucher_point_redeem' => $this->request->getVar('voucher_point_redeem'),
|
|
'promo_setting_id' => $promo_setting_id,
|
|
'voucher_details' => $this->request->getVar('voucher_details'),
|
|
'voucher_tnc' => $this->request->getVar('voucher_tnc'),
|
|
'voucher_status' => $this->request->getVar('voucher_status')
|
|
];
|
|
|
|
if ($imagePath) {
|
|
$data['voucher_image'] = $imagePath;
|
|
}
|
|
|
|
if ($this->voucher_points->update($id, $data)) {
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'message' => 'Voucher successfully updated!'
|
|
]);
|
|
} else {
|
|
return $this->fail([
|
|
'status' => 400,
|
|
'message' => 'Failed to update voucher. Please try again.'
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function delete($id = null)
|
|
{
|
|
$voucher_setting = $this->voucher_points->find($id);
|
|
|
|
if ($voucher_setting) {
|
|
$this->voucher_points->update($id, [
|
|
'voucher_status' => 'trash',
|
|
'deleted_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Voucher successfully deleted!']);
|
|
} else {
|
|
return $this->fail('No voucher found!', 400);
|
|
}
|
|
}
|
|
|
|
public function show($id = null)
|
|
{
|
|
$voucherSettings = $this->voucher_points->find($id);
|
|
|
|
if (empty($voucherSettings)) {
|
|
return $this->fail("No voucher settings found.", 400);
|
|
}
|
|
|
|
$voucherSettingData = [];
|
|
|
|
if ($voucherSettings) {
|
|
$voucherSettingData[] = [
|
|
'id' => $voucherSettings['id'],
|
|
'voucher_name' => $voucherSettings['voucher_name'],
|
|
'voucher_total_count' => $voucherSettings['voucher_total_count'],
|
|
'voucher_redeem_count' => $voucherSettings['voucher_redeem_count'],
|
|
'voucher_count_customer' => $voucherSettings['voucher_count_customer'],
|
|
'voucher_expiry_type' => $voucherSettings['voucher_expiry_type'],
|
|
'voucher_expiry_value' => $voucherSettings['voucher_expiry_value'],
|
|
'voucher_expired_date' => $voucherSettings['voucher_expired_date'],
|
|
'voucher_point_redeem' => $voucherSettings['voucher_point_redeem'],
|
|
'promo_setting_id'=> $voucherSettings['promo_setting_id'],
|
|
'voucher_details'=> $voucherSettings['voucher_details'],
|
|
'voucher_tnc'=> $voucherSettings['voucher_tnc'],
|
|
'voucher_status'=> $voucherSettings['voucher_status'],
|
|
'voucher_image_url'=> getImagePath('vouchers', $voucherSettings['voucher_image']),
|
|
];
|
|
}
|
|
return $this->respond(["status" => 200, "message" => "Successfully retrive data!", "data" => $voucherSettingData]);
|
|
}
|
|
|
|
function indexVoucherSchedule() {
|
|
$voucherSchedules = $this->voucher_schedules
|
|
->where('deleted_at', null)
|
|
->findAll();
|
|
|
|
if (empty($voucherSchedules)) {
|
|
return $this->fail("No Voucher Schedule found.", 400);
|
|
}
|
|
return $this->respond(["status" => 200, "message" => "Successfully retrive data!", "data" => $voucherSchedules]);
|
|
}
|
|
|
|
public function createVoucherSchedule()
|
|
{
|
|
$validation = $this->validate([
|
|
'promo_setting_id' => 'required',
|
|
'voucher_schedule_mode' => 'required',
|
|
'voucher_date_type' => 'required',
|
|
'filter_membership' => 'required',
|
|
'filter_customer_type' => 'required',
|
|
'schedule_date' => 'required',
|
|
'schedule_time' => 'required',
|
|
'quantity' => 'required',
|
|
'voucher_expiration' => 'permit_empty',
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$promo_setting_id = $this->request->getVar('promo_setting_id');
|
|
|
|
// Check if the referenced voucher setting exists
|
|
$curr_promo_setting = $this->promo_setting
|
|
->where('id', $promo_setting_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
|
|
if (!$curr_promo_setting) {
|
|
return $this->fail('The voucher setting ID does not exist.', 400);
|
|
}
|
|
|
|
// Prepare data
|
|
$data = [
|
|
'promo_setting_id' => $promo_setting_id,
|
|
'voucher_schedule_mode' => $this->request->getVar('voucher_schedule_mode'),
|
|
'voucher_date_type' => $this->request->getVar('voucher_date_type'),
|
|
'filter_membership' => $this->request->getVar('filter_membership'),
|
|
'filter_customer_type' => $this->request->getVar('filter_customer_type'),
|
|
'schedule_date' => $this->request->getVar('schedule_date'),
|
|
'schedule_time' => $this->request->getVar('schedule_time'),
|
|
'quantity' => $this->request->getVar('quantity'),
|
|
'voucher_expiration' => $this->request->getVar('voucher_expiration'),
|
|
];
|
|
|
|
// Insert into DB
|
|
$id = $this->voucher_schedules->insert($data);
|
|
|
|
if (!$id) {
|
|
return $this->fail('Failed to create voucher schedule!', 400);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'message' => 'Voucher schedule successfully created!'
|
|
]);
|
|
}
|
|
|
|
public function updateVoucherSchedule($id = null)
|
|
{
|
|
// Find the voucher setting first
|
|
$voucher_schedules = $this->voucher_schedules->find($id);
|
|
|
|
if (!$voucher_schedules) {
|
|
return $this->fail('No voucher schedule found!', 400);
|
|
}
|
|
|
|
// Run validation
|
|
$validation = $this->validate([
|
|
'promo_setting_id' => 'required',
|
|
'voucher_schedule_mode' => 'required',
|
|
'voucher_date_type' => 'required',
|
|
'filter_membership' => 'required',
|
|
'filter_customer_type' => 'required',
|
|
'schedule_date' => 'required',
|
|
'schedule_time' => 'required',
|
|
'quantity' => 'required',
|
|
'voucher_expiration' => 'permit_empty',
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$promo_setting_id = $this->request->getVar('promo_setting_id');
|
|
|
|
// Check if the referenced voucher setting exists
|
|
$curr_promo_setting = $this->promo_setting
|
|
->where('id', $promo_setting_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
|
|
if (!$curr_promo_setting) {
|
|
return $this->fail('The voucher setting ID does not exist.', 400);
|
|
}
|
|
|
|
$data = [
|
|
'promo_setting_id' => $promo_setting_id,
|
|
'voucher_schedule_mode' => $this->request->getVar('voucher_schedule_mode'),
|
|
'voucher_date_type' => $this->request->getVar('voucher_date_type'),
|
|
'filter_membership' => $this->request->getVar('filter_membership'),
|
|
'filter_customer_type' => $this->request->getVar('filter_customer_type'),
|
|
'schedule_date' => $this->request->getVar('schedule_date'),
|
|
'schedule_time' => $this->request->getVar('schedule_time'),
|
|
'quantity' => $this->request->getVar('quantity'),
|
|
'voucher_expiration' => $this->request->getVar('voucher_expiration'),
|
|
];
|
|
|
|
// Perform the update
|
|
$this->voucher_schedules->update($id, $data);
|
|
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'message' => 'Voucher Schedule successfully updated!'
|
|
]);
|
|
}
|
|
|
|
function deleteVoucherSchedule($id = null)
|
|
{
|
|
$voucher_schedule = $this->voucher_schedules->find($id);
|
|
|
|
if ($voucher_schedule) {
|
|
$this->voucher_schedules->update($id, [
|
|
'deleted_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Voucher Schedule successfully deleted!']);
|
|
} else {
|
|
return $this->fail('No voucher schedule found!', 400);
|
|
}
|
|
}
|
|
|
|
public function showVoucherSchedule($id = null)
|
|
{
|
|
$voucherSchedules = $this->voucher_schedules->find($id);
|
|
|
|
if (empty($voucherSchedules)) {
|
|
return $this->fail("No Voucher Schedule found.", 400);
|
|
}
|
|
return $this->respond(["status" => 200, "message" => "Successfully retrive data!", "data" => $voucherSchedules]);
|
|
}
|
|
|
|
public function sendVoucher()
|
|
{
|
|
$currentDate = date('Y-m-d');
|
|
$currentTime = date('H:i');
|
|
|
|
$voucherSchedules = $this->voucher_schedules
|
|
->where('deleted_at', null)
|
|
->findAll();
|
|
// print_r($voucherSchedules);exit;
|
|
$result = [];
|
|
//mode daily - monthly
|
|
//date type - birthday, membership
|
|
foreach ($voucherSchedules as $schedule) {
|
|
$schedule_date = $schedule['schedule_date'];
|
|
$schedule_time = $schedule['schedule_time'];
|
|
$mode = $schedule['voucher_schedule_mode'];
|
|
$date_type = $schedule['voucher_date_type'];
|
|
|
|
$flag = false;
|
|
|
|
if($mode == 'daily'){
|
|
if(substr($currentTime, 0, 5) == substr($schedule_time, 0, 5)){
|
|
$flag = true;
|
|
}
|
|
}else if($mode == 'monthly'){
|
|
if($schedule_date == $currentDate && substr($currentTime, 0, 5) == substr($schedule_time, 0, 5)){
|
|
$flag = true;
|
|
}
|
|
}
|
|
|
|
if($flag){
|
|
$send_quantity = $schedule['quantity'];
|
|
$voucher_expiration = $schedule['voucher_expiration'];
|
|
$voucher_expire_date = date('Y-m-d H:i:s', strtotime("+{$voucher_expiration} days"));
|
|
$filter_membership = array_filter(explode(',', $schedule['filter_membership']));
|
|
$filter_customer_type_id = array_filter(explode(',', $schedule['filter_customer_type']));
|
|
|
|
$filter_customer_type_name = [];
|
|
if (!empty($filter_customer_type_id)) {
|
|
$filter_customer_type_name = $this->customer_type
|
|
->select('name')
|
|
->whereIn('id', $filter_customer_type_id)
|
|
->get()
|
|
->getResultArray();
|
|
|
|
$filter_customer_type_name = array_column($filter_customer_type_name, 'name');
|
|
}
|
|
|
|
$customer_list = [];
|
|
|
|
$builder = $this->customers->where('deleted_at', null);
|
|
|
|
$month = date('m');
|
|
|
|
switch($date_type){
|
|
case 'membership':
|
|
$builder->where("DATE_FORMAT(created_at, '%m')", $month);
|
|
break;
|
|
case 'birthday':
|
|
$builder->where("DATE_FORMAT(birthday, '%m')", $month);
|
|
break;
|
|
}
|
|
|
|
if (!empty($filter_customer_type_name) || !empty($filter_membership)) {
|
|
$builder->groupStart();
|
|
|
|
if (!empty($filter_customer_type_name)) {
|
|
$builder->whereIn('customer_type', $filter_customer_type_name);
|
|
}
|
|
|
|
if (!empty($filter_membership)) {
|
|
$builder->whereIn('customer_tier_id', $filter_membership);
|
|
}
|
|
|
|
$builder->groupEnd();
|
|
}
|
|
|
|
$customer_list = $builder->findAll();
|
|
|
|
foreach($customer_list as $customer){
|
|
|
|
for($i = 0 ; $i < $send_quantity ; $i++){
|
|
$voucherCode = strtoupper(uniqid('VCH'));
|
|
|
|
$inserted = $this->customer_voucher_lists->insert([
|
|
'customer_id' => $customer['id'],
|
|
'promo_setting_id' => $schedule['promo_setting_id'],
|
|
'voucher_code' => $voucherCode,
|
|
'voucher_expiry_date' => $voucher_expire_date,
|
|
'voucher_status' => 'active',
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
if ($inserted) {
|
|
|
|
$matched[] = [
|
|
'customer' => [
|
|
'id' => $customer['id'],
|
|
'name' => $customer['name'],
|
|
'email' => $customer['email'],
|
|
'customer_type' => $customer['customer_type'],
|
|
'customer_tier_id' => $customer['customer_tier_id'],
|
|
],
|
|
'voucher_code' => $voucherCode
|
|
];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (!empty($matched)) {
|
|
$result[] = [
|
|
'schedule_id' => $schedule['id'],
|
|
'schedule_time' => $schedule['schedule_date'] . ' ' . $schedule['schedule_time'],
|
|
'promo_setting_id' => $schedule['promo_setting_id'],
|
|
'matched_customers' => $matched
|
|
];
|
|
}
|
|
}
|
|
|
|
if (empty($result)) {
|
|
return $this->respond([
|
|
'status' => 404,
|
|
'message' => 'No matched customers found for any voucher schedule.'
|
|
]);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'message' => 'Vouchers sent to matched customers',
|
|
'data' => $result
|
|
]);
|
|
}
|
|
|
|
function createCustomerVoucherList()
|
|
{
|
|
$validation = $this->validate([
|
|
'customer_id' => 'required',
|
|
'promo_setting_id' => 'required',
|
|
'voucher_order_id' => 'required',
|
|
'voucher_topup_id' => 'permit_empty',
|
|
'voucher_code' => 'required',
|
|
'voucher_expired_date' => 'permit_empty',
|
|
'voucher_status' => 'required',
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$customer_id = $this->request->getVar('customer_id');
|
|
|
|
// Check if the referenced customer exists
|
|
$customer = $this->customers
|
|
->where('id', $customer_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
|
|
if (!$customer) {
|
|
return $this->fail('The customer does not exist.', 400);
|
|
}
|
|
|
|
$promo_setting_id = $this->request->getVar('promo_setting_id');
|
|
|
|
// Check if the referenced voucher setting exists
|
|
$voucher_setting = $this->promo_setting
|
|
->where('id', $promo_setting_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
|
|
if (!$voucher_setting) {
|
|
return $this->fail('The voucher setting does not exist.', 400);
|
|
}
|
|
|
|
$order_id = $this->request->getVar('voucher_order_id');
|
|
|
|
// Check if the referenced order exists
|
|
$order = $this->orders
|
|
->where('id', $order_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
|
|
if (!$order) {
|
|
return $this->fail('The order does not exist.', 400);
|
|
}
|
|
|
|
$topup_id = $this->request->getVar('voucher_topup_id');
|
|
|
|
|
|
if (!empty($topup_id)) {
|
|
// Check if the referenced topup exists
|
|
$topup = $this->topups
|
|
->where('id', $topup_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
if (!$topup) {
|
|
return $this->fail('The topup does not exist.', 400);
|
|
}
|
|
}
|
|
|
|
// Prepare data
|
|
$data = [
|
|
'customer_id' => $customer_id,
|
|
'promo_setting_id' => $promo_setting_id,
|
|
'voucher_order_id' => $order_id,
|
|
'voucher_topup_id' => $topup_id,
|
|
'voucher_code' => $this->request->getVar('voucher_code'),
|
|
'voucher_expired_date' => $this->request->getVar('voucher_expired_date'),
|
|
'voucher_status' => $this->request->getVar('voucher_status'),
|
|
];
|
|
|
|
// Insert into DB
|
|
$id = $this->customer_voucher_lists->insert($data);
|
|
|
|
if (!$id) {
|
|
return $this->fail('Failed to create customer voucher list!', 400);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'message' => 'Customer Voucher List successfully created!'
|
|
]);
|
|
}
|
|
|
|
public function updateCustomerVoucherList($id = null)
|
|
{
|
|
// Find the voucher setting first
|
|
$customer_voucher_lists = $this->customer_voucher_lists->find($id);
|
|
|
|
if (!$customer_voucher_lists) {
|
|
return $this->fail('No Customer Voucher List found!', 400);
|
|
}
|
|
|
|
$validation = $this->validate([
|
|
'customer_id' => 'required',
|
|
'promo_setting_id' => 'required',
|
|
'voucher_order_id' => 'required',
|
|
'voucher_topup_id' => 'permit_empty',
|
|
'voucher_code' => 'required',
|
|
'voucher_expired_date' => 'permit_empty',
|
|
'voucher_status' => 'required',
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$customer_id = $this->request->getVar('customer_id');
|
|
|
|
// Check if the referenced customer exists
|
|
$customer = $this->customers
|
|
->where('id', $customer_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
|
|
if (!$customer) {
|
|
return $this->fail('The customer does not exist.', 400);
|
|
}
|
|
|
|
$promo_setting_id = $this->request->getVar('voucher_setting_id');
|
|
|
|
// Check if the referenced voucher setting exists
|
|
$voucher_setting = $this->promo_setting
|
|
->where('id', $promo_setting_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
|
|
if (!$voucher_setting) {
|
|
return $this->fail('The voucher setting does not exist.', 400);
|
|
}
|
|
|
|
$order_id = $this->request->getVar('voucher_order_id');
|
|
|
|
// Check if the referenced order exists
|
|
$order = $this->orders
|
|
->where('id', $order_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
|
|
if (!$order) {
|
|
return $this->fail('The order does not exist.', 400);
|
|
}
|
|
|
|
$topup_id = $this->request->getVar('voucher_topup_id');
|
|
|
|
if (!empty($topup_id)) {
|
|
// Check if the referenced topup exists
|
|
$topup = $this->topups
|
|
->where('id', $topup_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
if (!$topup) {
|
|
return $this->fail('The topup does not exist.', 400);
|
|
}
|
|
}
|
|
// Prepare data
|
|
$data = [
|
|
'customer_id' => $customer_id,
|
|
'promo_setting_id' => $promo_setting_id,
|
|
'voucher_order_id' => $order_id,
|
|
'voucher_topup_id' => $topup_id,
|
|
'voucher_code' => $this->request->getVar('voucher_code'),
|
|
'voucher_expired_date' => $this->request->getVar('voucher_expired_date'),
|
|
'voucher_status' => $this->request->getVar('voucher_status'),
|
|
];
|
|
|
|
// Perform the update
|
|
$this->customer_voucher_lists->update($id, $data);
|
|
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'message' => 'Customer Voucher List successfully updated!'
|
|
]);
|
|
}
|
|
|
|
function deleteCustomerVoucherList($id = null)
|
|
{
|
|
$customer_voucher_list = $this->customer_voucher_lists->find($id);
|
|
|
|
if ($customer_voucher_list) {
|
|
$this->customer_voucher_lists->update($id, [
|
|
'voucher_status' => 'trash',
|
|
'deleted_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
// $this->customer_voucher_lists->delete($id);
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Customer Voucher List successfully deleted!']);
|
|
} else {
|
|
return $this->fail('No Customer Voucher List found!', 400);
|
|
}
|
|
}
|
|
|
|
function createVoucherRedemption()
|
|
{
|
|
$validation = $this->validate([
|
|
'customer_voucher_list_id' => 'required',
|
|
'order_id' => 'required',
|
|
'customer_id' => 'required',
|
|
'redeemed_at' => 'required',
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$customer_voucher_list_id = $this->request->getVar('customer_voucher_list_id');
|
|
|
|
// Check if the referenced customer voucher list exists
|
|
$customer_voucher_list = $this->customer_voucher_lists
|
|
->where('id', $customer_voucher_list_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
|
|
if (!$customer_voucher_list) {
|
|
return $this->fail('The customer voucher list does not exist.', 400);
|
|
}
|
|
|
|
$customer_id = $this->request->getVar('customer_id');
|
|
|
|
// Check if the referenced customer exists
|
|
$customer = $this->customers
|
|
->where('id', $customer_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
|
|
if (!$customer) {
|
|
return $this->fail('The customer does not exist.', 400);
|
|
}
|
|
|
|
$order_id = $this->request->getVar('order_id');
|
|
|
|
// Check if the referenced order exists
|
|
$order = $this->orders
|
|
->where('id', $order_id)
|
|
->where('deleted_at', NULL)
|
|
->first();
|
|
|
|
if (!$order) {
|
|
return $this->fail('The order does not exist.', 400);
|
|
}
|
|
|
|
// Prepare data
|
|
$data = [
|
|
'customer_voucher_list_id' => $customer_voucher_list_id,
|
|
'order_type' => getOrderType($order_id),
|
|
'order_id' => $order_id,
|
|
'customer_id' => $customer_id,
|
|
'redeemed_at' => $this->request->getVar('redeemed_at'),
|
|
];
|
|
|
|
// Insert into DB
|
|
$id = $this->voucher_redemptions->insert($data);
|
|
|
|
if (!$id) {
|
|
return $this->fail('Failed to create Voucher Redemption!', 400);
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'message' => 'Voucher Redemption successfully created!'
|
|
]);
|
|
}
|
|
|
|
function deleteVoucherRedemption($id = null)
|
|
{
|
|
$voucher_redemption = $this->voucher_redemptions->find($id);
|
|
|
|
if ($voucher_redemption) {
|
|
$this->voucher_redemptions->update($id, [
|
|
'deleted_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
// $this->voucher_redemptions->delete($id);
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Voucher Redemption successfully deleted!']);
|
|
} else {
|
|
return $this->fail('No Voucher Redemption found!', 400);
|
|
}
|
|
}
|
|
|
|
function voucherRedemptionList(){
|
|
$voucher_redemption = $this->voucher_redemptions->findAll();
|
|
|
|
if(empty($voucher_redemption)) {
|
|
return $this->fail('No Voucher Redemption found!', 400);
|
|
}
|
|
|
|
$voucher_data = array();
|
|
foreach ($voucher_redemption as $voucher) {
|
|
$voucher_detail = $this->customer_voucher_lists->find($voucher['customer_voucher_list_id']);
|
|
$customer = $this->customers->find($voucher['customer_id']);
|
|
$voucher_data[] = [
|
|
'id' => $voucher['id'],
|
|
'voucher_detail'=> $voucher_detail,
|
|
'order_type'=> $voucher['order_type'],
|
|
'order_id' => $voucher['order_id'],
|
|
'customer'=> [
|
|
'customer_id'=> $customer['id'],
|
|
'customer_name'=> $customer['name'],
|
|
],
|
|
];
|
|
}
|
|
|
|
return $this->respond(['status' => 200 , 'message'=> 'Voucher Redemption Data Retrieve Successfully','data'=> $voucher_data]);
|
|
}
|
|
|
|
function voucherRedemptionDetail($id = null){
|
|
$voucher_redemption = $this->voucher_redemptions->find($id);
|
|
|
|
if(empty($voucher_redemption)) {
|
|
return $this->fail('No Voucher Redemption found!', 400);
|
|
}
|
|
|
|
$voucher_data = array();
|
|
if ($voucher_redemption) {
|
|
$voucher_detail = $this->customer_voucher_lists->find($voucher_redemption['customer_voucher_list_id']);
|
|
$customer = $this->customers->find($voucher_redemption['customer_id']);
|
|
$order = getFullOrderDetail($voucher_redemption['order_id']);
|
|
$voucher_data[] = [
|
|
'id' => $voucher_redemption['id'],
|
|
'voucher_detail'=> $voucher_detail,
|
|
'order_type'=> $voucher_redemption['order_type'],
|
|
'order' => $order,
|
|
'customer'=> [
|
|
'customer_id'=> $customer['id'],
|
|
'customer_name'=> $customer['name'],
|
|
],
|
|
];
|
|
}
|
|
|
|
return $this->respond(['status' => 200 , 'message'=> 'Voucher Redemption Data Retrieve Successfully','data'=> $voucher_data]);
|
|
}
|
|
|
|
public function getMemberList()
|
|
{
|
|
$customer_phone = $this->request->getVar('customer_phone');
|
|
$customer_tier_id = $this->request->getVar('customer_tier_id');
|
|
$customer_type = $this->request->getVar('customer_type');
|
|
$birthday_month = $this->request->getVar('birthday_month');
|
|
|
|
$customer = $this->customers->select('id, name, customer_type, customer_tier_id, phone, birthday, status');
|
|
|
|
if ($customer_phone) {
|
|
// Use LIKE for partial matching
|
|
$customer = $customer->like('phone', $customer_phone);
|
|
}
|
|
|
|
if ($customer_tier_id) {
|
|
$customer = $customer->where('customer_tier_id', $customer_tier_id);
|
|
}
|
|
|
|
if ($customer_type) {
|
|
$customer = $customer->where('customer_type', $customer_type);
|
|
}
|
|
|
|
if ($birthday_month && $birthday_month != 'all') {
|
|
// Extract month from birthday (stored as YYYY-MM-DD) and compare with input
|
|
$customer = $customer->where('MONTH(birthday)', $birthday_month);
|
|
}
|
|
|
|
$customer = $customer->where('deleted_at', NULL)->findAll();
|
|
|
|
return $this->respond(['status' => 200, 'message' => 'Successfully retrive data!', 'data' => $customer]);
|
|
}
|
|
|
|
public function sendVoucherByFilteredMemberList()
|
|
{
|
|
|
|
$validation = $this->validate([
|
|
'customer_ids' => 'required',
|
|
'expired_date' => 'required',
|
|
'promo_id' => 'required',
|
|
'quantity' => 'required',
|
|
]);
|
|
|
|
if (!$validation) {
|
|
return $this->failValidationErrors($this->validator->getErrors());
|
|
}
|
|
|
|
$customer_ids = $this->request->getVar('customer_ids');
|
|
$expired_date = $this->request->getVar('expired_date');
|
|
$promo_setting_id = $this->request->getVar('promo_id');
|
|
$quantity = $this->request->getVar('quantity');
|
|
|
|
try {
|
|
foreach($customer_ids as $customer_id) {
|
|
for($i = 1; $i <= $quantity; $i++) {
|
|
$voucherCode = strtoupper(uniqid('VCH'));
|
|
$voucher_data = [
|
|
'customer_id' => $customer_id,
|
|
'promo_setting_id' => $promo_setting_id,
|
|
'voucher_expiry_date'=> $expired_date,
|
|
'voucher_code' => $voucherCode,
|
|
'voucher_order_id' => 0,
|
|
'voucher_topup_id' => 0,
|
|
'voucher_status' => 'active'
|
|
];
|
|
|
|
if (! $this->customer_voucher_lists->insert($voucher_data)) {
|
|
throw new \Exception("Failed to insert voucher for customer ID: {$customer_id}");
|
|
}
|
|
}
|
|
}
|
|
|
|
return $this->respond([
|
|
'status' => 200,
|
|
'message' => 'Vouchers sent successfully',
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return $this->failServerError('Error sending vouchers: ' . $e->getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
}
|