diff --git a/app/Controllers/StockController.php b/app/Controllers/StockController.php index 3be86c1e..d0fd93ac 100644 --- a/app/Controllers/StockController.php +++ b/app/Controllers/StockController.php @@ -11,6 +11,7 @@ use App\Models\FactoryVehicleDieselStockSummaryModel; use App\Models\IncomingSilicaSandDetailsModel; use App\Models\Inwardgateregister_model; use App\Models\Rawmaterialdetails_model; +use App\Models\BagStockDetailsModel; use CodeIgniter\HTTP\ResponseInterface; use DateTime; @@ -26,6 +27,8 @@ class StockController extends BaseController protected $incomingSilicaSandDetails_model; protected $inwardGateRegister_model; protected $rawmaterialdetails_model; + protected $bagStockDetails_model; + protected $Rawmaterialdetails_model; /** * This is default constructor of the class @@ -42,6 +45,8 @@ class StockController extends BaseController $this->rawmaterialdetails_model = new Rawmaterialdetails_model(); $this->incomingSilicaSandDetails_model = new IncomingSilicaSandDetailsModel(); $this->inwardGateRegister_model = new Inwardgateregister_model(); + $this->bagStockDetails_model = new BagStockDetailsModel(); + $this->session = session(); helper('form'); @@ -257,15 +262,133 @@ class StockController extends BaseController public function loadView_bagStockDetails(){ + 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'); + } + $data=[]; + $data['month']=$month; + $this->global['pageTitle']='Bag Stock Details'; - return $this->loadViews("bagStockDetails", $this->global, $data, NULL); + $startDate = "$month-01"; + $endDate = date("Y-m-t", strtotime($startDate)); + $data['bagStockDetails'] = $this->bagStockDetails_model + ->where('date >=', $startDate) + ->where('date <=', $endDate) + ->groupBy(['materialCode','date']) + ->orderBy('date','asc') + ->orderBy('materialCode','asc') + ->findAll(); + + // Initialize an empty array to hold the grouped data + $data['groupedBagStockDetails'] = []; + + // Temporary array to store grouped data by date + $groupedByDate = []; + + // Group by 'date' + foreach ($data['bagStockDetails'] as $detail) { + $date = $detail['date']; + + // Initialize the array for this date if it doesn't exist + if (!isset($groupedByDate[$date])) { + $groupedByDate[$date] = []; + } + + // Append the detail to the sub-array for this date + $groupedByDate[$date][] = $detail; + } + + // Reset the structure to have indexed arrays without the date keys + $data['groupedBagStockDetails'] = array_values($groupedByDate); + + + + $bagMaterials= $this->rawmaterialdetails_model->getAllBagMaterialCode(); + + + + foreach($bagMaterials as $key => $bagMaterial){ + $materialCode = $bagMaterial['MaterialCode']; + $bagMaterials[$key]['customers'] = $this->bagStockDetails_model + ->select('customer') + ->where('date >=', $startDate) + ->where('date <=', $endDate) + ->where('materialCode', $materialCode) + ->first()['customer'] ?? ''; + } + + $bagMaterialcount = count($bagMaterials); + + $data['bagMaterialcount'] = $bagMaterialcount; + $data['bagMaterials'] = $bagMaterials; + + $date = DateTime::createFromFormat('Y-m', $month); + + $formattedDate = $date->format('M-Y'); + + $data['month']=$formattedDate; + + return $this->loadViews("bagStockDetails", $this->global, $data, NULL); } - public function updateBagStockDetails(){} + public function updateBagStockDetails(){ + + $postData = $this->request->getPost(); + + $updateBagStockDetailsData = json_decode($postData['updateBagStockDetails'], true); + + foreach($updateBagStockDetailsData as $data){ + + $date = $data['date']; + $materialCode = $data['materialCode']; + + + $dbData= [ + 'date' =>$data['date'], + 'materialCode' =>$data['materialCode'], + 'customer' =>$data['customer'], + 'opening' =>$data['opening'], + 'receipt' =>$data['receipt'], + 'used' =>$data['used'], + 'balanceStock' =>$data['balanceStock'], + ]; + + $resultExists=$this->bagStockDetails_model + ->where('date',$date) + ->where('materialCode',$materialCode) + ->first(); + + + if(!empty($resultExists)){ + + $updated=$this->bagStockDetails_model + ->where('date',$date) + ->where('materialCode',$materialCode) + ->set($dbData) + ->update(); + + }else{ + $inserted = $this->bagStockDetails_model->insert($dbData); + } + + } + + echo "Data Saved Successfully"; + + } diff --git a/app/Models/Rawmaterialdetails_model.php b/app/Models/Rawmaterialdetails_model.php index f5d792f3..45981afe 100755 --- a/app/Models/Rawmaterialdetails_model.php +++ b/app/Models/Rawmaterialdetails_model.php @@ -136,6 +136,20 @@ class Rawmaterialdetails_model extends Model return $materialCodes = array_column($materialResults, 'MaterialCode'); } + function getAllBagMaterialCode(){ + + $builder = $this->db->table('t_materialmaster') + ->select('MaterialCode , MaterialName') + ->where('IsActive', 1) + ->where('Category', 'bag') + ->orderBy('MaterialCode', 'asc'); + $query = $builder->get(); + $materialResults = $query->getResultArray(); + + return $materialResults; + + } + function getMaterialstock($MaterialCode, $currentdate) diff --git a/app/Views/bagStockDetails.php b/app/Views/bagStockDetails.php index cfde7953..ff8bd25c 100644 --- a/app/Views/bagStockDetails.php +++ b/app/Views/bagStockDetails.php @@ -105,9 +105,10 @@ border: 1px solid #ccc; color: #ffffff; font-weight: bold; - padding: 2px 4px; + padding: 6px 10px;; text-align: center; } + ._Separador { background-color: #fff; @@ -124,6 +125,7 @@ .celda_normal { /*background-color: #fff;*/ /* */ + text-align: center; border: 1px solid #ccc; padding: 2px 4px; } @@ -195,12 +197,9 @@
- - - " class="form-control mt-2" data-provide="datepicker" data-date-format="M-yyyy" data-date-min-view-mode="1"> -
@@ -208,104 +207,206 @@
+ +
- +
- + - - - - + + + + + - - + + + + - - - + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + - + $bagStockDetails){ + ?> + + + + + + $bagStock){ ?> + + format('d-m-Y'); + ?> + + + + + + + + + + + + + + + + + + + + + + + + modify('first day of this month')->format('Y-m-d'); + $endDate = $date->modify('last day of this month')->format('Y-m-d'); + + $datesInMonth = []; + + + $period = new DatePeriod( + new DateTime($startDate), + new DateInterval('P1D'), + (new DateTime($endDate))->modify('+1 day') + ); + + foreach ($period as $day) { + $datesInMonth[] = $day->format('Y-m-d'); + } + + ?> + + $dateInMonth){ ?> - - - - - - - - + $bagMaterial){ ?> + - - - - - - - - + + format('d-m-Y'); + ?> + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - -
material material material 1material 2 + + +
Customer
Customer - + + -
Date Date material customerOpeningReceiptUsedBalance StockDate material customerOpeningReceiptUsedBalance Stock
Date Opening StockReceiptUsedBalance Stock
+ + + + + oninput="openingStockChange(this)" + contenteditable="true" + + + > + + + + + +
23-10-202423-10-2024 material 1 customer 147211212-47223-10-2024 material 2 customer 247211212-472 + + + + oninput="openingStockChange(this)" + contenteditable="true" + + + >0000
24-10-202424-10-2024 material 1 customer 147211212-47224-10-2024 material 2 customer 247211212-472
@@ -339,13 +440,15 @@ $(document).ready(function () { - - + $('#saveId').click(function () { //table to json function - var updateBagStockDetails = tableToJSON(); + var updateBagStockDetails = tableToJson(); + + + alert('Updation may take a while, And we appreciate your patience..!!'); $('#loader').show(); $.ajax({ @@ -358,18 +461,26 @@ success: function (data) { if (data) { $('#loader').hide(); - + console.log(data); alert(data); - } + }, + error: function(xhr, status, error) { + + alert("An error occurred while processing the request. Please try again."); + console.error("Error Code:", xhr.status); + console.error("Error Message:", error); + console.error("Response Text:", xhr.responseText); + }, + complete: function() { + $('#loader').hide(); } }); }); - }); @@ -389,7 +500,7 @@ for (let i = 1; i < rowCells.length; i += 7) { const rowData = { date: rowCells.eq(i).text().trim(), - material: rowCells.eq(i+1).text().trim(), + materialCode: rowCells.eq(i+1).text().trim(), customer: rowCells.eq(i+2).text().trim(), opening: rowCells.eq(i+3).text().trim(), receipt: rowCells.eq(i+4).text().trim(), @@ -404,8 +515,112 @@ return JSON.stringify(rows, null, 2); } + + - console.log(tableToJson()); + \ No newline at end of file + updateStockValue( date,materialCode,currentBalanceStock ) + + + } catch(error){ + + console.error("There is an error updating stock ..!!" + error); + + } + } + + + + function usedStockChange(tdElement) { + + try { + let dataId = tdElement.getAttribute('data-id'); + let [date,materialCode] = tdElement.getAttribute('data-id').split(' '); + let openingStock = document.querySelector(`td.openingStock[data-id="${dataId}"]`).innerText; + let receiptStock = document.querySelector(`td.receiptStock[data-id="${dataId}"]`).innerText; + let usedStock = tdElement.innerText; + let currentBalanceStock = ( Number(openingStock) + Number(receiptStock) ) - Number(usedStock); + document.querySelector(`td.balanceStock[data-id="${dataId}"]`).innerText=currentBalanceStock; + + updateStockValue( date,materialCode,currentBalanceStock ) + + + } catch (error) { + + console.error("There is an error updating stock ..!!" + error); + + } + } + + + + + diff --git a/app/Views/incomingSilicaSandDetails.php b/app/Views/incomingSilicaSandDetails.php index ec8a92c7..fc720e10 100644 --- a/app/Views/incomingSilicaSandDetails.php +++ b/app/Views/incomingSilicaSandDetails.php @@ -528,7 +528,17 @@ alert(data); - } + }, + error: function(xhr, status, error) { + + alert("An error occurred while processing the request. Please try again."); + console.error("Error Code:", xhr.status); + console.error("Error Message:", error); + console.error("Response Text:", xhr.responseText); + }, + complete: function() { + $('#loader').hide(); + } } });