Gas stock details changes-1 vadivel J 27-11-2024

This commit is contained in:
vadivelJ96 2024-11-27 17:44:17 +05:30
parent a205586d35
commit aa67d69b8d

View File

@ -134,6 +134,9 @@ class StockController extends BaseController
public function addNewCoatingMachineDetails() public function addNewCoatingMachineDetails()
{ {
$message1 = '';
$message2 = '';
$postData = $this->request->getPost(); $postData = $this->request->getPost();
$month = $this->request->getGet('month'); $month = $this->request->getGet('month');
$month = DateTime::createFromFormat('M-Y', $month)->format('Y-m'); $month = DateTime::createFromFormat('M-Y', $month)->format('Y-m');
@ -158,14 +161,19 @@ class StockController extends BaseController
try { try {
// Insert into coating machine details table // Insert into coating machine details table
$inserted = $this->coatingMachineDetails_model->insert($postData); $inserted = $this->coatingMachineDetails_model->insert($postData);
if (!$inserted) {
throw new \Exception('Failed to insert coating machine details');
}
// Update gas stock consumption if ($inserted !== false && $inserted > 0) {
$dateToBeInserted = $postData['date'];
$consumption = $postData['totalGasConsumption']; $message1 = 'Coating Machine details updated successfully';
$this->updateGasStockConsumption($dateToBeInserted, $consumption);
// Update gas stock consumption
$dateToBeInserted = $postData['date'];
$consumption = $postData['totalGasConsumption'];
$updatedResult = $this->updateGasStockConsumption($dateToBeInserted, $consumption);
$message2 = ($updatedResult !== false && $updatedResult > 0) ? 'And Updated Gas Consumption in Gas Stock' : 'And failed to Updated Gas Consumption in Gas Stock';
}
// Commit the transaction if all queries succeed // Commit the transaction if all queries succeed
$this->db->transComplete(); $this->db->transComplete();
@ -175,7 +183,7 @@ class StockController extends BaseController
} }
// Set flashdata for success message // Set flashdata for success message
return redirect()->to("/coatingMachineDetails?month=$month")->with('success', 'Coating Machine details updated successfully'); return redirect()->to("/coatingMachineDetails?month=$month")->with('success', $message1 . " " . $message2);
} catch (\Exception $error) { } catch (\Exception $error) {
// Rollback on any exception // Rollback on any exception
@ -188,6 +196,8 @@ class StockController extends BaseController
public function updateCoatingMachineDetails() public function updateCoatingMachineDetails()
{ {
$message1 = '';
$message2 = '';
$putData = $this->request->getPost(); $putData = $this->request->getPost();
$month = $this->request->getGet('month'); $month = $this->request->getGet('month');
@ -195,27 +205,48 @@ class StockController extends BaseController
$id = $putData['editCoatingMachineDetailId']; $id = $putData['editCoatingMachineDetailId'];
$updated = $this->coatingMachineDetails_model->update($id, $putData); // Start transaction
$this->db->transStart();
try {
$updated = $this->coatingMachineDetails_model->update($id, $putData);
if ($updated !== false && $updated > 0) {
$message1 = "successfully updated Coating machineDetail .";
//update Gas stock consumption if update is successfull..!!
$dateToBeUpdated = $putData['date'];
if ($updated) { $existingCoatingGasConsumption = $putData['existingTotalGasConsumption'];
//update Gas stock consumption if update is successfull..!! $updatedCoatingGasConsumption = $putData['totalGasConsumption'];
$dateToBeUpdated = $putData['date'];
$existingCoatingGasConsumption = $putData['existingTotalGasConsumption']; if ($existingCoatingGasConsumption != $updatedCoatingGasConsumption) {
$updatedCoatingGasConsumption = $putData['totalGasConsumption'];
if ($existingCoatingGasConsumption != $updatedCoatingGasConsumption) { $changeInConsumption = $updatedCoatingGasConsumption - $existingCoatingGasConsumption;
$changeInConsumption = $updatedCoatingGasConsumption - $existingCoatingGasConsumption; $updateResult = $this->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption);
$this->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption); $message2 = ($updateResult !== false && $updateResult > 0)
? 'And Updated Gas Stock Consumption '
: "But Failed to Update Gas Stock Consumption ";
}
// Commit the transaction if all queries succeed
$this->db->transComplete();
if ($this->db->transStatus() === false) {
throw new \Exception('Transaction failed');
}
// Set flashdata for success message
return redirect()->to("/coatingMachineDetails?month=$month")->with('success', $message1 . " " . $message2);
} }
} catch (\Exception $error) {
// Set flashdata for success message // Rollback on any exception
return redirect()->to("/coatingMachineDetails?month=$month")->with('success', 'Coating Machine details updated successfully'); $this->db->transRollback();
} else { return redirect()->back()->with('error', 'Failed to update Coating machine details' . $error->getMessage());
// Set flashdata for error message
return redirect()->back()->with('error', 'Failed to update Coating machine details');
} }
} }
public function deleteCoatingMachineDetails() public function deleteCoatingMachineDetails()
@ -913,7 +944,7 @@ class StockController extends BaseController
private function updateGasStockConsumption($date, $consumption) private function updateGasStockConsumption($date, $consumption)
{ {
$count = 0;
$resultExists = $this->gasStockDetails_model $resultExists = $this->gasStockDetails_model
->where('date', $date) ->where('date', $date)
@ -937,36 +968,43 @@ class StockController extends BaseController
$resultExists = $this->gasStockDetails_model $resultExists = $this->gasStockDetails_model
->where('date >=', $date) ->where('date >=', $date)
->where('date <=', $endDate) ->where('date <=', $endDate)
->orderBy('date' , 'asc') ->orderBy('date', 'asc')
->findAll(); ->findAll();
$updatedOpeningStock=0; $updatedOpeningStock = 0;
$updates =[]; $updates = [];
foreach($resultExists as $index => $result){ foreach ($resultExists as $index => $result) {
if($index == 0){ $count++;
$result['consumption'] = $result['consumption'] + ($consumption);
$result['total'] = $result['opening'] + $result['purchaseBharath'] + $result['purchaseIndian'] ; if ($index == 0) {
$result['consumption'] = $result['consumption'] + ($consumption);
$result['total'] = $result['opening'] + $result['purchaseBharath'] + $result['purchaseIndian'];
$result['balanceStock'] = $result['total'] - $result['consumption']; $result['balanceStock'] = $result['total'] - $result['consumption'];
$updatedOpeningStock = $result['balanceStock']; $updatedOpeningStock = $result['balanceStock'];
}else{ } else {
$result['opening'] = $updatedOpeningStock;
$result['opening'] = $updatedOpeningStock ; $result['total'] = $result['opening'] + $result['purchaseBharath'] + $result['purchaseIndian'];
$result['total'] = $result['opening'] + $result['purchaseBharath'] + $result['purchaseIndian'] ; $result['balanceStock'] = $result['total'] - $result['consumption'];
$result['balanceStock'] = $result['total'] - $result['consumption']; $updatedOpeningStock = $result['balanceStock'];
$updatedOpeningStock = $result['balanceStock'];
} }
log_message('info',$count);
$updates [] = $result; log_message('info',$date);
log_message('info', $result['opening']);
$updates[] = $result;
} }
if(!empty($updates)){ if (!empty($updates)) {
$this->gasStockDetails_model->updateBatch($updates,'id'); $result = $this->gasStockDetails_model->updateBatch($updates, 'id');
return ($result !== false && $result > 0) ? $result : 0;
} else {
return 0;
} }
} else { } else {
@ -995,7 +1033,7 @@ class StockController extends BaseController
$startDate = DateTime::createFromFormat('Y-m-d', $date)->modify('first day of this month')->format('Y-m-d'); $startDate = DateTime::createFromFormat('Y-m-d', $date)->modify('first day of this month')->format('Y-m-d');
$endDate = DateTime::createFromFormat('Y-m-d', $date)->modify('last day of this month')->format('Y-m-d'); $endDate = DateTime::createFromFormat('Y-m-d', $date)->modify('last day of this month')->format('Y-m-d');
$datesInMonth = []; $datesInMonth = [];
$inserts = []; $inserts = [];
@ -1036,7 +1074,10 @@ class StockController extends BaseController
} }
if (!empty($inserts)) { if (!empty($inserts)) {
$this->gasStockDetails_model->insertBatch($inserts); $result = $this->gasStockDetails_model->insertBatch($inserts);
return ($result !== false && $result > 0) ? $result : 0;
} else {
return 0;
} }
} }
@ -1154,24 +1195,27 @@ class StockController extends BaseController
// Check if a row with the same date already exists // Check if a row with the same date already exists
$existingRow = $this->drierMachineDetails_model->where('date', $data['date'])->first(); $existingRow = $this->drierMachineDetails_model->where('date', $data['date'])->first();
if ($existingRow) { if ($existingRow) {
// If exists, update the row // If exists, update the row
//before updating the row update gas stock //before updating the row update gas stock
$existingDrierGasConsumption = $existingRow['total_gas_consumption']; $existingDrierGasConsumption = $existingRow['total_gas_consumption'];
$updatedDrierGasConsumption = $row['Tot Gas Consumption']; $updatedDrierGasConsumption = $row['Tot Gas Consumption'];
if ($existingDrierGasConsumption != $updatedDrierGasConsumption) { if ($existingDrierGasConsumption != $updatedDrierGasConsumption) {
$changeInConsumption = $updatedDrierGasConsumption - $existingDrierGasConsumption;
$dateToBeUpdated = $existingRow['date']; $changeInConsumption = $updatedDrierGasConsumption - $existingDrierGasConsumption ;
$this->updateGasStockConsumption($dateToBeUpdated, $changeInConsumption); $dateToBeUpdated = $existingRow['date'] ;
$result =$this->gasStockDetails_model
->where('date',$dateToBeUpdated)
->find();
$consumption = $result[0]['consumption'] + $changeInConsumption ;
$result = $this->updateGasStockConsumption($dateToBeUpdated, $consumption);
} }
$this->drierMachineDetails_model->update($existingRow['id'], $data); $this->drierMachineDetails_model->update($existingRow['id'], $data);
} else { } else {
// If not, insert a new row // If not, insert a new row
//before inserting the row update gas stock //before inserting the row update gas stock
$consumption = $row['Tot Gas Consumption']; $consumption = $row['Tot Gas Consumption'];
$dateToBeInserted = $data['date']; $dateToBeInserted = $data['date'];