public function uploadReceiveConsignments()
 {
     $params = $this->form_values;
     $wh_id = $params['wh_id'];
     $rec_id = $params['rec_id'];
     $batch_no = $params['batch_no'];
     $qty = str_replace(",", "", $params['qty']);
     $voucher = $params['voucher'];
     $consignments_location = $this->_em->getRepository("PipelineConsignmentsPlacements")->findBy(array("pipelineConsignment" => $rec_id));
     if (count($consignments_location) > 0) {
         foreach ($consignments_location as $cl) {
             $location_id[] = $cl->getPlacementLocation()->getPkId();
         }
     }
     $str_qry = $this->_em->createQueryBuilder()->select("pc.masterId")->from("PipelineConsignments", "pc")->where("pc.voucherNumber = '" . $voucher . "'")->andWhere("pc.masterId > 0")->andWhere("pc.toWarehouse = " . $wh_id);
     $stock_master_id = $str_qry->getQuery()->getResult();
     if (count($stock_master_id) > 0) {
         $master_id = $stock_master_id[0]['masterId'];
     } else {
         $master_id = 0;
     }
     $pipeline_consignments = $this->_em->getRepository("PipelineConsignments")->find($rec_id);
     /**
      * Record must exists
      */
     if (count($pipeline_consignments) == 0) {
         $result_msg = array("message" => "Selected record id {$rec_id} does not exist in pipeline consignments. Please update your data.");
         App_FileLogger::info($result_msg['message']);
         return $result_msg;
     }
     /**
      * Check if we need to create new voucher or add detail entry in existing voucher
      * If 0 then New Voucher
      * If greater then 0 then Add in Existing voucher 
      */
     if ($master_id == 0) {
         $tr_date = date("d/m/Y");
         $obj_master = new Model_StockMaster();
         $trans = $obj_master->getTransactionNumber(1, $tr_date, $wh_id);
         $stock_master = new StockMaster();
         $stock_master->setTransactionDate(new DateTime());
         $stock_master->setTransactionNumber($trans['trans_no']);
         $stock_master->setTransactionCounter($trans['id']);
         $stock_master->setTransactionReference($pipeline_consignments->getReferenceNumber());
         $stock_master->setDraft(0);
         $stock_master->setComments($pipeline_consignments->getDescription());
         $type = $this->_em->getRepository("TransactionTypes")->find(1);
         $stock_master->setTransactionType($type);
         $stock_master->setFromWarehouse($pipeline_consignments->getFromWarehouse());
         $stock_master->setToWarehouse($pipeline_consignments->getToWarehouse());
         $stock_master->setParentId(0);
         $stock_master->setStakeholderActivity($pipeline_consignments->getStakeholderActivity());
         $stock_master->setCreatedBy($pipeline_consignments->getCreatedBy());
         $stock_master->setCreatedDate(new DateTime(date("Y-m-d H:i:s")));
         $this->_em->persist($stock_master);
         $this->_em->flush();
         $master_id = $stock_master->getPkId();
     }
     $array = array("item_id" => $pipeline_consignments->getItemPackSize()->getPkId(), "number" => $batch_no, "wh_id" => $pipeline_consignments->getToWarehouse()->getPkId());
     $batch_id = $this->checkBatch($array);
     /**
      * Check if Batch exists or not
      * If 0 then create new batch
      * If greater then 0 then add quantity in existing batch
      */
     if ($batch_id === 0) {
         $stock_batch = new StockBatch();
         $stock_batch->setNumber(strtoupper($batch_no));
         $stock_batch->setExpiryDate($pipeline_consignments->getExpiryDate());
         $stock_batch->setQuantity($qty);
         $stock_batch->setStatus('Stacked');
         $stock_batch->setUnitPrice(0);
         $stock_batch->setProductionDate($pipeline_consignments->getProductionDate());
         $stock_batch->setLastUpdate(new DateTime(date("Y-m-d")));
         $stock_batch->setItemPackSize($pipeline_consignments->getItemPackSize());
         $vvm_type = $this->_em->getRepository("VvmTypes")->find(2);
         $stock_batch->setVvmType($vvm_type);
         $stock_batch->setWarehouse($pipeline_consignments->getToWarehouse());
         $stock_batch->setStakeholderItemPackSize($pipeline_consignments->getManufacturer());
         $this->_em->persist($stock_batch);
         $this->_em->flush();
         $batch_id = $stock_batch->getPkId();
     }
     /*
      * Add Entry in stock detail against selected master voucher
      */
     $stock_detail = new StockDetail();
     $stock_detail->setQuantity($qty);
     $stock_detail->setTemporary(0);
     $vvms = $this->_em->getRepository("VvmStages")->find(1);
     $stock_detail->setVvmStage($vvms);
     $stock_detail->setIsReceived(1);
     $stock_detail->setAdjustmentType(1);
     $stock_master = $this->_em->getRepository("StockMaster")->find($master_id);
     $stock_detail->setStockMaster($stock_master);
     $stock_batch = $this->_em->getRepository("StockBatch")->find($batch_id);
     $stock_detail->setStockBatch($stock_batch);
     $stock_detail->setItemUnit($pipeline_consignments->getItemPackSize()->getItemUnit());
     $this->_em->persist($stock_detail);
     $this->_em->flush();
     if (count($location_id) > 0) {
         foreach ($location_id as $plac_id) {
             $plac_loc_id = $this->_em->getRepository("PlacementLocations")->find($plac_id);
             /*
              * Add entry in Placement table
              */
             $placements = new Placements();
             $placements->setQuantity($qty);
             $vvms = $this->_em->getRepository("VvmStages")->find(1);
             $placements->setVvmStage($vvms);
             $placements->setIsPlaced(1);
             $placements->setPlacementLocation($plac_loc_id);
             $placements->setStockBatch($stock_batch);
             $placements->setStockDetail($stock_detail);
             $trans_type = $this->_em->getRepository("ListDetail")->find(Model_ListDetail::STOCK_PLACEMENT);
             $placements->setPlacementTransactionType($trans_type);
             $placements->setCreatedBy($pipeline_consignments->getCreatedBy());
             $placements->setCreatedDate(new DateTime(date("Y-m-d H:i:s")));
             $this->_em->persist($placements);
             $this->_em->flush();
         }
     }
     /*
      * Update Received Qty in Future Arrivals table
      */
     //$update_qty = $pipeline_consignments->getReceivedQuantity() + $qty;
     //if ($update_qty <= $pipeline_consignments->getQuantity()) {
     $pipeline_consignments->setReceivedQuantity($qty);
     $pipeline_consignments->setMasterId($master_id);
     $this->_em->persist($pipeline_consignments);
     $this->_em->flush();
     //}
     /*
      * Adjust Batch Quantity By Warehouse
      */
     $this->adjustQuantityByWarehouse($batch_id, $pipeline_consignments->getToWarehouse()->getPkId());
     /*
      * Adjust Warehouse data for selected month and item
      */
     $warehouse_data = new Model_WarehousesData();
     $warehouse_data->form_values = array('report_month' => date("m"), 'report_year' => date("Y"), 'item_id' => $pipeline_consignments->getItemPackSize()->getPkId(), 'warehouse_id' => $pipeline_consignments->getToWarehouse()->getPkId(), 'created_by' => $pipeline_consignments->getCreatedBy()->getPkId());
     $warehouse_data->adjustStockReport();
     $stock_master = $this->_em->getRepository("StockMaster")->find($master_id);
     return "V" . base64_encode($stock_master->getTransactionNumber() . "|" . $stock_master->getPkId());
 }
 public function setLastUpdate($lastUpdate)
 {
     $this->__load();
     return parent::setLastUpdate($lastUpdate);
 }
 public function autoReceiveData($stock_master_id)
 {
     if ($this->getWarehouseLevelByStockId($stock_master_id) == 6) {
         $master_result = $this->_em->getRepository("StockMaster")->find($stock_master_id);
         $trans_date = $master_result->getTransactionDate();
         $to_wh_id = $master_result->getToWarehouse()->getPkId();
         $new_date = $trans_date->format("d/m/Y H:i:s");
         $trans_no = $this->getTransactionNumber(1, $new_date);
         $new_master = new StockMaster();
         $new_master->setTransactionDate($trans_date);
         $new_master->setTransactionNumber($trans_no['trans_no']);
         $new_master->setTransactionCounter($trans_no['id']);
         $new_master->setTransactionReference($master_result->getTransactionReference());
         $new_master->setDraft(0);
         $new_master->setComments($master_result->getComments());
         $new_master->setParentId($stock_master_id);
         $new_master->setCampaignId($master_result->getCampaignId());
         $new_master->setCreatedDate($master_result->getCreatedDate());
         $new_master->setModifiedDate(App_Tools_Time::now());
         $new_master->setStakeholderActivity($master_result->getStakeholderActivity());
         $new_user_id = $this->getUserIdByWarehouse($to_wh_id);
         $new_master->setCreatedBy($new_user_id);
         $modified_by = $this->_em->getRepository('Users')->find($this->_user_id);
         $new_master->setModifiedBy($modified_by);
         $new_master->setFromWarehouse($master_result->getFromWarehouse());
         $new_master->setToWarehouse($master_result->getToWarehouse());
         $trans_type = $this->_em->getRepository("TransactionTypes")->find(1);
         $new_master->setTransactionType($trans_type);
         $this->_em->persist($new_master);
         $this->_em->flush();
         $detail_result = $this->_em->getRepository("StockDetail")->findBy(array("stockMaster" => $stock_master_id));
         if (count($detail_result) > 0) {
             foreach ($detail_result as $detail_row) {
                 $detail_id = $detail_row->getPkId();
                 $item_id = $detail_row->getStockBatchWarehouse()->getStockBatch()->getPackInfo()->getStakeholderItemPackSize()->getItemPackSize()->getPkId();
                 $batch_number = $detail_row->getStockBatch()->getNumber();
                 $from_batch_id = $detail_row->getStockBatch()->getPkId();
                 $batch_result = $this->_em->getRepository("StockBatch")->findOneBy(array("itemPackSize" => $item_id, "warehouse" => $to_wh_id, "number" => $batch_number));
                 if (count($batch_result) > 0) {
                     $new_batch_id = $batch_result;
                 } else {
                     $from_batch_result = $this->_em->getRepository("StockBatch")->find($from_batch_id);
                     $obj_batch = new StockBatch();
                     $obj_batch->setNumber(strtoupper($from_batch_result->getNumber()));
                     $obj_batch->setBatchMasterId($from_batch_result->getBatchMasterId());
                     $obj_batch->setExpiryDate($from_batch_result->getExpiryDate());
                     $obj_batch->setUnitPrice($from_batch_result->getUnitPrice());
                     $obj_batch->setQuantity(0);
                     $obj_batch->setStatus("Running");
                     $obj_batch->setProductionDate($from_batch_result->getProductionDate());
                     $obj_batch->setLastUpdate($from_batch_result->getLastUpdate());
                     if ($from_batch_result->getStakeholderItemPackSize() != null) {
                         $obj_batch->setStakeholderItemPackSize($from_batch_result->getStakeholderItemPackSize());
                     }
                     $obj_batch->setItemPackSize($from_batch_result->getItemPackSize());
                     if ($from_batch_result->getVvmType() != null) {
                         $obj_batch->setVvmType($from_batch_result->getVvmType());
                     }
                     $to_wh = $this->_em->getRepository("Warehouses")->find($to_wh_id);
                     $obj_batch->setWarehouse($to_wh);
                     $created_by = $this->_em->getRepository('Users')->find($this->_user_id);
                     $obj_batch->setModifiedBy($created_by);
                     $obj_batch->setModifiedDate(App_Tools_Time::now());
                     $obj_batch->setCreatedBy($created_by);
                     $obj_batch->setCreatedDate(App_Tools_Time::now());
                     $this->_em->persist($obj_batch);
                     $this->_em->flush();
                     $new_batch_id = $obj_batch;
                 }
                 $obj_detail = new StockDetail();
                 $obj_detail->setQuantity(ABS($detail_row->getQuantity()));
                 $obj_detail->setTemporary(0);
                 $obj_detail->setVvmStage($detail_row->getVvmStage());
                 $obj_detail->setIsReceived($detail_id);
                 $obj_detail->setAdjustmentType(1);
                 $obj_detail->setItemUnit($detail_row->getItemUnit());
                 $obj_detail->setStockBatch($new_batch_id);
                 $obj_detail->setStockMaster($new_master);
                 $created_by = $this->_em->getRepository('Users')->find($this->_user_id);
                 $obj_detail->setModifiedBy($created_by);
                 $obj_detail->setModifiedDate(App_Tools_Time::now());
                 $obj_detail->setCreatedBy($created_by);
                 $obj_detail->setCreatedDate(App_Tools_Time::now());
                 $this->_em->persist($obj_detail);
                 $this->_em->flush();
                 $this->adjustQuantityByWarehouse($new_batch_id->getPkId(), $to_wh_id);
             }
         }
         // STOP Auto Receive on 6th level
         /* $stock_master = new Model_StockMaster();
                       $stock_master->form_values['pk_id'] = $stock_master_id;
                       $result = $stock_master->getItemDetailFromStock();
         
                       if ($result != FALSE) {
                       foreach ($result as $stockdata) {
                       $tdate = $stockdata['transaction_date'];
                       $product = $stockdata['item_id'];
                       $month = App_Controller_Functions::dateFormat($tdate, "first day of this month", "m");
                       $year = App_Controller_Functions::dateFormat($tdate, "first day of this month", "Y");
         
                       $newdate = App_Controller_Functions::dateFormat($tdate, "-1 month", "d/m/Y");
         
                       $qty = abs($stockdata['quantity']);
                       $wh_data = new Model_HfDataMaster();
                       $cboflastmonth = $wh_data->closingBalanceOfMonth(App_Controller_Functions::monthFromDate($newdate), App_Controller_Functions::yearFromDate($newdate), $to_wh_id, $product);
                       $wh_data->form_values['reporting_start_date'] = $year . "-" . $month . "-01";
                       $wh_data->form_values['item_id'] = $product;
                       $wh_data->form_values['warehouse_id'] = $to_wh_id;
                       $wh_data->form_values['opening_balance'] = $cboflastmonth;
                       $wh_data->form_values['received_balance'] = $qty;
                       $wh_data->form_values['issue_balance'] = '';
                       $wh_data->form_values['closing_balance'] = ($cboflastmonth + $qty);
                       $wh_data->form_values['created_by'] = $this->_user_id;
                       $wh_data->form_values['created_on'] = date("d/m/Y");
                       $wh_data->saveData();
                       }
                       } */
         //$row2 = $this->_em->getConnection()->prepare("UPDATE stock_detail set is_received=1 WHERE stock_master_id = $stock_master_id");
         //$row2->execute();
     }
     return true;
 }