#define SV_REF_X

#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<alloc.h>
#include<stdlib.h>


#include "c:\things\borlandc\jd_lib\jd_graph.h"
#include "sv_util.h"
#include "sv_graph.h"
#include "sv_ref_x.h"


#define Letra_Boton    setcolor(1);
#define Azul           1
#define Texto_Normal   setcolor(63);
#define Texto_Negrita  setcolor(0);

extern int x_raton, y_raton, raton;
extern int boton_izq, boton_der;

extern void  desactiva_raton(void);
extern void  activa_raton(void);
extern void  Anclar_Raton(void);
extern void  inicializa_raton_grafico(int x1, int y1, int x2, int y2, int px, int py);
extern int Espera_Tecla_o_Raton(void);
extern void BEEP(void);

extern int InputCadenaG(char *s, int numalp, int lmax, int cc, int cf, struct boton Campo);
extern int InputCadenaG_l(char *s, int numalp, int lmax, int cc, int cf, struct boton Campo, int Row);
extern int InputCadenaG_T4(char *s, int numalp, int lmax, int cc, int cf, struct boton Campo, int RoW);

void PutLineStock(void);
void GetLineStock(void);
void Menu_de_Stock(void);
void AddRecordStock(void);
void PrintLineStock(void);
void Edit_Item_Stock(void);
void Abre_Ficha_Stock(void);
void DeleteRecordStock(void);
void Mueve_Abajo_Stock(void);
void Mueve_Arriba_Stock(void);
void CompruebaLineasStock(void);
void PrintWholeScreenStock(void);
void EditCursorStock(int ShowHide);
void Imprimir_Stock();
void Poner_a_cero_estructura_actual_Stock(void);
void Recalcula_Porcentaje(int Individual_Seleccionado);

// void Llena_con_Stock_basicas(void);
extern void Coloca_Nombres_a_botones(void);






// extern int Confirma(char *f1, char *f2, char *f3);
extern int  Comprueba_Impresora(void);





struct Stock_inx {  unsigned int Fin;  };


struct Stock_actual { 	int  Ref; long Und_0; long Und_1;    };

struct Stock_actual Stock1;
struct Stock_actual Stock1_Graficas[10];
struct Stock_inx    Stock1_inx;

struct boton *PS;//[5]
extern struct boton Comunes[9];

FILE *Stock; 	                // Fichero de Stock


int CurrRowStock1;
int CurrColStock1;
int CurrTopLineStock;





void Menu_de_Stock(void) {

 int finished = 0, key, bpush;
 CurrRowStock1 = 1;  CurrColStock1 = 1;

 CurrTopLineStock = 1;

 inicializa_raton_grafico(0, 0, 624, 464, 320, 240);
 if(raton!=0) desactiva_raton();

 if( ( PS = (struct boton *) malloc( sizeof(struct boton)*5 ) ) == NULL )
     {
      Confirma("Memoria insuficiente", "para crear tablas de", "posisi�n PS.");
      return ;
     }


 cleardevice();
 Dibuja_Pantalla_Stock(PS);
 Fuente(SMALL_FONT, 5);
 setcolor(NEGRO);
 Coloca_Nombres_a_botones();
	setcolor(GRIS_C);
	outtextxy(Comunes[3].left,   Comunes[3].up, "   SWAP"     );
	outtextxy(Comunes[3].left,   Comunes[3].up+18, "    F6  "    );


 Fuente(SMALL_FONT, 4);
 Abre_Ficha_Stock();
 Abre_Ficha_Referencias();
  Carga_Referencias();

 PrintWholeScreenStock();
 GetLineStock();
 int oldCR1=0, oldCC1=0;

do{

if(oldCR1!=CurrRowStock1 || oldCC1!=CurrColStock1 )
			 EditCursorStock(0);

oldCR1 = CurrRowStock1; oldCC1 = CurrColStock1;

 if(raton!=0) activa_raton();
  Espera_Tecla_o_Raton();
 if(raton!=0) desactiva_raton();

 if(kbhit()){
   EditCursorStock(1);
   key = getch();
   if( isprint(key) || isspace(key) ) {
    EditCursorStock(0);
    if(key!=13) ungetch(key);
    Edit_Item_Stock();
    Recalcula_Porcentaje(1);
    EditCursorStock(0);
   } else if( key == 0 ) {
     switch( (key=getch()) ){
      // Flecha Arriba
      case  72:
	Mueve_Arriba_Stock();
		Recalcula_Porcentaje(1);
	break;
      // Flecha Izquierda
      case  75:
      case  15:
	CurrColStock1--;
	if ( CurrColStock1<1 ) CurrColStock1=5;
	break;
      // Flecha Derecha
      case  77:
      case   9:
	CurrColStock1++;
	if ( CurrColStock1>5 ) CurrColStock1=1;
	break;
      // Flecha Abajo
      case  80:
	Mueve_Abajo_Stock();
		Recalcula_Porcentaje(1);
	break;
      // Buscar F2
      case  60:
//	BuscarStock();
	break;
      // Imprimir Stock F5
      case  63:
	Imprimir_Stock();
	break;

      // F9 --> A�ADIR
      case 67:
//     if( Confirma("A�ADIR NUEVO STOCK", "INICIAL", "") )
//	 if ( Pide_Codigo(SV_CFG.COD_Stock) )
//						AddRecordStock();
       break;
      // F10 -> BORRAR
      case 68:
       if( Confirma("ELIMINAR TOTALMENTE", "STOCK ACTUAL", "") )
//		if ( Pide_Codigo(SV_CFG.COD_Stock) )
						DeleteRecordStock();
		Recalcula_Porcentaje(1);
       break;

      // INICIO
      case 71:
       CurrColStock1 = 1;
       break;
      // FIN
      case 79:
       CurrColStock1 = 5;
       break;
      // ReP�g
      case 73:
       // CurrRow = 1;
//����������������������������������������������������������������������������
	       if( CurrTopLineStock > /*28*/26 ) { CurrTopLineStock -= /*28*/26; } else { CurrTopLineStock = 1; }
	       if( CurrTopLineStock < 1  ) { CurrTopLineStock = 1; }
	       PrintWholeScreenStock();
	       GetLineStock();
	       EditCursorStock(0);
		Recalcula_Porcentaje(1);
       break;
//����������������������������������������������������������������������������
      // AvP�g
      case 81:
       // CurrRow = 1;
//����������������������������������������������������������������������������
	       CurrTopLineStock += /*28*/26;
	       if ( (CurrTopLineStock+CurrRowStock1) > Stock1_inx.Fin) { CurrTopLineStock = Stock1_inx.Fin; CurrRowStock1 = 1; }
		if (CurrTopLineStock==0) CurrTopLineStock = 1;
	       PrintWholeScreenStock();
	       GetLineStock();
	       EditCursorStock(0);
	       Recalcula_Porcentaje(1);
//����������������������������������������������������������������������������
       break;
      // Salir  F1
      case  59:
	finished = 1;
	break;
      default:
	break;
     }
     EditCursorStock(0);
   }
 } else if(raton!=0 && ( boton_izq==1 || boton_der==1 ) )
     if( (bpush = CheckButon( 9, Comunes))>0 ) {
       EditCursorStock(1);
       switch(bpush){
	 // Flecha Arriba
	 case 1:
		Mueve_Arriba_Stock();
		Recalcula_Porcentaje(1);
		break;
	 // Buscar
	 case 2:
//		BuscarVend();
		break;
	 // Flecha Abajo
	 case 3:
		Mueve_Abajo_Stock();
		Recalcula_Porcentaje(1);
		break;
	 case 4:
		break;
	 // Imprimir
	 case 5:
		Imprimir_Stock();
		break;
	 // A�adir
	 case 6:
//		if( Confirma("A�ADIR NUEVO STOCK", "INICIAL", "") )
//		 if ( Pide_Codigo(SV_CFG.COD_Stock) )
//							AddRecordStock();
		break;
	 // Borrar
	 case 7:
		if( Confirma("ELIMINAR TOTALMENTE", "STOCK ACTUAL", "") )
//		 if ( Pide_Codigo(SV_CFG.COD_Stock) )
							DeleteRecordStock();
		Recalcula_Porcentaje(1);
		break;
	 // Salir
	 case 9:
		finished = 1;
		break;
	 default:
		break;
       }
       EditCursorStock(0);
     } else CompruebaLineasStock();

}while(!finished);

fclose(Stock);          // Fichero Vendedores
fclose(Ref);          // Fichero Vendedores
free(PS); // Libera la memoria

if(Memory_ok==1) farfree(Addr); else free(Ref_inx_addr);



}

void Mueve_Abajo_Stock(void){

 int CurrRecord = CurrTopLineStock + CurrRowStock1 - 1;

 if (CurrRowStock1==/*28*/40) {
   if ( (CurrRecord - 1) >= Stock1_inx.Fin) { BEEP(); EditCursorStock(0); }
   else { CurrTopLineStock++;
	  PrintWholeScreenStock();
	  //������������������������������������������������������������
	  //������������������������������������������������������������
	  //������������������������������������������������������������
	  GetLineStock();
// PrintLineStock();
   }
 } else {
  if( (CurrRecord - 1) >= Stock1_inx.Fin) { BEEP(); EditCursorStock(0); }
   else { CurrRowStock1++;
	  GetLineStock();
// PrintLineStock();
	 }
 }

}

void Mueve_Arriba_Stock(void){
 int CurrRecord = CurrTopLineStock + CurrRowStock1 - 1;

 if (CurrRowStock1==1) {
  if ((CurrRecord - 1) <= 0) { BEEP(); EditCursorStock(0); }
   else {
	  CurrTopLineStock--;
	  PrintWholeScreenStock();
	  //������������������������������������������������������������
	  //������������������������������������������������������������
	  //������������������������������������������������������������
	  GetLineStock();
// PrintLineStock();
   }

 } else {
  if( (CurrRecord - 1) <= 0) BEEP();
   else { CurrRowStock1--;

	  GetLineStock();
// PrintLineStock();
	 }
 }

}

void EditCursorStock(int ShowHide) {

char Buffer[80];

  setcolor(GRIS_C);
  setfillstyle(SOLID_FILL, GRIS_C);
  bar(10, 455, 545, 473);


 if (!ShowHide) {
		  setcolor(63);
		  setfillstyle(SOLID_FILL, 63);
		  bar((PS[CurrColStock1-1].left+2), PS[CurrColStock1-1].up+(CurrRowStock1-1)*/*15*/12+/*2*/4, PS[CurrColStock1-1].right-2, PS[CurrColStock1-1].up+/*3*/4+((CurrRowStock1)-1)*12+10);
		  setcolor(0);
  } else {
		  setcolor(0);
		  setfillstyle(SOLID_FILL, 0);
		  bar((PS[CurrColStock1-1].left+2), PS[CurrColStock1-1].up+(CurrRowStock1-1)*/*15*/12+/*2*/4, PS[CurrColStock1-1].right-2, PS[CurrColStock1-1].up+/*3*/4+((CurrRowStock1)-1)*12+10);
		  Texto_Normal
  }

  if(Ref_no_valid) setcolor(ROJO);

// struct Stock_actual { 	int  Ref; long Und_0; long Und_1;    };


  switch(CurrColStock1){
// LA REFERENCIA DEBE SER INDEDITABLE...

		   case 1:            //4
		     sprintf(Buffer, "% 5d", Stock1.Ref );
		     outtextxy( PS[0].left+2, (PS[0].up+3)+(CurrRowStock1-1)*12, Buffer );
		     outtextxy(  15, 455, "Referencia de Producto.     4 digitos m�ximo.");
		     break;
		   case 2:
		     outtextxy( PS[1].left+2, (PS[1].up+3)+(CurrRowStock1-1)*12, Ref1.Concepto );
		     outtextxy(  15, 455, "Concepto.");
		     break;
		   case 3:
		     sprintf(Buffer, " % 5d", Stock1.Und_1); // 4
		     outtextxy( PS[2].left+2, (PS[2].up+3)+(CurrRowStock1-1)*12, Buffer);
		     outtextxy(  15, 455, "Unidades con Cod. 1");
		     break;
		   case 4:
		     sprintf(Buffer, " % 5d", Stock1.Und_0); // 4
		     outtextxy( PS[3].left+2, (PS[3].up+3)+(CurrRowStock1-1)*12, Buffer );
		     outtextxy(  15, 455, "Unidades con Cod. 0");
		     break;
		   case 5:
		     sprintf(Buffer, "% 10d", ( Stock1.Und_0 + Stock1.Und_1 ) ); // 4
		     outtextxy( PS[4].left+2, (PS[4].up+3)+(CurrRowStock1-1)*12, Buffer );
		     outtextxy(  15, 455, "Unidades TOTALES.");
		     break;
  }

}



void PrintLineStock(void){

	char Buffer[80];

 setcolor(0);
 setfillstyle(SOLID_FILL, 0);
 bar((PS[0].left+2), PS[0].up+(CurrRowStock1-1)*12+/*2*/4, PS[0].right-2, PS[0].up+(CurrRowStock1-1)*12+10);
 bar((PS[1].left+2), PS[1].up+(CurrRowStock1-1)*12+/*2*/4, PS[1].right-2, PS[1].up+(CurrRowStock1-1)*12+10);
 bar((PS[2].left+2), PS[2].up+(CurrRowStock1-1)*12+/*2*/4, PS[2].right-2, PS[2].up+(CurrRowStock1-1)*12+10);
 bar((PS[3].left+2), PS[3].up+(CurrRowStock1-1)*12+/*2*/4, PS[3].right-2, PS[3].up+(CurrRowStock1-1)*12+10);
 bar((PS[4].left+2), PS[4].up+(CurrRowStock1-1)*12+/*2*/4, PS[4].right-2, PS[4].up+(CurrRowStock1-1)*12+10);

 Texto_Normal

	  if(Ref_no_valid) setcolor(ROJO);
                                      //
		     sprintf(Buffer, "% 5d", Stock1.Ref );
		     outtextxy( PS[0].left+2, (PS[0].up+3)+(CurrRowStock1-1)*12, Buffer );
		     outtextxy( PS[1].left+2, (PS[1].up+3)+(CurrRowStock1-1)*12, Ref1.Concepto );
		     sprintf(Buffer, " % 5d", Stock1.Und_1); // 4
		     outtextxy( PS[2].left+2, (PS[2].up+3)+(CurrRowStock1-1)*12, Buffer);
		     sprintf(Buffer, " % 5d", Stock1.Und_0); // 4
		     outtextxy( PS[3].left+2, (PS[3].up+3)+(CurrRowStock1-1)*12, Buffer );
		     sprintf(Buffer, "% 10d", ( Stock1.Und_0 + Stock1.Und_1 ) ); // 4
		     outtextxy( PS[4].left+2, (PS[4].up+3)+(CurrRowStock1-1)*12, Buffer );

}


void PrintWholeScreenStock(void){

int tmp = CurrRowStock1;

for(int i=0; i<5; i++)
	ImprimeBoton (1, NEGRO, GRIS_O, BLANCO, PS[i]);


  for(CurrRowStock1 = 1; CurrRowStock1 < (/*28*/40+1); CurrRowStock1++)

	if ( (CurrTopLineStock + CurrRowStock1 - 1) <= (Stock1_inx.Fin + 1) ) {
		GetLineStock();
		PrintLineStock();
	}



CurrRowStock1 = tmp;

}

void Edit_Item_Stock(void){

	char buffer[80];

	 switch (CurrColStock1) {

		case 1:/*
			if( Stock1.Ref == -1 ) {
			 sprintf( buffer, "%d", Stock1.Ref);
			 InputCadenaG_T4( buffer, 1, 4, 63, 57, PS[0], CurrRowStock1-1);
			 Stock1.Ref = atoi(buffer);
                         PutLineStock();
			}
                       */
                        while( kbhit() ) getch();
			break;
                case 2:
                        while( kbhit() ) getch();
			break;
		case 3: // int Old_Und_0 = Stock1.Und_0;
			sprintf( buffer, "%d", Stock1.Und_1);
			InputCadenaG_T4( buffer,      1,    4, 63, 57, PS[2], CurrRowStock1-1);
			Stock1.Und_1 = atol(buffer);
			PutLineStock();
			break;
		case 4: // int Old_Und_1 = Stock1.Und_1;
			sprintf( buffer, "%d", Stock1.Und_0);
			InputCadenaG_T4( buffer,      1,    4, 63, 57, PS[3], CurrRowStock1-1);
			Stock1.Und_0 = atol(buffer);
			PutLineStock();
			break;
		default:
			return;
	 }

	 PrintLineStock();

}

void Abre_Ficha_Stock(void){

char ArchivoDatos[]="SV_Stock.DAT";

//��������������������������������������������������������������������������//
//�     Stockrueba apertura correcta de Indice, y Datos y creaci�n...       �//
//��������������������������������������������������������������������������//

  int LSJ = sizeof(struct JD);
  int LSI = sizeof(struct Stock_inx);
  int LSC = sizeof(struct Stock_actual);

  if ( (Stock = fopen( ArchivoDatos, "r+b"))==NULL) {

    FILE *fa;

    if ( (fa = fopen( ArchivoDatos, "wb" ) )!=NULL ) {

       Poner_a_cero_estructura_actual_Stock();
       Stock1_inx.Fin = 0;
       rewind(fa);

       fwrite(&cabecera, LSJ, 1, fa);   fseek(fa,   (long)LSJ,     SEEK_SET);
       fwrite(&Stock1_inx,      LSI, 1, fa);   fseek(fa,   (long)LSJ+LSI, SEEK_SET);
       fwrite(&Stock1, LSC, 1, fa);

       if (fclose(fa) == EOF ) exit(1);
    }

    if ( (Stock = fopen( ArchivoDatos, "r+b"))==NULL) {
						      restorecrtmode();
						      printf("NO SE PUDO ABRIR EL FICHERO INDICE EN LA 2� PASADA");
						      exit(1);
    }
  }

  fseek(Stock,   (long)LSJ,     SEEK_SET);
  if ( fread(&Stock1_inx,       LSI, 1, Stock) != 1 ) {
						      restorecrtmode();
						      printf("NO SE PUDO LEER EN EL FICHERO: INDICE");
						      exit(1);
  }
  fseek(Stock,   (long)LSJ+LSI, SEEK_SET);
  if ( fread(&Stock1,        LSC, 1, Stock) != 1 ) {
						      restorecrtmode();
						      printf("NO SE PUDO LEER EN EL FICHERO: DATOS");
						      exit(1);
  }


}

void DeleteRecordStock(void) {

unsigned int CurrRecord;
  int LSJ = sizeof(struct JD);
  int LSI = sizeof(struct Stock_inx);
  int LSC = sizeof(struct Stock_actual);

CurrRecord = CurrTopLineStock + CurrRowStock1 - 1;

  if ( (Stock1_inx.Fin == 0) ) { BEEP();

   Poner_a_cero_estructura_actual_Stock();
   Stock1.Ref=-1;
   fseek(Stock, (long)LSJ + LSI + ( LSC * ( 0 ) ), SEEK_SET);   // Se coloca
   fwrite(&Stock1, LSC, 1, Stock);       				           	       // Y escribe
/* ���� */   PrintWholeScreenStock();
/*    � */   GetLineStock();
	     EditCursorStock(0);


  } else {

	   Stock1_inx.Fin -= 1;

	   unsigned int p = CurrRecord - 1;

	   while( p <= Stock1_inx.Fin ) {

		fseek(Stock, (long)LSJ + LSI + ( LSC * ( p + 1 ) ), SEEK_SET); // Se coloca
		fread(&Stock1, LSC, 1, Stock);     			      // Y lee

		fseek(Stock, (long)LSJ + LSI + ( LSC * p ), SEEK_SET);         // Se coloca
		fwrite(&Stock1, LSC, 1, Stock);    				      // Y escribe

		p++;
	   };

	   fseek(Stock, (long)LSJ, SEEK_SET);
	   fwrite(&Stock1_inx, LSI, 1, Stock);          // Acualiza n� de registros

/* �    */   if ((CurrRecord - 1)  == Stock1_inx.Fin + 1) CurrRowStock1--;
/* �    */
/* ���� */   PrintWholeScreenStock();
/*    � */   GetLineStock();
	     EditCursorStock(0);

//  PrintLineStock();
  }
}



void AddRecordStock(void){

unsigned int CurrRecord= CurrTopLineStock + CurrRowStock1 - 1;

  int LSJ = sizeof(struct JD);
  int LSI = sizeof(struct Stock_inx);
  int LSC = sizeof(struct Stock_actual);

if(CurrRecord < 10000 ) {

  long avance = Stock1_inx.Fin /*+ 1*/;

   while( avance >= (CurrRecord-1) ) {

	fseek(Stock, (long)LSJ + LSI + ( LSC * avance ), SEEK_SET); // Se coloca
	fread(&Stock1, LSC, 1, Stock);     			   // Y lee

	fseek(Stock, (long)LSJ + LSI + ( LSC * (avance + 1) ), SEEK_SET);  // Se coloca
	fwrite(&Stock1, LSC, 1, Stock);    				  // Y escribe

	avance--;
   };

//   Stock1_inx.Fin = Stock1_inx.Fin + 1;
	Stock1_inx.Fin++;

   fseek(Stock,   (long)LSJ, SEEK_SET);
   fwrite(&Stock1_inx, LSI, 1, Stock);                  // Acualiza n� de registros

   Poner_a_cero_estructura_actual_Stock();
   Stock1.Ref=-1;

   fseek(Stock, (long)LSJ + LSI + ( LSC * ( CurrTopLineStock + CurrRowStock1 - 1) ), SEEK_SET);   // Se coloca
   fwrite(&Stock1, LSC, 1, Stock);       				           	       // Y escribe

   CurrRowStock1++;
	if (CurrRowStock1>/*28*/40) { CurrTopLineStock++; CurrRowStock1--; }
   PrintWholeScreenStock();
   GetLineStock();
   EditCursorStock(0);
// PrintLineStock();

  }

}


void PutLineStock(void){

  int LSJ = sizeof(struct JD);
  int LSI = sizeof(struct Stock_inx);
  int LSC = sizeof(struct Stock_actual);

  int CurrRecord = CurrTopLineStock + CurrRowStock1 - 1;

fseek(Stock, (long)LSJ + LSI + ( LSC * CurrRecord ) - LSC, SEEK_SET);   // Se coloca
fwrite(&Stock1, LSC, 1, Stock);       			   	       // Y escribe

}

void GetLineStock(void){

  int LSJ = sizeof(struct JD);
  int LSI = sizeof(struct Stock_inx);
  int LSC = sizeof(struct Stock_actual);

  int CurrRecord = CurrTopLineStock + CurrRowStock1 - 1;

fseek(Stock, (long)LSJ + LSI + ( LSC * CurrRecord ) - LSC, SEEK_SET);   // Se coloca
fread(&Stock1, LSC, 1, Stock);                                           // Y lee

  BuscaReferencia(Stock1.Ref);

}





void Poner_a_cero_estructura_actual_Stock(void){

       cabecera.esc = 27;      strcpy(cabecera.color, "[31;40m");
       strcpy(cabecera.prog, "Jos� David Guill�n Dominguez");
       cabecera.nl = 13;  cabecera.rt = 10;
       strcpy(cabecera.autor, "		 6 / Mayo  / 1995 ");
       cabecera.pt =  7;  cabecera.ef = 26;

       Stock1.Ref=0; Stock1.Und_1=0; Stock1.Und_0=0;

}


void Imprimir_Stock(void){

int LSJ = sizeof(struct JD);
int LSI = sizeof(struct Stock_inx);
int LSC = sizeof(struct Stock_actual);
int lprint = 3, Curr_Stock = 0;

if ( Comprueba_Impresora() == 0 ) {
Confirma("Error Imprimiendo.", "Impresora Fuera de", "Servicio.");
return;
}

fprintf( stdprn, "\n\r");


  fprintf( stdprn, " Ref   Concepto                  Und.C.0      Und.C.1         TOTAL\n\r");
  fprintf( stdprn, "��������������������������������������������������������������������������������\n\r");

  while( (Curr_Stock <= Stock1_inx.Fin) ){

	fseek(Stock, (long)LSJ + LSI + ( LSC * Curr_Stock ), SEEK_SET);   // Se coloca
	fread(&Stock1, LSC, 1, Stock);                                    // Y lee
	BuscaReferencia(Stock1.Ref);

		fprintf( stdprn, "% 5d  %-20s       % 4ld       % 4ld         % 8ld  \n\r", Stock1.Ref, Ref1.Concepto, Stock1.Und_0, Stock1.Und_1,  ( Stock1.Und_0 + Stock1.Und_1 ) );

		lprint++;
		if (lprint==55) { lprint = 3;
					    fputc(12, stdprn);
					    fprintf( stdprn, " Ref   Concepto                  Und.C.0      Und.C.1         TOTAL\n\r");
					    fprintf( stdprn, "��������������������������������������������������������������������������������\n\r");
		}
	Curr_Stock++;
  }
   fputc(12, stdprn);
   GetLineStock();

}


void CompruebaLineasStock(void){
     // Check una pulsacion sobre un campo, y se situa, si es posible
     int ok_p=0, inc=0, md=0;
     int oldr, oldc;
//     Anclar_Raton();

     do{
	inc++;
	if(x_raton >= (PS[(inc)-1].left+2)  && x_raton <= (PS[inc-1].right-2) ) { ok_p=1; md = 1;
									      oldc=inc; }
	if(inc==5) ok_p=1;
     }while(!ok_p);
     if(md==1){
	     inc=0; 	     ok_p=0;	     md = 0;
     do{
	inc++;
	if( (y_raton >= (PS[(oldc)-1].up+4+(inc-1)*15+2))   && (y_raton <= (PS[oldc-1].up+4+(inc-1)*15+16)) ) { ok_p=1; oldr=inc; md = 1;}
	if(inc==26) ok_p=1;
     }while(!ok_p);
     }
     int newc;

     switch(oldc){
      case 1:
      case 2: newc = oldc; break;
      case 4:
      case 5: newc = (oldc - 1); break;
      default:
	      break;
     }


     if( (md==1) && ((CurrColStock1!=newc) || (CurrRowStock1!=oldr)) )
			{

			  if( (CurrTopLineStock + oldr - 2 )<= Stock1_inx.Fin ){
				  EditCursorStock(1);
				  switch(oldc){
				   case 1:
				   case 2:
					  CurrColStock1 = oldc;
					  CurrRowStock1 = oldr;
					  break;
				   case 4:
				   case 5:
					  CurrColStock1 = (oldc-1);
					  CurrRowStock1 = oldr;
					  break;
				   default:
					  EditCursorStock(0);
					  break;
				  }
				  GetLineStock();
			  }
			}

     }


void Actualizar_Stock(int Referencia, int Unidades, int Codigo){

  int LSJ = sizeof(struct JD);
  int LSI = sizeof(struct Stock_inx);
  int LSC = sizeof(struct Stock_actual);

  int ok = 0;
  int Curr_Stock = 0, Encuentro = 0;


  while( (Curr_Stock <= Stock1_inx.Fin) && (ok == 0) ){

	fseek(Stock, (long)LSJ + LSI + ( LSC * Curr_Stock ), SEEK_SET);   // Se coloca
	fread(&Stock1, LSC, 1, Stock);                                    // Y lee

	if( Stock1.Ref == Referencia ) {
					if(Codigo==1) Stock1.Und_1 += Unidades;
					else Stock1.Und_0 += Unidades;
                                        fseek(Stock, (long)LSJ + LSI + ( LSC * Curr_Stock ), SEEK_SET);   // Se coloca
					fwrite(&Stock1, LSC, 1, Stock);   // Y escribe
					ok = 1;
					Encuentro = 1;
	}

	Curr_Stock++;
  }

  if (Encuentro==0) {
	Stock1.Ref = Referencia;
	if(Codigo==1) { Stock1.Und_1 = Unidades; Stock1.Und_0 = 0; }
	else { Stock1.Und_0 = Unidades; Stock1.Und_1 = 0; }

	fseek(Stock, (long)LSJ + LSI + ( LSC * (Stock1_inx.Fin + 1) ), SEEK_SET);   // Se coloca
	fwrite(&Stock1, LSC, 1, Stock);                        // Y escribe

	Stock1_inx.Fin ++;

	fseek(Stock,   (long)LSJ, SEEK_SET);
	fwrite(&Stock1_inx, LSI, 1, Stock);                  // Acualiza n� de registros
  }


}

void Recalcula_Porcentaje(int Individual_Seleccionado){

 if( Individual_Seleccionado == 1 ) {

   long P_0, P_1;
   struct boton Ptmp;

   Ptmp.up    = 15;                Ptmp.down  = 213;
   Ptmp.left  = 349;               Ptmp.right = 549;
   ImprimeBoton (1, NEGRO, GRIS_O, BLANCO, Ptmp);

//   Centro = (449, 114) radio = 80
// si  (Stock1.Und_0 + Stock1.Und_1) es 100
//     Stock1.Und_0		  es PS

// si  360  es  100
//       x  es   PS
if ( (abs(Stock1.Und_0)+abs(Stock1.Und_1) )!= 0 ) {

 P_0 = labs( ( 360 * ( (labs(Stock1.Und_0) * 100) / (labs(Stock1.Und_0)+labs(Stock1.Und_1)) ) ) / 100);
 P_1 = labs( ( 360 * ( (labs(Stock1.Und_1) * 100) / (labs(Stock1.Und_0)+labs(Stock1.Und_1)) ) ) / 100);
 // struct Stock_actual { 	int  Ref; long Und_0; long Und_1;    };
 if( Stock1.Und_1 < 0 )
			{ P_0 += P_1; P_1 = 0; }
 if( Stock1.Und_0 < 0 )
			{ P_1 += P_0; P_0 = 0; }
if( P_1 != 0 ) {
 setcolor(ROJO);
 setfillstyle(9, ROJO);
 pieslice(449, 114, 0, P_1, 80);
}
if( P_0 != 0 ) {
 setcolor(AZUL);
 setfillstyle(9, AZUL);
 pieslice(449, 114, ( 360 - P_0 ), 360, 80);
}

}


 } else {

 // struct Stock_actual Stock1_Graficas[10];
 // bar3d


 }

}