public function ajaxMergeBatchQuantityAction() { $this->_helper->layout->disableLayout(); $this->_helper->viewRenderer->setNoRender(TRUE); $em = Zend_Registry::get('doctrine'); $em->getConnection()->beginTransaction(); try { $data = explode("|", substr($this->_request->data, 0, -1)); $count = count($data); $merge_into_batch = $this->_request->merge; for ($i = 0; $i < $count; $i++) { $stock_batch[] = $this->_em->getRepository("StockBatch")->find($data[$i]); } $stock_batch_merge = $this->_em->getRepository("StockBatch")->find($merge_into_batch); $quantity = 0; for ($i = 0; $i < $count; $i++) { $quantity = $quantity + $stock_batch[$i]->getQuantity(); $batch_id = $stock_batch[$i]->getPkId(); $stock_detail = $this->_em->getRepository("StockDetail")->findBy(array("stockBatch" => $batch_id)); if (count($stock_detail) > 0) { foreach ($stock_detail as $sd) { $sd->setStockBatch($stock_batch_merge); $this->_em->persist($sd); } $this->_em->flush(); } if ($merge_into_batch != $batch_id) { $this->_em->remove($stock_batch[$i]); } } $stock_batch_merge->setQuantity($quantity); $this->_em->persist($stock_batch_merge); $this->_em->flush(); $stock_master = new Model_StockMaster(); $stock_master->adjustQuantityByWarehouse($merge_into_batch, $stock_batch_merge->getWarehouse()->getPkId()); $em->getConnection()->commit(); echo 1; } catch (Exception $e) { $em->getConnection()->rollback(); $em->close(); echo 0; } }