/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $data = $request->all();
     // Salva o lote
     $lote = Lote::create(['descricao' => $data['descricao'], 'obra' => $data['obra_id'], 'fkestagio' => null, 'fketapa' => $data['etapa_id'], 'producao' => null]);
     if ($lote) {
         $sys_notifications[] = array('type' => 'success', 'message' => 'Lote criado com sucesso!');
     } else {
         $sys_notifications[] = array('type' => 'danger', 'message' => 'Não foi possível criar o lote');
         $request->session()->flash('sys_notifications', $sys_notifications);
         return back()->withInput($request->all());
     }
     // Salva o Cronograma do Conjunto
     $data['fkobra'] = $data['obra_id'];
     $data['fketapa'] = $data['etapa_id'];
     $handles_ids = array();
     foreach (@$data['handles_ids'] as $handle_id) {
         if ($request->has('grouped')) {
             $handles_conjunto = Handle::find($handle_id)->group();
             foreach ($handles_conjunto as $handle) {
                 // Atualiza HANDLE [lote]
                 $handle->fklote = $lote->id;
                 $handle->save();
                 $handles_ids[] = $handle->id;
             }
         } else {
             $handl = Handle::find($handle_id);
             // Atualiza HANDLE [lote]
             $handl->fklote = $lote->id;
             $handl->save();
             $handles_ids[] = $handl->id;
         }
     }
     foreach ($data as $key => $value) {
         if (empty($value)) {
             unset($data[$key]);
         }
     }
     $cronosaved = 0;
     foreach ($handles_ids as $handle_id) {
         $data['fkpeca'] = $handle_id;
         $data['fklote'] = $lote->id;
         $cjtcrono = CjtCrono::create($data);
         if ($cjtcrono) {
             $cronosaved++;
         }
     }
     if ($cronosaved > 0) {
         $sys_notifications[] = array('type' => 'success', 'message' => 'Novo Conograma criado com  ' . $cronosaved . ' itens!');
     } else {
         $sys_notifications[] = array('type' => 'danger', 'message' => 'Não foi possível criar o CRONOGRAMA!');
         $request->session()->flash('sys_notifications', $sys_notifications);
         return back()->withInput($request->all());
     }
     $request->session()->flash('sys_notifications', $sys_notifications);
     return back()->withInput($request->all());
 }
 public function postEliminar()
 {
     $idFinca = Input::get('id');
     $lote = Lote::where('finca_id', '=', $idFinca);
     $finca = Finca::find($idFinca);
     $finca->delete();
     if ($lote->count()) {
         $lote->delete();
     }
 }
 public function postEliminar()
 {
     $acidez = Acidez::find(Input::get('id'));
     $lote = Lote::where('acidez_id', '=', Input::get('id'));
     if ($lote->count()) {
         $lote = $lote->first();
         $lote->acidez_id = 0;
         $lote->save();
     }
     $acidez->delete();
 }
 public function postEliminar()
 {
     $aroma = Aroma::find(Input::get('id'));
     $lote = Lote::where('aroma_id', '=', Input::get('id'));
     if ($lote->count()) {
         $lote = $lote->first();
         $lote->aroma_id = 0;
         $lote->save();
     }
     $aroma->delete();
 }
 public function postEliminar()
 {
     $sabor = Sabor::find(Input::get('id'));
     $lote = Lote::where('sabor_id', '=', Input::get('id'));
     if ($lote->count()) {
         $lote = $lote->first();
         $lote->sabor_id = 0;
         $lote->save();
     }
     $sabor->delete();
 }
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index(Request $request, $obra_id = null, $etapa_id = null)
 {
     if ($request->ajax()) {
         $flg_rec = $request->input('flg_rec', 3);
         $orderBy = $request->input('sort', ['id' => 'asc']);
         $handle = new Handle();
         $cjtcrono = new CjtCrono();
         $handles = $handle->where('obra', $obra_id)->where('fketapa', $etapa_id)->where('FLG_REC', $flg_rec)->orderBy(current(array_keys($orderBy)), current($orderBy))->with('lote');
         // if you need options data anyway
         if ($request->input('grouped')) {
             if ($flg_rec == 3) {
                 $handles = $handles->groupBy('MAR_PEZ')->select($handle->getTable() . '.*', DB::raw('SUM(QTA_PEZ) as QTA_PEZ'))->get();
             } else {
                 $handles = $handles->groupBy('POS_PEZ')->select($handle->getTable() . '.*', DB::raw('SUM(QTA_PEZ) as QTA_PEZ'))->get();
             }
         } else {
             $handles = $handles->get();
         }
         if ($request->ajax()) {
             $response = array();
             $response['data'] = array();
             foreach ($handles as $handle) {
                 // dd( $handle->group );
                 $response['data'][] = ['PROJETO' => $handle->PROJETO, 'HANDLE' => $handle->HANDLE, 'FLG_REC' => $handle->FLG_REC, 'NUM_COM' => $handle->NUM_COM, 'DES_COM' => $handle->DES_COM, 'LOT_COM' => $handle->LOT_COM, 'DLO_COM' => $handle->DLO_COM, 'CLI_COM' => $handle->CLI_COM, 'IND_COM' => $handle->IND_COM, 'DT1_COM' => $handle->DT1_COM, 'DT2_COM' => $handle->DT2_COM, 'NUM_DIS' => $handle->NUM_DIS, 'DES_DIS' => $handle->DES_DIS, 'NOM_DIS' => $handle->NOM_DIS, 'REV_DIS' => $handle->REV_DIS, 'DAT_DIS' => $handle->DAT_DIS, 'TRA_PEZ' => $handle->TRA_PEZ, 'SBA_PEZ' => $handle->SBA_PEZ, 'DES_SBA' => $handle->DES_SBA, 'TIP_PEZ' => $handle->TIP_PEZ, 'MAR_PEZ' => $handle->MAR_PEZ, 'MBU_PEZ' => $handle->MBU_PEZ, 'DES_PEZ' => $handle->DES_PEZ, 'POS_PEZ' => $handle->POS_PEZ, 'NOT_PEZ' => $handle->NOT_PEZ, 'ING_PEZ' => $handle->ING_PEZ, 'MAX_LEN' => $handle->MAX_LEN, 'QTA_PEZ' => $handle->QTA_PEZ, 'QT1_PEZ' => $handle->QT1_PEZ, 'MCL_PEZ' => $handle->MCL_PEZ, 'COD_PEZ' => $handle->COD_PEZ, 'COS_PEZ' => $handle->COS_PEZ, 'NOM_PRO' => $handle->NOM_PRO == null ? 'CHAPA' : $handle->NOM_PRO, 'LUN_PRO' => $handle->LUN_PRO, 'LAR_PRO' => $handle->LAR_PRO, 'SPE_PRO' => $handle->SPE_PRO, 'MAT_PRO' => $handle->MAT_PRO, 'TIP_BUL' => $handle->TIP_BUL, 'DIA_BUL' => $handle->DIA_BUL, 'LUN_BUL' => $handle->LUN_BUL, 'PRB_BUL' => $handle->PRB_BUL, 'PUN_LIS' => $handle->PUN_LIS, 'SUN_LIS' => $handle->SUN_LIS, 'PRE_LIS' => $handle->PRE_LIS, 'FLG_DWG' => $handle->FLG_DWG, 'obra' => $handle->obra, 'id' => $handle->id, 'fklote' => $handle->lote ? $handle->lote->descricao : '', 'fkestagio' => $handle->fkestagio, 'grp' => $handle->grp, 'nome_file1' => $handle->nome_file1, 'nome_file2' => $handle->nome_file2, 'nome_file3' => $handle->nome_file3, 'nome_file4' => $handle->nome_file4, 'fketapa' => $handle->fketapa, 'CATEPERFIL' => $handle->CATEPERFIL, 'fkImportacao' => $handle->fkImportacao, 'fkpreparacao' => $handle->fkpreparacao, 'fkmedicao' => $handle->fkmedicao, 'status' => null];
             }
             $response['current'] = $request->input('current', 1);
             $response['rowCount'] = $request->input('rowCount', 10);
             $response['total'] = $handles->count();
             return json_encode($response);
         }
     } else {
         $obras = new ObrasController();
         $obras = $obras->index($request);
         $lotes = Lote::all();
         if ($request->old('obra_id')) {
             $etapas = $obras->find($request->old('obra_id'))->etapas->lists('codigo', 'id');
         } else {
             $etapas = array();
         }
         JavaScript::put(['urlbase' => env("APP_URL") . env("APP_URLPREFIX"), 'obra_id' => $request->old('obra_id'), 'etapa_id' => $request->old('etapa_id'), 'etapas' => $etapas, 'selected' => $request->old('handles_ids')]);
         return view('pecas.index', compact('obras', 'lotes'));
     }
 }
 public function postEliminar()
 {
     $medio = Medio::where('productor_id', '=', Input::get('id'));
     if ($medio->count()) {
         $medio->delete();
     }
     $certificacion = Certificacion_Productor::where('productor_id', '=', Input::get('id'));
     if ($certificacion->count()) {
         $certificacion->delete();
     }
     $finca = Finca::where('productor_id', '=', Input::get('id'));
     if ($finca->count()) {
         $finca = $finca->first();
         $lote = Lote::where('finca_id', '=', $finca->id);
         $lote->delete();
         $finca->delete();
     }
     $productor = Productor::where('id', '=', Input::get('id'));
     $productor->delete();
 }
 public function getLotes()
 {
     $lotes = Lote::with('finca', 'variedad1', 'tipo_beneficio', 'tipo_secado', 'acidez', 'aroma', 'sabor')->get();
     return response()->json($lotes)->setCallback(Input::get('callback'));
 }
 public function postEliminar()
 {
     $id = Input::get('id');
     $perfl = Input::get('perfil');
     Lote::find($id)->fill(Input::all())->delete();
 }