// Ñêðèïò ïðîâåðÿåò öåíû çàêóïêè âî âñåõ ïåðâè÷íûõ êàðòî÷êàõ è âûñòàâëÿåò èõ ðàâíûìè ýòèì öåíàì (ïîëå cena)


var
  table :ttable;

  tapr_t,tars_t,tapr_n,tars_n :Ttable;

  ID :integer;
  ID1 :integer;

  pCycle :integer;

  recc :integer;

  cnt_changed :integer;

  tm :tdatetime;

  Field :TField;

begin

 tm := now;


 table := ttable.create(nil);
 with TMyWait.create('Èäåò ïðîâåðêà äàííûõ') do
 try

   tapr_t := ttable.create(table);
   tars_t := ttable.create(table);
   tapr_n := ttable.create(table);
   tars_n := ttable.create(table);


   try
     table.databasename := 'dbkassa';
     table.tablename := 'pr_tovar';
     table.open;

     begin


       tapr_t.close;
       tapr_t.databasename := table.databasename;
       tapr_t.tablename := 'pr_tovar.db';
       tapr_t.open;

       tapr_n.close;
       tapr_n.databasename := table.databasename;
       tapr_n.tablename := 'pr_nakl.db';
       tapr_n.open;


       tars_t.close;
       tars_t.databasename := table.databasename;
       tars_t.tablename := 'rs_tovar.db';
       tars_t.open;

       tars_n.close;
       tars_n.databasename := table.databasename;
       tars_N.tablename := 'rs_nakl.db';
       tars_n.open;

       progressbarmax := table.recordcount;

       cnt_changed := 0;


       //Query.disablecontrols;
       try
           table.first;
           while not table.eof do
           begin
             if
                tapr_t.findkey([table.fieldbyname('id').asinteger]) // ïðîñòî ñèíõðîíèçàöèÿ
                and tapr_n.findkey([table.fieldbyname('id_nakl').asinteger])
                and ((pos('Ï',tapr_n.fieldbyname('osnov').asstring)=1)
                     or (pos('Â',tapr_n.fieldbyname('osnov').asstring)=1))
             then begin
                 // ïåðâè÷íàÿ êàðòî÷êà

                 pcycle := 0;
                 while true do
                 begin
                    pcycle := pcycle + 1;
                    if pcycle >1000 then break;

                    if  tapr_n.findkey([tapr_t.fieldbyname('id_nakl').asinteger])
                       and ((pos('Ï', tapr_n.fieldbyname('osnov').asstring)=1)
                            or (pos('Â', tapr_n.fieldbyname('osnov').asstring)=1)
                           )
                       and tars_t.findkey([tapr_t.fieldbyname('id_rs').asinteger])
                       and tapr_t.findkey([tars_t.fieldbyname('id_pr').asinteger])
                    then id := tapr_t.fieldbyname('id').asinteger
                    else break;
                 end;

                 if (tapr_t.fieldbyname('id').asinteger <> table.fieldbyname('id').asinteger)
                    and (tapr_t.fieldbyname('cena').asfloat<> table.fieldbyname('cena').asfloat)
                 then begin
                   table.edit;
                   table.fieldbyname('cena').asfloat := tapr_t.fieldbyname('cena').asfloat;
                   table.post;
                   cnt_changed := cnt_changed + 1;
                   advancedtext := 'Èçìåíåíî ïîçèöèé: '+inttostr(cnt_changed);
                 end;
             end;
             table.next;
             incprogress;

           end;
       finally
         //Query.enablecontrols;
       end;

       //CreateHint('Ïðîéäåíî');
       application.processmessages;
     end;

     table.close;


     CreateHIntI('Îïåðàöèÿ  çàâåðøåíà çà '+timetostr(now-tm)+#13+'Èçìåíåíî ïîçèöèé: '+inttostr(cnt_changed),'',0);

   except
     bvMessageError(ExceptionMessage);
   end

 finally
   free;
   table.free;
 end

end.
