unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, FIBDataSet, pFIBDataSet, FIBDatabase, pFIBDatabase, StdCtrls,
  ExtCtrls;

type
  TForm3 = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    edtVMostrador: TEdit;
    Button1: TButton;
    Button2: TButton;
    trTrans: TpFIBTransaction;
    IBQuery1: TpFIBDataSet;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure Inicio();

    function Rellena(Cadena:String):String;

  end;

var
  Form3: TForm3;

implementation
uses Unit1;

{$R *.dfm}

procedure TForm3.Button1Click(Sender: TObject);
var lFolio:String;
lDoctoPvId:Integer;
begin
try
  begin
    if edtVMostrador.Text='' then
    begin
      MessageDlg('El folio '+ lFolio +' de venta mostrador es incorrecto.', mtInformation, [mbOk], 0);
      Exit;
    end;
    lFolio := edtVMostrador.Text;
    lFolio := Rellena(lFolio);
    //Revisar que exista el folio
    IBQuery1.Close;
    IBQuery1.SelectSQL.Clear;
    IBQuery1.SelectSQL.Add('SELECT A.ID_SESION, A.FOLIO_PEDIDO ');
    IBQuery1.SelectSQL.Add('FROM MGNADRO_SESIONES A ');
    IBQuery1.SelectSQL.Add('WHERE A.FOLIO_VM = :FOLIO_VM ');
    IBQuery1.SelectSQL.Add('AND A.FOLIO_PEDIDO <> '''' ');
    IBQuery1.SelectSQL.Add('AND A.FOLIO_PEDIDO IS NOT NULL ');
    IBQuery1.SelectSQL.Add('AND A.ESTATUS = ''T'' ');
    IBQuery1.ParamByName('FOLIO_VM').AsString := lFolio;
    IBQuery1.Open;
    if IBQuery1.FieldByName('FOLIO_PEDIDO').AsString <> '' then
    begin
      MessageDlg('Folio de cierre de venta Nadro '+ IBQuery1.FieldByName('FOLIO_PEDIDO').AsString +' de venta mostrador '+lFolio+' Microsip.', mtInformation, [mbOk], 0);
    end
    else
    begin
      MessageDlg('El folio '+ lFolio +' de venta mostrador Microsip no tiene folio de cierre de venta Nadro.', mtInformation, [mbOk], 0);
    end;

    if trTrans.InTransaction then
      trTrans.Commit;

    Inicio();
  end;
except on e:exception do
begin
  if trTrans.InTransaction  then
    trTrans.Rollback;
  MessageDlg('Error al verificar datos.'+#13+e.Message,mtInformation, [mbOk], 0);
end;
end;
end;

procedure TForm3.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
var I:Integer;
begin
  for I := 0 to trTrans.FIBDataSetsCount - 1 do
    trTrans.FIBDataSets[I].Close;
  trTrans.CloseAllQueryHandles;
  trTrans.Active := False;
end;

procedure TForm3.FormShow(Sender: TObject);
begin
  Inicio();
end;

function TForm3.Rellena(Cadena:String):String;
var serie,folio:string;
S,i:integer;
begin
  folio := cadena;
  S:=0;
  for i:= 1 to length(folio) do
    if (folio[i] <> '0') and (folio[i] <> '1') and (folio[i] <> '2') and (folio[i] <> '3') and (folio[i] <> '4') and (folio[i] <> '5') and (folio[i] <> '6') and (folio[i] <> '7') and (folio[i] <> '8') and (folio[i] <> '9') then
      S := S+1;

  serie:=copy(folio,1,S);
  serie:=Uppercase(serie);
  if length(serie) < 4 then
  begin
    if S=0 then cadena := '000000000';
    if S=1 then cadena := '00000000';
    if S=2 then cadena := '0000000';
    if S=3 then cadena := '000000';

    folio := copy(folio,S+1,length(folio)-S);

    for i:=1 to length(folio) do
      cadena[10-S-i] := folio[length(folio)+1-i];
    Result:=serie+cadena;
  end
  else Result := '';
end;

procedure TForm3.Inicio();
begin
try
begin
  if trTrans.InTransaction  then
    trTrans.Rollback;
  trTrans.StartTransaction;

  edtVMostrador.Text := '';

end;
except on e:exception do
begin
  if trTrans.InTransaction then
    trTrans.Rollback;

  MessageDlg('Error inicial.' + e.Message , mtInformation,  [mbOk], 0);
end;
end;
end;


end.
