var pr_nakl,pr_tovar :ttable;

   cntdel,cntchan :integer;
begin

  pr_nakl := ttable.create(selfscript);
  pr_tovar := ttable.create(selfscript);
  with tmywait.create1('Èäåò ÷èñòêà',selfscript) do
  try
    pr_nakl.databasename := 'dbkassa';
    pr_tovar.databasename := 'dbkassa';
    pr_nakl.tablename := 'pr_nakl';
    pr_tovar.tablename := 'pr_tovar';
    pr_nakl.open;
    pr_tovar.open;

    cntdel := 0;
    cntchan := 0;

    pr_tovar.indexfieldnames := 'id_nakl';

    progressbarmax := pr_nakl.recordcount;
    progressbarpos := 0;
    pr_nakl.first;
    while not pr_nakl.eof do begin
       incprogress;
       application.processmessages;

       if pr_nakl.fieldbyname('data').asdatetime < encodedate(2009,07,01)
       then begin
         pr_tovar.cancelrange;
         pr_tovar.setrangestart;
         pr_tovar.fieldbyname('id_nakl').asinteger := pr_nakl.fieldbyname('id').asinteger;
         pr_tovar.setrangeend;
         pr_tovar.fieldbyname('id_nakl').asinteger := pr_nakl.fieldbyname('id').asinteger;
         pr_tovar.applyrange;

         pr_tovar.first;
         while not pr_tovar.eof do
         begin
           if pr_tovar.fieldbyname('ostat').asfloat<=0 then
           begin
             pr_tovar.next;
           end
           else begin
             if pr_tovar.fieldbyname('kol').asfloat > pr_tovar.fieldbyname('ostat').asfloat
             then begin
                pr_tovar.edit;
                pr_tovar.fieldbyname('kol').asfloat := pr_tovar.fieldbyname('kol').asfloat - pr_tovar.fieldbyname('ostat').asfloat;
                pr_tovar.fieldbyname('ostat').asfloat := 0;
                pr_tovar.post;
                cntchan := cntchan + 1;
                pr_tovar.next;
             end
             else begin
               pr_tovar.delete;
               cntdel := cntdel + 1;
             end

           end

         end;
       end;

       pr_nakl.next;
    end;

    createhinti('Óäàëåíî : '+inttostr(CntDel)+', îòðåäàêòèðîâàíî : '+inttostr(CntChan),'óïàêîâêà',0);
  finally
    free;
    pr_nakl.free;
  end;
end.
