unit U_DJBRKDDList;

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, BtnEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmDJBRKDDList = 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;
    TBDel: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    SPName: TEdit;
    SPID: TEdit;
    Label7: TLabel;
    OrderNo: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label9: TLabel;
    ToFactoryName: TEdit;
    Label12: TLabel;
    CRType: TComboBox;
    Label10: TLabel;
    KuWei: TEdit;
    Label13: TLabel;
    SPColor: TEdit;
    v1Column20: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    TBPrint1: TToolButton;
    begTime: TDateTimePicker;
    endTime: TDateTimePicker;
    v1Column22: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column32: TcxGridDBColumn;
    Cmb_BZ: TComboBox;
    v1Column34: TcxGridDBColumn;
    Label14: TLabel;
    QCorderNo: TEdit;
    ToolButton2: TToolButton;
    Label15: TLabel;
    note: TEdit;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    edtnote: TEdit;
    FactoryName: TEdit;
    Label6: TLabel;
    SPColorNo: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Label8: TLabel;
    ZNote: TEdit;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure SPIDKeyPress(Sender: TObject; var Key: Char);
    procedure SPIDChange(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure TBPrint1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column12PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    canshu1, canshu2: string;
    procedure InitGrid(); 
    { Private declarations }
  public
    fckName: string;
    { Public declarations }
  end;

var
  frmDJBRKDDList: TfrmDJBRKDDList;

implementation

uses
  U_DataLink, U_RTFun, U_Fun, U_DJBCKInPut, U_DJBDDRK_Edit, U_ZdyAttachGYS;

{$R *.dfm}

procedure TfrmDJBRKDDList.FormDestroy(Sender: TObject);
begin
  frmDJBRKDDList := nil;
end;

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

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

procedure TfrmDJBRKDDList.InitGrid();
var
  mBegdate, mEnddate, fsj: string;
begin
  mBegdate := FormatDateTime('yyyy-MM-dd', begdate.Date) + ' ' + FormatDateTime('HH:nn', begTime.Time);
  mEnddate := FormatDateTime('yyyy-MM-dd', enddate.Date) + ' ' + FormatDateTime('HH:nn', endTime.Time);

//  fsj := 'select distinct(FactoryName) name from CK_XNMD_CR A  ';
//
//  fsj := fsj + ' where A.CRTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.Date) + ''' ';
//  fsj := fsj + ' and A.CRTime<''' + FormatDateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ';
//
//  SInitComBoxBySql(ADOQueryTemp, FactoryName, False, fsj);
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add(' select A.*  ');
      sql.Add(' from CK_XNMD_CR A');
      sql.add(' where A.CRTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.add(' and A.CRTime<''' + FormatDateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      SQL.Add(' and CRFlag=''入库''    ');
      SQL.Add('  order by  [dbo].[getNum](GangNo)      ');
      Open;
   //   ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
    tbfind.Click;
  end;
end;

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

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

procedure TfrmDJBRKDDList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('待检布入库列表1', Tv1, '待检布仓库');
  Close;
end;

procedure TfrmDJBRKDDList.FormShow(Sender: TObject);
begin

  ReadCxGrid('待检布入库列表1', Tv1, '待检布仓库');
  InitGrid();
end;

procedure TfrmDJBRKDDList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('待检布入库列表', cxGrid2);
end;

procedure TfrmDJBRKDDList.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 TfrmDJBRKDDList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

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

procedure TfrmDJBRKDDList.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select APID from CK_BanCP_CR where isnull(APID,'''')=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
    Open;
  end;
  if not ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('已入库不能删除数据!', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_money_CR where YFTypeId=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已有财务记录，不能删除!', '提示', 0);
    Exit;
  end;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      while CDS_Main.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete CK_XNMD_CR where SPID=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
          sql.Add('delete from MD_MJJY where  not exists(select SPID from CK_XNMD_CR X where X.SPID=MD_MJJY.APID)');
          if Trim(CDS_Main.fieldbyname('CRType').AsString) = '客户退货' then
          begin
            sql.Add('delete from YF_Money_CR ');
            sql.Add(' where mainID=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
          end;
          ExecSQL;
        end;
        CDS_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除异常!', '提示', 0);
  end;

end;

procedure TfrmDJBRKDDList.TBAddClick(Sender: TObject);
begin

  try
    frmDJBDDRK_Edit := TfrmDJBDDRK_Edit.Create(Application);
    with frmDJBDDRK_Edit do
    begin
      fkeyNo := '';
      fckName := self.fckName;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmDJBDDRK_Edit.Free;
  end;
end;

procedure TfrmDJBRKDDList.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if Trim(CDS_Main.fieldbyname('CRType').AsString) = '平移入库' then
  begin
    Application.MessageBox('平移入库的数据为自动生成，不能修改!', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select APID from CK_BanCP_CR where isnull(APID,'''')=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
    Open;
  end;
  if not ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('已入库不能修改数据!', '提示', 0);
    Exit;
  end;
 { try
    frmDJBCKInPut:=TfrmDJBCKInPut.Create(Application);
    with frmDJBCKInPut do
    begin
      FBCId:=Trim(CDS_Main.fieldbyname('SPID').AsString);
      TBDel.Visible:=False;
      TBAdd.Visible:=False;
      fckName:=self.fckName;
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmDJBCKInPut.Free;
  end; }
  try
    frmDJBDDRK_Edit := TfrmDJBDDRK_Edit.Create(Application);
    with frmDJBDDRK_Edit do
    begin
      fkeyNo := Trim(self.CDS_Main.fieldbyname('SPID').AsString);
      fckName := self.fckName;
      TBDel.Visible := False;
      TBAdd.Visible := False;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmDJBDDRK_Edit.Free;
  end;
end;

procedure TfrmDJBRKDDList.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(',ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId)');
        //sql.Add(' ,ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId )');
        sql.Add(' from CK_XNMD_CR A');
        sql.add(' where SPID like :SXID');
        Parameters.ParamByName('SXID').Value:='%'+Trim(SXID.Text)+'%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end; }
  end;
end;

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

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

procedure TfrmDJBRKDDList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmDJBRKDDList.TBPrint1Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if CDS_Main.IsEmpty then
    EXIT;
  if CDS_Main.Locate('SSel', True, []) = false then
  begin
    Application.MessageBox('请选择数据', '提示', 0);
    EXIT;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TbForBatch where DName=' + quotedstr(Trim(DName)));
    SQL.Add('and MainType=' + quotedstr('码单打印'));
    ExecSQL;
  end;
  while CDS_Main.Locate('SSel', True, []) do
  begin
    with Self.ADOQueryCmd do                                                //WFB_MJJY
    begin
      Close;
      sql.Clear;
      sql.Add(' insert into TbForBatch(DName,MainType,Value1) ');
      sql.Add(' values( ' + quotedstr(Trim(DName)) + ',' + quotedstr('码单打印') + ',' + quotedstr(Trim(Self.CDS_Main.FieldByName('SPID').AsString)) + ')');
      ExecSQL;
    end;
    with CDS_Main do
    begin
      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
    Next;
  end;
  ExportFtErpFile(trim(Cmb_BZ.Text) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(Cmb_BZ.Text) + '.rmf';
  if Cmb_BZ.Text = '出库码单1' then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD11 ' + quotedstr(Trim(DName)));
      Open;
    end;
  end;
  if Cmb_BZ.Text = '出库码单2' then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD22 ' + quotedstr(Trim(DName)));
      Open;
    end;
  end;
  if Cmb_BZ.Text = '出库码单3' then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD33 ' + quotedstr(Trim(DName)));
      Open;
    end;
  end;
  if Cmb_BZ.Text = '出库码单4' then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD44 ' + quotedstr(Trim(DName)));
      Open;
    end;
  end;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmDJBRKDDList.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  try
    frmDJBDDRK_Edit := TfrmDJBDDRK_Edit.Create(Application);
    with frmDJBDDRK_Edit do
    begin
      fkeyNo := Trim(self.CDS_Main.fieldbyname('SPID').AsString);
      fckName := self.fckName;
      TBDel.Visible := False;
      TBAdd.Visible := False;
      TBsave.Visible := false;
      Tv2.OptionsData.Editing := false;
      Tv2.OptionsData.Deleting := false;
      tv2.OnCellDblClick := nil;
      Tv1.OptionsData.Editing := false;
      Label3.Visible := false;
      Label5.Visible := false;
      QtyMX.Visible := false;

      if ShowModal = 1 then
      begin
       // Self.InitGrid();
      end;
    end;
  finally
    frmDJBDDRK_Edit.Free;
  end;
end;

procedure TfrmDJBRKDDList.ToolButton2Click(Sender: TObject);
begin
  try
    frmDJBDDRK_Edit := TfrmDJBDDRK_Edit.Create(Application);
    with frmDJBDDRK_Edit do
    begin
      CopyFlag := 1;
      fkeyNo := Trim(self.CDS_Main.fieldbyname('SPID').AsString);
      fckName := self.fckName;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmDJBDDRK_Edit.Free;
  end;
end;

procedure TfrmDJBRKDDList.v1Column12PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' update CK_XNMD_CR set ' + FFieldName + ' = ' + QuotedStr(mvalue));
    sql.Add(' where  SPID= ' + quotedstr(Trim(self.CDS_Main.fieldbyname('SPID').AsString)));
    ExecSQL;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmDJBRKDDList.ToolButton4Click(Sender: TObject);
var
  fPrintFile: string;
  WSql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  WSql := '';
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while CDS_Main.Locate('SSel', True, []) do
    begin
      if WSql <> '' then
        WSql := WSql + ',' + QuotedStr(Trim(CDS_Main.fieldbyname('SPId').AsString))
      else
        WSql := 'where SPID in (' + QuotedStr(Trim(CDS_Main.fieldbyname('SPId').AsString));
      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  CDS_Main.EnableControls;
  WSql := WSql + ')';
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' update CK_XNMD_CR set note=' + QuotedStr(trim(edtnote.Text)));
      sql.Add(WSql);
//      ShowMessage(SQL.Text);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除异常!', '提示', 0);
  end;
  InitGrid();
end;

end.

