unit U_YPInList;

interface

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

type
  TfrmYPInList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label7: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CYNo: TEdit;
    CYColor: TEdit;
    CDS_Main: TClientDataSet;
    ADOQueryHC: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1P_ChnName: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Quantity: TcxGridDBColumn;
    v1UnitName: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column20: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    v1Column21: TcxGridDBColumn;
    Panel2: TPanel;
    Edit1: TEdit;
    Label5: TLabel;
    v1Column25: TcxGridDBColumn;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ToolButton2: TToolButton;
    Panel4: TPanel;
    Label13: TLabel;
    LabName: TBtnEditA;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    CYType: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure CYNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure LabNameBtnClick(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure SaveChkData();
    procedure SaveChkNoData();
    procedure ReadINIFile();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmYPInList: TfrmYPInList;

implementation
uses
  U_DataLink,U_RTFun,U_YPCRIn,U_FileUp, U_ZDYHelp;

{$R *.dfm}

procedure TfrmYPInList.FormDestroy(Sender: TObject);
begin
  frmYPInList:=nil;
end;
procedure TfrmYPInList.ReadINIFile();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;

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

procedure TfrmYPInList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
end;

procedure TfrmYPInList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('样品入库列表',Tv1,'样品仓库');
end;

procedure TfrmYPInList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select  A.CRTime,A.CRID,A.BCID,A.Qty,A.QtyUnit,A.CYColor,A.Note,A.CYType,A.BCID ');
      sql.Add(' ,A.CRPrice,A.BZTYpe,A.PBFactory,A.RCFactory,A.HJGFactory,A.CYID,A.WeiZhi,A.MiLenNote');
      SQL.Add(',B.CYNO,B.CYName,B.CYMF,B.CYKZ,B.CYSpec,B.CYCF,B.CYHX,B.CYEName,B.CYECF,B.CYColorEng');
      sql.Add(',MLen=dbo.F_Get_MLen(A.CYID),C.KCQty,A.CYID ');
      SQL.Add(' from CK_YP_CR A inner join CP_YDang B on A.CYID=B.CYID ');
      sql.Add(' inner join CK_YP_KC C on A.CRID=C.CRID');
      sql.Add(' where CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
      SQL.Add(' and CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
      sql.Add(' and CRFlag=''入库'' ');
      if CheckBox1.Checked=False then
      begin
        sql.Add(' and KCQty>0');
      end;
     { sql.Add(' order by ');
      sql.Add(' substring(A.WeiZhi,1,1),Cast(substring(A.WeiZhi,2,charindex(''-'',A.WeiZhi)-2) as Int)');
      sql.Add(',Cast(substring(A.WeiZhi,charindex(''-'',A.WeiZhi)+1,len(A.WeiZhi)-charindex(''-'',A.WeiZhi)) as Int)');}
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYPInList.InitForm();
begin
  ReadCxGrid('样品入库列表',Tv1,'样品仓库');

  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-365;
  InitGrid();
end;

procedure TfrmYPInList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,CDS_Main);
  SInitCDSData20(ADOQueryMain,CDS_Main);
end;

procedure TfrmYPInList.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  try
    frmYPCRIn:=TfrmYPCRIn.Create(Application);
    with frmYPCRIn do
    begin
      FMainId:=Trim(self.CDS_Main.fieldbyname('BCId').AsString);
      ToolButton1.Visible:=False;
      ToolButton2.Visible:=False;
      ToolButton3.Visible:=False;
      ToolButton4.Visible:=False;
      if ShowModal=1 then
      begin
        Self.InitGrid();
        Self.TBFind.Click;
        Self.CDS_Main.Locate('BCId',FMainID,[]);
      end;
    end;
  finally
    frmYPCRIn.Free;
  end;
end;

procedure TfrmYPInList.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    CDS_Main.Delete;
  end;
end;

function TfrmYPInList.DelData():Boolean;
var
  FInt:Integer;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CK_YP_CR where BCId='''+Trim(CDS_Main.fieldbyname('BCId').AsString)+'''');
      sql.Add('UPdate CK_YP_KC Set KCQty=KCQty-'+Trim(CDS_Main.fieldbyname('Qty').AsString));
      sql.Add(' where CRID='+Trim(CDS_Main.fieldbyname('CRID').AsString));
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_YP_KC  ');
      sql.Add(' where CRID='+Trim(CDS_Main.fieldbyname('CRID').AsString));
      Open;
    end;
    if ADOQueryTemp.FieldByName('KCQty').Value<0 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('已有出库数据!','提示',0);
      Exit;
    end;
    {with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate YP_SQ Set NowOutFlag=0 where SQID= ');
      sql.Add(' (select Top 1 SQID from YP_SQ A where A.CYNO='''+Trim(CDS_Main.fieldbyname('CYNO').AsString)+'''');
      sql.Add('  and A.CYColor='''+Trim(CDS_Main.fieldbyname('CYColor').AsString)+'''');
      SQL.Add('  and A.YWY='''+Trim(CDS_Main.fieldbyname('YWY').AsString)+'''');
      sql.Add('  and SQTime<='''+Trim(CDS_Main.fieldbyname('CRTime').AsString)+'''');
      sql.Add('  and A.SQType=''调样申请'' and A.ChkStatus=''审核通过'' ');
      SQL.Add('  order by SQTime desc)');
      ExecSQL;
    end;   }
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmYPInList.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel('样品入库列表',cxGrid1);
end;

procedure TfrmYPInList.TBAddClick(Sender: TObject);
begin
  try
    frmYPCRIn:=TfrmYPCRIn.Create(Application);
    with frmYPCRIn do
    begin
      FMainID:='';
      if ShowModal=1 then
      begin
        Self.InitGrid();
        Self.TBFind.Click;
        Self.CDS_Main.Locate('BCId',FMainID,[]);
      end;
    end;
  finally
    frmYPCRIn.Free;
  end;
end;

procedure TfrmYPInList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;


procedure TfrmYPInList.CYNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,CDS_Main);
  SInitCDSData20(ADOQueryMain,CDS_Main);
end;

procedure TfrmYPInList.FormShow(Sender: TObject);
begin
  InitForm();
  if Trim(DParameters1)='查看' then
  begin
    TBAdd.Visible:=False;
    TBEdit.Visible:=False;
    TBDel.Visible:=False;
  end;
end;
procedure TfrmYPInList.SaveChkData();
var
  FCRID:String;
  PState:Integer;
begin
  PState:=0;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YP_SQ_KC');
    sql.Add(' where CYNO='''+Trim(CDS_Main.fieldbyname('CYNO').AsString)+'''');
    sql.Add(' and CYColor='''+Trim(CDS_Main.fieldbyname('CYColor').AsString)+'''');
    sql.Add(' and YWY='''+Trim(CDS_Main.fieldbyname('YWY').AsString)+'''');
    sql.Add(' and SQQtyUnit='''+Trim(CDS_Main.fieldbyname('QtyUnit').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update CK_YP_CRID Set CRID=CRID+1 select * from CK_YP_CRID');
      ExecSQL;
      Open;
    end;
    FCRID:=Trim(ADOQueryCmd.fieldbyname('CRID').AsString);
  end else
  begin
    FCRID:=Trim(ADOQueryTemp.fieldbyname('CRID').AsString);
    PState:=1;
  end;
  if PState=0 then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into YP_SQ_KC(CRID,CYNO,CYColor,YWY,SQQtyUnit,KCValid)');
      sql.Add(' select '+FCRID);
      sql.Add(','''+Trim(CDS_Main.fieldbyname('CYNO').AsString)+'''');
      sql.Add(','''+Trim(CDS_Main.fieldbyname('CYColor').AsString)+'''');
      sql.Add(','''+Trim(CDS_Main.fieldbyname('YWY').AsString)+'''');
      sql.Add(','''+Trim(CDS_Main.fieldbyname('QtyUnit').AsString)+'''');
      sql.Add(',''Y'' ');
      ExecSQL;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YP_SQ_KC Set SQQty=SQQty+'+Trim(CDS_Main.fieldbyname('Qty').AsString));
    SQL.Add(' where CRID='+FCRID);
    sql.Add('UPdate YP_SQ Set ChkStatus=''审核通过'' ');
    sql.Add(',Chker='''+Trim(DName)+'''');
    sql.Add(',ChkTime=:ChkTime');
    sql.Add(',CRID='+FCRID);
    sql.Add(' where SQID='''+Trim(CDS_Main.fieldbyname('SQID').AsString)+'''');
    Parameters.ParamByName('ChkTime').Value:=SGetServerDateTime(ADOQueryTemp);
    ExecSQL;
  end;
end;
procedure TfrmYPInList.SaveChkNoData();
var
  FCRID:String;
begin
  if Trim(CDS_Main.FieldByName('ChkStatus').AsString)='审核通过' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YP_SQ_KC');
      sql.Add(' where CYNO='''+Trim(CDS_Main.fieldbyname('CYNO').AsString)+'''');
      sql.Add(' and CYColor='''+Trim(CDS_Main.fieldbyname('CYColor').AsString)+'''');
      sql.Add(' and YWY='''+Trim(CDS_Main.fieldbyname('YWY').AsString)+'''');
      sql.Add(' and SQQtyUnit='''+Trim(CDS_Main.fieldbyname('QtyUnit').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
    end else
    begin
      FCRID:=Trim(ADOQueryTemp.fieldbyname('CRID').AsString);
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YP_SQ_KC Set SQQty=SQQty-'+Trim(CDS_Main.fieldbyname('Qty').AsString));
        SQL.Add(' where CRID='+FCRID);
        ExecSQL;
      end;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate YP_SQ Set ChkStatus=''审核不通过'' ');
    sql.Add(',Chker='''+Trim(DName)+'''');
    sql.Add(',ChkTime=:ChkTime');
    sql.Add(' where SQID='''+Trim(CDS_Main.fieldbyname('SQID').AsString)+'''');
    Parameters.ParamByName('ChkTime').Value:=SGetServerDateTime(ADOQueryTemp);
    ExecSQL;
  end;


end;

procedure TfrmYPInList.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  try
    frmYPCRIn:=TfrmYPCRIn.Create(Application);
    with frmYPCRIn do
    begin
      FMainId:=Trim(self.CDS_Main.fieldbyname('BCId').AsString);
      //ToolButton1.Visible:=False;
      //ToolButton2.Visible:=False;
      //ToolButton3.Visible:=False;
      //ToolButton4.Visible:=False;
      FCopyInt:=99;
      if ShowModal=1 then
      begin
        Self.InitGrid();
        Self.TBFind.Click;
        Self.CDS_Main.Locate('BCId',FMainID,[]);
      end;
    end;
  finally
    frmYPCRIn.Free;
  end;
end;

procedure TfrmYPInList.TBPrintClick(Sender: TObject);
var
   fPrintFile,FFCYID:string;
   DPS,i,j:Integer;
begin
  CYNo.SetFocus;
  if CDS_Main.IsEmpty then Exit;
  //if CDS_CYNO.IsEmpty then Exit;
  if Trim(LabName.Text)='' then
  begin
    Application.MessageBox('标签名称不能为空!','提示',0);
    Exit;
  end;
  if Trim(Edit1.Text)<>'' then
  begin
    if TryStrToInt(Edit1.Text,i)=False then
    begin
      Application.MessageBox('份数录入错误!','提示',0);
      exit;
    end;
  end;

  fPrintFile:= ExtractFilePath(Application.ExeName) +'Report\'+Trim(LabName.Text)+'.rmf';
  CDS_Main.DisableControls;
  DPS:=0;
  FFCYID:='';
  i:=1;
  if Trim(Edit1.Text)='' then
  begin
    j:=1;
  end else
  begin
    j:=StrToInt(Edit1.Text);
  end;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.FieldByName('SSel').AsBoolean=True then
      begin
        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\'+Trim(LabName.Text)+'.rmf'),'提示',0);
            Exit;
          end;
        end;

        if DPS=0 then
        begin
          FFCYID:=Trim(CDS_Main.fieldbyname('CYID').AsString);
        end;
      end;
      //CDS_Main.Locate('CYID',Trim(CDS_CYNO.fieldbyname('CYID').AsString),[]);

      Next;
    end;
  end;
  CDS_Main.EnableControls;
  CDS_Main.Locate('CYID',FFCYID,[]);
  Edit1.Text:='1';

end;

procedure TfrmYPInList.ToolButton2Click(Sender: TObject);
var
   fPrintFile:string;
begin
  if CDS_Main.IsEmpty then Exit;
  if Trim(LabName.Text)='' then
  begin
    Application.MessageBox('标签名称不能为空!','提示',0);
    Exit;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(LabName.Text)+'.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\'+Trim(LabName.Text)+'.rmf'),'提示',0);
  end;
end;


procedure TfrmYPInList.LabNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YPLabName';
      flagname:='样品标签';
      fnote:=True;
      TBAdd.Visible:=False;
      TBEdit.Visible:=False;
      TBDel.Visible:=False;
      if ShowModal=1 then
      begin
        Self.LabName.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
