unit U_DyeCardGlideEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, BtnEdit, ComCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ToolWin,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGrid, ADODB, DBClient,
  cxCalendar, cxContainer, cxTextEdit, cxCurrencyEdit, cxDropDownEdit,
  cxCheckBox, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, U_BaseInput, System.ImageList,
  Vcl.ImgList, dxBarBuiltInMenu, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxButtonEdit, Vcl.Menus, dxScrollbarAnnotations;

type
  TfrmDyeCardGlideEdit = class(TfrmBaseInput)
    CDS_Glide: TClientDataSet;
    DS_Glide: TDataSource;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    ADO_Car: TADOQuery;
    ADO_Glide: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    Panel1: TPanel;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBGX: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    v2PSName: TcxGridDBColumn;
    v2PSNote: TcxGridDBColumn;
    v2SYRName: TcxGridDBColumn;
    vLDPrice: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    CDS_Card: TClientDataSet;
    ToolButton1: TToolButton;
    ToolButton6: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure TBGXClick(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    fOrdType: string;
    procedure InitGrid1();
    procedure InitCard();
    procedure InitGlide();
    { Private declarations }
  public
    FWorkshop, FPCIDS, FNAME: string;
    { Public declarations }
  end;

var
  frmDyeCardGlideEdit: TfrmDyeCardGlideEdit;

implementation

uses
  U_DataLink, U_RTFun, U_DyeTechnicsSel;
{$R *.dfm}

procedure TfrmDyeCardGlideEdit.InitGrid1();
begin
  with ADOQueryTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select *  from Dye_Process ');
    Open;
  end;
  SCreateCDS(ADOQueryTmp, CDS_1);
  SInitCDSData(ADOQueryTmp, CDS_1);
end;

procedure TfrmDyeCardGlideEdit.InitGlide();
begin
  try
    with Ado_Glide do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*');
      sql.Add('from Dye_Plan_Glide A ');
      sql.Add('where A.PCID=''' + trim(CDS_Card.fieldbyname('RTValue').AsString) + ''' ');
      open;
    end;
    SCreateCDS(Ado_Glide, CDS_Glide);
    SInitCDSData(Ado_Glide, CDS_Glide);
  finally
  end;
end;

procedure TfrmDyeCardGlideEdit.InitCard();
begin
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from [dbo].[F_Tool_SplitString](' + quotedstr(FPCIDS) + ','';'')  A ');
      open;
    end;
    SCreateCDS(ADOQueryTmp, CDS_Card);
    SInitCDSData(ADOQueryTmp, CDS_Card);
  finally
  end;
end;

procedure TfrmDyeCardGlideEdit.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyeCardGlideEdit := nil;
end;

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

procedure TfrmDyeCardGlideEdit.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption + 'TV1', Tv1, '生产指示单管理');
  WriteCxGrid(self.Caption + 'TV2', Tv2, '生产指示单管理');
end;

procedure TfrmDyeCardGlideEdit.ToolButton2Click(Sender: TObject);
begin
  with CDS_Glide do
  begin
    if IsEmpty then
      Exit;
    if RecNo >= 1 then
    begin
      //Prior;
      Edit;
      fieldByName('GlideNo').Value := fieldByName('GlideNo').Value - 1;
      Post;
      Prior;
      Edit;
      fieldByName('GlideNo').Value := fieldByName('GlideNo').Value + 1;
      Post;
    end;
    Prior;
  end;
end;

procedure TfrmDyeCardGlideEdit.ToolButton3Click(Sender: TObject);
begin
  CDS_Glide.IndexFieldNames := 'GlideNo';
  with CDS_Glide do
  begin
    if IsEmpty then
      Exit;
    Next;
    if not Eof then
      Prior
    else
    begin
      exit;
    end;
    if not Eof then
    begin
      Edit;
      fieldByName('GlideNo').Value := fieldByName('GlideNo').Value + 1;
      Post;
      Prior;
      Edit;
      fieldByName('GlideNo').Value := fieldByName('GlideNo').Value - 1;
      Post;
    end;
    Next;
  end;
end;

procedure TfrmDyeCardGlideEdit.ToolButton4Click(Sender: TObject);
var
  k: integer;
begin
  if cds_Glide.IsEmpty then
    exit;
  cds_Glide.Delete;
  cds_Glide.IndexFieldNames := 'GlideNo';
  cds_Glide.DisableControls;
  cds_Glide.First;
  k := 1;
  while not cds_Glide.Eof do
  begin
    cds_Glide.Edit;
    cds_Glide.FieldByName('GlideNo').Value := k;
    cds_Glide.Post;
    inc(k);
    cds_Glide.Next;
  end;
  cds_Glide.First;
  cds_Glide.EnableControls;
end;

procedure TfrmDyeCardGlideEdit.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption + 'TV1', Tv1, '生产指示单管理');
  ReadCxGrid(self.Caption + 'TV2', Tv2, '生产指示单管理');
  InitGrid1();
  InitCard();
  InitGlide();

end;

procedure TfrmDyeCardGlideEdit.ToolButton5Click(Sender: TObject);
var
  maxID: string;
begin
  if CDS_Glide.IsEmpty then
    exit;
  if CDS_Card.IsEmpty then
    exit;
  try
    CDS_Glide.DisableControls;
    with CDS_Card do
    begin
      CDS_Card.First;
      while not eof do
      begin
        try
          ADOQueryCmd.Connection.BeginTrans;
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('delete from Dye_Plan_Glide ');
            sql.Add('where PCID=''' + trim(CDS_Card.fieldbyname('RTValue').asstring) + '''');
            execsql;
          end;

          CDS_Glide.First;
          while not CDS_Glide.Eof do
          begin
            if trim(CDS_Glide.FieldByName('GlideID').asstring) = '' then
            begin
              if GetLSNo(ADOQueryCmd, maxID, 'CG', 'Dye_Plan_Glide', 4, 1) = False then
                raise Exception.Create(PChar('取最大号失败!'));
            end;
            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
              sql.Add('select * from Dye_Plan_Glide A where 1=2');
              open;

              append;
              fieldbyname('Filler').Value := trim(DName);
              FieldByName('GlideID').Value := trim(maxID);
              FieldByName('PCID').Value := trim(CDS_Card.fieldbyname('RTValue').AsString);
              FieldByName('GlideNo').Value := trim(CDS_Glide.fieldbyname('GlideNo').AsString);
              FieldByName('GlideName').Value := trim(CDS_Glide.fieldbyname('GlideName').AsString);
              FieldByName('Note').Value := trim(CDS_Glide.fieldbyname('Note').AsString);
              post;
            end;
            CDS_Glide.Next;
          end;
          ADOQueryCmd.Connection.CommitTrans;
        except
          ADOQueryCmd.Connection.RollbackTrans;
          raise Exception.Create(PChar(Exception(ExceptObject).Message));
        end;
        CDS_Card.Next;
      end;
    end;
    CDS_Glide.EnableControls;
    ModalResult := 1;
  except
    CDS_Glide.EnableControls;
    application.MessageBox('保存失败！', '提示信息', MB_ICONERROR);
  end;

end;

procedure TfrmDyeCardGlideEdit.ToolButton6Click(Sender: TObject);
begin
  close;

end;

procedure TfrmDyeCardGlideEdit.TV1DblClick(Sender: TObject);
begin

  with CDS_1 do
  begin
    CDS_Glide.Append;
    CDS_Glide.FieldByName('GlideNo').Value := CDS_Glide.RecordCount + 1;
    CDS_Glide.FieldByName('GlideName').Value := CDS_1.fieldbyname('PSName').AsString;
    CDS_Glide.FieldByName('Note').Value := CDS_1.fieldbyname('Note').AsString;
    CDS_Glide.Post;
  end;
end;

procedure TfrmDyeCardGlideEdit.TBGXClick(Sender: TObject);
begin

  frmDyeTechnicsSel := TfrmDyeTechnicsSel.create(self);
  with frmDyeTechnicsSel do
  begin
    FNAME := SELF.FNAME;
    if showmodal = 1 then
    begin

      with frmDyeTechnicsSel.CDS_1 do
      begin
        First;
        CDS_Glide.EmptyDataSet;

        while not Eof do
        begin
          CDS_Glide.Append;
          CDS_Glide.FieldByName('GlideNo').Value := CDS_Glide.RecordCount + 1;
          CDS_Glide.FieldByName('GlideName').Value := frmDyeTechnicsSel.CDS_1.fieldbyname('PSName').AsString;
          CDS_Glide.FieldByName('Note').Value := frmDyeTechnicsSel.CDS_1.fieldbyname('Note').AsString;
          CDS_Glide.Post;
          Next;
        end;

      end;

    end;
    free;
  end;

end;

end.

