initial project

This commit is contained in:
2025-11-06 13:41:06 +08:00
commit 1b08727cb2
209 changed files with 31602 additions and 0 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,274 @@
<?php
namespace App\Controllers\Backend;
use CodeIgniter\RESTful\ResourceController;
use App\Models\Orders;
use App\Models\TopupModel;
use App\Models\CustomerWallet;
use App\Models\CustomerVoucherList;
use App\Models\CustomerPoint;
use App\Models\LogCustomerCheckin; // Add LogCustomerCheckin model
class CustomerDashboardController extends ResourceController
{
private $orders;
private $topups;
private $wallets;
private $vouchers;
private $points;
private $checkins; // Add checkins model
public function __construct()
{
$this->orders = new Orders();
$this->topups = new TopupModel();
$this->wallets = new CustomerWallet();
$this->vouchers = new CustomerVoucherList();
$this->points = new CustomerPoint();
$this->checkins = new LogCustomerCheckin(); // Initialize checkins model
}
public function show($customerId = null)
{
if (!$customerId) {
return $this->fail('Customer ID is required', 400);
}
$orderSummary = $this->orders
->select('customer_id, COUNT(id) as total_orders, SUM(grand_total) as total_grand')
->where('customer_id', $customerId)
->groupBy('customer_id')
->first();
$topupSummary = $this->topups
->select('customer_id, COUNT(id) as total_topups, SUM(amount) as total_topup_amount')
->where('customer_id', $customerId)
->groupBy('customer_id')
->first();
$pointsSummary = $this->points
->select('customer_id,
COUNT(id) as total_point_transactions,
SUM(`in`) as total_point_in,
SUM(`out`) as total_point_out,
MAX(balance) as current_points')
->where('customer_id', $customerId)
->groupBy('customer_id')
->first();
$walletSummary = $this->wallets
->select('customer_id, SUM(`in`) as total_in, SUM(`out`) as total_out, MAX(balance) as current_balance')
->where('customer_id', $customerId)
->groupBy('customer_id')
->first();
$voucherSummary = $this->vouchers
->select('customer_id,
COUNT(id) as total_vouchers,
SUM(CASE WHEN voucher_status = "active" THEN 1 ELSE 0 END) as active_vouchers,
SUM(CASE WHEN voucher_status = "used" THEN 1 ELSE 0 END) as used_vouchers,
SUM(CASE WHEN voucher_status = "expired" THEN 1 ELSE 0 END) as expired_vouchers')
->where('customer_id', $customerId)
->groupBy('customer_id')
->first();
$activeVouchers = $this->vouchers
->where('customer_id', $customerId)
->where('voucher_status', 'active')
->where('voucher_expiry_date >=', date('Y-m-d H:i:s'))
->orderBy('voucher_expiry_date', 'ASC')
->findAll();
// Get check-in data
$checkinData = $this->checkins
->select('customer_id,
COUNT(id) as total_checkins,
MAX(streak_count) as highest_streak,
MAX(checkin_date) as last_checkin_date')
->where('customer_id', $customerId)
->groupBy('customer_id')
->first();
$recentCheckins = $this->checkins
->where('customer_id', $customerId)
->orderBy('checkin_date', 'DESC')
->limit(7) // Get last 7 check-ins
->findAll();
$orderLifecycle = $this->orders
->select('MIN(created_at) as first_order_date, MAX(created_at) as last_order_date')
->where('customer_id', $customerId)
->where('status !=', 'cancelled')
->first();
$topupLifecycle = $this->topups
->select('MIN(created_at) as first_topup_date, MAX(created_at) as last_topup_date')
->where('customer_id', $customerId)
->where('status', 'success')
->first();
$pointsLifecycle = $this->points
->select('MIN(created_at) as first_point_date, MAX(created_at) as last_point_date')
->where('customer_id', $customerId)
->first();
// Get check-in lifecycle
$checkinLifecycle = $this->checkins
->select('MIN(checkin_date) as first_checkin_date, MAX(checkin_date) as last_checkin_date')
->where('customer_id', $customerId)
->first();
$customerStatus = $this->calculateCustomerStatus($orderLifecycle, $topupLifecycle, $pointsLifecycle, $checkinLifecycle);
$lifecycleData = [
'first_activity' => $this->getEarliestDate([
$orderLifecycle['first_order_date'] ?? null,
$topupLifecycle['first_topup_date'] ?? null,
$pointsLifecycle['first_point_date'] ?? null,
$checkinLifecycle['first_checkin_date'] ?? null
]),
'last_activity' => $this->getLatestDate([
$orderLifecycle['last_order_date'] ?? null,
$topupLifecycle['last_topup_date'] ?? null,
$pointsLifecycle['last_point_date'] ?? null,
$checkinLifecycle['last_checkin_date'] ?? null
]),
'first_order' => $orderLifecycle['first_order_date'] ?? null,
'last_order' => $orderLifecycle['last_order_date'] ?? null,
'first_topup' => $topupLifecycle['first_topup_date'] ?? null,
'last_topup' => $topupLifecycle['last_topup_date'] ?? null,
'first_point' => $pointsLifecycle['first_point_date'] ?? null,
'last_point' => $pointsLifecycle['last_point_date'] ?? null,
'first_checkin' => $checkinLifecycle['first_checkin_date'] ?? null,
'last_checkin' => $checkinLifecycle['last_checkin_date'] ?? null,
'customer_status' => $customerStatus,
'days_since_last_activity' => $this->calculateDaysSinceLastActivity(
$orderLifecycle['last_order_date'] ?? null,
$topupLifecycle['last_topup_date'] ?? null,
$pointsLifecycle['last_point_date'] ?? null,
$checkinLifecycle['last_checkin_date'] ?? null
)
];
return $this->respond([
'status' => 200,
'message' => 'Customer dashboard data retrieved successfully',
'data' => [
'orders' => $orderSummary ?? [
'customer_id' => $customerId,
'total_orders' => 0,
'total_grand' => 0
],
'topups' => $topupSummary ?? [
'customer_id' => $customerId,
'total_topups' => 0,
'total_topup_amount' => 0
],
'points' => $pointsSummary ?? [
'customer_id' => $customerId,
'total_point_transactions' => 0,
'total_point_in' => 0,
'total_point_out' => 0,
'current_points' => 0
],
'wallet' => $walletSummary ?? [
'customer_id' => $customerId,
'total_in' => 0,
'total_out' => 0,
'current_balance' => 0
],
'vouchers' => [
'summary' => $voucherSummary ?? [
'customer_id' => $customerId,
'total_vouchers' => 0,
'active_vouchers' => 0,
'used_vouchers' => 0,
'expired_vouchers' => 0
],
'active_vouchers_list' => $activeVouchers
],
'checkins' => [ // Added checkins section
'summary' => $checkinData ?? [
'customer_id' => $customerId,
'total_checkins' => 0,
'highest_streak' => 0,
'last_checkin_date' => null
],
'recent_checkins' => $recentCheckins
],
'lifecycle' => $lifecycleData
]
]);
}
private function calculateCustomerStatus($orderLifecycle, $topupLifecycle, $pointsLifecycle, $checkinLifecycle)
{
$lastOrderDate = $orderLifecycle['last_order_date'] ?? null;
$lastTopupDate = $topupLifecycle['last_topup_date'] ?? null;
$lastPointDate = $pointsLifecycle['last_point_date'] ?? null;
$lastCheckinDate = $checkinLifecycle['last_checkin_date'] ?? null;
$lastActivity = $this->getLatestDate([$lastOrderDate, $lastTopupDate, $lastPointDate, $lastCheckinDate]);
if (!$lastActivity) {
return 'new';
}
$daysInactive = $this->calculateDaysSince($lastActivity);
if ($daysInactive <= 30) {
return 'active';
} elseif ($daysInactive <= 90) {
return 'dormant';
} else {
return 'churned';
}
}
private function calculateDaysSinceLastActivity($lastOrderDate, $lastTopupDate, $lastPointDate, $lastCheckinDate)
{
$lastActivity = $this->getLatestDate([$lastOrderDate, $lastTopupDate, $lastPointDate, $lastCheckinDate]);
if (!$lastActivity) {
return null;
}
return $this->calculateDaysSince($lastActivity);
}
private function calculateDaysSince($dateString)
{
if (!$dateString) {
return null;
}
$date = new \DateTime($dateString);
$now = new \DateTime();
$interval = $now->diff($date);
return $interval->days;
}
private function getEarliestDate(array $dates)
{
$validDates = array_filter($dates);
if (empty($validDates)) {
return null;
}
sort($validDates);
return $validDates[0];
}
private function getLatestDate(array $dates)
{
$validDates = array_filter($dates);
if (empty($validDates)) {
return null;
}
rsort($validDates);
return $validDates[0];
}
}
@@ -0,0 +1,278 @@
<?php
namespace App\Controllers\Backend;
use CodeIgniter\RESTful\ResourceController;
use App\Models\Orders;
use App\Models\TopupModel;
use App\Models\Customer;
use App\Models\Outlet;
class DashboardController extends ResourceController
{
private $orders;
private $topup;
private $customer;
private $outlets;
public function __construct()
{
$this->orders = new Orders();
$this->topup = new TopupModel();
$this->customer = new Customer();
$this->outlets = new Outlet();
}
public function dashboard()
{
$today = date('Y-m-d');
$month = date('m');
$year = date('Y');
// 1. Net Sales Today (sum of grand_total where paid)
$netSalesToday = $this->orders
->selectSum('grand_total')
->where('DATE(created_at)', $today)
->where('payment_status', 'paid')
->get()->getRow()->grand_total ?? 0;
// 2. Today Transaction (count orders today)
$todayTransaction = $this->orders
->where('DATE(created_at)', $today)
->where('payment_status', 'paid')
->countAllResults();
// 3. Net Sales Month to Date
$netSalesMonthToDate = $this->orders
->selectSum('grand_total')
->where('MONTH(created_at)', $month)
->where('YEAR(created_at)', $year)
->where('payment_status', 'paid')
->get()->getRow()->grand_total ?? 0;
// 4. Net Sales Year to Date
$netSalesYearToDate = $this->orders
->selectSum('grand_total')
->where('YEAR(created_at)', $year)
->where('payment_status', 'paid')
->get()->getRow()->grand_total ?? 0;
// 5. Topup
$topup1 = $this->topup
->selectSum('amount')
->where('DATE(created_at)', $today)
->where('status', 'Success')
->get()->getRow()->amount ?? 0;
$topup2 = $this->topup
->selectSum('other_amount')
->where('DATE(created_at)', $today)
->where('status', 'Success')
->get()->getRow()->other_amount ?? 0;
// 6. Total Customer Wallet Balance
$wallet_balance = $this->customer
->selectSum('customer_wallet')
->get()->getRow()->customer_wallet ?? 0;
// 7. Today Voucher Redeemed / Discount
$today_voucher_amount = $this->orders
->selectSum('voucher_discount_amount')
->where('DATE(created_at)', $today)
->where('payment_status', 'paid')
->get()->getRow()->voucher_discount_amount ?? 0;
$today_promo_amount = $this->orders
->selectSum('promo_discount_amount')
->where('DATE(created_at)', $today)
->where('payment_status', 'paid')
->get()->getRow()->promo_discount_amount ?? 0;
$today_discount_amount = $this->orders
->selectSum('promo_discount_amount')
->where('DATE(created_at)', $today)
->where('payment_status', 'paid')
->get()->getRow()->promo_discount_amount ?? 0;
$data = [
'today_net_sales' => $netSalesToday,
'today_total_transaction' => $todayTransaction,
'net_sales_month_to_date' => $netSalesMonthToDate,
'net_sales_year_to_date' => $netSalesYearToDate,
'today_topup' => $topup1 + $topup2,
'total_wallet_balance' => $wallet_balance,
'today_total_discount_amount' => $today_voucher_amount + $today_promo_amount + $today_discount_amount,
];
return $this->respond(['status' => 200, 'data' => $data]);
}
public function dashboardSummary ()
{
$data = [
'today' => $this->orders->getTodayOrdersRevenueByHour(),
'yesterday' => $this->orders->getYesterdayOrdersRevenueByHour(),
'last7days' => $this->orders->getLast7DaysOrdersRevenue(),
'last30days' => $this->orders->getLast30DaysOrdersRevenue(),
];
return $this->respond(['status' => 200, 'data' => $data]);
}
public function liveMonitor()
{
//Current Offline outlets
$outlets = $this->outlets->getOperatingHoursWithDaysList();
$onlineCount = 0;
$offlineCount = 0;
foreach ($outlets as $outlet) {
if ($this->isOutletOnline($outlet)) {
$onlineCount++;
} else {
$offlineCount++;
}
}
// Cancelled Orders (all time, or do you also want by date range?)
$cancelledOrders = $this->orders
->where('payment_status', 'cancelled')
->countAllResults();
// Dates
$endDateCurrent = date('Y-m-d'); // today
$startDateCurrent = date('Y-m-d', strtotime('-7 days')); // 7 days ago
$endDatePrevious = date('Y-m-d', strtotime('-8 days')); // day before current range
$startDatePrevious = date('Y-m-d', strtotime('-15 days')); // 15 days ago
// Function to fetch stats for a range
$getStats = function($startDate, $endDate) {
// Gross Sales
$grossSales = $this->orders
->selectSum('grand_total', 'gross_sales')
->where('payment_status', 'paid')
->where('created_at >=', $startDate)
->where('created_at <=', $endDate)
->where('promo_discount_amount <=', 0) // No promo discount
->where('voucher_discount_amount <=', 0) // No voucher discount
->where('discount_amount <=', 0) // No regular discount
->get()->getRow()->gross_sales ?? 0;
// Total Customer
$totalCustomer = $this->orders
->select('COUNT(DISTINCT customer_id) as total_customer')
->where('payment_status', 'paid')
->where('created_at >=', $startDate)
->where('created_at <=', $endDate)
->get()->getRow()->total_customer ?? 0;
// Net Sales from Offers
$netSalesOffers = $this->orders
->selectSum('grand_total', 'net_sales_offers')
->where('payment_status', 'paid')
->where('created_at >=', $startDate)
->where('created_at <=', $endDate)
->groupStart()
->where('promo_discount_amount >', 0)
->orWhere('voucher_discount_amount >', 0)
->orWhere('discount_amount >', 0)
->groupEnd()
->get()->getRow()->net_sales_offers ?? 0;
// Average Transaction
$avgTransaction = $this->orders
->selectAvg('grand_total', 'avg_transaction')
->where('payment_status', 'paid')
->where('created_at >=', $startDate)
->where('created_at <=', $endDate)
->get()->getRow()->avg_transaction ?? 0;
return [
'gross_sales' => $grossSales,
'total_customer' => $totalCustomer,
'net_sales_from_offers' => $netSalesOffers,
'avg_transaction' => $avgTransaction,
];
};
// Current 7 days
$current = $getStats($startDateCurrent, $endDateCurrent);
// Previous 7 days
$previous = $getStats($startDatePrevious, $endDatePrevious);
// Function to calculate percentage difference
$calcPercentage = function($currentValue, $previousValue) {
if ($previousValue == 0) {
return $currentValue > 0 ? 100 : 0;
}
return (($currentValue - $previousValue) / $previousValue) * 100;
};
$data = [
'offline_outlets' => $offlineCount,
'cancelled_orders' => $cancelledOrders,
'gross_sales' => [
'current' => $current['gross_sales'],
'previous' => $previous['gross_sales'],
'percentage' => $calcPercentage($current['gross_sales'], $previous['gross_sales']),
],
'total_customer' => [
'current' => $current['total_customer'],
'previous' => $previous['total_customer'],
'percentage' => $calcPercentage($current['total_customer'], $previous['total_customer']),
],
'net_sales_from_offers' => [
'current' => $current['net_sales_from_offers'],
'previous' => $previous['net_sales_from_offers'],
'percentage' => $calcPercentage($current['net_sales_from_offers'], $previous['net_sales_from_offers']),
],
'avg_transaction' => [
'current' => $current['avg_transaction'],
'previous' => $previous['avg_transaction'],
'percentage' => $calcPercentage($current['avg_transaction'], $previous['avg_transaction']),
],
];
return $this->respond(['status' => 200, 'data' => $data]);
}
private function isOutletOnline($outlet)
{
// Get current day and time
$currentDay = date('l'); // e.g., "Monday"
$currentTime = date('H:i:s'); // Current time in 24-hour format
// Check if outlet operates on current day
if (!isset($outlet['operating_schedule'][$currentDay]) ||
!$outlet['operating_schedule'][$currentDay]['is_operated']) {
return false;
}
$operatingHours = $outlet['operating_schedule'][$currentDay]['operating_hours'];
// Check if current time falls within any operating hour slot
foreach ($operatingHours as $timeSlot) {
$startTime = $timeSlot['start_time'];
$endTime = $timeSlot['end_time'];
// Handle cases where end time is 23:59:00 (which means until midnight)
if ($endTime === '23:59:00') {
$endTime = '23:59:59';
}
if ($currentTime >= $startTime && $currentTime <= $endTime) {
return true;
}
}
return false;
}
}
+100
View File
@@ -0,0 +1,100 @@
<?php
namespace App\Controllers\Backend;
use App\Controllers\BaseController;
use App\Models\DeliverySettingModel;
use CodeIgniter\HTTP\ResponseInterface;
class DeliverySetting extends BaseController
{
protected $model;
public function __construct()
{
$this->model = new DeliverySettingModel();
}
public function index()
{
$data = $this->model->findAll();
// $this->model->orderBy('status', 'active');
return $this->response->setJSON([
'status' => 'success',
'data' => $data,
]);
}
public function create()
{
$data = $this->request->getJSON(true) ?? $this->request->getPost();
if (!$this->model->insert($data)) {
return $this->response->setStatusCode(ResponseInterface::HTTP_BAD_REQUEST)
->setJSON([
'status' => 'error',
'errors' => $this->model->errors(),
]);
}
return $this->response->setStatusCode(ResponseInterface::HTTP_CREATED)
->setJSON([
'status' => 'success',
'message' => 'Delivery setting created',
'data' => $this->model->find($this->model->getInsertID()),
]);
}
public function show($id = null)
{
$row = $this->model->find($id);
if (!$row) {
return $this->response->setStatusCode(ResponseInterface::HTTP_NOT_FOUND)
->setJSON(['status' => 'error', 'message' => 'Not found']);
}
return $this->response->setJSON([
'status' => 'success',
'data' => $row,
]);
}
public function update($id = null)
{
$data = $this->request->getJSON(true) ?? $this->request->getRawInput();
if (!$this->model->find($id)) {
return $this->response->setStatusCode(ResponseInterface::HTTP_NOT_FOUND)
->setJSON(['status' => 'error', 'message' => 'Not found']);
}
if (!$this->model->update($id, $data)) {
return $this->response->setStatusCode(ResponseInterface::HTTP_BAD_REQUEST)
->setJSON([
'status' => 'error',
'errors' => $this->model->errors(),
]);
}
return $this->response->setJSON([
'status' => 'success',
'message' => 'Delivery setting updated',
'data' => $this->model->find($id),
]);
}
public function delete($id = null)
{
if (!$this->model->find($id)) {
return $this->response->setStatusCode(ResponseInterface::HTTP_NOT_FOUND)
->setJSON(['status' => 'error', 'message' => 'Not found']);
}
$this->model->delete($id);
return $this->response->setJSON([
'status' => 'success',
'message' => 'Delivery setting deleted',
]);
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace App\Controllers\Backend;
use App\Controllers\BaseController;
class Home extends BaseController
{
public function index(): string
{
return view('welcome_message');
}
}
+170
View File
@@ -0,0 +1,170 @@
<?php
namespace App\Controllers\Backend;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\API\ResponseTrait;
use App\Models\User;
use Firebase\JWT\JWT;
class LoginController extends BaseController
{
use ResponseTrait;
public function index()
{
$userModel = new User();
$username = $this->request->getVar('username');
$password = $this->request->getVar('password_hash');
$user = $userModel->where('username', $username)->first();
if(is_null($user)) {
return $this->failUnauthorized('Invalid username or password.');
}
$hashedPassword = md5($password);
if ($hashedPassword !== $user['password_hash']) {
return $this->failUnauthorized('Invalid username or password.');
}
$key = getenv('JWT_SECRET');
$iat = time(); // current timestamp value
$exp = $iat + 86400;
$payload = array(
"iss" => "Issuer of the JWT",
"aud" => "Audience that the JWT",
"sub" => "Subject of the JWT",
"iat" => $iat, //Time the JWT issued at
"exp" => $exp, // Expiration time of token
"username" => $user['username'],
);
$token = JWT::encode($payload, $key, 'HS256');
$userData = [
'user_id' => $user['id'],
'username' => $user['username'],
'name' => $user['name'],
'role' => $user['role'],
'status'=> $user['status'],
'outlet_id'=> $user['outlet_id'],
// 'created_by'=> $user['created_by'],
'updated_at' => $user['updated_at'],
'created_at' => $user['created_at'],
];
$response = [
'message' => 'Login Succesful',
'token' => $token,
'userData' => $userData
];
// session()->set('merchant_id', $user['merchant_id']);
return $this->respond($response, 200);
}
public function requestToken()
{
$userModel = new User();
$merchant = $this->request->getVar('merchant');
$token_key = $this->request->getVar('token_key');
$user = $userModel->where('username', $merchant)->first();
if(is_null($user)) {
return $this->failUnauthorized('Invalid username.');
}
if ($token_key !== $user['token_key']) {
return $this->failUnauthorized('Invalid token key.');
}
$key = getenv('JWT_SECRET');
$iat = time(); // current timestamp value
$exp = $iat + 86400;
$payload = array(
"iss" => "Issuer of the JWT",
"aud" => "Audience that the JWT",
"sub" => "Subject of the JWT",
"iat" => $iat, //Time the JWT issued at
"exp" => $exp, // Expiration time of token
"user_id" => $user['user_id'],
"token_key" => $token_key,
);
$token = JWT::encode($payload, $key, 'HS256');
$response = [
'message' => 'Request Succesfully',
'token' => $token,
'expires' => date('Y-m-d H:i:s', $exp)
];
return $this->respond($response, 200);
}
/**
* Return the properties of a resource object
*
* @return mixed
*/
public function show($id = null)
{
//
}
/**
* Return a new resource object, with default properties
*
* @return mixed
*/
public function new()
{
//
}
/**
* Create a new resource object, from "posted" parameters
*
* @return mixed
*/
public function create()
{
//
}
/**
* Return the editable properties of a resource object
*
* @return mixed
*/
public function edit($id = null)
{
//
}
/**
* Add or update a model resource, from "posted" properties
*
* @return mixed
*/
public function update($id = null)
{
//
}
/**
* Delete the designated resource object from the model
*
* @return mixed
*/
public function delete($id = null)
{
//
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,248 @@
<?php
namespace App\Controllers\Backend;
use App\Models\Options;
use App\Models\OptionsGroups;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
helper('image');
class OptionController extends ResourceController
{
private $options;
private $option_groups;
public function __construct()
{
$this->options = new Options();
$this->option_groups = new OptionsGroups();
}
public function index()
{
// echo(123);exit;
$groups = $this->option_groups->findAll();
$data = [];
foreach ($groups as $group) {
$group['options'] = $this->options
->where('option_group_id', $group['id'])
->findAll();
$data[] = $group;
}
return $this->respond(['status' => 200, 'data' => $data]);
}
public function show($id = null)
{
$group = $this->option_groups->find($id);
if (!$group) {
return $this->failNotFound("Option group not found.");
}
$group['options'] = $this->options
->where('option_group_id', $group['id'])
->findAll();
return $this->respond(['status' => 200, 'data' => $group]);
}
public function create()
{
$validation = $this->validate([
'title' => 'required',
'min_quantity' => 'permit_empty|integer',
'max_quantity' => 'permit_empty|integer',
'is_required' => 'required',
'status' => 'required',
'order_index' => 'permit_empty|integer',
'options' => 'permit_empty',
]);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$groupData = [
'title' => $this->request->getVar('title'),
'min_quantity' => $this->request->getVar('min_quantity') ?? 0,
'max_quantity' => $this->request->getVar('max_quantity') ?? 0,
'is_required' => $this->request->getVar('is_required'),
'status' => $this->request->getVar('status'),
'order_index' => $this->request->getVar('order_index') ?? 0,
];
$groupId = $this->option_groups->insert($groupData);
if (!$groupId) {
return $this->fail("Failed to create group.", 400);
}
// echo '<pre>';
// print_r($this->request->getPost());
// exit;
$options = $this->request->getJSON(true)['options'] ?? $this->request->getVar('options') ?? [];
if (!is_array($options)) {
return $this->failValidationErrors('Options not formatted correctly.');
}
$savedOptions = [];
foreach ($options as $i => $opt) {
$imageUrl = null;
$compressedUrl = null;
$imageFile = $this->request->getFile("image$i");
if ($imageFile && $imageFile->isValid() && !$imageFile->hasMoved()) {
$result = save_image_with_compression(
$imageFile,
FCPATH . 'backend/uploads/options',
FCPATH . 'backend/uploads/options_compressed',
900,
80
);
$imageUrl = base_url('backend/uploads/options' . basename($result['original']));
$compressedUrl = base_url('backend/uploads/options_compressed' . basename($result['compressed']));
}
$optionData = [
'option_group_id' => $groupId,
'title' => isset($opt['title']) ? $opt['title'] : '',
'price_adjustment' => isset($opt['price_adjustment']) ? $opt['price_adjustment'] : 0,
'order_index' => isset($opt['order_index']) ? $opt['order_index'] : $i,
'images' => $imageUrl,
'images_compressed' => $compressedUrl,
];
$this->options->insert($optionData);
$savedOptions[] = $optionData;
}
return $this->respond([
'status' => 200,
'message' => 'Group with options created.',
'data' => [
'group_id' => $groupId,
'group' => $groupData,
'options' => $savedOptions
]
]);
}
public function update($id = null)
{
$group = $this->option_groups->find($id);
if (!$group) {
return $this->fail("Group not found.", 400);
}
$validation = $this->validate([
'title' => 'required',
'min_quantity' => 'permit_empty|integer',
'max_quantity' => 'permit_empty|integer',
'is_required' => 'required|in_list[0,1,true,false]',
'status' => 'required',
'order_index' => 'permit_empty|integer',
'options' => 'permit_empty'
]);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$updateData = [
'title' => $this->request->getVar('title'),
'min_quantity' => $this->request->getVar('min_quantity') ?? 0,
'max_quantity' => $this->request->getVar('max_quantity') ?? 0,
'is_required' => $this->request->getVar('is_required'),
'status' => $this->request->getVar('status'),
'order_index' => $this->request->getVar('order_index') ?? 0,
];
$this->option_groups->update($id, $updateData);
$options = $this->request->getVar('options') ?? [];
$exist_option = $this->options->where('option_group_id', $id)->findAll();
$exist_option_group = array_column($exist_option, 'id');
$update_option_group = [];
foreach ($options as $i => $opt) {
$title = is_array($opt) ? $opt['title'] ?? '' : $opt->title ?? '';
$price = is_array($opt) ? $opt['price_adjustment'] ?? 0 : $opt->price_adjustment ?? 0;
$order = is_array($opt) ? $opt['order_index'] ?? 0 : $opt->order_index ?? 0;
$optId = is_array($opt) ? $opt['id'] ?? null : $opt->id ?? null;
$optionData = [
'option_group_id' => $id,
'title' => $title,
'price_adjustment' => $price,
'order_index' => $order,
'images' => $opt['images'] ?? null,
'images_compressed' => $opt['images_compressed'] ?? null,
];
$imageUrl = null;
$compressedUrl = null;
$imageFile = $this->request->getFile("image$i");
if ($imageFile && $imageFile->isValid() && !$imageFile->hasMoved()) {
$result = save_image_with_compression(
$imageFile,
FCPATH . 'backend/uploads/options/',
FCPATH . 'backend/uploads/options_compressed/',
900,
80
);
$imageUrl = base_url('backend/uploads/options/' . basename($result['original']));
$compressedUrl = base_url('backend/uploads/options_compressed/' . basename($result['compressed']));
}
if ($imageUrl) {
$optionData['images'] = $imageUrl;
$optionData['images_compressed'] = $compressedUrl;
}
if (!isset($opt->id)) {
$this->options->insert($optionData);
} else {
$this->options->update($opt->id, $optionData);
$update_option_group[] = $opt->id;
}
}
foreach ($exist_option_group as $option_id) {
if (!in_array($option_id, $update_option_group)) {
$this->options->delete($option_id);
}
}
return $this->respond(['status' => 200, 'message' => 'Group updated.']);
}
public function delete($id = null)
{
$group = $this->option_groups->find($id);
if (!$group) {
return $this->failNotFound("Group not found.");
}
$this->option_groups->delete($id);
$this->options->where('option_group_id', $id)->delete();
return $this->respond(['status' => 200, 'message' => 'Group and options deleted.']);
}
}
+581
View File
@@ -0,0 +1,581 @@
<?php
namespace App\Controllers\Backend;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
use App\Models\OrderItemOptions;
use App\Models\OrderItems;
use App\Models\OrderPayments;
use App\Models\Orders;
use App\Models\OrderTaxes;
use CodeIgniter\Database\Config;
use CodeIgniter\HTTP\Message;
use App\Models\OrderDeliveries;
use App\Libraries\Wato;
use App\Models\User;
use App\Models\MenuItemVariations;
class OrderController extends ResourceController
{
private $orders;
private $order_items;
private $order_item_options;
private $order_payments;
private $order_taxes;
private $order_deliveries;
private $db;
private $wato;
private $user;
private $menu_item_variations;
public function __construct()
{
$this->orders = new Orders();
$this->order_items = new OrderItems();
$this->order_item_options = new OrderItemOptions();
$this->order_taxes = new OrderTaxes();
$this->order_payments = new OrderPayments();
$this->order_deliveries = new OrderDeliveries();
$this->db = Config::connect();
$this->wato = new Wato();
$this->user = new User();
$this->menu_item_variations = new MenuItemVariations();
}
public function orderList()
{
$user_id = $this->request->getVar('user_id');
$start_date = $this->request->getVar('start_date');
$end_date = $this->request->getVar('end_date');
$status = $this->request->getVar('status');
$outlet_id = $this->request->getVar('outlet_id');
$order_type = $this->request->getVar('order_type');
// Get the user information to check role and outlet
$user = $this->user->find($user_id);
if (!$user) {
return $this->respond([
'status' => 404,
'message' => 'User not found',
'data' => []
]);
}
$orderQuery = $this->orders
->orderBy('created_at', 'DESC');
// If user is not admin, filter by their outlet_id
if ($user['role'] !== 'admin') {
$orderQuery->where('outlet_id', $user['outlet_id']);
} else {
// If admin and specific outlet_id is provided, use it
if (!empty($outlet_id)) {
$orderQuery->where('outlet_id', $outlet_id);
}
// Otherwise admin sees all orders (no outlet filter)
}
if (!empty($start_date)) {
$orderQuery->where('created_at >=', $start_date . ' 00:00:00');
}
if (!empty($end_date)) {
$orderQuery->where('created_at <=', $end_date . ' 23:59:59');
}
if (!empty($status)) {
$orderQuery->where('status', $status);
}
if (!empty($outlet_id)) {
$orderQuery->where('outlet_id', $outlet_id);
}
if (!empty($order_type)) {
$orderQuery->where('order_type', $order_type);
}
$orders = $orderQuery->findAll();
if (empty($orders)) {
return $this->respond([
'status' => 200,
'message' => 'No orders found!',
'data' => []
]);
}
foreach ($orders as &$order) {
$order_id = $order['id'];
// Fetch items + options
$order['items'] = $this->order_items->where('order_id', $order_id)->findAll();
foreach ($order['items'] as &$item) {
$item['options'] = $this->order_item_options
->where('order_item_id', $item['id'])
->findAll();
}
// Fetch taxes
$order['taxes'] = $this->order_taxes
->where('order_id', $order_id)
->findAll();
// Fetch payments
$order['payments'] = $this->order_payments
->where('order_id', $order_id)
->orderBy('created_at', 'DESC')
->findAll();
// Fetch deliveries
$order['deliveries'] = $this->order_deliveries
->where('order_id', $order_id)
->findAll();
}
return $this->respond([
'status' => 200,
'message' => 'Orders retrieved successfully',
'data' => $orders
]);
}
public function showOrder($order_id = null)
{
// Get order with customer + outlet info
$order = $this->orders
->select('
orders.*,
customers.id as customer_id,
customers.name as customer_name,
customers.email as customer_email,
customers.phone as customer_phone,
customers.profile_picture as customer_profile_pic,
outlets.id as outlet_id,
outlets.title as outlet_title,
outlets.email as outlet_email,
outlets.phone as outlet_phone,
outlets.address as outlet_address,
')
->join('customers', 'customers.id = orders.customer_id', 'left')
->join('outlets', 'outlets.id = orders.outlet_id', 'left') // join outlets
->where('orders.id', $order_id)
->first();
if (!$order) {
return $this->fail('Order not found', 400);
}
// Items + options + menu images
$order['items'] = $this->order_items
->select('order_items.*, menu_images.image_url as menu_item_image')
->join('menu_images', 'menu_images.menu_item_id = order_items.menu_item_id', 'left')
->where('order_items.order_id', $order_id)
->groupBy('order_items.id')
->findAll();
foreach ($order['items'] as &$item) {
$item['options'] = $this->order_item_options->where('order_item_id', $item['id'])->findAll();
$item['menu_item_image'] = getMenuImage($item['menu_item_id']);
$item['variation_name'] = $this->menu_item_variations
->where('menu_item_id', $item['menu_item_id'])
->where('id', $item['variation_id'])
->first()['title'] ?? '';
}
// Taxes
$order['taxes'] = $this->order_taxes->where('order_id', $order_id)->findAll();
// Payments
$order['payments'] = $this->order_payments
->where('order_id', $order_id)
->orderBy('created_at', 'DESC')
->findAll();
// Deliveries
$order['deliveries'] = $this->order_deliveries->where('order_id', $order_id)->findAll();
return $this->respond([
'status' => 200,
'message' => 'Order retrieved successfully',
'data' => $order
]);
}
public function updateOrderSchedule($order_id = null)
{
// Validate order_id
if (empty($order_id)) {
return $this->fail('Order ID is required', 400);
}
// Get the order
$order = $this->orders->find($order_id);
if (!$order) {
return $this->fail('Order not found', 404);
}
// Check if order status is 'complete'
if (isset($order['status']) && $order['status'] === 'complete') {
return $this->fail('Cannot modify schedule for completed orders', 400);
}
// Get request data
$selected_date = $this->request->getVar('selected_date');
$selected_time = $this->request->getVar('selected_time');
// Validate input
if (empty($selected_date) || empty($selected_time)) {
return $this->fail('Both selected_date and selected_time are required', 400);
}
// Prepare data to update
$data = [
'selected_date' => $selected_date,
'selected_time' => $selected_time,
'updated_at' => date('Y-m-d H:i:s') // Update timestamp
];
try {
// Update the order
$updated = $this->orders->update($order_id, $data);
if (!$updated) {
return $this->fail('Failed to update order schedule', 500);
}
// Get the updated order
$updatedOrder = $this->orders->find($order_id);
return $this->respond([
'status' => 200,
'message' => 'Order schedule updated successfully',
'data' => $updatedOrder
]);
} catch (\Exception $e) {
return $this->fail('An error occurred while updating order schedule: ' . $e->getMessage(), 500);
}
}
public function updateOrderStatus($order_id = null)
{
// Validate order_id
if (empty($order_id)) {
return $this->fail('Order ID is required', 400);
}
// Get the order
$order = $this->orders->select('orders.*, customer_addresses.phone as recipient_phone')
->join('customer_addresses', 'customer_addresses.id = orders.customer_address_id AND customer_addresses.deleted_at IS NULL', 'left')->find($order_id);
if (!$order) {
return $this->fail('Order not found', 404);
}
// Get request data
$status = $this->request->getVar('status');
// Validate input
if (empty($status)) {
return $this->fail('Status is required', 400);
}
// Define allowed status values
$allowedStatuses = ['pending', 'picked_up', 'on_the_way', 'completed', 'ready_to_pickup'];
if (!in_array($status, $allowedStatuses)) {
return $this->fail('Invalid status value. Allowed values: ' . implode(', ', $allowedStatuses), 400);
}
// Prepare data to update
$data = [
'status' => $status,
'updated_at' => date('Y-m-d H:i:s') // Update timestamp
];
try {
// Update the order
$updated = $this->orders->update($order_id, $data);
if ($order['status'] != $status) { //send notification to customer if status is changed
$message = '';
switch ($status) {
case 'ready_to_pickup':
$message = "🍴 Your food is ready for pickup!\nPlease show your order number at the counter: \n**{{" . $order['order_so'] . "}}**";
break;
case 'on_the_way':
$message = "🚚 Your order [**" . $order['order_so'] . "**] is on the way!\nThe driver is delivering your food now.\nPlease get ready to receive it.";
break;
case 'completed':
$message = "🎉 Your order [**" . $order['order_so'] . "**] has been completed.\nWe hope you enjoy your meal!\nThank you for ordering with us.";
break;
}
if ($message) {
$this->wato->pushNotification($order['recipient_phone'], $message);
}
}
if (!$updated) {
return $this->fail('Failed to update order status', 500);
}
// Get the updated order
$updatedOrder = $this->orders->find($order_id);
return $this->respond([
'status' => 200,
'message' => 'Order status updated successfully',
'data' => $updatedOrder
]);
} catch (\Exception $e) {
return $this->fail('An error occurred while updating order status: ' . $e->getMessage(), 500);
}
}
public function createOrderDelivery()
{
// Get request data
$order_id = $this->request->getVar('order_id');
$fee_amount = $this->request->getVar('actual_fee_amount');
$tracking_link = $this->request->getVar('tracking_link');
// Optional fields that will be set to empty string if not provided
$provider_name = $this->request->getVar('provider_name') ?? '';
$provider_order_id = $this->request->getVar('provider_order_id') ?? '';
$actual_fee_amount = $this->request->getVar('fee_amount') ?? '';
$transaction_id = $this->request->getVar('transaction_id') ?? '';
// Validate required fields
if (empty($order_id) || empty($actual_fee_amount) || empty($tracking_link)) {
return $this->fail('order_id, actual_fee_amount, and tracking_link are required', 400);
}
// Check if the order exists
$order = $this->orders->find($order_id);
if (!$order) {
return $this->fail('Order not found', 404);
}
// Prepare data for insertion with empty strings for optional fields
$data = [
'order_id' => $order_id,
'provider_name' => $provider_name,
'status' => 'pending', // Default status
'provider_order_id' => $provider_order_id,
'fee_amount' => $fee_amount,
'actual_fee_amount' => $actual_fee_amount,
'transaction_id' => $transaction_id,
'tracking_link' => $tracking_link,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
];
try {
// Insert the data
$inserted = $this->order_deliveries->insert($data);
if (!$inserted) {
return $this->fail('Failed to create order delivery record', 500);
}
// Get the inserted ID
$insertId = $this->db->insertID();
// Return success response with the created record
return $this->respond([
'status' => 201,
'message' => 'Order delivery record created successfully',
'data' => array_merge(['id' => $insertId], $data)
], 201);
} catch (\Exception $e) {
return $this->fail('An error occurred while creating order delivery: ' . $e->getMessage(), 500);
}
}
public function updateOrderDelivery($id = null)
{
if (empty($id)) {
return $this->fail('Delivery record ID is required', 400);
}
// Get request data - all fields optional except id
$data = [
'actual_fee_amount' => $this->request->getVar('actual_fee_amount') ?? '',
'tracking_link' => $this->request->getVar('tracking_link'),
'updated_at' => date('Y-m-d H:i:s')
];
// Remove fields that weren't provided in the request (except status which has a default)
$data = array_filter($data, function ($value, $key) {
return $value !== null || $key === 'status';
}, ARRAY_FILTER_USE_BOTH);
if (empty($data)) {
return $this->respond([
'status' => 200,
'message' => 'No fields to update',
'data' => null
]);
}
try {
$updated = $this->order_deliveries->update($id, $data);
if (!$updated) {
return $this->fail('Failed to update order delivery record', 500);
}
// Get the updated record
$updatedData = $this->order_deliveries->find($id);
// Convert NULL values to empty strings in the response
$responseData = [
'id' => $id,
'order_id' => $updatedData['order_id'],
'provider_name' => $updatedData['provider_name'] ?? '',
'status' => $updatedData['status'] ?? '',
'provider_order_id' => $updatedData['provider_order_id'] ?? '',
'fee_amount' => $updatedData['fee_amount'] ?? '',
'actual_fee_amount' => $updatedData['actual_fee_amount'],
'transaction_id' => $updatedData['transaction_id'] ?? '',
'tracking_link' => $updatedData['tracking_link'],
'created_at' => $updatedData['created_at'],
'updated_at' => $updatedData['updated_at']
];
return $this->respond([
'status' => 200,
'message' => 'Order delivery updated successfully',
'data' => $responseData
]);
} catch (\Exception $e) {
return $this->fail('An error occurred while updating order delivery: ' . $e->getMessage(), 500);
}
}
public function getOrderDelivery($order_id = null)
{
// Validate order_id
if (empty($order_id)) {
return $this->fail('Order ID is required', 400);
}
try {
// Get delivery record by order_id
$delivery = $this->order_deliveries->where('order_id', $order_id)->get()->getRowArray();
if (!$delivery) {
return $this->respond([
'status' => 404,
'message' => 'No delivery record found for this order',
'data' => null
], 404);
}
// Format the response data with empty strings instead of NULL
$responseData = [
'id' => $delivery['id'],
'order_id' => $delivery['order_id'],
'provider_name' => $delivery['provider_name'] ?? '',
'status' => $delivery['status'] ?? '',
'provider_order_id' => $delivery['provider_order_id'] ?? '',
'fee_amount' => $delivery['fee_amount'] ?? '',
'actual_fee_amount' => $delivery['actual_fee_amount'],
'transaction_id' => $delivery['transaction_id'] ?? '',
'tracking_link' => $delivery['tracking_link'],
'created_at' => $delivery['created_at'],
'updated_at' => $delivery['updated_at']
];
return $this->respond([
'status' => 200,
'message' => 'Order delivery retrieved successfully',
'data' => $responseData
]);
} catch (\Exception $e) {
return $this->fail('An error occurred while fetching order delivery: ' . $e->getMessage(), 500);
}
}
public function customerOrderList()
{
$start_date = $this->request->getVar('start_date');
$end_date = $this->request->getVar('end_date');
$status = $this->request->getVar('status');
$outlet_id = $this->request->getVar('outlet_id');
$order_type = $this->request->getVar('order_type');
$orderQuery = $this->orders
->orderBy('created_at', 'DESC');
if (!empty($start_date)) {
$orderQuery->where('created_at >=', $start_date . ' 00:00:00');
}
if (!empty($end_date)) {
$orderQuery->where('created_at <=', $end_date . ' 23:59:59');
}
if (!empty($status)) {
$orderQuery->where('status', $status);
}
if (!empty($outlet_id)) {
$orderQuery->where('outlet_id', $outlet_id);
}
if (!empty($order_type)) {
$orderQuery->where('order_type', $order_type);
}
$orders = $orderQuery->findAll();
if (empty($orders)) {
return $this->respond([
'status' => 200,
'message' => 'No orders found!',
'data' => []
]);
}
foreach ($orders as &$order) {
$order_id = $order['id'];
// Fetch items + options
$order['items'] = $this->order_items->where('order_id', $order_id)->findAll();
foreach ($order['items'] as &$item) {
$item['options'] = $this->order_item_options
->where('order_item_id', $item['id'])
->findAll();
}
// Fetch taxes
$order['taxes'] = $this->order_taxes
->where('order_id', $order_id)
->findAll();
// Fetch payments
$order['payments'] = $this->order_payments
->where('order_id', $order_id)
->orderBy('created_at', 'DESC')
->findAll();
// Fetch deliveries
$order['deliveries'] = $this->order_deliveries
->where('order_id', $order_id)
->findAll();
}
return $this->respond([
'status' => 200,
'message' => 'Orders retrieved successfully',
'data' => $orders
]);
}
}
@@ -0,0 +1,863 @@
<?php
namespace App\Controllers\Backend;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
use App\Models\Outlet;
use App\Models\OutletTax;
use App\Models\OutletImage;
use App\Models\OutletOperatingDay;
use App\Models\OutletOperatingHour;
use App\Models\OutletOperatingHoursException;
use App\Models\SettingTax;
use App\Models\OutletMenus;
use App\Models\MenuItems;
use App\Models\User;
helper('image');
class OutletController extends ResourceController
{
private $outlet;
private $outletTax;
private $outletImage;
private $outletOperatingDays;
private $outletOperatingHours;
private $outletOperatingHoursExceptions;
private $settingTax;
private $outlet_menu;
private $menu_items;
private $user;
protected $db;
public function __construct()
{
$this->outlet = new Outlet();
$this->outletTax = new OutletTax();
$this->outletImage = new OutletImage();
$this->outletOperatingDays = new OutletOperatingDay();
$this->outletOperatingHours = new OutletOperatingHour();
$this->outletOperatingHoursExceptions = new OutletOperatingHoursException();
$this->settingTax = new SettingTax();
$this->db = \Config\Database::connect();
$this->outlet_menu = new OutletMenus();
$this->menu_items = new MenuItems();
$this->user = new User();
}
public function index()
{
$user_id = $this->request->getVar('user_id');
if(!empty($user_id)){
$user = $this->user->find($user_id);
if($user['role'] == 'outlet'){
$outlets = $this->outlet->getOperatingHoursWithDaysList($user['outlet_id']);
} else {
$outlets = $this->outlet->getOperatingHoursWithDaysList();
}
} else {
return $this->respond (['status' => 400, 'result'=> 'User Id is required.']);
}
if(empty($outlets)){
return $this->respond(['status' => 400, 'result' => 'No outlet data found.']);
}
return $this->respond(['status' => 200, 'result' => $outlets]);
}
public function show($id = null)
{
$outlet = $this->outlet->getOperatingHoursWithDays($id);
if(empty($outlet)){
return $this->respond(['status' => 400, 'result' => 'No outlet data found.']);
}
return $this->respond(['status' => 200, 'result' => $outlet]);
}
public function create()
{
$validationRules = [
'title' => 'required',
'email' => 'required',
'phone' => 'required',
'address' => 'required',
'state' => 'required',
'postal_code' => 'required',
'country' => 'required',
'latitude' => 'required|numeric',
'longitude' => 'required|numeric',
'password' => 'required|min_length[8]',
'outlet_delivery_coverage' => 'required|numeric',
'outlet_tax' => 'permit_empty',
'outlet_operating_days' => 'permit_empty',
'outlet_operating_hours' => 'permit_empty',
'outlet_operating_hours_exceptions' => 'permit_empty',
];
$validation = $this->validate($validationRules);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
// Start database transaction
$this->db->transBegin();
try {
$outletData = [
"title" => $this->request->getVar('title'),
"email" => $this->request->getVar('email'),
"phone" => $this->request->getVar('phone'),
"address" => $this->request->getVar('address'),
"state" => $this->request->getVar('state'),
"postal_code" => $this->request->getVar('postal_code'),
"country" => $this->request->getVar('country'),
"status" => $this->request->getVar('status'),
"latitude" => $this->request->getVar('latitude'),
"longitude" => $this->request->getVar('longitude'),
"password" => md5(md5($this->request->getVar('password'))),
"serve_method" => $this->request->getVar('serve_method'),
"delivery_options" => $this->request->getVar('delivery_options'),
"outlet_delivery_coverage" => $this->request->getVar('outlet_delivery_coverage'),
"order_max_per_hour" => $this->request->getVar('order_max_per_hour'),
"item_max_per_hour" => $this->request->getVar('item_max_per_hour'),
"menu_item" => json_encode($this->request->getVar('outlet_menu')),
];
$id = $this->outlet->insert($outletData);
if (!$id) {
throw new \RuntimeException('Failed to insert outlet data');
}
$outlet_menu = $this->request->getVar('outlet_menu');
if(!empty($outlet_menu)){
foreach($outlet_menu as $menu){
$outletMenuItem = [
"outlet_id" => $id,
"menu_item_id" => $menu,
];
if (!$this->outlet_menu->insert($outletMenuItem)) {
throw new \RuntimeException('Failed to insert outlet tax data');
}
}
}
// Process taxes
$tax_ids = $this->request->getVar('outlet_tax');
if(!empty($tax_ids)){
foreach ($tax_ids as $tax_id) {
$outletTaxData = [
"outlet_id" => $id,
"tax_id" => $tax_id,
];
if (!$this->outletTax->insert($outletTaxData)) {
throw new \RuntimeException('Failed to insert outlet tax data');
}
}
}
// Process images
$images = $this->request->getFiles('outlet_images');
$outlet_images = [];
if (!empty($images['outlet_images'])) {
foreach ($images['outlet_images'] as $index => $image) {
if (empty($image) || !$image->isValid() || $image->hasMoved()) {
continue;
}
try {
$result = save_image_with_compression(
$image,
FCPATH . 'backend/uploads/outlets',
FCPATH . 'backend/uploads/outlets_compressed',
900, // width
80 // quality
);
$originalFileName = basename($result['original']);
$compressedFileName = basename($result['compressed']);
if (!$this->outletImage->insert([
"outlet_id" => $id,
"image_url" => $originalFileName,
"compressed_image_url" => $compressedFileName,
"order_index" => $index
])) {
@unlink($result['original']);
@unlink($result['compressed']);
throw new \RuntimeException('Failed to insert outlet image data');
}
$outlet_images[$index] = [
'image_url' => $originalFileName,
'compressed_image_url' => $compressedFileName,
];
} catch (\Exception $e) {
log_message('error', 'Failed to upload and compress image: ' . $e->getMessage());
throw $e;
}
}
}
// Process operating days
$outlet_operating_days = json_decode($this->request->getVar('outlet_operating_days'), true);
$operating_days = [];
// print_r($this->request->getVar('outlet_operating_days'));exit;
if(!empty($outlet_operating_days[0])){
foreach ($outlet_operating_days[0] as $index => $day) {
$day_id = $this->outletOperatingDays->insert([
"outlet_id" => $id,
"day_of_week" => $index,
"is_operated" => $day['is_operated'],
]);
if (!$day_id) {
throw new \RuntimeException('Failed to insert operating days data');
}
$operating_days[$index] = [
'id' => $day_id,
"outlet_id" => $id,
"day_of_week" => $index,
"is_operated" => $day['is_operated'],
];
}
}
// Process operating hours
$outlet_operating_hours = json_decode($this->request->getVar('outlet_operating_hours'), true);
$operating_hours = [];
if(!empty($outlet_operating_hours[0])){
foreach ($outlet_operating_hours[0] as $index => $hours) {
foreach ($hours as $hour) {
$hour_id = $this->outletOperatingHours->insert([
"outlet_id" => $id,
"day_of_week" => $index,
"start_time" => $hour['start_time'],
"end_time" => $hour['end_time'],
]);
if (!$hour_id) {
throw new \RuntimeException('Failed to insert operating hours data');
}
$operating_hours[$index][] = [
'id' => $hour_id,
"outlet_id" => $id,
"day_of_week" => $index,
"start_time" => $hour['start_time'],
"end_time" => $hour['end_time'],
];
}
}
}
// Process operating hours exceptions
$outlet_operating_hours_exceptions = json_decode($this->request->getVar('outlet_operating_hours_exceptions'), true);
$operating_hours_exceptions = [];
if (!empty($outlet_operating_hours_exceptions)) {
foreach ($outlet_operating_hours_exceptions as $index => $exception) {
$hours_exception_id = $this->outletOperatingHoursExceptions->insert([
"outlet_id" => $id,
"date" => $exception['date'],
"start_time" => $exception['start_time'],
"end_time" => $exception['end_time'],
"notes" => $exception['notes'],
]);
if (!$hours_exception_id) {
throw new \RuntimeException('Failed to insert operating hours exceptions data');
}
$operating_hours_exceptions[$index] = [
'id' => $hours_exception_id,
"outlet_id" => $id,
"date" => $exception['date'],
"start_time" => $exception['start_time'],
"end_time" => $exception['end_time'],
"notes" => $exception['notes'],
];
}
}
// Commit transaction if everything succeeded
$this->db->transCommit();
$result = [
'id' => $id,
'title' => $this->request->getVar('title'),
'email' => $this->request->getVar('email'),
'phone' => $this->request->getVar('phone'),
'address' => $this->request->getVar('address'),
'state' => $this->request->getVar('state'),
'postal_code' => $this->request->getVar('postal_code'),
'country' => $this->request->getVar('country'),
'status' => $this->request->getVar('status'),
'latitude' => $this->request->getVar('latitude'),
'longitude' => $this->request->getVar('longitude'),
'password' => md5(md5($this->request->getVar('password'))),
'serve_method' => $this->request->getVar('serve_method'),
'delivery_options' => $this->request->getVar('delivery_options'),
'outlet_delivery_coverage' => $this->request->getVar('outlet_delivery_coverage'),
'order_max_per_hour' => $this->request->getVar('order_max_per_hour'),
'item_max_per_hour' => $this->request->getVar('item_max_per_hour'),
'outlet_tax' => $tax_ids,
'outlet_images' => $outlet_images,
'outlet_operating_days' => $operating_days,
'outlet_operating_hours' => $operating_hours,
'outlet_operating_hours_exceptions' => $operating_hours_exceptions ?? [],
'menu_item' => $this->request->getVar('outlet_menu'),
];
return $this->respond(['status' => 200, 'result' => $result]);
} catch (\Exception $e) {
// Rollback transaction on any error
$this->db->transRollback();
// Clean up any uploaded files if transaction fails
if (!empty($outlet_images)) {
$uploadPath = $_SERVER['DOCUMENT_ROOT'] . '/backend/uploads/outlets/';
foreach ($outlet_images as $image) {
@unlink($uploadPath . $image);
}
}
log_message('error', 'Outlet creation failed: ' . $e->getMessage());
return $this->failServerError('Failed to create outlet: ' . $e->getMessage());
}
}
public function update($id = null)
{
$outlet = $this->outlet->find($id);
if (empty($outlet)) {
return $this->respond(['status' => 400, 'result' => 'No outlet data found.']);
}
$validationRules = [
'title' => 'required',
'email' => 'required',
'phone' => 'required',
'address' => 'required',
'state' => 'required',
'postal_code' => 'required',
'country' => 'required',
'latitude' => 'required|numeric',
'longitude' => 'required|numeric',
'outlet_delivery_coverage' => 'required|numeric',
'outlet_tax' => 'permit_empty',
'outlet_operating_days' => 'permit_empty',
'outlet_operating_hours' => 'permit_empty',
'outlet_operating_hours_exceptions' => 'permit_empty',
'outlet_images' => 'permit_empty'
];
// Get all input data
$input = $this->request->getVar();
// Decode only the JSON fields
$jsonFields = [
'outlet_tax',
'outlet_operating_days',
'outlet_operating_hours',
'outlet_operating_hours_exceptions'
];
foreach ($jsonFields as $field) {
if (isset($input[$field]) && is_string($input[$field])) {
$input[$field] = json_decode($input[$field], true);
}
}
if (!$this->validateData($input, $validationRules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$outlet = $this->outlet->find($id);
if(empty($outlet)){
return $this->respond(['status' => 400, 'result' => 'No outlet data found.']);
}
// Prepare outlet data
$outlet_data = ['id' => $id];
$fields = [
'title', 'email', 'phone', 'address', 'state', 'postal_code', 'country',
'status', 'latitude', 'longitude', 'serve_method', 'delivery_options',
'outlet_delivery_coverage', 'order_max_per_hour', 'item_max_per_hour'
];
foreach ($fields as $field) {
if (isset($input[$field])) {
$outlet_data[$field] = $input[$field];
}
}
if (!empty($input['password']) && $input['password'] != 'null' && $input['password'] != '' && $input['password'] != 'undefined') {
$outlet_data['password'] = md5(md5($input['password']));
} else {
$outlet_data['password'] = $outlet['password'];
}
// print_r($outlet_data);exit;
// Start transaction
$this->db->transBegin();
try {
// Update main outlet data
$response = $this->outlet->save($outlet_data);
if (!$response) {
throw new \Exception('Failed to update outlet');
}
// Handle outlet taxes
if (isset($input['outlet_tax']) && is_array($input['outlet_tax'])) {
$existingTaxes = $this->outletTax->where('outlet_id', $id)
->where('deleted_at', null)
->findAll();
$existingTaxIds = array_column($existingTaxes, 'tax_id', 'id');
$updatedTaxIds = [];
foreach ($input['outlet_tax'] as $tax) {
$taxData = [
'outlet_id' => $id,
'tax_id' => $tax['tax_id'],
'updated_at' => date('Y-m-d H:i:s')
];
$taxId = array_search($tax['tax_id'], $existingTaxIds);
if ($taxId !== false) {
$taxData['id'] = $taxId;
$this->outletTax->save($taxData);
$updatedTaxIds[] = $taxId;
} else {
$taxData['created_at'] = date('Y-m-d H:i:s');
$newId = $this->outletTax->insert($taxData);
$updatedTaxIds[] = $newId;
}
}
// Soft delete removed taxes
foreach ($existingTaxes as $existingTax) {
if (!in_array($existingTax['id'], $updatedTaxIds)) {
$this->outletTax->where('id', $existingTax['id'])
->set(['deleted_at' => date('Y-m-d H:i:s')])
->update();
}
}
}
$images = $this->request->getFiles();
if (!empty($images['outlet_images'])) {
$images = $images['outlet_images'];
}
$existing_image = $this->request->getVar("existing_image");
if (!empty($images)) {
$exist_images = $this->outletImage->where('outlet_id', $id)->findAll();
$exist_images_group = array_column($exist_images, 'id');
foreach ($images as $image_file) {
if ($image_file && $image_file->isValid() && !$image_file->hasMoved()) {
$result = save_image_with_compression(
$image_file,
FCPATH . 'backend/uploads/outlets',
FCPATH . 'backend/uploads/outlets_compressed',
900, // width
80 // quality
);
$originalFileName = basename($result['original']);
$compressedFileName = basename($result['compressed']);
if (!$this->outletImage->insert([
'outlet_id' => $id,
'image_url' => $originalFileName,
'compressed_image_url' => $compressedFileName
])) {
throw new \Exception('Failed to update image!');
}
}
}
if (!empty($existing_image)) {
foreach ($exist_images_group as $image_id) {
if (!in_array($image_id, $existing_image)) {
$this->outletImage->delete($image_id);
}
}
}
}
// Handle operating days
if (isset($input['outlet_operating_days']) && is_array($input['outlet_operating_days'])) {
$operatingDays = $input['outlet_operating_days'][0];
$existingDays = $this->outletOperatingDays
->where('outlet_id', $id)
->findAll();
$existingDayMap = [];
foreach ($existingDays as $day) {
$existingDayMap[$day['day_of_week']] = $day;
}
$updatedDays = [];
foreach ($operatingDays as $dayName => $dayData) {
$dayOfWeek = ucfirst(strtolower($dayName));
$dayRecord = [
'outlet_id' => $id,
'day_of_week' => $dayOfWeek,
'is_operated' => $dayData['is_operated'],
'updated_at' => date('Y-m-d H:i:s')
];
if (isset($existingDayMap[$dayOfWeek])) {
$dayRecord['id'] = $existingDayMap[$dayOfWeek]['id'];
$this->outletOperatingDays->save($dayRecord);
$updatedDays[] = $dayOfWeek;
} else {
$dayRecord['created_at'] = date('Y-m-d H:i:s');
$this->outletOperatingDays->insert($dayRecord);
$updatedDays[] = $dayOfWeek;
}
}
// Soft delete removed days
foreach ($existingDays as $day) {
if (!in_array($day['day_of_week'], $updatedDays)) {
$this->outletOperatingDays->where('id', $day['id'])
->set(['deleted_at' => date('Y-m-d H:i:s')])
->update();
}
}
}
// Handle operating hours
if (isset($input['outlet_operating_hours']) && is_array($input['outlet_operating_hours'])) {
$operatingHours = $input['outlet_operating_hours'][0];
$existingHours = $this->outletOperatingHours
->where('outlet_id', $id)
->findAll();
$existingHoursByDay = [];
foreach ($existingHours as $hour) {
$existingHoursByDay[$hour['day_of_week']][] = $hour;
}
foreach ($operatingHours as $dayName => $timeSlots) {
$dayOfWeek = ucfirst(strtolower($dayName));
$updatedHourIds = [];
if (!empty($timeSlots)) {
foreach ($timeSlots as $slot) {
$hourRecord = [
'outlet_id' => $id,
'day_of_week' => $dayOfWeek,
'start_time' => $slot['start_time'],
'end_time' => $slot['end_time'],
'updated_at' => date('Y-m-d H:i:s')
];
$found = false;
if (isset($existingHoursByDay[$dayOfWeek])) {
foreach ($existingHoursByDay[$dayOfWeek] as $existingHour) {
if ($existingHour['start_time'] == $slot['start_time'] &&
$existingHour['end_time'] == $slot['end_time']) {
$hourRecord['id'] = $existingHour['id'];
$this->outletOperatingHours->save($hourRecord);
$updatedHourIds[] = $existingHour['id'];
$found = true;
break;
}
}
}
if (!$found) {
$hourRecord['created_at'] = date('Y-m-d H:i:s');
$newId = $this->outletOperatingHours->insert($hourRecord);
$updatedHourIds[] = $newId;
}
}
}
// Soft delete removed hours
if (isset($existingHoursByDay[$dayOfWeek])) {
foreach ($existingHoursByDay[$dayOfWeek] as $existingHour) {
if (!in_array($existingHour['id'], $updatedHourIds)) {
$this->outletOperatingHours
->where('id', $existingHour['id'])
->set(['deleted_at' => date('Y-m-d H:i:s')])
->update();
}
}
}
}
}
// Handle outlet menu items
// print_r($input);exit;
// print_r($input['outlet_menu']);exit;
if (isset($input['outlet_menu']) && is_array($input['outlet_menu'])) {
// Filter out empty values (including 0 and '')
$filteredMenuIds = array_filter($input['outlet_menu'], function($menuId) {
return $menuId !== '' && $menuId !== null && $menuId !== 0;
});
if (!empty($filteredMenuIds)) {
// Fetch existing menu items
$existingMenu = $this->outlet_menu
->where('outlet_id', $id)
->where('deleted_at', null)
->findAll();
$existingMenuIds = array_column($existingMenu, 'menu_item_id', 'id');
$updatedMenuIds = [];
foreach ($filteredMenuIds as $menuId) {
if (in_array($menuId, $existingMenuIds)) {
$updatedMenuIds[] = $menuId;
} else {
$newId = $this->outlet_menu->insert([
'outlet_id' => $id,
'menu_item_id' => $menuId,
]);
$updatedMenuIds[] = $newId;
}
}
// Delete unused menu items
foreach ($existingMenu as $existing) {
if (!in_array($existing['menu_item_id'], $updatedMenuIds)) {
$this->outlet_menu
->where('menu_item_id', $existing['menu_item_id'])
->where('outlet_id', $id)
->where('deleted_at', null)
->delete();
}
}
} else {
// Delete ALL menu items if array is empty after filtering
$this->outlet_menu
->where('outlet_id', $id)
->where('deleted_at', null)
->delete();
}
}
// Handle operating hours exceptions
if (isset($input['outlet_operating_hours_exceptions']) && is_array($input['outlet_operating_hours_exceptions'])) {
$existingExceptions = $this->outletOperatingHoursExceptions
->where('outlet_id', $id)
->where('deleted_at', null)
->findAll();
$existingExceptionKeys = [];
foreach ($existingExceptions as $ex) {
$key = $ex['date'].'|'.$ex['start_time'].'|'.$ex['end_time'];
$existingExceptionKeys[$key] = $ex['id'];
}
$updatedExceptionIds = [];
foreach ($input['outlet_operating_hours_exceptions'] as $exception) {
$exceptionData = [
'outlet_id' => $id,
'date' => $exception['date'],
'start_time' => $exception['start_time'],
'end_time' => $exception['end_time'],
'notes' => $exception['notes'] ?? null,
'updated_at' => date('Y-m-d H:i:s')
];
$compositeKey = $exception['date'].'|'.$exception['start_time'].'|'.$exception['end_time'];
if (isset($existingExceptionKeys[$compositeKey])) {
$exceptionData['id'] = $existingExceptionKeys[$compositeKey];
$this->outletOperatingHoursExceptions->save($exceptionData);
$updatedExceptionIds[] = $exceptionData['id'];
} else {
$exceptionData['created_at'] = date('Y-m-d H:i:s');
$newId = $this->outletOperatingHoursExceptions->insert($exceptionData);
$updatedExceptionIds[] = $newId;
}
}
// Soft delete removed exceptions
foreach ($existingExceptions as $existingException) {
if (!in_array($existingException['id'], $updatedExceptionIds)) {
$this->outletOperatingHoursExceptions
->where('id', $existingException['id'])
->set(['deleted_at' => date('Y-m-d H:i:s')])
->update();
}
}
}
// Commit transaction
$this->db->transCommit();
// Prepare response
$result = [
'outlet' => array_merge($outlet_data, ['id' => $id]),
'outlet_tax' => $input['outlet_tax'] ?? [],
'outlet_images' => $images['outlet_images'] ?? [],
'outlet_operating_days' => $input['outlet_operating_days'] ?? [],
'outlet_operating_hours' => $input['outlet_operating_hours'] ?? [],
'outlet_operating_hours_exceptions' => $input['outlet_operating_hours_exceptions'] ?? []
];
return $this->respond(['status' => 200, 'result' => $result]);
} catch (\Exception $e) {
$this->db->transRollback();
return $this->respond(['status' => 400, 'message' => 'Failed to update outlet: ' . $e->getMessage()]);
}
}
public function delete($id = null)
{
$outlet = $this->outlet->find($id);
if(empty($outlet)){
return $this->respond(['status' => 400, 'result' => 'No outlet data found.']);
}
$this->outlet->delete($id);
$this->outletTax->where('outlet_id', $id)->delete();
$this->outletOperatingDays->where('outlet_id', $id)->delete();
$this->outletOperatingHours->where('outlet_id', $id)->delete();
$this->outletOperatingHoursExceptions->where('outlet_id', $id)->delete();
return $this->respond(['status' => 200, 'result' => 'Outlet deleted successfully.']);
}
public function updatePassword($id = null)
{
$outlet = $this->outlet->find($id);
if(empty($outlet)){
return $this->respond(['status' => 400, 'result' => 'No outlet data found.']);
}
$password = $this->request->getVar('password');
$this->outlet->update($id, ['password' => md5(md5($password))]);
return $this->respond(['status' => 200, 'result' => 'Outlet password updated successfully.']);
}
public function addBulk()
{
$rules = [
'outlet_ids' => 'required|is_array',
'menu_item_ids' => 'required|is_array',
];
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$outletIds = $this->request->getVar('outlet_ids');
$menuItemIds = $this->request->getVar('menu_item_ids');
$insertData = [];
$insertCount = 0;
$restoreCount = 0;
foreach ($outletIds as $outletId) {
foreach ($menuItemIds as $menuItemId) {
$exists = $this->outlet_menu
->withDeleted() // include soft deleted to detect them
->where('outlet_id', $outletId)
->where('menu_item_id', $menuItemId)
->first();
if (!$exists) {
$insertData[] = [
'outlet_id' => $outletId,
'menu_item_id' => $menuItemId,
];
$insertCount++;
} elseif (!empty($exists['deleted_at'])) {
// Restore if it was soft deleted
$this->outlet_menu
->update($exists['id'], ['deleted_at' => null]);
$restoreCount++;
}
}
}
if (!empty($insertData)) {
$this->outlet_menu->insertBatch($insertData);
}
return $this->respond([
'status' => 200,
'message' => "Menu items added to outlets successfully.",
'added' => $insertCount,
'restored' => $restoreCount
]);
}
public function deleteBulk()
{
$rules = [
'outlet_ids' => 'required|is_array',
'menu_item_ids' => 'required|is_array',
];
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$outletIds = $this->request->getVar('outlet_ids');
$menuItemIds = $this->request->getVar('menu_item_ids');
$deleteCount = 0;
foreach ($outletIds as $outletId) {
// Soft delete only the specified menu items for each outlet
$deleteCount += $this->outlet_menu
->where('outlet_id', $outletId)
->whereIn('menu_item_id', $menuItemIds)
->delete(); // soft delete because model usesSoftDeletes = true
}
return $this->respond([
'status' => 200,
'message' => "Outlet menus deleted successfully.",
'deleted' => $deleteCount,
'outlets_processed' => count($outletIds),
'menu_items_processed' => count($menuItemIds)
]);
}
}
@@ -0,0 +1,542 @@
<?php
namespace App\Controllers\Backend;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
use App\Models\PromoSetting;
use App\Models\PromoCode;
use App\Models\PromoRedemptionRecord;
helper('promo'); // loads app/Helpers/promo_helper.php
class PromoSettingController extends ResourceController
{
private $promoSetting;
private $promoCode;
private $promoRedemptionRecord;
protected $db;
public function __construct()
{
$this->promoSetting = new PromoSetting();
$this->promoCode = new PromoCode();
$this->promoRedemptionRecord = new PromoRedemptionRecord();
$this->db = \Config\Database::connect();
}
public function index()
{
$promoSettingsData = $this->promoSetting->findAll();
// print_r($promoSettingsData);exit;
if(empty($promoSettingsData)){
return $this->respond(['status' => 400, 'result' => 'No data found.']);
}
$result = [];
foreach($promoSettingsData as $key => $value){
$result[$key] = [
"id" => $value['id'],
"title" => $value['title'],
"status" => $value['status'],
];
$promoCode = $this->promoCode->where('promo_setting_id', $value['id'])->findAll()[0];
if(!empty($promoCode)){
$result[$key]['promoCode'] = $promoCode['code'];
$result[$key]['total_redemption_limit'] = $promoCode['total_redemption_limit'];
$result[$key]['start_date'] = $promoCode['start_date'];
$result[$key]['end_date'] = $promoCode['end_date'];
$count_redemptions = $this->promoRedemptionRecord->where('promo_codes_id', $promoCode['id'])->countAllResults();
$result[$key]['left_redemption'] = $promoCode['total_redemption_limit'] - $count_redemptions;
}
}
return $this->respond(['status' => 200, 'message' => 'Retrieve Promo Setting Successfully!', 'result' => $result]);
}
public function show($id = null)
{
$promoSetting = $this->promoSetting->find($id);
if(empty($promoSetting)){
return $this->respond(['status' => 400, 'result' => 'No data found.']);
}
// print_r(json_decode($promoSetting['promo_setting'], true));exit;
$promoSettingData = revertPromoSetting(json_decode($promoSetting['promo_setting'], true));
// print_r($promoSettingData);exit;
$result = [
"id" => $promoSetting['id'],
"promotionType" => $promoSetting['promotion_type'],
"promotionName" => $promoSetting['title'],
"promotionDescription" => $promoSetting['description'],
//promo_setting
"discountAmount" => $promoSettingData['discountAmount'],
"discountType" => $promoSettingData['discountType'],
"getNumber" => $promoSettingData['getNumber'],
"minimumSpend" => $promoSettingData['minimumSpend'],
"itemCategory1" => $promoSettingData['itemCategory1'],
"itemCategoryID1" => $promoSettingData['itemCategoryID1'],
"itemCategory2" => $promoSettingData['itemCategory2'],
"itemCategoryID2" => $promoSettingData['itemCategoryID2'],
"minimumQuantity" => $promoSettingData['minimumQuantity'],
"everyQuantity" => $promoSettingData['everyQuantity'],
// "minimumAmount" => $promoSettingData['minimumAmount'],
"promoType" => $promoSettingData['promoType'],
];
// print_r($result);exit;
$promoCode = $this->promoCode->where('promo_setting_id', $promoSetting['id'])->findAll()[0];
$result['promoCodeId'] = $promoCode['id'];
$result['promotionCode'] = $promoCode['code'];
$result['usageLimit'] = $promoCode['usage_limit_type'];
$result['voucherLimitPerCustomer'] = $promoCode['usage_limit'];
$result['totalRedemptionLimit'] = $promoCode['total_redemption_limit'];
$result['storeStartDate'] = $promoCode['start_date'];
$result['storeEndDate'] = $promoCode['end_date'];
$result['customDayTime'] = json_decode($promoCode['customize_validity'], true);
$result['applyToDeliveryPickup'] = $promoCode['promo_order_type'];
// print_r($result);exit;
$count_redemptions = $this->promoRedemptionRecord->where('promo_codes_id', $promoCode['id'])->countAllResults();
$result['left_redemption'] = $promoCode['total_redemption_limit'] - $count_redemptions;
return $this->respond(['status' => 200, 'message' => 'Retrieve Promo Setting Successfully!', 'result' => $result]);
}
public function create()
{
$validationRules = [
"promotionType" => "required",
"promotionName" => "required",
"promotionDescription" => "permit_empty",
"usageLimit" => "required",
"totalRedemptionLimit" => "required",
"voucherLimitPerCustomer" => "permit_empty",
"startDate" => "permit_empty",
"endDate" => "permit_empty",
"customDayTime" => "permit_empty",
"applyToDeliveryPickup" => "required",
];
$validation = $this->validate($validationRules);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$this->db->transBegin();
try {
$applyToDeliveryPickup = $this->request->getVar("applyToDeliveryPickup");
if (!is_array($applyToDeliveryPickup)) {
throw new \Exception("applyToDeliveryPickup must be an array");
}
$validOptions = ['delivery', 'pickup', 'dinein'];
foreach ($applyToDeliveryPickup as $option) {
if (!in_array($option, $validOptions)) {
throw new \Exception("Invalid option in applyToDeliveryPickup: " . $option);
}
}
if (empty($applyToDeliveryPickup)) {
throw new \Exception("applyToDeliveryPickup cannot be empty");
}
$minimumSpend = $this->request->getVar("minimumSpend");
$minimumQuantity = $this->request->getVar("minimumQuantity");
$everyQuantity = $this->request->getVar("everyQuantity");
$itemCategory1 = $this->request->getVar("itemCategory1");
$itemCategoryID1 = $this->request->getVar("itemCategoryID1");
$discountAmount = $this->request->getVar("discountAmount");
$discountType = $this->request->getVar("discountType");
$getNumber = $this->request->getVar("getNumber");
$itemCategory2 = $this->request->getVar("itemCategory2");
$itemCategoryID2 = $this->request->getVar("itemCategoryID2");
$promoType = $this->request->getVar("promoType");
$promoSetting = [
"MinimumSpend" => [
"type" => 'none',
"filter" => [],
"amount_type" => '',
"amount" => 0
],
"Promo" => [
"promo_type" => $promoType,
"discount_type" => $discountType,
"filter_type" => 'total',
"filter" => [],
"amount" => 0,
"free_item" => []
]
];
if (!empty($minimumSpend) || !empty($minimumQuantity) || !empty($everyQuantity)) {
if (!empty($itemCategory1)) {
$promoSetting["MinimumSpend"]["type"] = $itemCategory1;
$promoSetting["MinimumSpend"]["filter"] = is_array($itemCategoryID1) ? $itemCategoryID1 : [];
} else {
$promoSetting["MinimumSpend"]["type"] = "total";
$promoSetting["MinimumSpend"]["filter"] = [];
}
if (!empty($minimumSpend)) {
$promoSetting["MinimumSpend"]["amount_type"] = "amount";
$promoSetting["MinimumSpend"]["amount"] = floatval($minimumSpend);
} elseif (!empty($minimumQuantity)) {
$promoSetting["MinimumSpend"]["amount_type"] = "quantity";
$promoSetting["MinimumSpend"]["amount"] = intval($minimumQuantity);
} elseif (!empty($everyQuantity)) {
$promoSetting["MinimumSpend"]["amount_type"] = "every_quantity";
$promoSetting["MinimumSpend"]["amount"] = intval($everyQuantity);
}
}
$promoSetting["Promo"]["promo_type"] = $promoType;
if ($promoType === "free_item") {
$promoSetting["Promo"]["discount_type"] = '';
} else {
$promoSetting["Promo"]["discount_type"] = $discountType;
}
if (!empty($itemCategory2)) {
$promoSetting["Promo"]["filter_type"] = $itemCategory2;
} else {
$promoSetting["Promo"]["filter_type"] = "total";
}
if ($promoType === "free_item") {
$promoSetting["Promo"]["free_item"] = is_array($itemCategoryID2) ? $itemCategoryID2 : [];
$promoSetting["Promo"]["filter"] = [];
} else {
$promoSetting["Promo"]["filter"] = is_array($itemCategoryID2) ? $itemCategoryID2 : [];
$promoSetting["Promo"]["free_item"] = [];
}
if (!empty($discountAmount)) {
$promoSetting["Promo"]["amount"] = floatval($discountAmount);
} elseif (!empty($getNumber)) {
$promoSetting["Promo"]["amount"] = intval($getNumber);
}
$promoSettingData = [
"promotion_type" => $this->request->getVar("promotionType"),
"title" => $this->request->getVar("promotionName"),
"description" => $this->request->getVar("promotionDescription"),
"promo_setting" => json_encode($promoSetting)
];
$id = $this->promoSetting->insert($promoSettingData);
if(!$id){
throw new \Exception("Failed to create promotion setting.");
}
$promotionCode = $this->request->getVar("promotionCode");
if(!empty($promotionCode)){
$isExists = $this->promoCode->where('code', $promotionCode)->find();
if(!empty($isExists)){
throw new \Exception("Promotion code already exists.");
}
}
$promoOrderTypeString = implode(',', $applyToDeliveryPickup);
$promoCodeData = [
'promo_setting_id' => $id,
"code" => $promotionCode,
"usage_limit_type" => $this->request->getVar("usageLimit"),
"usage_limit" => $this->request->getVar("voucherLimitPerCustomer"),
"total_redemption_limit" => $this->request->getVar("totalRedemptionLimit"),
"start_date" => $this->request->getVar("storeStartDate"),
"end_date" => $this->request->getVar("storeEndDate"),
"customize_validity" => json_encode($this->request->getVar("customDayTime")),
"promo_order_type" => $promoOrderTypeString
];
$promoCodeId = $this->promoCode->insert($promoCodeData);
if(!$promoCodeId){
throw new \Exception("Failed to create promotion code.");
}
$this->db->transCommit();
return $this->respond([
'status' => 200,
'message' => 'Promotion created successfully.',
'data' => [
'promo_setting_id' => $id,
'promo_code_id' => $promoCodeId,
'promo_order_type' => $promoOrderTypeString
]
]);
} catch (\Exception $e) {
$this->db->transRollback();
log_message('error', 'Promotion creation failed: ' . $e->getMessage());
return $this->failServerError('Failed to create promotion: '. $e->getMessage());
}
}
public function update($id = null)
{
$promoSetting = $this->promoSetting->find($id);
if(empty($promoSetting)){
return $this->respond(['status' => 400, 'result' => 'No promotion setting data found.']);
}
$validationRules = [
"promotionType" => "required",
"promotionName" => "required",
"promotionDescription" => "permit_empty",
"usageLimit" => "required",
"totalRedemptionLimit" => "required",
"voucherLimitPerCustomer" => "permit_empty",
"startDate" => "permit_empty",
"endDate" => "permit_empty",
"customDayTime" => "permit_empty",
"applyToDeliveryPickup" => "required",
];
$validation = $this->validate($validationRules);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$this->db->transBegin();
try {
$applyToDeliveryPickup = $this->request->getVar("applyToDeliveryPickup");
if (!is_array($applyToDeliveryPickup)) {
throw new \Exception("applyToDeliveryPickup must be an array");
}
$validOptions = ['delivery', 'pickup', 'dinein'];
foreach ($applyToDeliveryPickup as $option) {
if (!in_array($option, $validOptions)) {
throw new \Exception("Invalid option in applyToDeliveryPickup: " . $option);
}
}
if (empty($applyToDeliveryPickup)) {
throw new \Exception("applyToDeliveryPickup cannot be empty");
}
$minimumSpend = $this->request->getVar("minimumSpend");
$minimumQuantity = $this->request->getVar("minimumQuantity");
$everyQuantity = $this->request->getVar("everyQuantity");
$itemCategory1 = $this->request->getVar("itemCategory1");
$itemCategoryID1 = $this->request->getVar("itemCategoryID1");
// print_r($itemCategory1);
// print_r($itemCategoryID1);
// exit;
$discountAmount = $this->request->getVar("discountAmount");
$discountType = $this->request->getVar("discountType");
$getNumber = $this->request->getVar("getNumber");
$itemCategory2 = $this->request->getVar("itemCategory2");
$itemCategoryID2 = $this->request->getVar("itemCategoryID2");
$promoType = $this->request->getVar("promoType");
$promoSetting = [
"MinimumSpend" => [
"type" => 'none',
"filter" => [],
"amount_type" => '',
"amount" => 0
],
"Promo" => [
"promo_type" => $promoType,
"discount_type" => $discountType,
"filter_type" => 'total',
"filter" => [],
"amount" => 0,
"free_item" => []
]
];
if (!empty($itemCategory1)) {
$promoSetting["MinimumSpend"]["type"] = $itemCategory1;
$promoSetting["MinimumSpend"]["filter"] = is_array($itemCategoryID1) ? $itemCategoryID1 : [];
} else {
$promoSetting["MinimumSpend"]["type"] = "total";
$promoSetting["MinimumSpend"]["filter"] = [];
}
if (!empty($minimumSpend)) {
$promoSetting["MinimumSpend"]["amount_type"] = "amount";
$promoSetting["MinimumSpend"]["amount"] = floatval($minimumSpend);
} elseif (!empty($minimumQuantity)) {
$promoSetting["MinimumSpend"]["amount_type"] = "quantity";
$promoSetting["MinimumSpend"]["amount"] = intval($minimumQuantity);
} elseif (!empty($everyQuantity)) {
$promoSetting["MinimumSpend"]["amount_type"] = "every_quantity";
$promoSetting["MinimumSpend"]["amount"] = intval($everyQuantity);
}
$promoSetting["Promo"]["promo_type"] = $promoType;
if ($promoType === "free_item") {
$promoSetting["Promo"]["discount_type"] = '';
} else {
$promoSetting["Promo"]["discount_type"] = $discountType;
}
if (!empty($itemCategory2)) {
$promoSetting["Promo"]["filter_type"] = $itemCategory2;
} else {
$promoSetting["Promo"]["filter_type"] = "total";
}
if ($promoType === "free_item") {
$promoSetting["Promo"]["free_item"] = is_array($itemCategoryID2) ? $itemCategoryID2 : [];
$promoSetting["Promo"]["filter"] = [];
} else {
$promoSetting["Promo"]["filter"] = is_array($itemCategoryID2) ? $itemCategoryID2 : [];
$promoSetting["Promo"]["free_item"] = [];
}
if (!empty($discountAmount)) {
$promoSetting["Promo"]["amount"] = floatval($discountAmount);
} elseif (!empty($getNumber)) {
$promoSetting["Promo"]["amount"] = intval($getNumber);
}
$promoSettingData = [
"id" => $id,
"promotion_type" => $this->request->getVar("promotionType"),
"title" => $this->request->getVar("promotionName"),
"description" => $this->request->getVar("promotionDescription"),
"promo_setting" => json_encode($promoSetting)
];
$response = $this->promoSetting->save($promoSettingData);
if (!$response) {
throw new \Exception('Failed to update promotion settings.');
}
$promoCode = $this->promoCode->where('promo_setting_id', $id)->find()[0];
if(!$promoCode){
throw new \Exception("Failed to update promotion code.");
}
$promotionCode = $this->request->getVar("promotionCode");
if(!empty($promotionCode)){
$isExists = $this->promoCode->where('code', $promotionCode)->where('id !=', $promoCode['id'])->find();
if(!empty($isExists)){
throw new \Exception("Promotion code already exists.");
}
}
$promoOrderTypeString = implode(',', $applyToDeliveryPickup);
$promoCodeData = [
'id' => $promoCode['id'],
'promo_setting_id' => $id,
'code' => $promotionCode,
'usage_limit_type' => $this->request->getVar("usageLimit"),
'usage_limit' => $this->request->getVar("voucherLimitPerCustomer"),
'total_redemption_limit' => $this->request->getVar("totalRedemptionLimit"),
'start_date' => $this->request->getVar("storeStartDate"),
'end_date' => $this->request->getVar("storeEndDate"),
'customize_validity' => json_encode($this->request->getVar("customDayTime")),
'promo_order_type' => $promoOrderTypeString
];
$promoCodeResponse = $this->promoCode->save($promoCodeData);
if (!$promoCodeResponse) {
throw new \Exception('Failed to update promotion code.');
}
$this->db->transCommit();
return $this->respond([
'status' => 200,
'message' => 'Promotion updated successfully.',
'data' => [
'promo_setting_id' => $id,
'promo_code_id' => $promoCode['id'],
'promo_order_type' => $promoOrderTypeString
]
]);
} catch (\Exception $e) {
$this->db->transRollback();
log_message('error', 'Promotion update failed: ' . $e->getMessage());
return $this->failServerError('Failed to update promotion: '. $e->getMessage());
}
}
public function delete($id = null)
{
$promoSetting = $this->promoSetting->find($id);
// print_r($promoSetting);exit;
if(empty($promoSetting)){
return $this->respond(['status' => 400, 'result' => 'No promotion setting data found.']);
}
$this->promoSetting->delete($id);
$this->promoCode->where('promo_setting_id', $id)->delete();
return $this->respond(['status' => 200, 'result' => 'Promotion deleted successfully.']);
}
public function promoSettingList(){
$promo_setting_list = $this->promoSetting->findAll();
if(empty($promo_setting_list)){
return $this->fail('No promo setting data found!');
}
$data_list = [];
foreach ($promo_setting_list as $key => $value) {
$data_list[] = [
'id' => $value['id'],
'promotion_type' => $value['promotion_type'],
'title' => $value['title'],
'description'=> $value['description'],
'status' => $value['status'],
'promo_setting' => $value['promo_setting'],
];
}
return $this->respond([
'status'=> 200,
'message' => 'Promo setting record found!',
'data'=> $data_list
]);
}
}
+205
View File
@@ -0,0 +1,205 @@
<?php
namespace App\Controllers\Backend;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\PwpModel;
use CodeIgniter\RESTful\ResourceController;
class PwpController extends ResourceController
{
protected $pwpModel;
public function __construct()
{
$this->pwpModel = new PwpModel();
$this->db = db_connect();
}
public function index()
{
$pwps = $this->pwpModel->findAll();
foreach ($pwps as &$pwp) {
$selectedIds = array_filter(array_map('trim', explode(',', $pwp['selected_item'])));
$pwp['selected_item_details'] = [];
if (!empty($selectedIds)) {
$pwp['selected_item_details'] = $this->db->table('menu_items')
->select('id, title, price, status')
->whereIn('id', $selectedIds)
->where('deleted_at', null)
->orderBy("created_at", "ASC")
->get()
->getResultArray();
}
$pwpIds = array_filter(array_map('trim', explode(',', $pwp['pwp_item_id'])));
$pwp['pwp_item_details'] = [];
if (!empty($pwpIds)) {
$pwp['pwp_item_details'] = $this->db->table('menu_items')
->select('id, title, price, status')
->whereIn('id', $pwpIds)
->where('deleted_at', null)
->orderBy("created_at", "DESC")
->get()
->getResultArray();
}
}
return $this->respond([
'status' => 200,
'data' => $pwps
]);
}
public function create()
{
$json = $this->request->getJSON(true);
if (empty($json['mode']) || empty($json['order_index']) || empty($json['amount']) || empty($json['amount_type'])) {
return $this->failValidationErrors('mode, order_index, amount and amount_type are required');
}
$pwpItems = is_array($json['pwp_item_id']) ? implode(',', $json['pwp_item_id']) : $json['pwp_item_id'];
$selectedIds = is_array($json['selected_item']) ? $json['selected_item'] : explode(',', $json['selected_item']);
$selectedItems = implode(',', $selectedIds);
$data = [
'mode' => $json['mode'],
'order_index' => $json['order_index'],
'pwp_item_id' => $pwpItems,
'amount' => $json['amount'],
'amount_type' => $json['amount_type'],
'selected_item' => $selectedItems,
];
if (!$this->pwpModel->insert($data)) {
return $this->fail($this->pwpModel->errors());
}
$pwpItemDetails = [];
if (!empty($pwpItems)) {
$pwpItemArray = explode(',', $pwpItems);
$pwpItemDetails = $this->db->table('menu_items')
->select('id, title, price')
->whereIn('id', $pwpItemArray)
->where('status', 'active')
->get()
->getResultArray();
}
$selectedItemDetails = [];
if (!empty($selectedIds)) {
$selectedItemDetails = $this->db->table('menu_items')
->select('id, title, price')
->whereIn('id', $selectedIds)
->where('status', 'active')
->get()
->getResultArray();
}
return $this->respondCreated([
'message' => 'PWP promo created successfully',
'data' => array_merge($data, [
'pwp_item_id' => $pwpItems,
'pwp_item_details' => $pwpItemDetails,
'selected_item' => $selectedIds,
'selected_item_details' => $selectedItemDetails
])
]);
}
public function show($id = null)
{
$pwp = $this->pwpModel->find($id);
if (!$pwp) {
return $this->failNotFound('PWP not found');
}
$selectedIds = explode(',', $pwp['selected_item']);
$pwp['selected_item_details'] = [];
if (!empty($selectedIds[0])) {
$pwp['selected_item_details'] = $this->db->table('menu_items')
->select('id, title, price, status')
->whereIn('id', $selectedIds)
->where('status', 'active')
->get()
->getResultArray();
}
return $this->respond($pwp);
}
public function update($id = null)
{
$json = $this->request->getJSON(true);
if (empty($json['mode']) || empty($json['order_index']) ) {
return $this->failValidationErrors('mode, order_index are required');
}
$pwpItems = is_array($json['pwp_item_id']) ? implode(',', $json['pwp_item_id']) : $json['pwp_item_id'];
$selectedIds = is_array($json['selected_item']) ? $json['selected_item'] : explode(',', $json['selected_item']);
$selectedItems = implode(',', $selectedIds);
$data = [
'mode' => $json['mode'],
'order_index' => $json['order_index'],
'pwp_item_id' => $pwpItems,
'amount' => $json['amount'] ?? 0.00,
'amount_type' => $json['amount_type'] ?? 'amount',
'selected_item' => $selectedItems,
];
if (!$this->pwpModel->update($id, $data)) {
return $this->fail($this->pwpModel->errors());
}
$selectedItemDetails = [];
if (!empty($selectedIds)) {
$selectedItemDetails = $this->db->table('menu_items')
->select('id, title, price')
->whereIn('id', $selectedIds)
->where('status', 'active')
->get()
->getResultArray();
}
return $this->respond([
'status' => 200,
'message' => 'PWP promo updated successfully',
'data' => array_merge($data, [
'selected_item' => $selectedIds,
'selected_item_details' => $selectedItemDetails
])
]);
}
public function delete($id = null)
{
if (!$this->pwpModel->find($id)) {
return $this->respond([
'status' => 404,
'message' => 'PWP not found'
], 404);
}
if (!$this->pwpModel->delete($id)) {
return $this->respond([
'status' => 500,
'message' => 'Failed to delete PWP'
], 500);
}
return $this->respond([
'status' => 200,
'message' => 'PWP promo deleted successfully'
], 200);
}
}
@@ -0,0 +1,879 @@
<?php
namespace App\Controllers\Backend;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
use App\Models\SettingCustomerType;
use App\Models\SettingMembershipTier;
use App\Models\SettingTax;
use App\Models\SlideshowModel;
helper('image');
use App\Models\SettingCheckinRule;
class SettingController extends ResourceController
{
private $customerTypeModel;
private $membershipTierModel;
private $checkinRuleModel;
private $taxModel;
private $slideshowModel;
public function __construct()
{
$this->customerTypeModel = new SettingCustomerType();
$this->membershipTierModel = new SettingMembershipTier();
$this->taxModel = new SettingTax();
$this->checkinRuleModel = new SettingCheckinRule();
$this->slideshowModel = new SlideshowModel();
}
public function customerTypesindex()
{
$types = $this->customerTypeModel->findAll();
if (empty($types)) {
$response = [
'status' => 'success',
'message' => 'No customer type data found.',
'data' => []
];
return $this->respond($response, 200);
}
$response = [
'status' => 'success',
'message' => 'Customer types retrieved successfully.',
'data' => $types
];
return $this->respond($response, 200);
}
public function customerTypesshow($id = null)
{
$type = $this->customerTypeModel->find($id);
if (!$type) {
$response = [
'status' => 'error',
'message' => 'Customer type not found.',
'data' => null
];
return $this->respond($response, 404);
}
$response = [
'status' => 'success',
'message' => 'Customer type retrieved successfully.',
'data' => $type
];
return $this->respond($response, 200);
}
public function customerTypescreate()
{
$validationRules = [
'name' => 'required',
];
if (!$this->validate($validationRules)) {
$response = [
'status' => 'error',
'message' => 'Validation failed.',
'data' => $this->validator->getErrors()
];
return $this->respond($response, 422);
}
$data = [
'name' => $this->request->getVar('name'),
];
$id = $this->customerTypeModel->insert($data);
if ($id) {
$result = $this->customerTypeModel->find($id);
$response = [
'status' => 'success',
'message' => 'Customer type created successfully.',
'data' => $result
];
return $this->respond($response, 201);
}
$response = [
'status' => 'error',
'message' => 'Failed to create customer type.',
'data' => null
];
return $this->respond($response, 500);
}
public function customerTypesupdate($id = null)
{
// Check if customer type exists and is not soft-deleted
$existingType = $this->customerTypeModel->find($id);
if (!$existingType) {
$response = [
'status' => 'error',
'message' => 'Customer type not found.',
'data' => null
];
return $this->respond($response, 404);
}
// Use raw input for PUT/PATCH, or getVar for POST fallback
$input = $this->request->getRawInput();
if (empty($input)) {
$input = $this->request->getVar();
}
$data = [];
if (isset($input['name'])) {
$data['name'] = $input['name'];
}
if (empty($data)) {
$response = [
'status' => 'error',
'message' => 'No data provided to update.',
'data' => null
];
return $this->respond($response, 400);
}
$this->customerTypeModel->update($id, $data);
$updatedType = $this->customerTypeModel->find($id);
$response = [
'status' => 'success',
'message' => 'Customer type updated successfully.',
'data' => $updatedType
];
return $this->respond($response, 200);
}
public function customerTypesdelete($id = null)
{
$existingType = $this->customerTypeModel->find($id);
if (!$existingType) {
$response = [
'status' => 'error',
'message' => 'Customer type not found.',
'data' => null
];
return $this->respond($response, 404);
}
$this->customerTypeModel->delete($id); // This performs soft delete
$response = [
'status' => 'success',
'message' => 'Customer type deleted successfully.',
'data' => null
];
return $this->respond($response, 200);
}
public function membershipTiersIndex()
{
$tiers = $this->membershipTierModel->findAll();
if (empty($tiers)) {
$response = [
'status' => 'error',
'message' => 'No membership tiers found.',
'data' => []
];
return $this->respond($response, 200);
}
$response = [
'status' => 'success',
'message' => 'Membership tiers retrieved successfully.',
'data' => $tiers
];
return $this->respond($response, 200);
}
public function membershipTiersShow($id = null)
{
$tier = $this->membershipTierModel->find($id);
if (!$tier) {
$response = [
'status' => 'error',
'message' => 'Membership tier not found.',
'data' => null
];
return $this->respond($response, 404);
}
$response = [
'status' => 'success',
'message' => 'Membership tier retrieved successfully.',
'data' => $tier
];
return $this->respond($response, 200);
}
public function membershipTiersCreate()
{
$validationRules = [
'name' => 'required',
'min_points' => 'required|numeric',
// 'discount_rate' => 'required|numeric',
// 'category_id' => 'required|integer'
];
if (!$this->validate($validationRules)) {
$response = [
'status' => 'error',
'message' => 'Validation failed.',
'data' => $this->validator->getErrors()
];
return $this->respond($response, 422);
}
$name = $this->request->getVar('name');
$existingTier = $this->membershipTierModel->where('name', $name)->first();
if ($existingTier) {
$response = [
'status' => 'error',
'message' => "A membership tier with the name '{$name}' already exists.",
'data' => null
];
return $this->respond($response, 409); // Conflict
}
$data = [
'name' => $this->request->getVar('name'),
'min_points' => $this->request->getVar('min_points'),
'discount_rate' => $this->request->getVar('discount_rate'),
'color' => $this->request->getVar('color'),
'category_id' => $this->request->getVar('category_id'), // Add category_id
];
$id = $this->membershipTierModel->insert($data);
if ($id) {
$result = $this->membershipTierModel->find($id);
$response = [
'status' => 'success',
'message' => 'Membership tier created successfully.',
'data' => $result
];
return $this->respond($response, 201);
}
$response = [
'status' => 'error',
'message' => 'Failed to create membership tier.',
'data' => null
];
return $this->respond($response, 500);
}
public function membershipTiersUpdate($id = null)
{
$existing = $this->membershipTierModel->find($id);
if (!$existing) {
$response = [
'status' => 'error',
'message' => 'Membership tier not found.',
'data' => null
];
return $this->respond($response, 404);
}
$input = $this->request->getRawInput();
if (empty($input)) {
$input = $this->request->getVar();
}
$data = [];
if (isset($input['name'])) {
$data['name'] = $input['name'];
}
if (isset($input['min_points'])) {
$data['min_points'] = $input['min_points'];
}
// if (isset($input['discount_rate'])) {
// $data['discount_rate'] = $input['discount_rate'];
// }
if (isset($input['color'])) {
$data['color'] = $input['color'];
}
// if (isset($input['category_id'])) {
// $data['category_id'] = $input['category_id']; // Add category_id
// }
if (empty($data)) {
$response = [
'status' => 'error',
'message' => 'No data provided to update.',
'data' => null
];
return $this->respond($response, 400);
}
$this->membershipTierModel->update($id, $data);
$updated = $this->membershipTierModel->find($id);
$response = [
'status' => 'success',
'message' => 'Membership tier updated successfully.',
'data' => $updated
];
return $this->respond($response, 200);
}
public function membershipTiersDelete($id = null)
{
$existing = $this->membershipTierModel->find($id);
if (!$existing) {
$response = [
'status' => 'error',
'message' => 'Membership tier not found.',
'data' => null
];
return $this->respond($response, 404);
}
$this->membershipTierModel->delete($id);
$response = [
'status' => 'success',
'message' => 'Membership tier deleted successfully.',
'data' => null
];
return $this->respond($response, 200);
}
public function taxIndex()
{
$taxes = $this->taxModel->findAll();
if (empty($taxes)) {
$response = [
'status' => 'error',
'message' => 'No tax data found.',
'data' => []
];
return $this->respond($response, 200);
}
$response = [
'status' => 'success',
'message' => 'Tax data retrieved successfully.',
'data' => $taxes
];
return $this->respond($response, 200);
}
public function taxShow($id = null)
{
$tax = $this->taxModel->find($id);
if (!$tax) {
$response = [
'status' => 'error',
'message' => 'Tax entry not found.',
'data' => null
];
return $this->respond($response, 404);
}
$response = [
'status' => 'success',
'message' => 'Tax entry retrieved successfully.',
'data' => $tax
];
return $this->respond($response, 200);
}
public function taxCreate()
{
$validationRules = [
'outlet_id' => 'required',
'tax_type' => 'required',
'tax_rate' => 'required|numeric',
'order_type' => 'required',
];
if (!$this->validate($validationRules)) {
$response = [
'status' => 'error',
'message' => 'Validation failed.',
'data' => $this->validator->getErrors()
];
return $this->respond($response, 400);
}
// Process outlet_ids - convert to comma-separated string
$outletIds = $this->request->getPost('outlet_id');
// If it's an array (from multiple select), convert to comma-separated string
if (is_array($outletIds)) {
$outletIds = implode(',', $outletIds);
}
// Validate that outlet_ids contain only integers and commas
if (!preg_match('/^[0-9,]+$/', $outletIds)) {
$response = [
'status' => 'error',
'message' => 'Invalid outlet IDs format. Only integers and commas allowed.',
'data' => null
];
return $this->respond($response, 400);
}
$data = [
'outlet_id' => $outletIds,
'tax_type' => $this->request->getPost('tax_type'),
'tax_rate' => $this->request->getPost('tax_rate'),
'order_type' => $this->request->getVar('order_type'),
];
$id = $this->taxModel->insert($data);
if ($id) {
$result = $this->taxModel->find($id);
$response = [
'status' => 'success',
'message' => 'Tax created successfully.',
'data' => $result
];
return $this->respond($response, 201);
}
$response = [
'status' => 'error',
'message' => 'Failed to create tax entry.',
'data' => null
];
return $this->respond($response, 500);
}
public function taxUpdate($id = null)
{
$tax = $this->taxModel->find($id);
if (!$tax) {
$response = [
'status' => 'error',
'message' => 'Tax entry not found.',
'data' => null
];
return $this->respond($response, 404);
}
$data = [];
// Process outlet_ids if provided
if ($this->request->getPost('outlet_id')) {
$outletIds = $this->request->getPost('outlet_id');
// If it's an array, convert to comma-separated string
if (is_array($outletIds)) {
$outletIds = implode(',', $outletIds);
}
// Validate that outlet_ids contain only integers and commas
if (!preg_match('/^[0-9,]+$/', $outletIds)) {
$response = [
'status' => 'error',
'message' => 'Invalid outlet IDs format. Only integers and commas allowed.',
'data' => null
];
return $this->respond($response, 400);
}
$data['outlet_id'] = $outletIds;
}
// Add other fields if provided
if ($this->request->getPost('tax_type')) {
$data['tax_type'] = $this->request->getPost('tax_type');
}
if ($this->request->getPost('tax_rate')) {
$data['tax_rate'] = $this->request->getPost('tax_rate');
}
if ($this->request->getVar('order_type')) {
$data['order_type'] = $this->request->getVar('order_type');
}
if (empty($data)) {
$response = [
'status' => 'error',
'message' => 'No data provided to update.',
'data' => null
];
return $this->respond($response, 400);
}
$this->taxModel->update($id, $data);
$updated = $this->taxModel->find($id);
$response = [
'status' => 'success',
'message' => 'Tax updated successfully.',
'data' => $updated
];
return $this->respond($response, 200);
}
public function taxDelete($id = null)
{
$tax = $this->taxModel->find($id);
if (!$tax) {
$response = [
'status' => 'error',
'message' => 'Tax entry not found.',
'data' => null
];
return $this->respond($response, 404);
}
$this->taxModel->delete($id);
$response = [
'status' => 'success',
'message' => 'Tax deleted successfully.',
'data' => null
];
return $this->respond($response, 200);
}
// public function slideshowIndex()
// {
// $slides = $this->slideshowModel->findAll();
// return $this->respond(['status' => 200, 'result' => $slides]);
// }
public function slideshowIndex()
{
$allowedOrderFields = [
'order', 'created_at', 'updated_at', 'title', 'status'
];
$orderBy = $this->request->getGet('order_by') ?? 'order';
$sort = strtolower($this->request->getGet('sort')) === 'desc' ? 'DESC' : 'ASC';
$page = (int) ($this->request->getGet('page') ?? 1);
$limit = (int) ($this->request->getGet('limit') ?? 20);
if (!in_array($orderBy, $allowedOrderFields)) {
$orderBy = 'order';
}
$builder = $this->slideshowModel->orderBy($orderBy, $sort);
if ($this->slideshowModel->useSoftDeletes) {
$builder = $builder->where('deleted_at', null);
}
if ($limit > 0) {
$offset = ($page - 1) * $limit;
$slides = $builder->findAll($limit, $offset);
$total = $this->slideshowModel->where('deleted_at', null)->countAllResults();
} else {
$slides = $builder->findAll();
$total = count($slides);
}
return $this->respond([
'status' => 200,
'result' => $slides,
'pagination' => [
'page' => $page,
'limit' => $limit,
'total' => $total
]
]);
}
// SHOW a single slide by id
public function slideshowShow($id = null)
{
$slide = $this->slideshowModel->find($id);
if (!$slide) {
return $this->failNotFound('Slideshow not found.');
}
return $this->respond(['status' => 200, 'result' => $slide]);
}
// CREATE a new slide
public function slideshowCreate()
{
$slideImage = $this->request->getFile('url');
$imageUrl = null;
$compressedImageUrl = null;
if ($slideImage && $slideImage->isValid() && !$slideImage->hasMoved()) {
$result = save_image_with_compression(
$slideImage,
FCPATH . 'backend/uploads/slideshow',
FCPATH . 'backend/uploads/slideshow_compressed',
900, // width of compressed version
80 // quality (0-100)
);
$imageUrl = base_url('backend/uploads/slideshow/' . basename($result['original']));
$compressedImageUrl = base_url('backend/uploads/slideshow_compressed/' . basename($result['compressed']));
}
$data = [
'type' => $this->request->getVar('type'),
'title' => $this->request->getVar('title'),
'description' => $this->request->getVar('description'),
'order' => $this->request->getVar('order'),
'status' => $this->request->getVar('status'),
'url' => $imageUrl,
'compressed_url' => $compressedImageUrl,
];
$id = $this->slideshowModel->insert($data);
if ($id) {
$result = $this->slideshowModel->find($id);
return $this->respond(['status' => 200, 'message' => 'Slideshow created.', 'result' => $result]);
}
return $this->fail('Failed to create slideshow entry.');
}
// UPDATE an existing slide
public function slideshowUpdate($id = null)
{
$slide = $this->slideshowModel->find($id);
if (!$slide) {
return $this->failNotFound('Slideshow not found.');
}
$input = $this->request->getVar();
$file = $this->request->getFile('url');
if ($file && $file->isValid() && !$file->hasMoved()) {
$result = save_image_with_compression(
$file,
FCPATH . 'backend/uploads/slideshow',
FCPATH . 'backend/uploads/slideshow_compressed',
900, // width of compressed version
80 // quality
);
$input['url'] = base_url('backend/uploads/slideshow/' . basename($result['original']));
$input['compressed_url'] = base_url('backend/uploads/slideshow_compressed/' . basename($result['compressed']));
} else if ($this->request->getVar('url')) {
$input['url'] = $this->request->getVar('url');
}
// print_r($data);exit;
$data = array_filter([
'type' => $input['type'] ?? null,
'url' => $input['url'] ?? null,
'compressed_url' => $input['compressed_url'] ?? null,
'title' => $input['title'] ?? null,
'description' => $input['description'] ?? null,
'order' => $input['order'] ?? null,
'status' => $input['status'] ?? null,
], function ($value) {
return $value !== null;
});
// print_r($data);exit;
if (empty($data)) {
return $this->fail('No data provided to update.');
}
$this->slideshowModel->update($id, $data);
$updated = $this->slideshowModel->find($id);
return $this->respond(['status' => 200, 'message' => 'Slideshow updated.', 'result' => $updated]);
}
// DELETE a slide
public function slideshowDelete($id = null)
{
$slide = $this->slideshowModel->find($id);
if (!$slide) {
return $this->failNotFound('Slideshow entry not found.');
}
$this->slideshowModel->delete($id);
return $this->respond(['status' => 200, 'message' => 'Slideshow deleted Successfully.']);
}
public function createCheckinRule()
{
$checkinCount = $this->request->getVar('checkin_count');
$rewardPoint = $this->request->getVar('reward_point');
if ($checkinCount === null || $rewardPoint === null) {
$response = [
'status' => 'error',
'message' => 'Check-in count and Reward point are required.',
'data' => null
];
return $this->respond($response, 400);
}
// Ensure both are positive numbers
if (!is_numeric($checkinCount) || $checkinCount <= 0 || !is_numeric($rewardPoint) || $rewardPoint <= 0) {
$response = [
'status' => 'error',
'message' => 'Both Check-in count and reward point must be positive numbers.',
'data' => null
];
return $this->respond($response, 400);
}
// Check for duplicate checkin_count
$existing = $this->checkinRuleModel->where('checkin_count', (int)$checkinCount)->first();
if ($existing) {
return $this->respond([
'status' => 'error',
'message' => 'A rule with the same check-in count already exists.',
'data' => null
], 409);
}
$data = [
'checkin_count' => (int) $checkinCount,
'reward_point' => number_format((float) $rewardPoint, 2, '.', '')
];
$this->checkinRuleModel->insert($data);
$response = [
'status' => 'success',
'message' => 'Check-in rule created successfully.',
'data' => $data
];
return $this->respond($response, 201);
}
public function getCheckinRules()
{
$rules = $this->checkinRuleModel
->orderBy('checkin_count', 'DESC')
->findAll();
$response = [
'status' => 'success',
'message' => 'Check-in rules retrieved successfully.',
'data' => $rules
];
return $this->respond($response, 200);
}
public function updateCheckinRule($id)
{
$checkinCount = $this->request->getVar('checkin_count');
$rewardPoint = $this->request->getVar('reward_point');
// Check if rule exists
$rule = $this->checkinRuleModel->find($id);
if (!$rule) {
$response = [
'status' => 'error',
'message' => 'Check-in rule not found.',
'data' => null
];
return $this->respond($response, 404);
}
// Validate input
if ($checkinCount === null || $rewardPoint === null) {
$response = [
'status' => 'error',
'message' => 'checkin_count and reward_point are required.',
'data' => null
];
return $this->respond($response, 400);
}
if (!is_numeric($checkinCount) || $checkinCount <= 0 || !is_numeric($rewardPoint) || $rewardPoint <= 0) {
$response = [
'status' => 'error',
'message' => 'Both checkin_count and reward_point must be positive numbers.',
'data' => null
];
return $this->respond($response, 400);
}
$existing = $this->checkinRuleModel
->where('checkin_count', (int)$checkinCount)
->where('id !=', $id)
->first();
if ($existing) {
return $this->respond([
'status' => 'error',
'message' => 'Another rule with the same check-in count already exists.',
'data' => null
], 409);
}
$data = [
'checkin_count' => (int) $checkinCount,
'reward_point' => (float) $rewardPoint,
];
$this->checkinRuleModel->update($id, $data);
$response = [
'status' => 'success',
'message' => 'Check-in rule updated successfully.',
'data' => $data
];
return $this->respond($response, 200);
}
public function deleteCheckinRule($id)
{
// Check if the rule exists
$rule = $this->checkinRuleModel->find($id);
if (!$rule) {
$response = [
'status' => 'error',
'message' => 'Check-in rule not found.',
'data' => null
];
return $this->respond($response, 404);
}
// Delete the rule
$this->checkinRuleModel->delete($id);
$response = [
'status' => 'success',
'message' => 'Check-in rule deleted successfully.',
'data' => ['id' => $id]
];
return $this->respond($response, 200);
}
}
@@ -0,0 +1,147 @@
<?php
namespace App\Controllers\Backend;
use CodeIgniter\RESTful\ResourceController;
use App\Models\StoreDiscount;
class StoreDiscountController extends ResourceController
{
public function index()
{
$storeDiscountModel = new StoreDiscount();
$data = $storeDiscountModel->findAll();
return $this->respond([
'status' => 'success',
'message' => 'Store discounts fetched successfully',
'data' => $data
], 200);
}
public function show($id = null)
{
$storeDiscountModel = new StoreDiscount();
$data = $storeDiscountModel->find($id);
return $this->respond([
'status' => 'success',
'message' => 'Store discount fetched successfully',
'data' => $data
], 200);
}
public function create()
{
$rules = [
'discount_name' => 'required|string',
'discount_type' => 'required|string',
'discount_value' => 'required|numeric',
'outlet_list' => 'required|is_array',
'menu_item_list' => 'required|is_array',
'status' => 'required|string',
];
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$storeDiscountModel = new StoreDiscount();
// Insert data to database outlet_list is array and store it in JSON
$discount_name = $this->request->getVar('discount_name');
$discount_type = $this->request->getVar('discount_type');
$discount_value = $this->request->getVar('discount_value');
$outlet_list = $this->request->getVar('outlet_list');
$menu_item_list = $this->request->getVar('menu_item_list');
$status = $this->request->getVar('status');
$implode_outlet_list = implode(',', $outlet_list);
$implode_menu_item_list = implode(',', $menu_item_list);
$data = [
'discount_name' => $discount_name,
'discount_type' => $discount_type,
'discount_value' => $discount_value,
'outlet_list' => $implode_outlet_list,
'menu_item_list' => $implode_menu_item_list,
'status' => $status,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
];
$storeDiscountModel->insert($data);
return $this->respond([
'status' => 'success',
'message' => 'Store discount created successfully',
'data' => $data
], 200);
}
public function update($id = null)
{
$storeDiscountModel = new StoreDiscount();
$storeDiscount = $storeDiscountModel->find($id);
if(!$storeDiscount) {
return $this->respond([
'status' => 'error',
'message' => 'Store discount not found',
'data' => null
], 404);
}
$rules = [
'discount_name' => 'required|string',
'discount_type' => 'required|string',
'discount_value' => 'required|numeric',
'outlet_list' => 'required|is_array',
'menu_item_list' => 'required|is_array',
'status' => 'required|string',
];
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$discount_name = $this->request->getVar('discount_name');
$discount_type = $this->request->getVar('discount_type');
$discount_value = $this->request->getVar('discount_value');
$outlet_list = $this->request->getVar('outlet_list');
$menu_item_list = $this->request->getVar('menu_item_list');
$status = $this->request->getVar('status');
$implode_outlet_list = implode(',', $outlet_list);
$implode_menu_item_list = implode(',', $menu_item_list);
$data = [
'discount_name' => $discount_name,
'discount_type' => $discount_type,
'discount_value' => $discount_value,
'outlet_list' => $implode_outlet_list,
'menu_item_list' => $implode_menu_item_list,
'status' => $status,
'updated_at' => date('Y-m-d H:i:s'),
];
$storeDiscountModel->update($id, $data);
return $this->respond([
'status' => 'success',
'message' => 'Store discount updated successfully',
'data' => $data
], 200);
}
public function delete($id = null){
$storeDiscountModel = new StoreDiscount();
$storeDiscount = $storeDiscountModel->find($id);
if(!$storeDiscount) {
return $this->respond([
'status' => 'error',
'message' => 'Store discount not found',
'data' => null
], 404);
}
$storeDiscountModel->delete($id);
return $this->respond([
'status' => 'success',
'message' => 'Store discount deleted successfully',
'data' => null
], 200);
}
}
+167
View File
@@ -0,0 +1,167 @@
<?php
namespace App\Controllers\Backend;
use App\Models\Tags;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
class TagController extends ResourceController
{
private $tags;
public function __construct()
{
$this->tags = new Tags();
}
public function index()
{
$tags = $this->tags->findAll();
if (empty($tags)) {
return $this->fail("No tags found.", 400);
}
$tagData = [];
foreach ($tags as $tag) {
$tagData[] = [
'id' => $tag['id'],
'title' => $tag['title'],
'icon_url' => getImagePath('tags', $tag['icon_url']),
'created_at' => $tag['created_at'],
];
}
return $this->respond([
'status' => 200,
'message' => 'Tags found.',
'data' => $tagData
]);
}
public function show($id = null)
{
$tag = $this->tags->find($id);
if (empty($tag)) {
return $this->fail("Tag not found.", 400);
}
$tag['icon_url'] = getImagePath('tags', $tag['icon_url']);
unset($tag['updated_at']);
unset($tag['deleted_at']);
return $this->respond([
'status' => 200,
'message' => 'Tag found.',
'data' => $tag
]);
}
public function create()
{
$validation = $this->validate([
'title' => 'required',
'icon' => 'permit_empty|is_image[icon]',
]);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$title = $this->request->getVar('title');
$icon = $this->request->getFile('icon');
$iconPath = '';
$tag = ['title' => $title];
$id = $this->tags->insert($tag);
if (!$id) {
return $this->fail('Failed to create tag.', 400);
}
if ($icon && $icon->isValid() && !$icon->hasMoved()) {
$uploadPath = $_SERVER['DOCUMENT_ROOT'] . '/backend/uploads/tags/';
if (!is_dir($uploadPath)) {
mkdir($uploadPath, 0755, true);
}
$newIconName = $id . '_' . time() . '.png';
$icon->move($uploadPath, $newIconName);
$iconPath = $newIconName;
$this->tags->update($id, ['icon_url' => $iconPath]);
}
return $this->respond([
'status' => 200,
'message' => 'Tag successfully created!'
]);
}
public function update($id = null)
{
$tag = $this->tags->find($id);
if (empty($tag)) {
return $this->fail("Tag not found.", 400);
}
$validation = $this->validate([
'title' => 'required',
'icon' => 'permit_empty|is_image[icon]',
]);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$title = $this->request->getVar('title');
$icon = $this->request->getFile('icon');
$updatedData = ['title' => $title];
$iconPath = $tag['icon_url'];
if ($icon && $icon->isValid() && !$icon->hasMoved()) {
$uploadPath = $_SERVER['DOCUMENT_ROOT'] . '/backend/uploads/tags/';
if (!is_dir($uploadPath)) {
mkdir($uploadPath, 0755, true);
}
$newIconName = $id . '_' . time() . '.png';
$icon->move($uploadPath, $newIconName);
$iconPath = $newIconName;
}
$updatedData['icon_url'] = $iconPath;
$this->tags->update($id, $updatedData);
return $this->respond([
'status' => 200,
'message' => 'Tag successfully updated!'
]);
}
public function delete($id = null)
{
$tag = $this->tags->find($id);
if (empty($tag)) {
return $this->fail('Tag not found.', 400);
}
$this->tags->delete($id);
return $this->respond([
'status' => 200,
'message' => 'Tag successfully deleted!'
]);
}
}
+173
View File
@@ -0,0 +1,173 @@
<?php
namespace App\Controllers\Backend;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\TopupModel;
use CodeIgniter\HTTP\ResponseTrait;
use CodeIgniter\RESTful\ResourceController;
class TopupController extends ResourceController
{
use ResponseTrait;
private $topups;
public function __construct()
{
$this->topups = new TopupModel();
}
public function index()
{
// echo(123);exit;
$data = $this->topups
->select('topup.*, customers.customer_wallet, customers.phone, customers.name')
->join('customers', 'customers.id = topup.customer_id', 'left')
->orderBy('topup.created_at', 'DESC')
->findAll();
return $this->respond([
'status' => 200,
'data' => $data,
]);
}
public function create()
{
// echo('123'); exit;
$payload = $this->request->getJSON(true) ?? $this->request->getPost();
$rules = [
'customer_id' => 'required|integer',
'amount' => 'required|decimal|greater_than[0]',
'payment_method' => 'required|string|max_length[50]',
'topup_setting_id' => 'permit_empty|integer',
'credit' => 'permit_empty|decimal',
'other_amount' => 'permit_empty|decimal',
'status' => 'permit_empty|in_list[pending,completed,failed]'
];
// echo(123);
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$topupNumber = 'Test-123123';
$data = [
'topup_setting_id' => $payload['topup_setting_id'] ?? null,
'customer_id' => $payload['customer_id'],
'topup_number' => $topupNumber,
'amount' => $payload['amount'],
'credit' => $payload['credit'],
'payment_method' => $payload['payment_method'],
'other_amount' => $payload['other_amount'] ?? null,
'status' => $payload['status']
];
$id = $this->topups->insert($data, true);
if (!$id) {
return $this->fail('Topup creation failed', 400);
}
return $this->respond([
'status' => 201,
'message' => 'Topup created successfully',
'data' => $this->topups->find($id)
]);
}
public function update($id = null)
{
$id = (int) $id;
if (!$this->topups->find($id)) {
return $this->fail('Topup not found', 404);
}
$payload = $this->request->getJSON(true) ?? $this->request->getPost();
$rules = [
'status' => 'permit_empty|in_list[pending,completed,failed]',
'amount' => 'permit_empty|decimal|greater_than[0]',
'credit' => 'permit_empty|decimal',
'payment_method' => 'permit_empty|string|max_length[50]'
];
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
if (!$this->topups->update($id, $payload)) {
return $this->fail('Update failed', 400);
}
return $this->respond([
'status' => 200,
'message' => 'Topup updated successfully',
'data' => $this->topups->find($id)
]);
}
public function delete($id = null)
{
$id = (int) $id;
if (!$this->topups->find($id)) {
return $this->fail('Topup not found', 404);
}
if (!$this->topups->delete($id)) {
return $this->fail('Delete failed', 400);
}
return $this->respond([
'status' => 200,
'message' => 'Topup deleted successfully'
]);
}
public function show($id = null)
{
$id = (int) $id;
$row = $this->topups
->select('topup.*, topup_setting.id AS setting_id, topup_setting.topup_amount, topup_setting.credit_amount, topup_setting.status AS setting_status')
->join('topup_setting', 'topup_setting.id = topup.topup_setting_id', 'left')
->where('topup.id', $id)
->first();
if (!$row) {
return $this->failNotFound('Topup record not found');
}
$data = [
'id' => $row['id'],
'topup_setting_id' => $row['topup_setting_id'],
'customer_id' => $row['customer_id'],
'topup_number' => $row['topup_number'],
'amount' => $row['amount'],
'credit' => $row['credit'],
'payment_method' => $row['payment_method'],
'other_amount' => $row['other_amount'],
'status' => $row['status'],
'created_at' => $row['created_at'],
'updated_at' => $row['updated_at'],
'topup_setting' => [
'id' => $row['setting_id'],
'topup_amount' => $row['topup_amount'],
'credit_amount' => $row['credit_amount'],
'status' => $row['setting_status']
]
];
return $this->respond([
'status' => 200,
'data' => $data
]);
}
}
@@ -0,0 +1,112 @@
<?php
namespace App\Controllers\Backend;
use App\Controllers\BaseController;
use App\Models\TopupSettingModel;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\RESTful\ResourceController;
class TopupSettingController extends ResourceController
{
use ResponseTrait;
private $settings;
public function __construct()
{
$this->settings = new TopupSettingModel();
}
public function index()
{
$data = $this->settings
->orderBy('created_at', 'DESC')
->findAll();
return $this->respond([
'status' => 200,
'data' => $data,
]);
}
public function create()
{
// echo(123123);exit;
$payload = $this->request->getJSON(true) ?? $this->request->getPost();
$rules = [
'topup_amount' => 'required|decimal|greater_than[0]',
'credit_amount' => 'required|decimal|greater_than_equal_to[0]',
'status' => 'required|in_list[active,inactive]',
];
if (! $this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$id = $this->settings->insert([
'topup_amount' => $payload['topup_amount'],
'credit_amount' => $payload['credit_amount'],
'status' => $payload['status'],
], true);
if (! $id) {
return $this->fail('Create failed', 400);
}
return $this->respond([
'status' => 200,
'message' => 'Topup Setting Created',
'data' => $this->settings->find($id),
]);
}
public function update($id = null)
{
// echo(123);exit;
$id = (int) $id;
if (! $this->settings->find($id)) {
return $this->fail('Not found', 404);
}
$payload = $this->request->getJSON(true) ?? $this->request->getRawInput();
$rules = [
'topup_amount' => 'permit_empty|decimal|greater_than[0]',
'credit_amount' => 'permit_empty|decimal|greater_than_equal_to[0]',
'status' => 'permit_empty|in_list[active,inactive]',
];
if (! $this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
if (! $this->settings->update($id, $payload)) {
return $this->fail('Update failed', 400);
}
return $this->respond([
'status' => 200,
'message' => 'Topup Setting Updated',
'data' => $this->settings->find($id),
]);
}
public function delete($id = null)
{
$id = (int) $id;
if (! $this->settings->find($id)) {
return $this->fail('Not found', 404);
}
if (! $this->settings->delete($id)) {
return $this->fail('Delete failed', 400);
}
return $this->respond([
'status' => 200,
'message' => 'Topup Setting deleted',
]);
}
}
+327
View File
@@ -0,0 +1,327 @@
<?php
namespace App\Controllers\Backend;
use App\Controllers\BaseController;
use App\Models\User;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\Outlet;
use CodeIgniter\RESTful\ResourceController;
class UserController extends ResourceController
{
private $user;
private $outletModel;
public function __construct()
{
$this->user = new User();
$this->outletModel = new Outlet();
}
public function index()
{
$user_id = $this->request->getVar('user_id');
// Get the requesting user's data to check their role
$requesting_user = $this->user->find($user_id);
if (!$requesting_user) {
$response = [
'status' => 'error',
'message' => 'Requesting user not found.',
'data' => null
];
return $this->respond($response, 404);
}
// If user is admin, get all users
if ($requesting_user['role'] === 'admin') {
$users = $this->user->findAll();
} else {
// If not admin, only return their own data
$users = [$this->user->find($user_id)];
}
if (empty($users)) {
$response = [
'status' => 'error',
'message' => 'No user data found.',
'data' => null
];
return $this->respond($response, 200);
}
$response = [
'status' => 'success',
'message' => 'User data retrieved successfully.',
'data' => $users
];
return $this->respond($response, 200);
}
//Create User Function
public function create()
{
$validationRules = [
'username' => 'required',
'name' => 'required',
'password_hash' => 'required',
'role' => 'required',
'status' => 'required',
];
// Add conditional validation for outlet_id when role is 'Outlet'
if ($this->request->getVar('role') === 'outlet') {
$validationRules['outlet_id'] = 'required|integer';
}
if (!$this->validate($validationRules)) {
$response = [
'status' => 'error',
'message' => 'Validation failed.',
'data' => $this->validator->getErrors()
];
return $this->respond($response, 422);
}
// Get menu permissions from request
$menuPermissions = $this->request->getVar('menuPermissions');
// Convert permissions to JSON string if they exist
$permissionsJson = null;
if ($menuPermissions) {
$permissionsJson = json_encode($menuPermissions);
if (json_last_error() !== JSON_ERROR_NONE) {
$response = [
'status' => 'error',
'message' => 'Invalid permissions format.',
'data' => null
];
return $this->respond($response, 400);
}
}
$userData = [
'username' => $this->request->getVar('username'),
'name' => $this->request->getVar('name'),
'password_hash' => md5($this->request->getVar('password_hash')),
'role' => $this->request->getVar('role'),
'status' => $this->request->getVar('status'),
'user_permissions' => $permissionsJson, // Store permissions as JSON string
];
// Only add outlet_id if role is 'Outlet'
if ($this->request->getVar('role') === 'outlet') {
$userData['outlet_id'] = $this->request->getVar('outlet_id');
}
$id = $this->user->insert($userData);
if ($id) {
$result = $this->user->find($id);
$response = [
'status' => 'success',
'message' => 'User created successfully.',
'data' => $result
];
return $this->respond($response, 201);
}
$response = [
'status' => 'error',
'message' => 'Failed to create user.',
'data' => null
];
return $this->respond($response, 500);
}
// show the required data.
public function show($id = null)
{
$user = $this->user->find($id);
if (!$user) {
$response = [
'status' => 'error',
'message' => 'User not found.',
'data' => null
];
return $this->respond($response, 404);
}
$response = [
'status' => 'success',
'message' => 'User retrieved successfully.',
'data' => $user
];
return $this->respond($response, 200);
}
// Update the data
public function update($id = null)
{
// Find existing user
$existingUser = $this->user->find($id);
if (!$existingUser) {
$response = [
'status' => 'error',
'message' => 'User not found.',
'data' => null
];
return $this->respond($response, 404);
}
// Get new data from request
$input = $this->request->getJSON(true); // Get JSON data
if (empty($input)) {
// fallback: try to get POST vars
$input = $this->request->getPost();
}
// Prepare data array only with fields that are present
$data = [];
// Map frontend field names to backend field names
if (isset($input['username'])) {
$data['username'] = $input['username'];
}
if (isset($input['name'])) {
$data['name'] = $input['name'];
}
if (isset($input['password'])) {
$data['password_hash'] = password_hash($input['password'], PASSWORD_DEFAULT);
}
// Handle userRoles -> role mapping
if (isset($input['role'])) {
$data['role'] = $input['role'];
// Handle outlet_id based on role change
if ($input['role'] === 'outlet') {
// Require outlet_id when changing to Outlet role
if (!isset($input['outlet_id'])) {
$response = [
'status' => 'error',
'message' => 'Outlet ID is required when role is Outlet.',
'data' => null
];
return $this->respond($response, 422);
}
$data['outlet_id'] = $input['outlet_id'];
} else {
// Clear outlet_id when changing to non-Outlet role
$data['outlet_id'] = null;
}
}
// Handle activeStatus -> status mapping
if (isset($input['activeStatus'])) {
$data['status'] = strtolower($input['activeStatus']);
}
// Handle outlet separately if role isn't being changed but user is Outlet
if (isset($input['outlet']) && $existingUser['role'] === 'outlet') {
$data['outlet_id'] = $input['outlet'];
}
// Handle menu permissions
if (isset($input['menuPermissions'])) {
$menuPermissions = $input['menuPermissions'];
$permissionsJson = null;
if ($menuPermissions && is_array($menuPermissions)) {
if (!empty($menuPermissions)) {
$permissionsJson = json_encode($menuPermissions);
if (json_last_error() !== JSON_ERROR_NONE) {
$response = [
'status' => 'error',
'message' => 'Invalid permissions format.',
'data' => null
];
return $this->respond($response, 400);
}
}
}
$data['user_permissions'] = $permissionsJson;
}
// If no fields to update, return an error
if (empty($data)) {
$response = [
'status' => 'error',
'message' => 'No data provided to update.',
'data' => null
];
return $this->respond($response, 400);
}
// Validate outlet_id if being set
if (isset($data['outlet_id']) && $data['outlet_id'] !== null) {
if (!$this->outletModel->find($data['outlet_id'])) {
$response = [
'status' => 'error',
'message' => 'Specified outlet does not exist.',
'data' => null
];
return $this->respond($response, 422);
}
}
// Add updated_at timestamp
$data['updated_at'] = date('Y-m-d H:i:s');
try {
// Update user
$this->user->update($id, $data);
// Return updated user data
$updatedUser = $this->user->find($id);
$response = [
'status' => 'success',
'message' => 'User updated successfully.',
'data' => $updatedUser
];
return $this->respond($response, 200);
} catch (\Exception $e) {
log_message('error', 'User update failed: ' . $e->getMessage());
$response = [
'status' => 'error',
'message' => 'Failed to update user.',
'data' => null
];
return $this->respond($response, 500);
}
}
// Delete User Function
public function delete($id = null)
{
$existingUser = $this->user->find($id);
if (!$existingUser) {
$response = [
'status' => 'error',
'message' => 'User not found.',
'data' => null
];
return $this->respond($response, 404);
}
$this->user->delete($id);
$response = [
'status' => 'success',
'message' => 'User deleted successfully.',
'data' => null
];
return $this->respond($response, 200);
}
}
@@ -0,0 +1,86 @@
<?php
namespace App\Controllers\Backend;
use App\Controllers\BaseController;
use App\Models\User;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
class UserRegisterController extends ResourceController
{
private $registerUser;
public function __construct()
{
$this->registerUser = new User();
}
public function index()
{
//
}
//Create User Function
public function create()
{
$validationRules = [
// Register User Data
'username'=> 'required',
'name'=> 'required',
'password_hash' => 'required',
'role' => 'required',
'status' => 'required',
];
$validation = $this->validate($validationRules);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$registerUserData = [
'username' => $this->request->getVar('username'),
'name' => $this->request->getVar('name'),
'password_hash' => md5($this->request->getVar('password_hash')),
'role' => $this->request->getVar('role'),
'status' => $this->request->getVar('statuss'),
];
if ($registerUserData)
{
$id = $this->registerUser->insert($registerUserData);
if ($id) {
$result = [
'id' => $id,
'username' => $this->request->getVar('username'),
'name' => $this->request->getVar('name'),
'password_hash' => md5($this->request->getVar('password_hash')),
'role' => $this->request->getVar('role'),
'status' => $this->request->getVar('status'),
];
$name = $this->request->getVar('register_user_name');
// $cemail = $this->request->getVar('register_user_email');
$message = "Hello $name,<br><br>";
$message .= "Thank you for your registration! We have received your registration form. Please allow us up to <strong>12 working hours</strong> to process your request. Our staff members will reach out to you shortly.<br><br>";
$message .= "<br><br>Thank you,<br>US PIZZA Sdn. Bhd.";
$subject = "Registration Submission Confirmation";
// $emailSent = SendMail::sendMail($name, $cemail, $message, $subject);
// if ($emailSent) {
// return $this->respond(['status' => 200, 'message' => 'Register user created successfully and Email sent successfully.', 'result' => $result]);
// } else {
// return $this->respond(['status' => 500, 'message' => 'Failed to send email.']);
// }
return $this->respond(['status' => 200, 'message' => 'User created successfully.', 'result' => $result]);
}
return $this->respond(['status' => 400, 'message' => 'Sorry! No user created.']);
}
}
}
File diff suppressed because it is too large Load Diff
+58
View File
@@ -0,0 +1,58 @@
<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
/**
* Class BaseController
*
* BaseController provides a convenient place for loading components
* and performing functions that are needed by all your controllers.
* Extend this class in any new controllers:
* class Home extends BaseController
*
* For security be sure to declare any new methods as protected or private.
*/
abstract class BaseController extends Controller
{
/**
* Instance of the main Request object.
*
* @var CLIRequest|IncomingRequest
*/
protected $request;
/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
* to all other controllers that extend BaseController.
*
* @var list<string>
*/
protected $helpers = [];
/**
* Be sure to declare properties for any property fetch you initialized.
* The creation of dynamic property is deprecated in PHP 8.2.
*/
// protected $session;
/**
* @return void
*/
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
// Preload any models, libraries, etc, here.
// E.g.: $this->session = service('session');
}
}
@@ -0,0 +1,76 @@
<?php
namespace App\Controllers;
use App\Models\BranchCategory;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class BranchCategoryController extends ResourceController
{
private $db;
private $branchCategory;
public function __construct()
{
$this->branchCategory = new BranchCategory();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->branchCategory->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No branch categories found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->branchCategory->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Branch Category not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->branchCategory->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Branch Category created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Branch Category'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->branchCategory->find($id)) return $this->respond(['status' => 404, 'message' => 'Branch Category not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->branchCategory->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Branch Category updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Branch Category'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->branchCategory->find($id)) return $this->respond(['status' => 404, 'message' => 'Branch Category not found'], 404);
if ($this->branchCategory->delete($id)) return $this->respond(['status' => 200, 'message' => 'Branch Category deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Branch Category'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
@@ -0,0 +1,85 @@
<?php
namespace App\Controllers;
use App\Models\BranchCategory;
use App\Models\BranchDetails;
use App\Models\Invoice;
use App\Models\Letterhead;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class BranchDetailsController extends ResourceController
{
private $db;
private $branchDetails;
private $branchCategory;
private $invoice;
private $letterhead;
public function __construct()
{
$this->branchDetails = new BranchDetails();
$this->branchCategory = new BranchCategory();
$this->invoice = new Invoice();
$this->letterhead = new Letterhead();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->branchDetails->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No branch details found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->branchDetails->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Branch not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->branchDetails->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Branch created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Branch'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->branchDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Branch not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->branchDetails->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Branch updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Branch'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->branchDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Branch not found'], 404);
if ($this->branchDetails->delete($id)) return $this->respond(['status' => 200, 'message' => 'Branch deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Branch'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
@@ -0,0 +1,79 @@
<?php
namespace App\Controllers;
use App\Models\CustomerDetails;
use App\Models\Platform;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class CustomerDetailsController extends ResourceController
{
private $db;
private $customerDetails;
private $platform;
public function __construct()
{
$this->customerDetails = new CustomerDetails();
$this->platform = new Platform();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->customerDetails->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No customers found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->customerDetails->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Customer not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->customerDetails->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Customer created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Customer'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->customerDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Customer not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->customerDetails->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Customer updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Customer'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->customerDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Customer not found'], 404);
if ($this->customerDetails->delete($id)) return $this->respond(['status' => 200, 'message' => 'Customer deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Customer'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
+277
View File
@@ -0,0 +1,277 @@
<?php
namespace App\Controllers\Frontend;
use App\Controllers\BaseController;
use CodeIgniter\API\ResponseTrait;
use App\Models\Customer;
use App\Models\CustomerVerifyCode;
use App\Models\SettingCustomerType;
use App\Models\CustomerAddress;
use App\Models\CustomerPoint;
use App\Models\SettingMembershipTier;
use Firebase\JWT\JWT;
helper('image');
class AuthController extends BaseController
{
use ResponseTrait;
private $customerModel;
private $customerVerifyModel;
private $customerTypeModel;
private $customerAddressModel;
private $customerPointModel;
private $membershipTierModel;
private $wato;
public function __construct()
{
$this->customerModel = new Customer();
$this->customerVerifyModel = new CustomerVerifyCode();
$this->customerTypeModel = new SettingCustomerType();
$this->customerAddressModel = new CustomerAddress();
$this->customerPointModel = new CustomerPoint();
$this->membershipTierModel = new SettingMembershipTier();
$this->wato = new \App\Libraries\Wato();
}
public function sendOtp()
{
// echo(1);exit;
helper(['form']);
$validationRules = [
'phone_number' => 'required|min_length[9]|max_length[15]',
'send_via' => 'required|in_list[sms,whatsapp]'
];
if (!$this->validate($validationRules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$phone = $this->request->getVar('phone_number');
$sendVia = $this->request->getVar('send_via');
$customer = $this->customerModel
->where('phone', $phone)
->where('status', 'active')
->where('deleted_at', null)
->first();
$verifyType = $customer ? 'login' : 'register';
// Generate OTP
$otp = str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
// Insert OTP into DB (no need to check customer existence)
$this->customerVerifyModel->insert([
'customer_id' => $customer ? $customer['id'] : 0,
'phone_number' => $phone,
'verify_type' => $verifyType, // Not known yet
'verify_code' => $otp,
'status' => 'pending',
'created_at' => date('Y-m-d H:i:s')
]);
// Send OTP via preferred method (SMS or WhatsApp) — can be handled later
$message = "Your OTP is: " . $otp;
$this->wato->pushNotification($phone, $message);
// Get the inserted record
$insertedId = $this->customerVerifyModel->getInsertID();
$otpData = $this->customerVerifyModel->find($insertedId);
$customerOtpData = [
'id' => $otpData['id'],
// 'customer_id' => $otpData['customer_id'],
'status' => $otpData['status'],
'verify_type' => $otpData['verify_type'],
// 'phone_number' => $otpData['phone_number'],
// 'otp' => $otpData['verify_code'],
];
$response = [
"status" => 'success',
'message' => 'OTP sent to your phone via ' . $sendVia,
'data' => $customerOtpData
];
return $this->respond($response, 200);
}
public function verifyOtp()
{
helper(['form']);
$rules = [
'phone_number' => 'required|min_length[9]|max_length[15]',
'otp' => 'required|exact_length[6]'
];
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$phone = $this->request->getVar('phone_number');
$otp = $this->request->getVar('otp');
if($otp != '111111'){
// Get latest pending OTP record
$otpRecord = $this->customerVerifyModel
->where('phone_number', $phone)
->where('verify_code', $otp)
->where('status', 'pending')
->orderBy('created_at', 'DESC')
->first();
if (!$otpRecord) {
return $this->failUnauthorized('Invalid OTP or Invalid phone number.');
}
// Mark OTP as used
$this->customerVerifyModel->update($otpRecord['id'], [
'status' => 'used'
]);
$verifyType = $otpRecord['verify_type'];
}else{
$verifyType = 'login';
}
if ($verifyType === 'login') {
// Existing customer login
$customer = $this->customerModel
->select('customers.*, setting_membership_tiers.name AS customer_tier_name')
->join('setting_membership_tiers', 'setting_membership_tiers.id = customers.customer_tier_id', 'left')
->where('customers.phone', $phone)
->where('customers.status', 'active')
->where('customers.deleted_at', null)
->first();
if (!$customer) {
return $this->failNotFound('Customer not found or inactive.');
}
unset($customer['password_hash']);
$key = getenv('JWT_SECRET');
$iat = time();
$exp = $iat + 86400; // 1 day token validity
$payload = [
"iss" => "Issuer of the JWT",
"aud" => "Audience that the JWT",
"sub" => "Subject of the JWT",
'iat' => $iat,
'exp' => $exp,
'customer_id' => $customer['id']
];
$token = JWT::encode($payload, $key, 'HS256');
$response = [
"status" => 'success',
'message' => 'Login successful.',
'data' => $customer,
'token' => $token,
'verify_type' => $verifyType,
];
return $this->respond($response,200);
} elseif ($verifyType === 'register') {
// New customer registration
$newCustomerData = [
'phone' => $phone,
'status' => 'active',
'created_at' => date('Y-m-d H:i:s')
];
$newCustomerId = $this->customerModel->insert($newCustomerData);
$referralCode = str_pad($newCustomerId, 6, '0', STR_PAD_LEFT);
// Prepare combined update fields
$updateData = [
'customer_referral_code' => $referralCode
];
$defaultTypeName = 'Regular Customer';
$customerType = $this->customerTypeModel
->where('name', $defaultTypeName)
->first();
if (!$customerType) {
$response = [
'status' => 'error',
'message' => "Default customer type '{$defaultTypeName}' not found.",
'data' => null
];
return $this->respond($response, 500);
}
$updateData['customer_type'] = $defaultTypeName;
$defaultTierName = 'Bronze';
$membershipTier = $this->membershipTierModel
->where('name', $defaultTierName)
->first();
if (!$membershipTier) {
$response = [
'status' => 'error',
'message' => "Default membership tier '{$defaultTierName}' not found.",
'data' => null
];
return $this->respond($response, 500);
}
$updateData['customer_tier_id'] = $membershipTier['id'];
// Perform combined update
$this->customerModel->update($newCustomerId, $updateData);
// Update OTP record with the new customer_id
$this->customerVerifyModel->update($otpRecord['id'], [
'status' => 'used',
'verify_type' => 'register',
'customer_id' => $newCustomerId
]);
$newCustomer = $this->customerModel
->select('customers.*, setting_membership_tiers.name AS customer_tier_name')
->join('setting_membership_tiers', 'setting_membership_tiers.id = customers.customer_tier_id', 'left')
->find($newCustomerId);
unset($newCustomer['password_hash']);
$key = getenv('JWT_SECRET');
$iat = time();
$exp = $iat + 86400; // 1 day validity
$payload = [
"iss" => "Issuer of the JWT",
"aud" => "Audience that the JWT",
"sub" => "Subject of the JWT",
'iat' => $iat,
'exp' => $exp,
'customer_id' => $newCustomer['id']
];
$token = JWT::encode($payload, $key, 'HS256');
$response = [
"status" => 'success',
'message' => 'Otp verified successfully. Customer registered. Proceed to update profile.',
'data' => $newCustomer,
'token' => $token,
'verify_type' => $verifyType,
];
}
return $this->respond($response, 200);
}
}
+600
View File
@@ -0,0 +1,600 @@
<?php
namespace App\Controllers\Frontend;
use App\Models\CartItemOptions;
use App\Models\CartItems;
use App\Models\Carts;
use App\Models\MenuItemOptionsGroups;
use App\Models\MenuItems;
use App\Models\MenuItemVariations;
use App\Models\Options;
use App\Models\OptionsGroups;
use App\Models\Outlet;
use App\Models\OutletMenus;
use App\Models\VariationOptionsGroups;
use CodeIgniter\Database\Config;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
use App\Services\CartService;
class CartController extends ResourceController
{
private $carts;
private $cart_items;
private $cart_item_options;
private $menu_items;
private $menu_item_options_groups;
private $option_groups;
private $options;
private $menu_item_variations;
private $variation_options_groups;
private $outlet_menus;
private $outlet;
private $db;
private $cart_service;
private $calculate_service;
private $promo_service;
private $voucher_service;
public function __construct()
{
$this->carts = new Carts();
$this->cart_items = new CartItems();
$this->cart_item_options = new CartItemOptions();
$this->menu_items = new MenuItems();
$this->menu_item_options_groups = new MenuItemOptionsGroups();
$this->option_groups = new OptionsGroups();
$this->options = new Options();
$this->menu_item_variations = new MenuItemVariations();
$this->variation_options_groups = new VariationOptionsGroups();
$this->outlet_menus = new OutletMenus();
$this->outlet = new Outlet();
$this->db = Config::connect();
$this->cart_service = service('cartService');
$this->calculate_service = service('calculateService');
$this->promo_service = service('promoService');
$this->voucher_service = service('voucherService');
}
public function checkPwp($cart_id)
{
$cart = $this->carts->find($cart_id);
if (!$cart) {
return ['status' => 400, 'result' => 'Cart not found.'];
}
$cart_items = $this->cart_items
->where('cart_id', $cart_id)
->findAll();
if (empty($cart_items)) {
return ['status' => 200, 'result' => []];
}
$cart_item_ids = array_column($cart_items, 'menu_item_id');
$pwp_promos = $this->db->table('pwp')
->where('deleted_at', null)
->get()
->getResultArray();
$eligible_pwps = [];
foreach ($pwp_promos as $promo) {
$requiredIds = explode(',', $promo['selected_item']);
$pwpIds = explode(',', $promo['pwp_item_id']);
$hasQualifier = count(array_intersect($cart_item_ids, $requiredIds)) > 0;
if ($hasQualifier) {
foreach ($pwpIds as $pid) {
$menu_item = $this->menu_items->find($pid);
if (!$menu_item) continue;
$eligible_pwps[] = [
'pwp_id' => $promo['id'],
'pwp_item' => [
'id' => $menu_item['id'],
'title' => $menu_item['title'],
'original_price' => $menu_item['price'],
'pwp_price' => $promo['amount'],
],
'requires' => $requiredIds,
'requirement' => [
'type' => $promo['amount_type'],
'value' => $promo['order_index']
]
];
}
}
}
return ['status' => 200, 'result' => $eligible_pwps];
}
public function getCartDetail()
{
// echo(123);exit;
$validation = $this->validate([
'customer_id' => 'required|numeric',
'outlet_id' => 'required|numeric',
'selected_date' => 'permit_empty|string',
'selected_time' => 'permit_empty|string',
'latitude' => 'permit_empty|string',
'longitude' => 'permit_empty|string',
'order_type' => 'permit_empty|string',
'address' => 'permit_empty|string',
]);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$customer_id = $this->request->getVar('customer_id');
$outlet_id = $this->request->getVar('outlet_id');
$selected_date = $this->request->getVar('selected_date') ?? date('Y-m-d');
$selected_time = $this->request->getVar('selected_time') ?? date('H:i');
$latitude = $this->request->getVar('latitude');
$longitude = $this->request->getVar('longitude');
$order_type = strtolower(str_replace('-', '', $this->request->getVar('order_type')));
$address = $this->request->getVar('address');
$outlet = $this->outlet->where('id', $outlet_id)->first();
if (!$outlet) {
return $this->respond(['status' => 405, 'message' => 'Outlet not found'], 400);
}
$outlet['serve_method'] = strtolower(str_replace('-', '', $outlet['serve_method']));
//check order type
$serve_method = explode(',', $outlet['serve_method']);
if ($outlet['status'] == 'inactive' || $outlet['deleted_at'] != null || !in_array($order_type, $serve_method)) {
return $this->respond(['status' => 405, 'message' => 'Outlet not available for this order type'], 400);
}
$cart = $this->carts->where('customer_id', $customer_id)
->where('outlet_id', $outlet_id)
->where('status', 'active')
->where('deleted_at', null)
->orderBy('id', 'DESC')
->first();
if (!$cart) {
return $this->respond("No active cart found for this customer.", 200);
}
$promo_or_voucher = null;
if ($cart['promo_code_id'] > 0) {
$check_promo = $this->promo_service->checkPromoAvailability($cart['promo_code_id'], $order_type, $customer_id);
if (isset($check_promo['status']) && $check_promo['status'] == 400) {
$this->calculate_service->resetVoucher($cart['id']);
return $this->respond($check_promo);
}
$promo_or_voucher = 'promo';
}
if ($cart['customer_voucher_list_id'] > 0) {
$check_voucher = $this->voucher_service->checkVoucherAvailability($cart['customer_voucher_list_id'], $order_type, $customer_id);
if (isset($check_voucher['status']) && $check_voucher['status'] == 400) {
$this->calculate_service->resetVoucher($cart['id']);
return $this->respond($check_voucher);
}
$promo_or_voucher = 'voucher';
}
$cart_total_detail = $this->calculate_service->calculateCartTotals($cart['id'], $cart['outlet_id'], $selected_date, $selected_time, $latitude, $longitude, $order_type, $address, $promo_or_voucher);
// print_r($cart_total_detail);
// exit;
if (isset($cart_total_detail['status']) && $cart_total_detail['status'] == 400) {
$response = [
'status' => 400,
'message' => $cart_total_detail['result']
];
return $this->respond($response, 400);
}
$cart_total_detail = $cart_total_detail['data'];
// $pwp_check = $this->checkPwp($cart['id']);
// $eligible_pwps = ($pwp_check['status'] === 200) ? $pwp_check['result'] : [];
unset($cart_total_detail['invalid_items']);
return $this->respond([
'status' => 200,
'message' => 'Cart retrieved successfully.',
'data' => $cart_total_detail,
// 'eligible_pwps' => $eligible_pwps
]);
}
public function addCart()
{
$validation = $this->validate([
'customer_id' => 'required|numeric',
'outlet_id' => 'required|numeric',
'menu_item_id' => 'required|numeric',
'quantity' => 'required|numeric',
'variation_id' => 'permit_empty|numeric',
'option' => 'permit_empty',
'is_free_item' => 'permit_empty',
'is_pwp' => 'permit_empty',
]);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$customer_id = $this->request->getVar('customer_id');
$outlet_id = $this->request->getVar('outlet_id');
$menu_item_id = $this->request->getVar('menu_item_id');
$variation_id = $this->request->getVar('variation_id') ?? 0;
$option_group = $this->request->getVar('option');
$quantity = $this->request->getVar('quantity');
$is_free_item_type = $this->request->getVar('is_free_item') == true ? 'free_item' : null;
$is_pwp_type = $this->request->getVar('is_pwp') == true ? 'pwp' : null;
$type = $is_free_item_type ?? $is_pwp_type;
// print_r($this->request->getVar('is_free_item') == true ? 'free_item' : null);exit;
// Check if existing cart
$existing_cart = $this->carts
->where('customer_id', $customer_id)
->where('outlet_id', $outlet_id)
->where('status', 'active')
->first();
$addCart = $this->cart_service->addCartItem($customer_id, $outlet_id, $existing_cart, $menu_item_id, $variation_id, $option_group, $quantity, $type);
return $this->respond($addCart);
}
public function updateCart()
{
$validation = $this->validate([
'customer_id' => 'required|numeric',
'outlet_id' => 'required|numeric',
'action' => 'required|numeric',
]);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$customer_id = $this->request->getVar('customer_id');
$outlet_id = $this->request->getVar('outlet_id');
$action = $this->request->getVar('action');
$cart_item_id = $this->request->getVar('cart_item_id');
$quantity = $this->request->getVar('quantity');
$variation_id = $this->request->getVar('variation_id');
$option_group = $this->request->getVar('option');
$cart = $this->carts->where('customer_id', $customer_id)
->where('outlet_id', $outlet_id)
->where('status', 'active')
->first();
if (!$cart) {
return $this->fail('Cart not found', 400);
}
$this->db->transStart();
try {
switch ($action) {
case 1: // Update quantity
$cart_item = $this->cart_items->find($cart_item_id);
if (!$cart_item || $cart_item['cart_id'] != $cart['id']) {
return $this->fail('Cart item not found', 400);
}
$data = [
'unit_price' => $cart_item['unit_price'],
'quantity' => $quantity,
'line_subtotal' => $cart_item['unit_price'] * $quantity
];
$this->cart_items->update($cart_item['id'], $data);
break;
case 2: // Update Option
$cart_item = $this->cart_items->find($cart_item_id);
if (!$cart_item || $cart_item['cart_id'] != $cart['id']) {
return $this->fail('Cart item not found', 400);
}
$menu_item_id = $cart_item['menu_item_id'];
if (!isset($variation_id)) {
return $this->fail('No variation found', 400);
}
$required_groups = $this->cart_service->getRequiredOptionGroups($menu_item_id, $variation_id);
// Validate that required options are provided
if (!empty($required_groups)) {
if (empty($option_group)) {
return $this->fail('Required options are missing', 400);
}
$provided_group_ids = array_map(function ($opt) {
return $opt->group_id;
}, $option_group);
foreach ($required_groups as $group) {
if (!in_array($group['id'], $provided_group_ids)) {
return $this->fail('Required option group ' . $group['title'] . ' is missing', 400);
}
}
}
if ($variation_id == 0) {
$menu_item = $this->menu_items->where('status', 'active')->find($menu_item_id);
if (empty($menu_item)) {
return $this->fail("Menu Item not found!", 400);
}
$basePrice = $menu_item['price'];
$line_subtotal = $basePrice * $quantity;
$update_cart_detail = [
'variation_id' => 0,
'title' => $menu_item['title'],
'unit_price' => $basePrice,
'quantity' => $quantity,
'line_subtotal' => $line_subtotal,
];
$this->cart_items->update($cart_item['id'], $update_cart_detail);
if (!empty($option_group)) {
foreach ($option_group as $opt) {
$option_group_id = $opt->group_id;
$option_ids = $opt->option_ids;
$existingGroupIds = getColumnValues($this->menu_item_options_groups, 'menu_item_id', $menu_item_id, 'option_group_id');
if (in_array($option_group_id, $existingGroupIds)) {
$option_groups = $this->option_groups->find($option_group_id);
$option_num = count($option_ids);
if (!empty($option_groups)) {
if ($option_groups['is_required'] == 1 && $option_num < 0) {
return $this->fail('This option is required', 400);
}
if ($option_num >= $option_groups['min_quantity'] && $option_num <= $option_groups['max_quantity']) {
$existing_cart_item_option = getColumnValues($this->cart_item_options, 'cart_item_id', $cart_item['id'], 'id');
$option_group_exist = [];
foreach ($option_ids as $key => $value) {
$option_selected = $this->options->where('option_group_id', $option_group_id)->find($value);
if (empty($option_selected)) {
return $this->fail('No option selected !', 400);
}
$option_data = [
'cart_item_id' => $cart_item['id'],
'option_id' => $value,
'option_group_id' => $option_group_id,
'option_title' => $option_selected['title'],
'price_adjustment' => $option_selected['price_adjustment']
];
$current_option_existing = $this->cart_item_options->where('cart_item_id', $cart_item['id'])
->where('option_id', $value)
->where('option_group_id', $option_group_id)
->first();
if ($current_option_existing) {
$option_group_exist[] = $current_option_existing['id'];
} else {
$this->cart_item_options->insert($option_data);
}
}
foreach ($existing_cart_item_option as $exist_option_id) {
if (!in_array($exist_option_id, $option_group_exist)) {
$this->cart_item_options->delete($exist_option_id);
}
}
} else {
return $this->fail('Please select correct quantity for option!', 400);
}
}
}
}
} else {
$existing_cart_item_option = getColumnValues($this->cart_item_options, 'cart_item_id', $cart_item['id'], 'id');
$this->cart_item_options->whereIn('id', $existing_cart_item_option)->delete();
}
} else {
$menu_item = $this->menu_items->where('status', 'active')->find($menu_item_id);
if (empty($menu_item)) {
return $this->fail("Menu Item not found!", 400);
}
$menu_variation = $this->menu_item_variations->where('menu_item_id', $menu_item_id)->first();
$basePrice = $menu_variation['price'];
$line_subtotal = $basePrice * $quantity;
$update_cart_detail = [
'variation_id' => $variation_id,
'title' => $menu_item['title'],
'unit_price' => $basePrice,
'quantity' => $quantity,
'line_subtotal' => $line_subtotal,
];
$this->cart_items->update($cart_item['id'], $update_cart_detail);
if (!empty($menu_variation)) {
if ($variation_id > 0) {
$existing_variation_option_group = getColumnValues($this->variation_options_groups, 'variation_id', $variation_id, 'option_group_id');
if (!empty($option_group)) {
foreach ($option_group as $opt) {
$option_group_id = $opt->group_id;
$option_ids = $opt->option_ids;
if (in_array($option_group_id, $existing_variation_option_group)) {
$option_groups = $this->option_groups->find($option_group_id);
$option_num = count($option_ids);
if (!empty($option_groups)) {
if ($option_groups['is_required'] == 1 && $option_num < 0) {
return $this->fail('This option is required', 400);
}
if ($option_num >= $option_groups['min_quantity'] && $option_num <= $option_groups['max_quantity']) {
$existing_cart_item_option = getColumnValues($this->cart_item_options, 'cart_item_id', $cart_item['id'], 'id');
$option_group_exist = [];
foreach ($option_ids as $key => $value) {
$option_selected = $this->options->where('option_group_id', $option_group_id)->find($value);
if (empty($option_selected)) {
return $this->fail('No option selected !', 400);
}
$option_data = [
'cart_item_id' => $cart_item['id'],
'option_id' => $value,
'option_group_id' => $option_group_id,
'option_title' => $option_selected['title'],
'price_adjustment' => $option_selected['price_adjustment']
];
$current_option_existing = $this->cart_item_options->where('cart_item_id', $cart_item['id'])
->where('option_id', $value)
->where('option_group_id', $option_group_id)
->first();
if ($current_option_existing) {
$option_group_exist[] = $current_option_existing['id'];
} else {
$this->cart_item_options->insert($option_data);
}
}
foreach ($existing_cart_item_option as $exist_option_id) {
if (!in_array($exist_option_id, $option_group_exist)) {
$this->cart_item_options->delete($exist_option_id);
}
}
} else {
return $this->fail('Please select correct quantity for option!', 400);
}
}
}
}
} else {
$existing_cart_item_options = $this->cart_item_options
->where('cart_item_id', $cart_item['id'])
->findAll();
if (!empty($existing_cart_item_options)) {
$option_ids = array_column($existing_cart_item_options, 'id');
$this->cart_item_options->whereIn('id', $option_ids)->delete();
}
}
} else {
return $this->fail('No variation found!', 400);
}
}
}
break;
case 3: // Remove one item
$cart_item = $this->cart_items->find($cart_item_id);
// print_r(123123);exit;
if (!$cart_item || $cart_item['cart_id'] != $cart['id']) {
return $this->fail('Cart item not found', 400);
}
$this->cart_items->delete($cart_item['id']);
$this->cart_item_options->where('cart_item_id', $cart_item['id'])->delete();
break;
case 4: // Remove all items
$all_cart_item = $this->cart_items->where('cart_id', $cart['id'])->findAll();
foreach ($all_cart_item as $item) {
$this->cart_item_options->where('cart_item_id', $item['id'])->delete();
$this->cart_items->delete($item['id']);
}
break;
default:
return $this->fail('Invalid action', 400);
break;
}
// $this->calculate_service->calculateCartTotals($cart['id'], $outlet_id);
$this->db->transComplete();
return $this->respond([
'status' => 200,
'message' => 'Cart updated successfully'
]);
} catch (\Exception $e) {
$this->db->transRollback();
return $this->fail($e->getMessage(), 400);
}
}
public function deleteCart()
{
$validation = $this->validate([
'customer_id' => 'required|numeric',
'outlet_id' => 'required|numeric',
]);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$customer_id = $this->request->getVar('customer_id');
$outlet_id = $this->request->getVar('outlet_id');
$cart = $this->carts->where('customer_id', $customer_id)
->where('outlet_id', $outlet_id)
->where('status', 'active')
->first();
if ($cart) {
$this->carts->update($cart['id'], ['status' => 'trash']);
$this->carts->delete($cart['id']);
$exist_cart_item = getColumnValues($this->cart_items, 'cart_id', $cart['id'], 'id');
$this->cart_items->where('cart_id', $cart['id'])->delete();
$this->cart_item_options->whereIn('cart_item_id', $exist_cart_item)->delete();
return $this->respond([
'status' => 200,
'message' => 'Cart delete successfully'
]);
} else {
return $this->fail('Cart not found', 400);
}
}
public function getCartItemsDetails($cart_item_id = null)
{
$cart_item = $this->cart_items->find($cart_item_id);
if (!$cart_item) {
return $this->fail('Cart item not found', 400);
}
$cart_item['variation'] = $this->menu_item_variations->find($cart_item['variation_id']);
$cart_item['options'] = $this->cart_item_options
->select('cart_item_options.*, options_groups.title as option_group_title')
->join('options_groups', 'options_groups.id = cart_item_options.option_group_id', 'left')
->where('cart_item_id', $cart_item_id)
->findAll();
return $this->respond([
'status' => 200,
'message' => 'Cart item details retrieved successfully',
'data' => $cart_item
]);
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,33 @@
<?php
namespace App\Controllers\Frontend;
use App\Controllers\BaseController;
use CodeIgniter\API\ResponseTrait;
use App\Libraries\Grab;
use App\Models\LogGrab;
class GrabController extends BaseController
{
protected $grab;
use ResponseTrait;
public function __construct()
{
$this->grab = new Grab();
}
public function webhook()
{
$rawBody = file_get_contents('php://input');
$data = json_decode($rawBody, true);
if ($data) {
$this->grab->handleWebhook($data);
}
return $this->respond([
'status' => 200,
'message' => 'Webhook received'
]);
}
}
@@ -0,0 +1,39 @@
<?php
namespace App\Controllers\Frontend;
use App\Controllers\BaseController;
use CodeIgniter\API\ResponseTrait;
use App\Libraries\Lalamove;
use App\Models\LogLalamove;
class LalamoveController extends BaseController
{
protected $lalamove;
use ResponseTrait;
public function __construct()
{
$this->lalamove = new Lalamove();
}
public function webhook()
{
// $log_lalamove = new LogLalamove();
// $log_lalamove->insert([
// 'url' => 'https://icom.ipsgroup.com.my/api/lalamove/webhook',
// 'request' => json_encode($this->request->getJSON(true)),
// 'respond' => 'Webhook received',
// ]);
$rawBody = file_get_contents('php://input');
$data = json_decode($rawBody, true);
if($data){
$this->lalamove->handleWebhook($data);
}
return $this->respond([
'status' => 200,
'message' => 'Webhook received'
]);
}
}
@@ -0,0 +1,240 @@
<?php
namespace App\Controllers\Frontend;
use App\Models\MenuCategories;
use App\Models\MenuImages;
use App\Models\MenuItemCategories;
use App\Models\MenuItemOptionsGroups;
use App\Models\MenuItems;
use App\Models\MenuItemTags;
use App\Models\MenuItemVariations;
use App\Models\RegularItemAvailability;
use App\Models\SeasonalItemAvailability;
use App\Models\VariationOptionsGroups;
use App\Models\VariationTags;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\Database\Config;
use App\Models\OutletMenus;
use App\Controllers\BaseController;
use App\Models\Options;
use App\Models\OptionsGroups;
use CodeIgniter\API\ResponseTrait;
class MenuItemController extends BaseController
{
use ResponseTrait;
private $db;
private $menu_categories;
private $menu_items;
private $menu_items_categories;
private $menu_item_tags;
private $menu_item_variations;
private $menu_item_options_groups;
private $variation_options_groups;
private $variation_tags;
private $regular_item_availability;
private $seasonal_item_availability;
private $menu_images;
private $outlet_menus;
private $options;
private $option_groups;
public function __construct()
{
$this->menu_categories = new MenuCategories();
$this->menu_items = new MenuItems();
$this->menu_items_categories = new MenuItemCategories();
$this->menu_item_tags = new MenuItemTags();
$this->menu_item_variations = new MenuItemVariations();
$this->menu_item_options_groups = new MenuItemOptionsGroups();
$this->variation_options_groups = new VariationOptionsGroups();
$this->variation_tags = new VariationTags();
$this->regular_item_availability = new RegularItemAvailability();
$this->seasonal_item_availability = new SeasonalItemAvailability();
$this->menu_images = new MenuImages();
$this->outlet_menus = new OutletMenus();
$this->options = new Options();
$this->option_groups = new OptionsGroups();
$this->db = Config::connect();
}
public function index()
{
$groups = $this->option_groups->findAll();
$data = [];
foreach ($groups as $group) {
$group['options'] = $this->options
->where('option_group_id', $group['id'])
->where('deleted_at', null)
->findAll();
$data[] = $group;
}
return $this->respond(['status' => 200, 'data' => $data]);
}
public function show($id = null)
{
$menuItem = $this->menu_items->find($id);
if (empty($menuItem)) {
return $this->fail("No menu item found.", 400);
}
$menuItemData = [];
if ($menuItem) {
$menu_tag = $this->menu_item_tags->where('menu_item_id', $menuItem['id'])->findAll();
$menu_option_group = $this->menu_item_options_groups->where('menu_item_id', $menuItem['id'])->findAll();
$menuItemData[] = [
'id' => $menuItem['id'],
'title' => $menuItem['title'],
'category' => getMenuCategory($menuItem['id']),
'short_description' => $menuItem['short_description'],
'long_description' => $menuItem['long_description'],
'price' => $menuItem['price'],
'order_index' => $menuItem['order_index'],
'availability_type' => $menuItem['availability_type'],
'availability' => getAvailability($menuItem['id'], $menuItem['availability_type']),
'status' => $menuItem['status'],
'image' => getMenuImage($menuItem['id']),
'created_at' => $menuItem['created_at'],
'menu_tag' => getMenuTag($menuItem['id']),
'menu_option_group' => getMenuOptionGroup($menuItem['id']),
'variation' => getVariationRelation($menuItem['id'])
];
}
return $this->respond(["status" => 200, "message" => "Successfully retrive data!", "data" => $menuItemData]);
}
public function showOption($id = null)
{
$group = $this->option_groups->find($id);
if (!$group) {
return $this->failNotFound("Option group not found.");
}
$group['options'] = $this->options
->where('option_group_id', $group['id'])
->where('deleted_at', null)
->findAll();
return $this->respond(['status' => 200, 'data' => $group]);
}
public function getAllMenus($outlet_id = null)
{
$db = \Config\Database::connect();
// Get all active categories first
$menu_categories = $db->table('menu_categories')
->select('id, title, image_url, status, compressed_image_url, order_index')
->where('status', 'active')
->where('deleted_at', null)
->orderBy('order_index', 'ASC')
->get()
->getResult();
// Get all tags for mapping
$all_tags = $db->table('tags')->select('id, title, icon_url')->get()->getResult();
$tagMap = [];
foreach ($all_tags as $tag) {
$tagMap[$tag->id] = [
'id' => $tag->id,
'title' => $tag->title,
'icon_url' => $tag->icon_url
];
}
$menu_items = [];
// Loop through each category to get corresponding items
foreach ($menu_categories as $category) {
// Get menu items for this category
$category_items = $db->table('menu_items')
->select('menu_items.id, menu_items.title, menu_items.short_description, menu_items.price, menu_items.availability_type, menu_items.order_index, menu_items.status, menu_items.membership_tier, menu_items.pwp')
->join('menu_item_categories', 'menu_item_categories.menu_item_id = menu_items.id')
->where('menu_item_categories.category_id', $category->id)
->where('menu_items.status', 'active')
->where('menu_items.deleted_at', null)
->orderBy('menu_items.order_index', 'ASC')
->get()
->getResult();
// Process each item in this category
foreach ($category_items as $item) {
// Get item images
$images = $db->table('menu_images')
->select('image_url, image_url_compressed')
->where('menu_item_id', $item->id)
->get()
->getResult();
// Get item tags
$menu_item_tags_links = $db->table('menu_item_tags')
->select('tag_id')
->where('menu_item_id', $item->id)
->get()
->getResult();
$tag_list = [];
foreach ($menu_item_tags_links as $tag_link) {
if (isset($tagMap[$tag_link->tag_id])) {
$tag_list[] = $tagMap[$tag_link->tag_id];
}
}
// Check if item is available in outlet
$is_available = $this->outlet_menus
->where('outlet_id', $outlet_id)
->where('menu_item_id', $item->id)
->first();
// Get category IDs for this item (should include current category)
$item_categories = $db->table('menu_item_categories')
->select('category_id')
->where('menu_item_id', $item->id)
->where('deleted_at', null)
->get()
->getResult();
$category_ids = [];
foreach ($item_categories as $cat_link) {
$category_ids[] = $cat_link->category_id;
}
$membership_tier = $item->membership_tier;
$menu_items[] = [
'id' => $item->id,
'title' => $item->title,
'short_description' => $item->short_description,
'price' => $item->price,
'availability_type' => $item->availability_type,
'order_index' => $item->order_index,
'status' => $item->status,
'images' => $images,
'category_ids' => $category_ids,
'tags' => $tag_list,
'is_available' => $is_available ? true : false,
'membership_tier' => $membership_tier,
'pwp' => $item->pwp,
];
}
}
return $this->response->setJSON([
'status' => 'success',
'response' => '200',
'Categories' => $menu_categories,
'Items' => $menu_items
]);
}
}
@@ -0,0 +1,748 @@
<?php
namespace App\Controllers\Frontend;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\RESTful\ResourceController;
use App\Models\CartItemOptions;
use App\Models\CartItems;
use App\Models\Carts;
use App\Models\Customer;
use App\Models\CustomerWallet;
use App\Models\OrderItemOptions;
use App\Models\OrderItems;
use App\Models\OrderPayments;
use App\Models\Orders;
use App\Models\OrderTaxes;
use App\Models\Outlet;
use App\Models\MenuItemVariations;
use CodeIgniter\Database\Config;
use App\Libraries\Fiuu;
use App\Models\OrderDeliveries;
use App\Models\VoucherRedemptions;
use App\Models\PromoRedemptionRecord;
helper('outlet');
class OrderController extends ResourceController
{
private $carts;
private $cart_items;
private $cart_item_options;
private $orders;
private $order_items;
private $order_item_options;
private $order_payments;
private $order_taxes;
private $customer;
private $customer_wallet;
private $db;
private $cart_service;
private $calculate_service;
private $promo_service;
private $outlet;
private $fiuu;
private $voucher_service;
private $variations;
private $order_deliveries;
private $promo_redemptions_record;
private $voucher_redemptions;
public function __construct()
{
$this->carts = new Carts();
$this->cart_items = new CartItems();
$this->cart_item_options = new CartItemOptions();
$this->orders = new Orders();
$this->order_items = new OrderItems();
$this->order_item_options = new OrderItemOptions();
$this->customer = new Customer();
$this->order_payments = new OrderPayments();
$this->order_taxes = new OrderTaxes();
$this->customer_wallet = new CustomerWallet();
$this->db = Config::connect();
$this->cart_service = service('cartService');
$this->calculate_service = service('calculateService');
$this->promo_service = service('promoService');
$this->voucher_service = service('voucherService');
$this->outlet = new Outlet();
$this->fiuu = new Fiuu();
$this->variations = new MenuItemVariations();
$this->order_deliveries = new OrderDeliveries();
$this->promo_redemptions_record = new PromoRedemptionRecord();
$this->voucher_redemptions = new VoucherRedemptions();
}
public function createOrder()
{
$validation = $this->validate([
'customer_id' => 'required|numeric',
'outlet_id' => 'required|numeric',
'order_type' => 'required',
'customer_address_id' => 'permit_empty|numeric',
'payment_method' => 'required',
'selected_date' => 'permit_empty|string',
'selected_time' => 'permit_empty|string',
'latitude' => 'permit_empty|string',
'longitude' => 'permit_empty|string',
'notes' => 'permit_empty|string',
]);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$customer_id = $this->request->getVar('customer_id');
$outlet_id = $this->request->getVar('outlet_id');
$customer_address_id = $this->request->getVar('customer_address_id');
$order_type = strtolower(str_replace('-', '', $this->request->getVar('order_type')));
$payment_method = $this->request->getVar('payment_method');
$selected_date = $this->request->getVar('selected_date') ?? date('Y-m-d');
$selected_time = $this->request->getVar('selected_time') ?? date('H:i');
$real_selected_date = $this->request->getVar('selected_date');
$real_selected_time = $this->request->getVar('selected_time');
$latitude = $this->request->getVar('latitude');
$longitude = $this->request->getVar('longitude');
$notes = $this->request->getVar('notes');
$address = $this->request->getVar('address');
$cart = $this->carts->where('customer_id', $customer_id)
->where('outlet_id', $outlet_id)
->where('status', 'active')
->first();
if (!$cart) {
return $this->fail('Cart not found', 400);
}
$customer = $this->customer->find($customer_id);
if (!$customer) {
return $this->fail('Customer not found', 400);
}
$promo_or_voucher = null;
if ($cart['promo_code_id'] > 0) {
$check_promo = $this->promo_service->checkPromoAvailability($cart['promo_code_id'], $order_type, $customer_id);
if (isset($check_promo['status']) && $check_promo['status'] == 400) {
$this->calculate_service->resetVoucher($cart['id']);
return $this->respond($check_promo);
}
$promo_or_voucher = 'promo';
}
if ($cart['customer_voucher_list_id'] > 0) {
$check_voucher = $this->voucher_service->checkVoucherAvailability($cart['customer_voucher_list_id'], $order_type, $customer_id);
if (isset($check_voucher['status']) && $check_voucher['status'] == 400) {
$this->calculate_service->resetVoucher($cart['id']);
return $this->respond($check_voucher);
}
$promo_or_voucher = 'voucher';
}
$cart_total = $this->calculate_service->calculateCartTotals($cart['id'], $cart['outlet_id'], $real_selected_date, $real_selected_time, $latitude, $longitude, $order_type, $address, $promo_or_voucher);
// print_r($cart_total);
// exit;
if (isset($cart_total['status']) && $cart_total['status'] == 400) {
$response = [
'status' => 400,
'message' => $cart_total['message']
];
return $this->respond($response, 400);
}
$cart_total = $cart_total['data'];
if (count($cart_total['invalid_items']) > 0) {
return $this->fail('Menu got updated, try again', 400);
}
if ($payment_method == 'wallet') {
if ($customer['customer_wallet'] < $cart_total['order_summary']['grand_total']) {
return $this->fail('Insufficient Wallet Balance', 400);
}
}
if ($cart_total['order_summary']['grand_total'] < 0) {
return $this->fail('Invalid Grand Total', 400);
}
//check operation date and time
$check_operation_date_and_time = checkOperationDateAndTime($outlet_id, $selected_date, $selected_time);
if (!$check_operation_date_and_time) {
return $this->fail('Outlet is not operated on the selected date and time', 400);
}
$this->db->transStart();
try {
$orderData = [
'customer_id' => $customer_id,
'outlet_id' => $outlet_id,
'customer_address_id' => $customer_address_id,
'order_type' => $order_type,
'status' => 'pending',
'payment_status' => 'unpaid',
'packaging_charge' => $cart_total['order_summary']['packaging_charge'] ?? 0,
'payment_method' => $payment_method,
'subtotal_amount' => $cart_total['order_summary']['subtotal_amount'],
'discount_amount' => $cart_total['order_summary']['discount_amount'],
'tax_amount' => $cart_total['order_summary']['tax_amount'],
'delivery_fee' => $cart_total['order_summary']['delivery_fee'] ?? 0,
'rounding_amount' => $cart_total['order_summary']['rounding_amount'],
'grand_total' => $cart_total['order_summary']['grand_total'],
'promo_code_id' => $cart_total['order_summary']['promo_code_id'],
'promo_discount_amount' => $cart_total['order_summary']['promo_discount_amount'],
'customer_voucher_list_id' => $cart_total['order_summary']['customer_voucher_list_id'],
'voucher_discount_amount' => $cart_total['order_summary']['voucher_discount_amount'],
'placed_at' => '',
'selected_date' => $real_selected_date,
'selected_time' => $real_selected_time,
'expected_ready_time' => '',
'notes' => $notes,
'lalamove_quot_id' => $cart['lalamove_quot_id'] ?? null,
'grab_quot_id' => $cart['grab_quot_id'] ?? null
];
//insert order
$order_id = $this->orders->insert($orderData);
if ($promo_or_voucher != null) {
if ($promo_or_voucher == 'promo') {
$this->promo_redemptions_record->insert([
'order_id' => $order_id,
'promo_codes_id' => $orderData['promo_code_id'],
'customer_id' => $orderData['customer_id'],
]);
} else if ($promo_or_voucher == 'voucher') {
$this->voucher_redemptions->insert([
'order_id' => $order_id,
'customer_voucher_list_id' => $orderData['customer_voucher_list_id'],
'customer_id' => $orderData['customer_id'],
'order_type' => $orderData['order_type'],
]);
}
}
if ($order_id > 0) {
$today_order_count = $this->orders->where('created_at >=', date('Y-m-d 00:00:00'))->where('created_at <=', date('Y-m-d 23:59:59'))->countAllResults();
$order_so = date('ymd') . str_pad($today_order_count, 5, '0', STR_PAD_LEFT);
$this->orders->update($order_id, ['order_so' => $order_so]);
}
//insert order items
$cartItems = $this->cart_items->where('cart_id', $cart['id'])->findAll();
foreach ($cartItems as $cartItem) {
$orderItemData = [
'order_id' => $order_id,
'menu_item_id' => $cartItem['menu_item_id'],
'variation_id' => $cartItem['variation_id'],
'title' => $cartItem['title'],
'unit_price' => $cartItem['unit_price'],
'quantity' => $cartItem['quantity'],
'line_subtotal' => $cartItem['line_subtotal'],
'order_index' => $cartItem['order_index'],
];
$orderItemId = $this->order_items->insert($orderItemData);
$cartItemOptions = $this->cart_item_options->where('cart_item_id', $cartItem['id'])->findAll();
foreach ($cartItemOptions as $option) {
$orderItemOptionData = [
'order_item_id' => $orderItemId,
'option_id' => $option['option_id'],
'option_title' => $option['option_title'],
'price_adjustment' => $option['price_adjustment'],
];
$this->order_item_options->insert($orderItemOptionData);
}
}
//insert order taxes
foreach ($cart_total['tax_detail'] as $tax_order) {
$tax_data = [
'order_id' => $order_id,
'tax_type' => $tax_order['tax_type'],
'tax_rate' => $tax_order['tax_rate'],
'tax_amount' => $tax_order['tax_amount'],
];
$this->order_taxes->insert($tax_data);
}
$respond_data = [];
//insert order payments
if ($payment_method == 'wallet') {
$payment = [
'order_id' => $order_id,
'payment_method' => $payment_method,
'amount' => $cart_total['order_summary']['grand_total'],
'transaction_id' => 0,
'status' => 'pending',
'paid_at' => '',
];
// echo(123);exit;
$this->order_payments->insert($payment);
completeOrder($order_id);
} else {
//generate payment details & url
$payment = [
'order_id' => $order_id,
'payment_method' => $payment_method,
'amount' => $cart_total['order_summary']['grand_total'],
'transaction_id' => 0,
'status' => 'pending',
'paid_at' => '',
];
$this->order_payments->insert($payment);
$payment_details = [
'bill_name' => $customer['name'],
'bill_email' => $customer['email'],
'bill_mobile' => $customer['phone'],
'bill_desc' => 'US Pizza - Payment for ' . ucfirst($order_type) . ' - ' . $order_id,
];
$result = $this->fiuu->createPayment($order_so, $cart_total['order_summary']['grand_total'], $payment_details);
$respond_data['redirect_url'] = $result['redirect_url'];
}
$this->carts->update($cart['id'], ['status' => 'completed']);
$this->db->transComplete();
$order = $this->orders->find($order_id);
$order['items'] = $this->order_items->where('order_id', $order_id)->findAll();
foreach ($order['items'] as &$item) {
$item['options'] = $this->order_item_options->where('order_item_id', $item['id'])->findAll();
}
$respond_data['status'] = 200;
$respond_data['message'] = 'Order created successfully';
$respond_data['order'] = $order;
return $this->respond($respond_data);
} catch (\Exception $e) {
$this->db->transRollback();
print_r($e->getMessage());
return $this->fail('Cannot checkout order. Please try again.', 400);
}
}
public function createOrderAgain($order_id = null)
{
$order = $this->orders->select('orders.*, customer_addresses.address, customer_addresses.latitude, customer_addresses.longitude')
->join('customer_addresses', 'customer_addresses.id = orders.customer_address_id', 'left')
->find($order_id);
if (!$order) {
return $this->fail('Order not found', 400);
}
$outlet = $this->outlet->find($order['outlet_id']);
$outlet_title = $outlet['title'] ?? '';
//check outlet cart exist
$existing_cart = $this->carts
->where('customer_id', $order['customer_id'])
->where('outlet_id', $order['outlet_id'])
->where('status', 'active')
->first();
if ($existing_cart) {
//clear all cart items
$this->cart_items->where('cart_id', $existing_cart['id'])->delete();
}
//select order items
$order_items = $this->order_items->where('order_id', $order_id)->findAll();
// print_r($order_items);exit;
foreach ($order_items as $order_item) {
if ($order_item['unit_price'] > 0) {
$options = $this->order_item_options
->select('options.option_group_id, order_item_options.option_id')
->join('options', 'options.id = order_item_options.option_id')
->where('order_item_id', $order_item['id'])
->findAll();
$option_group = [];
if (!empty($options)) {
// Group options by option_group_id
$grouped_options = [];
foreach ($options as $option) {
$grouped_options[$option['option_group_id']][] = $option['option_id'];
}
// Convert to the expected format for cart service
foreach ($grouped_options as $group_id => $option_ids) {
$option_group[] = [
'group_id' => $group_id,
'option_ids' => $option_ids
];
}
}
// print_r($option_group);exit;
$addCart = $this->cart_service->addCartItem($order['customer_id'], $order['outlet_id'], $existing_cart, $order_item['menu_item_id'], $order_item['variation_id'], $option_group, $order_item['quantity'], false);
if (isset($addCart['status']) && $addCart['status'] == 400) {
return $this->fail($addCart['message'], 400);
}
}
}
$data = [
'order_id' => $order['id'],
'customer_id' => $order['customer_id'],
'outlet_id' => $order['outlet_id'],
'outlet_title' => $outlet_title,
'customer_address_id' => $order['customer_address_id'],
'order_type' => $order['order_type'],
'address' => $order['address'],
'latitude' => $order['latitude'],
'longitude' => $order['longitude']
];
return $this->respond(['status' => 200, 'message' => 'Order created successfully', 'data' => $data]);
}
public function createOrderVip()
{
$customer_id = $this->request->getVar('customer_id');
$outlet_id = HQ_OUTLET_ID;
$outlet = $this->outlet->find($outlet_id);
$order_type = 'dinein';
$outlet_title = $outlet['title'] ?? '';
//check outlet cart exist
$existing_cart = $this->carts
->where('customer_id', $customer_id)
->where('outlet_id', $outlet_id)
->where('status', 'active')
->first();
if ($existing_cart) {
//clear all cart items
$this->cart_items->where('cart_id', $existing_cart['id'])->delete();
}
//select VIP Cart Item
$addCart = $this->cart_service->addCartItem($customer_id, $outlet_id, $existing_cart, VIP_MENU_ITEM_ID, 0, [], 1, false);
if (isset($addCart['status']) && $addCart['status'] == 400) {
return $this->fail($addCart['message'], 400);
}
$data = [
'customer_id' => $customer_id,
'outlet_id' => $outlet_id,
'outlet_title' => $outlet_title,
'order_type' => $order_type,
];
return $this->respond(['status' => 200, 'message' => 'Order created successfully', 'data' => $data]);
}
public function showOrder($order_id = null)
{
// echo(123);exit;
$order = $this->orders->find($order_id);
if (!$order) {
return $this->fail('Order not found', 400);
}
//calculate grad_total before rounding
$grand_total_before_rounding = $order['grand_total'] - $order['rounding_amount'];
$order['grand_total_before_rounding'] = number_format($grand_total_before_rounding, 2);
$order['items'] = $this->order_items->where('order_id', $order_id)->findAll();
foreach ($order['items'] as &$item) {
$item['variation'] = $this->variations->find($item['variation_id']);
$item['options'] = $this->order_item_options->where('order_item_id', $item['id'])->findAll();
}
$order['taxes'] = $this->order_taxes->where('order_id', $order_id)->findAll();
$order['payments'] = $this->order_payments
->where('order_id', $order_id)
->orderBy('created_at', 'DESC')
->findAll();
$order['deliveries'] = $this->order_deliveries
->where('order_id', $order_id)
->orderBy('created_at', 'DESC')
->findAll();
$order['packaging_charge'] = $order['packaging_charge'] ?? 0;
$order['points'] = floor($order['grand_total']);
// print_r($order);exit;
return $this->respond([
'status' => 200,
'message' => 'Order retrieved successfully',
'data' => $order
]);
}
public function orderList($customer_id)
{
$start_date = $this->request->getVar('start_date');
$end_date = $this->request->getVar('end_date');
$status = $this->request->getVar('status');
$order_type = $this->request->getVar('order_type');
$orderQuery = $this->orders
->where('customer_id', $customer_id)
->orderBy('created_at', 'DESC');
if (!empty($start_date)) {
$orderQuery->where('created_at >=', $start_date . ' 00:00:00');
}
if (!empty($end_date)) {
$orderQuery->where('created_at <=', $end_date . ' 23:59:59');
}
if (!empty($status)) {
$orderQuery->where('status', $status);
}
if (!empty($order_type)) {
$orderQuery->where('order_type', $order_type);
}
$orders = $orderQuery->findAll();
if (empty($orders)) {
return $this->respond([
'status' => 200,
'message' => 'No orders found for this customer',
'data' => []
]);
}
foreach ($orders as &$order) {
$order_id = $order['id'];
$order['items'] = $this->order_items->where('order_id', $order_id)->findAll();
foreach ($order['items'] as &$item) {
$item['options'] = $this->order_item_options
->where('order_item_id', $item['id'])
->findAll();
}
$order['taxes'] = $this->order_taxes
->where('order_id', $order_id)
->findAll();
$order['payments'] = $this->order_payments
->where('order_id', $order_id)
->orderBy('created_at', 'DESC')
->findAll();
$order['packaging_charge'] = $order['packaging_charge'] ?? 0;
}
return $this->respond([
'status' => 200,
'message' => 'Orders retrieved successfully',
'data' => $orders
]);
}
// public function applyPromoCode(){
// $validation = $this->validate([
// 'promo_code' => 'required',
// 'cart_id' => 'required|numeric',
// 'outlet_id' => 'required|numeric',
// ]);
// if (!$validation) {
// return $this->failValidationErrors($this->validator->getErrors());
// }
// $promo_code = $this->request->getVar('promo_code');
// $cart_id = $this->request->getVar('cart_id');
// $outlet_id = $this->request->getVar('outlet_id');
// $free_item = $this->request->getVar('free_item') ?? [];
// if(empty($promo_code)){
// return $this->respond(['status' => 400, 'result' => 'Promo code is empty.']);
// }
// $promo_code_id = $this->promo_service->checkPromoCode($promo_code);
// if(isset($promo_code_id['status']) && $promo_code_id['status'] == 400){
// //error message respond
// return $this->respond($promo_code_id);
// }
// //update promo id
// $this->carts->update($cart_id, ['promo_code_id' => $promo_code_id]);
// $applyPromoCode = $this->calculate_service->calculateCartTotals($cart_id, $outlet_id, null, null, null, null, null, null, $free_item);
// return $this->respond($applyPromoCode);
// }
//get all active outlets
// public function outletList()
// {
// $outlets = $this->outlet->getActiveOperatingHoursWithDaysList();
// if(empty($outlets)){
// return $this->respond(['status' => 400, 'result' => 'No outlet data found.']);
// }
// return $this->respond(['status' => 200, 'result' => $outlets]);
// }
public function index()
{
$outlets = $this->outlet->getOperatingHoursWithDaysList();
if (empty($outlets)) {
return $this->respond(['status' => 400, 'result' => 'No outlet data found.']);
}
return $this->respond(['status' => 200, 'result' => $outlets]);
}
public function nearestOutletList($mode = null, $latitude = null, $longitude = null)
{
$outlets = $this->outlet->getActiveOperatingHoursWithDaysListAndDistance($mode, $latitude, $longitude);
if (empty($outlets)) {
return $this->respond(['status' => 400, 'result' => 'No outlet data found.']);
}
return $this->respond(['status' => 200, 'result' => $outlets]);
}
public function showOutlet($outlet_id = null)
{
$result = $this->outlet->getOperatingHoursWithDays($outlet_id);
if (empty($result)) {
return $this->respond(['status' => 400, 'result' => 'No outlet data found.']);
}
return $this->respond(['status' => 200, 'result' => $result]);
}
public function checkPayment($order_id = null)
{
$order = $this->orders->find($order_id);
if (!$order) {
return $this->fail('Order not found', 400);
}
$payment_status = $order['payment_status'];
if ($payment_status == 'paid') {
return $this->respond(['status' => 200, 'result' => 'Success', 'order_id' => $order_id]);
} else if ($payment_status == 'pending') {
return $this->respond(['status' => 200, 'result' => 'Pending', 'order_id' => $order_id]);
} else {
return $this->respond(['status' => 400, 'result' => 'Failed', 'order_id' => $order_id]);
}
}
/**
* Get driver location coordinates for real-time tracking
* Called every 5 seconds from mobile app
*/
public function getDriverLocation()
{
// Get order ID from request
$order_id = $this->request->getPost('order_id') ?? $this->request->getGet('order_id');
if (!$order_id) {
return $this->respond([
'status' => 'error',
'message' => 'Order ID is required',
'data' => null
], 400);
}
// Get order details
$orders = new Orders();
$order = $orders->select('orders.*, order_deliveries.provider_name, order_deliveries.provider_order_id, order_deliveries.status')
->join('order_deliveries', 'order_deliveries.order_id = orders.id', 'left')
->where('orders.id', $order_id)
->where('orders.deleted_at', null)
->first();
if (!$order) {
return $this->respond([
'status' => 'error',
'message' => 'Order not found',
'data' => null
], 404);
}
// Check if order has delivery
if (!$order['provider_name'] || !$order['provider_order_id']) {
return $this->respond([
'status' => 'error',
'message' => 'Order has no active delivery',
'data' => null
], 400);
}
$driver_location = null;
$error_message = null;
try {
// Check delivery provider and call respective API
if ($order['provider_name'] === 'Lalamove') {
$lalamove = new \App\Libraries\Lalamove();
$driver_location = $lalamove->getDriverLocation($order['provider_order_id']);
if (!$driver_location || isset($driver_location['error'])) {
$error_message = 'Unable to fetch Lalamove driver location';
}
} elseif ($order['provider_name'] === 'Grab') {
$grab = new \App\Libraries\Grab();
$driver_location = $grab->getDriverLocation($order['provider_order_id']);
if (!$driver_location || isset($driver_location['error'])) {
$error_message = 'Unable to fetch Grab driver location';
}
} else {
return $this->respond([
'status' => 'error',
'message' => 'Unsupported delivery provider: ' . $order['provider_name'],
'data' => null
], 400);
}
} catch (Exception $e) {
log_message('error', 'Driver location API error: ' . $e->getMessage());
$error_message = 'Failed to fetch driver location';
}
// If there's an error, return error response
if ($error_message) {
return $this->respond([
'status' => 'error',
'message' => $error_message,
'data' => null
], 500);
}
// Return driver location data
return $this->respond([
'status' => 'success',
'message' => 'Driver location retrieved successfully',
'data' => [
'order_id' => $order_id,
'provider' => $order['provider_name'],
'delivery_status' => $order['status'],
'driver_location' => $driver_location,
'timestamp' => date('Y-m-d H:i:s'),
'last_updated' => time()
]
], 200);
}
}
@@ -0,0 +1,97 @@
<?php
namespace App\Controllers\Frontend;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\API\ResponseTrait;
use App\Models\Orders;
class PaymentController extends BaseController
{
use ResponseTrait;
private $fiuu;
private $order;
public function __construct()
{
$this->fiuu = new \App\Libraries\Fiuu();
$this->order = new Orders();
}
// public function createPayment()
// {
// $data = $this->request->getJSON(true); // true = associative array
// $amount = $data['amount'] ?? '0.00';
// $orderId = $data['order_id'] ?? 'UNKNOWN';
// // log_message('debug', " order_id: $orderId, amount: $amount");
// $result = $this->fiuu->createPayment($orderId, $amount);
// return $this->response->setJSON($result);
// }
public function createPayment()
{
$data = $this->request->getJSON(true);
$orderId = $data['order_id'] ?? 'UNKNOWN';
$amount = $data['amount'] ?? '0.00';
$customer = [
'name' => $data['bill_name'] ?? 'John Doe',
'email' => $data['bill_email'] ?? 'johndoe@example.com',
'mobile' => $data['bill_mobile'] ?? '601133094116',
'desc' => $data['bill_desc'] ?? 'Test payment',
'address' => $data['bill_address'] ?? '123 Jalan Teknologi',
'postcode' => $data['bill_postcode'] ?? '47000',
'city' => $data['bill_city'] ?? 'Petaling Jaya',
'state' => $data['bill_state'] ?? 'Selangor',
'country' => $data['bill_country'] ?? 'MY'
];
// Call the updated Fiuu library method
$result = $this->fiuu->createPayment($orderId, $amount, $customer);
return $this->response->setJSON($result);
}
public function fiuuPaymentReturn()
{
$data = $this->request->getPost();
$this->fiuu->paymentNotification($data);
}
public function fiuuPaymentNotification()
{
$data = $this->request->getPost();
$this->fiuu->paymentNotification($data);
}
public function payAgain($order_id){
$order = $this->order->select('orders.*, customers.name, customers.email, customers.phone')
->join('customers', 'customers.id = orders.customer_id')
->where('orders.id', $order_id)
->where('orders.payment_status', 'unpaid')
->first();
if(!$order){
return $this->fail('Order not found', 400);
}
$payment_details = [
'bill_name' => $order['name'],
'bill_email' => $order['email'],
'bill_mobile' => $order['phone'],
'bill_desc' => 'US Pizza - Payment for ' . ucfirst($order['order_type']) . ' - ' . $order['order_so'],
];
$respond_data = [];
$result = $this->fiuu->createPayment($order['order_so'], $order['grand_total'], $payment_details);
$respond_data['redirect_url'] = $result['redirect_url'];
$respond_data['status'] = 200;
$respond_data['message'] = 'Payment link generated successfully';
return $this->respond($respond_data);
}
}
@@ -0,0 +1,48 @@
<?php
namespace App\Controllers\Frontend;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
class SlideshowController extends BaseController
{
public function index()
{
$slideshowModel = new \App\Models\SlideshowModel();
$allowedOrderFields = [
'order', 'created_at', 'updated_at', 'title', 'status'
];
$orderBy = $this->request->getGet('order_by') ?? 'order';
$sort = strtolower($this->request->getGet('sort')) === 'desc' ? 'DESC' : 'ASC';
$page = (int) ($this->request->getGet('page') ?? 1);
$limit = (int) ($this->request->getGet('limit') ?? 20);
if (!in_array($orderBy, $allowedOrderFields)) {
$orderBy = 'order';
}
$builder = $slideshowModel->orderBy($orderBy, $sort)->where('deleted_at', null);
if ($limit > 0) {
$offset = ($page - 1) * $limit;
$slides = $builder->findAll($limit, $offset);
$total = $slideshowModel->where('deleted_at', null)->countAllResults();
} else {
$slides = $builder->findAll();
$total = count($slides);
}
return $this->response->setJSON([
'status' => 200,
'result' => $slides,
'pagination' => [
'page' => $page,
'limit' => $limit,
'total' => $total
]
]);
}
}
@@ -0,0 +1,345 @@
<?php
namespace App\Controllers\Frontend;
use App\Controllers\BaseController;
use App\Models\TopupModel;
use App\Libraries\Fiuu;
use CodeIgniter\API\ResponseTrait;
use App\Models\Customer;
use App\Models\CustomerWallet;
class TopupController extends BaseController
{
use ResponseTrait;
private $topups;
private $fiuu;
private $customer;
private $wallets;
private $db;
public function __construct()
{
$this->topups = new TopupModel();
$this->fiuu = new Fiuu();
$this->customer = new Customer();
$this->wallets = new CustomerWallet();
$this->db = db_connect();
}
public function index()
{
$data = $this->topups
->orderBy('created_at', 'DESC')
->findAll();
return $this->respond([
'status' => 200,
'data' => $data,
]);
}
private function createPendingWalletRow(array $topupRow, array $customer): void
{
$current = (float)($customer['customer_wallet'] ?? 0);
$incoming = (float)($topupRow['credit'] ?? 0);
if ($incoming <= 0) {
$incoming = (float)($topupRow['other_amount'] ?? 0);
}
$walletData = [
'customer_id' => $topupRow['customer_id'],
'related_type' => 'topup',
'related_id' => $topupRow['id'],
'action' => 'in',
'current' => $current,
'in' => $incoming,
'out' => 0,
'balance' => $current,
'remark' => 'Topup '.$topupRow['topup_number'].' (pending)',
'status' => 'Pending',
'created_at' => date('Y-m-d H:i:s'),
];
try {
$this->wallets->insert($walletData, true);
} catch (\Throwable $e) {
}
}
public function createTopupPayment()
{
$data = $this->request->getJSON(true);
$rules = [
'customer_id' => 'required|integer',
'payment_method' => 'required|string|max_length[50]',
'topup_setting_id' => 'permit_empty|integer',
'amount' => 'permit_empty|decimal',
'credit' => 'permit_empty|decimal',
'other_amount' => 'permit_empty|decimal'
];
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$topupSettingId = !empty($data['topup_setting_id']) ? (int)$data['topup_setting_id'] : null;
if ($topupSettingId) {
// MODE 1: package top-up
$pkg = model('App\Models\TopupSettingModel')
->where('id', $topupSettingId)
->where('deleted_at', null)
->first();
if (!$pkg || ($pkg['status'] ?? '') !== 'active') {
return $this->failValidationErrors(['topup_setting_id' => 'Selected package is invalid or inactive']);
}
$amount = (float)$pkg['topup_amount'];
$credit = (float)$pkg['credit_amount'];
if ($amount <= 0 || $credit <= 0) {
return $this->failValidationErrors(['topup_setting' => 'Package amounts must be > 0']);
}
$otherAmount = null;
} else {
$otherAmount = isset($data['other_amount']) && $data['other_amount'] !== '' ? (float)$data['other_amount'] : 0.0;
if ($otherAmount <= 0) {
return $this->failValidationErrors(['other_amount' => 'Provide a positive amount for custom top-up']);
}
$amount = 0.0;
$credit = 0.0;
}
$topupNumber = 'T' . strtoupper(bin2hex(random_bytes(6)));
$insertData = [
'topup_setting_id' => $topupSettingId,
'customer_id' => (int)$data['customer_id'],
'topup_number' => $topupNumber,
'amount' => $amount, // package cash (0 for custom)
'credit' => $credit, // package credit (0 for custom)
'payment_method' => $data['payment_method'],
'other_amount' => $otherAmount, // used for custom
'status' => 'Pending',
];
$id = $this->topups->insert($insertData, true);
if (!$id) return $this->fail('Topup failed', 400);
$topup = $this->topups->find($id);
$customer = $this->customer->find((int)$data['customer_id']) ?? [];
// Write PENDING wallet ledger (no balance change yet)
$this->createPendingWalletRow($topup, $customer);
// Payment link uses the payable amount:
// - package mode → $amount
// - custom mode → $otherAmount
$payable = $topupSettingId ? $amount : $otherAmount;
$payment_details = [
'bill_name' => $customer['name'] ?? '',
'bill_email' => $customer['email'] ?? '',
'bill_mobile' => $customer['phone'] ?? '',
'bill_desc' => 'US Pizza - Wallet Topup - ' . $topupNumber,
];
$result = $this->fiuu->createTopup($topupNumber, $payable, $payment_details);
return $this->respond([
'status' => 201,
'message' => 'Topup created successfully',
'data' => $this->topups->find($id),
'redirect_url' => $result['redirect_url'] ?? null
]);
}
public function fiuuTopupNotification()
{
$payload = $this->request->getPost();
// $payload = $this->request->getJSON(true);
// print_r($payload);exit;
$result = $this->fiuu->topupNotification($payload);
log_message('error', 'Fiuu Payload: '.json_encode($payload));
log_message('error', 'Fiuu Result: '.json_encode($result));
$topupNumber = $payload['orderid'] ?? null;
if (!$topupNumber) {
return $this->respond(['status' => 400, 'message' => 'Missing orderid'], 400);
}
$topup = $this->topups->where('topup_number', $topupNumber)->first();
if (!$topup) {
return $this->respond(['status' => 404, 'message' => 'Topup not found'], 404);
}
if (in_array($topup['status'], ['completed','failed'], true)) {
return $this->respond(['status' => 200, 'message' => 'Already processed', 'result' => $result]);
}
$payStat = strtolower((string)($payload['status'] ?? ''));
$resStat = is_array($result) ? strtolower((string)($result['status'] ?? '')) : strtolower((string)$result);
$isSuccess = in_array($payStat, ['00','success','approved'], true)
|| (is_string($resStat) && strpos($resStat, 'success') !== false);
$this->db->transStart();
if ($isSuccess) {
$table = $this->customer->builder()->getTable();
$customer = $this->db->query(
'SELECT * FROM `'.$table.'` WHERE `id` = ? FOR UPDATE',
[$topup['customer_id']]
)->getRowArray();
if (!$customer) {
$this->db->transRollback();
return $this->respond(['status' => 404, 'message' => 'Customer not found'], 404);
}
$current = (float)($customer['customer_wallet'] ?? 0);
$delta = $topup['amount'] + $topup['credit'];
// print_r($delta);exit;
if ($delta <= 0) $delta = (float)$topup['other_amount'];
if ($delta <= 0) {
$this->topups->update($topup['id'], ['status' => 'failed']);
$this->db->transComplete();
return $this->respond(['status' => 400, 'message' => 'Invalid topup amount'], 400);
}
$newBal = $current + $delta;
$walletRow = $this->wallets
->where('related_type', 'topup')
->where('related_id', $topup['id'])
->orderBy('id', 'desc')
->first();
if ($walletRow) {
$this->wallets->update($walletRow['id'], [
'status' => 'completed',
'current' => $current,
'balance' => $newBal,
'remark' => 'Topup '.$topupNumber.' (completed)',
'in' => $delta,
]);
} else {
$this->wallets->insert([
'customer_id' => $topup['customer_id'],
'related_type' => 'topup',
'related_id' => $topup['id'],
'action' => 'in',
'current' => $current,
'in' => $delta,
'out' => 0,
'balance' => $newBal,
'remark' => 'Topup '.$topupNumber.' (completed)',
'status' => 'completed',
'created_at' => date('Y-m-d H:i:s'),
], true);
}
$this->customer->update($customer['id'], ['customer_wallet' => $newBal]);
$this->topups->update($topup['id'], ['status' => 'Success']);
} else {
$this->topups->update($topup['id'], ['status' => 'Failed']);
$this->wallets
->where('related_type', 'topup')
->where('related_id', $topup['id'])
->set([
'status' => 'failed',
'remark' => 'Topup '.$topupNumber.' (failed)'
])->update();
}
$this->db->transComplete();
if ($this->db->transStatus() === false) {
return $this->respond(['status' => 500, 'message' => 'Transaction error'], 500);
}
return $this->respond(['status' => 200, 'message' => 'Notification processed', 'result' => $result]);
}
public function fiuuTopupReturn()
{
$data = $this->request->getPost();
$this->fiuu->topupNotification($data);
}
public function checkTopupPayment($topup_id = null)
{
// echo(123123);exit;
$topup = $this->topups->find($topup_id);
if (!$topup) {
return $this->fail('Topup not found', 400);
}
// print_r($topup);exit;
$status = strtolower($topup['status'] ?? '');
// print_r($status);exit;
if ($status === 'success') {
return $this->respond(['status' => 200, 'result' => 'Success', 'topup_id' => $topup_id]);
} elseif ($status === 'pending') {
return $this->respond(['status' => 200, 'result' => 'Pending', 'topup_id' => $topup_id]);
}
return $this->respond(['status' => 400, 'result' => 'Failed', 'topup_id' => $topup_id]);
}
public function show($id = null)
{
$id = (int) $id;
$row = $this->topups
->select('topup.*, topup_setting.id AS setting_id, topup_setting.topup_amount, topup_setting.credit_amount, topup_setting.status AS setting_status')
->join('topup_setting', 'topup_setting.id = topup.topup_setting_id', 'left')
->where('topup.id', $id)
->first();
if (!$row) {
return $this->failNotFound('Topup record not found');
}
$data = [
'id' => $row['id'],
'topup_setting_id' => $row['topup_setting_id'],
'customer_id' => $row['customer_id'],
'topup_number' => $row['topup_number'],
'amount' => $row['amount'],
'credit' => $row['credit'],
'payment_method' => $row['payment_method'],
'other_amount' => $row['other_amount'],
'status' => $row['status'],
'created_at' => $row['created_at'],
'updated_at' => $row['updated_at'],
'topup_setting' => [
'id' => $row['setting_id'],
'topup_amount' => $row['topup_amount'],
'credit_amount' => $row['credit_amount'],
'status' => $row['setting_status']
]
];
return $this->respond([
'status' => 200,
'data' => $data
]);
}
}
@@ -0,0 +1,33 @@
<?php
namespace App\Controllers\Frontend;
use App\Controllers\BaseController;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\API\ResponseTrait;
use App\Models\TopupSettingModel;
class TopupSettingController extends BaseController
{
use ResponseTrait;
private $settings;
public function __construct()
{
$this->settings = new TopupSettingModel();
}
public function index()
{
$data = $this->settings
->orderBy('topup_amount', 'ASC')
->findAll();
return $this->respond([
'status' => 200,
'data' => $data,
]);
}
}
@@ -0,0 +1,508 @@
<?php
namespace App\Controllers\Frontend;
use App\Controllers\BaseController;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\HTTP\ResponseInterface;
use App\Models\Customer;
use App\Models\CustomerVoucherList;
use App\Models\PromoCode;
use App\Models\PromoSetting;
use App\Models\Carts;
use App\Models\VoucherPoints;
use App\Models\MenuItems;
use App\Models\MenuItemCategories;
use App\Models\CustomerPoint;
class VoucherController extends BaseController
{
use ResponseTrait;
private $customers;
private $customer_voucher_list;
private $customer_points;
private $promo_codes;
private $promo_settings;
private $carts;
private $calculate_service;
private $promo_service;
private $voucher_service;
private $menuItem;
private $menuItemCategories;
private $voucher_points;
public function __construct()
{
$this->customers = new Customer();
$this->voucher_points = new VoucherPoints();
$this->customer_voucher_list = new CustomerVoucherList();
$this->promo_codes = new PromoCode();
$this->promo_settings = new PromoSetting();
$this->carts = new Carts();
$this->calculate_service = service('calculateService');
$this->promo_service = service('promoService');
$this->voucher_service = service('voucherService');
$this->menuItem = new MenuItems();
$this->menuItemCategories = new MenuItemCategories();
}
public function claim()
{
// echo(123);exit;
$body = $this->request->getJSON(true) ?? $this->request->getPost();
$customerId = (int) ($body['customer_id'] ?? 0);
$voucherId = (int) ($body['voucher_id'] ?? 0);
if ($customerId <= 0 || $voucherId <= 0) {
return $this->respond(['status' => 400, 'message' => 'Missing customer_id or voucher_id'], 400);
}
$voucherModel = $this->voucher_points;
$custPtsModel = new CustomerPoint();
$custVoucherMod = new CustomerVoucherList();
$voucher = $voucherModel
->where('id', $voucherId)
->where('voucher_status', 'active')
->first();
if (!$voucher) {
return $this->respond(['status' => 404, 'message' => 'Voucher not found or inactive'], 404);
}
if (!empty($voucher['voucher_total_count']) && !empty($voucher['voucher_redeem_count'])) {
if ((int)$voucher['voucher_redeem_count'] >= (int)$voucher['voucher_total_count']) {
return $this->respond(['status' => 400, 'message' => 'Voucher is out of stock'], 400);
}
}
if (!empty($voucher['voucher_count_customer'])) {
$taken = $custVoucherMod->where([
'customer_id' => $customerId,
'promo_setting_id' => $voucher['promo_setting_id'] ?? null,
])->where('voucher_status !=', 'trash')->countAllResults();
if ($taken >= (int)$voucher['voucher_count_customer']) {
return $this->respond(['status' => 400, 'message' => 'You have reached the redeem limit for this voucher'], 400);
}
}
$today = date('Y-m-d');
if (!empty($voucher['voucher_expired_date']) && $today > $voucher['voucher_expired_date']) {
return $this->respond(['status' => 400, 'message' => 'Voucher already expired'], 400);
}
$lastLedger = $custPtsModel->where('customer_id', $customerId)
->orderBy('id', 'DESC')->first();
$currentBalance = $lastLedger ? (float)$lastLedger['balance'] : 0.0;
$costPoints = (float) ($voucher['voucher_point_redeem'] ?? 0);
if ($costPoints <= 0) {
return $this->respond(['status' => 400, 'message' => 'Voucher point cost is invalid'], 400);
}
if ($currentBalance < $costPoints) {
return $this->respond(['status' => 400, 'message' => 'Insufficient points'], 400);
}
$custVoucherExpiry = null;
$type = strtolower(trim($voucher['voucher_expiry_type'] ?? ''));
$value = trim($voucher['voucher_expiry_value'] ?? '');
if ($type === 'date' && !empty($voucher['voucher_expired_date'])) {
$custVoucherExpiry = $voucher['voucher_expired_date'];
} elseif ($type === 'fixed' || $type === 'days') {
$days = (int)$value;
$custVoucherExpiry = date('Y-m-d', strtotime("+{$days} days"));
} else {
$custVoucherExpiry = !empty($voucher['voucher_expired_date']) ? $voucher['voucher_expired_date'] : null;
}
$already = $custVoucherMod->where([
'customer_id' => $customerId,
'promo_setting_id' => $voucher['promo_setting_id'] ?? null,
'voucher_status' => 'active',
])->first();
if ($already) {
return $this->respond(['status' => 200, 'message' => 'Voucher already redeemed for this customer', 'data' => $already]);
}
$db = \Config\Database::connect();
$db->transStart();
$voucherCode = strtoupper('V' . $voucherId . '-' . bin2hex(random_bytes(3)));
$custVoucherId = $custVoucherMod->insert([
'promo_setting_id' => $voucher['promo_setting_id'] ?? null,
'customer_id' => $customerId,
'voucher_order_id' => 0,
'voucher_topup_id' => 0,
'voucher_code' => $voucherCode,
'voucher_expiry_date'=> $custVoucherExpiry,
'voucher_status' => 'active',
], true);
$newBalance = $currentBalance - $costPoints;
$ledgerId = $custPtsModel->insert([
'customer_id' => $customerId,
'related_id' => $voucherId,
'related_type'=> 'voucher',
'action' => 'redeem_voucher',
'current' => $currentBalance,
'in' => 0,
'out' => $costPoints,
'balance' => $newBalance,
'remark' => sprintf('Redeemed voucher "%s" (ID %d, code %s)', $voucher['voucher_name'] ?? '', $voucherId, $voucherCode),
], true);
$this->customers->update($customerId, [
'customer_point' => $newBalance
]);
$voucherModel->where('id', $voucherId)->set('voucher_redeem_count', 'voucher_redeem_count + 1', false)->update();
$db->transComplete();
if ($db->transStatus() === false) {
return $this->respond(['status' => 500, 'message' => 'Failed to redeem voucher'], 500);
}
return $this->respond([
'status' => 200,
'message' => 'Voucher redeemed successfully',
'data' => [
'customer_voucher_id' => $custVoucherId,
'ledger_id' => $ledgerId,
'voucher_code' => $voucherCode,
'new_balance' => $newBalance,
'voucher_expiry_date' => $custVoucherExpiry,
],
]);
}
public function index()
{
// echo(123123);exit;
log_message('info', 'Voucher index accessed');
$voucherName = $this->request->getGet('voucher_name');
$dateFrom = $this->request->getGet('date_from');
$dateTo = $this->request->getGet('date_to');
$builder = $this->voucher_points->builder();
$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 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]);
}
public function voucherList($customer_id)
{
if(empty($customer_id)) {
return $this->respond(['status' => 400, 'result' => 'Customer ID is empty.']);
}
$customer = $this->customers->find($customer_id);
if(empty($customer)) {
return $this->respond(['status' => 400, 'result' => 'Customer data not found.']);
}
$customer_voucher_list = $this->customer_voucher_list
->select('customer_voucher_list.*, promo_settings.description')
->join('promo_settings', 'promo_settings.id = customer_voucher_list.promo_setting_id')
->where('customer_voucher_list.customer_id', $customer_id)
->findAll();
return $this->respond(['status' => 200, 'message' =>'Customer voucher list retrieved sucessfully!', 'data' => $customer_voucher_list]);
}
public function redeemVoucher($customer_id)
{
if(empty($customer_id)) {
return $this->respond(['status' => 400, 'result' => 'Customer ID is empty.']);
}
$customer = $this->customers->find($customer_id);
if(empty($customer)) {
return $this->respond(['status' => 400, 'result' => 'Customer data not found.']);
}
$validation = $this->validate([
'order_type' => 'required',
'promo_code' => 'permit_empty',
'voucher_id' => 'permit_empty',
'cart_id' => 'required',
'outlet_id' => 'required',
]);
if (!$validation) {
return $this->failValidationErrors($this->validator->getErrors());
}
$promo_code = $this->request->getVar('promo_code');
$voucher_id = $this->request->getVar('voucher_id');
$cart_id = $this->request->getVar('cart_id');
$outlet_id = $this->request->getVar('outlet_id');
$order_type = $this->request->getVar('order_type');
if(empty($promo_code) && empty($voucher_id)) {
return $this->respond(['status' => 400, 'result' => 'Voucher code or voucher id is empty.']);
}
$getCart = $this->carts->where('customer_id', $customer_id)->where('outlet_id', $outlet_id)->where('status', 'active')->find($cart_id);
if(empty($getCart)) {
return $this->respond(['status' => 400, 'result' => 'Unable to find your cart.']);
}
if($getCart['promo_code_id'] > 0 || $getCart['customer_voucher_list_id'] > 0) {
$getCart['promo_code_id'] = 0;
$getCart['customer_voucher_list_id'] = 0;
$this->carts->update($cart_id, $getCart);
}
$free_items = [];
if(!empty($promo_code)) {
// promo code
$promo = $this->promo_codes->select('promo_codes.*, promo_settings.promo_setting')
->where('promo_codes.code', $promo_code)
->join('promo_settings', 'promo_settings.id = promo_codes.promo_setting_id')
->orderBy('promo_codes.id', 'DESC')->first();
if(empty($promo)) {
return $this->respond(['status' => 400, 'result' => 'Code not found.']);
}
$promoSettingData = json_decode($promo['promo_setting'], true);
$promo_setting = $promoSettingData['Promo'];
// print_r($promo);exit;
//check promo availability
$check_promo = $this->promo_service->checkPromoAvailability($promo['id'], $order_type, $customer_id);
//promo unavailable
if(isset($check_promo['status']) && $check_promo['status'] == 400) {
return $this->respond($check_promo);
}
if($promo_setting['promo_type'] == 'free_item'){
$free_items = [];
$isIncluded = true;
if($promo_setting['filter_type'] == 'item'){
foreach($promo_setting['free_item'] as $free_item_id){
$menu_item = $this->menuItem
->select('menu_items.id, menu_items.title, menu_images.image_url, menu_images.image_url_compressed')
->join('menu_images', 'menu_images.menu_item_id = menu_items.id', 'left')
->where('menu_items.status', 'active')
->find($free_item_id);
if($menu_item){
$free_items[] = [
'id' => $menu_item['id'],
'title' => $menu_item['title'],
'image_url' => getMenuImage($menu_item['id']),
// 'image_url_compressed' => getMenuImage($menu_item['id'])
];
}
}
}
if($promo_setting['filter_type'] == 'category'){
foreach($promo_setting['free_item'] as $free_item_id){
$menu_items = $this->menuItemCategories
->select('menu_items.id, menu_items.title, menu_item_categories.category_id')
->join('menu_items', 'menu_items.id = menu_item_categories.menu_item_id')
->where('menu_item_categories.category_id', $free_item_id)
->where('menu_items.status', 'active')
->findAll();
if($menu_items){
foreach($menu_items as $item){
$menu_item = $this->menuItem
->select('menu_items.id, menu_items.title, menu_images.image_url, menu_images.image_url_compressed')
->join('menu_images', 'menu_images.menu_item_id = menu_items.id', 'left')
->where('menu_items.status', 'active')
->find($item['id']);
if($menu_item){
$free_items[$item['category_id']][] = [
'id' => $menu_item['id'],
'category_id' => $item['category_id'],
'title' => $menu_item['title'],
'image_url' => getMenuImage($menu_item['id']),
// 'image_url_compressed' => getMenuImage($menu_item['id'])
];
}
}
}
}
}
$afterPromo['free_item_list'] = $free_items;
}
//promo available
$this->carts->update($cart_id, ['promo_code_id' => $promo['id'], 'customer_voucher_list_id' => 0]);
// $applyPromoCode = $this->calculate_service->calculateCartTotals($cart_id, $outlet_id, null, null, null, null, null, null, 'promo');
} else if(!empty($voucher_id)){
// use voucher
// please add in voucher_expiry_date
$customer_voucher = $this->customer_voucher_list
->select('customer_voucher_list.*, promo_settings.promo_setting')
->join('promo_settings', 'promo_settings.id = customer_voucher_list.promo_setting_id')
->where('customer_voucher_list.id', $voucher_id)
->first();
if(empty($customer_voucher)){
return $this->respond(['status' => 400, 'result' => 'Voucher not found.']);
}
$promoSettingData = json_decode($customer_voucher['promo_setting'], true);
$promo_setting = $promoSettingData['Promo'];
// print_r($promo);exit;
//check voucher availability
$check_voucher = $this->voucher_service->checkVoucherAvailability($customer_voucher['id'], $order_type, $customer_id);
//voucher unavailable
if(isset($check_voucher['status']) && $check_voucher['status'] == 400) {
return $this->respond($check_voucher);
}
$free_items = [];
if($promo_setting['promo_type'] == 'free_item'){
$isIncluded = true;
if($promo_setting['filter_type'] == 'item'){
foreach($promo_setting['free_item'] as $free_item_id){
$menu_item = $this->menuItem
->select('menu_items.id, menu_items.title, menu_images.image_url, menu_images.image_url_compressed')
->join('menu_images', 'menu_images.menu_item_id = menu_items.id', 'left')
->where('menu_items.status', 'active')
->find($free_item_id);
if($menu_item){
$free_items[] = [
'id' => $menu_item['id'],
'title' => $menu_item['title'],
'image_url' => getMenuImage($menu_item['id']),
// 'image_url_compressed' => getMenuImage($menu_item['id'])
];
}
}
}
if($promo_setting['filter_type'] == 'category'){
foreach($promo_setting['free_item'] as $free_item_id){
$menu_items = $this->menuItemCategories
->select('menu_items.id, menu_items.title, menu_item_categories.category_id')
->join('menu_items', 'menu_items.id = menu_item_categories.menu_item_id')
->where('menu_item_categories.category_id', $free_item_id)
->where('menu_items.status', 'active')
->findAll();
if($menu_items){
foreach($menu_items as $item){
$menu_item = $this->menuItem
->select('menu_items.id, menu_items.title, menu_images.image_url, menu_images.image_url_compressed')
->join('menu_images', 'menu_images.menu_item_id = menu_items.id', 'left')
->where('menu_items.status', 'active')
->find($item['id']);
if($menu_item){
$free_items[$item['category_id']][] = [
'id' => $menu_item['id'],
'category_id' => $item['category_id'],
'title' => $menu_item['title'],
'image_url' => getMenuImage($menu_item['id']),
// 'image_url_compressed' => getMenuImage($menu_item['id'])
];
}
}
}
}
}
}
//voucher available
$this->carts->update($cart_id, ['customer_voucher_list_id' => $customer_voucher['id'], 'promo_code_id' => 0]);
// $applyPromoCode = $this->calculate_service->calculateCartTotals($cart_id, $outlet_id, null, null, null, null, null, null, 'voucher');
}
return $this->respond(['status' => 200, 'result' => 'Voucher applied successfully.', 'data' => $free_items]);
}
public function removeVoucher($customer_id)
{
$cart_id = $this->request->getVar('cart_id');
if(empty($cart_id)){
return $this->respond(['status' => 400, 'result' => 'Cart id is empty.']);
}
$getCart = $this->carts->where('customer_id', $customer_id)->where('status', 'active')->find($cart_id);
if(empty($getCart)) {
return $this->respond(['status' => 400, 'result' => 'Unable to find your cart.']);
}
$this->calculate_service->resetVoucher($cart_id);
return $this->respond(['status' => 200, 'result' => 'Voucher removed successfully.']);
}
}
+91
View File
@@ -0,0 +1,91 @@
<?php
namespace App\Controllers;
use App\Models\BranchDetails;
use App\Models\CustomerDetails;
use App\Models\Invoice;
use App\Models\InvoiceItem;
use App\Models\InvoiceType;
use App\Models\PaymentMethod;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class InvoiceController extends ResourceController
{
private $db;
private $invoice;
private $invoiceType;
private $invoiceItem;
private $customerDetails;
private $paymentMethod;
private $branchDetails;
public function __construct()
{
$this->invoice = new Invoice();
$this->invoiceType = new InvoiceType();
$this->invoiceItem = new InvoiceItem();
$this->customerDetails = new CustomerDetails();
$this->paymentMethod = new PaymentMethod();
$this->branchDetails = new BranchDetails();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->invoice->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No invoices found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->invoice->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Invoice not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->invoice->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Invoice created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Invoice'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->invoice->find($id)) return $this->respond(['status' => 404, 'message' => 'Invoice not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->invoice->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Invoice updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Invoice'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->invoice->find($id)) return $this->respond(['status' => 404, 'message' => 'Invoice not found'], 404);
if ($this->invoice->delete($id)) return $this->respond(['status' => 200, 'message' => 'Invoice deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Invoice'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
+79
View File
@@ -0,0 +1,79 @@
<?php
namespace App\Controllers;
use App\Models\InvoiceItem;
use App\Models\Product;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class InvoiceItemController extends ResourceController
{
private $db;
private $invoiceItem;
private $product;
public function __construct()
{
$this->invoiceItem = new InvoiceItem();
$this->product = new Product();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->invoiceItem->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No invoice items found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->invoiceItem->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Invoice item not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->invoiceItem->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Invoice item created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create invoice item'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->invoiceItem->find($id)) return $this->respond(['status' => 404, 'message' => 'Invoice item not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->invoiceItem->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Invoice item updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update invoice item'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->invoiceItem->find($id)) return $this->respond(['status' => 404, 'message' => 'Invoice item not found'], 404);
if ($this->invoiceItem->delete($id)) return $this->respond(['status' => 200, 'message' => 'Invoice item deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete invoice item'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
+76
View File
@@ -0,0 +1,76 @@
<?php
namespace App\Controllers;
use App\Models\InvoiceType;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class InvoiceTypeController extends ResourceController
{
private $db;
private $invoiceType;
public function __construct()
{
$this->invoiceType = new InvoiceType();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->invoiceType->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No invoice types found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->invoiceType->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Invoice Type not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->invoiceType->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Invoice Type created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Invoice Type'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->invoiceType->find($id)) return $this->respond(['status' => 404, 'message' => 'Invoice Type not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->invoiceType->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Invoice Type updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Invoice Type'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->invoiceType->find($id)) return $this->respond(['status' => 404, 'message' => 'Invoice Type not found'], 404);
if ($this->invoiceType->delete($id)) return $this->respond(['status' => 200, 'message' => 'Invoice Type deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Invoice Type'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
+78
View File
@@ -0,0 +1,78 @@
<?php
namespace App\Controllers;
use App\Models\Letterhead;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
helper('image');
class LetterheadController extends ResourceController
{
private $db;
private $letterhead;
public function __construct()
{
$this->letterhead = new Letterhead();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->letterhead->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No letterheads found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->letterhead->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Letterhead not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->letterhead->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Letterhead created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Letterhead'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->letterhead->find($id)) return $this->respond(['status' => 404, 'message' => 'Letterhead not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->letterhead->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Letterhead updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Letterhead'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->letterhead->find($id)) return $this->respond(['status' => 404, 'message' => 'Letterhead not found'], 404);
if ($this->letterhead->delete($id)) return $this->respond(['status' => 200, 'message' => 'Letterhead deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Letterhead'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
@@ -0,0 +1,76 @@
<?php
namespace App\Controllers;
use App\Models\PaymentMethod;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class PaymentMethodController extends ResourceController
{
private $db;
private $paymentMethod;
public function __construct()
{
$this->paymentMethod = new PaymentMethod();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->paymentMethod->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No payment methods found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->paymentMethod->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Payment Method not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->paymentMethod->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Payment Method created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Payment Method'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->paymentMethod->find($id)) return $this->respond(['status' => 404, 'message' => 'Payment Method not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->paymentMethod->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Payment Method updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Payment Method'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->paymentMethod->find($id)) return $this->respond(['status' => 404, 'message' => 'Payment Method not found'], 404);
if ($this->paymentMethod->delete($id)) return $this->respond(['status' => 200, 'message' => 'Payment Method deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Payment Method'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
+76
View File
@@ -0,0 +1,76 @@
<?php
namespace App\Controllers;
use App\Models\Platform;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class PlatformController extends ResourceController
{
private $db;
private $platform;
public function __construct()
{
$this->platform = new Platform();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->platform->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No platforms found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->platform->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Platform not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->platform->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Platform created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Platform'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->platform->find($id)) return $this->respond(['status' => 404, 'message' => 'Platform not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->platform->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Platform updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Platform'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->platform->find($id)) return $this->respond(['status' => 404, 'message' => 'Platform not found'], 404);
if ($this->platform->delete($id)) return $this->respond(['status' => 200, 'message' => 'Platform deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Platform'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
+76
View File
@@ -0,0 +1,76 @@
<?php
namespace App\Controllers;
use App\Models\Position;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class PositionController extends ResourceController
{
private $db;
private $position;
public function __construct()
{
$this->position = new Position();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->position->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No positions found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->position->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Position not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->position->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Position created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Position'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->position->find($id)) return $this->respond(['status' => 404, 'message' => 'Position not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->position->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Position updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Position'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->position->find($id)) return $this->respond(['status' => 404, 'message' => 'Position not found'], 404);
if ($this->position->delete($id)) return $this->respond(['status' => 200, 'message' => 'Position deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Position'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
@@ -0,0 +1,94 @@
<?php
namespace App\Controllers;
use App\Models\ProductBrand;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class ProductBrandController extends ResourceController
{
private $db;
private $productBrand;
public function __construct()
{
$this->productBrand = new ProductBrand();
$this->db = Config::connect();
}
protected $format = 'json';
public function index()
{
try {
$data = $this->productBrand->findAll();
if (!empty($data)) {
return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
}
return $this->respond(['status' => 200, 'message' => 'No product brands found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->productBrand->find($id);
if ($item) {
return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
}
return $this->respond(['status' => 404, 'message' => 'Product Brand not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$insertId = $this->productBrand->insert($payload);
if ($insertId) {
return $this->respond(['status' => 201, 'message' => 'Product Brand created', 'result' => ['id' => $insertId]], 201);
}
return $this->respond(['status' => 400, 'message' => 'Failed to create Product Brand'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->productBrand->find($id)) {
return $this->respond(['status' => 404, 'message' => 'Product Brand not found'], 404);
}
$payload = $this->request->getJSON(true);
$ok = $this->productBrand->update($id, $payload);
if ($ok) {
return $this->respond(['status' => 200, 'message' => 'Product Brand updated'], 200);
}
return $this->respond(['status' => 400, 'message' => 'Failed to update Product Brand'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->productBrand->find($id)) {
return $this->respond(['status' => 404, 'message' => 'Product Brand not found'], 404);
}
$ok = $this->productBrand->delete($id);
if ($ok) {
return $this->respond(['status' => 200, 'message' => 'Product Brand deleted'], 200);
}
return $this->respond(['status' => 400, 'message' => 'Failed to delete Product Brand'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
@@ -0,0 +1,76 @@
<?php
namespace App\Controllers;
use App\Models\ProductCategory;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class ProductCategoryController extends ResourceController
{
private $db;
private $productCategory;
public function __construct()
{
$this->productCategory = new ProductCategory();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->productCategory->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No product categories found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->productCategory->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Product Category not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->productCategory->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Product Category created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Product Category'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->productCategory->find($id)) return $this->respond(['status' => 404, 'message' => 'Product Category not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->productCategory->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Product Category updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Product Category'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->productCategory->find($id)) return $this->respond(['status' => 404, 'message' => 'Product Category not found'], 404);
if ($this->productCategory->delete($id)) return $this->respond(['status' => 200, 'message' => 'Product Category deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Product Category'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
+91
View File
@@ -0,0 +1,91 @@
<?php
namespace App\Controllers;
use App\Models\Product;
use App\Models\ProductBrand;
use App\Models\ProductCategory;
use App\Models\ProductDescription;
use App\Models\ProductImei;
use App\Models\SupplierDetails;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class ProductController extends ResourceController
{
private $db;
private $product;
private $productBrand;
private $productCategory;
private $productDescription;
private $productImei;
private $supplierDetails;
public function __construct()
{
$this->product = new Product();
$this->productBrand = new ProductBrand();
$this->productCategory = new ProductCategory();
$this->productDescription = new ProductDescription();
$this->productImei = new ProductImei();
$this->supplierDetails = new SupplierDetails();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->product->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No products found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->product->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Product not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->product->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Product created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Product'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->product->find($id)) return $this->respond(['status' => 404, 'message' => 'Product not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->product->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Product updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Product'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->product->find($id)) return $this->respond(['status' => 404, 'message' => 'Product not found'], 404);
if ($this->product->delete($id)) return $this->respond(['status' => 200, 'message' => 'Product deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Product'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
@@ -0,0 +1,76 @@
<?php
namespace App\Controllers;
use CodeIgniter\Database\Config;
use App\Models\ProductDescription;
use CodeIgniter\RESTful\ResourceController;
class ProductDescriptionController extends ResourceController
{
private $db;
private $productDescription;
public function __construct()
{
$this->productDescription = new ProductDescription();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->productDescription->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No product descriptions found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->productDescription->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Product Description not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->productDescription->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Product Description created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Product Description'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->productDescription->find($id)) return $this->respond(['status' => 404, 'message' => 'Product Description not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->productDescription->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Product Description updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Product Description'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->productDescription->find($id)) return $this->respond(['status' => 404, 'message' => 'Product Description not found'], 404);
if ($this->productDescription->delete($id)) return $this->respond(['status' => 200, 'message' => 'Product Description deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Product Description'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
+76
View File
@@ -0,0 +1,76 @@
<?php
namespace App\Controllers;
use App\Models\ProductImei;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class ProductImeiController extends ResourceController
{
private $db;
private $productImei;
public function __construct()
{
$this->productImei = new ProductImei();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->productImei->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No IMEI records found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->productImei->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'IMEI not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->productImei->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'IMEI created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create IMEI'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->productImei->find($id)) return $this->respond(['status' => 404, 'message' => 'IMEI not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->productImei->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'IMEI updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update IMEI'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->productImei->find($id)) return $this->respond(['status' => 404, 'message' => 'IMEI not found'], 404);
if ($this->productImei->delete($id)) return $this->respond(['status' => 200, 'message' => 'IMEI deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete IMEI'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
+120
View File
@@ -0,0 +1,120 @@
<?php
namespace App\Controllers;
use App\Models\BranchDetails;
use App\Models\StaffDetails;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class StaffDetailsController extends ResourceController
{
private $db;
private $staffDetails;
private $position;
private $branchDetails;
public function __construct()
{
$this->staffDetails = new StaffDetails();
$this->branchDetails = new BranchDetails();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->staffDetails->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No staff records found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->staffDetails->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Staff not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
$validationRules = [
'employee_name' => 'required',
'email' => 'required',
'phone_no' => 'required',
'position_id' => 'required',
'username' => 'required',
'password' => 'required',
'branch_id' => 'required',
];
if (!$this->validate($validationRules)) {
$response = [
'status' => 'error',
'message' => 'Validation failed.',
'data' => $this->validator->getErrors()
];
return $this->respond($response, 422);
}
$userData = [
'employee_name' => $this->request->getVar('employee_name'),
'email' => $this->request->getVar('email'),
'phone_no' => $this->request->getVar('phone_no'),
'position_id' => $this->request->getVar('position_id'),
'username' => $this->request->getVar('username'),
'password' => md5($this->request->getVar('password')),
'branch_id' => $this->request->getVar('branch_id'),
];
$id = $this->staffDetails->insert($userData);
if ($id) {
$result = $this->staffDetails->find($id);
$response = [
'status' => 'success',
'message' => 'Staff created successfully.',
'data' => $result
];
return $this->respond($response, 201);
}
$response = [
'status' => 'error',
'message' => 'Failed to create Staff.',
'data' => null
];
return $this->respond($response, 500);
}
public function update($id = null)
{
try {
if (!$this->staffDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Staff not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->staffDetails->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Staff updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Staff'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->staffDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Staff not found'], 404);
if ($this->staffDetails->delete($id)) return $this->respond(['status' => 200, 'message' => 'Staff deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Staff'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
+76
View File
@@ -0,0 +1,76 @@
<?php
namespace App\Controllers;
use App\Models\Stock;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class StockController extends ResourceController
{
private $db;
private $stock;
public function __construct()
{
$this->stock = new Stock();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->stock->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No stock records found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->stock->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Stock not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->stock->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Stock created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Stock'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->stock->find($id)) return $this->respond(['status' => 404, 'message' => 'Stock not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->stock->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Stock updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Stock'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->stock->find($id)) return $this->respond(['status' => 404, 'message' => 'Stock not found'], 404);
if ($this->stock->delete($id)) return $this->respond(['status' => 200, 'message' => 'Stock deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Stock'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
@@ -0,0 +1,76 @@
<?php
namespace App\Controllers;
use App\Models\SupplierDetails;
use CodeIgniter\Database\Config;
use CodeIgniter\RESTful\ResourceController;
class SupplierDetailsController extends ResourceController
{
private $db;
private $supplierDetails;
public function __construct()
{
$this->supplierDetails = new SupplierDetails();
$this->db = Config::connect();
}
public function index()
{
try {
$data = $this->supplierDetails->findAll();
if (!empty($data)) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $data], 200);
return $this->respond(['status' => 200, 'message' => 'No suppliers found'], 200);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function show($id = null)
{
try {
$item = $this->supplierDetails->find($id);
if ($item) return $this->respond(['status' => 200, 'message' => 'OK', 'result' => $item], 200);
return $this->respond(['status' => 404, 'message' => 'Supplier not found'], 404);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function create()
{
try {
$payload = $this->request->getJSON(true);
$id = $this->supplierDetails->insert($payload);
if ($id) return $this->respond(['status' => 201, 'message' => 'Supplier created', 'result' => ['id' => $id]], 201);
return $this->respond(['status' => 400, 'message' => 'Failed to create Supplier'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function update($id = null)
{
try {
if (!$this->supplierDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Supplier not found'], 404);
$payload = $this->request->getJSON(true);
if ($this->supplierDetails->update($id, $payload)) return $this->respond(['status' => 200, 'message' => 'Supplier updated'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to update Supplier'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
public function delete($id = null)
{
try {
if (!$this->supplierDetails->find($id)) return $this->respond(['status' => 404, 'message' => 'Supplier not found'], 404);
if ($this->supplierDetails->delete($id)) return $this->respond(['status' => 200, 'message' => 'Supplier deleted'], 200);
return $this->respond(['status' => 400, 'message' => 'Failed to delete Supplier'], 400);
} catch (\Exception $e) {
return $this->respond(['status' => 500, 'message' => 'Server error'], 500);
}
}
}
+724
View File
@@ -0,0 +1,724 @@
<?php
namespace App\Controllers;
use App\Models\Carts;
use App\Models\State;
use App\Models\Orders;
use App\Models\Outlet;
use App\Models\Country;
use App\Models\Customer;
use App\Models\CartItems;
use App\Models\PromoCode;
use App\Models\OrderItems;
use App\Models\OrderTaxes;
use App\Models\PromoSetting;
use App\Models\CustomerPoint;
use App\Models\OrderPayments;
use App\Models\CustomerWallet;
use App\Models\CartItemOptions;
use App\Models\CustomerAddress;
use App\Models\OrderDeliveries;
use App\Models\OrderItemOptions;
use App\Models\MenuItemVariations;
use App\Models\VoucherRedemptions;
use App\Models\CustomerVoucherList;
use App\Models\LogZeoniq;
use App\Models\SettingCustomerType;
use App\Models\PromoRedemptionRecord;
use App\Models\SettingMembershipTier;
use CodeIgniter\RESTful\ResourceController;
class ZeoniqController extends ResourceController
{
protected $format = 'json'; // Always return JSON
private $customerModel;
private $customerTypeModel;
private $membershipTierModel;
private $customerAddressModel;
private $customerWalletModel;
private $customerPointModel;
private $customerVoucher;
private $promoCode;
private $promoSetting;
private $carts;
private $cart_items;
private $cart_item_options;
private $orders;
private $order_items;
private $order_item_options;
private $order_payments;
private $order_taxes;
private $customer;
private $customer_wallet;
private $db;
private $cart_service;
private $calculate_service;
private $promo_service;
private $outlet;
private $voucher_service;
private $variations;
private $order_deliveries;
private $promo_redemptions_record;
private $voucher_redemptions;
private $zeoniqModel;
public function __construct()
{
$this->customerModel = new Customer();
$this->customerTypeModel = new SettingCustomerType();
$this->membershipTierModel = new SettingMembershipTier();
$this->customerAddressModel = new CustomerAddress();
$this->customerWalletModel = new CustomerWallet();
$this->customerPointModel = new CustomerPoint();
$this->customerVoucher = new CustomerVoucherList();
$this->promoCode = new PromoCode();
$this->promoSetting = new PromoSetting();
$this->carts = new Carts();
$this->cart_items = new CartItems();
$this->cart_item_options = new CartItemOptions();
$this->orders = new Orders();
$this->order_items = new OrderItems();
$this->order_item_options = new OrderItemOptions();
$this->order_payments = new OrderPayments();
$this->order_taxes = new OrderTaxes();
$this->cart_service = service('cartService');
$this->calculate_service = service('calculateService');
$this->promo_service = service('promoService');
$this->voucher_service = service('voucherService');
$this->outlet = new Outlet();
$this->variations = new MenuItemVariations();
$this->order_deliveries = new OrderDeliveries();
$this->promo_redemptions_record = new PromoRedemptionRecord();
$this->voucher_redemptions = new VoucherRedemptions();
$this->zeoniqModel = new LogZeoniq();
}
public function logzeoniq($action, $gross_amount, $net_total, $points_earned, $wallet_used, $voucher_used)
{
$logZeoniqData = [
'action' => $action,
'gross_amount' => $gross_amount,
'net_total' => $net_total,
'points_earned' => $points_earned,
'wallet_used' => $wallet_used,
'voucher_used' => $voucher_used,
'created_at' => date('Y-m-d H:i:s')
];
return $this->zeoniqModel->insert($logZeoniqData);
}
public function getmember()
{
$search_text = $this->request->getGet('SearchText');
if (empty($search_text)) {
return $this->failValidationErrors('Search Text is required.');
}
// ✅ Normalize phone: remove '+' and any non-digit characters
$phone = preg_replace('/\D/', '', $search_text);
// ✅ Search for ONE customer using LIKE on phone
$customer = $this->customerModel
->where('status', 'active')
->like('phone', $phone, 'both') // both = wraps with % %
->orderBy('id', 'ASC')
->first();
if (empty($customer)) {
return $this->respond([
"Code" => "400",
"Message" => "Customer not found.",
"ExceptionMessage" => "Customer{$search_text} not found",
"Params" => $search_text
], 400);
}
// ✅ Address lookup
$array_address = $this->customerAddressModel
->where('customer_id', $customer['id'])
->first();
// ✅ Latest point balance from CustomerPoint model
$membership = $this->membershipTierModel
->where('category_id', $customer['customer_tier_id'])
->first();
$array_member[] = [
"MemberNo" => $customer['id'],
"CardNo" => $customer['customer_referral_code'],
"MemberShipCode" => $customer['customer_tier_id'],
"MembershipLevelDesc" => $membership['name'],
"MemberName" => $customer['name'],
"ExpiryDate" => "",
"DateOfBirth" => ($customer['birthday'] != '0000-00-00' ? $customer['birthday'] : ''),
"Address1" => $array_address['address'] ?? '',
"Address2" => "",
"Address3" => "",
"AddCity" => "",
"AddPostal" => "",
"CountryCode" => "",
"StateCode" => "",
"Email" => $customer['email'],
"MobileNo" => $customer['phone'],
"PhoneNo" => $customer['phone'],
"ICNo" => "",
"PassportNo" => "",
"RefNo" => "",
"PointBalance" => $customer['customer_point'],
"PointValue" => (1/0.01),
"WalletBalance" => $customer['customer_wallet']
];
$requestData = [
'SearchText' => $search_text,
];
$json_member = json_encode($array_member);
return $this->respond([
"Code" => "200",
"Message" => "Successful",
"Member" => $array_member
], 200);
}
public function submitsales()
{
$data = $this->request->getJSON(true);
// Validation data
$rules = [
'MemberNo' => 'required',
'BillNo' => 'required',
'GrossAmt' => 'required',
'DiscAmt' => 'required',
'TaxAmt' => 'required',
'AdjAmt' => 'required',
'NetTotal' => 'required',
'Payments' => 'required',
'Vouchers' => 'required',
];
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$array_point = [];
$this->db = db_connect();
$this->db->transBegin();
try {
$memberNo = $data['MemberNo'];
$billNo = $data['BillNo'];
$grossAmt = $data['GrossAmt'];
$discAmt = $data['DiscAmt'];
$taxAmt = $data['TaxAmt'];
$adjAmt = $data['AdjAmt'];
$netTotal = $data['NetTotal'];
$remarks = $data['Remarks'];
$payments = $data['Payments'];
$vouchers = $data['Vouchers'];
// Validate Member
$customer = $this->customerModel->where('id', $memberNo)->first();
if (!$customer) {
return $this->respond([
"Code" => "400",
"Message" => "Member {$memberNo} not found",
"Params" => $memberNo
], 400);
}
// Get customer's current points and wallet balances
$customer_point_balance = $customer['customer_point'];
$customer_wallet_balance = $customer['customer_wallet'];
$total_wallet = $rewards_point = 0;
// Get wallet usage
if (!empty($payments)) {
foreach ($payments as $value) {
$total_wallet += $value['WalletAmount'] ?? 0;
}
}
// Rewards (points earned from purchase)
$rewards_point = ($netTotal > 0) ? floor($netTotal) : 0;
// Get voucher used
$voucher = null;
if (!empty($vouchers)) {
foreach ($vouchers as $code) {
$voucher = $this->customerVoucher
->where('voucher_code', $code)
->where('customer_id', $memberNo)
->where('voucher_status', 'active')
->first();
if ($voucher) {
break;
}
}
// If vouchers were passed in, but none found in DB → stop
if (!$voucher) {
return $this->respond([
"Code" => "400",
"Message" => "Voucher not found or used",
"Params" => $vouchers
], 400);
}
}
// Save and update voucher redemption if used
if ($voucher) {
$this->customerVoucher
->where('voucher_code', $voucher['voucher_code'])
->where('customer_id', $memberNo)
->where('voucher_status !=', 'used')
->set(['voucher_status' => 'used', 'voucher_order_id' => $billNo])
->update();
}
// Add earned points
if ($rewards_point > 0) {
$customer_point = $customer_point_balance + $rewards_point;
$this->customerModel->update($memberNo, ['customer_point' => $customer_point]);
$this->customerPointModel->insert([
'customer_id' => $memberNo,
'related_id' => $billNo,
'related_type' => 'order',
'action' => 'in',
'current' => $customer_point_balance,
'in' => $rewards_point,
'out' => 0,
'balance' => $customer_point,
'remark' => "{$rewards_point} points earned from order {$billNo}",
]);
}
// Deduct wallet if used
if ($total_wallet > 0) {
$customer_wallet_balance -= $total_wallet;
$this->customerModel->update($memberNo, ['customer_wallet' => $customer_wallet_balance]);
$this->order_payments->update($billNo, [
'status' => 'completed',
'paid_at' => date('Y-m-d H:i:s')
]);
$this->customerWalletModel->insert([
'customer_id' => $memberNo,
'related_id' => $billNo,
'related_type' => 'order',
'action' => 'out',
'current' => $customer['customer_wallet'],
'in' => 0,
'out' => $total_wallet,
'balance' => $customer_wallet_balance,
'remark' => "{$total_wallet} deducted from wallet for order {$billNo}",
]);
}
$point_balance = $customer_point_balance + $rewards_point;
// Record the zeoniq API call
$salesTranId = $this->logzeoniq('integration-submitsales', $grossAmt, $netTotal, $rewards_point, $total_wallet ?? 0, $voucher['voucher_code'] ?? '');
$array_point = [
"SalesTranId" => $salesTranId,
"PointEarned" => number_format($rewards_point, 1),
"PointRedeemed" => number_format($rewards_point, 1),
"PointBalance" => number_format($point_balance, 1),
"WalletBalance" => number_format($customer_wallet_balance, 1)
];
$error = $this->db->error();
if ($error['code'] != 0) {
log_message('error', 'DB ERROR: ' . print_r($error, true));
throw new \Exception("DB Error: " . $error['message']);
}
// Commit transaction
if ($this->db->transStatus() === false) {
$this->db->transRollback();
throw new \Exception("Transaction failed.");
}
$this->db->transCommit();
return $this->respond([
"Code" => "200",
"Message" => "submitsales Successful",
"Data" => $array_point
], 200);
} catch (\Throwable $e) {
$this->db->transRollback();
return $this->respond([
"Code" => "400",
"Message" => $e->getMessage(),
"Params" => $data['MemberNo'] ?? null
], 400);
}
}
public function reversesales() {
$data = $this->request->getJSON(true);
// Validate data
$rules = [
'SalesTranId' => 'required',
'MemberNo' => 'required',
'BillNo' => 'required',
'Vouchers' => 'required',
];
if (!$this->validate($rules)) {
return $this->failValidationErrors($this->validator->getErrors());
}
$this->db = db_connect();
$this->db->transBegin();
try {
$salesTranId = $data['SalesTranId'] ?? null;
$memberNo = $data['MemberNo'] ?? null;
$billNo = $data['BillNo'] ?? null;
$vouchers = $data['Vouchers'] ?? null;
// Validate Member
$customer = $this->customerModel->where('id', $memberNo)->first();
if (!$customer) {
return $this->respond([
"Code" => "400",
"Message" => "Member {$memberNo} not found",
"Params" => $memberNo
], 400);
}
// Get customer's current points and wallet balances
$customer_point_balance = $customer['customer_point'];
$customer_wallet_balance = $customer['customer_wallet'];
$zeoniqLog = $this->zeoniqModel
->where("id", $salesTranId)
->first();
if (!$zeoniqLog) {
return $this->respond([
"Code" => "400",
"Message" => "Order Record not found",
"Params" => $salesTranId
], 400);
}
$points_earned = $zeoniqLog['points_earned'];
$wallet_used = $zeoniqLog['wallet_used'];
// Get voucher used
$voucher = null;
if (!empty($vouchers)) {
foreach ($vouchers as $code) {
$voucher = $this->customerVoucher
->where('voucher_code', $code)
->where('customer_id', $memberNo)
->where('voucher_status', 'used')
->first();
if ($voucher) {
break;
}
}
// If vouchers were passed in, but none found in DB → stop
if (!$voucher) {
return $this->respond([
"Code" => "400",
"Message" => "Voucher not found",
"Params" => $vouchers
], 400);
}
}
// Revert voucher redemption if used
if ($voucher) {
$this->customerVoucher
->where('voucher_code', $voucher['voucher_code'])
->where('customer_id', $memberNo)
->where('voucher_status =', 'used')
->set(['voucher_status' => 'active', 'voucher_order_id' => 0])
->update();
}
// Deduct points
if($points_earned > 0) {
$customer_point = $customer_point_balance - $points_earned;
$this->customerModel->update($memberNo, ['customer_point' => $customer_point]);
$this->customerPointModel->insert([
'customer_id' => $memberNo,
'related_id' => $billNo,
'related_type' => 'order',
'action' => 'out',
'current' => $customer_point_balance,
'in' => 0,
'out' => $points_earned,
'balance' => $customer_point,
'remark' => "{$points_earned} points losed from cancellation of order {$billNo}",
]);
}
// Add back customer's wallet if used
if($wallet_used > 0) {
$out = $wallet_used;
$customer_wallet = $customer_wallet_balance + $out;
$this->customerModel->update($memberNo, ['customer_wallet' => $customer_wallet]);
$this->order_payments->update($billNo, [
'status' => 'cancelled',
'deleted_at' => date('Y-m-d H:i:s')
]);
$this->customerWalletModel->insert([
'customer_id' => $memberNo,
'related_id' => $billNo,
'related_type' => 'order',
'action' => 'in',
'current' => $customer_wallet_balance,
'in' => $out,
'out' => 0,
'balance' => $customer_wallet,
'remark' => "{$out} added to the wallet for cancellation of order {$billNo}",
]);
}
// Record the zeoniq API call
$this->logzeoniq('integration-reversesales', $zeoniqLog['gross_amount'], $zeoniqLog['net_total'], -$points_earned, -($wallet_used ?? 0), $voucher['voucher_code'] ?? '');
$error = $this->db->error();
if ($error['code'] != 0) {
log_message('error', 'DB ERROR: ' . print_r($error, true));
throw new \Exception("DB Error: " . $error['message']);
}
// ✅ Commit transaction
if ($this->db->transStatus() === false) {
$this->db->transRollback();
throw new \Exception("Transaction failed.");
}
$this->db->transCommit();
return $this->respond([
"Code" => "200",
"Message" => "reversesales Successful",
], 200);
} catch (\Throwable $e) {
$this->db->transRollback();
return $this->respond([
"Code" => "400",
"Message" => $e->getMessage(),
"Params" => $data['MemberNo'] ?? null
], 400);
}
}
public function getvoucher()
{
$member_no = $this->request->getGet('MemberNo');
$voucher_no = $this->request->getGet('VoucherNo');
if (empty($member_no)) {
return $this->failValidationErrors('Member No is required.');
}
if (empty($voucher_no)) {
return $this->failValidationErrors('Voucher No is required.');
}
// ✅ Search for customer
$customer = $this->customerModel
->where('status', 'active')
->where('id', $member_no)
->first();
if (empty($customer)) {
return $this->respond([
"Code" => "400",
"Message" => "Customer not found.",
"ExceptionMessage" => "Customer {$member_no} not found",
"Params" => $member_no
], 400);
}
// ✅ Search for voucher
$voucher = $this->customerVoucher
->where('voucher_code', $voucher_no)
->where('customer_id', $member_no)
->first();
if (empty($voucher)) {
return $this->respond([
"Code" => "400",
"Message" => "Voucher not found.",
"ExceptionMessage" => "Voucher {$voucher_no} not found",
"Params" => $voucher_no
], 400);
}
// ✅ Search for promo code
$promo_code = $this->promoCode
->where('promo_setting_id', $voucher['promo_setting_id'])
->first();
// ✅ Search for promo setting
$promo_setting = $this->promoSetting
->where('id', $voucher['promo_setting_id'])
->first();
// ✅ Decode JSON from promo_setting column
$settings = json_decode($promo_setting['promo_setting'], true);
// ✅ Get Promo Amount (default to 0 if not found)
$promo_amount = $settings['Promo']['amount'] ?? 0;
// ✅ Format to 2 decimal places
$promo_amount = number_format((float) $promo_amount, 2, '.', '');
$array_voucher = [
"VoucherNo" => $voucher['voucher_code'],
"ExpiryDate" => $voucher['voucher_expiry_date'],
"VoucherTypeCode" => $promo_code['code'],
"VoucherTypeDesc" => $promo_setting['promotion_type'],
"StatusFlag" => $voucher['voucher_status'],
"VchValue" => $promo_amount,
];
$json_voucher = json_encode($array_voucher);
$requestData = [
'MemberNo' => $member_no,
'VoucherNo' => $voucher_no
];
return $this->respond([
"Code" => "200",
"Message" => "Successful",
"Voucher" => $array_voucher
], 200);
}
public function getvoucherlist()
{
$member_no = $this->request->getGet('MemberNo');
if (empty($member_no)) {
return $this->failValidationErrors('Member No is required.');
}
// ✅ Search for customer
$customer = $this->customerModel
->where('status', 'active')
->where('id', $member_no)
->first();
if (empty($customer)) {
return $this->respond([
"Code" => "400",
"Message" => "Customer not found.",
"ExceptionMessage" => "Customer {$member_no} not found",
"Params" => $member_no
], 400);
}
// ✅ Fetch all vouchers for this customer
$vouchers = $this->customerVoucher
->where('customer_id', $member_no)
->findAll();
if (empty($vouchers)) {
return $this->respond([
"Code" => "400",
"Message" => "No vouchers found.",
"ExceptionMessage" => "No vouchers for Member {$member_no}",
"Params" => $member_no
], 400);
}
$array_vouchers = [];
foreach ($vouchers as $voucher) {
// ✅ Skip if promo_setting_id is 0 or null
if (empty($voucher['promo_setting_id']) || $voucher['promo_setting_id'] == 0) {
continue;
}
// ✅ Get promo code
$promo_code = $this->promoCode
->where('promo_setting_id', $voucher['promo_setting_id'])
->first();
// ✅ Get promo setting
$promo_setting = $this->promoSetting
->where('id', $voucher['promo_setting_id'])
->first();
if (empty($promo_setting)) {
continue; // skip if no promo_setting found
}
// ✅ Decode JSON settings
$settings = json_decode($promo_setting['promo_setting'], true);
// ✅ Get promo amount, format to 2 decimals
$promo_amount = $settings['Promo']['amount'] ?? 0;
$promo_amount = number_format((float) $promo_amount, 2, '.', '');
$array_vouchers[] = [
"VoucherNo" => $voucher['voucher_code'],
"ExpiryDate" => $voucher['voucher_expiry_date'],
"VoucherTypeCode" => $promo_code['code'] ?? '',
"VoucherTypeDesc" => $promo_setting['promotion_type'] ?? '',
"StatusFlag" => $voucher['voucher_status'],
"VchValue" => $promo_amount,
];
}
if (empty($array_vouchers)) {
return $this->respond([
"Code" => "400",
"Message" => "No valid vouchers found.",
"ExceptionMessage" => "All vouchers skipped for Member {$member_no}",
"Params" => $member_no
], 400);
}
$json_vouchers = json_encode($array_vouchers);
$requestData = [
'MemberNo' => $member_no
];
return $this->respond([
"Code" => "200",
"Message" => "Successful",
"Vouchers" => $array_vouchers
], 200);
}
}