unit U_DJBKKList;

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,
  cxPC, Math, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, Buttons, dxSkinsCore, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle,
  dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary,
  dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin,
  dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter;

type
  TfrmDJBKKList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    TBDel: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    Label3: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    SPName: TEdit;
    FactoryName: TEdit;
    SPID: TEdit;
    SPSpec: TEdit;
    Label6: TLabel;
    SPCF: TEdit;
    Label7: TLabel;
    OrderNo: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label12: TLabel;
    CRType: TComboBox;
    v1Column19: TcxGridDBColumn;
    Label11: TLabel;
    RCGangNo: TEdit;
    ADOQueryPrint: TADOQuery;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    Label16: TLabel;
    PBFactoryName: TEdit;
    ToolButton3: TToolButton;
    v1Column5: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Label9: TLabel;
    conno: TEdit;
    Label10: TLabel;
    SPColor: TEdit;
    v1Column25: TcxGridDBColumn;
    DYCS: TEdit;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    IsJYTime: TCheckBox;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    tbTSXG: TToolButton;
    cxgrdISHD: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    Label1: TLabel;
    WSTaiTou: TComboBox;
    v1Column29: TcxGridDBColumn;
    N3: TMenuItem;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    v1Column30: TcxGridDBColumn;
    Label4: TLabel;
    DJLockStr: TComboBox;
    v1Column31: TcxGridDBColumn;
    Label13: TLabel;
    XiaoPiType: TComboBox;
    Label14: TLabel;
    XiaoPiStr: TComboBox;
    v1Column32: TcxGridDBColumn;
    ToolButton8: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Label15: TLabel;
    YWY: TEdit;
    TBKW: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    PanCar: TPanel;
    PanZG: TPanel;
    ZGDate: TDateTimePicker;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    CarNo: TComboBox;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Tv1Column5: TcxGridDBColumn;
    Label17: TLabel;
    Label18: TLabel;
    DaPiType: TComboBox;
    DaPiStr: TComboBox;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    lblHJZPPS: TLabel;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure SPIDKeyPress(Sender: TObject; var Key: Char);
    procedure CRTypeChange(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure tbTSXGClick(Sender: TObject);
    procedure v1Column28PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure N3Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure TBKWClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure CheckBox3Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    procedure SetStatus();
    procedure InitGrid();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

//var
//  frmDJBRKList: TfrmDJBRKList;

implementation

uses
  U_DataLink, U_RTFun, U_Fun, U_DJBKKInPut, U_ZDYHelp, U_ZdyAttachGYS,
  U_ProductOrderListSel, U_SysLogHelp;

{$R *.dfm}

procedure TfrmDJBKKList.SetStatus();
begin
  TBAdd.Visible := False;
  TBDel.Visible := False;
  TBEdit.Visible := False;
  ToolButton1.Visible := False;
  ToolButton4.Visible := False;
  tbTSXG.Visible := False;
  cxgrdISHD.Visible := False;
  if Trim(canshu1) = '录入' then
  begin
    TBAdd.Visible := True;
    PanCar.Visible := True;
    TBKW.Visible := False;

  end;
  if Trim(canshu1) = '管理' then
  begin
    TBAdd.Visible := True;
    TBDel.Visible := True;
    TBEdit.Visible := True;
    ToolButton1.Visible := True;
    ToolButton4.Visible := True;
    cxgrdISHD.Visible := True;
    TBKW.Visible := True;
    PanCar.Visible := True;
    PanZG.Visible := True;
  end;
  if Trim(canshu1) = '高权限' then
  begin
//    TBAdd.Visible:=True;
//    TBDel.Visible:=True;
    TBEdit.Visible := True;
    tbTSXG.Visible := True;
    PanCar.Visible := True;
    PanZG.Visible := True;
  end;
  if Trim(canshu1) = '查询' then
  begin
    PanZG.Visible := True;
    ToolButton8.Visible := False;
    ToolButton5.Visible := False;
    ToolButton6.Visible := False;
    ToolButton7.Visible := False;
    TBKW.Visible := False;
  end;
  N3.Visible := False;
end;

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

procedure TfrmDJBKKList.FormCreate(Sender: TObject);
var
  fsj: string;
begin
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;
  ZGDate.Date := EndDate.Date;
  ZGDate.Checked := False;

  fsj := 'select distinct(WSTaiTou) name from CK_SXPB_CR   ';
  SInitComBoxBySql(ADOQueryCmd, WSTaiTou, False, fsj);
end;

procedure TfrmDJBKKList.InitGrid();
var
  fwhere, Pwhere, fsj: string;
begin
  fsj := ' 15:00:00';
  if IsJYTime.Checked then
  begin
    fwhere := ' where CRTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
    fwhere := fwhere + ' and CRTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  end
  else
    fwhere := ' where 1=1 ';
  Pwhere := SGetFilters(Panel1, 1, 2);
  if Pwhere <> '' then
    fwhere := fwhere + ' and ' + Pwhere;
  case cxTabControl1.TabIndex of
    0:
      begin
        fwhere := fwhere + ' and isnull(PrintCount,0)=0 '
      end;
    1:
      begin
        fwhere := fwhere + ' and isnull(PrintCount,0)>0 '
      end;
    2:
      begin
        fwhere := fwhere + ' and ISNULL(ZGDate,''2000-01-01'')<>''2000-01-01'' and ZGDate<GETDATE() and ISNULL(DJDate,''2000-01-01'')=''2000-01-01'' ' + ' and ISNULL(CarNo,'''')='''' ';

        //ShowMessage(fwhere);
      end;
    3:
      begin
        fwhere := fwhere + ' and ISNULL(ZGDate,''2000-01-01'')<>''2000-01-01'' and ZGDate<GETDATE() and ISNULL(DJDate,''2000-01-01'')=''2000-01-01'' ' + ' and ISNULL(CarNo,'''')<>''''  ';
      end;
  end;

  if CheckBox3.Checked then
  begin
    fwhere := fwhere + ' and isnull(ZPPS,0)=0 '
  end;
  if CheckBox4.Checked then
  begin
    fwhere := fwhere + ' and isnull(HGJFlag,'''')='''' '
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec P_Page_KaiKa ');
      sql.Add('@pageIndex=' + inttostr(CurrentPage));
      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
      sql.Add(',@criteria= ' + quotedstr(fwhere));
      //ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));

  finally
    ADOQueryMain.EnableControls;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Page_KaiKaTotal ');
    sql.Add('@criteria= ' + quotedstr(fwhere));
//    ShowMessage(SQL.Text);
    Open;
  end;
  lblHJZPPS.Caption := ADOQueryTemp.fieldbyname('RT').AsString;
end;

procedure TfrmDJBKKList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;

  CurrentPage := 1;
  InitGrid();
end;

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

procedure TfrmDJBKKList.TBCloseClick(Sender: TObject);
begin
  if Trim(DName) = 'ADMIN' then
  begin
    WriteCxGridAll(trim(self.Caption), Tv1, ADOQueryCmd, ADOQueryTemp);
  end;
  Close;
end;

procedure TfrmDJBKKList.FormShow(Sender: TObject);
begin
  CurrentPage := 1;
  RecordsNumber := 500;
  SetStatus();
  ReadCxGridAll(trim(self.Caption), Tv1);
  InitGrid();
end;

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

procedure TfrmDJBKKList.N1Click(Sender: TObject);
begin
  SelOKNoFilter(Tv1, True);
end;

procedure TfrmDJBKKList.N2Click(Sender: TObject);
begin
  SelOKNoFilter(Tv1, False);
end;

procedure TfrmDJBKKList.TBDelClick(Sender: TObject);
var
  strsql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY where APID=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已有打卷记录，不能删除!', '提示', 0);
    Exit;
  end;
  if Trim(CDS_Main.fieldbyname('SPID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;
      strsql := '卡号：' + Trim(CDS_Main.fieldbyname('SPID').AsString) + ' 订单号:' + Trim(CDS_Main.fieldbyname('ConNo').AsString);

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete CK_SXPB_CR where SPID=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
        sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
        sql.Add(' ' + quotedstr(trim(DName)));
        sql.Add(',getdate() ');
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(trim('删除开卡')));
        sql.Add(',' + quotedstr(trim(strsql)));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Update_XiaoPi :SubId');
        Parameters.ParamByName('SubId').Value := Trim(CDS_Main.fieldbyname('ORDSubIdRK').AsString);
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Main.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end
  else
  begin
    CDS_Main.Delete;
  end;
end;

procedure TfrmDJBKKList.TBAddClick(Sender: TObject);
begin
  try
    frmDJBKKInPut := TfrmDJBKKInPut.Create(Application);
    with frmDJBKKInPut do
    begin
      FBCId := '';
      if ShowModal = 1 then
      begin                                   
        Self.InitGrid();
      end;                                   
    end;
  finally
    frmDJBKKInPut.Free;                  
  end;
end;

procedure TfrmDJBKKList.TBEditClick(Sender: TObject);
var
  strsql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY where MJType=''正品'' and APID=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已有打卷记录，不能修改!', '提示', 0);
    Exit;
  end;

  strsql := '卡号：' + Trim(CDS_Main.fieldbyname('SPID').AsString) + ' 订单号:' + Trim(CDS_Main.fieldbyname('ConNo').AsString);

  with Self.ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
    sql.Add(' ' + quotedstr(trim(DName)));
    sql.Add(',getdate() ');
    sql.Add(',' + quotedstr(trim(self.Caption)));
    sql.Add(',' + quotedstr(trim('修改开卡')));
    sql.Add(',' + quotedstr(trim(strsql)));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    execsql;
  end;

  try
    frmDJBKKInPut := TfrmDJBKKInPut.Create(Application);
    with frmDJBKKInPut do
    begin
      FBCId := Trim(CDS_Main.fieldbyname('SPID').AsString);
      TBDel.Visible := False;
      TBAdd.Visible := False;
      canshu1 := Self.canshu1;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmDJBKKInPut.Free;
  end;
end;

procedure TfrmDJBKKList.ToolButton3Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  ExportFtErpFile('检验指示单标签.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\检验指示单标签.rmf';
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while CDS_Main.Locate('SSel', True, []) do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' update CK_SXPB_CR set PrintCount=isnull(PrintCount,0)+1 ');
        sql.Add(' where SPId=''' + Trim(CDS_Main.fieldbyname('SPId').AsString) + ''' ');
        ExecSQL;
      end;

      with ADOQueryPrint do
      begin
        Close;
        sql.Clear;
        sql.Add(' exec P_Print_JYZSDLab ');
        sql.Add(' @SPId=''' + Trim(CDS_Main.fieldbyname('SPId').AsString) + ''' ');
        Open;
      end;
      SCreateCDS20(ADOQueryPrint, CDS_PRT);
      SInitCDSData20(ADOQueryPrint, CDS_PRT);
      try
        Moudle := LoadLibrary('MakeQRBarcode.dll');
        @Makebar := GetProcAddress(Moudle, 'Make');
        @Mixtext := GetProcAddress(Moudle, 'MixText');
        Txt := Trim(CDS_PRT.fieldbyname('SPID').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
        CDS_Main.EnableControls;
        application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
        exit;
      end;

      if FileExists(fPrintFile) then
      begin
        RMVariables['QRBARCODE'] := fImagePath;
        RM1.LoadFromFile(fPrintFile);
//        RM1.ShowReport;
        RM1.DefaultCopies := StrToIntdef(DYCS.Text, 1);
        RM1.PrintReport;
      end
      else
      begin
        CDS_Main.EnableControls;
        Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
        Exit;
      end;
      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  InitGrid();
  CDS_Main.EnableControls;

end;

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

procedure TfrmDJBKKList.ToolButton1Click(Sender: TObject);
var
  MSPID, MYConNo, MYSubId, MXConNo, MXOrderNo, MXMainId, MXSubId, strsql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  MSPID := Trim(CDS_Main.fieldbyname('SPID').AsString);
  MYConNo := Trim(CDS_Main.fieldbyname('conno').AsString);
  MYSubId := Trim(CDS_Main.fieldbyname('ORDSubIdRK').AsString);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_BanCP_CR where CRFLAG=''出库'' and isnull(XSTYpe,'''')=''销售出库''  and APID=''' + Trim(MSPID) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已有销售出库记录，不能修改!', '提示', 0);
    Exit;
  end;

  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        MXMainId := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
        MXSubId := Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubId').AsString);
        MXOrderNo := Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
        MXConNo := Trim(frmProductOrderListSel.Order_Main.fieldbyname('conno').AsString);
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
  if Trim(MXMainId) = '' then
    Exit;
  strsql := '卡号：' + MSPID + ' 订单号:' + MYConNo + ' >>> ' + MXConNo + '';
  if Application.MessageBox(PChar('请确定：' + strsql), '提示', 32 + 4) <> IDYES then
    Exit;
  strsql := strsql + 'SubId:' + MYSubId + ' >>> ' + MXSubId;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update CK_SXPB_CR set ConNo=' + quotedstr(Trim(MXConNo)) + ',OrderNo=' + quotedstr(Trim(MXOrderNo)) + ',ORDMainIdRK=' + quotedstr(Trim(MXMainId)) + ',ORDSubIdRK=' + quotedstr(Trim(MXSubId)) + ' where SPID=' + quotedstr(Trim(MSPID)));
    sql.Add('update WFB_MJJY set MainId=X.ORDMainIdRK,SubId=X.ORDSubIdRK from CK_SXPB_CR X where WFB_MJJY.APID=X.SPID and X.SPID=' + quotedstr(Trim(MSPID)));
    sql.Add('update CK_BanCP_CR set MainId=X.ORDMainIdRK,SubId=X.ORDSubIdRK from CK_SXPB_CR X where CK_BanCP_CR.APID=X.SPID and X.SPID=' + quotedstr(Trim(MSPID)));
    sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
    sql.Add(' ' + quotedstr(trim(DName)));
    sql.Add(',getdate() ');
    sql.Add(',' + quotedstr(trim(self.Caption)));
    sql.Add(',' + quotedstr(trim('修改订单号')));
    sql.Add(',' + quotedstr(trim(strsql)));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    execsql;
  end;
//  InitGrid();
//  if not CDS_Main.IsEmpty then
//    CDS_Main.Locate('SPID', MSPID, []);
end;

procedure TfrmDJBKKList.ToolButton2Click(Sender: TObject);
begin
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
  //  facction:='生产指示单删除';
    showmodal;
    free;
  end;
end;

procedure TfrmDJBKKList.ToolButton4Click(Sender: TObject);
var
  MSPID, MYRCGangNo, MXRCGangNo, strsql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  MSPID := Trim(CDS_Main.fieldbyname('SPID').AsString);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select RCGangNo=');
    sql.Add('cast((select distinct '' '' + Mjstr4  from WFB_MJJY where  APID=' + quotedstr(Trim(MSPID)) + ' for xml path('''')) as varchar(60))');
//    showmessage(sql.text);
    Open;
  end;
  MYRCGangNo := Trim(ADOQueryTemp.fieldbyname('RCGangNo').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_BanCP_CR where CRFLAG=''出库'' and isnull(XSTYpe,'''')=''销售出库'' and APID=''' + Trim(MSPID) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已有销售出库记录，不能修改!', '提示', 0);
    Exit;
  end;
  if InputQuery('缸号', '请输入！', MXRCGangNo) then
  begin
    if Trim(MXRCGangNo) = '' then
      Exit;
    strsql := '卡号：' + MSPID + ' 缸号:' + MYRCGangNo + ' >>> ' + MXRCGangNo + '';
    if Application.MessageBox(PChar('请确定：' + strsql), '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update WFB_MJJY set Mjstr4=' + quotedstr(Trim(MXRCGangNo)) + ' where APID= ' + quotedstr(Trim(MSPID)));
      sql.Add('update CK_BanCP_CR set ganghao=' + quotedstr(Trim(MXRCGangNo)) + ' where APID= ' + quotedstr(Trim(MSPID)));
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('修改缸号')));
      sql.Add(',' + quotedstr(trim(strsql)));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
//    InitGrid();
//    if not CDS_Main.IsEmpty then
//      CDS_Main.Locate('SPID', MSPID, []);
  end;

end;

procedure TfrmDJBKKList.ToolButton5Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      while CDS_Main.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' exec P_UPdate_LCKJY ''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
          execsql;
        end;
        CDS_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_Main.EnableControls;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_Main.EnableControls;
    Application.MessageBox('删除异常!', '提示', 0);
  end;
  InitGrid();
end;

procedure TfrmDJBKKList.SPIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmDJBKKList.CRTypeChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmDJBKKList.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmDJBKKList.BTNPClick(Sender: TObject);
begin
  if CurrentPage < CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmDJBKKList.tbTSXGClick(Sender: TObject);
var
  strsql: string;
begin
  if CDS_Main.IsEmpty then
    Exit;

  strsql := '特殊卡号：' + Trim(CDS_Main.fieldbyname('SPID').AsString) + ' 订单号:' + Trim(CDS_Main.fieldbyname('ConNo').AsString);

  with Self.ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
    sql.Add(' ' + quotedstr(trim(DName)));
    sql.Add(',getdate() ');
    sql.Add(',' + quotedstr(trim(self.Caption)));
    sql.Add(',' + quotedstr(trim('特殊修改开卡')));
    sql.Add(',' + quotedstr(trim(strsql)));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    execsql;
  end;

  try
    frmDJBKKInPut := TfrmDJBKKInPut.Create(Application);
    with frmDJBKKInPut do
    begin
      XGQX := '特殊修改';
      FBCId := Trim(CDS_Main.fieldbyname('SPID').AsString);
      TBDel.Visible := False;
      TBAdd.Visible := False;
      canshu1 := Self.canshu1;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmDJBKKInPut.Free;
  end;
end;

procedure TfrmDJBKKList.v1Column28PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

 { mvalue := TcxCheckBox(Sender).EditValue;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    if mvalue = 'False' then
      sql.Add('update CK_SXPB_CR set ISHD=0 where SPID=' + quotedstr(Trim(CDS_Main.fieldbyname('SPID').AsString)))
    else
      sql.Add('update CK_SXPB_CR set ISHD=1 where SPID=' + quotedstr(Trim(CDS_Main.fieldbyname('SPID').AsString)));

    sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
    sql.Add(' ' + quotedstr(trim(DName)));
    sql.Add(',getdate() ');
    sql.Add(',' + quotedstr(trim(self.Caption)));
    sql.Add(',' + quotedstr(trim('修改核对开卡')));
    sql.Add(',' + quotedstr(Trim(CDS_Main.fieldbyname('SPID').AsString)));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    execsql;
  end; }
end;

procedure TfrmDJBKKList.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if cxTabControl1.TabIndex <> 1 then
  begin
    N3.Visible := True;
    cxgrdISHD.Options.Editing := False;
  end
  else
  begin
    if canshu1 = '管理' then
    begin
      N3.Visible := True;
      cxgrdISHD.Options.Editing := True;
    end
    else
    begin
      N3.Visible := False;
      cxgrdISHD.Options.Editing := False;
    end;

  end;
end;

procedure TfrmDJBKKList.N3Click(Sender: TObject);
begin

  {with CDS_Main do
  begin
    first;
    while not Eof do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;

        sql.Add('update CK_SXPB_CR set ISHD=1 where SPID=' + quotedstr(Trim(CDS_Main.fieldbyname('SPID').AsString)));

        sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
        sql.Add(' ' + quotedstr(trim(DName)));
        sql.Add(',getdate() ');
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(trim('修改核对开卡')));
        sql.Add(',' + quotedstr(Trim(CDS_Main.fieldbyname('SPID').AsString)));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;
      Next;
    end;
  end; }

end;

procedure TfrmDJBKKList.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked then
  begin
    FactoryName.Tag := 1;
  end
  else
  begin
    FactoryName.Tag := 2;
  end;
  InitGrid();
end;

procedure TfrmDJBKKList.CheckBox2Click(Sender: TObject);
var
  FTag, i: Integer;
begin

  if CheckBox2.Checked then
  begin
    FTag := 1;
  end
  else
  begin
    FTag := 2;
  end;

  with Panel1 do
  begin
    for i := 0 to ControlCount - 1 do
    begin
      if Controls[i] is TLabel then
        continue;

      if Controls[i] is TEdit then
      begin

        TEdit(Controls[i]).Tag := FTag;

      end
      else if Controls[i] is TComboBox then
      begin

        TComboBox(Controls[i]).Tag := FTag;
      end;
    end;
  end;
  InitGrid();

end;

procedure TfrmDJBKKList.ToolButton6Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while CDS_Main.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' update CK_SXPB_CR set DJLockFlag=1,DJLockTime=getdate(),DJLockPerson=''' + Trim(DName) + '''');
          sql.Add(' where SPId=''' + Trim(CDS_Main.fieldbyname('SPId').AsString) + ''' ');
          ExecSQL;
        end;
        Edit;
        FieldByName('SSel').Value := False;
        FieldByName('DJLockFlag').Value := True;
        Post;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;

end;

procedure TfrmDJBKKList.ToolButton7Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while CDS_Main.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' update CK_SXPB_CR set DJLockFlag=0,DJLockTime=getdate(),DJLockPerson=''' + Trim(DName) + '''');
          sql.Add(' where SPId=''' + Trim(CDS_Main.fieldbyname('SPId').AsString) + ''' ');
          ExecSQL;
        end;
        Edit;
        FieldByName('SSel').Value := False;
        FieldByName('DJLockFlag').Value := false;
        Post;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmDJBKKList.ToolButton8Click(Sender: TObject);
var
  FAPID, FSubId, FMainId, FYY: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if Application.MessageBox('确定要整缸布次品进行撤销吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  FAPID := Trim(CDS_Main.fieldbyname('SPID').AsString);
  FMainId := Trim(CDS_Main.fieldbyname('ORDMainIdRK').AsString);
  FSubId := Trim(CDS_Main.fieldbyname('ORDSubIdRK').AsString);
  FYY := Trim(CDS_Main.fieldbyname('CiPinNote').AsString);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update CK_SXPB_CR set SPType=''正品'',CiPinNote=Null,GangCD=Null where SPID=' + quotedstr(Trim(FAPID)));
    sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
    sql.Add(' ' + quotedstr(trim(DName)));
    sql.Add(',getdate() ');
    sql.Add(',' + quotedstr(trim('开卡管理')));
    sql.Add(',' + quotedstr(trim('整缸次品撤销')));
    sql.Add(',' + quotedstr(trim('卡号：' + FAPID)));
    sql.Add(',' + quotedstr(trim(' 成功')));
    sql.Add(')');
    execsql;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    SQL.Add('Update JYorder_Sub Set CPNoteS1=Null,CPNoteS2=Null where Subid=''' + Trim(FSubId) + '''');
    SQL.Add('Update JYorder_Main Set CPNoteM1=Null,CPNoteM2=Null where Mainid=''' + Trim(FMainId) + '''');
    ExecSQL;
  end;
  if Trim(FYY) = '整缸次品,不能发货' then
  begin

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('Update JYorder_Sub Set CPNoteS1=dbo.F_Get_Order_SubStr(''' + Trim(FSubId) + ''',''CPNoteS1'')');
      sql.Add(' where subid=''' + Trim(FSubId) + '''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('Update JYorder_Main Set CPNoteM1=dbo.F_Get_Order_SubStr(''' + Trim(FMainId) + ''',''CPNoteM1'')');
      sql.Add(' where Mainid=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
  end
  else if Trim(FYY) = '整缸布大部分很差，将就发货' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('Update JYorder_Sub Set CPNoteS2=dbo.F_Get_Order_SubStr(''' + Trim(FSubId) + ''',''CPNoteS2'')');
      sql.Add(' where subid=''' + Trim(FSubId) + '''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('Update JYorder_Main Set CPNoteM2=dbo.F_Get_Order_SubStr(''' + Trim(FMainId) + ''',''CPNoteM2'')');
      sql.Add(' where Mainid=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
  end;
  Application.MessageBox('撤销成功!', '提示', 0);
end;

procedure TfrmDJBKKList.TBKWClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'KWName';
      flagname := '库位';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDJBKKList.BitBtn1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if ZGDate.Checked = True then
  begin
    if Application.MessageBox('确定要设定为当前选择的装柜日期吗？', '提示', 32 + 4) <> IDYES then
      Exit;
  end
  else
  begin
    if Application.MessageBox('确定要置空装柜日期吗？', '提示', 32 + 4) <> IDYES then
      Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while CDS_Main.Locate('SSel', True, []) do
      begin
        if ZGDate.Checked = False then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' update CK_SXPB_CR set ZGDate=Null,ZGSetTime=getdate(),ZGSetPerson=''' + Trim(DName) + '''');
            sql.Add(' where SPId=''' + Trim(CDS_Main.fieldbyname('SPId').AsString) + ''' ');
            ExecSQL;
          end;
          Edit;
          FieldByName('SSel').Value := False;
          FieldByName('ZGDate').Value := Null;
          Post;
        end
        else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' update CK_SXPB_CR set ZGDate=''' + Trim(FormatDateTime('yyyy-MM-dd', ZGDate.Date)) + ''',ZGSetTime=getdate(),ZGSetPerson=''' + Trim(DName) + '''');
            sql.Add(' where SPId=''' + Trim(CDS_Main.fieldbyname('SPId').AsString) + ''' ');
            ExecSQL;
          end;
          Edit;
          FieldByName('SSel').Value := False;
          FieldByName('ZGDate').Value := ZGDate.Date;
          Post;
        end;

      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmDJBKKList.BitBtn2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;

  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Trim(CarNo.Text) <> '' then
  begin
    if Application.MessageBox('确定要指定为当前机台吗？', '提示', 32 + 4) <> IDYES then
      Exit;
  end
  else
  begin
    if Application.MessageBox('确定要置空机台吗？', '提示', 32 + 4) <> IDYES then
      Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while CDS_Main.Locate('SSel', True, []) do
      begin
        if Trim(CarNo.Text) = '' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' update CK_SXPB_CR set CarNo=Null,CarSetTime=getdate(),CarSetPerson=''' + Trim(DName) + '''');
            sql.Add(' where SPId=''' + Trim(CDS_Main.fieldbyname('SPId').AsString) + ''' ');
            ExecSQL;
          end;
          Edit;
          FieldByName('SSel').Value := False;
          FieldByName('CarNo').Value := Null;
          Post;
        end
        else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' update CK_SXPB_CR set CarNo=''' + Trim(CarNo.Text) + ''',CarSetTime=getdate(),CarSetPerson=''' + Trim(DName) + '''');
            sql.Add(' where SPId=''' + Trim(CDS_Main.fieldbyname('SPId').AsString) + ''' ');
            ExecSQL;
          end;
          Edit;
          FieldByName('SSel').Value := False;
          FieldByName('CarNo').Value := Trim(CarNo.Text);
          Post;
        end;

      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmDJBKKList.CheckBox3Click(Sender: TObject);
begin
  BegDate.SetFocus;
//  BegDate.Date := EndDate.Date - 30;
  CurrentPage := 1;
  InitGrid();
end;

end.

