public function purposeTransferManagementAction()
 {
     $form = new Form_PurposeTransfer();
     $stock_detail = new Model_StockDetail();
     $msg = '';
     if ($this->_request->isPost()) {
         $product_id = $this->_request->getPost('product');
         $batch_id = $this->_request->getPost('batch');
         //          $msg = ''; // Undefined variable: msg (when page initially loaded)
         $em = Zend_Registry::get('doctrine');
         $em->getConnection()->beginTransaction();
         //            try {
         $quantity = $this->_request->quantity;
         $newpurpose = $this->_request->newpurpose;
         $comments = $this->_request->comments;
         $toproducts = $this->_request->toproducts;
         if (!is_null($quantity)) {
             foreach ($quantity as $key => $value) {
                 if (!empty($value) && $value > 0) {
                     $updatedqty = $value;
                     list($batchid, $vvmid, $locationid, $activity_id) = explode("_", $key);
                     $batch_detail = $this->_em->getRepository("StockBatchWarehouses")->find($batchid);
                     $microtime = strtotime(date("Y-m-d H:i:s"));
                     $stock_master = new Model_StockMaster();
                     $data = array('adjustment_date' => date("d/m/Y"), 'ref_no' => '', 'product' => $batch_detail->getStockBatch()->getPackInfo()->getStakeholderItemPackSize()->getItemPackSize()->getPkId(), 'batch_no' => $batchid, 'adjustment_type' => 17, 'quantity' => $updatedqty, 'comments' => $comments, 'item_unit_id' => $batch_detail->getStockBatch()->getPackInfo()->getStakeholderItemPackSize()->getItemPackSize()->getItemUnit()->getPkId(), 'vvm_stage' => $vvmid, 'location_id' => $locationid . "|" . $vvmid, 'purpose' => $activity_id, 'is_received' => $microtime);
                     $stock_master->form_values = $data;
                     $stock_master->addAdjustment();
                     $stock_batch = new Model_StockBatch();
                     $array = array('wh_id' => $batch_detail->getWarehouse()->getPkId(), 'number' => $batch_detail->getStockBatch()->getNumber(), 'item_id' => $toproducts[$key], 'expiry_date' => $batch_detail->getStockBatch()->getExpiryDate()->format("d/m/Y"), 'quantity' => $updatedqty, 'production_date' => $batch_detail->getStockBatch()->getProductionDate() != null ? $batch_detail->getStockBatch()->getProductionDate()->format("d/m/Y") : '', 'vvm_type_id' => $batch_detail->getStockBatch()->getVvmType()->getPkId(), 'unit_price' => $batch_detail->getStockBatch()->getUnitPrice(), 'manufacturer_id' => $batch_detail->getStockBatch()->getPackInfo()->getStakeholderItemPackSize()->getPkId());
                     $transbatchid = $stock_batch->createBatch($array);
                     $data2 = array('adjustment_type' => 16, 'batch_no' => $transbatchid, 'adjustment_date' => date("d/m/Y"), 'ref_no' => '', 'product' => $toproducts[$key], 'quantity' => $updatedqty, 'comments' => $comments, 'item_unit_id' => $batch_detail->getStockBatch()->getPackInfo()->getStakeholderItemPackSize()->getItemPackSize()->getItemUnit()->getPkId(), 'vvm_stage' => $vvmid, 'location_id' => $locationid . "|" . $vvmid, 'purpose' => $newpurpose[$key], 'is_received' => $microtime);
                     $stock_master->form_values = $data2;
                     $stock_master->addAdjustment();
                     //Log Entry START
                     $history = new PurposeTransferHistory();
                     $from_batch = $this->_em->getRepository("StockBatchWarehouses")->find($batchid);
                     $history->setFromStockBatchWarehouse($from_batch);
                     $to_batch = $this->_em->getRepository("StockBatchWarehouses")->find($transbatchid);
                     $history->setToStockBatchWarehouse($to_batch);
                     $from_activity = $this->_em->getRepository("StakeholderActivities")->find($activity_id);
                     $history->setFromActivity($from_activity);
                     $to_activity = $this->_em->getRepository("StakeholderActivities")->find($newpurpose[$key]);
                     $history->setToActivity($to_activity);
                     $created_by = $this->_em->getRepository("Users")->find($this->_userid);
                     $history->setCreatedBy($created_by);
                     $history->setModifiedBy($created_by);
                     $history->setCreatedDate(App_Tools_Time::now());
                     $history->setModifiedDate(App_Tools_Time::now());
                     $history->setQuantity(ABS($updatedqty));
                     $transaction_type = $this->_em->getRepository("TransactionTypes")->find(Model_TransactionTypes::CHANGE_PURPOSE_POSITIVE);
                     $history->setTransactionType($transaction_type);
                     $this->_em->persist($history);
                     $this->_em->flush();
                     //Log Entry END
                     $stock_batch->adjustQuantityByWarehouse($transbatchid);
                     $msg = 'Purpose has been changed successfully!';
                     $product_id = $batch_detail->getStockBatch()->getPackInfo()->getStakeholderItemPackSize()->getItemPackSize()->getPkId();
                     $batch_id = $batchid;
                 }
             }
         }
         $this->view->result = $stock_detail->purposeTransferManagement($batch_id);
         //$this->view->h_result = $stock_detail->getPurposeTransferHistory($product_id);
         //$this->view->history = $stock_detail->purposeTransferHistory($batch_id);
         $form->product->setValue($product_id);
         $this->view->batch = $batch_id;
         $em->getConnection()->commit();
         //            } catch (Exception $e) {
         //                $em->getConnection()->rollback();
         //                $em->close();
         //                App_FileLogger::info($e);
         //            }
         $this->view->opening_balance_result = $stock_detail->getOpeningBalancePurpose($product_id, $batch_id);
         $this->view->closing_balance_result = $stock_detail->getClosingBalancePurpose($product_id, $batch_id);
         $this->view->h_result = $stock_detail->getPurposeTransferHistory($product_id);
     }
     $this->view->form = $form;
     $this->view->msg = $msg;
 }