unit U_QryPrtDyeInfoList;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ToolWin, cxGraphics,
  cxControls, cxLookAndFeels,
  cxContainer, cxEdit, dxLayoutcxEditAdapters, dxBarBuiltInMenu, cxPC,
  dxLayoutContainer, cxClasses, cxTextEdit, dxLayoutControl, cxStyles,
  cxCustomData, cxFilter, cxData, cxDataStorage, cxNavigator, dxDateRanges,
  dxScrollbarAnnotations, Data.DB, cxDBData, cxCalendar, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridCustomView,
  cxGrid, U_BaseList, Data.Win.ADODB, cxGridCustomPopupMenu, cxGridPopupMenu,
  Datasnap.DBClient, cxCheckBox, cxButtonEdit, cxSplitter, dxCore, cxDateUtils,
  cxMaskEdit, cxDropDownEdit,  cxProgressBar, Vcl.Menus,
  RM_Common, RM_Class, RM_GridReport, RM_Dataset, cxLookAndFeelPainters,
  Vcl.ExtCtrls, dxSkinsCore, dxSkinsDefaultPainters, dxLayoutControlAdapters,
  FrameDateSel10, Vcl.StdCtrls, MovePanel, cxCurrencyEdit, U_WindowFormdesign,
  BtnEdit, dxSkinWXI;

type
  TfrmQryPrtDyeInfoList = class(TfrmBaseList)
    ToolBar3: TToolBar;
    Trefresh: TToolButton;
    TsaveGrid: TToolButton;
    Tclose: TToolButton;
    DataSource1: TDataSource;
    ADOQueryList: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    dxLayoutControl_query: TdxLayoutControl;
    dxLayoutControl_queryGroup_Root: TdxLayoutGroup;
    CDS_List: TClientDataSet;
    cxProgressBar1: TMovePanel;
    ADOQueryCmd: TADOQuery;
    TBADD: TToolButton;
    TBdel: TToolButton;
    TBedit: TToolButton;
    TBexcpet: TToolButton;
    TgridSet: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    P_Code: TcxTextEdit;
    dxLayoutItem1: TdxLayoutItem;
    P_Name: TcxTextEdit;
    dxLayoutItem2: TdxLayoutItem;
    P_Spec: TcxTextEdit;
    dxLayoutItem3: TdxLayoutItem;
    ADOQueryTemp: TADOQuery;
    procedure TrefreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TcloseClick(Sender: TObject);
    procedure TsaveGridClick(Sender: TObject);
    procedure dxLayoutControl_queryDblClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBADDClick(Sender: TObject);
    procedure TBdelClick(Sender: TObject);
    procedure TBeditClick(Sender: TObject);
    procedure TV1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure TBexcpetClick(Sender: TObject);
    procedure TgridSetClick(Sender: TObject);
    procedure Tv1Column10PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column6PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column7PropertiesChange(Sender: TObject);
    procedure Tv1Column9PropertiesEditValueChanged(Sender: TObject);
    procedure P_CodeKeyPress(Sender: TObject; var Key: Char);

  private
    fWindowDesign: TWindowFormdesign;
    fDesignCode:string;
    procedure initGrid();
    procedure initForm();
  protected
  public
    canshu1, FSTKName, canshu3: string;
  end;

var
  frmQryPrtDyeInfoList: TfrmQryPrtDyeInfoList;

implementation
uses
  U_RTFun, U_globalVar, U_dataLink,U_FormLayOutDesign,
  U_LabelPrint,U_cxGridCustomSet, U_PrtDyeInfoInPut, U_ZDYHelp;
{$R *.dfm}

procedure TfrmQryPrtDyeInfoList.TsaveGridClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, gDllFileCaption);
  if gIsCanDesign then
  begin
    saveLayOut(application, dxLayoutControl_query, ADOQueryCmd,PWideChar( fDllFileName + '|' + Self.Name + '|' + dxLayoutControl_query.Name + '.ini'));
  end;
end;

procedure TfrmQryPrtDyeInfoList.Tv1Column10PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_List do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate BS_Product_Info ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' where bpiid=' + QuotedStr(CDS_List.fieldbyname('bpiid').AsString));

      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv1.Controller.EditingController.ShowEdit();
  except
    tv1.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmQryPrtDyeInfoList.TV1Column3PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  with CDS_List do
  begin
    Edit;
    FieldByName('PH').Value := Trim(mvalue);
    Post;
  end;

  try
    with AdoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update Dye_Formula_Sample_Sub SET PH=''' + trim(mvalue) + ''' where DFSMID=''' + trim(CDS_List.fieldbyName('DFSMID').AsString) + '''');
      sql.Add('and fsNo=''' + trim(CDS_List.fieldbyName('fsNo').AsString) + '''');
      execsql;
    end;
  except

  end;

  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmQryPrtDyeInfoList.Tv1Column6PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_List do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate BS_Product_Info ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' where bpiid=' + QuotedStr(CDS_List.fieldbyname('bpiid').AsString));

      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv1.Controller.EditingController.ShowEdit();
  except
    tv1.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmQryPrtDyeInfoList.Tv1Column7PropertiesChange(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_List do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate BS_Product_Info ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' where bpiid=' + QuotedStr(CDS_List.fieldbyname('bpiid').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('bpiid：' + trim(CDS_List.FieldByName('bpiid').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv1.Controller.EditingController.ShowEdit();
  except
    tv1.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmQryPrtDyeInfoList.Tv1Column9PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_List do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate BS_Product_Info ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' where bpiid=' + QuotedStr(CDS_List.fieldbyname('bpiid').AsString));

      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv1.Controller.EditingController.ShowEdit();
  except
    tv1.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmQryPrtDyeInfoList.TBADDClick(Sender: TObject);
begin
  try
    frmPrtDyeInfoInPut := TfrmPrtDyeInfoInPut.Create(Application);
    with frmPrtDyeInfoInPut do
    begin
      FBCId := '';
      FSTKName := self.FSTKName;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmPrtDyeInfoInPut.Free;
  end;
end;

procedure TfrmQryPrtDyeInfoList.TBdelClick(Sender: TObject);
begin
  if CDS_List.IsEmpty then
    Exit;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from BS_Product_IO where P_Code=''' + Trim(CDS_List.fieldbyname('P_Code').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已入库不能修改!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' delete BS_Product_Info where BPIID=''' + Trim(CDS_List.fieldbyname('BPIID').AsString) + '''');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除异常!', '提示', 0);
  end;

  CDS_List.Delete;

end;

procedure TfrmQryPrtDyeInfoList.TBeditClick(Sender: TObject);
begin
  if CDS_List.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('UPDATE Dye_Formula_Large_Sub SET BPIID=(SELECT BPIID FROM Bs_Product_info e where e.p_name=Dye_Formula_Large_Sub.p_name)');
    sql.Add('where p_name=' + quotedstr(CDS_List.fieldbyname('P_NAME').AsString));
    sql.Add('UPDATE Dye_Formula_Pick_Sub SET BPIID=(SELECT BPIID FROM Bs_Product_info e where e.p_name=Dye_Formula_Pick_Sub.p_name)');
    sql.Add('where p_name=' + quotedstr(CDS_List.fieldbyname('P_NAME').AsString));
    ExecSQL;
  end;
  try
    frmPrtDyeInfoInPut := TfrmPrtDyeInfoInPut.Create(Application);
    with frmPrtDyeInfoInPut do
    begin
      FBCId := Trim(CDS_List.fieldbyname('BPIID').AsString);
      FSTKName := self.FSTKName;
      TBDel.Visible := False;
      TBAdd.Visible := False;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmPrtDyeInfoInPut.Free;
  end;
end;

procedure TfrmQryPrtDyeInfoList.TBexcpetClick(Sender: TObject);
begin
  if CDS_List.IsEmpty then
    exit;
  TcxGridToExcel(self.Caption, cxGrid2);
end;

procedure TfrmQryPrtDyeInfoList.TcloseClick(Sender: TObject);
begin
   close;
end;

procedure TfrmQryPrtDyeInfoList.TgridSetClick(Sender: TObject);
begin
  fWindowDesign.OpenGridDesignWin10(fDesignCode,'cxgrid2',tv1);
end;

procedure TfrmQryPrtDyeInfoList.TrefreshClick(Sender: TObject);
begin
  inherited;
  initGrid();
end;

procedure TfrmQryPrtDyeInfoList.cxTabControl1Change(Sender: TObject);
begin
  inherited;
  initGrid();
end;

procedure TfrmQryPrtDyeInfoList.dxLayoutControl_queryDblClick(
  Sender: TObject);
begin
  layoutDesign(TdxLayoutControl(Sender),ADOQueryCmd,PWideChar(dcode));
end;

procedure TfrmQryPrtDyeInfoList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action:=cafree;
end;

procedure TfrmQryPrtDyeInfoList.FormCreate(Sender: TObject);
begin
  inherited;
  cxgrid2.Align:=alClient;
  canshu1 := Trim(Self.fParameters1);
  FSTKName := Trim(Self.fParameters2);
  canshu3 := Trim(Self.fParameters3);
end;

procedure TfrmQryPrtDyeInfoList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmQryPrtDyeInfoList := nil;
end;

procedure TfrmQryPrtDyeInfoList.FormShow(Sender: TObject);
begin
  inherited;
  InitForm();
end;

procedure TfrmQryPrtDyeInfoList.InitForm();
begin
  addQryContionByLay(ADOQueryBaseTemp,fformId,'cxgrid2',dxLayoutControl_query,7);
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, gDllFileCaption);
  if (canshu1 = '查询') or (canshu1 = '编号录入') then
  begin
    TBAdd.Visible := False;
    TBEdit.Visible := False;
    TBDel.Visible := False;
    v1Column9.Visible := False;
    v1Column9.Hidden := true;

  end
  else
  begin
    v1Column9.Visible := true;
    v1Column9.Hidden := false;
  end;
  if canshu1 = '编号录入' then
  begin
    Tv1Column6.Options.Editing := True;
    Tv1Column8.Options.Editing := True;
  end
  else
  begin
    Tv1Column6.Options.Editing := false;
    Tv1Column8.Options.Editing := false;
  end;
  InitGrid();
end;

procedure TfrmQryPrtDyeInfoList.InitGrid();
var
  mSqlWhere: string;
begin
  mSqlWhere := SLGetFilters(dxLayoutControl_query, 1, 2);
  if trim(mSqlWhere) <> '' then
  begin
    mSqlWhere := ' and ' + trim(mSqlWhere);
  end;
  try
    ADOQueryList.DisableControls;
    with ADOQueryList do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' from BS_Product_Info A');
      sql.Add(' where isnull(STKNAME,'''')=''' + Trim(FSTKName) + '''');
      sql.Add(Trim(mSqlWhere));
      Open;
    end;
    SCreateCDS(ADOQueryList, CDS_List);
    SInitCDSData(ADOQueryList, CDS_List);
  finally
    ADOQueryList.EnableControls;
  end;
end;
procedure TfrmQryPrtDyeInfoList.P_CodeKeyPress(Sender: TObject; var Key: Char);
begin
  inherited;
  if key = #13 then
  begin
    initGrid();
  end;
end;

end.
