/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request, $id)
 {
     $document = \App\Models\Document::find($id);
     $type = $document->os_type;
     if ($type == 'movables' || $type == 'value_movables') {
         $item = new \App\Models\Item();
         $item->name = Input::get('name');
         $item->os_date = Input::get('os_date');
         $item->number = Input::get('number');
         //$item -> os_view=Input::get('os_view');
         $item->okof = Input::get('okof');
         $item->carrying_amount = Input::get('carrying_amount');
         $item->financing_source = Input::get('financing_source');
         $item->additional_field = Input::get('additional_field');
         $item->document_id = $id;
         $item->save();
         $variable = new \App\Models\Variable();
         $variable->exploitation_date = Input::get('exploitation_date');
         $variable->residual_value = Input::get('residual_value');
         $variable->monthly_rate = Input::get('monthly_rate');
         $variable->useful_life = Input::get('useful_life');
         $item->variable()->save($variable);
     }
     if ($type == 'car') {
         $item = new \App\Models\Item();
         $item->name = Input::get('name');
         $item->os_date = Input::get('os_date');
         $item->number = Input::get('number');
         $item->okof = Input::get('okof');
         $item->carrying_amount = Input::get('carrying_amount');
         $item->financing_source = Input::get('financing_source');
         $item->additional_field = Input::get('additional_field');
         $item->document_id = $id;
         $item->save();
         $variable = new \App\Models\Variable();
         $variable->exploitation_date = Input::get('exploitation_date');
         $variable->residual_value = Input::get('residual_value');
         $variable->monthly_rate = Input::get('monthly_rate');
         $variable->useful_life = Input::get('useful_life');
         $item->variable()->save($variable);
         $car = new \App\Models\Car();
         $car->brand = Input::get('brand');
         $car->model = Input::get('model');
         $car->manufacture_year = Input::get('manufacture_year');
         $car->vin = Input::get('vin');
         $car->kpp = Input::get('kpp');
         $car->engine = Input::get('engine');
         $car->power = Input::get('power');
         $car->color = Input::get('color');
         $car->car_type = Input::get('car_type');
         $item->car()->save($car);
     }
     if ($type == 'buildings') {
         $item = new \App\Models\Item();
         $item->name = Input::get('name');
         $item->os_date = Input::get('os_date');
         $item->number = Input::get('number');
         //$item -> os_view=Input::get('os_view');
         $item->okof = Input::get('okof');
         $item->carrying_amount = Input::get('carrying_amount');
         $item->financing_source = Input::get('financing_source');
         $item->additional_field = Input::get('additional_field');
         $item->document_id = $id;
         $item->save();
         $variable = new \App\Models\Variable();
         $variable->exploitation_date = Input::get('exploitation_date');
         $variable->residual_value = Input::get('residual_value');
         $variable->monthly_rate = Input::get('monthly_rate');
         $variable->useful_life = Input::get('useful_life');
         $item->variable()->save($variable);
         $building = new \App\Models\Building();
         $building->appointment = Input::get('appointment');
         $building->wall_material = Input::get('wall_material');
         $building->date_construction = Input::get('date_construction');
         $building->floors = Input::get('floors');
         $item->building()->save($building);
         $address = new \App\Models\Address();
         $address->state = Input::get('state');
         $address->district = Input::get('district');
         $address->city = Input::get('city');
         $address->street = Input::get('street');
         $address->building_number = Input::get('building_number');
         $address->building_number_2 = Input::get('building_number_2');
         $item->address()->save($address);
     }
     if ($type == 'parcels') {
         $item = new \App\Models\Item();
         $item->name = Input::get('name');
         $item->os_date = Input::get('os_date');
         $item->number = Input::get('number');
         $item->carrying_amount = Input::get('carrying_amount');
         $item->financing_source = Input::get('financing_source');
         $item->additional_field = Input::get('additional_field');
         $item->document_id = $id;
         $item->save();
         $parcel = new \App\Models\Parcel();
         $parcel->cadastral = Input::get('cadastral');
         $parcel->assigning_land = Input::get('assigning_land');
         $parcel->area = Input::get('area');
         $item->parcel()->save($parcel);
         $address = new \App\Models\Address();
         $address->state = Input::get('state');
         $address->district = Input::get('district');
         $address->city = Input::get('city');
         $address->street = Input::get('street');
         $address->building_number = Input::get('building_number');
         $address->building_number_2 = Input::get('building_number_2');
         $item->address()->save($address);
     }
     return Redirect::action('DocumentController@show', [$item->document_id]);
 }
 public function postImport($id)
 {
     $document = \App\Models\Document::find($id);
     $type = $document->os_type;
     if (Input::hasFile('file')) {
         $file = Input::file('file');
         $filename = $file->getClientOriginalName();
         $destinationPath = public_path() . '/uploads/';
         Input::file('file')->move($destinationPath, $filename);
         $handle = fopen(public_path() . '/uploads/' . $filename, "r");
         if ($handle !== FALSE) {
             if ($type == 'movables' || $type == 'value_movables') {
                 while (($data = fgetcsv($handle, 1000, ';')) !== FALSE) {
                     $item = new \App\Models\Item();
                     $name = iconv("Windows-1251", "utf-8", $data[0]);
                     $item->name = $name;
                     $item->number = $data[1];
                     $os_date = date("Y-m-d", strtotime($data[2]));
                     $item->os_date = $os_date;
                     $okof = $data[5];
                     $okof = str_replace(" ", "", $okof);
                     $item->okof = $okof;
                     $carrying_amount = $data[6];
                     $carrying_amount = str_replace(",", ".", $carrying_amount);
                     $carrying_amount = str_replace(" ", "", $carrying_amount);
                     $item->carrying_amount = $carrying_amount;
                     $item->financing_source = 1;
                     $item->document_id = $id;
                     $item->save();
                     $variable = new \App\Models\Variable();
                     $exploitation_date = date("Y-m-d", strtotime($data[3]));
                     $variable->exploitation_date = $exploitation_date;
                     $residual_value = $data[8];
                     $residual_value = str_replace(",", ".", $residual_value);
                     $residual_value = str_replace(" ", "", $residual_value);
                     $variable->residual_value = $residual_value;
                     $monthly_rate = $data[10];
                     $monthly_rate = str_replace(",", ".", $monthly_rate);
                     $variable->monthly_rate = $monthly_rate;
                     $variable->useful_life = $data[11];
                     $item->variable()->save($variable);
                 }
             }
             if ($type == 'car') {
                 while (($data = fgetcsv($handle, 2000, ';')) !== FALSE) {
                     $item = new \App\Models\Item();
                     $name = iconv("Windows-1251", "utf-8", $data[0]);
                     $item->name = $name;
                     $item->number = $data[1];
                     $os_date = date("Y-m-d", strtotime($data[2]));
                     $item->os_date = $os_date;
                     $okof = $data[5];
                     $okof = str_replace(" ", "", $okof);
                     $item->okof = $okof;
                     $carrying_amount = $data[6];
                     $carrying_amount = str_replace(",", ".", $carrying_amount);
                     $carrying_amount = str_replace(" ", "", $carrying_amount);
                     $item->carrying_amount = $carrying_amount;
                     $item->financing_source = 1;
                     $item->document_id = $id;
                     $item->save();
                     $variable = new \App\Models\Variable();
                     $exploitation_date = date("Y-m-d", strtotime($data[3]));
                     $variable->exploitation_date = $exploitation_date;
                     $residual_value = $data[8];
                     $residual_value = str_replace(",", ".", $residual_value);
                     $residual_value = str_replace(" ", "", $residual_value);
                     $variable->residual_value = $residual_value;
                     $monthly_rate = $data[10];
                     $monthly_rate = str_replace(",", ".", $monthly_rate);
                     $variable->monthly_rate = $monthly_rate;
                     $variable->useful_life = $data[11];
                     $item->variable()->save($variable);
                     $car = new \App\Models\Car();
                     $car->brand = $data[12];
                     $car->model = $data[13];
                     $manufacture_year = date("Y-m-d", strtotime($data[14]));
                     $car->manufacture_year = $manufacture_year;
                     $vin = $data[15];
                     $vin = str_replace('"', '', $vin);
                     $car->vin = $vin;
                     $car->kpp = $data[16];
                     $car->engine = $data[17];
                     $car->power = $data[18];
                     if (isset($data[19])) {
                         $car->color = $data[19];
                     }
                     $item->car()->save($car);
                 }
             }
             if ($type == 'buildings') {
                 while (($data = fgetcsv($handle, 1000, ';')) !== FALSE) {
                     $item = new \App\Models\Item();
                     $name = iconv("Windows-1251", "utf-8", $data[0]);
                     $item->name = $name;
                     $item->number = $data[1];
                     $os_date = date("Y-m-d", strtotime($data[2]));
                     $item->os_date = $os_date;
                     $okof = $data[5];
                     $okof = str_replace(" ", "", $okof);
                     $item->okof = $okof;
                     $carrying_amount = $data[6];
                     $carrying_amount = str_replace(",", ".", $carrying_amount);
                     $carrying_amount = str_replace(" ", "", $carrying_amount);
                     $item->carrying_amount = $carrying_amount;
                     $item->financing_source = 1;
                     $item->document_id = $id;
                     $item->save();
                     $variable = new \App\Models\Variable();
                     $exploitation_date = date("Y-m-d", strtotime($data[3]));
                     $variable->exploitation_date = $exploitation_date;
                     $residual_value = $data[8];
                     $residual_value = str_replace(",", ".", $residual_value);
                     $residual_value = str_replace(" ", "", $residual_value);
                     $variable->residual_value = $residual_value;
                     $monthly_rate = $data[10];
                     $monthly_rate = str_replace(",", ".", $monthly_rate);
                     $monthly_rate = str_replace(" ", "", $monthly_rate);
                     $variable->monthly_rate = $monthly_rate;
                     $variable->useful_life = $data[11];
                     $item->variable()->save($variable);
                     $building = new \App\Models\Building();
                     $appointment = iconv("Windows-1251", "utf-8", $data[12]);
                     $building->appointment = $appointment;
                     $wall_material = iconv("Windows-1251", "utf-8", $data[13]);
                     $building->wall_material = $wall_material;
                     $date_construction = date("Y-m-d", strtotime($data[14]));
                     $building->date_construction = $date_construction;
                     $building->floors = $data[15];
                     $item->building()->save($building);
                     $address = new \App\Models\Address();
                     $state = iconv("Windows-1251", "utf-8", $data[16]);
                     $address->state = $state;
                     $item->address()->save($address);
                 }
             }
             if ($type == 'parcels') {
                 while (($data = fgetcsv($handle, 1000, ';')) !== FALSE) {
                     $item = new \App\Models\Item();
                     $name = iconv("Windows-1251", "utf-8", $data[0]);
                     $item->name = $name;
                     $item->number = $data[1];
                     $os_date = date("Y-m-d", strtotime($data[2]));
                     $item->os_date = $os_date;
                     $carrying_amount = $data[3];
                     $carrying_amount = str_replace(",", ".", $carrying_amount);
                     $carrying_amount = str_replace(" ", "", $carrying_amount);
                     $item->carrying_amount = $carrying_amount;
                     $item->financing_source = 1;
                     $item->document_id = $id;
                     $item->save();
                     $parcel = new \App\Models\Parcel();
                     $parcel->cadastral = $data[5];
                     $assigning_land = iconv("Windows-1251", "utf-8", $data[6]);
                     $parcel->assigning_land = $assigning_land;
                     $parcel->area = $data[7];
                     $item->parcel()->save($parcel);
                     $address = new \App\Models\Address();
                     $state = iconv("Windows-1251", "utf-8", $data[8]);
                     $address->state = $state;
                     $state = iconv("Windows-1251", "utf-8", $data[8]);
                     $item->address()->save($address);
                 }
             }
             fclose($handle);
         }
     }
     return redirect()->back();
 }