We’re having major lag issue while updating production status and updating production quantity. We’re using JQUERY AJAX post method to do that work. Things work smartly and with good speed, when we comment-out the function “recalculateProduction()” inside the “AppController.php” file for both updating status and updating quantity.
The function “recalculateProduction()” consist of huge calculation, updating records of different DB tables and deleting records from different tables.
public function recalculateProduction($production_id, $action_performed_by){
$this->loadModel(‘Production’);
$this->loadModel(‘ProductionItem’);
$this->loadModel(‘ProductionOrder’);
$this->loadModel(‘ProductionRecipe’);
$this->loadModel(‘ProductionIngredient’);
$this->loadModel(‘ProductionRawMaterial’);
$this->loadModel(‘ProductionOrderProductionRecipe’);
$this->loadModel(‘ProductionOrderProductionIngredient’);
$this->loadModel(‘ProductionOrderProductionRawMaterial’);
$production = $this->Production->read(null, $production_id);
$this->ProductionItem->updateAll(array('order_case_qty'=>0, 'quantity_lbs'=>0, 'production_case_qty'=>0), array('ProductionItem.production_id'=>$production_id));
$this->ProductionRecipe->updateAll(array('qty_lbs'=>0, 'batch_qty_lbs'=>0, 'batches'=>0, 'batch_size'=>0, 'actual_lbs'=>0, 'percent_yield'=>0), array('ProductionRecipe.production_id'=>$production_id));
$this->ProductionIngredient->updateAll(array('qty_lbs'=>0), array('ProductionIngredient.production_id'=>$production_id));
$this->ProductionRawMaterial->updateAll(array('qty_lbs'=>0), array('ProductionRawMaterial.production_id'=>$production_id));
$this->ProductionOrderProductionRecipe->deleteAll(array('ProductionOrderProductionRecipe.production_order_id IN (SELECT id FROM imrp_production_order ProductionOrder WHERE ProductionOrder.production_id='.$production_id.')'));
$this->ProductionOrderProductionIngredient->deleteAll(array('ProductionOrderProductionIngredient.production_order_id IN (SELECT id FROM imrp_production_order ProductionOrder WHERE ProductionOrder.production_id='.$production_id.')'));
$this->ProductionOrderProductionRawMaterial->deleteAll(array('ProductionOrderProductionRawMaterial.production_order_id IN (SELECT id FROM imrp_production_order ProductionOrder WHERE ProductionOrder.production_id='.$production_id.')'));
$this->ProductionOrder->recursive = 0;
$production_orders = $this->ProductionOrder->find('all', array('conditions'=>array('ProductionOrder.status IN (0, 1)', 'ProductionOrder.production_id'=>$production_id)));
if(count($production_orders)>0){
foreach($production_orders as $production_order){
$this->processOrderDetails($production_id, $production_order['ProductionOrder']['item_id'], $production_order['ProductionOrder']['id'], $production['Production']['production_date'], $production_order['ProductionOrder']['quantity_lbs'], $production_order['ProductionOrder']['quantity_cases'], $action_performed_by);
}
}
$this->ProductionItem->deleteAll(array('ProductionItem.production_id'=>$production_id, 'ProductionItem.order_case_qty'=>0.00, 'ProductionItem.quantity_lbs'=>0.00, 'ProductionItem.production_case_qty'=>0.00));
$this->ProductionRecipe->deleteAll(array('ProductionRecipe.production_id'=>$production_id, 'ProductionRecipe.qty_lbs'=>0.00, 'ProductionRecipe.batch_qty_lbs'=>0.00000, 'ProductionRecipe.batches'=>0.00000, 'ProductionRecipe.batch_size'=>0, 'ProductionRecipe.actual_lbs'=>0.00, 'ProductionRecipe.percent_yield'=>0.000));
$this->ProductionIngredient->deleteAll(array('ProductionIngredient.production_id'=>$production_id, 'ProductionIngredient.qty_lbs'=>0.00));
$this->ProductionRawMaterial->deleteAll(array('ProductionRawMaterial.production_id'=>$production_id, 'ProductionRawMaterial.qty_lbs'=>0.00));
$this->processProductionRecipes($production_id, $action_performed_by);
}
Can you guys see any optimization in the above function code? We already applied “recursive” but it’s not making any difference. Your help will be highly appreciated.