unit U_CPManage_LY;

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, RM_BarCode,
  cxButtonEdit, cxTextEdit,jpeg,U_SLT,Math, cxPC,MMSystem;

type
  TfrmCPManage_LY = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    DataSource1: TDataSource;
    ADOQueryTree: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Panel1: TPanel;
    Label2: TLabel;
    MJID: TEdit;
    ToolButton2: TToolButton;
    Label1: TLabel;
    CYName: TEdit;
    Label4: TLabel;
    CYEName: TEdit;
    Label5: TLabel;
    CYSpec: TEdit;
    DataSource2: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_Main: TClientDataSet;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    SaveDialog1: TSaveDialog;
    Label3: TLabel;
    CYNO: TEdit;
    Panel2: TPanel;
    DSCYNO: TDataSource;
    CDS_CYNO: TClientDataSet;
    Label7: TLabel;
    CYKZ: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    CYCF: TEdit;
    CYECF: TEdit;
    Label10: TLabel;
    CYMF: TEdit;
    Label11: TLabel;
    CYHX: TEdit;
    Label12: TLabel;
    DefStr2: TEdit;
    RMBarCodeObject1: TRMBarCodeObject;
    MianLiao: TEdit;
    Label13: TLabel;
    defstr1: TEdit;
    Label14: TLabel;
    Panel3: TPanel;
    adoqueryPicture: TADOQuery;
    defstr10: TEdit;
    Label15: TLabel;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    RMDB_Sub: TRMDBDataSet;
    RM2: TRMGridReport;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1CYNo: TcxGridDBColumn;
    v1CPName: TcxGridDBColumn;
    v1DefStr2: TcxGridDBColumn;
    v1DefStr10: TcxGridDBColumn;
    v1CYName: TcxGridDBColumn;
    v1CYEName: TcxGridDBColumn;
    v1CYCF: TcxGridDBColumn;
    v1CYECF: TcxGridDBColumn;
    v1CYSpec: TcxGridDBColumn;
    v1DefStr6: TcxGridDBColumn;
    v1CYMF: TcxGridDBColumn;
    v1CYKZ: TcxGridDBColumn;
    v1CYHX: TcxGridDBColumn;
    v1CYColor: TcxGridDBColumn;
    v1CYSeHao: TcxGridDBColumn;
    v1DefStr7: TcxGridDBColumn;
    v1CYPrice12: TcxGridDBColumn;
    v1CYPrice3: TcxGridDBColumn;
    v1CYPrice5: TcxGridDBColumn;
    v1CYPrice15: TcxGridDBColumn;
    v1CYPrice: TcxGridDBColumn;
    v1DefStr11: TcxGridDBColumn;
    v1CYNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1XYPlace: TcxGridDBColumn;
    YP_FZ: TClientDataSet;
    DataSource4: TDataSource;
    v1isly: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    cxTabControl1: TcxTabControl;
    TYP: TToolButton;
    v1OrdPerson2: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1ConNO: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CYNoChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure CYSpecChange(Sender: TObject);
    procedure CYMFChange(Sender: TObject);
    procedure CYNameChange(Sender: TObject);
    procedure CYCFChange(Sender: TObject);
    procedure DefStr2Change(Sender: TObject);
    procedure MJIDKeyPress(Sender: TObject; var Key: Char);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure MJIDExit(Sender: TObject);
    procedure TYPClick(Sender: TObject);


  private
    { Private declarations }
    CPID:string;
    PState:Integer;
    FCPID,FTopID:String;
    procedure InitGrid();

  public
    { Public declarations }
  end;

var
  frmCPManage_LY: TfrmCPManage_LY;
  Mach: array of TfrmSlt;

implementation
uses
  U_DataLink,U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmCPManage_LY.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    SQL.Clear;
    SQL.add('	select * ');
    sql.add(',OrdPerson2=(select Top 1 B.OrdPerson2 from JYOrder_Main B where B.MPRTCode=A.CYNo order by B.Filltime)');
    sql.add(',CustomerNoName=(select Top 1 B.CustomerNoName from JYOrder_Main B where B.MPRTCode=A.CYNo order by B.Filltime)');
    sql.add(',ConNO=(select Top 1 B.ConNO from JYOrder_Main B where B.MPRTCode=A.CYNo order by B.Filltime)');
    sql.Add(' from CP_YDang A');
	  SQL.add(' where exists(select X.MainId from WFB_MJJY X left join JYOrder_Main Y on X.MainId=Y.MainId where Y.MPRTCode=A.CYNo)');
    if cxTabControl1.TabIndex <2 then
    SQL.Add(' and A.isly='''+inttostr(cxTabControl1.TabIndex)+''' ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDS_Main);
  SInitCDSData20(ADOQueryMain,CDS_Main);
end;

procedure TfrmCPManage_LY.FormDestroy(Sender: TObject);
begin
  frmCPManage_LY:=nil;
end;

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

procedure TfrmCPManage_LY.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('样品列表1',Tv1,'样品管理');
  Close;
end;
procedure TfrmCPManage_LY.FormShow(Sender: TObject);
begin
  ReadCxGrid('样品列表1',Tv1,'样品管理');
  InitGrid();
end;

procedure TfrmCPManage_LY.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_LY.CYNoChange(Sender: TObject);
begin
  if Length(Trim(TEdit(Sender).Text))<4 then Exit;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_LY.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel('样品档案',cxGrid1);
end;

procedure TfrmCPManage_LY.ToolButton9Click(Sender: TObject);
begin
  if YP_FZ.IsEmpty then exit;
  YP_FZ.Delete;
end;

procedure TfrmCPManage_LY.CYSpecChange(Sender: TObject);
begin
  if Length(Trim(CYSpec.Text))<3 then Exit;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_LY.CYMFChange(Sender: TObject);
begin
  if Length(Trim(TEdit(Sender).Text))<2 then Exit;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_LY.CYNameChange(Sender: TObject);
begin
  if Length(Trim(TEdit(Sender).Text))<2 then Exit;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_LY.CYCFChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_LY.DefStr2Change(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCPManage_LY.MJIDKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
  begin
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('	update  CP_YDang SET isLY=1 ');
        SQL.Add('where exists(select X.MainId from WFB_MJJY X inner join JYOrder_Main Y on X.MainId=Y.MainId where Y.MPRTCode=CP_YDang.CYNo and X.MJID='+quotedstr(Trim(MJID.Text))+')');
        ExecSQL;
      end;
       ADOQueryCmd.Connection.CommitTrans;
      IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\正确.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\正确.wav'),0, SND_ASYNC);
      Exit;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      IF FileExists(PChar(ExtractFilePath(Application.ExeName)+'wav\错误.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName)+'wav\错误.wav'),0, SND_ASYNC);
    end
  end;
end;

procedure TfrmCPManage_LY.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCPManage_LY.ToolButton3Click(Sender: TObject);
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('	update  CP_YDang SET isLY=1 ');
      SQL.Add('where CYNO='+quotedstr(Trim(CDS_Main.Fieldbyname('CYNO').AsString)));
      ExecSQL;
    end;
    CDS_Main.Delete;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('留样成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('留样失败！','提示',0);
  end
end;

procedure TfrmCPManage_LY.ToolButton4Click(Sender: TObject);
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('	update  CP_YDang SET isLY=0 ');
      SQL.Add('where CYNO='+quotedstr(Trim(CDS_Main.Fieldbyname('CYNO').AsString)));
      ExecSQL;
    end;
    CDS_Main.Delete;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('撤销留样成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销留样失败！','提示',0);
  end
end;

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

procedure TfrmCPManage_LY.MJIDExit(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCPManage_LY.TYPClick(Sender: TObject);
var
  fPrintFile,FFCYID,FFCLRID:string;
  DPS,i,j:Integer;
  Txt,fImagePath:string;
  Moudle: THandle;
  Makebar:TMakebar;
  Mixtext:TMixtext;
begin
  ToolBar1.SetFocus;
  if CDS_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\样品标签.rmf';
  CDS_Main.DisableControls;
  DPS:=0;
  FFCYID:='';
  i:=1;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.FieldByName('SSel').AsBoolean=True then
      begin
        try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(CDS_Main.fieldbyname('CYID').AsString);
          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
          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;
          end else
          begin
            Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\样品标签.rmf'),'提示',0);
            Exit;
          end;
        end;
        if DPS=0 then
        begin
          FFCYID:=Trim(CDS_Main.fieldbyname('CYID').AsString);
        end;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
end;

end.
