CREATE OR REPLACE FUNCTION public.sp_crud_guia(serie character varying, numero integer, tipo character varying, serie_relacionado character varying, num_relacionado character varying, tipo_relacionado character varying, serie_baja character varying, num_baja character varying, tipo_baja character varying, emisor_numdoc character varying, emisor_tipodoc character varying, emisor_razsocial character varying, receptor_numdoc character varying, receptor_tipodoc character varying, receptor_razsocial character varying, tercero_numdoc character varying, tercero_tipodoc character varying, tercero_razsocial character varying, cod_motivo character varying, desc_motivo character varying, transbordo character varying, peso_bruto character varying, bultos character varying, modo_traslado character varying, fecha_traslado character varying, transportista_numdoc character varying, transportista_tipo_doc character varying, transportista_razsoc character varying, vehiculo_placa character varying, conductor_numdoc character varying, conductor_tipodoc character varying, llegada_ubigeo character varying, llegada_direccion character varying, partida_ubigeo character varying, partida_direccion character varying, numero_contenedor character varying, puerto_desembarque character varying, observaciones character varying, ruta_comprobante character varying, email character varying, estado_email character varying, estado_sunat character varying, anulado character varying, orden_item integer, codigo character varying, descripcion character varying, cantidad integer, unid_medida character varying, accion character varying)
 RETURNS character varying
 LANGUAGE plpgsql
AS $function$
declare
	
	idp integer;

/*
	_serie character varying; 
	_numero integer; 
	_tipo character varying;  
	_serie_relacionado character varying; 
	_num_relacionado character varying; 
	_tipo_relacionado character varying; 
	_serie_baja character varying; 
	_num_baja character varying; 
	_tipo_baja character varying; 
	_emisor_numdoc character varying; 
	_emisor_tipodoc character varying; 
	_emisor_razsocial character varying; 
	_receptor_numdoc character varying; 
	_receptor_tipodoc character varying; 
	_receptor_razsocial character varying; 
	_tercero_numdoc character varying; 
	_tercero_tipodoc character varying; 
	_tercero_razsocial character varying; 
	_cod_motivo character varying; 
	_desc_motivo character varying; 
	_transbordo character varying; 
	_peso_bruto integer; 
	_bultos character varying; 
	_modo_traslado character varying; 
	_fecha_traslado character varying; 
	_transportista_numdoc character varying; 
	_transportista_tipo_doc character varying; 
	_transportista_razsoc character varying; 
	_vehiculo_placa character varying; 
	_conductor_numdoc character varying; 
	_conductor_tipodoc character varying; 
	_llegada_ubigeo character varying; 
	_llegada_direccion character varying; 
	_partida_ubigeo character varying; 
	_partida_direccion character varying; 
	_numero_contenedor character varying; 
	_puerto_desembarque character varying; 
	_observaciones character varying; 
	_ruta_comprobante character varying; 
	_email character varying; 
	_estado_email character varying; 
	_estado_sunat character varying; 
	_anulado character varying;
	*/
begin


	Case accion

		When 'g' then

				Insert Into guias (
				guia_serie, 
				guia_numero, 
				guia_tipo, 
				guia_fecha_emision, 
				guia_serie_relacionado, 
				guia_num_relacionado, 
				guia_tipo_relacionado, 
				guia_serie_baja, 
				guia_num_baja, 
				guia_tipo_baja, 
				guia_emisor_numdoc, 
				guia_emisor_tipodoc, 
				guia_emisor_razsocial, 
				guia_receptor_numdoc, 
				guia_receptor_tipodoc, 
				guia_receptor_razsocial, 
				guia_tercero_numdoc, 
				guia_tercero_tipodoc, 
				guia_tercero_razsocial, 
				guia_cod_motivo, 
				guia_desc_motivo, 
				guia_transbordo, 
				guia_peso_bruto, 
				guia_bultos, 
				guia_modo_traslado, 
				guia_fecha_traslado, 
				guia_transportista_numdoc, 
				guia_transportista_tipo_doc, 
				guia_transportista_razsoc, 
				guia_vehiculo_placa, 
				guia_conductor_numdoc, 
				guia_conductor_tipodoc, 
				guia_llegada_ubigeo, 
				guia_llegada_direccion, 
				guia_partida_ubigeo, 
				guia_partida_direccion, 
				guia_numero_contenedor, 
				guia_puerto_desembarque, 
				guia_observaciones, 
				guia_ruta_comprobante, 
				guia_email, 
				guia_estado_email, 
				guia_estado_sunat, 
				guia_anulado)
			Values(
				serie, 
				(select coalesce(max(g.guia_numero),'0')+1 from guias g where g.guia_serie = serie), 
				tipo, 
				now(), 
				serie_relacionado, 
				num_relacionado, 
				tipo_relacionado, 
				serie_baja, 
				num_baja, 
				tipo_baja, 
				emisor_numdoc, 
				emisor_tipodoc, 
				emisor_razsocial, 
				receptor_numdoc, 
				receptor_tipodoc, 
				receptor_razsocial, 
				tercero_numdoc, 
				tercero_tipodoc, 
				tercero_razsocial, 
				cod_motivo, 
				desc_motivo, 
				transbordo, 
				peso_bruto, 
				bultos, 
				modo_traslado, 
				now(),
				transportista_numdoc, 
				transportista_tipo_doc, 
				transportista_razsoc, 
				vehiculo_placa, 
				conductor_numdoc, 
				conductor_tipodoc, 
				llegada_ubigeo, 
				llegada_direccion, 
				partida_ubigeo, 
				partida_direccion, 
				numero_contenedor, 
				puerto_desembarque, 
				observaciones, 
				ruta_comprobante, 
				email, 
				estado_email, 
				estado_sunat, 
				anulado);
		
				idp := (SELECT currval('guias_id_seq'));

		When 'd' then

			if numero > 0 Then

				Insert Into guia_detalles (guiad_serie, guiad_numero, guiad_tipo, guiad_orden_item, guiad_codigo, guiad_descripcion, guiad_cantidad, guiad_unid_medida)
				VALUES(serie, numero, tipo, 
				orden_item, 
				codigo, 
				descripcion, 
				cantidad, 
				unid_medida);

			Else
				idp:=0;
			End if;

		End Case;

	return idp;


end;
$function$
;