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

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


const ExportDir = 'c:\temp\1C\'; // êàòàëîã ýêñïîðòà ôàéëîâ


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

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


var
  TaPR :TTable;
  TaRS :TTAble;
  //TAPRNakl :TTAble;
  //tarsNakl :TTAble;
  SaveDialog :TSaveDialog;
  FileName :string;

  QueryPR,QueryRS :tQuery;
begin


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

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

      taprnakl.databasename := 'dbkassa';
      taprnakl.tablename := 'rs_nakl';
      taprnakl.open;
      *)

      QueryPR.databasename := 'dbkassa';
      QueryRS.databasename := 'dbkassa';
      //TApr.tablename := includetrailingpathdelimiter(ExportDir)+';

      forcedirectories(exportdir);

      tars.TableName := includetrailingpathdelimiter(ExportDir)+'out_docs.dbf';
      if fileexists(tars.tablename) then deletefile(tars.tablename);
      tapr.TableName := includetrailingpathdelimiter(ExportDir)+'in_docs.dbf';
      if fileexists(tapr.tablename) then deletefile(tapr.tablename);


      with tapr do
      begin
        with fielddefs do begin
          clear;
          add('id',ftinteger,0,false);
          add('id_invoice',ftinteger,0,false);
          add('date_doc',ftdate,0,false);
          add('number_doc',ftinteger,0,false);
          add('org_id',ftinteger, 0,false);
          add('org_name',ftstring, 250,false);
          add('org_inn',ftstring,12,false);
          add('storage_id',ftinteger, 0,false);
          add('storage_nm',ftstring, 250,false);
          add('code',ftstring,20,false);
          add('name',ftstring,250,false);
          add('ed',ftstring,10,false);
          add('prod_id',ftinteger, 0,false);
          add('prod_name',ftstring,250,false);
          add('country_id',ftstring,3,false);
          add('country_nm',ftstring,250,false);
          add('quantity',ftfloat,0,false);
          add('price',ftfloat,0,false);
          add('price_out',ftfloat,0,false);
          add('nds',ftinteger, 0,false);
          add('nds_out',ftinteger, 0,false);
          add('certif',ftstring,50,false);
          add('regn',ftstring,50,false);
          add('seria',ftstring,50,false);
          add('date_cert',ftdate,0,false);
          add('date_dead',ftdate,0,false);
          add('date_d_s',ftdate,0,false);
        end;

        createtable;
        Open;
      end;

      with tars do
      begin
        with fielddefs do begin
          clear;
          add('id',ftinteger,0,false);
          add('id_invoice',ftinteger,0,false);
          add('date_doc',ftdate,0,false);
          add('number_doc',ftinteger,0,false);
          add('org_id',ftinteger, 0,false);
          add('org_name',ftstring, 250,false);
          add('org_inn',ftstring,12,false);
          add('storage_id',ftinteger, 0,false);
          add('storage_nm',ftstring, 250,false);
          add('id_in',ftinteger,0,false);
          add('code',ftstring,20,false);
          add('name',ftstring,250,false);
          add('ed',ftstring,10,false);
          add('quantity',ftfloat,0,false);
          add('price',ftfloat,0,false);
          add('nds',ftinteger, 0,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,c.name as name_producer,a.idsertif,d.name as name_sertif,a.regn,a.datesertif '+
                          ' from pr_tovar a join tovar b on a.nn = b.nn '+
                          ' left join producer c on c.id = a.producer '+
                          ' left join sertif d on d.id = a.idsertif '+
                          ' where a.id_nakl  = :id_n';

      QueryRS.sql.text := 'select a.*,b.ed,b.name' +
                          ' from rs_tovar a join tovar b on a.nn = b.nn '+
                          ' 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').asinteger := dataset.fieldbyname('id').asinteger;
              tapr.fieldbyname('id_invoice').asinteger := dataset.fieldbyname('id_nakl').asinteger;
              tapr.fieldbyname('date_doc').asdatetime := dataset.fieldbyname('data').asdatetime;
              tapr.fieldbyname('number_doc').asstring := dataset.fieldbyname('nomer').asstring;
              tapr.fieldbyname('org_id').asinteger := dataset.fieldbyname('agent').asinteger;
              tapr.fieldbyname('org_name').asstring := dataset.fieldbyname('agentname').asstring;
              tapr.fieldbyname('org_inn').asstring := vartostr ( BDEQueryVAlue('select inn from agents where id = :id', [dataset.fieldbyname('agent').asinteger]));
              tapr.fieldbyname('storage_id').asinteger := dataset.fieldbyname('sklad').asinteger;
              tapr.fieldbyname('storage_nm').asstring := dataset.fieldbyname('skladname').asstring;


              tapr.fieldbyname('code').asstring := queryPR.fieldbyname('nn').asstring;
              tapr.fieldbyname('name').asstring := queryPR.fieldbyname('name').asstring;
              tapr.fieldbyname('ed').asstring := queryPR.fieldbyname('ed').asstring;
              tapr.fieldbyname('prod_id').asinteger := queryPR.fieldbyname('producer').asinteger;
              tapr.fieldbyname('prod_name').asstring := queryPR.fieldbyname('name_producer').asstring;

              tapr.fieldbyname('country_id').asstring :=
                    vartostr( BDEQueryVAlue('select country from producer where id = :id', [QueryPR.fieldbyname('producer').asinteger]));


              tapr.fieldbyname('country_nm').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('Quantity').asfloat := queryPR.fieldbyname('kol').asfloat;
              tapr.fieldbyname('price').asfloat := queryPR.fieldbyname('cena').asfloat;
              tapr.fieldbyname('price_out').asfloat := queryPR.fieldbyname('rs_cena').asfloat;
              tapr.fieldbyname('nds').asinteger := queryPR.fieldbyname('nds').asinteger;
              tapr.fieldbyname('nds_out').asinteger := queryPR.fieldbyname('ndsout').asinteger;

              tapr.fieldbyname('certif').asstring := queryPR.fieldbyname('sertif').asstring;
              tapr.fieldbyname('regn').asstring := queryPR.fieldbyname('regn').asstring;

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

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

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

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


              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').asinteger := dataset.fieldbyname('id').asinteger;
              tars.fieldbyname('id_invoice').asinteger := dataset.fieldbyname('id_nakl').asinteger;
              tars.fieldbyname('date_doc').asdatetime := dataset.fieldbyname('data').asdatetime;
              tars.fieldbyname('number_doc').asstring := dataset.fieldbyname('nomer').asstring;
              tars.fieldbyname('org_id').asinteger := dataset.fieldbyname('agent').asinteger;
              tars.fieldbyname('org_name').asstring := dataset.fieldbyname('agentname').asstring;
              tars.fieldbyname('org_inn').asstring := vartostr ( BDEQueryVAlue('select inn from agents where id = :id', [dataset.fieldbyname('agent').asinteger]));
              tars.fieldbyname('storage_id').asinteger := dataset.fieldbyname('sklad').asinteger;
              tars.fieldbyname('storage_nm').asstring := dataset.fieldbyname('skladname').asstring;


              tars.fieldbyname('code').asstring := queryrs.fieldbyname('nn').asstring;
              tars.fieldbyname('name').asstring := queryrs.fieldbyname('name').asstring;
              tars.fieldbyname('ed').asstring := queryrs.fieldbyname('ed').asstring;

              tars.fieldbyname('Quantity').asfloat := queryrs.fieldbyname('kol').asfloat;
              tars.fieldbyname('price').asfloat := queryrs.fieldbyname('cena').asfloat;
              tars.fieldbyname('nds').asinteger := queryrs.fieldbyname('nds').asinteger;
              tars.fieldbyname('id_in').asinteger := Queryrs.fieldbyname('id_pr').asinteger;

              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.

