⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 agentlqfrm.pas

📁 这个是个简单的关于出票申请的处理
💻 PAS
字号:

unit AgentLQFrm;

interface

uses
  Windows, SysUtils, Classes, Controls, Forms,
  Dialogs, BaseFrm, StdCtrls, ExtCtrls, ComCtrls;

type
  TAgentLQTicket = record
    AgentId: Integer;
    StartPrintCode: string;
    TicketCount: Integer;
    LQDate: TDateTime;
    UserArea: string;
  end;
  
  TFormAgentLQ = class(TFormBase)
    pnlTop: TPanel;
    Label1: TLabel;
    EdtIdEntifyCode: TEdit;
    PnlBottom: TPanel;
    PnlClient: TPanel;
    GroupBox1: TGroupBox;
    btnFind: TButton;
    Label9: TLabel;
    EdtStarTicketNum: TEdit;
    Label7: TLabel;
    EdtDisTicket: TEdit;
    BtnConfirmation: TButton;
    Panel1: TPanel;
    Label8: TLabel;
    mmResult: TMemo;
    Label4: TLabel;
    EdtApplyTicket: TEdit;
    EdtAgentId: TEdit;
    Label2: TLabel;
    EdtApplyDate: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    EdtAgentName: TEdit;
    Label3: TLabel;
    EdtSurplusTicket: TEdit;
    cbAgree: TCheckBox;
    GroupBox2: TGroupBox;
    lvHistory: TListView;
    Label10: TLabel;
    cbArea: TComboBox;
    lblStoreCount: TLabel;
    procedure btnFindClick(Sender: TObject);
    procedure BtnConfirmationClick(Sender: TObject);
    procedure EdtDisTicketKeyPress(Sender: TObject; var Key: Char);
    procedure EdtIdEntifyCodeKeyPress(Sender: TObject; var Key: Char);
    procedure FormCreate(Sender: TObject);
    procedure cbAreaChange(Sender: TObject);
  private
    { Private declarations }
    FExistIndentifyCode: Boolean;
    function GetPrintCode: string;
    procedure LoadAgentLQTicketInfo(AAgentId: Integer);
    procedure InsertAgentLQTicketHistory(AAgentLQTicket: TAgentLQTicket; AIsHead: Boolean = False);
    function GetExistInvalidTicketCount: Integer;
    function GetCanLQTicketCount(AAgentId: integer): Integer;
    procedure WriteDisTicketHistory(AStartTicketNo: string; ATicketCount: Integer);
    function isExistPrintCode(APrintCode: string): Boolean;
    procedure UpdateStoreCountCaption(AStorCount: Integer);
  public
    { Public declarations }
    procedure InitComponentEditInfo;
    procedure SetAgentLQFormActive;
  end;

var
  FormAgentLQ: TFormAgentLQ;

implementation
uses
  systemPH, SystemDM, CommonFuncLib, IniOptions, LQTicketHintFrm;

{$R *.dfm}

procedure TFormAgentLQ.btnFindClick(Sender: TObject);
var
  sSqlStr: string;
  nAgentId: Integer;
begin
  inherited;
  try
    if not DMSystem.ConnMain.Connected then
    begin
      SendMessage(Application.MainForm.Handle, DM_CONNECTDB, 0, 0);
      Exit;
    end;
     sSqlStr := 'SELECT IDENTIFYCODE, ISUSED FROM TICKET_APPLY WHERE IDENTIFYCODE = ' + #39 + EdtIdEntifyCode.Text + #39;
     if DMSystem.SQL_Querys(DMSystem.Qry_Temp, sSqlStr) then
     begin
       if DMSystem.Qry_Temp.FieldByName('ISUSED').AsBoolean then
       begin
         ShowMessage('该识别码已经被使用!');
         Exit;
       end;
     end;


    sSqlStr := 'SELECT A.AGENTID, A.TICKETCOUNT, A.APPLYDATE, A.IDENTIFYCODE, A.ISDOAPPLY, A.ISAGREE, A.RESULTS, B.ID, B.USERNAME, B.TICKET_COUNT ' +
               'FROM TICKET_APPLY A INNER JOIN SITE_USER B ON A.AGENTID = B.ID WHERE  A.IDENTIFYCODE = ' + #39 + Trim(EdtIdEntifyCode.Text) + #39 + 'AND A.ISDOAPPLY = 1';
    if DMSystem.SQL_Querys(DMSystem.Qry_Temp, sSqlStr) then
    with DMSystem.Qry_Temp do
    begin
      nAgentId := FieldByName('AGENTID').AsInteger;
      EdtAgentId.Text := IntToStr(nAgentId);
      EdtAgentName.Text := FieldByName('USERNAME').AsString;
      EdtApplyTicket.Text := FieldByName('TICKETCOUNT').AsString;
      EdtSurplusTicket.Text := IntToStr(FieldByName('TICKET_COUNT').AsInteger - GetCanLQTicketCount(nAgentId));
      EdtApplyDate.Text := FieldByName('APPLYDATE').AsString;
      cbAgree.Checked := FieldByName('ISAGREE').AsBoolean;
      mmResult.Text := FieldByName('RESULTS').AsString;
      FExistIndentifyCode := True;
      BtnConfirmation.Enabled := True;
      EdtStarTicketNum.Text := GetPrintCode;

      lvHistory.Clear;
      LoadAgentLQTicketInfo(nAgentId);
    end
    else begin
      ShowMessage('该识别码不存在或者该申请还没有被答复。');
      Exit;
    end;
  except
  end;
end;

procedure TFormAgentLQ.BtnConfirmationClick(Sender: TObject);
var
  sSqlStr: string;
  nDisTicket, I, CurDisTicketCount: Integer;
  oAgentLQTicket: TAgentLQTicket;
  PrintTicketNo, CurStartTicketNo: Double;
  LQTickerList: TStrings;
begin
  inherited;
  CurDisTicketCount := 0;
  if not DMSystem.isConnectionDB then Exit;

  if StrToIntDef(EdtDisTicket.Text, 0) = 0 then
  begin
    ShowMessage('分配的票数不能为零!');
    Exit;
  end;

  if (StrToIntDef(EdtSurplusTicket.Text, 0) < StrToIntDef(EdtDisTicket.Text, 0)) then
  begin
    ShowMessage('分配的票数不能大于可领取的剩余票数!');
    Exit;
  end;

  if (GetExistInvalidTicketCount - GLOBALYLTICKETCOUNT) < StrToIntDef(EdtDisTicket.Text, 0) then
  begin
    ShowMessage('票号不足,不能进行分配!');
    Exit;
  end;

  try
    LQTickerList := TStringList.Create;
    LQTickerList.Add('代理人帐号:' + EdtAgentId.Text);
    nDisTicket := StrToIntDef(EdtDisTicket.Text, 0);

    PrintTicketNo := StrToFloatDef(Trim(EdtStarTicketNum.Text), 0);
    CurStartTicketNo := PrintTicketNo;
    for I := 0 to nDisTicket-1 do
    try
      Inc(CurDisTicketCount);
      if isExistPrintCode(FloatToStr(PrintTicketNo)) then
      begin
        sSqlStr := 'UPDATE TICKET_NUMBER SET ISUSED = 1 WHERE PRINTCODE = ' + #39 + FloatToStr(PrintTicketNo) + #39;
        DMSystem.SQL_Exec(DMSystem.Qry_Temp, sSqlStr);
        PrintTicketNo := PrintTicketNo + 1;
      end
      else
      begin //跳号
        sSqlStr := 'SELECT TOP 1 PRINTCODE FROM TICKET_NUMBER WHERE ISUSED = 0' +
                  ' AND USERAREA = ' + IntToStr(cbArea.ItemIndex) + ' ORDER BY PRINTCODE';
        if DMSystem.SQL_Querys(DMSystem.Qry_Temp, sSqlStr) then
        begin
          LQTickerList.Add(Format(DEF_LQTICKETHINT, [FloatToStr(CurStartTicketNo), FloatToStr(PrintTicketNo-1), IntToStr(CurDisTicketCount-1)]));
          PrintTicketNo := StrToFloatDef(DMSystem.Qry_Temp.FieldByName('PRINTCODE').AsString, 0);

          WriteDisTicketHistory(FloatToStr(CurStartTicketNo), CurDisTicketCount-1);
          CurStartTicketNo := PrintTicketNo;
          CurDisTicketCount := 1;

          sSqlStr := 'UPDATE TICKET_NUMBER SET ISUSED = 1 WHERE PRINTCODE = ' + #39 + FloatToStr(PrintTicketNo) + #39;
          if not DMSystem.SQL_Exec(DMSystem.Qry_Temp, sSqlStr) then  Exit;

          PrintTicketNo := 1 + PrintTicketNo;
        end;
      end;
    except
      DMSystem.Qry_Temp.SQL.SaveToFile('UPDATE_TICKET_NUMBER_SQL.TXT');
    end;
    LQTickerList.Add(Format(DEF_LQTICKETHINT, [FloatToStr(CurStartTicketNo), FloatToStr(PrintTicketNo-1), IntToStr(CurDisTicketCount)]));
    LQTickerList.Add('总共领取票数:' + EdtDisTicket.Text);
    WriteDisTicketHistory(FloatToStr(CurStartTicketNo), CurDisTicketCount);

    sSqlStr := 'UPDATE TICKET_APPLY SET ISUSED = 1 WHERE IDENTIFYCODE = ' + #39 + EdtIdEntifyCode.Text + #39;
    DMSystem.SQL_Exec(DMSystem.Qry_Temp, sSqlStr);
  finally
     //显示领取信息;
    TFrmLQTicketHint.ShowLQTicketHintForm(LQTickerList);
    LQTickerList.Free;

    //写历史信息
    oAgentLQTicket.StartPrintCode := Trim(EdtStarTicketNum.Text);
    oAgentLQTicket.TicketCount := StrToIntDef(Trim(EdtDisTicket.Text), 0);
    oAgentLQTicket.LQDate := Now;
    oAgentLQTicket.UserArea := cbArea.Text;
    InsertAgentLQTicketHistory(oAgentLQTicket);

    //初始化
    BtnConfirmation.Enabled := False;
    EdtIdEntifyCode.Text := '';
    EdtStarTicketNum.Text := GetPrintCode;
    EdtDisTicket.Text := '';
    EdtAgentId.Text := '';
    EdtAgentName.Text := '';
    EdtApplyTicket.Text := '';
    EdtSurplusTicket.Text := '';
    EdtApplyDate.Text := '';
    mmResult.Text := '';
    cbAgree.Checked := False;
    lvHistory.Clear;
    GlobalStoreCount := GlobalStoreCount - nDisTicket;
    UpdateStoreCountCaption(GlobalStoreCount);
  end;
end;

procedure TFormAgentLQ.EdtDisTicketKeyPress(Sender: TObject;
  var Key: Char);
begin
  inherited;
  if not (Key in ['0'..'9', #8]) then Key := #0;
end;

procedure TFormAgentLQ.InitComponentEditInfo;
begin
  EdtIdEntifyCode.Text := '';
  EdtAgentId.Text := '';
  EdtAgentName.Text := '';
  EdtSurplusTicket.Text := '0';
  EdtApplyTicket.Text := '0';
  EdtApplyDate.Text := '';
  cbAgree.Checked := False;
  EdtDisTicket.Text := '0';
  mmResult.Text := '';
end;

function TFormAgentLQ.GetPrintCode: string;
var
  sSqlStr: string;
begin
  Result := '';
  sSqlStr := 'SELECT TOP 1 PRINTCODE FROM TICKET_NUMBER WHERE ISUSED = 0' +
             ' AND USERAREA = ' + IntToStr(cbArea.ItemIndex) + 'ORDER BY PRINTCODE';
  if DMSystem.SQL_Querys(DMSystem.Qry_Temp, sSqlStr)then
    Result := DMSystem.Qry_Temp.FieldByName('PRINTCODE').AsString;
end;

procedure TFormAgentLQ.LoadAgentLQTicketInfo(AAgentId: Integer);
var
  I, nIndex: Integer;
  sSqlStr: string;
  oAgentLQTIcket: TAgentLQTicket;
begin
  sSqlStr := 'SELECT * FROM TICKET_NUMBER_HISTORY WHERE AGENTID = ' + IntToStr(AAgentId) +
             '  ORDER BY OPERATEDATE DESC';
  if DMSystem.SQL_Querys(DMSystem.Qry_Temp, sSqlStr) then
  with DMSystem.Qry_Temp do
  try
    First;
    SendMessage(Application.MainForm.Handle, WM_STATUS_MSG, DEF_START_UPDATE, RecordCount);
    for I := 1 to RecordCount do
    begin
      oAgentLQTIcket.AgentId := AAgentId;
      oAgentLQTIcket.StartPrintCode := Trim(FieldByName('StartPrintCode').AsString);
      oAgentLQTIcket.TicketCount := FieldByName('TicketCount').AsInteger;
      oAgentLQTIcket.LQDate := FieldByName('OPERATEDATE').AsDateTime;
      nIndex := FieldByName('UserArea').AsInteger;
      if nIndex < cbArea.Items.Count then
      oAgentLQTIcket.UserArea := cbArea.Items[nIndex];
      InsertAgentLQTicketHistory(oAgentLQTIcket);
      SendMessage(Application.MainForm.Handle, WM_STATUS_MSG, DEF_PLAYING_UPDATE, I);
      Next;
      Application.ProcessMessages;
    end;
  finally
    SendMessage(Application.MainForm.Handle, WM_STATUS_MSG, DEF_FINISHED_UPDATE, 0);
  end
  else
    lvHistory.Clear;
end;

procedure TFormAgentLQ.InsertAgentLQTicketHistory(
  AAgentLQTicket: TAgentLQTicket; AIsHead: Boolean = False);
var
  oListItem: TListItem;
begin
  oListItem := lvHistory.Items.Add;
  oListItem.Caption := IntToStr(lvHistory.Items.Count);
  oListItem.SubItems.Add(AAgentLQTicket.StartPrintCode);
  oListItem.SubItems.Add(IntToStr(AAgentLQTicket.TicketCount));
  oListItem.SubItems.Add(DateTimeToStr(AAgentLQTicket.LQDate));
end;


function TFormAgentLQ.GetExistInvalidTicketCount: Integer;
var
  sSqlStr: string;
begin
  Result := 0;
  sSqlStr := 'SELECT COUNT(ID) AS TOTALTICKETCOUNT FROM TICKET_NUMBER WHERE ISUSED = 0' +
             ' AND USERAREA = ' + IntToStr(cbArea.ItemIndex);
  if DMSystem.SQL_Querys(DMSystem.Qry_Temp, sSqlStr) then
    Result := DMSystem.Qry_Temp.FieldByName('TOTALTICKETCOUNT').AsInteger
end;

function TFormAgentLQ.GetCanLQTicketCount(AAgentId: integer): Integer;
var
  sSqlStr: string;
begin
  Result := 0;
  sSqlStr := 'SELECT SUM(NUM - USED) AS KUSEDTICKETCOUNT FROM PRINTCODE WHERE AGENT_ID = ' + IntToStr(AAgentId);
  if DMSystem.SQL_Querys(DMSystem.Qry_Info, sSqlStr) then
    Result := DMSystem.Qry_Info.FieldByName('KUSEDTICKETCOUNT').AsInteger;
end;

procedure TFormAgentLQ.EdtIdEntifyCodeKeyPress(Sender: TObject;
  var Key: Char);
begin
  inherited;
  if Key = #13 then
    btnFind.OnClick(nil);
end;

procedure TFormAgentLQ.FormCreate(Sender: TObject);
begin
  inherited;
  cbArea.ItemIndex := 0;
end;

procedure TFormAgentLQ.cbAreaChange(Sender: TObject);
begin
  inherited;
  EdtStarTicketNum.Text := GetPrintCode;
end;

procedure TFormAgentLQ.SetAgentLQFormActive;
begin
  Self.BringToFront;
  EdtStarTicketNum.Text := GetPrintCode;
  UpdateStoreCountCaption(GlobalStoreCount);
end;

procedure TFormAgentLQ.WriteDisTicketHistory(AStartTicketNo: string; ATicketCount: Integer);
var
  sSqlStr: string;
begin
  sSqlStr := 'INSERT INTO TICKET_NUMBER_HISTORY(AGENTID, STARTPRINTCODE, TICKETCOUNT, OPERATEDATE, USERAREA)' +
             'VALUES(' + Trim(EdtAgentId.Text) + ','
                       + #39 + AStartTicketNo + #39 + ','
                       + IntToStr(ATicketCount) + ','
                       + #39 + FormatDateTime('YYYY-MM-DD hh:nn:ss', Now) + #39 + ','
                       + IntToStr(cbArea.ItemIndex) + ')';
  if not DMSystem.SQL_Exec(DMSystem.Qry_Temp, sSqlStr) then
    DMSystem.Qry_Temp.SQL.SaveToFile('TICKET_NUMBER_HISTORY_SQL.TXT');
end;

function TFormAgentLQ.isExistPrintCode(APrintCode: string): Boolean;
var
  sSqlStr: string;
begin
  sSqlStr := 'SELECT PRINTCODE FROM TICKET_NUMBER WHERE ISUSED = 0 AND PRINTCODE = ' + #39 + APrintCode + #39;
  Result := DMSystem.SQL_Querys(DMSystem.Qry_Temp, sSqlStr);
end;

procedure TFormAgentLQ.UpdateStoreCountCaption(AStorCount: Integer);
begin
  lblStoreCount.AutoSize := True;
  lblStoreCount.Caption := '(目前库存票数:' + IntToStr(AStorCount) + '张)';
  lblStoreCount.AutoSize := False;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -