diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 50fe8953..d177174c 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -147,8 +147,8 @@ $routes->get('configurationctrl/reActivateConfigValue', 'Configurationctrl::reAc $routes->get('configurationctrl/editconfig', 'Configurationctrl::editconfig'); $routes->post('configurationctrl/updateconfig', 'Configurationctrl::updateconfig'); $routes->post('configurationctrl/configNameCheck', 'Configurationctrl::configNameCheck'); -$routes->get('configurationctrl/deleteConfig', 'configurationctrl::deleteConfig'); -$routes->get('configurationctrl/reActivateConfig', 'configurationctrl::reActivateConfig'); +$routes->get('configurationctrl/deleteConfig', 'Configurationctrl::deleteConfig'); +$routes->get('configurationctrl/reActivateConfig', 'Configurationctrl::reActivateConfig'); // Department Master Routes $routes->get('departmentListing', 'Department::index'); @@ -304,7 +304,6 @@ $routes->post('GetIGRLineItemDetails', 'Inwardgateregister::getIGRLineItemDetail $routes->post('SaveIGR', 'Inwardgateregister::SaveIGR');// Save IGR PAGE - Working $routes->post('UpdateIGR', 'Inwardgateregister::UpdateIGR'); $routes->post('updateIGRWeight', 'Inwardgateregister::updateIGRWeight'); -$routes->post('UpdateIGRLineItem', 'Inwardgateregister::updateIGRLineItem'); $routes->post('ViewIGRHistory', 'Inwardgateregister::ViewIGRHistory'); $routes->post('getCostCenterData', 'Inwardgateregister::getCostCenterData'); $routes->post('ViewIGRfile', 'Inwardgateregister::ViewIGRfile'); diff --git a/app/Controllers/Inwardgateregister.php b/app/Controllers/Inwardgateregister.php index 988f4ce5..d13d0063 100755 --- a/app/Controllers/Inwardgateregister.php +++ b/app/Controllers/Inwardgateregister.php @@ -450,6 +450,7 @@ class Inwardgateregister extends BaseController $IGRNO = $this->inwardgateregister_model->addigrM($igr); if($IGRNO){ + $files = $this->request->getFiles(); $fileNames = $this->request->getPost('file_name'); // Get user-entered file names if(isset($files['emp_file'])) @@ -476,19 +477,17 @@ class Inwardgateregister extends BaseController } } - #Step 5 Save Master Detail On History Table - if($VehicleNo){ - $hist = array('key' => 'VehicleNo', 'value' => $VehicleNo, 'igr_no' => $IGRNO, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } - if($DriverName){ - $hist = array('key' => 'DriverName', 'value' => $DriverName, 'igr_no' => $IGRNO, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } - if($DriverMobileNumber){ - $hist = array('key' => 'DriverMobileNumber', 'value' => $DriverMobileNumber, 'igr_no' => $IGRNO, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } + #Step 5 Save Master Detail On Stored on History Table + foreach ($igr as $key => $value) { + if (!empty($value)) { // Ensure the value is not empty + if($key == 'CreatedBy'){ $key = 'MasterCreatedBy'; } + if($key == 'UpdateBY'){ $key = 'MasterUpdatedBy'; } + if($key == 'CreatedDate'){ $key = 'MasterCreatedDate'; } + if($key == 'UpdatedOn'){ $key = 'MasterUpdatedDate'; } + $hist = array( 'key' => $key, 'value' => $value, 'igr_no' => $IGRNO,'igr_item_no' => NULL); + $this->saveIGRHistory($hist); + } + } #Step 6 Updating OGR Details $OGRStatus = $IGRStatus; @@ -580,33 +579,17 @@ class Inwardgateregister extends BaseController } $historydetails = array('MaterialCode' => $MaterialCode, 'Transaction_type' => "Add", 'Ref_Type' => "IGR", 'Ref_No' => $igrlineno, 'Quantity' => $QuantityAsPerInvoice, 'CreatedBy' => $CreatedBy, 'CreatedOn' => $createddt, 'SupplierID' => $SupplierId, 'ItemValue' => $itemvalue); $this->inwardgateregister_model->addmaterialhistory($historydetails); - if($GrossWeight){ - $hist = array('key' => 'GrossWeight', 'value' => $GrossWeight, 'igr_no' => $IGRNO, 'igr_item_no' => $igrlineno); - $this->saveIGRHistory($hist); - } - if($TareWeight){ - $hist = array('key' => 'TareWeight', 'value' => $TareWeight, 'igr_no' => $IGRNO, 'igr_item_no' => $igrlineno); - $this->saveIGRHistory($hist); - } - if($NetWeight){ - $hist = array('key' => 'NetWeight', 'value' => $NetWeight, 'igr_no' => $IGRNO, 'igr_item_no' => $igrlineno); - $this->saveIGRHistory($hist); - } - if($GrossWeightDate){ - $hist = array('key' => 'GrossWeightDate', 'value' => $GrossWeightDate, 'igr_no' => $IGRNO, 'igr_item_no' => $igrlineno); - $this->saveIGRHistory($hist); - } - if($TareWeightDate){ - $hist = array('key' => 'TareWeightDate', 'value' => $TareWeightDate, 'igr_no' => $IGRNO, 'igr_item_no' => $igrlineno); - $this->saveIGRHistory($hist); - } - if($QuantityAsPerInvoice){ - $hist = array('key' => 'QuantityAsPerInvoice', 'value' => $QuantityAsPerInvoice, 'igr_no' => $IGRNO, 'igr_item_no' => $igrlineno); - $this->saveIGRHistory($hist); - } - if($Remarks){ - $hist = array('key' => 'Remarks', 'value' => $Remarks, 'igr_no' => $IGRNO, 'igr_item_no' => $igrlineno); - $this->saveIGRHistory($hist); + #Save IGR Detail On History Table + foreach ($igrDetails as $key => $value) { + if (!empty($value)) { // Ensure the value is not empty + if($key == 'CreatedBy'){ $key = 'LineItemCreatedBy'; } + if($key == 'UpdateBY'){ $key = 'LineItemUpdatedBy'; } + if($key == 'IGRNO'){ $key = 'LineItemIGRNO'; } + if($key == 'CreatedDate'){ $key = 'LineItemCreatedDate'; } + if($key == 'UpdatedOn'){ $key = 'LineItemUpdatedDate'; } + $hist = array( 'key' => $key, 'value' => $value, 'igr_no' => $IGRNO,'igr_item_no' => $igrlineno); + $this->saveIGRHistory($hist); + } } $get_pre_qty = $this->inwardgateregister_model->get_CurrentQty($MaterialCode); $av_qty = 0; //$av_qty means Avaliable Quantity @@ -618,7 +601,7 @@ class Inwardgateregister extends BaseController $currentav_qty = (int)$QuantityAsPerInvoice + (int)$av_qty; $current_qty = array('Current_stock' => $currentav_qty); $this->inwardgateregister_model->addmaterialmaster($current_qty, $MaterialCode); - $Recqty = $this->inwardgateregister_model->getPOLineItemReceivedQty($PONO, $MaterialCode); + $Recqty = $this->inwardgateregister_model->getPOLineItemReceivedQty($PONO, $MaterialCode); $ReceivedQuantity = 0.00; if (count($Recqty) > 0) { foreach ($Recqty as $key) { @@ -855,18 +838,19 @@ function viewIGRDetails() $updateigrmaster = $this->inwardgateregister_model->updateigr($igrarr, $IGRNo); log_message('error', "Master updated successfully: " . $updateigrmaster); if(!empty($tableData )){ - if($VehicleNo){ - $hist = array('key' => 'VehicleNo', 'value' => $VehicleNo, 'igr_no' => $IGRNo, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } - if($DriverName){ - $hist = array('key' => 'DriverName', 'value' => $DriverName, 'igr_no' => $IGRNo, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } - if($DriverMobileNumber){ - $hist = array('key' => 'DriverMobileNumber', 'value' => $DriverMobileNumber, 'igr_no' => $IGRNo, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); + #Save IGR Master On History Table + foreach ($igrarr as $key => $value) { + + if (!empty($value)) { // Ensure the value is not empty + if($key == 'CreatedBy'){ $key = 'MasterCreatedBy'; } + if($key == 'UpdateBY'){ $key = 'MasterUpdatedBy'; } + if($key == 'CreatedDate'){ $key = 'MasterCreatedDate'; } + if($key == 'UpdatedOn'){ $key = 'MasterUpdatedDate'; } + $hist = array( 'key' => $key, 'value' => $value, 'igr_no' => $IGRNo,'igr_item_no' => NULL); + $this->saveIGRHistory($hist); + } } + foreach ($tableData as $row) { $quantityAsPerInvoice = $row['QuantityAsPerInvoice']; $remarks = $row['Remarks']; @@ -906,63 +890,76 @@ function viewIGRDetails() // } $updateigrlineitem = $this->inwardgateregister_model->updateIGRLineItem($igrline, $igrItemNo); - log_message('error', "lineitem updated successfully: " . $updateigrlineitem); - log_message('error', 'UpdateIGR updateigrlineitem: ' . json_encode($igrline)); - - if ($quantityAsPerInvoice) { - $hist = array('key' => 'QuantityAsPerInvoice', 'value' => $quantityAsPerInvoice, 'igr_no' => $IGRNo, 'igr_item_no' => $igrItemNo); - $this->saveIGRHistory($hist); + if($updateigrlineitem){ + foreach ($igrline as $key => $value) { + if (!empty($value)) { // Ensure the value is not empty + if($key == 'CreatedBy'){ $key = 'LineItemCreatedBy'; } + if($key == 'UpdateBY'){ $key = 'LineItemUpdatedBy'; } + if($key == 'IGRNO'){ $key = 'LineItemIGRNO'; } + + $hist = array( 'key' => $key, 'value' => $value, 'igr_no' => $IGRNo,'igr_item_no' => NULL); + $this->saveIGRHistory($hist); + } } - if($remarks){ - $hist = array('key' => 'Remarks', 'value' => $remarks, 'igr_no' => $IGRNo, 'igr_item_no' => $igrItemNo); - $this->saveIGRHistory($hist); } if($grossWeight){ $hist = array('key' => 'GrossWeight', 'value' => $grossWeight, 'igr_no' => $IGRNo, 'igr_item_no' => $igrItemNo); $this->saveIGRHistory($hist); - } - if($tareWeight){ - $hist = array('key' => 'TareWeight', 'value' => $tareWeight, 'igr_no' => $IGRNo, 'igr_item_no' => $igrItemNo); + } + if($grossWeight){ + $hist = array('key' => 'GrossWeight', 'value' => $grossWeight, 'igr_no' => $IGRNo, 'igr_item_no' => $igrItemNo); $this->saveIGRHistory($hist); } - if($netWeight){ - $hist = array('key' => 'NetWeight', 'value' => $netWeight, 'igr_no' => $IGRNo, 'igr_item_no' => $igrItemNo); - $this->saveIGRHistory($hist); - } - if($GrossWtDt){ - $hist = array('key' => 'GrossWeightDate', 'value' => $GrossWtDt, 'igr_no' => $IGRNo, 'igr_item_no' => $igrItemNo); - $this->saveIGRHistory($hist); - } - if($TareWtDt){ - $hist = array('key' => 'TareWeightDate', 'value' => $TareWtDt, 'igr_no' => $IGRNo, 'igr_item_no' => $igrItemNo); - $this->saveIGRHistory($hist); - } - - + log_message('error', "lineitem updated successfully: " . $updateigrlineitem); + log_message('error', 'UpdateIGR updateigrlineitem: ' . json_encode($igrline)); } + + + } if ($getigrstatus == 'ST027' && ($updateigrmaster > 0 || $updateigrlineitem > 0)) { $result_for_email = $this->inwardgateregister_model->getDetailsforEmail($this->session->get('userId')); $result_for_history = $this->inwardgateregister_model->getHistoryDetailsforEmail($IGRNo); - // print_r($result_for_email); + print_r($result_for_history);die; $notification = new Notification(); $history_content = NULL; $history = $result_for_history; $field_labels = [ - "Remarks" => "Remarks", + "IGRItemNo" => "IGRItemNo", + "IGRNO" => "IGRNO", + "MaterialCode" => "MaterialCode", "QuantityAsPerInvoice" => "Received Qty", + "Remarks" => "Remarks", + "IGRItemStatus" => "IGRItemStatus", + "CancelNote" => "CancelNote", + "BankStatus" => "BankStatus", "GrossWeight" => "Gross Weight", - "TareWeight" => "Tare Weight", - "NetWeight" => "Net Weight", "GrossWeightDate" => "Gross Weight Date", + "TareWeight" => "Tare Weight", "TareWeightDate" => "Tare Weight Date", + "NetWeight" => "Net Weight", + "WeightFile" => "WeightFile", + "IGRID" => "IGRID", + "IGRNO" => "IGRNO", + "PONO" => "PONO", + "DeliveryChellanOrInvoiceNo" => "Invoice NO", + "DeliveryChellanDate" => "Invoice Date", + "MaterialRcvdDate" => "Material Received Date", "VehicleNo" => "Vehicle Number", + "VehicleType" => "Vehicle Type", + "TransporterId" => "Transporter Id", + "CourierNo" => "Courier No", + "Remark" => "Master Remark", + "IGRStatus" => "IGR Status", + "file" => "File", + "Paymentstatus" => "Payment Status", "DriverName" => "Driver Name", - "DriverMobileNumber" => "Driver Mobile Number" + "DriverMobileNumber" => "Driver Mobile Number", + "MasterFile" => "Master File", ]; $history_content = "
The following values have been edited:"; @@ -973,11 +970,14 @@ function viewIGRDetails() $history_content .= "
" . $readable_field . ": `" . $history[$j]->old_data . "` was updated to `" . $history[$j]->new_data . "` ."; } - + $email = ""; for ($i = 0; $i < count($result_for_email['emails']); $i++) { + $email .= $result_for_email['emails'][$i]->email . " , "; + } + $email = rtrim($email, " ,"); $email_response = $notification->sendEmail([ - 'recipient_email' => $result_for_email['emails'][$i]->email, + 'recipient_email' => $email, 'subject' => $result_for_email['company'].' - IGR Notification', 'description' => 'IGR Number : '.$IGRNo.' is Edited By '.($result_for_email['full_name'] ?? $this->session->get('name'))." On ".date('d-m-Y H:i A', strtotime(get_current_date_time() . ' +30 seconds')). $history_content, 'company' => $result_for_email['company'], @@ -991,11 +991,11 @@ function viewIGRDetails() // Log the result if ($receiving_status) { - $this->logger->info("Email ID = " . $result_for_email['emails'][$i]->email . " " . $mail_log); + $this->logger->info("Email IDs = " . $email . " " . $mail_log); } else { - $this->logger->error("Email ID = " . $result_for_email['emails'][$i]->email . " " . $mail_log); + $this->logger->error("Email IDs = " . $email . " " . $mail_log); } - } + // echo "***".$history_content;die; } @@ -1016,129 +1016,7 @@ function viewIGRDetails() echo $finalstatement; } - function updateIGRLineItem() { - - $IGRNo = $this->request->getPost('IGRNO'); - $Status = $this->request->getPost('IGRStatus'); - $DriverName = $this->request->getPost('DriverName'); - $DriverMobileNumber = $this->request->getPost('DriverMobileNumber'); - $GrossWeight = $this->request->getPost('GrossWeight'); - $TareWeight = $this->request->getPost('TareWeight'); - $NetWeight = $this->request->getPost('NetWeight'); - $VehicleNo = $this->request->getPost('VehicleNo'); - $requestdata = $this->request->getPost('data'); - $updateby = $this->session->get('userId'); - $GrossWeightDate = (string)$this->request->getPost('GrossWeightDate'); - $GrossWtDt = !empty($GrossWeightDate) ? get_date_time_format($GrossWeightDate) : NULL; - $TareWeightDate = (string)$this->request->getPost('TareWeightDate'); - $TareWtDt = !empty($TareWeightDate) ? get_date_time_format($TareWeightDate) : NULL; - - - - // Check if request data is not null and is a string - if (is_string($requestdata)) { - $lineitemdata = json_decode($requestdata, true); // Decode JSON data - - if (is_array($lineitemdata)) { - if($GrossWeight){ - $hist = array('key' => 'GrossWeight', 'value' => $GrossWeight, 'igr_no' => $IGRNo, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } - if($TareWeight){ - $hist = array('key' => 'TareWeight', 'value' => $TareWeight, 'igr_no' => $IGRNo, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } - if($NetWeight){ - $hist = array('key' => 'NetWeight', 'value' => $NetWeight, 'igr_no' => $IGRNo, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } - if($GrossWtDt){ - $hist = array('key' => 'GrossWeightDate', 'value' => $GrossWtDt, 'igr_no' => $IGRNo, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } - if($TareWtDt){ - $hist = array('key' => 'TareWeightDate', 'value' => $TareWtDt, 'igr_no' => $IGRNo, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } - - // if($DeliveryChellanOrInvoiceNo){ - // $igr_history = array('field' => 'DeliveryChellanOrInvoiceNo','is_edit' => 1,'new_data' => $DeliveryChellanOrInvoiceNo,'old_data' => NULL,'history_dated' => get_current_date_time(),'by' => $this->session->get('userId'),'IGR_No' => $IGRNO); - // $history_id = $this->inwardgateregister_model->igr_history($igr_history); - // log_message('error', "History updated successfully. History id: " . $history_id); - // } - // if($DeliveryChellanDate){ - // $igr_history = array('field' => 'DeliveryChellanDate','is_edit' => 1,'new_data' => $DeliveryChellanDate,'old_data' => NULL,'history_dated' => get_current_date_time(),'by' => $this->session->get('userId'),'IGR_No' => $IGRNO); - // $history_id = $this->inwardgateregister_model->igr_history($igr_history); - // log_message('error', "History updated successfully. History id: " . $history_id); - // } - // if($MaterialRcvdDt){ - // $igr_history = array('field' => 'MaterialRcvdDate','is_edit' => 1,'new_data' => $MaterialRcvdDt,'old_data' => NULL,'history_dated' => get_current_date_time(),'by' => $this->session->get('userId'),'IGR_No' => $IGRNO); - // $history_id = $this->inwardgateregister_model->igr_history($igr_history); - // log_message('error', "History updated successfully. History id: " . $history_id); - // } - if($VehicleNo){ - $hist = array('key' => 'VehicleNo', 'value' => $VehicleNo, 'igr_no' => $IGRNo, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } - if($DriverName){ - $hist = array('key' => 'DriverName', 'value' => $DriverName, 'igr_no' => $IGRNo, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } - if($DriverMobileNumber){ - $hist = array('key' => 'DriverMobileNumber', 'value' => $DriverMobileNumber, 'igr_no' => $IGRNo, 'igr_item_no' => NULL); - $this->saveIGRHistory($hist); - } - - $igrarr = array('VehicleNo' => $VehicleNo, 'DriverName' => $DriverName, 'DriverMobileNumber' => $DriverMobileNumber,'IGRStatus' => $Status, 'UpdateBY' => $updateby,'GrossWeight'=>$GrossWeight,'TareWeight'=>$TareWeight,'NetWeight'=>$NetWeight,'GrossWeightDate'=>$GrossWtDt,'TareWeightDate'=>$TareWtDt); - - $masterdata = $this->inwardgateregister_model->updateigr($igrarr, $IGRNo); - $dummy = 0; - $i=1; - - foreach ($lineitemdata as $item) { - $IGRNO = $item['IGRNO'.$i]; - $IGRItemNo = $item['IGRItemNo'.$i]; - $MaterialCode = $item['MaterialCode'.$i]; - $QuantityAsPerInvoice = $item['txtQuantityAsPerInvoice'.$i]; - $Remarks = $item['txtRemarks'.$i]; - - $igrline = array( - 'IGRNO' => $IGRNO, - 'MaterialCode' => $MaterialCode, - 'IGRItemNo' => $IGRItemNo, - 'QuantityAsPerInvoice' => $QuantityAsPerInvoice, - 'Remarks' => $Remarks, - 'UpdateBY' => $updateby - ); -// print_r($igrline); - $lineitemdata = $this->inwardgateregister_model->updateIGRLineItem($igrline, $IGRItemNo); - if($QuantityAsPerInvoice){ - $hist = array('key' => 'QuantityAsPerInvoice', 'value' => $QuantityAsPerInvoice, 'igr_no' => $IGRNo, 'igr_item_no' => $IGRItemNo); - $this->saveIGRHistory($hist); - } - if($Remarks){ - $hist = array('key' => 'Remarks', 'value' => $Remarks, 'igr_no' => $IGRNo, 'igr_item_no' => $IGRItemNo); - $this->saveIGRHistory($hist); - } - $i++; - if ($lineitemdata > 0) { - $dummy++; - } - } - - if ($dummy > 0) { - echo json_encode(array('message' => 'Updated Successfully!')); - } else { - echo json_encode(array('message' => 'Not Updated!')); - } - } else { - echo json_encode(array('message' => 'Invalid data format!')); - } - } else { - echo json_encode(array('message' => 'Invalid request data!')); - } -} function updateIGRWeight(){ @@ -1158,30 +1036,18 @@ function updateIGRWeight(){ $path = ROOTPATH.'public/uploads/Igrfiles/'; if(!is_dir($path)) { mkdir($path, 0777, true); } - if($GrossWeight){ - $hist = array('key' => 'GrossWeight', 'value' => $GrossWeight, 'igr_no' => $IGRNO, 'igr_item_no' => $IGRItemNo); - $this->saveIGRHistory($hist); - } - if($GrossWeight){ - $hist = array('key' => 'TareWeight', 'value' => $TareWeight, 'igr_no' => $IGRNO, 'igr_item_no' => $IGRItemNo); - $this->saveIGRHistory($hist); - } - if($NetWeight){ - $hist = array('key' => 'NetWeight', 'value' => $NetWeight, 'igr_no' => $IGRNO, 'igr_item_no' => $IGRItemNo); - $this->saveIGRHistory($hist); - } - if($GrossWtDt){ - $hist = array('key' => 'GrossWeightDate', 'value' => $GrossWtDt, 'igr_no' => $IGRNO, 'igr_item_no' => $IGRItemNo); - $this->saveIGRHistory($hist); - } - if($TareWtDt){ - $hist = array('key' => 'TareWeightDate', 'value' => $TareWtDt, 'igr_no' => $IGRNO, 'igr_item_no' => $IGRItemNo); - $this->saveIGRHistory($hist); - } - - $IGRItemNo = $this->request->getPost('IGRlineitem'); $igr_lineitem = array('GrossWeight' => $GrossWeight,'GrossWeightDate' => $GrossWtDt,'TareWeightDate' => $TareWtDt,'TareWeight' => $TareWeight,'NetWeight' => $NetWeight,'UpdateBY' => $updateby); + + foreach ($igr_lineitem as $key => $value) { + if (!empty($value)) { // Ensure the value is not empty + if($key == 'CreatedBy'){ $key = 'LineItemCreatedBy'; } + if($key == 'UpdateBY'){ $key = 'LineItemUpdatedBy'; } + if($key == 'IGRNO'){ $key = 'LineItemIGRNO'; } + $hist = array( 'key' => $key, 'value' => $value, 'igr_no' => $IGRNO,'igr_item_no' => $IGRItemNo); + $this->saveIGRHistory($hist); + } + } $requestWeightFileName =null ; if($WeightFile){ if (!empty($requestWeightFileName)) { @@ -1568,62 +1434,53 @@ function updateIGRWeight(){ } - public function saveIGRHistory($hist){ - - $field = $hist['key']; + + public function saveIGRHistory($hist) { - $igr_no = $hist['igr_no']; - $igr_item_no = $hist['igr_item_no']; + // Extract fields from the input array + $field = $hist['key']; - $new_data = $hist['value']; - $old_data = NULL; - - $same = false; // Flag to check if the same data exists - - log_message('error', "$igr_no Save History function entered."); - - // Get existing history - $existingHist = $this->inwardgateregister_model->get_igr_history($field, $igr_no, $igr_item_no); - - if (!empty($existingHist)) { - foreach ($existingHist as $row) { - if ($row->new_data == $new_data) { - $same = true; // The same data already exists - break; - } else { - $old_data = $row->new_data; // Keep track of the latest `new_data` - } - } - } + $igr_no = $hist['igr_no']; + $igr_item_no = $hist['igr_item_no']; + $new_data = $hist['value']; // Incoming new value + $old_data = NULL; // Default old value is null - if (!$same) { - // Insert or update history since data is different - $igr_history = array( - 'field' => $field, - 'new_data' => $new_data, - 'old_data' => $old_data, - 'history_dated' => get_current_date_time(), - 'by' => $this->session->get('userId'), - 'IGR_No' => $igr_no, - 'IGRItem_No' => $igr_item_no - ); - - if ($old_data !== NULL) { - $igr_history['is_edit'] = 1; // Update existing record + log_message('error', "Save History function entered for IGR No: $igr_no, Item No: $igr_item_no, Field: $field."); + + $latestHist = $this->inwardgateregister_model->get_igr_history($field, $igr_no, $igr_item_no); + + if (!empty($latestHist)) { + if ($latestHist->new_data == $new_data) { + log_message('error', "No update needed for IGR No: $igr_no, Item No: $igr_item_no, Field: $field. Data already exists: $new_data"); + return; } else { - $igr_history['is_add'] = 1; // Insert new record + $old_data = $latestHist->new_data; } - - - $history_id = $this->inwardgateregister_model->igr_history($igr_history); - log_message('error', "History updated successfully. History ID: $history_id" ); - } else { - // echo "No update needed: ".$param['key']." ($value) already exists."; - log_message('error', "No update needed: $field ($new_data) already exists."); } - + + // Prepare history entry + $igr_history = [ + 'field' => $field, + 'new_data' => $new_data, + 'old_data' => $old_data, + 'history_dated' => get_current_date_time(), + 'by' => $this->session->get('userId'), + 'IGR_No' => $igr_no, + 'IGRItem_No' => $igr_item_no, + 'is_edit' => ($old_data !== NULL) ? 1 : 0, + 'is_add' => ($old_data === NULL) ? 1 : 0 + ]; + + // Insert history entry + $history_id = $this->inwardgateregister_model->igr_history($igr_history); + if ($history_id) { + log_message('error', "History updated successfully for IGR No: $igr_no, Item No: $igr_item_no, Field: $field. History ID: $history_id"); + } else { + log_message('error', "Failed to update history for IGR No: $igr_no, Item No: $igr_item_no, Field: $field."); + } } + diff --git a/app/Models/Inwardgateregister_model.php b/app/Models/Inwardgateregister_model.php index f1bbb384..564b7620 100755 --- a/app/Models/Inwardgateregister_model.php +++ b/app/Models/Inwardgateregister_model.php @@ -231,20 +231,22 @@ class Inwardgateregister_model extends Model { $builder = $this->db->table('t_igr_history Hist') - ->select("Hist.*"); - $builder->where('Hist.field', $col); - if($IGR){ - $builder->where('Hist.IGR_No', $IGR); - } - if ($IGRITEM !== null) { - $builder->where('Hist.IGRItem_No', $IGRITEM); - } - $query = $builder->get(); - $result = $query->getResult(); - return $result; - // $result = $model->get_igr_history('001'); - // $result = $model->get_igr_history('001', 1); + ->select("Hist.*") + ->where('Hist.field', $col); + if ($IGR) { + $builder->where('Hist.IGR_No', $IGR); + } + + if ($IGRITEM !== null) { + $builder->where('Hist.IGRItem_No', $IGRITEM); + } + + $builder->orderBy('Hist.history_dated', 'DESC'); + $builder->limit(1); + $query = $builder->get(); + $result = $query->getRow(); + return $result; } function addigrM($igrM) @@ -1192,9 +1194,48 @@ class Inwardgateregister_model extends Model $to = date('Y-m-d H:i:s', strtotime($date . ' +30 seconds')); $from = date('Y-m-d H:i:s', strtotime($date . ' -30 seconds')); - $builder = $this->db->table('t_igr_history')->where('IGR_NO', $igr) - ->where('is_edit', 1) - ->where('history_dated BETWEEN "'.$from.'" AND "'.$to.'"'); + + +$builder = $this->db->table('t_igr_history') + ->select(' + t_igr_history.id, + t_igr_history.field, + t_igr_history.is_add, + t_igr_history.is_edit, + t_igr_history.is_delete, + t_igr_history.history_dated, + t_igr_history.by, + t_igr_history.IGR_No, + t_igr_history.IGRItem_No, + + CASE + WHEN t_igr_history.field = "TransporterId" + THEN (SELECT name FROM t_transporter WHERE id = t_igr_history.new_data LIMIT 1) + WHEN t_igr_history.field = "MaterialCode" + THEN (SELECT MaterialName FROM t_materialmaster WHERE MaterialCode = t_igr_history.new_data LIMIT 1) + WHEN t_igr_history.field IN ("IGRStatus", "PaymentStatus", "IGRItemStatus") + THEN (SELECT StatusName FROM t_status WHERE StatusCode = t_igr_history.new_data LIMIT 1) + ELSE t_igr_history.new_data + END AS new_data, + t_igr_history.new_data as original_new_data, + + CASE + WHEN t_igr_history.field = "TransporterId" + THEN (SELECT name FROM t_transporter WHERE id = t_igr_history.old_data LIMIT 1) + WHEN t_igr_history.field = "MaterialCode" + THEN (SELECT MaterialName FROM t_materialmaster WHERE MaterialCode = t_igr_history.old_data LIMIT 1) + WHEN t_igr_history.field IN ("IGRStatus", "PaymentStatus", "IGRItemStatus") + THEN (SELECT StatusName FROM t_status WHERE StatusCode = t_igr_history.old_data LIMIT 1) + ELSE t_igr_history.old_data + END AS old_data, + t_igr_history.old_data as original_old_data + ') + // ->join('t_igr_details IGRD', 'IGRD.IGRItemNo = t_igr_history.IGRItem_No','left') + // ->join('t_materialmaster MM','MM.MaterialCode = IGRD.MaterialCode','left') + ->where('t_igr_history.IGR_No', $igr) + ->where('t_igr_history.is_edit', 1) + ->where('t_igr_history.history_dated BETWEEN "'.$from.'" AND "'.$to.'"'); + $query = $builder->get(); // echo $this->db->getLastQuery()->getQuery(); die; return $query->getResult();