public function ajaxUpdatePhysicalCurrentQuantityAction()
 {
     $this->_helper->layout->disableLayout();
     $this->_helper->viewRenderer->setNoRender(TRUE);
     $wh_id = $this->_identity->getWarehouseId();
     $data = explode("|", substr($this->_request->data, 0, -1));
     $count = count($data);
     $physical_quantity = 0;
     $current_quantity = 0;
     $em = Zend_Registry::get('doctrine');
     $em->getConnection()->beginTransaction();
     try {
         for ($i = 0; $i < $count; $i++) {
             $flag = 0;
             $placement_physical_summary = $this->_em->getRepository("PhysicalStockTakingDetail")->find($data[$i]);
             if ($placement_physical_summary->getStockBatch() != "") {
                 //$physical_quantity = $placement_physical_summary->getQuantity();
                 $current_quantity = $placement_physical_summary->getStockBatch()->getQuantity();
                 $batch_id = $placement_physical_summary->getStockBatch()->getPkId();
                 $stk_master = new Model_StockMaster();
                 $physical_quantity = $stk_master->getPhysicalBatchQuantity($batch_id);
                 /* $already_exists = $this->_em->getRepository("PhysicalStockTakingDetail")->findBy(array("stockBatch" => $batch_id, "warehouse" => $this->_identity->getWarehouseId()));
                    if (count($already_exists) > 0) {
                    foreach ($already_exists as $physical_stk) {
                    if ($physical_stk->getPkId() != $placement_physical_summary->getPkId()) {
                    $physical_quantity = $physical_quantity + $physical_stk->getQuantity();
                    }
                    }
                    } */
                 if ($current_quantity > $physical_quantity) {
                     $quantity = $current_quantity - $physical_quantity;
                     $type = 13;
                     $placement_physical_summary->setCurrentQuantity($current_quantity);
                     $this->_em->persist($placement_physical_summary);
                     $this->_em->flush();
                 } else {
                     if ($current_quantity < $physical_quantity) {
                         $quantity = $physical_quantity - $current_quantity;
                         $type = 12;
                         $placement_physical_summary->setCurrentQuantity($current_quantity);
                         $this->_em->persist($placement_physical_summary);
                         $this->_em->flush();
                     } else {
                         if ($current_quantity == $physical_quantity) {
                             $flag = 1;
                             $stock_detail = null;
                         }
                     }
                 }
             } else {
                 $stock_batch_model = new Model_StockBatch();
                 $batch_number = $placement_physical_summary->getBatchNumber();
                 $item_pack_size_id = $placement_physical_summary->getItemPackSize()->getPkId();
                 //$production_date = ($placement_physical_summary->getProductionDate() != null) ? $placement_physical_summary->getProductionDate()->format("d/m/Y") : '';
                 $expiry_date = $placement_physical_summary->getExpiryDate()->format("d/m/Y");
                 $vvm_type_id = 2;
                 //$manufacturer_id = $this->_em->getRepository("StakeholderItemPackSizes")->findBy(array('itemPackSize' => $placement_physical_summary->getItemPackSize()->getPkId()));
                 $array = array('number' => $batch_number, 'quantity' => $placement_physical_summary->getQuantity(), 'item_id' => $item_pack_size_id, 'production_date' => '', 'expiry_date' => $expiry_date, 'unit_price' => '0', 'vvm_type_id' => $vvm_type_id, 'manufacturer_id' => $placement_physical_summary->getStakeholderItemPackSize());
                 $batch_id = $stock_batch_model->createBatch($array);
                 $type = 12;
                 $quantity = 0;
                 $multi_batches = $this->_em->getRepository("PhysicalStockTakingDetail")->findBy(array("batchNumber" => $batch_number, "warehouse" => $this->_identity->getWarehouseId(), "physicalStockTaking" => Model_PhysicalStockTakingDetail::STOCKID));
                 if (count($multi_batches) > 0) {
                     foreach ($multi_batches as $add_batch_id) {
                         $add_batch_id->setCurrentQuantity($current_quantity);
                         $ba_id01 = $this->_em->getRepository('StockBatch')->find($batch_id);
                         $add_batch_id->setStockBatch($ba_id01);
                         $this->_em->persist($add_batch_id);
                         $this->_em->flush();
                         $quantity += $add_batch_id->getQuantity();
                     }
                 }
             }
             if ($flag == 0) {
                 $tranaction_type = new Model_TransactionTypes();
                 $type_nature = $tranaction_type->findById($type);
                 $stock_batch_vvm = new StockBatchVvm();
                 $stock_batch_vvm->setQuantity($type_nature['0']['nature'] . $quantity);
                 $stock_batch_vvm->setVvmStage($placement_physical_summary->getVvmStage());
                 $ba_id1 = $this->_em->getRepository('StockBatch')->find($batch_id);
                 $stock_batch_vvm->setStockBatch($ba_id1);
                 $this->_em->persist($stock_batch_vvm);
                 $this->_em->flush();
                 $stock_batch_vvm_id = $stock_batch_vvm->getPkId();
                 $stock_batch_vvm_history = new StockBatchVvmHistory();
                 $stock_batch_vvm_ID = $this->_em->getRepository('StockBatchVvm')->find($stock_batch_vvm_id);
                 $stock_batch_vvm_history->setStockBatchVvm($stock_batch_vvm_ID);
                 $stock_batch_vvm_history->setQuantity($type_nature['0']['nature'] . $quantity);
                 $stock_batch_vvm_history->setVvmStage($placement_physical_summary->getVvmStage());
                 $ba_id2 = $this->_em->getRepository('StockBatch')->find($batch_id);
                 $stock_batch_vvm_history->setStockBatch($ba_id2);
                 $this->_em->persist($stock_batch_vvm_history);
                 $this->_em->flush();
                 $stock_master = new StockMaster();
                 $stock_master->setTransactionDate(new \DateTime(date('Y-m-d')));
                 $tran_type = $this->_em->getRepository('TransactionTypes')->find($type);
                 $stock_master->setTransactionType($tran_type);
                 $warehouse_id = $this->_em->getRepository('Warehouses')->find($wh_id);
                 $stock_master->setFromWarehouse($warehouse_id);
                 $stock_master->setToWarehouse($warehouse_id);
                 $created_by = $this->_em->getRepository('Users')->find($this->_userid);
                 $stock_master->setCreatedBy($created_by);
                 $stock_master->setCreatedDate(new \DateTime(date("Y-m-d")));
                 $adjustment_date = date('d/m/Y');
                 $sm = new Model_StockMaster();
                 $trans = $sm->getTransactionNumber($type, $adjustment_date);
                 $stock_master->setTransactionNumber($trans['trans_no']);
                 $stock_master->setDraft(0);
                 $stock_master->setTransactionCounter($trans['id']);
                 $stock_master->setParentId(0);
                 $this->_em->persist($stock_master);
                 $this->_em->flush();
                 $stock_detail = new StockDetail();
                 $stock_detail->setStockMaster($stock_master);
                 $b_id = $this->_em->getRepository('StockBatch')->find($batch_id);
                 $stock_detail->setStockBatch($b_id);
                 $stock_detail->setQuantity($type_nature[0]['nature'] . $quantity);
                 $stock_detail->setAdjustmentType($type);
                 $stock_detail->setVvmStage($placement_physical_summary->getVvmStage());
                 $stock_detail->setTemporary(0);
                 $stock_detail->setItemUnit($placement_physical_summary->getItemPackSize()->getItemUnit());
                 $this->_em->persist($stock_detail);
                 $this->_em->flush();
                 $stock_ID = $stock_master->getPkId();
                 $stock_batch = new Model_StockBatch();
                 $warehouse_data = new Model_WarehousesData();
                 $stock_batch->adjustQuantityBywarehouse($batch_id);
                 $warehouse_data->addReport($stock_ID, $type);
                 $placement_physical_summary->setStockMaster($stock_master);
                 $this->_em->persist($placement_physical_summary);
                 $this->_em->flush();
             }
             $already_exists = $this->_em->getRepository("PhysicalStockTakingDetail")->findBy(array("stockBatch" => $batch_id, "warehouse" => $this->_identity->getWarehouseId(), "physicalStockTaking" => Model_PhysicalStockTakingDetail::STOCKID));
             if (count($already_exists) > 0) {
                 foreach ($already_exists as $physical_stk) {
                     if ($physical_stk->getPlacementLocation() != null && $physical_stk->getQuantity() > 0) {
                         $placement = new Placements();
                         $placement->setQuantity($physical_stk->getQuantity());
                         $placement->setPlacementLocation($physical_stk->getPlacementLocation());
                         $placement->setStockBatch($physical_stk->getStockBatch());
                         if ($stock_detail != null) {
                             $placement->setStockDetail($stock_detail);
                         }
                         $type_id = $this->_em->getRepository("ListDetail")->find(114);
                         $placement->setPlacementTransactionType($type_id);
                         $created_by = $this->_em->getRepository("Users")->find($this->_userid);
                         $placement->setCreatedBy($created_by);
                         $placement->setIsPlaced(1);
                         $placement->setCreatedDate(new \DateTime(date("Y-m-d")));
                         $vvm = $physical_stk->getVvmStage();
                         $placement->setVvmStage($vvm);
                         $this->_em->persist($placement);
                         $this->_em->flush();
                     }
                 }
             }
         }
         $em->getConnection()->commit();
         echo 1;
     } catch (Exception $e) {
         $em->getConnection()->rollback();
         $em->close();
         echo 0;
     }
 }
 public function addAdjustment()
 {
     $wh_id = $this->_identity->getWarehouseId();
     $ref_no = '';
     $quantity = '';
     $comments = '';
     if (isset($this->form_values['adjustment_date']) && !empty($this->form_values['adjustment_date'])) {
         $adjustment_date = $this->form_values['adjustment_date'];
         list($dd, $mm, $yy) = explode("/", $adjustment_date);
     } else {
         $adjustment_date = date('m/d/Y');
         list($dd, $mm, $yy) = explode("/", $adjustment_date);
     }
     if (isset($this->form_values['ref_no']) && !empty($this->form_values['ref_no'])) {
         $ref_no = $this->form_values['ref_no'];
     }
     if (isset($this->form_values['product']) && !empty($this->form_values['product'])) {
         $product = $this->form_values['product'];
     }
     if (isset($this->form_values['batch_no']) && !empty($this->form_values['batch_no'])) {
         $batch_id = $this->form_values['batch_no'];
     }
     if (isset($this->form_values['available']) && !empty($this->form_values['available'])) {
         $available = $this->form_values['available'];
     }
     if (isset($this->form_values['adjustment_type']) && !empty($this->form_values['adjustment_type'])) {
         $type = $this->form_values['adjustment_type'];
     }
     if (isset($this->form_values['quantity']) && !empty($this->form_values['quantity'])) {
         $quantity = str_replace(',', '', $this->form_values['quantity']);
     }
     if (isset($this->form_values['comments']) && !empty($this->form_values['comments'])) {
         $comments = $this->form_values['comments'];
     }
     if (isset($this->form_values['item_unit_id']) && !empty($this->form_values['item_unit_id'])) {
         $unit = $this->form_values['item_unit_id'];
     }
     if (isset($this->form_values['vvm_stage']) && !empty($this->form_values['vvm_stage'])) {
         $vvm_stage = $this->form_values['vvm_stage'];
     }
     if (isset($this->form_values['purpose']) && !empty($this->form_values['purpose'])) {
         $purpose = $this->form_values['purpose'];
     }
     $is_received = 0;
     if (isset($this->form_values['is_received']) && !empty($this->form_values['is_received'])) {
         $is_received = $this->form_values['is_received'];
     }
     $stock_batch_warehouse = $this->_em->getRepository('StockBatchWarehouses')->find($batch_id);
     $tranaction_type = new Model_TransactionTypes();
     $type_nature = $tranaction_type->findById($type);
     $trans_nature = $type_nature[0]['nature'];
     if ($stock_batch_warehouse->getQuantity() == 0 && $trans_nature == '-') {
         throw new Exception('NEGATIVE_OR_ZERO_QTY');
     }
     list($location, $old_vvm, $placed_qty) = explode("|", $this->form_values['location_id']);
     if ($trans_nature == '+') {
         $old_vvm = $this->form_values['vvm_stage'];
     }
     if ($quantity > $stock_batch_warehouse->getQuantity() && $trans_nature == '-') {
         throw new Exception('ADJ_QTY_GREATER_BATCH_QTY');
     }
     if (!empty($placed_qty)) {
         if ($placed_qty < 0) {
             throw new Exception('PLCD_QTY_LESS_EQUAL_ZERO');
         }
         if ($placed_qty < $quantity && $trans_nature == '-') {
             throw new Exception('ADJ_QTY_LESS_EQUAL_PLCD_QTY');
         }
     }
     $stock_master = new StockMaster();
     $stock_master->setTransactionDate(new \DateTime($yy . "-" . $mm . "-" . $dd));
     $tran_type = $this->_em->getRepository('TransactionTypes')->find($type);
     $stock_master->setTransactionType($tran_type);
     $stock_master->setTransactionReference($ref_no);
     $warehouse_id = $this->_em->getRepository('Warehouses')->find($wh_id);
     $stock_master->setFromWarehouse($warehouse_id);
     $stock_master->setToWarehouse($warehouse_id);
     $user_id = $this->_em->getRepository('Users')->find($this->_user_id);
     $stock_master->setCreatedBy($user_id);
     $stock_master->setModifiedBy($user_id);
     $stock_master->setCreatedDate(App_Tools_Time::now());
     $stock_master->setModifiedDate(App_Tools_Time::now());
     $stock_master->setComments($comments);
     $trans_no = $this->getTransactionNumber($type, $adjustment_date);
     $stock_master->setTransactionNumber($trans_no['trans_no']);
     if (!empty($purpose)) {
         $stakeholder_activity = $this->_em->getRepository('StakeholderActivities')->find($purpose);
         $stock_master->setStakeholderActivity($stakeholder_activity);
     }
     //$stock_master->BatchID = $batch_id;
     $stock_master->setDraft(0);
     $stock_master->setTransactionCounter($trans_no['id']);
     $stock_master->setParentId(0);
     $this->_em->persist($stock_master);
     $this->_em->flush();
     $stock_ID = $stock_master->getPkId();
     $stock_detail = new StockDetail();
     $s_id = $this->_em->getRepository('StockMaster')->find($stock_ID);
     $stock_detail->setStockMaster($s_id);
     $b_id = $this->_em->getRepository('StockBatchWarehouses')->find($batch_id);
     $stock_detail->setStockBatchWarehouse($b_id);
     $stock_detail->setQuantity($type_nature[0]['nature'] . $quantity);
     $stock_detail->setAdjustmentType($type);
     $stock_detail->setTemporary(0);
     if (empty($old_vvm) || $old_vvm == null || $old_vvm == '') {
         $old_vvm = 0;
     }
     $vvms = $this->_em->getRepository("VvmStages")->find($old_vvm);
     $stock_detail->setVvmStage($vvms);
     $u_id = $this->_em->getRepository('ItemUnits')->find($unit);
     $stock_detail->setItemUnit($u_id);
     $stock_detail->setIsReceived($is_received);
     $stock_detail->setCreatedBy($user_id);
     $stock_detail->setModifiedBy($user_id);
     $stock_detail->setCreatedDate(App_Tools_Time::now());
     $stock_detail->setModifiedDate(App_Tools_Time::now());
     $this->_em->persist($stock_detail);
     $this->_em->flush();
     $stock_detail_id = $stock_detail->getPkId();
     $stock_batch = new Model_StockBatch();
     $warehouse_data = new Model_HfDataMaster();
     $stock_batch->adjustQuantityBywarehouse($batch_id);
     $warehouse_data->addReport($stock_ID, $type);
     if (!empty($location)) {
         $placements = new Model_Placements();
         $placements->form_values = array('batch_id' => $batch_id, 'placement_loc_id' => $location, 'detail_id' => $stock_detail_id, 'user_id' => $this->_user_id, 'created_date' => date("Y-m-d"));
         /* if (!empty($old_vvm) && !empty($this->form_values['vvm_stage']) && $old_vvm != $this->form_values['vvm_stage']) {
                       $placements->form_values['vvmstage'] = $old_vvm;
                       $placements->form_values['is_placed'] = 0;
                       $placements->form_values['quantity'] = "-" . $quantity;
                       $placements->form_values['placement_loc_type_id'] = 115;
                       $placements->add();
         
                       $placements->form_values['vvmstage'] = $this->form_values['vvm_stage'];
                       $placements->form_values['is_placed'] = 1;
                       $placements->form_values['quantity'] = $quantity;
                       $placements->form_values['placement_loc_type_id'] = 114;
                       $placements->add();
         
                       $old_vvm = $this->form_values['vvm_stage'];
                       } */
         if ($type_nature[0]['nature'] == '-') {
             // Check if batch exists in cold room
             $str_sql = $this->_em->createQueryBuilder()->select("SUM(ps.quantity) as quantity")->from("Placements", "ps")->where("ps.placementLocation = " . $location)->andWhere("ps.stockBatchWarehouse = " . $batch_id);
             $row = $str_sql->getQuery()->getResult();
             if (count($row) > 0) {
                 $qty = $row[0]['quantity'];
                 if ($qty < $quantity) {
                     // If pick quantity is greater than placed quantity
                     throw new Exception('PICK_ERROR');
                 }
             }
             $loc_type = 115;
             $is_placed = 0;
         } else {
             $loc_type = 114;
             $is_placed = 1;
         }
         $placements->form_values['vvmstage'] = $old_vvm;
         $placements->form_values['is_placed'] = $is_placed;
         $placements->form_values['quantity'] = $type_nature[0]['nature'] . $quantity;
         $placements->form_values['placement_loc_type_id'] = $loc_type;
         $placements->add();
     }
     return $stock_master->getTransactionNumber();
 }