unit NaklUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBTables, frxClass, frxADOComponents, frxDBSet,StdCtrls,
  frxDesgn;

type
  TNaklForm = class(TForm)
    frxDatasetPrCap: TfrxDBDataset;
    frxADOComponents1: TfrxADOComponents;
    frxDatasetPrStr: TfrxDBDataset;
    TrCheckBox: TCheckBox;
    forSumNDS_1: TCheckBox;
    PrCapQuery: TQuery;
    PrCapQueryIdDoc: TIntegerField;
    PrCapQueryIdPredpr: TSmallintField;
    PrCapQueryKodSkl: TSmallintField;
    PrCapQueryVidDoc: TSmallintField;
    PrCapQueryDateDoc: TDateTimeField;
    PrCapQueryNumDoc: TIntegerField;
    PrCapQueryNameGrOper: TStringField;
    PrCapQueryUNNPlatPredpr: TStringField;
    PrCapQueryOKPOPlatPredpr: TStringField;
    PrCapQueryUNNPlatSkl: TStringField;
    PrCapQueryOKPOPlatSkl: TStringField;
    PrCapQueryLICPlatPred: TStringField;
    PrCapQueryLICPlatSkl: TStringField;
    PrCapQueryNamePredpr: TStringField;
    PrCapQueryNameSkl: TStringField;
    PrCapQueryStHoFlSkl: TStringField;
    PrCapQueryNamePlatPredpr: TStringField;
    PrCapQueryRschetPlatPredpr: TStringField;
    PrCapQueryKodMFOPlatPredpr: TStringField;
    PrCapQueryBankPlatPredpr: TStringField;
    PrCapQueryTownBankPredpr: TStringField;
    PrCapQueryRSchetPlatSkl: TStringField;
    PrCapQueryKodMFOPlatSkl: TStringField;
    PrCapQueryBankPlatSkl: TStringField;
    PrCapQueryTownBankSkl: TStringField;
    PrCapQueryTownPredpr: TStringField;
    PrCapQueryStHoFlPredpr: TStringField;
    PrCapQueryTownPlatPredpr: TStringField;
    PrCapQueryStHoFlPlatPredpr: TStringField;
    PrCapQueryPhonePlatPredpr: TStringField;
    PrCapQueryFaxPlatPredpr: TStringField;
    PrCapQueryNamePlatSkl: TStringField;
    PrCapQueryFIODir: TStringField;
    PrCapQueryTownPlatSkl: TStringField;
    PrCapQueryStHoFlPlatSkl: TStringField;
    PrCapQueryPhonePlatSkl: TStringField;
    PrCapQueryFaxPlatSkl: TStringField;
    PrCapQueryDogovor: TStringField;
    PrCapQueryCelPriobr: TStringField;
    PrCapQueryFioMol: TStringField;
    PrCapQueryProfMol: TStringField;
    PrCapQueryDover: TStringField;
    PrCapQueryPutList: TStringField;
    PrCapQueryVoditel: TStringField;
    PrCapQueryMarkAvto: TStringField;
    PrCapQueryOwnAvto: TStringField;
    PrCapQueryNomAvto: TStringField;
    PrCapQuerySumNDS: TBCDField;
    PrCapQuerySumIncNDS: TBCDField;
    PrCapQuerySumNac: TBCDField;
    PrCapQuerySumNacOpt: TBCDField;
    PrCapQuerySumTrn: TBCDField;
    PrCapQuerySumSkid: TBCDField;
    PrCapQueryItogo: TBCDField;
    PrCapQueryItogoTov: TBCDField;
    PrCapQueryItogoTar: TBCDField;
    PrCapQueryItogoRoz: TBCDField;
    PrCapQueryItogoOO: TBCDField;
    PrCapQueryItogoKolF: TBCDField;
    PrCapQueryItogoKolM: TBCDField;
    PrCapQueryItogoMas: TBCDField;
    PrCapQueryItogoKolKzp: TBCDField;
    PrCapQueryPrSumNds: TStringField;
    PrCapQueryPrVsego: TStringField;
    PrCapQueryItogoKolTov: TBCDField;
    PrCapQuerySert: TStringField;
    PrCapQueryItogoNaim: TSmallintField;
    PrCapQueryItogoTovR: TBCDField;
    PrCapQueryItogoTarR: TBCDField;
    PrCapQuerySumOpl: TBCDField;
    PrCapQueryFIOGlBuh: TStringField;
    PrCapQueryPoluchat: TStringField;
    PrCapQueryIndexSkl: TStringField;
    PrCapQueryIndexPlatSkl: TStringField;
    PrCapQueryIndexPredpr: TStringField;
    PrCapQueryIndexPlatPredpr: TStringField;
    PrCapQueryTownSkl: TStringField;
    PrCapQueryTovar: TBCDField;
    PrCapQueryStTara: TBCDField;
    PrCapQueryTara: TBCDField;
    PrCapQuerySumNDSRozn: TBCDField;
    PrCapQuerySumSkidPokup: TBCDField;
    PrStrQuery: TQuery;
    PrStrQueryNameVidTov: TStringField;
    PrStrQueryKodGr: TSmallintField;
    PrStrQueryKodPGr: TSmallintField;
    PrStrQueryMinUp: TBCDField;
    PrStrQueryVidUp: TStringField;
    PrStrQueryArt: TStringField;
    PrStrQueryModel: TStringField;
    PrStrQuerySizes: TStringField;
    PrStrQueryHight: TStringField;
    PrStrQuerySort: TStringField;
    PrStrQuerySertif: TStringField;
    PrStrQueryFasov: TBCDField;
    PrStrQueryEdIz: TStringField;
    PrStrQueryCenO: TBCDField;
    PrStrQueryCenRoz: TBCDField;
    PrStrQueryCenSt: TBCDField;
    PrStrQueryNDS: TBCDField;
    PrStrQueryNacOpt: TBCDField;
    PrStrQueryNacRozn: TBCDField;
    PrStrQueryNacObpit: TBCDField;
    PrStrQuerySkidPokup: TBCDField;
    PrStrQueryNacPosr: TBCDField;
    PrStrQuerySkidPost: TBCDField;
    PrStrQueryNalTr: TBCDField;
    PrStrQueryNalProd: TBCDField;
    PrStrQueryKolF: TBCDField;
    PrStrQueryKolm: TSmallintField;
    PrStrQuerySumNDS: TBCDField;
    PrStrQuerySumNDSR: TBCDField;
    PrStrQueryPrRazl: TStringField;
    PrStrQueryCenPor: TBCDField;
    PrStrQueryCenPriobr: TBCDField;
    PrStrQueryNamePredpr: TStringField;
    PrStrQueryNameCountry: TStringField;
    PrStrQueryKolPor: TBCDField;
    PrStrQueryProtkl: TStringField;
    PrStrQuerySertif1: TStringField;
    PrStrQuerySertif2: TStringField;
    forVsegoSNDS_1: TCheckBox;
    forVesTrEdit: TEdit;
    forTTNEdit: TEdit;
    forNumDoc: TEdit;
    forSeria: TEdit;
    forPogr: TEdit;
    forRazg: TEdit;
    forVidPer: TEdit;
    forPrim: TEdit;
    forStoimost: TCheckBox;
    forStoimost2_1: TCheckBox;
    forStoimost2_2: TCheckBox;
    forCheckNastrGrTov: TCheckBox;
    forPr_CenPriobr: TCheckBox;
    forPr_CenTr: TCheckBox;
    forPr_ProcTr: TCheckBox;
    forPr_CenWNal: TCheckBox;
    forPRAll: TEdit;
    forIdDoc: TEdit;
    PrStrQueryIdDoc: TIntegerField;
    PrStrQueryVes: TBCDField;
    PrStrQueryIdPrizLog: TSmallintField;
    forOtpRaz: TEdit;
    forReturnTypeTrPredpr: TCheckBox;
    PrCapQueryDateOtgr: TDateTimeField;
    PrCapQueryOtpRaz: TStringField;
    PrCapQueryNdsBns: TBCDField;
    PrCapQuerySumBns: TBCDField;
    PrCapQuerySumNdsBonus: TBCDField;
    SumBnsQuery: TQuery;
    frxDBDataset1: TfrxDBDataset;
    forFace3: TCheckBox;
    PrCapQueryNameF3Predpr: TStringField;
    PrCapQueryRschetF3Predpr: TStringField;
    PrCapQueryKodMFOF3Predpr: TStringField;
    PrCapQueryBankF3Predpr: TStringField;
    PrCapQueryTownF3Predpr: TStringField;
    PrCapQueryTownBankF3: TStringField;
    PrCapQueryUNNF3Predpr: TStringField;
    PrCapQueryOKPOF3Predpr: TStringField;
    PrCapQueryLICF3Pred: TStringField;
    PrCapQueryIndexF3Predpr: TStringField;
    PrCapQueryStHoFlF3Predpr: TStringField;
    PrCapQueryIdF3: TSmallintField;
    forDateDov: TEdit;
    forVlTr: TEdit;
    PrStrQueryPNT: TIntegerField;
    PrStrQuerypriznsyria: TStringField;
    PrStrQueryUsl: TBCDField;
    forSposPogr: TEdit;
    forSposRazgr: TEdit;
    forVrPrib: TEdit;
    forVrUb: TEdit;
    forVrProst: TEdit;
    forMOLGruzPoluch: TEdit;
    forNomPric: TEdit;
    PrStrQueryKolKzp: TBCDField;
    PrStrQueryEdIzDop: TStringField;
    PrStrQuerySkidOv: TBCDField;
    PrStrQueryKolOv: TBCDField;
    PrStrQueryKolZ: TBCDField;
    mdb: TDatabase;
    PrStrQueryPNTTar1: TIntegerField;
    PrStrQueryPNTTar3: TIntegerField;
    frxDesigner1: TfrxDesigner;
    NameGrPrQuery: TQuery;
    PrCapQueryProfMol1: TStringField;
    PrStrQueryprLogBKS: TSmallintField;
    PrCapQuerySumNacRozn: TBCDField;
    PrCapQuerySumNacObpit: TBCDField;
    PrCapQueryTovarOO: TBCDField;
    FioDovQuery: TQuery;
    FioDovQueryFio: TStringField;
    PrCapQueryOblPlat: TStringField;
    PrCapQueryOblPlatSkl: TStringField;
    PrCapQueryRaionPlat: TStringField;
    PrCapQueryRaionPlatSkl: TStringField;
    frxReport1: TfrxReport;
    frxReport2: TfrxReport;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    function Propis(S:Currency;KK:integer):string;
    function Num(Q:string;X:integer):string;
    function PROPER(S: string): string;
    procedure Run();

  private
    NalProdEd: string;
    { Private declarations }
  public
      { Public declarations }
     function Rounding(Value:Currency):Currency; // Округление до сотых Суховаров А.А.
     function kopeiki(Value:Currency):String;
  end;

var
  NaklForm: TNaklForm;
  {PRAll,}PrSumNds,PrVsego,PrSmall,Exsp: String;
  //===4===//
  {Memo4_1, Memo4_3, Memo4_2, Memo4_4,} Cen4_1, Cen4_2 : TfrxMemoView;
  //===5===//
//  Memo5_1, Memo5_3, Memo5_2, Memo5_4 : TfrxMemoView;
  //===6===//
  Memo6_3, Memo6_1, Memo6_4, Memo6_2, Sum6 : TfrxMemoView;
  //=== For other===//
  NumberTTN, Seria, SeriaSecondPage, Pogr, Razgr, PriborMemo{VidPerevoz,}, SumNDS_1,
  {SumNDS_2,} VsegoSNDS_3, VsegoSNDS_1, VsegoSNDS_2, Mest, MassaBrutto,
  NumNakl, Memo389, Memo388, Memo1,
  Memo338, Itog, KolProd, Stoimost, Stoimost2, {Stoimost3,} DataDoc,
  SummNDS, VsSNDS, KolGrMest, MassBrutt, Zakazchik,
  Memo94, Memo93, Primechanie, UNN_1 : TfrxMemoView;
  //=== Дополнительно ===//
  NamePredpr, NalProd, Memo59, Memo88, Memo89, Memo92, Memo92_2, Memo32, Memo33, Memo149, Memo159,
  {Memo95,} Memo96, Memo300, Memo336, SumSumNDS, Memo287, Memo35 : TfrxMemoView;
  //=== Суммы ===//
  SumTov, SumTar, SumStTar, SumTovRozn, SumTarRozn, SumVsSNds : TfrxMemoView;
  Memo3, Memo4, Memo5, Memo6, Memo7, Memo118, Memo151, Memo161, Memo152, Memo153, Memo154, Memo291, Memo292, Memo8,
  Memo51, Memo34, Memo57 : TfrxMemoView;

  MasterData1 : TfrxMasterData;
  DetailData1 : TfrxDetailData;
  VssndsInt, i, IdCheckNastrGrTov : integer;
  S : Currency;
  VssndsFloat:Currency;
implementation

uses Math;

{$R *.dfm}

var
  DllApp : TApplication;

procedure TNaklForm.Run();
var
  StText, StText2, SumNdsText, VsSNdsText,qwert : String;
  kolstr,kolline,kolitog,i,a,z : integer;
  GrPr : integer;
begin
//NaklForm.Visible:=false;
//NaklForm.Hide;

  if forPRAll.Text='' then
  begin
    PrCapQuery.Active:=False;
    PrCapQuery.ParamByName('IdDoc').Value:=StrToInt(forIdDoc.Text); // SklDM.DocQuery.FieldByName('IdDoc').Value;
    PrCapQuery.Active:=True;
  end;
  with PrCapQuery do
  begin
    Close;
    Params.ParamByName('idDoc').Value := StrToInt(forIdDoc.Text);
    Open;
  end;
  with PrstrQuery do
  begin
    ParamByName('IdDoc').Value:=StrToInt(forIdDoc.Text); //SklDm.DocQuery.FieldByName('idDoc').Value;
    Open;
  end;

  NameGrPrQuery.Active:=False;
  NameGrPrQuery.ParamByName('IdDoc').Value:=forIdDoc.Text;
  NameGrPrQuery.Active:=True;
  if (NameGrPrQuery.FieldByName('NameGrPredpr').AsString)='райпо' then
    Grpr:=1
  else
    GrPr:=0;
  SumBnsQuery.Active:=False;
  SumBnsQuery.ParamByName('IdDoc').Value:=forIdDoc.Text;
  SumBnsQuery.Active:=True;
//===================//
//=== Вывод отчёта===//
//===================//

  frxReport2.Variables['forSumNDS_1']:=QuotedStr(BoolToStr(forSumNDS_1.Checked));

  frxReport1.PrepareReport(true);
  frxReport2.PrepareReport(true);

  frxReport1.Modified:=True;
  frxReport2.Modified:=True;

//=====================//
//=== 1-ая страница ===//
//=====================//

  Memo287 := frxReport1.FindObject('Memo287') as TfrxMemoView;
  Memo287.Text:=forOtpRaz.Text;

  Zakazchik := frxReport1.FindObject('Zakazchik') as TfrxMemoView;

  if TrCheckBox.Checked then
    frxReport1.Variables['IdTrCheck']:=QuotedStr('1')
  else
    frxReport1.Variables['IdTrCheck']:=QuotedStr('0');

  if forFace3.Checked then
    frxReport1.Variables['IdF3Check']:=QuotedStr('1')
  else
    frxReport1.Variables['IdF3Check']:=QuotedStr('0');

  Memo151 := frxReport1.FindObject('Memo151') as TfrxMemoView;
  Memo151.Text:=forSposPogr.Text;

  Memo161 := frxReport1.FindObject('Memo161') as TfrxMemoView;
  Memo161.Text:=forSposRazgr.Text;

  Memo152 := frxReport1.FindObject('Memo152') as TfrxMemoView;
  Memo152.Text:=forVrPrib.Text;

  Memo153 := frxReport1.FindObject('Memo153') as TfrxMemoView;
  Memo153.Text:=forVrUb.Text;

  Memo154 := frxReport1.FindObject('Memo154') as TfrxMemoView;
  Memo154.Text:=forVrProst.Text;

  Memo8 := frxReport1.FindObject('Memo8') as TfrxMemoView;
  Memo8.Text:=forNomPric.Text;

  Memo291 := frxReport1.FindObject('Memo291') as TfrxMemoView;
  if forMOLGruzPoluch.Text<>'' then
    Memo291.Text:='[frxDatasetPrCap."ProfMol1"]'+ forMOLGruzPoluch.Text
  else
    Memo291.Text:='[frxDatasetPrCap."Poluchat"]';

  Memo292 := frxReport1.FindObject('Memo292') as TfrxMemoView;
  Memo118 := frxReport1.FindObject('Memo118') as TfrxMemoView;
  if PrCapQuery.FieldByName('Dover').IsNull then
  begin
    Memo292.Text:='';
    Memo118.Text:='';
  end
  else
  begin
    with FioDovQuery do
    begin
      ParamByName('IdPredpr').Value:= PrCapQuery.FieldByName('idpredpr').Value;
      ExecSql;
      open;
    end;
    If FioDovquery.RecordCount=1 then
      Memo118.Text:=' от '+forDateDov.Text+'г. выданной '+PrCapQuery.FieldByName('NamePlatPredpr').AsString+'  '+FioDovQuery.FieldByName('Fio').AsString
    else
      Memo118.Text:=' от '+forDateDov.Text+'г. выданной '+PrCapQuery.FieldByName('NamePlatPredpr').AsString;
  end;

  if TrCheckBox.Checked then
  begin
    Zakazchik.Text:=PrCapQuery.FieldByName('NamePlatPredpr').Value+
    ', '+PrCapQuery.FieldByName('IndexPlatPredpr').Value+' '+PrCapQuery.FieldByName('TownPlatPredpr').Value +', '+ PrCapQuery.FieldByName('StHoFlPlatPredpr').Value+' ;';
  end
  else
  begin
    if forFace3.Checked then
    begin
      Zakazchik.Text:=PrCapQuery.FieldByName('NameF3Predpr').Value+
      ', '+PrCapQuery.FieldByName('IndexF3Predpr').Value+' '+PrCapQuery.FieldByName('TownF3Predpr').Value +', '+ PrCapQuery.FieldByName('StHoFlF3Predpr').Value+' ;';
    end
    else
    begin
      Zakazchik.Text:=PrCapQuery.FieldByName('NamePlatSkl').Value+
      ', '+PrCapQuery.FieldByName('IndexPlatSkl').Value+' '+PrCapQuery.FieldByName('TownPlatSkl').Value +',  ' + PrCapQuery.FieldByName('StHoFlPlatSkl').Value+' ;';
    end;
  end;

  if forFace3.Checked=False then
    Zakazchik.Text:='';

  SumNDS_1 := frxReport1.FindObject('SumNDS_1') as TfrxMemoView;

  if forSumNDS_1.Checked=False then
    SumNDS_1.Text:='[<frxDatasetPrCap."SumIncNDS">+<frxDatasetPrCap."SumNDSRozn">]'
  else
    SumNDS_1.Text:='[<frxDatasetPrCap."SumIncNDS">+<frxDatasetPrCap."SumNDS">]';

  SumNDS_1.GetData;
  SumNDS_1.Value:=SumNDS_1.Value+PrCapQuery.FieldByName('SumNdsBonus').Value;
  if SumNDS_1.Text<>'0' then
  begin
    S:=SumNDS_1.Value;
    SumNDS_1.Memo.Text:=PROPER(Propis(S,0))+'   '
    +Copy(SumNDS_1.Text, Pos(',', SumNDS_1.Text)+1, 2)+' коп.';
  end
  else
    SumNDS_1.Memo.Text:='';

//-------------------//
  VsegoSNDS_1 := frxReport1.FindObject('VsegoSNDS_1') as TfrxMemoView;
  VsegoSNDS_1.GetData;
  if VsegoSNDS_1.Memo.Text<>'0' then
  begin
    if forStoimost2_1.Checked=False then
      if forSumNDS_1.Checked=True then
        VsegoSNDS_1.Value:=PrCapQuery.FieldByName('Itogo').AsFloat+PrCapQuery.FieldByName('SumNDS').AsFloat+SumBnsQuery.FieldByName('sumbns').AsFloat
      else
        VsegoSNDS_1.Value:=PrCapQuery.FieldByName('ItogoRoz').AsFloat
    else
      if forSumNDS_1.Checked=True then
        VsegoSNDS_1.Value:=PrCapQuery.FieldByName('ItogoOO').AsFloat+PrCapQuery.FieldByName('SumNDS').AsFloat
      else
        VsegoSNDS_1.Value:=PrCapQuery.FieldByName('ItogoRoz').AsFloat;
    S := VsegoSNDS_1.Value;
    VsegoSNDS_1.Memo.Text:=PROPER(Propis(S,0))+'   '
    +Copy(FloatToStrF(S,ffNumber,12,2), Pos(',', FloatToStrF(S,ffNumber,12,2))+1, 2)+' коп.';
  end
  else
    VsegoSNDS_1.Memo.Text:='';

//-------------------//

  Mest := frxReport1.FindObject('Mest') as TfrxMemoView;
  Mest.GetData;
  if Mest.Memo.Text<>'0' then
  begin
    S:=Mest.Value;
    Mest.Memo.Text:=Propis(S,2);
  end
  else
    Mest.Memo.Text:='';

//-------------------//

  MassaBrutto := frxReport1.FindObject('MassaBrutto') as TfrxMemoView;
  MassaBrutto.GetData;
  If (forPRAll.Text<>' ') and (forVesTrEdit.Text<>'') then
    MassaBrutto.Memo.Text:=FloatToStr(StrToFloat(MassaBrutto.Memo.Text)+StrToFloat(forVesTrEdit.Text));

  if MassaBrutto.Memo.Text<>'0' then
  begin
    S:=MassaBrutto.Value;
    MassaBrutto.Memo.Text:=Propis(S,1);
  end
  else
    MassaBrutto.Memo.Text:='';

//-------------------//

  Memo338 := frxReport1.FindObject('Memo338') as TfrxMemoView;
  Memo338.GetData;
  if NalProdEd<>'' then
    Memo338.Memo.Text:=FloatToStr(Rounding(StrToFloat( NalProdEd )*PrStrQuery.FieldByName('KolF').Value));
  if Memo338.Memo.Text='0' then
    Memo338.Memo.Text:='';

//-------------------//

  UNN_1 := frxReport1.FindObject('UNN_1') as TfrxMemoView;
  if TrCheckBox.Checked then
    UNN_1.Text:=PrCapQuery.FieldByName('UNNPlatPredpr').Value
  else
  begin
    if forFace3.Checked then
      UNN_1.Text:=PrCapQuery.FieldByName('UNNF3Predpr').Value
    else
      UNN_1.Text:=PrCapQuery.FieldByName('UNNPlatSkl').Value;
  end;

  if forFace3.Checked=False then
    UNN_1.Text:='';

//=== Если введён номер ТТН===//
  NumberTTN := frxReport1.FindObject('NumberTTN') as TfrxMemoView;
  If forPRAll.Text<>' ' then
    NumberTTN.Text:=forTTNEdit.Text
  else
    NumberTTN.Text:=forNumDoc.Text;
//=== Если введёна серия===//
  Seria := frxReport1.FindObject('Seria') as TfrxMemoView;
  if forSeria.Text<>'' then
    Seria.Memo.Text:=forSeria.Text;
//=== Если введён пункт погрузки===//
  Pogr := frxReport1.FindObject('Pogr') as TfrxMemoView;
  if (forPogr.Text='') or (forPogr.Text=' ') or (forPogr.Text='  ') or (forPogr.Text='   ') then
    Pogr.Text:=PrCapQuery.FieldByName('TownSkl').Value+', '+PrCapQuery.FieldByName('StHoFlSkl').Value
  else
    Pogr.Memo.Text:=forPogr.Text;
//=== Если введён пункт разгрузки===//
  Razgr := frxReport1.FindObject('Razgr') as TfrxMemoView;
  if (forRazg.Text='') or (forRazg.Text=' ') or (forRazg.Text='  ') or (forRazg.Text='   ') then
    Razgr.Text:=PrCapQuery.FieldByName('TownPredpr').Value+', '+PrCapQuery.FieldByName('StHoFlPredpr').Value
  else
    Razgr.Memo.Text:=forRazg.Text;
//=== Если введён вид перевозки===//
  PriborMemo := frxReport1.FindObject('PriborMemo') as TfrxMemoView;
  if forVidPer.Text<>'' then
    PriborMemo.Memo.Text:='Подлинность акцизной марки (специального знака) проверено комиссионно прибором '+forVidPer.Text;
//=== Если введено примечание===//
  Primechanie := frxReport1.FindObject('Primechanie') as TfrxMemoView;
  if forPrim.Text<>'' then
    Primechanie.Memo.Text:=forPrim.Text;

//==================================//

  Itog := frxReport1.FindObject('Itog') as TfrxMemoView;
  if forPRAll.Text<>'*' then
    Itog.Memo.Text:='Итого :'
  else
    Itog.Memo.Text:='Итого загружено :';


  KolProd := frxReport1.FindObject('KolProd') as TfrxMemoView;
  KolProd.GetData;
  if KolProd.Value='0' then
    KolProd.Memo.Text:='';

  Stoimost := frxReport1.FindObject('Stoimost') as TfrxMemoView;
//Stoimost.Text:='[frxDatasetPrCap."ItogoOO"]';
  Stoimost.GetData;
  if forStoimost2_1.Checked=False then
  begin
    if forSumNDS_1.Checked then
      Stoimost.Text:= kopeiki(PrCapQuery.FieldByName('Itogo').AsFloat+SumBnsQuery.FieldByName('sumbns').AsFloat)
    else
      Stoimost.Text:=kopeiki(PrCapQuery.FieldByName('ItogoRoz').AsFloat)
  end
  else
  begin
    if forSumNDS_1.Checked then
      Stoimost.Text:=kopeiki(PrCapQuery.FieldByName('ItogoOO').AsFloat)
    else
      Stoimost.Text:=kopeiki(PrCapQuery.FieldByName('ItogoRoz').AsFloat)
  end;

  Stoimost2 := frxReport1.FindObject('Stoimost2') as TfrxMemoView;
  if (forSumNDS_1.Checked=True) and (forStoimost2_2.Checked=True) then
    Stoimost2.Text:=kopeiki(Rounding(PrCapQuery.FieldByName('ItogoRoz').AsFloat));

  Stoimost2.GetData;
  StText2 := Stoimost2.Text;

  SummNDS := frxReport1.FindObject('SummNDS') as TfrxMemoView;
  if forSumNDS_1.Checked=False then
    SummNDS.Text:='[<frxDatasetPrCap."SumIncNDS">+<frxDatasetPrCap."SumNDSRozn">]'
  else
    SummNDS.Text:='[<frxDatasetPrCap."SumIncNDS">+<frxDatasetPrCap."SumNDS">]';

  SummNDS.GetData;
  if forPRAll.Text<>'*' then
  begin
    SummNDS.Value:=SummNDS.Value+PrCapQuery.FieldByName('SumNdsBonus').Value;
    SummNDS.Value:=kopeiki(Rounding(StrToFloat(SummNDS.Value)));
    if SummNDS.Text<>' ' then
      SummNDS.Text:=kopeiki(Rounding(StrToFloat(SummNDS.Value)));
    if SummNDS.Text='0' then
      SummNDS.Text:='';
  end
  else
    SummNDS.Value:='';

  SumNdsText := SummNDS.Text;

  Memo51 := frxReport1.FindObject('Memo51') as TfrxMemoView;
  if forSumNDS_1.Checked=False then
    Memo51.Text:='[frxDatasetPrCap."SumNDSRozn"]'
  else
    Memo51.Text:='[frxDatasetPrCap."SumNDS"]';

  Memo57 := frxReport1.FindObject('Memo57') as TfrxMemoView;
  Memo57.Text:='[frxDatasetPrCap."SumSkid"]';

  VsSNDS := frxReport1.FindObject('VsSNDS') as TfrxMemoView;
  if forStoimost2_1.Checked then
    if forSumNDS_1.Checked=True then
      VsSNDS.Text:=kopeiki(PrCapQuery.FieldByName('ItogoOO').AsFloat+PrCapQuery.FieldByName('SumNDS').AsFloat)
    else
      VsSNDS.Text:=kopeiki(PrCapQuery.FieldByName('ItogoRoz').AsFloat)
  else
    if forSumNDS_1.Checked=True then
      VsSNDS.Text:=kopeiki(PrCapQuery.FieldByName('Itogo').AsFloat+PrCapQuery.FieldByName('SumNDS').AsFloat+SumBnsQuery.FieldByName('sumbns').AsFloat)
    else
      VsSNDS.Text:=kopeiki(PrCapQuery.FieldByName('ItogoRoz').AsFloat);

  VsSNdsText := VsSNDS.Text;
  KolGrMest := frxReport1.FindObject('KolGrMest') as TfrxMemoView;
  KolGrMest.GetData;
//=== Kol-vo naimenovaniy ===//
  Memo92 := frxReport1.FindObject('Memo92') as TfrxMemoView;
  Memo1 := frxReport2.FindObject('Memo1') as TfrxMemoView;
  Memo1.Text:='[Line]';
  Memo1.GetData;
  Memo92.Text:=Memo1.Value;
  kolline:=Memo1.Value;
  Memo1.Text:='[Line]';

  kolstr:=kolline div 11;
  if kolline mod 11=0 then
    kolstr:=kolline div 11
  else
    kolstr:=(kolline div 11)+1;
  Memo389 := frxReport1.FindObject('Memo389') as TfrxMemoView;
  Memo389.Text:=IntToStr(kolstr);

  MassBrutt := frxReport1.FindObject('MassBrutt') as TfrxMemoView;
  MassBrutt.GetData;
  If (forPRAll.Text<>' ') and (forVesTrEdit.Text<>'') then
  begin
    if MassBrutt.Value<>'' then
      MassBrutt.Value:=FloatToStr(StrToFloat(MassBrutt.Value)+StrToFloat(forVesTrEdit.Text))
    else
      MassBrutt.Value:=forVesTrEdit.Text;
  end;

//============//
//=== Суммы===//
//============//
  SumTov := frxReport1.FindObject('SumTov') as TfrxMemoView;
  if forPRAll.Text='*' then
    SumTov.Text:='';

  SumTov.Text:='[frxDatasetPrCap."ItogoTov"]';
  SumTov.GetData;
  if SumTov.Value='0' then
    SumTov.Text:='';

  SumTar := frxReport1.FindObject('SumTar') as TfrxMemoView;
  SumTar.GetData;
  if forPRAll.Text='*' then
    SumTar.Text:='';
  if SumTar.Value='0' then
    SumTar.Text:='';

  SumStTar := frxReport1.FindObject('SumStTar') as TfrxMemoView;
  SumStTar.GetData;
  if forPRAll.Text='*' then
    SumStTar.Text:='';
  if SumStTar.Value='0' then
    SumStTar.Text:='';

  SumTovRozn := frxReport1.FindObject('SumTovRozn') as TfrxMemoView;
  SumTovRozn.GetData;
  if forPRAll.Text='*' then
    SumTovRozn.Text:='';
  if SumTovRozn.Value='0' then
    SumTovRozn.Text:='';
  if SumTovRozn.Value <> 0 then
  begin
    if forStoimost2_2.Checked=True then
      SumTovRozn.Value := SumTovRozn.Value
    else
      SumTovRozn.Text:='';
  end;

  SumTarRozn := frxReport1.FindObject('SumTarRozn') as TfrxMemoView;

  if forPRAll.Text='*' then
    SumTarRozn.Text:='';
  if SumTarRozn.Value='0' then
    SumTarRozn.Text:='';
  if SumTarRozn.Value <> '' then
  begin
    if forStoimost2_2.Checked=True then
      SumTarRozn.Value := SumTarRozn.Value
    else
      SumTarRozn.Text:='';
  end;

//===================//
//=== Конец: Суммы===//
//===================//

//============================//
//=== Конец: 1-ая страница ===//
//============================//

  frxReport1.ShowReport(true);

//=====================//
//=== 2-ая страница ===//
//=====================//
  frxReport2.Variables['NameGrPr']:=GrPr;
  if forReturnTypeTrPredpr.Checked=True then
  begin
    frxReport1.Variables['forTrPredpr']:=QuotedStr('1');
    frxReport2.Variables['forTrPredpr']:=QuotedStr('1');
  end
  else
  begin
    frxReport2.Variables['forTrPredpr']:=QuotedStr('0');
    frxReport2.Variables['forTrPredpr']:=QuotedStr('0');
  end;

  if forCheckNastrGrTov.Checked then
  begin
    frxReport1.Variables['IdCheckNastrGrTov']:=QuotedStr('1');
    frxReport2.Variables['IdCheckNastrGrTov']:=QuotedStr('1');
  end
  else
  begin
    frxReport1.Variables['IdCheckNastrGrTov']:=QuotedStr('0');
    frxReport2.Variables['IdCheckNastrGrTov']:=QuotedStr('0');
  end;

  Memo88 := frxReport2.FindObject('Memo88') as TfrxMemoView;
  Memo88.Text:='[frxDatasetPrStr."EdIz"]';
  Memo32 := frxReport2.FindObject('Memo32') as TfrxMemoView;
  Memo32.Text:='[frxDatasetPrStr."EdIzDop"]';

  Memo92_2 := frxReport2.FindObject('Memo92') as TfrxMemoView;
  Memo92_2.Text:='[IIF(<frxDatasetPrStr."NDS"><>0,<frxDatasetPrStr."NDS">,'+chr(39)+chr(39)+')]';

  Memo96 := frxReport2.FindObject('Memo96') as TfrxMemoView;
  Memo96.Text:='[IIF(<frxDatasetPrStr."KolM"><>0,<frxDatasetPrStr."KolM">,'+chr(39)+chr(39)+')]';

  Memo300 := frxReport2.FindObject('Memo300') as TfrxMemoView;
  if (forStoimost.Checked=True) and (forSumNDS_1.Checked=True) and (forPr_CenTr.Checked=True) and (forVsegoSNDS_1.Checked=False)
  and (forStoimost2_2.Checked=False) and (forPr_ProcTr.Checked=False) and (forPr_CenWNal.Checked=False)
  and (forStoimost2_1.Checked=False) and (forPr_CenPriobr.Checked=False) then
    Memo300.Text:='[<frxDatasetPrStr."Art">+IIF(<frxDatasetPrStr."CenPor"><>0,'+chr(39)+'ЦЕНА ЗА ПОРЦИЮ '+chr(39)+'+FloatToStr(<frxDatasetPrStr."CenPor">)+'+chr(39)+' КОЛ-ВО ПОРЦИЙ '+chr(39)+'+FloatToStr(<frxDatasetPrStr."KolPor">),IIF((<frxDatasetPrStr."CenSt"><>0) and (<frxDatasetPrStr."KodGr"><>15),'+chr(39)+chr(39)+',IIF(<frxDatasetPrStr."Sort">='+chr(39)+chr(39)+','+chr(39)+chr(39)+','+chr(39)+' СОРТ '+chr(39)+')+<frxDatasetPrStr."Sort">))+'+chr(39)+'  '+chr(39)+'+'
  else
    Memo300.Text:='[<frxDatasetPrStr."Art">+IIF(<frxDatasetPrStr."Sort">='+chr(39)+chr(39)+','+chr(39)+chr(39)+','+chr(39)+' СОРТ '+chr(39)+'+<frxDatasetPrStr."Sort">)+'+chr(39)+'  '+chr(39)+'+ IIF(<frxDatasetPrStr."CenPor"><>0,'+chr(39)+'ЦЕНА ЗА ПОРЦИЮ '+chr(39)+'+FloatToStr(<frxDatasetPrStr."CenPor">)+'+chr(39)+' КОЛ-ВО ПОРЦИЙ '+chr(39)+'+FloatToStr(<frxDatasetPrStr."KolPor">),IIF((<frxDatasetPrStr."CenSt"><>0) and (<frxDatasetPrStr."PNTTAR1"><>0),'+chr(39)+' ЦЕНА СО СТЕКЛОТАРОЙ '+chr(39)+'+FloatToStr(<frxDatasetPrStr."CenSt">),IIF((<frxDatasetPrStr."CenSt"><>0) and (<frxDatasetPrStr."PNTTar3"><>0),'+chr(39)+' ЦЕНА НАБОРА '+chr(39)+'+FloatToStr(<frxDatasetPrStr."CenSt">)+'+chr(39)+' *** '+chr(39)+','+chr(39)+chr(39)+')))+'+chr(39)+'  '+chr(39)+'+';

  Memo300.Text:=Memo300.Text+'IIF(<frxDatasetPrStr."Model"><>'+chr(39)+chr(39)+','+chr(39)+' Модель '+chr(39)+'+<frxDatasetPrStr."Model">,'+chr(39)+chr(39)+')+IIF(<frxDatasetPrStr."Sizes"><>'+chr(39)+chr(39)+','+chr(39)+' Размер '+chr(39)+'+<frxDatasetPrStr."Sizes">,'+chr(39)+chr(39)+')+IIF(<frxDatasetPrStr."Hight"><>'+chr(39)+chr(39)+','+chr(39)+' Рост '+chr(39)+'+<frxDatasetPrStr."Hight">,'+chr(39)+chr(39)+')+';
  Memo300.Text:=Memo300.Text+'IIF(<frxDatasetPrStr."Sertif"><>'+chr(39)+chr(39)+',<frxDatasetPrStr."Sertif">,'+chr(39)+chr(39)+')+'+chr(39)+'  '+chr(39)+'+IIF(<frxDatasetPrStr."Sertif1"><>'+chr(39)+chr(39)+',<frxDatasetPrStr."Sertif1">,'+chr(39)+chr(39)+')+'+chr(39)+' '+chr(39)+'+IIF(<frxDatasetPrStr."Sertif2"><>'+chr(39)+chr(39)+',<frxDatasetPrStr."Sertif2">,'+chr(39)+chr(39)+')+'+chr(39)+' '+chr(39)+ '+IIF(<frxDatasetPrStr."Protkl"><>'+chr(39)+chr(39)+','+chr(39)+' Обосн. цены '+chr(39)+'+<frxDatasetPrStr."Protkl">,'+chr(39)+chr(39)+')]';

  if (forStoimost.Checked=True) and (forSumNDS_1.Checked=True) and (forPr_CenTr.Checked=True) and (forVsegoSNDS_1.Checked=False)
  and (forStoimost2_2.Checked=False) and (forPr_ProcTr.Checked=False) and (forPr_CenWNal.Checked=False)
  and (forStoimost2_1.Checked=False) and (forPr_CenPriobr.Checked=False) then
    frxReport2.Variables['VernutTaru']:=QuotedStr('0')
  else
    frxReport2.Variables['VernutTaru']:=QuotedStr('1');

//=== Сумма по 9-ой колонке ===//
  SumVsSNds := frxReport2.FindObject('SumVsSNds') as TfrxMemoView;
//=== Конец: Сумма по 9-ой колонке ===//

//=== Сумма по 6-ой колонке ===//
//=== Конец: Сумма по 6-ой колонке ===//


  NamePredpr := frxReport2.FindObject('NamePredpr') as TfrxMemoView;
  NamePredpr.Text:='[IIF(<frxDatasetPrStr."NamePredpr"><>'+chr(39)+chr(39)+',<frxDatasetPrStr."NamePredpr">,IIF(<frxDatasetPrStr."NameCountry"><>'+chr(39)+chr(39)+',<frxDatasetPrStr."NameCountry">,'+chr(39)+chr(39)+'))]';
  NalProd := frxReport2.FindObject('NalProd') as TfrxMemoView;
  Cen4_1 := frxReport2.FindObject('Cen4_1') as TfrxMemoView;
  Cen4_2 := frxReport2.FindObject('Cen4_2') as TfrxMemoView;
//===6===//
  Memo6_1 := frxReport2.FindObject('Memo6_1') as TfrxMemoView;
  Memo6_2 := frxReport2.FindObject('Memo6_2') as TfrxMemoView;
  Memo6_3 := frxReport2.FindObject('Memo6_3') as TfrxMemoView;
  Memo6_4 := frxReport2.FindObject('Memo6_4') as TfrxMemoView;

  if forSumNDS_1.Checked=True then
  begin
    frxReport1.Variables['Pr_ONds']:=QuotedStr('1');
    frxReport2.Variables['Pr_ONds']:=QuotedStr('1');
  end
  else
  begin
    frxReport1.Variables['Pr_ONds']:=QuotedStr('0');
    frxReport2.Variables['Pr_ONds']:=QuotedStr('0');
  end;

  if forVsegoSNDS_1.Checked=True then
  begin
    frxReport1.Variables['Pr_RNds']:=QuotedStr('1');
    frxReport2.Variables['Pr_RNds']:=QuotedStr('1');
  end
  else
  begin
    frxReport1.Variables['Pr_RNds']:=QuotedStr('0');
    frxReport2.Variables['Pr_RNds']:=QuotedStr('0');
  end;

  if forStoimost2_2.Checked=True then
  begin
    frxReport1.Variables['Pr_CenRoz']:=QuotedStr('1');
    frxReport2.Variables['Pr_CenRoz']:=QuotedStr('1');
  end
  else
  begin
    frxReport1.Variables['Pr_CenRoz']:=QuotedStr('0');
    frxReport2.Variables['Pr_CenRoz']:=QuotedStr('0');
  end;

  if forStoimost.Checked=True then
  begin
    frxReport1.Variables['Pr_CenOpt']:=QuotedStr('1');
    frxReport2.Variables['Pr_CenOpt']:=QuotedStr('1');
  end
  else
  begin
    frxReport1.Variables['Pr_CenOpt']:=QuotedStr('0');
    frxReport2.Variables['Pr_CenOpt']:=QuotedStr('0');
  end;

  if forPr_CenPriobr.Checked=True then
  begin
    frxReport1.Variables['Pr_CenPriobr']:=QuotedStr('1');
    frxReport2.Variables['Pr_CenPriobr']:=QuotedStr('1');
  end
  else
  begin
    frxReport1.Variables['Pr_CenPriobr']:=QuotedStr('0');
    frxReport2.Variables['Pr_CenPriobr']:=QuotedStr('0');
  end;

  if forPr_CenTr.Checked=True then
  begin
    frxReport1.Variables['Pr_CenTr']:=QuotedStr('1');
    frxReport2.Variables['Pr_CenTr']:=QuotedStr('1');
  end
  else
  begin
    frxReport1.Variables['Pr_CenTr']:=QuotedStr('0');
    frxReport2.Variables['Pr_CenTr']:=QuotedStr('0');
  end;

  if forPr_ProcTr.Checked=True then
  begin
    frxReport1.Variables['Pr_ProcTr']:=QuotedStr('1');
    frxReport2.Variables['Pr_ProcTr']:=QuotedStr('1');
  end
  else
  begin
    frxReport1.Variables['Pr_ProcTr']:=QuotedStr('0');
    frxReport2.Variables['Pr_ProcTr']:=QuotedStr('0');
  end;

  if forPr_CenWNal.Checked=True then
  begin
    frxReport1.Variables['Pr_CenWNal']:=QuotedStr('1');
    frxReport2.Variables['Pr_CenWNal']:=QuotedStr('1');
  end
  else
  begin
    frxReport1.Variables['Pr_CenWNal']:=QuotedStr('0');
    frxReport2.Variables['Pr_CenWNal']:=QuotedStr('0');
  end;

  if forStoimost2_1.Checked=True then
  begin
    frxReport1.Variables['Pr_SchCenPriobr']:=QuotedStr('1');
    frxReport2.Variables['Pr_SchCenPriobr']:=QuotedStr('1');
  end
  else
  begin
    frxReport1.Variables['Pr_SchCenPriobr']:=QuotedStr('0');
    frxReport2.Variables['Pr_SchCenPriobr']:=QuotedStr('0');
  end;

  if forPRAll.Text='*' then
  begin
    frxReport1.Variables['PrAll']:=QuotedStr('1');
    frxReport2.Variables['PrAll']:=QuotedStr('1');
  end
  else
  begin
    frxReport1.Variables['PrAll']:=QuotedStr('0');
    frxReport2.Variables['PrAll']:=QuotedStr('0');
  end;

//=========//
//=== 8 ===//
//=========//

  Memo93 := frxReport2.FindObject('Memo93') as TfrxMemoView;
  Memo93.Text:='[IIF(<frxDatasetPrStr."SumNDS"><>0,<frxDatasetPrStr."SumNDS">,'+chr(39)+chr(39)+')]';

  if forSumNDS_1.Checked=False then
    Memo93.Text:=StringReplace(Memo93.Text,'SumNDS','SumNDSR',[rfReplaceAll,rfIgnoreCase]);

//============================================================//
//=== Конец: Определяется, какие из Memo должны выводиться ===//
//============================================================//

  NumNakl := frxReport2.FindObject('NumNakl') as TfrxMemoView;
  If forPRAll.Text<>' ' then
    NumNakl.Memo.Text:= forTTNEdit.Text
  else
    NumNakl.Memo.Text:=forNumDoc.Text;

  SeriaSecondPage := frxReport2.FindObject('SeriaSecondPage') as TfrxMemoView;
  if forSeria.Text<>'' then
    SeriaSecondPage.Text:=forSeria.Text;

  Memo3 := frxReport2.FindObject('Memo3') as TfrxMemoView;
  Memo4 := frxReport2.FindObject('Memo4') as TfrxMemoView;
  Memo5 := frxReport2.FindObject('Memo5') as TfrxMemoView;
  Memo6 := frxReport2.FindObject('Memo6') as TfrxMemoView;
  Memo7 := frxReport2.FindObject('Memo7') as TfrxMemoView;

  if (PrCapQuery.FieldByName('SumBns').Value<>0) then
    Memo3.Text:='Бонус';

  if (PrCapQuery.FieldByName('SumBns').Value<>0) then
    Memo4.Text:=FloatToStr(PrCapQuery.FieldByName('SumBns').AsFloat);

  if (PrCapQuery.FieldByName('SumBns').Value<>0) then
    Memo5.Text:=FloatToStr(PrCapQuery.FieldByName('NdsBns').AsFloat);

  if (PrCapQuery.FieldByName('SumBns').Value<>0) then
    Memo6.Text:=FloatToStr(PrCapQuery.FieldByName('SumNdsBonus').AsFloat);

  if (PrCapQuery.FieldByName('SumBns').Value<>0) then
    Memo7.Text:=FloatToStr(PrCapQuery.FieldByName('SumBns').AsFloat);


//============================//
//=== Конец: 2-ая страница ===//
//============================//

  frxReport2.ShowReport(true);

//==========================//
//=== Конец: Вывод отчёта===//
//==========================//

//frxReport1.ShowReport(true);
//frxReport2.ShowReport(true);

end;

procedure TNaklForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FioDovQuery.Close;
Action:=caFree;
NaklForm:=nil;
end;

//============================//
//=== Используемые функции ===//
//============================//

function TNaklForm.Propis(S:Currency;KK:Integer):string;
var
    S1:string;
    Rub:string;
    Kop:string;
    Prop:string;
    A1_1:string;
    A1_3:string;
    A4_6:string;
    A7_9:string;
    C:string;
begin
C:='000000000000000';
Prop:=' ';
S1:=FloatToStrF(S,ffFixed,15,2);
S1:=Copy(C,1,(15-length(S1)))+S1;
Rub:=Copy(S1,1,12);
Kop:=Copy(S1,14,2);
A1_1:=Copy(Rub,1,3);
A1_3:=Copy(Rub,4,3);
A4_6:=Copy(Rub,7,3);
A7_9:=Copy(Rub,10,3);
If StrToInt(A1_1)>0 then
Prop:=Num(A1_1,3);
If StrToInt(A1_3)>0 then
Prop:=Prop+Num(A1_3,1);
If StrToInt(A4_6)>0 then
Prop:=Prop+Num(A4_6,2);
If StrToInt(A7_9)>0 then
Prop:=Prop+Num(A7_9,0);
if Length(Prop)=1 then
Prop:='НОЛЬ';
if KK=0 then
//Propis:=Prop+'  руб.'+ Kop+'  коп.'
Propis:=Prop+'  белорусских рублей'
else
begin
if KK=1 then
Propis:=Prop+' ЗПТ '+ Kop
else
Propis:=Prop
end;
end;

function TNaklForm.Num(Q:string;X:integer):string;
var I,J,K,L:integer;
    A:Array[1..9,1..5] of string;
begin
A[1,1]:=' сто';
A[2,1]:=' двести';
A[3,1]:=' триста';
A[4,1]:=' четыреста';
A[5,1]:=' пятьсот';
A[6,1]:=' шестьсот';
A[7,1]:=' семьсот';
A[8,1]:=' восемьсот';
A[9,1]:=' девятьсот';
//*
A[1,2]:=' десять';
A[2,2]:=' двадцать';
A[3,2]:=' тридцать';
A[4,2]:=' сорок';
A[5,2]:=' пятьдесят';
A[6,2]:=' шестьдесят';
A[7,2]:=' семьдесят';
A[8,2]:=' восемьдесят';
A[9,2]:=' девяносто';
//*
A[1,3]:=' одиннадцать';
A[2,3]:=' двенадцать';
A[3,3]:=' тринадцать';
A[4,3]:=' четырнадцать';
A[5,3]:=' пятнадцать';
A[6,3]:=' шестнадцать';
A[7,3]:=' семнадцать';
A[8,3]:=' восемнадцать';
A[9,3]:=' девятнадцать';
//*
A[1,4]:=' один';
A[2,4]:=' два';
A[3,4]:=' три';
A[4,4]:=' четыре';
A[5,4]:=' пять';
A[6,4]:=' шесть';
A[7,4]:=' семь';
A[8,4]:=' восемь';
A[9,4]:=' девять';
//*
A[1,5]:=' одна';
A[2,5]:=' две';
A[3,5]:=' три';
A[4,5]:=' четыре';
A[5,5]:=' пять';
A[6,5]:=' шесть';
A[7,5]:=' семь';
A[8,5]:=' восемь';
A[9,5]:=' девять';
Result:='';
I:=StrToInt(Copy(Q,1,1));
J:=StrToInt(Copy(Q,2,1));
K:=StrToInt(Copy(Q,3,1));
L:=StrToInt(Q);
if I>0 then
Result:=Result+A[I,1];
if ((J>1) or ((J=1) and (K=0))) then
begin
Result:=Result+A[J,2];
  if K>0 then
  begin
   if X<>2 then
   Result:=Result+A[K,4]
   else
   Result:=Result+A[K,5];
  end;
end;
//*
if ((J=1) and (K>0)) then
Result:=Result+A[K,3];
if ((J=0) and (K>0)) then
begin
   if X<>2 then
   Result:=Result+A[K,4]
   else
   Result:=Result+A[K,5];
end;
//*
if X=3 then
begin
 if L>0 then
 Result:=Result+' миллиард';
 if (((L>4) and (L<21)) or (K>4) or ((J=0) and (K=0))) then
 Result:=Result+'ов'
 else
 begin
  if ((K>=2) and (K<=4)) then
  Result:=Result+'а';
 end;
end;
//*
if X=1 then
begin
 if L>0 then
 Result:=Result+' миллион';
 if (((L>4) and (L<21)) or (K>4) or ((J=0) and (K=0))) then
 Result:=Result+'ов'
 else
 begin
  if ((K>=2) and (K<=4)) then
  Result:=Result+'а';
 end;
end;
//*
if X=2 then
begin
 if L>0 then
 Result:=Result+' тысяч';
 if ((K=1) and ((L<9) or (L>20))) then
 Result:=Result+'а'
 else
 begin
  if (((K>1) and (K<5))and((L<9) or (L>20))and (J<>1)) then
  Result:=Result+'и';
 end;
end;
end;

function TNaklForm.PROPER(S: string): string;
var
  X: Integer;
begin
  Result := '';
  if Length(s) = 0 then
    exit;
  for X := 1 to length(s) do
    if s[x]<>' ' then begin
                      S[x] := AnsiUpperCase(s[x])[1];
                      Break;
                      end;
  Result := S;
end;

function TNaklForm.Rounding(Value:Currency):Currency; // Округление до сотых Суховаров А.А.
begin
  Result := Trunc(Value*100+0.5)/100;
end;

function TNaklForm.kopeiki(Value:Currency):String; // Вывод числа с копейками
begin
   Result := IntToStr(Trunc(Value))+','+Copy(FloatToStr(Frac(Value))+'000',3,2)
end;


end.

