
// Ñêðèïò ýêñïîðòà ñïèñêà íàêëàäíûõ â DBF-ôàéëû. Ñêðèïò òðåáóåò íàëè÷èÿ âåðñèè ïðîãðàììû 5.8.0.8
// Ñêðèïò èñïîëüçóåòñÿ ôîðìîé "Îò÷åòû/Ýêñïîðò â 1Ñ ñïèñêîâ íàêëàäíûõ". Ïðîïèñûâàåòñÿ â ïàðàìåòðàõ
// (âûçûâàåìûõ ïî êíîïêå "Ïàðàìåòðû" â ñàìîé ôîðìå ýêñïîðòà), â ïàðàìåòðå "âûïîëíèòü ñêðèïò". Áîëüøå íèêàêèå
// íàñòðîéêè â ïàðàìåòðàõ ýêñïîðòà íå íóæíû.

// Â ñàìîé ôîðìå íóæíî âûáðàòü ïåðèîä íàêëàäíûõ, òèïû íàêëàäíûõ (ïðèõîäû è ðàñõîäû), ñêëàäû, îñíîâàíèÿ íàêëàäíûõ,
// êîòîðûå ó÷àñòâóþò â îò÷åòå.


const ExportDir = 'c:\temp\1C\'; // êàòàëîã ýêñïîðòà ôàéëîâ
      CODELPU = '123'; // Êîä ËÏÓ äëÿ äàííîé îðãàíèçàöèè


// Íàêëàäíàÿ âûâîäèòñÿ ñ ïîëíîé äåòàëèçàöèåé, åñëè NeedDetailInvoices = true
//const NeedDetailInvoices = true;



var // ïðåäîïðåäåëåííûå ïðîãðàììîé ïåðåìåííûå

  DATASET :TTable; // òàáëèöà ñ øàïêàìè íàêëàäíûõ ,êîòîðóþ ñîáèðàåò âûçûâàþùàÿ ôîðìà. Èç íåå ôîðìèðóåì âûõîäíîé ôàéë
  MinDate :tDateTime; // Ïåðâàÿ èç äàò â äèàïàçîíå
  MaxDate :tDateTime; // Âòîðàÿ (ìàêñèìàëüíàÿ) äàòà â äèàïàçîíå
  CheckOutput :boolean; // True, åñëè çàïðîñ îñóùåñòâëÿëñÿ ïî ðàñõîäíûì äîêóìåíòàì
  CheckInput  :boolean; // True, åñëè çàïðîñ îñóùåñòâëÿëñÿ ïî ïðèõîäíûì äîêóìåíòàì
  CheckInputMoney :boolean; // True, åñëè çàïðîñ îñóùåñòâëÿëñÿ ïî âûïëàòàì îò ïîêóïàòåëåé


var
  TaPR :TTable;
  TaRS :TTAble;
  TAPRNakl :TTAble;
  TAAgents :TTAble;
  TaProducer :TTAble;
  TaCertif :TTable;
  TaCountry :TTAble;
  SaveDialog :TSaveDialog;
  FileName :string;
  taprtovar :ttable;


  QueryPR,QueryRS :tQuery;
begin


  QueryPR := TQuery.create(nil);
  with TMyWait.create('Èäåò ýêñïîðò íàêëàäíûõ') do
  try
      QueryRS := TQuery.create(QueryPR);
      TAPR := TTable.create(QueryPR);
      taRS := TtAble.create(QueryPR);
      taPRNakl := TTable.create(TAPR);
      Taagents := ttable.create(tapr);
      TaProducer := TTAble.create(tapr);
      taCertif := TTable.create(tapr);
      TaCountry := TTable.create(tapr);
      taprtovar := ttable.create(tapr);


      taprnakl.databasename := 'dbkassa';
      taprnakl.tablename := 'pr_nakl';
      taprnakl.open;

      taprtovar.databasename := 'dbkassa';
      taprtovar.tablename := 'pr_tovar';
      taprtovar.open;

      taagents.databasename := 'dbkassa';
      taagents.tablename := 'agents';
      taagents.open;

      TaProducer.databasename := 'dbkassa';
      taProducer.tablename := 'producer';
      TaProducer.open;

      TaCertif.databasename := 'dbkassa';
      TaCertif.tablename := 'sertif';
      TaCertif.Open;

      TACountry.databasename := 'dbkassa';
      TaCountry.tablename := 'country';
      taCountry.open;




      QueryPR.databasename := 'dbkassa';
      QueryRS.databasename := 'dbkassa';
      //TApr.tablename := includetrailingpathdelimiter(ExportDir)+';
      ForceDirectories(ExportDir);
      tars.TableName := includetrailingpathdelimiter(ExportDir)+'otp.dbf';
      if fileexists(tars.tablename) then deletefile(tars.tablename);
      tapr.TableName := includetrailingpathdelimiter(ExportDir)+'post_'+CODELPU+'.dbf';
      if fileexists(tapr.tablename) then deletefile(tapr.tablename);


      with tapr do
      begin
        with fielddefs do begin
          clear;
          add('id_prix',ftstring,25,false);
          add('dat_prixod',ftdate,0,false);
          add('n_nakl',ftstring,25,false);
          add('VD',ftstring,50,false);
          add('post',ftstring, 250,false);
          add('inn_p',ftstring,12,false);
          add('kodlstmn',ftstring,20,false);
          add('namelstmn',ftstring,250,false);
          add('ei',ftstring,10,false);
          add('proiz',ftstring,250,false);
          add('land',ftstring,50,false);
          add('kol_prih',ftfloat,0,false);
          add('sum_prix',ftfloat,0,false);
          add('sert',ftstring,50,false);
          add('idnum',ftstring,50,false);
          add('date_ends',ftdate,0,false);
          add('date_endm',ftdate,0,false);
          add('ser',ftstring,30,false);
        end;

        createtable;
        Open;
      end;

      with tars do
      begin
        with fielddefs do begin
          clear;
          add('id_perem',ftstring,25,false);
          add('dat_perem',ftdate,0,false);
          add('n_nakl',ftstring,25,false);
          add('otdel',ftstring,250,false);
          add('mol',ftstring,50,false);
          //add('post',ftstring, 250,false);
          add('kodlstmn',ftstring,20,false);
          add('namelstmn',ftstring,250,false);
          add('proiz',ftstring,250,false);
          add('land',ftstring,50,false);
          add('ei',ftstring,10,false);
          add('id_prix',ftstring,25,false);
          add('kol',ftfloat,0,false);
          add('sum',ftfloat,0,false);
          add('ser',ftstring,50,false);
          add('dat_prixod',ftdate,0,false);
          add('n_nakl1',ftstring,25,false);
          add('VD',ftstring,50,false);
          add('post',ftstring, 250,false);
          add('inn_p',ftstring,12,false);
        end;

        createtable;
        Open;
      end;


      QueryPR.sql.text := 'select a.id,a.nn,b.ed,b.name,a.kol,a.cena,a.rs_cena,a.nds,a.ndsout'+
                          ',a.seria,a.sertif,a.srok,a.sroksertif,a.producer'+
                          ',a.idsertif,a.regn '+
                          ' from pr_tovar a join tovar b on a.nn = b.nn '+
                          ' where a.id_nakl  = :id_n';

      QueryRS.sql.text := 'select a.id,a.nn,b.ed,b.name,a.kol,a.cena,a.nds,a.id_pr '+
                          //,pr.id as id_pr,pr.id_nakl as id_nakl_pr, pr.seria,pr.sertif,pr.sroksertif,pr.producer,pr.idsertif '+
                          ' from rs_tovar a join tovar b on a.nn = b.nn '+
                          //' left join  pr_tovar pr on a.id_pr = pr.id '+
                          ' where a.id_nakl  = :id_n';



      dataset.first;
      while not dataset.eof do
      begin
        if (dataset.fieldbyname('po').asstring = '+') and CheckInput
        then begin


            QueryPR.close;
            QueryPR.parambyname('id_n').asInteger := dataset.fieldbyname('id_nakl').asinteger;
            QueryPR.open;
            QueryPR.first;
            while not QueryPR.eof do begin

              tapr.append;

              tapr.fieldbyname('id_prix').asstring := dataset.fieldbyname('id_nakl').asstring;
              tapr.fieldbyname('dat_prixod').asdatetime := dataset.fieldbyname('data').asdatetime;
              tapr.fieldbyname('n_nakl').asstring := dataset.fieldbyname('nomer').asstring;

              //tapr.fieldbyname('VD').asstring :=  ÕÇ;

              tapr.fieldbyname('post').asstring := dataset.fieldbyname('agentname').asstring;

              if taagents.findkey([dataset.fieldbyname('agent').asinteger])
              then
                tapr.fieldbyname('inn_p').asstring :=  taagents.fieldbyname('inn').asstring;
              //tapr.fieldbyname('inn_p').asstring := vartostr ( BDEQueryVAlue('select inn from agents where id = :id', [dataset.fieldbyname('agent').asinteger]));


              tapr.fieldbyname('kodlstmn').asstring := queryPR.fieldbyname('nn').asstring;
              tapr.fieldbyname('namelstmn').asstring := queryPR.fieldbyname('name').asstring;
              tapr.fieldbyname('ei').asstring := queryPR.fieldbyname('ed').asstring;

              if taproducer.findkey([Querypr.fieldbyname('producer').asinteger])
              then begin
                  tapr.fieldbyname('proiz').asstring := taproducer.fieldbyname('name').asstring;
                  if tacountry.findkey([taproducer.fieldbyname('country').asstring])
                  then tapr.fieldbyname('land').asstring := tacountry.fieldbyname('name').asstring;
              end;

              //tapr.fieldbyname('proiz').asstring := queryPR.fieldbyname('name_producer').asstring;

              //tapr.fieldbyname('land').asstring :=  vartostr( BDEQueryVAlue('select b.name from producer a join country b on a.country = b.id '+
              //  ' where a.id = :id', [QueryPR.fieldbyname('producer').asinteger]));

              tapr.fieldbyname('kol_prih').asfloat := queryPR.fieldbyname('kol').asfloat;
              tapr.fieldbyname('sum_prix').asfloat := queryPR.fieldbyname('kol').asfloat * queryPR.fieldbyname('cena').asfloat;

              tapr.fieldbyname('sert').asstring := queryPR.fieldbyname('sertif').asstring;
              tapr.fieldbyname('idnum').asstring := queryPR.fieldbyname('regn').asstring;
              if QueryPR.fieldbyname('sroksertif').asdatetime <>0
              then
                tapr.fieldbyname('date_ends').asdatetime := queryPR.fieldbyname('sroksertif').asdatetime;

              if QueryPR.fieldbyname('srok').asdatetime <>0
              then
                tapr.fieldbyname('date_endm').asdatetime := queryPR.fieldbyname('srok').asdatetime;

              tapr.fieldbyname('ser').asstring := queryPR.fieldbyname('seria').asstring;

              if taprnakl.findkey([dataset.fieldbyname('id_nakl').asinteger])
              then begin
                if (taprnakl.fieldbyname('finance').asinteger<>0)
                    and taagents.findkey([taprnakl.fieldbyname('finance').asinteger])
                then
                  tapr.fieldbyname('vd').asstring := taagents.fieldbyname('name').asstring;
              end;


              tapr.post;

              QueryPR.next;
            end;
        end
        else
        if (dataset.fieldbyname('po').asstring = '-') and CheckOutput
        then begin

            Queryrs.close;
            Queryrs.parambyname('id_n').asInteger := dataset.fieldbyname('id_nakl').asinteger;
            Queryrs.open;
            Queryrs.first;
            while not Queryrs.eof do begin

              tars.append;

              tars.fieldbyname('id_perem').asstring := dataset.fieldbyname('id_nakl').asstring;
              tars.fieldbyname('dat_perem').asdatetime := dataset.fieldbyname('data').asdatetime;
              tars.fieldbyname('n_nakl').asstring := dataset.fieldbyname('nomer').asstring;

              //tapr.fieldbyname('VD').asstring :=  ÕÇ;

              tars.fieldbyname('otdel').asstring := dataset.fieldbyname('agentname').asstring;
              //tars.fieldbyname('agent').asstring := dataset.fieldbyname('agentname').asstring;
              //tapr.fieldbyname('inn_p').asstring :=  asstring( BDEQueryVAlue('select inn from agents where id = :id', dataset.fieldbyname('agent').asinteger));


              tars.fieldbyname('kodlstmn').asstring := queryrs.fieldbyname('nn').asstring;
              tars.fieldbyname('namelstmn').asstring := queryrs.fieldbyname('name').asstring;
              tars.fieldbyname('ei').asstring := queryrs.fieldbyname('ed').asstring;
              tars.fieldbyname('kol').asfloat := queryrs.fieldbyname('kol').asfloat;
              tars.fieldbyname('sum').asfloat := queryrs.fieldbyname('kol').asfloat * queryrs.fieldbyname('cena').asfloat;


              if
                   (queryrs.fieldbyname('id_pr').asinteger<>0)
                   and taprtovar.findkey([queryrs.fieldbyname('id_pr').asinteger])
                   and taprnakl.findkey([taprtovar.fieldbyname('id_nakl').asinteger])
              then begin

                tars.fieldbyname('id_prix').asstring :=  taprtovar.fieldbyname('id_nakl').asstring;

                tars.fieldbyname('dat_prixod').asdatetime := taprnakl.fieldbyname('data').asdatetime;
                tars.fieldbyname('n_nakl1').asstring := taprnakl.fieldbyname('nomer').asstring;
                if taagents.findkey([taprnakl.fieldbyname('agent').asinteger])
                then begin
                  tars.fieldbyname('post').asstring := taagents.fieldbyname('name').asstring;
                  tars.fieldbyname('inn_p').asstring := taagents.fieldbyname('inn').asstring;
                end;


                if (taprnakl.fieldbyname('finance').asinteger<>0)
                    and taagents.findkey([taprnakl.fieldbyname('finance').asinteger])
                then
                  tars.fieldbyname('vd').asstring := taagents.fieldbyname('name').asstring;


                //,pr.id as id_pr,pr.id_nakl as id_nakl_pr, pr.seria,pr.sertif,pr.sroksertif,pr.producer,pr.idsertif '+
                tars.fieldbyname('ser').asstring := taprtovar.fieldbyname('seria').asstring;

                //tars.fieldbyname('proiz').asstring := queryrs.fieldbyname('name_producer').asstring;
                if taproducer.findkey([taprtovar.fieldbyname('producer').asinteger])
                then begin
                    tars.fieldbyname('proiz').asstring := taproducer.fieldbyname('name').asstring;
                    if tacountry.findkey([taproducer.fieldbyname('country').asstring])
                    then tars.fieldbyname('land').asstring := tacountry.fieldbyname('name').asstring;
                end;

                //tars.fieldbyname('land').asstring :=  vartostr( BDEQueryVAlue('select b.name from producer a join country b on a.country = b.id '+
                //  ' where a.id = :id', [Queryrs.fieldbyname('producer').asinteger]));

                //add('VD',ftstring,50,false);


              end;


              tars.post;

              Queryrs.next;
            end;

        end
        else
        if (dataset.fieldbyname('po').asstring = '1') and CheckInputMoney
        then begin
          // do nothing çà íåíàäîáíîñòüþ
        end;


        dataset.next;
      end;

      TAPR.close;
      tars.close;

      CreateHintI('Îïåðàöèÿ ýêñïîðòà äàííûõ çàâåðøåíà');
  finally
    free;
    QueryPR.free;
  end;

end.

