/* $Id: rasf8dr.c,v 5.58 2013/08/13 05:49:53 sasa Exp $ */
/*14.04.2021	13.07.2000	Белых А.И.	rasf8drw.c
Вигрузка в *.dbf или *.lst файл формы 8ДР
*/
#include        <errno.h>
#include        "buhg_g.h"
void rasf8drw_kolrab(const char *nomd,int *kolr,GtkWidget *wpredok);
int  rasf1df_m(class iceb_u_str *kolih_v_htate,class iceb_u_str *kolih_po_sovm,GtkWidget *wpredok);

extern SQL_baza bd;

int rasf8drw(const char *nomd,
int metka_oth, //0-звітний 1-новий звітний 2-уточнюючий
class spis_oth *oth,
GtkWidget *wpredok)
{
int		kolstr;
SQL_str         row,row1;
char		strsql[1024];
class iceb_u_str inn("");
double		sumad,suman,isumad,isuman;
double		sumadn,sumann,isumadn,isumann;
class iceb_u_str priz("");
short		lgota=0;
class iceb_u_str lgotach("");
char		data1[32];
char		data2[32];
char		data11[32];
char		data21[32];
char		data12[32];
char		data22[32];
int		npp1;
char		imaf[64],imaffam[64];
FILE *ff_xml;
short		d,m,g;
int		kolr; /*Количество физических лиц*/
int 	klst=0; /*Количество строк*/
short		klls=1; /*Количество листов*/
short		god;
int		kvrt=0;
int		vidd;
class iceb_u_str bros("");
class iceb_u_str kod("");
short		TYP; /*Метка 0-юридическая особа 1-физическая особа*/
class iceb_u_str priznak("");
static class iceb_u_str kolih_v_htate("");
static class iceb_u_str kolih_po_sovm("");

class SQLCURSOR curr;
char imaf_xml[512];
memset(imaf_xml,'\0',sizeof(imaf_xml));


/*Читаем шапку документа*/

sprintf(strsql,"select * from F8dr where nomd='%s'",nomd);
if(iceb_sql_readkey(strsql,&row,&curr,wpredok) < 1)
 {
  iceb_menu_soob(gettext("Не найден документ !"),wpredok);
  return(1);
 } 
god=atoi(row[0]);
kvrt=atoi(row[1]);
int nomer_mes=atoi(row[12]);
vidd=atoi(row[3]);
kolih_v_htate.new_plus(row[10]);
kolih_po_sovm.new_plus(row[11]);


short mr=1;
if(kvrt == 1)
 mr=nomer_mes;
if(kvrt == 2 && nomer_mes == 1)
 mr=4;
if(kvrt == 2 && nomer_mes == 2)
 mr=5;
if(kvrt == 2 && nomer_mes == 3)
 mr=6;
if(kvrt == 3 && nomer_mes == 1)
 mr=7;
if(kvrt == 3 && nomer_mes == 2)
 mr=8;
if(kvrt == 3 && nomer_mes == 3)
 mr=9;
if(kvrt == 4 && nomer_mes == 1)
 mr=10;
if(kvrt == 4 && nomer_mes == 2)
 mr=11;
if(kvrt == 4 && nomer_mes == 3)
 mr=12;
 


sprintf(strsql,"select inn,sumad,suman,priz,datap,datau,lgota,fio,sumadn,sumann,pr,vsn,vsv \
from F8dr1 where nomd='%s' order by inn asc,pr desc",nomd);

SQLCURSOR cur;
if((kolstr=cur.make_cursor(&bd,strsql)) < 0)
 {
  iceb_msql_error(&bd,gettext("Ошибка создания курсора !"),strsql,wpredok);
  return(1);
 }

if(kolstr == 0)
 {
  iceb_menu_soob(gettext("Не найдено ни одной записи !"),wpredok);
  return(1);
 }

TYP=0;
class iceb_u_str telef_kontr("");
class iceb_u_str naim_kontr("");

sprintf(strsql,"select naikon,kod,telef from Kontragent where kodkon='00'");
if(sql_readkey(&bd,strsql,&row,&curr) == 1)
 {
  naim_kontr.new_plus(row[0]);
  kod.new_plus(row[1]);
  telef_kontr.new_plus(row[2]);
 }

if(iceb_poldan("Физическое лицо",&bros,"zarsdf1df.alx",wpredok) == 0)
 if(iceb_u_SRAV(bros.ravno(),"Вкл",1) == 0)
  {
   TYP=1;
  }

rasf8drw_kolrab(nomd,&kolr,wpredok);
kolih_v_htate.new_plus(kolr);
if(rasf1df_m(&kolih_v_htate,&kolih_po_sovm,wpredok) != 0)
 return(1);



class iceb_fopen fil_p4;

sprintf(imaf,"dov%d_%d.lst",kvrt,getpid());
if(fil_p4.start(imaf,"w",wpredok) != 0)
 return(1);

 
sprintf(imaffam,"dovf%d_%d.lst",kvrt,getpid());
class iceb_fopen fil_p4fam;
if(fil_p4fam.start(imaffam,"w",wpredok) != 0)
 return(1);
//Заголовок распечаток

zagf1dfw(kolih_v_htate.ravno_atoi(),kolih_po_sovm.ravno_atoi(),kvrt,kod.ravno(),TYP,&klst,vidd,0,0,god,0,0,0,metka_oth,fil_p4.ff,fil_p4fam.ff,wpredok);

spkvdw(1,&klst,fil_p4.ff,wpredok);


f1df_xml_zagw(mr,god,kod.ravno(),TYP,metka_oth,kolih_v_htate.ravno_atoi(),kolih_po_sovm.ravno_atoi(),naim_kontr.ravno(),imaf_xml,&ff_xml,wpredok);

double vsn=0.,vsv=0.;

isumad=isuman=0.;
isumadn=isumann=0.;
npp1=0;
//int kolstr1=0;
int nomer_xml_str=0;
while(cur.read_cursor(&row) != 0)
 {
//  strzag(LINES-1,0,kolstr,++kolstr1);  
  inn.new_plus(row[0]);

  sumad=atof(row[1]);
  sumad=iceb_u_okrug(sumad,0.01);

  suman=atof(row[2]);
  suman=iceb_u_okrug(suman,0.01);

  sumadn=atof(row[8]);
  sumadn=iceb_u_okrug(sumadn,0.01);

  sumann=atof(row[9]);
  sumann=iceb_u_okrug(sumann,0.01);
  vsn=atof(row[11]);
  vsv=atof(row[12]);
  
  isumad+=sumad;
  isuman+=suman;

  isumadn+=sumadn;
  isumann+=sumann;

  priz.new_plus(row[3]);

  d=m=g=0;
  iceb_u_rsdat(&d,&m,&g,row[4],2);
  memset(data1,'\0',sizeof(data1));
  memset(data11,'\0',sizeof(data11));
  memset(data12,'\0',sizeof(data12));
  if(d != 0)
   {
    sprintf(data1,"%04d%02d%02d",g,m,d);
    sprintf(data11,"%02d.%02d.%4d",d,m,g);
    sprintf(data12,"%02d%02d%4d",d,m,g);
   }
  d=m=g=0;
  iceb_u_rsdat(&d,&m,&g,row[5],2);
  memset(data2,'\0',sizeof(data2));
  memset(data21,'\0',sizeof(data21));
  memset(data22,'\0',sizeof(data22));
  if(d != 0)
   {
    sprintf(data2,"%04d%02d%02d",g,m,d);
    sprintf(data21,"%02d.%02d.%04d",d,m,g);
    sprintf(data22,"%02d%02d%04d",d,m,g);
   }

  lgota=atoi(row[6]);

  if(lgota != 0)
   sprintf(strsql,"%02d",lgota);  
  else
   memset(strsql,'\0',sizeof(strsql));

  lgotach.new_plus(strsql);

  priznak.new_plus("");
  if(vidd != 0)
    priznak.new_plus(row[10]);

  npp1++;    

    
  fprintf(fil_p4.ff,"%-5d|%-10s|%12.2f|%12.2f|%10.2f|%10.2f|%10.2f|%10.2f|  %-4s|%-10s|%-10s|%-10s|%6s|\n",
  npp1,inn.ravno(),sumadn,sumad,sumann,suman,vsn,vsv,priz.ravno(),data11,data21,lgotach.ravno(),priznak.ravno());

    //Узнаем инвентарный номер
    class iceb_u_str fam("");    
    class iceb_u_str tabn("");
    if(row[7][0] == '\0')
     {
      sprintf(strsql,"select tabn,fio from Kartb where inn='%s'",row[0]);
      if(sql_readkey(&bd,strsql,&row1,&curr) == 1)
       {
        tabn.new_plus(row1[0]);
        fam.new_plus(row1[1]);
       }
     }
    else
     {
      fam.new_plus(row[7]);      
     }

  fprintf(fil_p4fam.ff,"%-5d %-10s %12.2f %12.2f %10.2f %10.2f %10.2f %10.2f %-6s %10s %10s %-10s %6s %-5s %s\n",
  npp1,inn.ravno(),sumadn,sumad,sumann,suman,vsn,vsv,priz.ravno(),data11,data21,lgotach.ravno(),priznak.ravno(),tabn.ravno(),fam.ravno());   

  f1df_xml_strw(&nomer_xml_str,inn.ravno(),sumadn,sumad,sumann,suman,vsn,vsv,priz.ravno(),data12,data22,lgotach.ravno(),row[10],ff_xml);
 }
fprintf(fil_p4fam.ff,"ICEB_LST_END\n");
fprintf(fil_p4.ff,"ICEB_LST_END\n");


/*узнаём военный сбор*/
double nah_dohod_vs=0.;
double vip_dohod_vs=0.;
double nah_vs=0.;
double vip_vs=0.;

sprintf(strsql,"select sfr,svs,sfrv,svsv from F1dfvs where nomd='%s'",nomd);
if((kolstr=cur.make_cursor(&bd,strsql)) < 0)
 {
  iceb_msql_error(&bd,gettext("Ошибка создания курсора !"),strsql,wpredok);
  return(1);
 }


while(cur.read_cursor(&row) != 0)
 {
  nah_dohod_vs+=atof(row[0]);
  nah_vs+=atof(row[1]);
  vip_dohod_vs+=atof(row[2]);
  vip_vs+=atof(row[3]);
    
 }

//koncf8drw(kvrt,god,kod.ravno(),TYP,isumad,isumadn,isuman,isumann,isumad,isumadn,isuman,isumann,kolr,klls,&npp1,2,nah_dohod_vs,vip_dohod_vs,nah_vs,vip_vs,fil_p4.ff,fil_p4fam.ff,wpredok);
 koncf8drw(kvrt,god,kod.ravno(),TYP,isumad,isumadn,isuman,isumann,isumad,isumadn,isuman,isumann,kolr,klls,&npp1,2,nah_dohod_vs, vip_dohod_vs,nah_vs,vip_vs,fil_p4.ff,fil_p4fam.ff,wpredok);

fil_p4.end();
fil_p4fam.end();


f1df_xml_konw(isumadn,isumad,isumann,isuman,nomer_xml_str,kolr,klls,naim_kontr.ravno(),telef_kontr.ravno(),TYP,nah_dohod_vs,vip_dohod_vs,nah_vs,vip_vs,ff_xml,wpredok);


oth->spis_imaf.plus(imaf);
oth->spis_naim.plus(gettext("Приложение 4"));

sprintf(strsql,"%s+%s",
gettext("Приложение 4"),
gettext("Фамилия"));
oth->spis_naim.plus(strsql);
oth->spis_imaf.plus(imaffam);

for(int nom=0; nom < oth->spis_imaf.kolih(); nom++)
 {
// iceb_t_ustpeh(oth->spis_imaf.ravno(nom),1);
printf("3-%s\n",__FUNCTION__); 
  int orient=0;
  iceb_ustpeh(oth->spis_imaf.ravno(nom),3,&orient,wpredok);
printf("4-%s\n",__FUNCTION__); 
  iceb_rnl(oth->spis_imaf.ravno(nom),orient,"",&spkvdw,wpredok);
printf("5-%s\n",__FUNCTION__); 
 }

printf("2-%s\n",__FUNCTION__); 

oth->spis_imaf.plus(imaf_xml);
oth->spis_naim.plus(gettext("Документ в формате xml"));

return(0);
}

/**********************************/
/*Определяем количество работников*/
/**********************************/

void rasf8drw_kolrab(const char *nomd,//номер документа
int *kolr, //*Количество физических лиц*/
GtkWidget *wpredok)
{
char strsql[512];
SQLCURSOR       cur;
sprintf(strsql,"select distinct inn from F8dr1 where nomd='%s'",nomd);

if((*kolr=cur.make_cursor(&bd,strsql)) < 0)
  iceb_msql_error(&bd,gettext("Ошибка создания курсора !"),strsql,wpredok);

}
