unit U_JTList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP,ShellAPI,IniFiles, cxCheckBox, cxCalendar,
  cxButtonEdit, cxTextEdit, cxPC, cxCheckComboBox, cxDropDownEdit, Menus,
  RM_e_Xls, TeEngine, Series, TeeProcs, Chart, DbChart;

type
  TfrmJTList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2ssel: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel1: TPanel;
    Label5: TLabel;
    ZdyName: TEdit;
    RMXLSExport2: TRMXLSExport;
    RMDBMain: TRMDBDataSet;
    v2Column8: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Edit1: TEdit;
    ADOQueryPrt: TADOQuery;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure CustomerChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure v2Column8PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure PrintReport(FZDYNo:string);
    function  SaveData():Boolean;
  public
    fFlag:integer;
    { Public declarations }
    RKFlag,FCYID,fmanage:String;

  end;

var
  frmJTList: TfrmJTList;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}
procedure TfrmJTList.PrintReport(FZDYNo:string);
var
   fPrintFile,FFCYID:string;
   i,j:Integer;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if CDS_HZ.IsEmpty then Exit;

  if Trim(Edit1.Text)<>'' then
  begin
    if TryStrToInt(Edit1.Text,j)=False then
    begin
      Application.MessageBox('份数录入错误!','提示',0);
      exit;
    end;
  end;

  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\机台标签.rmf';
  with ADOQueryPrt do
  begin
    close;
    sql.Clear;
    sql.Add(' select top 1 * from  KH_ZDY where ZdyName='''+Trim(FZDYNo)+'''');
    open;
  end;
  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=Trim(FZDYNo);
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
    CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
    if FileExists(fImagePath) then DeleteFile(fImagePath);
    Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
    CDS_HZ.EnableControls;
    application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
    exit;
  end;
  RMVariables['QRBARCODE']:=fImagePath;
  for i:=1 to j do
  begin
    if FileExists(fPrintFile) then
    begin
      RM1.LoadFromFile(fPrintFile);
      RM1.PrintReport;
    //  RM1.ShowReport;
    end
    else
    begin
      Application.MessageBox(PChar('没有找'+fPrintFile+'!'),'提示',0);
      Exit;
    end;
  end;
end;
procedure TfrmJTList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add('select A.*,ZJM=dbo.getPinYin(A.ZdyName) from KH_ZDY A where A.Type=''CarNo''');
      Open;
    end;

    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmJTList.FormDestroy(Sender: TObject);
begin
  frmJTList:=nil;
end;

procedure TfrmJTList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmJTList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.caption),Tv2,'库位管理');
  Close;
end;
procedure TfrmJTList.FormShow(Sender: TObject);
begin
  ReadCxGrid(trim(self.caption),Tv2,'库位管理');

  InitGrid();
end;

procedure TfrmJTList.TBRafreshClick(Sender: TObject);
begin
  ZdyName.SetFocus;
  InitGrid();
end;

procedure TfrmJTList.ToolButton2Click(Sender: TObject);
var
  sql:string;
begin
  if ADOQueryMain.Active then
  begin
    sql:=SGetFilters(Panel1,1,2);
    SDofilter(ADOQueryMain,sql);
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
    if fmanage='查询' then
    begin
      with CDS_HZ do
      begin
        DisableControls;
         while not Eof do
         begin
           Edit;
           FieldByName('FactoryName').Value:='';
           Post;
           Next;
         end;
         EnableControls;
      end;  
    end;
  end;
end;

function TfrmJTList.SaveData():Boolean;
var
  maxId,CRID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'SY','KH_ZDY',3,1)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
     with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('insert into KH_ZDY(ZDYNo,Type)');
      sql.Add('values('
      +quotedstr(Trim(maxId))
      +',''KuWei'')');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;

procedure TfrmJTList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid;
end;

procedure TfrmJTList.N1Click(Sender: TObject);
begin
  IF  CDS_HZ.IsEmpty then exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=true;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmJTList.N2Click(Sender: TObject);
begin
 IF  CDS_HZ.IsEmpty then exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=false;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmJTList.CustomerChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmJTList.FormCreate(Sender: TObject);
begin
  fmanage:=Trim(DParameters1);
end;

procedure TfrmJTList.ToolButton3Click(Sender: TObject);

begin
  while  CDS_HZ.Locate('SSel',true,[]) do
  begin
    PrintReport(Trim(CDS_HZ.fieldbyname('ZdyName').AsString));
    CDS_HZ.Edit;
    CDS_HZ.FieldByName('SSel').Value:=false;
  end;

end;

procedure TfrmJTList.v2Column8PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value:=Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate KH_Zdy ');
      sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
      SQL.Add(' where ZDYNo='''+Trim(CDS_HZ.fieldbyname('ZDYNo').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('select * from KH_Zdy where Type=''KuWei''');
      sql.Add(' and ZdyName='''+Trim(CDS_HZ.fieldbyname('ZdyName').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      if ADOQueryTemp.RecordCount>1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        CDS_HZ.EnableControls;
        Application.MessageBox('名称重复！','提示',0);
        Exit;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();
  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end
end;

end.
