int idxP  = 0,
    idxN  = 1,
    idxT  = 2,
    idxWT = 3,
    idxS  = 4,
    idxSD = 5,
    idxSP = 6,
    CountIdx=7;
int TblFld2[CountIdx];

int ind1  = 0,
    ind2  = 1,
    ind3  = 2,
    ind4  = 3,
    ind5  = 4,
    CountInd=5;
double Fld[CountInd];

double Summ1=0, Summ2=0;
int    Time1=0, Time2=0;
double SumD1=0;   //double SumD1=0, SumD2=0;
double SumP1=0;   //double SumP1=0, SumP2=0;
double Prib=0;
int Kol1=0, Kol2=0;

//====================================================
int len = 512,
    n = 12;
char Dir[len]; // Рабочий каталог амбы (обычно ZPL)
string nm;     // Имя Excel шаблона
var WorkDir,   // Рабочий каталог модуля
    xls,       // объект Excel.Application
    wb,        // объект workbook
    wsh;       // объект worksheet
int cWsh=1,   // число worksheet'ов в workbook'е
    cWb=1,    // число workbook'ов
    cWbTmp=1; // число временных workbook'ов
//========================================================
var CMsg = 0,
    CFile,
    CTabl;
int cr=0, cfld=0, cr_ins=0, rec_ins=0, rec_work=0, CntHead=19, 
    CntFoot = 10, nStart = 12, tmpi=0, CntStr = 0, vp=0;
//=========================================================


double Sum1N=0, Sum2N=0;    //    для
int    Tim1N=0, Tim2N=0;    //     
double SmD1N=0;             //    итогов
double SmP1N=0;             //    по
double PribN=0;             //
int Kl1N=0, Kl2N=0;         //    НТН

double Sum1KP=0, Sum2KP=0, Sum1ZV=0, Sum2ZV=0;    //
int    Tim1KP=0, Tim2KP=0, Tim1ZV=0, Tim2ZV=0;    //
double SmD1KP=0, SmD1ZV=0;                        //    по
double SmP1KP=0, SmP1ZV=0;                        //    КП
double PribKP=0, PribZV=0;                        //    и
int Kl1KP=0, Kl2KP=0, Kl1ZV=0, Kl2ZV=0;                             //   Заводу

char Buf3[512], Buf4[512];


Tbl3()
{
  
  Setup();

  
  char Buf[512], Buf1[512], Buf2[512];   

  var CTabl = CreateObject("CurPrnTbl");
  if ( !FillIdxFldTbl2(CTabl) )  return;
  

  int cr=CTabl.Count_Rcr(); // число рабочих записей в таблице
 

  int cntRcrTab  =0;       
  double SumPod  =0,      
         ItSum   =0;

  int Nakl = 0;
  string OldPod  = CTabl.TextS("FCpodr",0);
  string OlPodN  = CTabl.TextS("PodrN",0);
  string NewPod;
  string NwPodN;
  int NwPodU;
  SetupExcel();
//______________ по записям таблицы _________________
  for (int zap=0; zap<cr; zap++)
  {
     int kp = 0;
     int zv = 0;
     NewPod  = CTabl.TextS("FCpodr",zap);
     NwPodN  = CTabl.TextS("PodrN",zap);
     NwPodU  = atoi(CTabl.TextS("Podr_U",zap));
     if ( NwPodU == 100) kp = 1;
     if ( NwPodU == 500) zv = 1;

     int cont = 0;    

     if ( NwPodN != OlPodN )
      {
        
        PrintTableToExcel(OldPod);
       
        PrintTableToExcel2(OlPodN);
        
        OldPod=NewPod;
        OlPodN=NwPodN;
        cont = 1;

        
      }

     if ( cont == 0)
      {
        if ( NewPod != OldPod )      // печать строки информации по OldPod
         {

           
           PrintTableToExcel(OldPod);
           OldPod=NewPod;
         }
      }
     Nakl = atof(CTabl.TextS("FCnakl",zap));  // признак накладника
     if ( Nakl != 1 ) Nakl =0;

     if ( Nakl == 1 )
      {
       Summ1 +=  atof(CTabl.TextS("Sum",zap));
       Time1 +=  atof(CTabl.TextS("Work_Time",zap));
       ++Kol1;   
       if (kp == 1)
         {
          Sum1KP +=  atof(CTabl.TextS("Sum",zap));
          Tim1KP +=  atof(CTabl.TextS("Work_Time",zap));
          Kl1KP++;
         }
       if (zv == 1)
         {
          Sum1ZV +=  atof(CTabl.TextS("Sum",zap));
          Tim1ZV +=  atof(CTabl.TextS("Work_Time",zap));
          Kl1ZV++;
         }
      }

     if ( Nakl == 0 )
      {
       Summ2 +=  atof(CTabl.TextS("Sum",zap));
       Time2 +=  atof(CTabl.TextS("Work_Time",zap));
       ++Kol2;

       if (kp == 1)
         {
          Sum2KP +=  atof(CTabl.TextS("Sum",zap));
          Tim2KP +=  atof(CTabl.TextS("Work_Time",zap));
          Kl2KP++;

          SmD1KP +=  atof(CTabl.TextS("SumDop",zap));  // вне зависимости от
          SmP1KP +=  atof(CTabl.TextS("SumPro",zap));  // признака накладник
          PribKP +=  atof(CTabl.TextS("SumPri",zap));

         }
       if (zv == 1)
         {
          Sum2ZV +=  atof(CTabl.TextS("Sum",zap));
          Tim2ZV +=  atof(CTabl.TextS("Work_Time",zap));
          Kl2ZV++;

          SmD1ZV +=  atof(CTabl.TextS("SumDop",zap));  // вне зависимости от
          SmP1ZV +=  atof(CTabl.TextS("SumPro",zap));  // признака накладник
          PribZV +=  atof(CTabl.TextS("SumPri",zap));

         }

      }
     //==================================================
     if(CMsg.Msg_Proc(" Подождите. Идёт экспорт данных в Excel.",100.*zap/cr)==ESC)
     {
        AmbaMessageBox("Печать не была завершена!","Предупреждение!",0,0);
        xls.Visible = true;
        return;
     }
     //==================================================
     SumD1 +=  atof(CTabl.TextS("SumDop",zap));  // вне зависимости от
     SumP1 +=  atof(CTabl.TextS("SumPro",zap));  // признака накладник
     Prib  +=  atof(CTabl.TextS("SumPri",zap));
  }
  
  
  PrintTableToExcel(OldPod);
 
  PrintTableToExcel2(OlPodN);
  PrintTableToExcel3();
  
  //Date_();
  xls.Visible = true;
  wsh.Rows(6).Select();  // которую будем копировать
  xls.Selection.Copy(wsh.Rows(n-1)); // куда копировать
  wsh.Rows(6).Delete();
  wsh.Rows(3).Delete();
}

FillIdxFldTbl2(&CTabl)
{
   TblFld2[idxP] = CTabl.Find_Name_Fld("FCpodr");
   TblFld2[idxN] = CTabl.Find_Name_Fld("FCnakl");
   TblFld2[idxT] = CTabl.Find_Name_Fld("C_FIO");
   TblFld2[idxWT] = CTabl.Find_Name_Fld("Work_Time");
   TblFld2[idxS] = CTabl.Find_Name_Fld("Sum");
   TblFld2[idxSD] = CTabl.Find_Name_Fld("SumDop");
   TblFld2[idxSP] = CTabl.Find_Name_Fld("SumPro");
   return 1;
}


PreFld()
{

  for ( int ind=0; ind<CountInd; ind++ )
   {
     Fld[ind] = 0;  
    // Fld2[ind] = 0;
   }

}


//===============================================
PrintTableToExcel(Frst)
{ // печать строки в Excel
  wsh.Rows(n).EntireRow.Insert();
  wsh.Rows(3).Select();  // которую будем копировать   n-2
  xls.Selection.Copy(wsh.Rows(n-1)); // куда копировать
    
  //=======================================
  double Itogo = Summ1 + Summ2 + SumD1 + SumP1 + Prib;

 
  double Sred1=0; if ((Summ1!=0) && (Kol1!=0)) Sred1=Summ1/Kol1;
  double Sred2=0; if ((Summ2!=0) && (Kol2!=0)) Sred2=Summ2/Kol2;

  
  char c1[9], c2[9], c3[9], c4[9], c5[9], c6[9], c7[9], c8[9], c9[9];
  char c10[9], c11[9], c12[9], c13[9];     
       

  sprintf(c1,"B%d",n-2);
  sprintf(c2,"C%d",n-2);
  sprintf(c3,"D%d",n-2);
  sprintf(c4,"E%d",n-2);
  sprintf(c5,"F%d",n-2);
  sprintf(c6,"G%d",n-2);
  sprintf(c7,"H%d",n-2);
  sprintf(c8,"I%d",n-2);
  sprintf(c9,"J%d",n-2);
  sprintf(c10,"K%d",n-2);
  sprintf(c11,"L%d",n-2);
  sprintf(c12,"M%d",n-2);
  sprintf(c13,"N%d",n-2);
  

  WriteToCell(c1,atoi(Frst));       
  WriteToCell(c2,Kol1);  
  WriteToCell(c3,static_cast_to_double(Time1));
  WriteToCell(c4,static_cast_to_double(Summ1));
  WriteToCell(c5,static_cast_to_double(Sred1));
  WriteToCell(c6,static_cast_to_double(Kol2));
  WriteToCell(c7,static_cast_to_double(Time2));
  WriteToCell(c8,static_cast_to_double(Summ2));
  WriteToCell(c9,static_cast_to_double(Sred2));
  WriteToCell(c10,static_cast_to_double(SumD1));
  WriteToCell(c11,static_cast_to_double(Prib));
  WriteToCell(c12,static_cast_to_double(SumP1));
  WriteToCell(c13,static_cast_to_double(Itogo));
  
  
  //PreFld();
  n=n+1;
  Sum1N += Summ1;
  Sum2N += Summ2;
  Kl1N  += Kol1;
  Kl2N  += Kol2;
  Tim1N += Time1;
  Tim2N += Time2;
  SmD1N += SumD1;
  SmP1N += SumP1;
  PribN += Prib;

  Summ1=0; Summ2=0;
  Kol1=0;  Kol2=0;
  Time1=0; Time2=0;
  SumD1=0; //SumD2=0;
  SumP1=0; //SumP2=0;
  Prib =0;
  return 0;
}

PrintTableToExcel2(Frst)
{ // печать строки в Excel
  wsh.Rows(n).EntireRow.Insert();
  wsh.Rows(10).Select();  // которую будем копировать   n-2
  xls.Selection.Copy(wsh.Rows(n-1)); // куда копировать
    
  //=======================================
  double Itogo = Sum1N + Sum2N + SmD1N + SmP1N + PribN;

  double Sred1=0; if ((Sum1N!=0) && (Kl1N!=0)) Sred1=Sum1N/Kl1N;
  double Sred2=0; if ((Sum2N!=0) && (Kl2N!=0)) Sred2=Sum2N/Kl2N;
  
  char c1[9], c2[9], c3[9], c4[9], c5[9], c6[9], c7[9], c8[9], c9[9];
  char c10[9], c11[9], c12[9], c13[9];     
       

  sprintf(c1,"B%d",n-2);
  sprintf(c2,"C%d",n-2);
  sprintf(c3,"D%d",n-2);
  sprintf(c4,"E%d",n-2);
  sprintf(c5,"F%d",n-2);
  sprintf(c6,"G%d",n-2);
  sprintf(c7,"H%d",n-2);
  sprintf(c8,"I%d",n-2);
  sprintf(c9,"J%d",n-2);
  sprintf(c10,"K%d",n-2);
  sprintf(c11,"L%d",n-2);
  sprintf(c12,"M%d",n-2);
  sprintf(c13,"N%d",n-2);
  

  WriteToCell(c1,atoi(Frst));       
  WriteToCell(c2,Kl1N);  
  WriteToCell(c3,static_cast_to_double(Tim1N));
  WriteToCell(c4,static_cast_to_double(Sum1N));
  WriteToCell(c5,static_cast_to_double(Sred1));
  WriteToCell(c6,static_cast_to_double(Kl2N));
  WriteToCell(c7,static_cast_to_double(Tim2N));
  WriteToCell(c8,static_cast_to_double(Sum2N));
  WriteToCell(c9,static_cast_to_double(Sred2));
  WriteToCell(c10,static_cast_to_double(SmD1N));
  WriteToCell(c11,static_cast_to_double(PribN));
  WriteToCell(c12,static_cast_to_double(SmP1N));
  WriteToCell(c13,static_cast_to_double(Itogo));
  
  //PreFld();
  n=n+1;
  Sum1N = 0;
  Sum2N = 0;
  Kl1N  = 0;
  Kl2N  = 0;
  Tim1N = 0;
  Tim2N = 0;
  SmD1N = 0;
  SmP1N = 0;
  PribN = 0;
   return 0;
}
PrintTableToExcel3()
{ // печать строки в Excel
  wsh.Rows(n).EntireRow.Insert();
  wsh.Rows(3).Select();  // которую будем копировать   n-2
  xls.Selection.Copy(wsh.Rows(n-1)); // куда копировать
    
  //=======================================
  char c1[9], c2[9], c3[9], c4[9], c5[9], c6[9], c7[9], c8[9], c9[9];
  char c10[9], c11[9], c12[9], c13[9];
  char b1[9], b2[9], b3[9], b4[9], b5[9], b6[9], b7[9], b8[9], b9[9];
  char b10[9], b11[9], b12[9], b13[9];
  double Itogo1 = Sum1KP + Sum2KP + SmD1KP + SmP1KP + PribKP;
  double Itogo2 = Sum1ZV + Sum2ZV + SmD1ZV + SmP1ZV + PribZV;
  double Sred1=0; if ((Sum1KP!=0) && (Kl1KP!=0)) Sred1=Sum1KP/Kl1KP;
  double Sred2=0; if ((Sum2KP!=0) && (Kl2KP!=0)) Sred2=Sum2KP/Kl2KP;
  double Sred3=0; if ((Sum1ZV!=0) && (Kl1ZV!=0)) Sred3=Sum1ZV/Kl1ZV;
  double Sred4=0; if ((Sum2ZV!=0) && (Kl2ZV!=0)) Sred4=Sum2ZV/Kl2ZV;


  sprintf(c1,"B%d",n-2);
  sprintf(c2,"C%d",n-2);
  sprintf(c3,"D%d",n-2);
  sprintf(c4,"E%d",n-2);
  sprintf(c5,"F%d",n-2);
  sprintf(c6,"G%d",n-2);
  sprintf(c7,"H%d",n-2);
  sprintf(c8,"I%d",n-2);
  sprintf(c9,"J%d",n-2);
  sprintf(c10,"K%d",n-2);
  sprintf(c11,"L%d",n-2);
  sprintf(c12,"M%d",n-2);
  sprintf(c13,"N%d",n-2);
  sprintf(b1,"B%d",n-1);
  sprintf(b2,"C%d",n-1);
  sprintf(b3,"D%d",n-1);
  sprintf(b4,"E%d",n-1);
  sprintf(b5,"F%d",n-1);
  sprintf(b6,"G%d",n-1);
  sprintf(b7,"H%d",n-1);
  sprintf(b8,"I%d",n-1);
  sprintf(b9,"J%d",n-1);
  sprintf(b10,"K%d",n-1);
  sprintf(b11,"L%d",n-1);
  sprintf(b12,"M%d",n-1);
  sprintf(b13,"N%d",n-1);
  
  WriteToCell(c1,100);       
  WriteToCell(c2,Kl1KP);  
  WriteToCell(c3,static_cast_to_double(Tim1KP));
  WriteToCell(c4,static_cast_to_double(Sum1KP));
  WriteToCell(c5,static_cast_to_double(Sred1));
  WriteToCell(c6,static_cast_to_double(Kl2KP));
  WriteToCell(c7,static_cast_to_double(Tim2KP));
  WriteToCell(c8,static_cast_to_double(Sum2KP));
  WriteToCell(c9,static_cast_to_double(Sred2));
  WriteToCell(c10,static_cast_to_double(SmD1KP));
  WriteToCell(c11,static_cast_to_double(PribKP));
  WriteToCell(c12,static_cast_to_double(SmP1KP));
  WriteToCell(c13,static_cast_to_double(Itogo1));
  
  WriteToCell(b1,500);       
  WriteToCell(b2,Kl1ZV);  
  WriteToCell(b3,static_cast_to_double(Tim1ZV));
  WriteToCell(b4,static_cast_to_double(Sum1ZV));
  WriteToCell(b5,static_cast_to_double(Sred3));
  WriteToCell(b6,static_cast_to_double(Kl2ZV));
  WriteToCell(b7,static_cast_to_double(Tim2ZV));
  WriteToCell(b8,static_cast_to_double(Sum2ZV));
  WriteToCell(b9,static_cast_to_double(Sred4));
  WriteToCell(b10,static_cast_to_double(SmD1ZV));
  WriteToCell(b11,static_cast_to_double(PribZV));
  WriteToCell(b12,static_cast_to_double(SmP1ZV));
  WriteToCell(b13,static_cast_to_double(Itogo2));
  //PreFld();
  n=n+1;
   return 0;
}


SetupExcel()
{
  xls = CreateObject("Excel.Application");
  GetCurrentDirectory(len,Dir);
  nm = "t_razved\\KB003.xls";
  WorkDir = Dir + "\\";
  xls.Caption = OemToAnsi("Ведомость");
  xls.Workbooks.Add(WorkDir+nm);
  wb = xls.Workbooks(1);
  wsh = wb.Worksheets.Item(1);
  return 0;
}
            

WriteToCell(cel,s)
{
  wsh.Range(static_cast_to_string(cel)).Value = static_cast_to_string(s);
  return 0;
}


Setup()
{ 
  int bTime = false;   // false-показывать процент,
                       // true-показывать оставшееся время

  CMsg = CreateObject("ProgressBar");
  CMsg.Initial(0,100,1);
  CMsg.SetShowTime(static_cast_to_int(bTime));

  CFile = CreateObject("CurPrnFile"); // объект класса для работы с текущим файлом печати
  CFile.SetFlagBrowse(0);             // запрещаем просмотр

  CTabl = CreateObject("CurPrnTbl");  // объект класса для работы с текущей таблицей
  cr = CTabl.Count_Rcr();             // число рабочих записей в таблице   
  cfld = CTabl.Count_Fld();           // число полей в таблице
  cr_ins = CTabl.GetCountInsertRcr(); // число выделенных записей

  if ( cr_ins ) rec_work = cr_ins;    // число записей которые должны быть обработаны
  else          rec_work = cr;

  tmpi = rec_work + CntHead + CntFoot;

  if ( tmpi > CntStr * 2 )
    vp = 2;

  if ( tmpi > CntStr && tmpi <= CntStr * 2 )
    vp = 1;

  if ( tmpi <= CntStr || rec_work <= 1 )
    vp = 0;

  return 0;
}
Date_()
{
  char s[256];        

  char arg[40], str[40];
  var Mes = CreateObject("KMonth");
  Mes.SetMonth(mtek);
  int iGod = Mes.GetYear();
  int iMes = Mes.GetMonth();

  sprintf(arg,"1,%d",mtek); R_DATA_EX(arg,str,20,0);  
  FreeSpaceString(str,3,"");
  string sMes = static_cast_to_string(str);  

  sprintf(str,"%s %d г.",sMes,iGod); FreeSpaceString(str,3,"");
    
  WriteToCell("K4",OemToAnsi(str));
  
  return 0;
}


 