setriscz01customer.pm


##########################################
######### Uprava parametru firmy #########
##########################################

sub setriscz01_customer_parametry_firmy : Local {
	my ( $self, $c ) = @_;

	if ($c->check_user_roles('customer')) 
	{
		$c->stash->{zakaznici} = $c->model("setriscz01::User")->find( {username => $c->user->get('username')} )->zakazniks->search_literal( 'zakaznik_idnad IS NULL' );
		my $form;
		my $zakaznik;
		my $newrow = $c->request->params->{newrow};

		$c->stash->{firmaon} = 1;
		my $zakaznikid = $c->request->params->{zakaznikidin};
		if ($c->request->params->{zakaznikid} && $c->request->params->{zakaznikid} > 0 )
		{
			$zakaznikid = $c->request->params->{zakaznikid};
		}

		if ($newrow)
		{
			$form = setriscz01::Form::zakaznik->new( field_list => [ newrow => {type => 'Hidden', default => 1}] );
			$zakaznik = $c->model('setriscz01::Zakaznik')->new_result({});
		}
		elsif ($zakaznikid)
		{
			$form = setriscz01::Form::zakaznik->new( field_list => [ zakaznikid => {type => 'Hidden', default => $zakaznikid }] );
			$zakaznik = $c->model('setriscz01::Zakaznik')->find( { zakaznik_id => $zakaznikid });
		}
		$c->stash->{navzakaznikid} = $zakaznikid || $zakaznik->zakaznik_id;

		my @params = $c->req->params ;
		delete $params[0]->{zakaznikidin};
		delete $params[0]->{newrow};

		$c->stash( template => 'customer/setriscz01_customer_zakaznik1.tt', form => $form );
		$form->process( item => $zakaznik, params => @params );
		return unless $form->validated;

		# soucasne musim vytvorit vazbu mezi uzivatelem a zakaznikem
		# ulozeni dat do spojovacky (pouze pokud se jedna o novy zaznam - pokud edituju, tak zaznam ve spojovacce jiz existuje jiz existuje)
		if ($newrow)
		{
			my $spoj = $c->model('setriscz01::Spojuserzakaznik')->create({
				spojuserzakaznik_zakaznikid  => $zakaznik->zakaznik_id,
				spojuserzakaznik_userid => $c->user->get('id')
			    });
		}
		# zjisteni GPS souradnice a ulozeni do tabulky zakaznik->zakaznik_glocation
		$zakaznik->update( { zakaznik_glocation => glocation($zakaznik->zakaznik_ulice . ', ' . $zakaznik->zakaznik_mesto . ', ' . 'Česká republika') } );


		$c->flash(status_msg => "Záznam byl úspěšně uložen.");
		#$c->response->redirect($c->uri_for($self->action_for('setriscz01_customer_index')));
		$c->response->redirect($c->uri_for($c->controller('setriscz01customer')->action_for('setriscz01_customer_parametry_firmy'),{zakaznikidin => $zakaznik->zakaznik_id}));
	}
	else
	{
		$c->flash(error_msg => "Musíte být přihlášen.");
		$c->response->redirect($c->uri_for($c->controller('setriscz')->action_for('setriscz01_index')));
	}
}