ria/app/Controllers/StockController.php

300 lines
8.9 KiB
PHP

<?php
namespace App\Controllers;
use App\Controllers\BaseController;
use App\Models\CoatingMachineDetailsModel;
use App\Models\DrierMachineDetailsModel;
use CodeIgniter\HTTP\ResponseInterface;
use DateTime;
class StockController extends BaseController
{
protected $coatingMachineDetails_model;
protected $drierMachineDetails_model;
protected $session;
/**
* This is default constructor of the class
*/
public function __construct()
{
parent::__construct();
$this->coatingMachineDetails_model = new CoatingMachineDetailsModel();
$this->drierMachineDetails_model = new DrierMachineDetailsModel();
$this->session = session();
helper('form');
$this->isLoggedIn();
}
public function index()
{
//
}
public function loadView_coatingMachineDetails(){
if ($this->request->getMethod() === 'POST'){
$month = $this->request->getPost('month');
$date = DateTime::createFromFormat('M-Y', $month);
$formattedDate = $date->format('Y-m');
$month=$formattedDate;
}
else{
$month = date('Y-m');
// dd($month);
}
$this->global['pageTitle'] = 'Coating Machine Details ';
$data['coatingMachineDetails']= $this->coatingMachineDetails_model
->where('is_active', 1)
->orderBy('created_at','DESC')
->like('created_at', $month, 'after')
->findAll();
$date = DateTime::createFromFormat('Y-m', $month);
$formattedDate = $date->format('M-Y');
$data['month']=$formattedDate;
return $this->loadViews("CoatingMachineDetail", $this->global, $data, NULL);
}
public function addNewCoatingMachineDetails(){
$postData=$this->request->getPost();
$inserted= $this->coatingMachineDetails_model->insert($postData);
if ($inserted) {
// Set flashdata for success message
return redirect()->to('/coatingMachineDetails')->with('success', 'Coating Machine details updated successfully');
} else {
// Set flashdata for error message
return redirect()->back()->with('error', 'Failed to update Coating machine details');
}
}
public function updateCoatingMachineDetails(){
$putData=$this->request->getPost();
$id = $putData['editCoatingMachineDetailId'];
$updated= $this->coatingMachineDetails_model->update($id,$putData);
if ($updated) {
// Set flashdata for success message
return redirect()->to('/coatingMachineDetails')->with('success', 'Coating Machine details updated successfully');
} else {
// Set flashdata for error message
return redirect()->back()->with('error', 'Failed to update Coating machine details');
}
}
public function deleteCoatingMachineDetails(){
$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');
}
}
// --------------------------------------------gwm----------------------------------------------
function getMonthDatesFromInput($monthYear) {
$date = DateTime::createFromFormat('M-Y', $monthYear);
if (!$date) {
return "Invalid date format. Use 'M-yyyy' format.";
}
$year = $date->format('Y');
$month = $date->format('m');
$totalDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$dates = [];
for ($day = 1; $day <= $totalDays; $day++) {
$currentDate = DateTime::createFromFormat('Y-m-d', "$year-$month-$day");
$dates[] = [
'readable' => $currentDate->format('d-m-Y'),
'database' => $currentDate->format('Y-m-d')
];
}
return $dates;
}
function fillMissingDates($monthData, $drierDetails) {
$filledDetails = [];
$drierDetailsByDate = [];
foreach ($drierDetails as $detail) {
$drierDetailsByDate[$detail['date']] = $detail;
}
foreach ($monthData as $day) {
$date = $day['database'];
if (isset($drierDetailsByDate[$date])) {
$filledDetails[] = $drierDetailsByDate[$date];
} else {
$filledDetails[] = [
'id' => null,
'date' => $date,
'drier_on_time' => null,
'drier_off_time' => null,
'drier_running_hours' => '0.00',
'supplier_name' => null,
'input_sand_moisture' => '0.00',
'total_gas_consumption' => '0.00',
'gas_per_ton' => '0.00',
'input_sand_qty' => '0.00',
'moisture_loss_qty' => '0.00',
'sand_dried_qty' => '0.00',
'qty_per_hours' => '0.00',
'dust_qty' => '0.00',
'customer_name' => null,
'created_at' => null,
'updated_at' => null
];
}
}
return $filledDetails;
}
function generateTimeOptions() {
$times = [];
$start = strtotime('00:00'); // Start at midnight (12:00 AM)
$end = strtotime('23:45'); // End at 11:45 PM
while ($start <= $end) {
$timeValue = date('H:i', $start); // 24-hour format for value
$timeLabel = date('h:i A', $start); // 12-hour format with AM/PM for display
$times[$timeValue] = $timeLabel; // Store time in associative array
$start = strtotime('+15 minutes', $start); // Increment by 15 minutes
}
return $times;
}
function drierMachineDetails()
{
$currentMonth = $this->request->getPost('month') ? $this->request->getPost('month') : date('M-Y');
$monthData = $this->getMonthDatesFromInput($currentMonth);
$data['startDate'] = $monthData[0]['database'];
$data['endDate'] = $monthData[count($monthData)-1]['database'];
$data['timeDropdown'] = $this->generateTimeOptions();
$data['drierDetails'] = $this->drierMachineDetails_model
->where('date >=', $data['startDate'])
->where('date <=', $data['endDate'])
->findAll();
$data['datefordropdown'] = $currentMonth;
$data['data'] = $this->fillMissingDates($monthData, $data['drierDetails']);
$this->global['pageTitle'] = 'Drier Details';
$this->loadViews("drierMachineDetails", $this->global, $data, NULL);
}
public function addOrEditdrierMachineDetails()
{
$drierData = json_decode($this->request->getPost('drierData'), true);
if (empty($drierData)) {
echo "No data received.";
return;
}
foreach ($drierData as $row) {
$data = [
'date' => DateTime::createFromFormat('d-m-Y', $row['Date'])->format('Y-m-d'),
'drier_on_time' => $row['Drier On Time'],
'drier_off_time' => $row['Drier Off Time'],
'drier_running_hours' => $row['Running Hrs'],
'supplier_name' => $row['Supplier Name'],
'input_sand_moisture' => $row['i/p Sand Moisture(%)'],
'total_gas_consumption'=> $row['Tot Gas Consumption'],
'gas_per_ton' => $row['Gas Pre Ton'],
'input_sand_qty' => $row['i/p Sand Qty'],
'moisture_loss_qty' => $row['Moisture Loss Qty'],
'sand_dried_qty' => $row['Sand Dried Qty'],
'qty_per_hours' => $row['Qty Per Hrs'],
'dust_qty' => $row['Dust Qty'],
'customer_name' => $row['Customer Name']
];
// Check if a row with the same date already exists
$existingRow = $this->drierMachineDetails_model->where('date', $data['date'])->first();
if ($existingRow) {
// If exists, update the row
$this->drierMachineDetails_model->update($existingRow['id'], $data);
} else {
// If not, insert a new row
$this->drierMachineDetails_model->insert($data);
}
}
echo "Data successfully saved.";
}
}