193 lines
7.3 KiB
PHP
193 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Controllers\BaseController;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use CodeIgniter\API\ResponseTrait;
|
|
use App\Models\InvoiceItemModel;
|
|
use App\Models\InvoiceModel;
|
|
use App\Models\InvoiceUtrModel;
|
|
use App\Models\PolicyTransactionModel;
|
|
|
|
class PayoutController extends BaseController
|
|
{
|
|
use ResponseTrait;
|
|
protected $myLogger;
|
|
protected $invoiceItemModel;
|
|
protected $invoiceModel;
|
|
protected $invoiceUtrModel;
|
|
protected $policyTransactionModel;
|
|
protected $payout_status;
|
|
|
|
public function __construct()
|
|
{
|
|
set_session_context('PayoutController');
|
|
$this->myLogger = \Config\Services::mylogger();
|
|
|
|
$this->payout_status = [
|
|
1 => "Draft",
|
|
2 => "Pending",
|
|
3 => "Complete",
|
|
];
|
|
|
|
$this->invoiceItemModel = new InvoiceItemModel();
|
|
$this->invoiceModel = new InvoiceModel();
|
|
$this->invoiceUtrModel = new InvoiceUtrModel();
|
|
$this->policyTransactionModel = new PolicyTransactionModel();
|
|
}
|
|
|
|
public function payoutList()
|
|
{
|
|
// for filtering list
|
|
if($this->request->is('post')){
|
|
try{
|
|
$data = $this->request->getPost();
|
|
// print_r($data); die;
|
|
|
|
$agent_id = $data['agent_id'] ?? null;
|
|
$status_id = $data['status_id'] ?? null;
|
|
$start_date = $data['start_date'] ?? null;
|
|
$end_date = $data['end_date'] ?? null;
|
|
|
|
$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);
|
|
|
|
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);
|
|
}
|
|
}catch (\Throwable $th) {
|
|
|
|
$this->myLogger->logme("error", "PayoutController - payoutList: Exception: " . $th->getMessage() . " --- Line: " . $th->getLine() . " --- Trace: " . $th->getTraceAsString());
|
|
$errorData = [
|
|
'message' => $th->getMessage(),
|
|
'file' => $th->getFile(),
|
|
'line' => $th->getLine(),
|
|
'code' => $th->getCode(),
|
|
'trace' => $th->getTraceAsString(),
|
|
'trace_array' => $th->getTrace(), // full array version (optional)
|
|
'function' => $th->getTrace()[0]['function'] ?? null,
|
|
'class' => $th->getTrace()[0]['class'] ?? null,
|
|
];
|
|
$payout_data = view('payout_list');
|
|
return $this->respond(['status' => false, 'code' => 500, 'data' => $payout_data, "message" => "No data found", 'error_data' => $errorData], 500);
|
|
}
|
|
}
|
|
|
|
// for list
|
|
$data['payout_status'] = $this->payout_status;
|
|
$data['agent_list'] = $this->invoiceModel->agentList();
|
|
$data['page_name'] = "Payout";
|
|
$payout_data['payout_list_data'] = $this->invoiceModel->invoiceList();
|
|
$data['payout_list'] = view('payout_list', $payout_data);
|
|
|
|
// dd($data);
|
|
return $this->loadLayout('payout_list_handler', $data);
|
|
}
|
|
|
|
public function fetchUtrDetails()
|
|
{
|
|
$invoice_id = $this->request->getPost('invoice_id') ?? null;
|
|
$payout_data = $this->constructUtrDetails($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);
|
|
}
|
|
}
|
|
|
|
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($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);
|
|
}
|
|
}
|
|
|
|
|
|
}
|