// Скрипт печатает суммы по чеку в табло покупателя

const ConnectType = 2; //0 - с использованием прямой записи в COM-порт,
                       //1 - c использованием драйвера Феликс
                       //2 - драйвер дисплея АТОЛ
      COMPORT = 'COM1'; // COM-порт
var
 RTLSummary :currency; // переменные определяются из программы. Здесь приведены для наглядности
 RTLPaySummary :currency;
 RTLDiscount :double;
 RTLDiscountSummary :double;
 RTLClientName :string;
 RTLDOC_REASON :string;
 RTLDATASET :TDATASET;

var IDF :integer;
    ECR :Variant;
    strValue,strValue1 :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 RTLPaySummary < RTLSummary then RTLPaySummary := RTLSummary;

     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 := 'ИТОГО:';
     if connecttype = 0
     then strVAlue :=  conversionstring(StrValue,ctAnsi2OEM);


     StrValue1 :=  FormatFloat('########0.00', RTLSummary);
     len := length(strvalue1);

     strVAlue1 := stringofchar(' ', 20 - len - length(strvalue))+strvalue1;

     if (CONNECTType = 1)
     then begin
       ECR.CAption := StrVAlue + StrVAlue1;
       ECR.LineNumber := 1;
       ECR.AdvancedDisplayWrite();
     end
     else if (CONNECTType = 2)
     then begin
        ECR.DisplayTextAt(0,0,StrVAlue+StrVAlue1,0);
     end
     else begin
       FileWrite(IDF,StrValue);
       FileWrite(IDF,StrValue1);
       //FileWrite(IDF,#13+#10);
     end;

     Strvalue := 'СДАЧА:';
     if connecttype = 0
     then strVAlue :=  conversionstring(StrValue,ctAnsi2OEM);
     
     StrValue1 :=  FormatFloat('########0.00', RTLPAYSummary-RTLSummary);
     len := length(strvalue1);
     strVAlue1 := stringofchar(' ', 20 - len - length(strvalue))+strvalue1;

     if (CONNECTType = 1)
     then begin
       ECR.CAption := StrVAlue + StrVAlue1;
       ECR.LineNumber := 1;
       ECR.AdvancedDisplayWrite();
     end
     else if (CONNECTType = 2)
     then begin
        ECR.DisplayTextAt(1,0,StrValue+StrVAlue1,0);
     end
     else begin
       FileWrite(IDF,StrValue);
       FileWrite(IDF,StrValue1);
     end;

     //ShowMessage('ok');
     
   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.
