#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <time.h>
#include "msql.h"

int msqlsock;
char datum[100];

#define VERBOSE 1

#define DATUM "00000000"

#define log(a) printf("%s\n", a)

char mesec[13];

int e_tim();

char *get_trans_date();
int get_bill();

/* MAIN */

main(int argc, char **argv) {
int credits, rows, members, id_bill;
char line[1000];
int **i, j, k, n, last_trans, prev_trans, prev_bill;
m_result *mresult;
m_row mrow;

for (j=0; j<13; j++) {
   mesec[j]=31;
   }
mesec[2]=28;

/* ------------------------------------------------------------------------- */
/*                      Konektovanje na LOCAL mSQL server                    */
/* ------------------------------------------------------------------------- */

if ((msqlsock=msqlConnect(NULL))<0) {
   log("Can not establish connection to mSQL server.");
   log(msqlErrMsg); 
   exit(0);
   }
if (msqlSelectDB(msqlsock, "MEMBERS")<0) {
   log(msqlErrMsg); 
   exit(0);
   }

/* ------------------------------------------------------------------------- */
/*                    Iz baze proveri da li je ovo MEMBER                    */
/* ------------------------------------------------------------------------- */

if ((rows=msqlQuery(msqlsock, "SELECT id_us FROM user_data WHERE member='1'"))<0) {
   log(msqlErrMsg); 
   exit(0);
   }
mresult=msqlStoreResult();
j=msqlNumRows(mresult);

#ifdef VERBOSE1
   printf("Have to reserve memory for %d members.\n", j);
#endif

i=(int**)malloc(k);
for (k=0; k<=j; k++) 
   i[k]=(int*)malloc(sizeof(int));

#ifdef VERBOSE1
   printf("Done reserving memory, getting all members NOW!\n");
#endif

j=0;
while ((mrow=msqlFetchRow(mresult))!=NULL) {
   n=atoi(mrow[0]);
   i[j]=(int*)n;
   j++;
   }
msqlFreeResult(mresult);
members=j;

for (k=0; k<members; k++) {
   printf("----------------------------------------------\n");
   id_bill=get_id_bill(i[k], &prev_bill);

   #ifdef VERBOSE
      printf("BILLING: id_us=%d ==> id_bill=%d\n", i[k], id_bill);
      printf("ROWS in 'billing' table: %d\n", j);
   #endif
 
   if (n>0 & j>0) {
      strcpy(datum, get_trans_date(id_bill, &last_trans, &prev_trans));
      }
   else {
      strcpy(datum, DATUM);
      }
   #ifdef VERBOSE
      printf("BILLING: id_bill=%d ==> datum=%s\n", id_bill, datum);
   #endif
   }

   e_tim(&datum, 31);

printf("EXIT\n");
}

/* ------------------------------------------------------------------------- */
char *get_trans_date(int id_bill, int *last, int *prev) {
int rows, j;
char line[200];
m_result *mresult;
m_row mrow;

sprintf(line, "SELECT datum, id_trans FROM transaction WHERE id_bill=%d ORDER BY id_trans DESC", id_bill);
#ifdef VERBOSE1
   printf("%s\n", line);
#endif
if ((rows=msqlQuery(msqlsock, line))<0) {
   log(msqlErrMsg); 
   exit(0);
   }
mresult=msqlStoreResult();
j=msqlNumRows(mresult);
#ifdef VERBOSE
   printf("ROWS in transaction table: %d\n", j);
#endif
if (j>0) {
   mrow=msqlFetchRow(mresult);
   strncpy(datum, mrow[0], 8);
   datum[8]=8;
   }
else
   strcpy(datum, DATUM);

msqlFreeResult(mresult);   
return(datum);
}

/* ------------------------------------------------------------------------- */
int get_id_bill(int id_us, int *prev_bill) {
int j, n, rows;
char line[200];
m_result *mresult;
m_row mrow;

sprintf(line, "SELECT id_bill FROM billing WHERE id_us=%d ORDER BY id_bill DESC", id_us);
#ifdef VERBOSE1
   printf("%s\n", line);
#endif
if ((rows=msqlQuery(msqlsock, line))<0) {
log(msqlErrMsg); 
   exit(0);
   }
mresult=msqlStoreResult();
j=msqlNumRows(mresult);
n=-1;
if (j>0) {
   mrow=msqlFetchRow(mresult);
   n=atoi(mrow[0]);
   }
msqlFreeResult(mresult);
return (n);
}

/* ------------------------------------------------------------------------- */

int e_tim(char *datt, int mes) {
char sy[20], sm[20], sd[20];
int days, months, years, unix_atime;

strncpy(sy, datt, 


return 0;
}
