ОК, выкладываю.
Код: Выделить всё
#make "DisposalAct.frm"
#include GalNet.Inc
end;
/*table struct CntMove
(
  dmove :date,
  cmove : comp, 
  cpodr : comp,
  cmol  : comp
)
with index
(
  CntMove01 =  cpodr + cmol + dmove (unique)
)
; */
//type TCntMove = record as table CntMove;
 
Interface Create_For_Sip_Manual 'Галактическое безобразие' ('', hcNoContext, sci1Esc);
show at(,,50,5);
var
   dfrom, dto : date;
  // cntMBPIn : TCntMove;
   Marker : longint;
create view GetReq
var
  PodrFilter, MolFilter: string;
as select *
from
  MBPMove, Synonym MBPMove MBPMoveCreate,  Synonym MBPMove CntMove/* IsAUTO*/,
  MBPIn,
  MBPOUT, Synonym MBPOut MBPOutCreate,
  SoprHoz,
  MagOper,
  Pick,
  Pick PickPodr,
  Pick PickMol,
  KatPodr,
  KatMol
   
where
((
      word(0) == MBPin.extmod    
  and word(1) == MBPIn.status  (noindex) // введено в эксплуатацию
 // and word(4) == MBPIn.instate (noindex) // только по вводу в эксплуатацию
  and word(1) == MBPIn.inekspl (noindex)
  
  and dfrom <<= MBPIn.dateeksp (noindex) 
  and dto   >>= MBPIn.dateeksp (noindex) 
  and MBPin.cmbp == katmbp.nrec
  and 'AUTO' == CntMove.descr
  and MBPin.cpodr == CntMove.cpodrt (noindex)
  and MBPin.cmol  == CntMove.cmolt(noindex)
 // and date(0,0,0) == mbpin.datespis (noindex)
  and date(last_day(MBPIn.DATEEKSP), month(MBPIn.DATEEKSP), year(MBPIn.DATEEKSP)) == CntMove.DMOVE
  and PickPodr.cRec == KatPodr.nRec
  and PickMol.cRec == KatMol.nRec
))
bounds byPodr as MBPIn.cPodr /== PickPodr.cRec and word(11) == PickPodr.wList
bounds byMol as MbpIn.cMol /== PickMol.cRec and word(8) == PickMol.wList
condition  SIZ as ((substr(KatMBP.KodGr, 7, 2) = '01') or (substr(KatMBP.KodGr, 1, 9) = '120000003'))
;
#include tranfunc.vpp
Function InitFunction : boolean;
{
  InitFunction := false 
  //set dfrom :=  date(1,month(cur_date)-3, year(cur_date)) ;
  set dfrom := date(1,1,2007);
  //set dto := date(last_day(cur_date), month(cur_date), year (cur_date));
  set dto := date(30,4,2012);
  //set dto := date(31,10,2011);
  Marker := InitMarker('Marker' , 8, 100, 10);
  PushCondition (tcSIZ);
  rereadrecord;
  InitFunction := true;   
};
Function GetNum (tip : word) : string;
var
   chislo : integer;
   rec : comp; 
{
  case tip of  
  1: { rec:= comp(4001672163BFB58Fh);
       Update LastNumD  Where (rec=LastNumD.nrec) set LastNumD.LndNum:= Doubletostr(integer(LastNumD.LndNum) + 1, '777777');  // нельзя терять нули впереди
       GetNum:= LastNumD.LndNum;
  };
  end;    
} ;
Function CreateOUT(dend : date) : boolean;
var
  i : integer;
begin
  CreateOUT  := false;
  
  ClearBuffer (tnMBPOutCreate);
  MBPOutCreate.NOM      := GetNum (1);
  MBPOutCreate.STATUS   := 4;
  MBPOutCreate.DOUT     := dend;
  MBPOutCreate.TKOL     := MBPIn.KOLfact;
  MBPOutCreate.TPERCENT := MBPIn.TPERCENT;
  MBPOutCreate.TSUMMA   := MBPIn.SUMFACT;
  MBPOutCreate.TSUMMAISN:= MBPIn.SUMISNF;
  MBPOutCreate.KOL      := MBPIn.KOLfact;
  MBPOutCreate.PERCENT  := 0;
  MBPOutCreate.SUMMA    := MBPIn.SUMFACT;
  MBPOutCreate.SUMMAisn := MBPIn.SUMISNF;
  MBPOutCreate.CMOVE    := MBPMoveCreate.nrec;
  MBPOutCreate.CMBPIN   := MBPIn.nrec;
  MBPOutCreate.NOMSOPRD := MBPIn.NOMSOPRD;
  MBPOutCreate.nrec     := 0h;
!  if (Insert current MBPMoveCreate) = 0 then message('good') else message('bad');
!  if (Insert current MBPMoveCreate) <> 0 then exit;
  if (Insert current MBPOutCreate) <>0 then exit; 
  
  ClearBuffer(tnSoprHoz);     
  SoprHoz.DATOB       := dend;
  SoprHoz.CSOPRDOC    := MBPOutCreate.nrec;
  SoprHoz.TIPDOC      := word(19);
  SoprHoz.TIDKGAL     := word(19);
  SoprHoz.DESCR       := sGetTune('User.Descr');
  SoprHoz.DESGR       := sgetTune('USER.DESGR');
  SoprHoz.NameSHO  := 'Списание '+String(MBPOutCreate.Kol)+' ед. МБП по цене '+ String(MBPIn.Price)+
                   If(MBPOutCreate.tPercent>0.0001
                   , ' (ранее нач.износ:'+string(MBPOutCreate.tPercent)+'%)'
                   , ' износ не начислялся' );
  SoprHoz.SUMMA       := MBPOutCreate.kol*MBPIn.price;
  SoprHoz.DIRECT      := word(2);
  SoprHoz.VHSUMHOZ    := Chr('+');
!  if (insert current SoprHoz) = 0 then message('good') else message('bad');
  if (Insert current SoprHoz) <> 0 then exit;
  for (i:=0; i<2; Inc(i)){
      ClearBuffer(tnMagOper);
      MagOper.TIPDOC      := word(19);
      MagOper.CSOPRDOC    := MBPOutCreate.nrec;
      MagOper.DHOZOPER    := dend;
      MagOper.CPLANSSCH   := if (i=0,0000000000000005h,4001AD7D0FF15F6Dh);
      MagOper.dESCR       := sGetTune('User.Descr');
      MagOper.DESGR       := sgetTune('USER.DESGR');
      MagOper.CSOPRHOZ    := SoprHoz.nrec;
!      if (insert current MagOper) <>0 then message('good') else message('bad');
      if (insert current MagOper) <>0 then exit;                      
  }
  CreateOUT  := true;    
end;
Function UpdateMBPIn (dend : date): boolean;
begin
  UpdateMBPIn := false;
  
  MBPIn.KOLFACT  := 0;
  MBPIn.SUMFACT  := 0;
  MBPIn.STATUS   := 0;
  MBPIn.DATESPIS := dend;
  MBPIn.DATEZERO := dend;
  if (UpDate current MBPIN) <> 0 exit;
  
  UpdateMBPIn := true;
end;
Function CreateMove(dend : date) : boolean;
begin
  CreateMove := false;
  ClearBuffer(tnMBPMoveCreate);
  MBPMoveCreate.NOM    := GetNum(1);
  MBPMoveCreate.DMOVE  := dend;
  MBPMoveCreate.CPODRF := MBPIn.CPODRF; 
  MBPMoveCreate.CMOLF  := MBPIn.CMOLF;
  MBPMoveCreate.CPODRt := MBPIn.CPODR;
  MBPMoveCreate.CMOLt  := MBPIn.CMOL;
  MBPMoveCreate.STATUS := 5;
  MBPMoveCreate.SUMMA  := MBPIn.SUMFACT;
  MBPMoveCreate.SUMMAISN:= MBPIn.SUMISNF;
  MBPMoveCreate.CVALOLD:= to_days (dend);
  MBPMoveCreate.DESCR  := 'ADMIN';
  if (Insert current MBPMoveCreate) <> 0 then {exit};
  
  if not CreateOUT (dend) then {/*MESSAGE('not CreateOUT '+KatMBP.nrec + '  '+katmbp.name);*/exit};
  if not UpDateMBPIn (dend) then {exit};
  CreateMove := true;                               
end;
Function UpdateMove(rec : comp; dend : date) : boolean;
begin
  UpdateMove := false;
  if getfirst MBPMoveCreate where ((rec == MBPMoveCreate.nrec)) <> 0 then exit;
  MBPMoveCreate.SUMMA  := MBPMoveCreate.SUMMA + MBPIn.SUMMA;
  MBPMoveCreate.SUMMAISN:= MBPMoveCreate.SUMMAISN + MBPIn.SUMMAISNN;
  MBPMoveCreate.DESCR  := 'ADMIN';
  if (Update Current MBPMoveCreate) <> 0 then exit;
    
  if not CreateOUT(dend) then exit;
  if not UpDateMBPIn(dend) then exit;
  
  UpdateMove := true; 
end;
              
Window winMBPin;
show at (, , ,);
Browse brMBPin;
show at (,,,20);
table MBPIN
fields
   MBPIN.nom :[6], protect;
   MBPIN.din : [6],  protect;
   KatMBP.name: [10], protect;
   KatMBP.KodGr : [10], protect;
   if (MBPIn.status=1, 'в эксплуатации', 'серенькая') :[15], protect;
   
end;
Screen ScrMBPIN;
show at (,21,,);
buttons
cmOk;
<<
  <. Окей .>
>>
end;
HandleEvent  
cmOk : {
  var i : integer;
  var HasSuchMove : boolean;
  PushCondition (tcSIZ);
   
  BegTrans;
  StartNewVisual( vtRotateVisual, vfTimer + vfBreak + vfConfirm, '',1);
  SetVisualTitle('Подождите пожалуйста...');
  SetVisualHeader('Производится формирование актов списания...');
  _Loop MBPIn
  {
       SetVisualHeader('проверяется карточка '+MBPin.nom + ' от '+datetostr(MBPIn.din, 'DD.MM.YY'));
       if not nextVisual then {
          StopVisual ('Прервано пользователем ',1);
          break;
       };
      if (getfirst CntMove = 0 )
      then      
      { 
         if not UpdateMove(cntMove.nrec,  CntMove.dmove)
         {
            message('Обновление существующего акта списания прошло неудачно.'#13 + 'Осуществляю откат изменений ... ');
            AbortTrans;
            //StopVisual ('',0);
            break;//exit;
         };
      }
      else
      {    
         if not CreateMove (date(last_day(MBPIn.DATEEKSP), month(MBPIn.DATEEKSP), year(MBPIn.DATEEKSP)))
         {  
            message('Вставка акта списания прошлa неудачно.'#13 + 'Осуществляю откат изменений ... ');
            AbortTrans;
           // StopVisual ('',0);
            break;//exit;
         }
      };
  }
  StopVisual ('',0);
  DoneTrans;
  PopCondition (tcSIZ);
}
end;//handleevent
end;//window
Screen scEnterParams 'Введите параметры' ('', hcNoContext, sci1Esc);
Fields
  dFrom: [10], noprotect;
  dTo: [10], noprotect;
  PodrFilter (,,sci13Esc): protect, PickButton;
  MolFilter (,,sci13Esc): protect, PickButton;
Buttons
  cmOk;
  cmCancel;
<<
   ` Период с ` .@@@@@@@@@ ` по ` .@@@@@@@@@
   ` Подразделение ` .@@@@@@@@@@@@@@@@@@@@@@
   ` МОЛ `           .@@@@@@@@@@@@@@@@@@@@@@
          <. ОК .>         <. Отмена .>
>>
End;
form Report('DisposalAct.out', 'Disposal_Act');
#include function_date.inc 
HandleEvent
cminit:
{ 
  if not InitFunction then Abort;
}
cmPick:{
  case CurField of
  #PodrFilter: if RunInterface(GetAnyPodr, 0h, 14) = cmDefault then {
    if BoundActive(tbByPodr) then
      SubBounds(tbByPodr);
    var Cnt: Integer;
    Cnt := 0;
    GetReq._loop Pick where ((word(11) == Pick.wList)) { Inc(Cnt); }
    case Cnt of
    0: Set PodrFilter := '';
    1: Set PodrFilter := KatPodr.Name;
    else
      Set PodrFilter := 'Выбрано ' + Cnt + ' подразделений';
    end;
    if PodrFilter <> '' then
      if not BoundActive(tbByPodr) then
        AddBounds(tbByPodr);
  }
  #MolFilter: if RunInterface(GetSomeMol) = cmDefault then {
    if BoundActive(tbByMol) then
      SubBounds(tbByMol);
    _loop Pick where ((word(8) == Pick.wList)) {}
    case RowCount of
    0: Set MolFilter := '';
    1: Set MolFilter := KatMol.Name;
    else
      Set MolFilter := 'Выбрано ' + RowCount + ' лиц';
    end;
    if MolFilter <> '' then
      if not BoundActive(tbByMol) then
        AddBounds(tbByMol);
  }
  end;
}
cmOk:{
    //RunWindowModal('winMBPin');
//---------------------------------------------------------------------------------------------------------------
  //if not (cur_date = last_workday_in_month()) then {abort; exit;}
  if (integer(dFrom) = 0) or (integer(dTo) = 0) or (dFrom > dTo) then {
    Message('Неверно задан период!', Error + CancelButton);
    Abort;
    Exit;
  }
  Var    FileName , LogString: string;
//  FileName :=  '\\ptc-adem\programmers\Solonnikova\AUTOSFO.LOG';
//  LogString := '--------Запуск Интерфейса Create_For_Sip в ручном режиме-------';
//  LogStrToFile (FileName, LogString);
//  LogString := string(cur_date) + ', начало запуска: ' + TimeToStr(cur_time, 'HH:MM:SS');
//  LogStrToFile (FileName, LogString);
  var i : integer;
  var HasSuchMove : boolean;
  PushCondition (tcSIZ);
  BegTrans;
  StartNewVisual( vtRotateVisual, vfTimer + vfBreak + vfConfirm, '',1);
  SetVisualTitle('Подождите пожалуйста...');
  SetVisualHeader('Производится формирование актов списания...');
  Report.write(Cur_Date);
  Report.write(PodrFilter);
  Report.write(MolFilter);
  Report.write('СИЗ');
  _Loop MBPIn
  {
       SetVisualHeader('проверяется карточка '+MBPin.nom + ' от '+ DateToStr(MBPIn.din, 'DD.MM.YY'));
       if not nextVisual then {
          StopVisual ('Прервано пользователем ',1);
          break;
       };
      if (getfirst CntMove = 0 )
      then
      {
         if not UpdateMove(cntMove.nrec,  CntMove.dmove)
         {
           // message('Обновление существующего акта списания прошло неудачно.'#13 + 'Осуществляю откат изменений ... ');
            LogString := 'Обновление существующего акта списания прошло неудачно.'
//            LogStrToFile (FileName, LogString);
            SetVisualHeader(LogString);
            AbortTrans;
            //StopVisual ('',0);
            break;//exit;
         };
      }
      else
      {
         if not CreateMove (date(last_day(MBPIn.DATEEKSP), month(MBPIn.DATEEKSP), year(MBPIn.DATEEKSP)))
         {
           // message('Вставка акта списания прошлa неудачно.'#13 + 'Осуществляю откат изменений ... ');
            LogString := 'Вставка акта списания прошлa неудачно.'
//            LogStrToFile (FileName, LogString);
            SetVisualHeader(LogString);
//            AbortTrans;
           // StopVisual ('',0);
            break;//exit;
         }
      };
      Report.PutEvent(feDoLoop);
      Report.write(KatMbp.Name);
      Report.write(KatMbp.NNumber);
      Report.write(MbpOutCreate.Kol);
      Report.write(MbpIn.Price);
      Report.write(MbpOutCreate.Summa);
      Report.write(MbpIn.dIn);
      Report.write(MbpIn.Nom);
  }
//  LogString := string(cur_date) + ', конец  запуска: ' + TimeToStr(cur_time, 'HH:MM:SS');
//  LogStrToFile (FileName, LogString);
  StopVisual ('',0);
//  DoneTrans;
  PopCondition (tcSIZ);
   if (Marker <> 0) DoneMarker(Marker, '');
  if not Report.Error then
    Report.ShowFile('DisposalAct.out');
  else
    Report.AbortForm;
//---------------------------------------------------------------------------------------------------------------
}
 /*cmDone: 
 {    abort;
  exit;     
 };    */
end;
end.