Gas stock details changes-2 vadivel J 26-11-2024
This commit is contained in:
parent
7d93dd731b
commit
a205586d35
@ -42,6 +42,8 @@ class StockController extends BaseController
|
|||||||
protected $resinStockDetails_model;
|
protected $resinStockDetails_model;
|
||||||
protected $gasStockDetails_model;
|
protected $gasStockDetails_model;
|
||||||
|
|
||||||
|
protected $db;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is default constructor of the class
|
* This is default constructor of the class
|
||||||
*/
|
*/
|
||||||
@ -132,7 +134,6 @@ class StockController extends BaseController
|
|||||||
|
|
||||||
public function addNewCoatingMachineDetails()
|
public function addNewCoatingMachineDetails()
|
||||||
{
|
{
|
||||||
|
|
||||||
$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');
|
||||||
@ -140,6 +141,7 @@ class StockController extends BaseController
|
|||||||
$date = $postData['date'];
|
$date = $postData['date'];
|
||||||
$shift = $postData['shift'];
|
$shift = $postData['shift'];
|
||||||
|
|
||||||
|
// Check if the same shift already exists on the same date
|
||||||
$sameShiftOnDateExists = $this->coatingMachineDetails_model
|
$sameShiftOnDateExists = $this->coatingMachineDetails_model
|
||||||
->where('date', $date)
|
->where('date', $date)
|
||||||
->where('shift', $shift)
|
->where('shift', $shift)
|
||||||
@ -150,22 +152,38 @@ class StockController extends BaseController
|
|||||||
return redirect()->back()->with('error', 'Already same shift exists on the same date ..!!');
|
return redirect()->back()->with('error', 'Already same shift exists on the same date ..!!');
|
||||||
}
|
}
|
||||||
|
|
||||||
$inserted = $this->coatingMachineDetails_model->insert($postData);
|
// Start transaction
|
||||||
|
$this->db->transStart();
|
||||||
|
|
||||||
if ($inserted) {
|
try {
|
||||||
//update Gas stock consumption if insert is successfull..!!
|
// Insert into coating machine details table
|
||||||
|
$inserted = $this->coatingMachineDetails_model->insert($postData);
|
||||||
|
if (!$inserted) {
|
||||||
|
throw new \Exception('Failed to insert coating machine details');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update gas stock consumption
|
||||||
$dateToBeInserted = $postData['date'];
|
$dateToBeInserted = $postData['date'];
|
||||||
$consumption = $postData['totalGasConsumption'];
|
$consumption = $postData['totalGasConsumption'];
|
||||||
$this->updateGasStockConsumption($dateToBeInserted, $consumption);
|
$this->updateGasStockConsumption($dateToBeInserted, $consumption);
|
||||||
|
|
||||||
// Set flashdata for success message
|
// Commit the transaction if all queries succeed
|
||||||
return redirect()->to("/coatingMachineDetails?month=$month")->with('success', 'Coating Machine details updated successfully');
|
$this->db->transComplete();
|
||||||
} else {
|
|
||||||
// Set flashdata for error message
|
if ($this->db->transStatus() === false) {
|
||||||
return redirect()->back()->with('error', 'Failed to update Coating machine details');
|
throw new \Exception('Transaction failed');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set flashdata for success message
|
||||||
|
return redirect()->to("/coatingMachineDetails?month=$month")->with('success', 'Coating Machine details updated successfully');
|
||||||
|
|
||||||
|
} catch (\Exception $error) {
|
||||||
|
// Rollback on any exception
|
||||||
|
$this->db->transRollback();
|
||||||
|
return redirect()->back()->with('error', 'Failed to update Coating machine details: ' . $error->getMessage());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function updateCoatingMachineDetails()
|
public function updateCoatingMachineDetails()
|
||||||
@ -896,15 +914,60 @@ class StockController extends BaseController
|
|||||||
private function updateGasStockConsumption($date, $consumption)
|
private function updateGasStockConsumption($date, $consumption)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
$resultExists = $this->gasStockDetails_model
|
$resultExists = $this->gasStockDetails_model
|
||||||
->where('date', $date)
|
->where('date', $date)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if ($resultExists) {
|
if ($resultExists) {
|
||||||
|
|
||||||
$resultExists['consumption'] = $resultExists['consumption'] + ($consumption);
|
|
||||||
$resultExists['balanceStock'] = $resultExists['opening'] - $resultExists['consumption'];
|
if ($date instanceof DateTime) {
|
||||||
$this->gasStockDetails_model->update($resultExists['id'], $resultExists);
|
$date = $date->format('Y-m-d');
|
||||||
|
} elseif (!strtotime($date)) {
|
||||||
|
throw new \Exception("Invalid date format provided.");
|
||||||
|
} else {
|
||||||
|
$date = date('Y-m-d', strtotime($date));
|
||||||
|
}
|
||||||
|
|
||||||
|
$endDate = date('Y-m-t', strtotime($date));
|
||||||
|
|
||||||
|
|
||||||
|
//what happens if the updation is on the last day of the month?
|
||||||
|
$resultExists = $this->gasStockDetails_model
|
||||||
|
->where('date >=', $date)
|
||||||
|
->where('date <=', $endDate)
|
||||||
|
->orderBy('date' , 'asc')
|
||||||
|
->findAll();
|
||||||
|
|
||||||
|
$updatedOpeningStock=0;
|
||||||
|
$updates =[];
|
||||||
|
|
||||||
|
|
||||||
|
foreach($resultExists as $index => $result){
|
||||||
|
|
||||||
|
if($index == 0){
|
||||||
|
$result['consumption'] = $result['consumption'] + ($consumption);
|
||||||
|
$result['total'] = $result['opening'] + $result['purchaseBharath'] + $result['purchaseIndian'] ;
|
||||||
|
$result['balanceStock'] = $result['total'] - $result['consumption'];
|
||||||
|
$updatedOpeningStock = $result['balanceStock'];
|
||||||
|
}else{
|
||||||
|
|
||||||
|
$result['opening'] = $updatedOpeningStock ;
|
||||||
|
$result['total'] = $result['opening'] + $result['purchaseBharath'] + $result['purchaseIndian'] ;
|
||||||
|
$result['balanceStock'] = $result['total'] - $result['consumption'];
|
||||||
|
$updatedOpeningStock = $result['balanceStock'];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$updates [] = $result;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!empty($updates)){
|
||||||
|
$this->gasStockDetails_model->updateBatch($updates,'id');
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user