I am new to Magento development and I am using a custom MySQL query in some places like in observer/plugin etc.
These codes are repeating for all these files. so I want to make a new reusable function/class and want to call this method instead of writing the same thing in all files.
I don't know even to create controller and all, could you please help me to create this?
$modelMaterials = $objectManager->create('Vendor\Module\Model\Grid');
$dataMaterials=$modelMaterials->getCollection()->addFieldToFilter('entity_id', array('in' => array($material_id)))->setPageSize(1)->setOrder('entity_id','DESC');;
if($dataMaterials->getSize()){
$materialsarr = $dataMaterials->getFirstItem();
$weight = $subject->getWeight();
$fineWeight = $data->getFineWeight();
$metalPrice = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
$metalPrice += (double)$data->getSurcharge();
$metalValue = (double)$materialsarr->getAmount() * ((float)$weight + (float)$fineWeight);
$price = (double)$materialsarr->getAmount() * (double)$weight;
$tx_rates = (double)$materialsarr->getTax() / 100;
$tax = $tx_rates * (double)$metalPrice;
$specialPrice = $subject->getSpecialPrice();
}