// Ñêðèïò ïå÷àòàåò ñóììû ïî ÷åêó â òàáëî ïîêóïàòåëÿ

var
 RTLDATASET :TDATASET;


const ConnectType = 2; //0 - ñ èñïîëüçîâàíèåì ïðÿìîé çàïèñè â COM-ïîðò,
                       //1 - c èñïîëüçîâàíèåì äðàéâåðà Ôåëèêñ
                       //2 - äðàéâåð äèñïëåÿ ÀÒÎË
      COMPORT = 'COM1'; // COM-ïîðò


var IDF :integer;
     ECR :Variant;
    strValue,strValue1,strValue2,StrValue3 :string;
    len :integer;


procedure CreateWindow;  // Íå çíàþ, ÷òî îíà äåëàåò, íî òàê â äðàéâåðå. Áåç íåå íå ïåðåíîñÿòñÿ ñòðîêè
var
  i :Integer;
begin
    ECR.CreateWindow(0, 0, 1, ECR.DeviceColumns, 12, 20);
    for I := 1 to ECR.DeviceRows - 1 do
    begin
      ECR.AddViewPort(I, 0, 12, ECR.GetDeviceColumns(I));
    end;
    ECR.CurrentWindow := 0;
    ECR.DestroyWindow;
end;

begin

   if CONNECTType = 0
   then begin
     IDF := FileCreate(COMPORT);
     IF IDF <=0 then raiseException('Íå ìîãó îòêðûòü ôàéë.');
   end
   else if ConnectTYpe = 2
   then begin
     ECR := CreateOleObject('AddIn.Line45');
     ECR.ApplicationHandle := Application.Handle; //
   end
   else begin
     ECR := CreateOleObject('AddIn.FprnM45');
     ECR.ApplicationHandle := Application.Handle; //
   end;

   try

     if (CONNECTType = 1)
     then begin
       ECR.DeviceEnabled := true;
       if ECR.ResultCode <> 0 then Exit;
     end
     else if (CONNECTType = 2)
     then begin
       ECR.DeviceEnabled := true;
       if ECR.ResultCode <> 0 then Exit;
     end
     else begin
       SetCommState(IDF, 9600, 8, 0, 0);
       //SetCommState(ID :Integer; BaudRate :DWORD; ByteSize :Byte; Parity :Byte; StopBits :byte)
     end;


     if (CONNECTType = 1)
     then begin
       ECR.CAption := stringofchar(' ',20);
       ECR.LineNumber := 1;
       ECR.AdvancedDisplayWrite();
     end
     else if (CONNECTType = 2)
     then begin
       CreateWindow;
       ECR.ClearTExt;
     end
     else begin
        FileWrite(IDF,#27+'@'); // èíèöèàëèçàöèÿ
        FileWrite(IDF,#$0C);    // î÷èñòêà
     end;

     Strvalue := copy(RTLDATASET.Fieldbyname('Name').asstring ,1,20);
     
     if ConnectTYPE = 0
     then 
       Strvalue := conversionstring(StrValue, ctAnsi2OEM);
     
     //FileWrite(IDF,StrValue);
     //FileWrite(IDF,StrValue1);

     StrValue1 :=   FormatFloat('#0.###',RTLDATASET.Fieldbyname('kol').asFloat ) +
                    'x'+FormatFloat('#0.00',RTLDATASET.Fieldbyname('prodCena').asfloat );

     STrValue2 :=   '='+FormatFloat('#0.00',RTLDATASET.Fieldbyname('kol').asFloat
                                * RTLDATASET.Fieldbyname('prodCena').asfloat );


     len := length(strvalue1);
     //strVAlue2 := stringofchar(' ', 20 - len - length(strvalue2))+strvalue2;



     //CreateHint(StrValue +#13+#10+ StrValue1 + StrValue2+'/'+inttostr(length(StrValue1+StrValue2)));


     if (CONNECTType = 1)
     then begin
       ECR.CAption := StrVAlue + StrVAlue1;
       ECR.LineNumber := 1;
       ECR.AdvancedDisplayWrite();
     end
     else if (CONNECTType = 2)
     then begin
        ECR.DisplayText(StrValue + StrVAlue1,0);
        //ShowMessage(StrValue + StrVAlue1);
     end
     else begin
       FileWrite(IDF,StrValue);
       FileWrite(IDF,StrValue1);
       //FileWrite(IDF,#13+#10);
     end;
     
     

   finally
      if (CONNECTType = 1) or (CONNECTType = 2)  then
      begin
        if ECR.ResultCode <> 0 then
          CreateHintE('Îøèáêà ÊÊÌ: ' + vartostr(ECR.ResultDescription));
      end
      else begin
        FileClose(IDF);
      end;
   end
end.
