stock module latest today vadivel J 04-02-2025

This commit is contained in:
vadivelJ96 2025-02-04 17:42:11 +05:30
parent e0adcd1a9f
commit b93073e96d
4 changed files with 200 additions and 83 deletions

View File

@ -450,8 +450,7 @@ $routes->post('getSilicaIGR', 'Supplier::getSilicaIGR');
$routes->match(['GET', 'POST'], 'coatingMachineDetails', 'StockController::loadView_coatingMachineDetails');
$routes->post('addNewCoatingMachineDetails', 'StockController::addNewCoatingMachineDetails');
$routes->post('updateCoatingMachineDetails', 'StockController::updateCoatingMachineDetails');
$routes->get('deleteCoatingMachineDetails', 'StockController::deleteCoatingMachineDetails');
$routes->post('deleteBatchCardFile','StockController::deleteBatchCardFile');
//drier machine details
$routes->match(['GET', 'POST', 'PUT', 'DELETE'], 'drierMachineDetails', 'StockController::drierMachineDetails');

View File

@ -149,12 +149,21 @@ class StockController extends BaseController
$startDate = "$month-01";
$endDate = date("Y-m-t", strtotime($startDate));
$data['coatingMachineDetails'] = $this->coatingMachineDetails_model
->select('t_coatingMachineDetails.*, t_batchcard_files.*') // Select required fields
->join('t_batchcard_files', 't_batchcard_files.coating_id = t_coatingMachineDetails.id', 'left')
->where('t_coatingMachineDetails.date >=', $startDate)
->where('t_coatingMachineDetails.date <=', $endDate)
->orderBy('t_coatingMachineDetails.date', 'asc')
->findAll();
->select('t_coatingMachineDetails.*,
JSON_ARRAYAGG(
JSON_OBJECT(
"client_given_name", t_batchcard_files.client_file_name,
"filename", t_batchcard_files.filename,
"batchCardId" , t_batchcard_files.id
)
) as batchcard_files')
->join('t_batchcard_files', 't_batchcard_files.coating_id = t_coatingMachineDetails.id', 'left')
->where('t_coatingMachineDetails.date >=', $startDate)
->where('t_coatingMachineDetails.date <=', $endDate)
->groupBy('t_coatingMachineDetails.id')
->orderBy('t_coatingMachineDetails.date', 'asc')
->findAll();
$date = DateTime::createFromFormat('Y-m', $month);
@ -268,6 +277,8 @@ class StockController extends BaseController
public function updateCoatingMachineDetails()
{
$message1 = '';
$message2 = '';
$message3 = '';
@ -282,6 +293,40 @@ class StockController extends BaseController
$this->db->transBegin();
try {
$updated = $this->coatingMachineDetails_model->update($id, $putData);
if ($updated !== false && $updated > 0) { // Assuming $id is the record ID being updated
$batchCardFiles = $this->request->getFileMultiple('batchCard');
// Step 4: Upload new files and insert into DB
foreach ($batchCardFiles as $index => $file) {
if ( !empty($batchCardFiles[$index]) && !($batchCardFiles[$index]->getError() === UPLOAD_ERR_NO_FILE)) {
$db = \Config\Database::connect();
$builder = $db->table('t_batchcard_files');
if ($file->isValid() && !$file->hasMoved()) {
$clientGivenName = $file->getClientName(); // User-given filename
$newName = $file->getRandomName(); // Randomized unique filename
// Move the new file to storage
if ($file->move(WRITEPATH . 'uploads/batchcard', $newName)) {
// Insert new file record into the database
$data = [
'client_file_name' => $clientGivenName,
'filename' => $newName,
'coating_id' => $id
];
$builder->insert($data);
}
}
}
}
}
if ($updated !== false && $updated > 0) {
$message1 = "successfully updated Coating machineDetail .";
@ -289,7 +334,7 @@ class StockController extends BaseController
$dateToBeUpdated = $putData['date'];
$existingCoatingGasConsumption = $putData['existingTotalGasConsumption'];
$updatedCoatingGasConsumption = $putData['totalGasConsumption'];
$updatedCoatingGasConsumption = $putData['totalGasConsumption'];
if ($existingCoatingGasConsumption != $updatedCoatingGasConsumption) {
@ -321,59 +366,7 @@ class StockController extends BaseController
} else {
throw new \Exception('Failed to update Coating machine details.');
}
if ($updated !== false && $updated > 0) { // Assuming $id is the record ID being updated
$batchCardFiles = $this->request->getFileMultiple('batchCard');
if (!empty($batchCardFiles) && !($batchCardFiles[0]->getError() === UPLOAD_ERR_NO_FILE)) {
// Optional: Delete only files that need to be replaced (if you have a way to identify which are replaced)
// Step 1: Fetch existing files linked with the $coating_id
$db = \Config\Database::connect();
$builder = $db->table('t_batchcard_files');
$oldFiles = $builder->where('coating_id', $id)->findAll();
// Step 2: Upload new files and save them in the database
foreach ($batchCardFiles as $file) {
if ($file->isValid() && !$file->hasMoved()) {
$clientGivenName = $file->getClientName(); // User-given filename
$newName = $file->getRandomName();
// Check if the new file is different from the old ones (if needed)
$isNewFile = true;
foreach ($oldFiles as $oldFile) {
if ($oldFile['filename'] === $newName) {
$isNewFile = false; // File already exists, skip uploading
break;
}
}
if ($isNewFile) {
// Move the new file and save to DB
if ($file->move(WRITEPATH . 'uploads/batchcard', $newName)) {
$data = [
'client_file_name' => $clientGivenName,
'filename' => $newName,
'coating_id' => $id
];
$builder->insert($data);
}
}
}
}
foreach ($oldFiles as $oldFile) {
$filePath = WRITEPATH . 'uploads/batchcard/' . $oldFile['filename'];
if (file_exists($filePath)) {
unlink($filePath); // Delete old file from storage
}
}
}
}
@ -384,23 +377,56 @@ class StockController extends BaseController
}
}
public function deleteCoatingMachineDetails()
public function deleteBatchCardFile()
{
$id = $this->request->getGet('id');
$data['is_active'] = 0;
$updated = $this->coatingMachineDetails_model->update($id, $data);
if ($updated) {
// Set flashdata for success message
return redirect()->to('/coatingMachineDetails')->with('success', 'Coating Machine details deleted successfully');
} else {
// Set flashdata for error message
return redirect()->back()->with('error', 'Failed to delete Coating machine details');
$data = $this->request->getPost();
if (!empty($data)) {
$batchId = $data['id'];
$db = \Config\Database::connect();
$builder = $db->table('t_batchcard_files');
// Step 1: Fetch the filename from DB before deleting
$fileRecord = $builder->select('filename')->where('id', $batchId)->get()->getRow();
if ($fileRecord) {
$filePath = WRITEPATH . 'uploads/batchcard/' . $fileRecord->filename;
// Step 2: Delete the file from storage
if (file_exists($filePath)) {
unlink($filePath);
}
// Step 3: Delete the record from the database
$sql = "DELETE FROM t_batchcard_files WHERE id = ?";
$deleted = $db->query($sql, [$batchId]);
if ($deleted) {
return $this->response->setJSON([
'status' => '200',
'message' => "Deleted Batch Card File Successfully..!!"
]);
} else {
return $this->response->setJSON([
'status' => '400',
'message' => "Failed to Delete..!! Try Again Later"
]);
}
} else {
return $this->response->setJSON([
'status' => '404',
'message' => "File Not Found..!!"
]);
}
}
return $this->response->setJSON([
'status' => '400',
'message' => "Invalid Request..!!"
]);
}
//incoming Silica Sand Details starts
public function loadView_incomingSilicaSandDetails()

View File

@ -13,7 +13,7 @@ class CoatingMachineDetailsModel extends Model
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = ['shift','machineOnTime','machineOffTime','totalCoating','date',
'machineRunningInHrs' , 'perHourCoatingSandQTY' , 'perHourGasConsumption' ,'grade','batchCard',
'machineRunningInHrs' , 'perHourCoatingSandQTY' , 'perHourGasConsumption' ,'grade',
'totalCoatingSandInKg','totalGasConsumption','customerName', 'is_active','created_at','updated_at'];
protected bool $allowEmptyInserts = false;

View File

@ -341,6 +341,7 @@
<?php
if (!empty($coatingMachineDetails)) {
$summary = [] ;
foreach ($coatingMachineDetails as $record) {
@ -392,6 +393,8 @@
</a>
</td>
<!-- td:eq(13) -->
<td style="display:none;"><?php echo $record['batchcard_files'] ?? ' ' ?></td>
</tr>
@ -703,7 +706,7 @@
<button type="button" class="btn btn-success btn-sm mt-2" id="batchCardAddBtnId">Add More</button>
</div>
</div>
@ -911,16 +914,23 @@
</div>
<div class="form-row">
<div class="form-row" id="editBatchCardRowId">
<!-- batch card file upload -->
<div class="form-group col-md-3">
<label for="editBatchCardId">upload batch card</label>
<span class="badge mandatory">*</span>
<input type="file" class="" id="editBatchCardId" name="batchCard"
value="" required >
<label for="editBatchCardId">Upload Batch Card</label>
<div>
<input type="file" class="editBatchFile " name="batchCard[]">
</div>
<button type="button" class="btn btn-success btn-sm mt-2" id="editBatchCardAddBtnId">Add More</button>
</div>
</div>
@ -1224,14 +1234,43 @@
var totalGasConsumption = row.find('td:eq(9)').text();
var perHourGasConsumption = row.find('td:eq(10)').text();
var perHourCoatingSandQTY = row.find('td:eq(11)').text();
var batchCards = row.find('td:eq(13)').text().trim();
console.log(batchCards);
JSON.parse(batchCards).map(element => {
if(!element.client_given_name){
console.log("filename null")
return;
}
let fileCount = $('.editBatchFile').length; // Count existing batch files
if (fileCount >= 5) {
return;
}
let string = `<div class="form-group col-md-3 batch-container">
<label for="">Change Batch Card</label>
<input type="file" class="editBatchFile additionalBatchFile" name="batchCard[]" >
<p>( Previously Uploaded File ${element.client_given_name} )</p>
<button type="button" class="btn btn-danger btn-sm mt-2 removeBatch" data-id="${element.batchCardId}">Remove</button>
</div>`;
$('#editBatchCardRowId').append(string)
});
// Fill the modal fields with the values
//batchfile
$('#editCoatingMachineDetailId').val(row.data('id'));
$('#editCustomerNameId').val(customerName);
$('#editDateId').val(date); //convert this from d-m-y to Y-m-d in javascript
$('#editDateId').val(date); //convert this from d-m-y to Y-m-d in javascript
$('#editShiftId').val(shift);
$('#editMachineOnTimeHrId').val(machineOnTimeHr).change();
$('#editMachineOffTimeHrId').val(machineOffTimeHr).change();
@ -1252,6 +1291,16 @@
});
</script>
<script>
$(document).ready(function(){
$('#editCoatingMachineDetailModal').on('hidden.bs.modal', function () {
$('.batch-container').remove(); // This will remove all child elements
});
});
</script>
<!-- calculations for some fields needed in onChange while adding coating machine details -->
<script>
@ -1610,4 +1659,47 @@
});
});
$(document).ready(function () {
$('#editBatchCardAddBtnId').click(function () {
let fileCount = $('.editBatchFile').length; // Count existing batch files
if (fileCount >= 5) { // Set your limit here (e.g., max 5 files)
alert("You can only upload up to 5 batch files.");
return;
}
let string = `<div class="form-group col-md-3 batch-container">
<label for="">Upload Batch Card</label>
<input type="file" class="editBatchFile" name="batchCard[]" >
<button type="button" class="btn btn-danger btn-sm mt-2 removeBatch">Remove</button>
</div>`;
$('#editBatchCardRowId').append(string);
});
// Remove batch card field dynamically
$(document).on('click', '.removeBatch', function () {
let batchId = this.getAttribute('data-id'); // Get the data-id
if (confirm("Are you sure you want to delete this batch file?")) {
$.ajax({
url: "<?php echo base_url();?>/deleteBatchCardFile",
type: "POST",
data: { id: batchId },
success: function (response) {
if(response.status == "200"){
alert("Batch file deleted successfully!");
$(this).closest('.batch-container').remove(); // Remove from UI
}
},
error: function (xhr, status, error) {
alert("Error deleting batch file: " + error);
}
});
}
});
});
</script>