unit U_SXCKOutList_SH;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridDBTableView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxDropDownEdit, cxCheckBox, RM_Common,
  RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, Menus,
  cxCalendar, cxButtonEdit, cxTextEdit, cxPC;

type
  TfrmSXCKOutList_SH = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    SPName: TEdit;
    FactoryName: TEdit;
    SPID: TEdit;
    SPSpec: TEdit;
    CRType: TComboBox;
    SPCF: TEdit;
    QCorderNo: TEdit;
    Label10: TLabel;
    CKorderNo: TEdit;
    Label11: TLabel;
    FromFactoryName: TEdit;
    Label13: TLabel;
    ToFactoryName: TEdit;
    Label14: TLabel;
    SPCode: TEdit;
    v1Column20: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1ConNoHZCK: TcxGridDBColumn;
    Label9: TLabel;
    Label15: TLabel;
    ConNoHZ: TEdit;
    ConNoHZCK: TEdit;
    v1Filler: TcxGridDBColumn;
    Label16: TLabel;
    PRTColor: TEdit;
    v1CKName: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure SPNameChange(Sender: TObject);
    procedure SPIDChange(Sender: TObject);
    procedure SPIDKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    canshu1,canshu2:string;
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmSXCKOutList_SH: TfrmSXCKOutList_SH;

implementation
uses
  U_DataLink,U_RTFun,U_SXCKOutPut,U_SXCKTHPut;

{$R *.dfm}

procedure TfrmSXCKOutList_SH.FormDestroy(Sender: TObject);
begin
  frmSXCKOutList_SH:=nil;
end;

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

procedure TfrmSXCKOutList_SH.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
end;

procedure TfrmSXCKOutList_SH.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ,B.PRTColorNo,B.PRTColor,B.PRTHX');
      sql.Add(',ConNoHZ=[dbo].[F_Get_Order_SubStr](A.ORDMainIdRK,''ConNoHZ'') ');
      sql.Add(',ConNoHZCK=[dbo].[F_Get_Order_SubStr](A.ORDMainIdCK,''ConNoHZ'') ');
      sql.Add(' from CK_SXPB_CR A');
      sql.Add(' left join JYOrder_Sub B on A.ORDSubIdCK=B.SubId');
      sql.add(' left join CK_SXPB_CR C on C.SPID=A.FZSPID');
      sql.Add(' where A.CRTime>=:begdate and A.CRTime<:enddate');
      SQL.Add(' and A.CRFlag=''出库''  ');
      if cxtabcontrol1.TabIndex=0 then
      sql.add(' and A.SHStatus=''需审核''');
      if cxtabcontrol1.TabIndex=1 then
      sql.add(' and A.SHStatus=''已审核''');
      sql.add(' and isnull(A.SHStatus,'''')<>''''');
      if Trim(canshu1)<>'高权限' then
      begin
        sql.Add(' and A.Filler='''+Trim(DName)+'''');
      end;
      sql.add(' and isnull(C.MCType,'''')='''' ');
      SQL.Add(' order by A.CRTime desc');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1));
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

procedure TfrmSXCKOutList_SH.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  end;
end;

procedure TfrmSXCKOutList_SH.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('坯布出库列表FM',Tv1,'坯布仓库');
  Close;
end;

procedure TfrmSXCKOutList_SH.FormShow(Sender: TObject);
begin
  
  ReadCxGrid('坯布出库列表FM',Tv1,'坯布仓库');
  //InitGrid();
end;

procedure TfrmSXCKOutList_SH.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('坯布出库列表',cxGrid2);
end;

procedure TfrmSXCKOutList_SH.TBFindClick(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 TfrmSXCKOutList_SH.SPNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmSXCKOutList_SH.SPIDChange(Sender: TObject);
begin
  if Length(Trim(SPID.Text))<4 then
  begin
    if Trim(SPID.Text)<>'' then Exit;
  end;
  TBFind.Click;
end;

procedure TfrmSXCKOutList_SH.SPIDKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    {if Length(Trim(SPID.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select A.* ');
        sql.Add(' from CK_SXPB_CR A');
        sql.add(' where SPID like :SXID');
        Parameters.ParamByName('SXID').Value:='%'+Trim(SPID.Text)+'%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end; }
  end;
end;

procedure TfrmSXCKOutList_SH.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

procedure TfrmSXCKOutList_SH.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,False);
end;

procedure TfrmSXCKOutList_SH.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if Trim(CDS_Main.fieldbyname('SPID').AsString)<>'' then
  begin
    if Trim(CDS_Main.fieldbyname('CRType').AsString)='平移出库' then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from CK_SXPB_CR where FZSPID=');
        sql.Add('(select SPID from CK_SXPB_CR where PYID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
        sql.Add(' and isnull(CRType,'''')=''平移入库'')');
        sql.Add(' and CRQtyFlag=-1');
        Open;
      end;
      if ADOQueryTemp.IsEmpty=False then
      begin
        Application.MessageBox('已有出库记录，不能删除!','提示',0);
        Exit;
      end;
    end;
    with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where YFTypeId='''+trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
        open;
      end;
      if  ADOQueryCmd.IsEmpty=false then
      begin
        application.MessageBox('已产生费用不能操作','提示');
        exit;
      end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete CK_SXPB_CR where SPID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
        sql.Add(' Update CK_SXPB_CR Set MXKCKgQty=(select sum(isnull(KGQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.add(',MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(' where SPID='''+Trim(CDS_Main.fieldbyname('FZSPID').AsString)+'''');
        if Trim(CDS_Main.fieldbyname('CRType').AsString)='平移出库' then
        begin
          sql.Add(' delete CK_SXPB_CR where isnull(PYID,'''')='''+Trim(CDS_Main.fieldbyname('PYID').AsString)+'''');
        end;
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
    end;
  end;
  CDS_Main.Delete;
end;

procedure TfrmSXCKOutList_SH.SPSpecChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmSXCKOutList_SH.TBAddClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  if application.MessageBox('确定要审核吗','提示',1)=2 then exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update CK_SXPB_CR set SHStatus=''已审核'' ');
    sql.add('where SPID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+''' ');
    open;
  end;
  CDS_Main.Delete;
end;

procedure TfrmSXCKOutList_SH.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then exit;
  if application.MessageBox('确定要审核吗','提示',1)=2 then exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update CK_SXPB_CR set SHStatus=''需审核'' ');
    sql.add('where SPID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+''' ');
    open;
  end;
  CDS_Main.Delete;
end;

procedure TfrmSXCKOutList_SH.cxTabControl1Change(Sender: TObject);
begin
  TBAdd.Visible:=false;
  TBEdit.Visible:=false;
  if cxTabcontrol1.TabIndex=0 then
  TBAdd.Visible:=true;
  if cxTabcontrol1.TabIndex=1 then
  TBEdit.Visible:=true;
  initgrid();
end;

end.
