public function addGatepass()
 {
     $form_values = $this->form_values;
     $gp_master = new GatepassMaster();
     if (!empty($form_values['vehicle_other'])) {
         $gp_vehicle = $this->_em->getRepository("GatepassVehicles")->findOneBy(array("vehicleType" => $form_values['vehicle_type_id'], "number" => $form_values['vehicle_other']));
         if (count($gp_vehicle) == 0) {
             $gp_vehicle = new GatepassVehicles();
             $gp_vehicle->setNumber($form_values['vehicle_other']);
             $gvehicle = $this->_em->getRepository("ListDetail")->find($form_values['vehicle_type_id']);
             $gp_vehicle->setVehicleType($gvehicle);
             $this->_em->persist($gp_vehicle);
             $this->_em->flush();
         }
         $form_values['gatepass_vehicle_id'] = $gp_vehicle->getPkId();
     }
     if (!empty($form_values['gatepass_vehicle_id'])) {
         $gatepass_vehicle_id = $this->_em->find("GatepassVehicles", $form_values['gatepass_vehicle_id']);
         $gp_master->setGatepassVehicle($gatepass_vehicle_id);
     }
     if (!empty($form_values['transaction_date'])) {
         $gp_master->setTransactionDate(new \DateTime(date("Y-m-d h:i")));
     }
     $number = substr(number_format(time() * rand(), 0, '', ''), 0, 10);
     $gp_master->setNumber($number);
     $warehouse_id = $this->_identity->getWarehouseId();
     $warehouse_id = $this->_em->find("Warehouses", $warehouse_id);
     $gp_master->setWarehouse($warehouse_id);
     $created_by = $this->_em->find('Users', $this->_user_id);
     $gp_master->setCreatedBy($created_by);
     $gp_master->setCreatedDate(App_Tools_Time::now());
     $gp_master->setModifiedBy($created_by);
     $gp_master->setModifiedDate(App_Tools_Time::now());
     $this->_em->persist($gp_master);
     $this->_em->flush();
     $gp_master_id = $gp_master->getPkId();
     $stock_detail = new Model_StockDetail();
     $count1 = 1;
     $detailId = array();
     foreach ($form_values['quantity'] as $key => $value) {
         $value1 = $value;
         if (!empty($value1)) {
             list($stm, $stcmaster) = explode('_', $key);
             $res_rec = $stock_detail->quantityDataByStcBatch($stm, $stcmaster);
             $dataArr = array();
             foreach ($res_rec as $res) {
                 $detail_pkid = $res['pkId'];
                 $detail_qty = abs($res['quantity']);
                 $dataArr[$detail_pkid] = $detail_qty;
             }
             while ($value1 > 0) {
                 $arr = $stock_detail->getClosest($dataArr, $value);
                 $qty = $value1;
                 $value1 = $value1 - $arr[1];
                 if ($value1 > 0) {
                     $detailId[$arr[0]] = (int) $arr[1];
                 } else {
                     $detailId[$arr[0]] = (int) $qty;
                 }
             }
         }
     }
     $gp_m_id = $this->_em->getRepository('GatepassMaster')->find($gp_master_id);
     foreach ($detailId as $detId => $quantity) {
         $gp_detail = new GatepassDetail();
         $stock_d_id = $this->_em->getRepository('StockDetail')->find($detId);
         $gp_detail->setStockDetail($stock_d_id);
         $gp_detail->setQuantity($quantity);
         $gp_detail->setGatepassMaster($gp_m_id);
         $created_by = $this->_em->find('Users', $this->_user_id);
         $gp_detail->setCreatedBy($created_by);
         $gp_detail->setCreatedDate(App_Tools_Time::now());
         $gp_detail->setModifiedBy($created_by);
         $gp_detail->setModifiedDate(App_Tools_Time::now());
         $this->_em->persist($gp_detail);
         $this->_em->flush();
     }
 }
 public function addGatepass()
 {
     $form_values = $this->form_values;
     $gp_master = new GatepassMaster();
     if (!empty($form_values['vehicle_other'])) {
         $gp_vehicle = $this->_em->getRepository("GatepassVehicles")->findOneBy(array("vehicleType" => $form_values['vehicle_type_id'], "number" => $form_values['vehicle_other']));
         if (count($gp_vehicle) == 0) {
             $gp_vehicle = new GatepassVehicles();
             $gp_vehicle->setNumber($form_values['vehicle_other']);
             $gvehicle = $this->_em->getRepository("ListDetail")->find($form_values['vehicle_type_id']);
             $gp_vehicle->setVehicleType($gvehicle);
             $this->_em->persist($gp_vehicle);
             $this->_em->flush();
         }
         $form_values['gatepass_vehicle_id'] = $gp_vehicle->getPkId();
     }
     if (!empty($form_values['gatepass_vehicle_id'])) {
         $gatepass_vehicle_id = $this->_em->find("GatepassVehicles", $form_values['gatepass_vehicle_id']);
         $gp_master->setGatepassVehicle($gatepass_vehicle_id);
     }
     if (!empty($form_values['transaction_date'])) {
         $gp_master->setTransactionDate(new \DateTime(date("Y-m-d h:i")));
     }
     //$number = "12";
     $number = substr(number_format(time() * rand(), 0, '', ''), 0, 10);
     $gp_master->setNumber($number);
     $warehouse_id = $this->_identity->getWarehouseId();
     $warehouse_id = $this->_em->find("Warehouses", $warehouse_id);
     $gp_master->setWarehouse($warehouse_id);
     $this->_em->persist($gp_master);
     $this->_em->flush();
     $gp_master_id = $gp_master->getPkId();
     $stock_detail = new Model_StockDetail();
     // list($stm, $stcmaster) = explode('_',$form_values['quantity']);
     //// $stm = explode('_',$form_values['quantity']);
     // print_r($stm);exit;
     $count1 = 1;
     $detailId = array();
     foreach ($form_values['quantity'] as $key => $value) {
         $value1 = $value;
         //2000
         // echo $value1;exit;
         // $gp_detail = new GatepassDetail();
         if (!empty($value1)) {
             list($stm, $stcmaster) = explode('_', $key);
             //  echo $stcmaster;
             //  echo $stm;exit;
             $res_rec = $stock_detail->quantityDataByStcBatch($stm, $stcmaster);
             $dataArr = array();
             foreach ($res_rec as $res) {
                 $detail_pkid = $res['pkId'];
                 //131
                 $detail_qty = abs($res['quantity']);
                 //5000
                 // echo $dataArr[$res['pkId']] = $res['quantity'];exit; //-5000
                 $dataArr[$detail_pkid] = $detail_qty;
                 //5000
             }
             // echo "<pre>"; print_r($dataArr[$detail_pkid]);exit;
             //print_r($dataArr);exit; // [131]=>5000
             while ($value1 > 0) {
                 $arr = $stock_detail->getClosest($dataArr, $value);
                 //  echo $arr[1];exit; //5000
                 $qty = $value1;
                 //2000exit;
                 $value1 = $value1 - $arr[1];
                 //  2000-(5000) = -3000
                 if ($value1 > 0) {
                     //echo $arr[0];exit; //131
                     $detailId[$arr[0]] = (int) $arr[1];
                     //  $detailId[131] = -5000
                 } else {
                     $detailId[$arr[0]] = (int) $qty;
                 }
             }
         }
     }
     // print_r($detailId);
     //exit;
     $gp_m_id = $this->_em->getRepository('GatepassMaster')->find($gp_master_id);
     foreach ($detailId as $detId => $quantity) {
         // echo "here";exit;
         //echo $quantity;
         //exit;
         //echo $quantity;exit;
         $gp_detail = new GatepassDetail();
         //echo $quantity;exit;
         $stock_d_id = $this->_em->getRepository('StockDetail')->find($detId);
         $gp_detail->setStockDetail($stock_d_id);
         $gp_detail->setQuantity($quantity);
         $gp_detail->setGatepassMaster($gp_m_id);
         // $sb_id = $this->_em->getRepository('StockBatch')->find(12);
         //$gp_detail->setStockBatch($sb_id);
         $this->_em->persist($gp_detail);
         $this->_em->flush();
         //    echo "here".$value."<br>".$detId."<br>".$gp_master_id;exit;
         // $count1++;
     }
     // $this->_em->persist($gp_detail);
     // $count1++;
     //var_dump($detailId);
     //exit;
     //$this->_em->persist($gp_detail);
 }