diff --git a/app/Config/Routes.php b/app/Config/Routes.php
index 6e7b375a..37e7f840 100755
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -914,7 +914,7 @@ $routes->group('sales', function($routes) {
$routes->get('/', 'SalesController::index');
$routes->get('loadactivities', 'SalesController::loadactivities');
-
+
$routes->get('loadtargets', 'SalesController::loadtargets');
$routes->get('page/(:segment)', 'SalesController::noPage/$1');
@@ -997,6 +997,13 @@ $routes->group('sales', function($routes) {
// Delete note
$routes->delete('notes/(:num)', 'SalesController::deleteNote/$1');
+ // ==================== TARGETS ROUTES ====================
+ $routes->get('targets', 'SalesController::getTargets');
+ $routes->get('targets/user/(:num)', 'SalesController::getTargetByUser/$1');
+ $routes->get('targets/fy/(:segment)','SalesController::getTargetByFY/$1');
+ $routes->post('targets', 'SalesController::createTarget');
+ $routes->put('targets/(:num)', 'SalesController::updateTarget/$1');
+ $routes->delete('targets/(:num)', 'SalesController::deleteTarget/$1');
//Dashboard
$routes->get('dashboard', 'SalesController::dashboard');
diff --git a/app/Controllers/SalesController.php b/app/Controllers/SalesController.php
index dccd9d91..44ea7aae 100644
--- a/app/Controllers/SalesController.php
+++ b/app/Controllers/SalesController.php
@@ -7,6 +7,7 @@ use App\Models\SalesActualLeadModel;
use App\Models\SalesContactPersonModel;
use App\Models\SalesActivityModel;
use App\Models\SalesLeadNoteModel;
+use App\Models\SalesTargetModel;
use App\Models\UserModel;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\API\ResponseTrait;
@@ -20,6 +21,8 @@ class SalesController extends BaseController
protected $activityModel;
protected $noteModel;
protected $userModel;
+ protected $targetModel;
+
public function __construct()
{
@@ -28,6 +31,7 @@ class SalesController extends BaseController
$this->activityModel = new SalesActivityModel();
$this->noteModel = new SalesLeadNoteModel();
$this->userModel = new UserModel();
+ $this->targetModel = new SalesTargetModel();
}
@@ -760,6 +764,138 @@ class SalesController extends BaseController
}
}
+ // ==================== SALES TARGET APIs ====================
+ /**
+ * Get all sales targets
+ * GET /api/sales/targets
+ */
+ public function getTargets()
+ {
+ try {
+ $targets = $this->targetModel->findAll();
+ return $this->respond([
+ 'status' => 'success',
+ 'data' => $targets
+ ]);
+ } catch (\Exception $e) {
+ return $this->fail($e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ /**
+ * Get sales target by user
+ * GET /api/sales/targets/user/{userId}
+ */
+ public function getTargetByUser($userId)
+ {
+ try {
+ $targets = $this->targetModel->where('user_id', $userId)->findAll();
+ return $this->respond([
+ 'status' => 'success',
+ 'data' => $targets
+ ]);
+ } catch (\Exception $e) {
+ return $this->fail($e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ /**
+ * Get sales target by FY year
+ * GET /api/sales/targets/fy/{fyYear}
+ */
+ public function getTargetByFY($fyYear)
+ {
+ try {
+ $targets = $this->targetModel->where('fy_year', $fyYear)->findAll();
+ return $this->respond([
+ 'status' => 'success',
+ 'data' => $targets
+ ]);
+ } catch (\Exception $e) {
+ return $this->fail($e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ /**
+ * Create sales target
+ * POST /api/sales/targets
+ */
+ public function createTarget()
+ {
+ try {
+ $data = $this->request->getJSON(true);
+ $data['created_by'] = $this->getUserId();
+ $data['updated_by'] = $this->getUserId();
+
+ if (!$this->targetModel->insert($data)) {
+ return $this->fail($this->targetModel->errors(), ResponseInterface::HTTP_BAD_REQUEST);
+ }
+
+ $targetId = $this->targetModel->getInsertID();
+ $target = $this->targetModel->find((int)$targetId);
+
+ return $this->respondCreated([
+ 'status' => 'success',
+ 'message' => 'Sales target created successfully',
+ 'data' => $target
+ ]);
+ } catch (\Exception $e) {
+ return $this->fail($e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ /**
+ * Update sales target
+ * PUT /api/sales/targets/{id}
+ */
+ public function updateTarget($id)
+ {
+ try {
+ $target = $this->targetModel->find((int)$id);
+ if (!$target) {
+ return $this->failNotFound('Sales target not found');
+ }
+
+ $data = $this->request->getJSON(true);
+ $data['updated_by'] = $this->getUserId();
+
+ if (!$this->targetModel->update($id, $data)) {
+ return $this->fail($this->targetModel->errors(), ResponseInterface::HTTP_BAD_REQUEST);
+ }
+
+ $updatedTarget = $this->targetModel->find((int)$id);
+ return $this->respond([
+ 'status' => 'success',
+ 'message' => 'Sales target updated successfully',
+ 'data' => $updatedTarget
+ ]);
+ } catch (\Exception $e) {
+ return $this->fail($e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ /**
+ * Delete sales target
+ * DELETE /api/sales/targets/{id}
+ */
+ public function deleteTarget($id)
+ {
+ try {
+ $target = $this->targetModel->find((int)$id);
+ if (!$target) {
+ return $this->failNotFound('Sales target not found');
+ }
+
+ $this->targetModel->delete((int)$id);
+ return $this->respondDeleted([
+ 'status' => 'success',
+ 'message' => 'Sales target deleted successfully'
+ ]);
+ } catch (\Exception $e) {
+ return $this->fail($e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
+ }
+ }
+
// ==================== HELPER METHODS ====================
/**
diff --git a/app/Models/SalesTargetModel.php b/app/Models/SalesTargetModel.php
new file mode 100644
index 00000000..f23193f6
--- /dev/null
+++ b/app/Models/SalesTargetModel.php
@@ -0,0 +1,38 @@
+ 'required|integer',
+ 'fy_year' => 'required|max_length[9]',
+ 'target_amount' => 'required|decimal',
+ ];
+
+ protected $validationMessages = [
+ 'user_id' => ['required' => 'User is required'],
+ 'fy_year' => ['required' => 'Financial year is required'],
+ 'target_amount' => ['required' => 'Target amount is required', 'decimal' => 'Target amount must be a valid number'],
+ ];
+}
\ No newline at end of file
diff --git a/app/Views/sales/target_view.php b/app/Views/sales/target_view.php
new file mode 100644
index 00000000..a500a2ba
--- /dev/null
+++ b/app/Views/sales/target_view.php
@@ -0,0 +1,581 @@
+
+
+
+
+
+
+
+
+
+
+
+ | Person |
+ Action |
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ | Financial Year |
+ Amount |
+ Action |
+
+
+
+ | No targets added yet. |
+
+
+
+
+
+
+
+
\ No newline at end of file