diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 3efa4981..9b083b0f 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -772,5 +772,6 @@ $routes->group('payout', function($routes) { $routes->match (['get','post'],'list',"PayoutController::payoutList"); $routes->post('fetchUtrDetails',"PayoutController::fetchUtrDetails"); $routes->post('saveUtrDetails',"PayoutController::saveUtrDetails"); + $routes->post('removeUtrDetails',"PayoutController::removeUtrDetails"); }); diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 0bed75f5..87ec5222 100755 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -5673,6 +5673,10 @@ class ClientController extends AdminController // $response = $ticketServiceController->extractExcelData("claims_dump_form_client.xlsx"); // dd($response); + // $TicketController = new TicketController(); + // $response = $TicketController->getMoreInfo($requestFrom = 'rest', $ticket_id = 70); + // dd($response); + // ---------- EMP SERVICE CONTROLLER -------------------------------------------------------------------------------- $empServiceController = new EmployeeServiceController(); diff --git a/app/Controllers/PayoutController.php b/app/Controllers/PayoutController.php index 7b798537..06762fd8 100644 --- a/app/Controllers/PayoutController.php +++ b/app/Controllers/PayoutController.php @@ -50,7 +50,7 @@ class PayoutController extends BaseController $start_date = $data['start_date'] ?? null; $end_date = $data['end_date'] ?? null; - $payout_data = $this->invoiceModel->invoiceList($this->payout_status, $agent_id, $status_id, $start_date, $end_date); + $payout_data = $this->invoiceModel->invoiceList($agent_id, $status_id, $start_date, $end_date); $payout_data['payout_list_data'] = $payout_data; $payout_data = view('payout_list', $payout_data); @@ -81,7 +81,7 @@ class PayoutController extends BaseController // for list $data['payout_status'] = $this->payout_status; $data['agent_list'] = $this->invoiceModel->agentList(); - $payout_data['payout_list_data'] = $this->invoiceModel->invoiceList($this->payout_status); + $payout_data['payout_list_data'] = $this->invoiceModel->invoiceList(); $data['payout_list'] = view('payout_list', $payout_data); // dd($data); @@ -90,23 +90,101 @@ class PayoutController extends BaseController public function fetchUtrDetails() { - $invoice_id = $this->request->getPost('invoice_id') ?? null; + $invoice_id = $this->request->getPost('invoice_id') ?? null; + $payout_data = $this->constructUtrDetails($invoice_id); - $payout_data = $this->invoiceModel->invoiceList(); + if(!empty($payout_data)){ + return $this->respond(['status' => true, 'code' => 200, 'data' => $payout_data], 200); + }else{ + return $this->respond(['status' => false, 'code' => 400, 'data' => $payout_data, "message" => "No data found"], 200); + } + } - $data['utr_data'] = $payout_data; - $payout_data = view('partner_utr_details', $data); + public function constructUtrDetails($invoice_id) + { + $utr_data = $this->invoiceUtrModel->where('is_active', 1)->where('invoice_id', $invoice_id)->findAll(); + $summary_data = $this->invoiceModel->utrSummary($invoice_id); - if(!empty($payout_data)){ - return $this->respond(['status' => true, 'code' => 200, 'data' => $payout_data], 200); - }else{ - return $this->respond(['status' => false, 'code' => 400, 'data' => $payout_data, "message" => "No data found"], 200); - } + if($summary_data['invoice_amount'] == $summary_data['total_utr_amount']) { + $data['invoice_completed'] = true; + } + + $data['utr_list_data'] = $utr_data; + $data['summary'] = $summary_data; + $data = view('payout_utr_details', $data); + + return $data; } public function saveUtrDetails() { $data = $this->request->getPost(); + $invoice_id = $this->request->getPost('invoice_id') ?? null; + $utr_id = $this->request->getPost('utr_pk') ?? null; + + if(isset($data['utr_date'])){ + $data['utr_date'] = change_date_format($data['utr_date']); + } + + + if(!empty($utr_id)){ + + $update = $this->invoiceUtrModel->where('id', $utr_id)->set($data)->update(); + $payout_edit_data = $this->constructUtrDetails($invoice_id); + + if($update){ + + $summary_data = $this->invoiceModel->utrSummary($invoice_id); + if($summary_data['invoice_amount'] == $summary_data['total_utr_amount']){ + $sql = "UPDATE partner_invoice SET payout_status = 2 WHERE id = ?"; + db_connect()->query($sql, [$invoice_id]); + } + + return $this->respond(['status' => true, 'code' => 200, 'data' => $payout_edit_data, "message" => "UTR successfully updated"], 200); + }else{ + return $this->respond(['status' => false, 'code' => 400, 'data' => $payout_edit_data, "message" => "Failed to update UTR"], 200); + } + + }else{ + + unset($data['utr_pk']); + $insert_id = $this->invoiceUtrModel->insert($data); + $payout_data = $this->constructUtrDetails($invoice_id); + + if($insert_id){ + + $summary_data = $this->invoiceModel->utrSummary($invoice_id); + if($summary_data['invoice_amount'] == $summary_data['total_utr_amount']){ + $sql = "UPDATE partner_invoice SET payout_status = 2 WHERE id = ?"; + db_connect()->query($sql, [$invoice_id]); + } + + return $this->respond(['status' => true, 'code' => 200, 'data' => $payout_data, "message" => "UTR added successfully"], 200); + }else{ + return $this->respond(['status' => false, 'code' => 400, 'data' => $payout_data, "message" => "Failed to add UTR"], 200); + } + } + } + + public function removeUtrDetails() + { + $data = $this->request->getPost(); + + if(isset($data['utr_id'])){ + + $sql = "UPDATE partner_invoice_utr SET is_active = 0 WHERE id = ?"; + $update = db_connect()->query($sql, [$data['utr_id']]); + + $payout_data = $this->constructUtrDetails($data['invoice_id'] ?? ""); + + if($update){ + return $this->respond(['status' => true, 'code' => 200, 'data' => $payout_data, "message" => "UTR removed successfully"], 200); + }else{ + return $this->respond(['status' => false, 'code' => 400, 'data' => $payout_data, "message" => "Failed to remove UTR"], 200); + } + }else { + return $this->respond(['status' => false, 'code' => 500, 'data' => "", "message" => "Failed to remove UTR"], 200); + } } diff --git a/app/Models/InvoiceModel.php b/app/Models/InvoiceModel.php index 2a3b71e0..49d91ced 100644 --- a/app/Models/InvoiceModel.php +++ b/app/Models/InvoiceModel.php @@ -41,8 +41,41 @@ class InvoiceModel extends Model protected $validationMessages = []; protected $skipValidation = false; + // Callbacks + protected $allowCallbacks = true; + protected $beforeInsert = ["checkAndADDCreatedByValue"]; + protected $afterInsert = []; + protected $beforeUpdate = ["checkAndUpdateUpdatedByValue"]; + protected $afterUpdate = []; + protected $beforeFind = []; + protected $afterFind = []; + protected $beforeDelete = []; + protected $afterDelete = []; - public function invoiceList($status = [], $agent_id = null, $status_id = null, $start_date = null, $end_date = null) + protected function checkAndADDCreatedByValue(array $data) + { + // Check if 'updated_by' value is null or empty + if (empty($data['data']['created_by'])) { + // Set 'updated_by' value to the current session user ID + $data['data']['created_by'] = get_session_userid(); + } + + return $data; + } + + protected function checkAndUpdateUpdatedByValue(array $data) + { + // Check if 'updated_by' value is null or empty + if (empty($data['data']['updated_by'])) { + // Set 'updated_by' value to the current session user ID + $data['data']['updated_by'] = get_session_userid(); + } + + return $data; + } + + + public function invoiceList($agent_id = null, $status_id = null, $start_date = null, $end_date = null) { $data = $this->select(" partner_invoice.*, @@ -67,9 +100,8 @@ class InvoiceModel extends Model -- Payout status CASE - WHEN payout_status = 1 THEN 'Draft' - WHEN payout_status = 2 THEN 'Pending' - WHEN payout_status = 3 THEN 'Complete' + WHEN payout_status = 1 THEN 'Pending' + WHEN payout_status = 2 THEN 'Complete' END AS status_text, partner_agent.name as agent_name @@ -111,7 +143,48 @@ class InvoiceModel extends Model return $return_data; } - public function agentList(){ + public function agentList() + { return $this->db->table('partner_agent')->where('is_active', 1)->get()->getResultArray(); } + + public function utrSummary($invoice_id) + { + $data = $this->select(" + + partner_invoice.*, + + -- Total UTR Amount + (SELECT SUM(piu.amount) + FROM partner_invoice_utr piu + WHERE piu.invoice_id = partner_invoice.id + AND piu.is_active = 1 + ) AS total_utr_amount, + + -- Balance Amount + (partner_invoice.invoice_amount - + IFNULL( + (SELECT SUM(piu2.amount) + FROM partner_invoice_utr piu2 + WHERE piu2.invoice_id = partner_invoice.id + AND piu2.is_active = 1 + ), + 0) + ) AS balance_amount, + + -- Payout status + CASE + WHEN payout_status = 1 THEN 'Pending' + WHEN payout_status = 2 THEN 'Complete' + END AS status_text, + + partner_agent.name as agent_name + ") + ->join('partner_agent', 'partner_invoice.agent_id = partner_agent.id') + ->where('partner_invoice.is_active', 1) + ->where('partner_invoice.id', $invoice_id) + ->first(); + + return $data; + } } diff --git a/app/Models/InvoiceUtrModel.php b/app/Models/InvoiceUtrModel.php index 9f0c44c4..a9da5960 100644 --- a/app/Models/InvoiceUtrModel.php +++ b/app/Models/InvoiceUtrModel.php @@ -17,6 +17,7 @@ class InvoiceUtrModel extends Model 'invoice_id', 'utr_no', 'amount', + 'utr_date', 'is_active', 'created_at', 'created_by', @@ -36,4 +37,37 @@ class InvoiceUtrModel extends Model protected $validationMessages = []; protected $skipValidation = false; + + // Callbacks + protected $allowCallbacks = true; + protected $beforeInsert = ["checkAndADDCreatedByValue"]; + protected $afterInsert = []; + protected $beforeUpdate = ["checkAndUpdateUpdatedByValue"]; + protected $afterUpdate = []; + protected $beforeFind = []; + protected $afterFind = []; + protected $beforeDelete = []; + protected $afterDelete = []; + + protected function checkAndADDCreatedByValue(array $data) + { + // Check if 'updated_by' value is null or empty + if (empty($data['data']['created_by'])) { + // Set 'updated_by' value to the current session user ID + $data['data']['created_by'] = get_session_userid(); + } + + return $data; + } + + protected function checkAndUpdateUpdatedByValue(array $data) + { + // Check if 'updated_by' value is null or empty + if (empty($data['data']['updated_by'])) { + // Set 'updated_by' value to the current session user ID + $data['data']['updated_by'] = get_session_userid(); + } + + return $data; + } } diff --git a/app/Views/payout_list.php b/app/Views/payout_list.php index 4b4eb477..d911715d 100644 --- a/app/Views/payout_list.php +++ b/app/Views/payout_list.php @@ -38,21 +38,145 @@ border-left: 4px solid #2196F3; } + + + + +
@@ -94,7 +218,7 @@
-