unit U_DyeFormulaSampleInput;

interface

uses
  Windows, Messages, strUtils, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, CheckLst, Spin, ComCtrls, Buttons, ExtCtrls, ToolWin,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit,
  DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMaskEdit,
  cxButtonEdit, cxContainer, cxTextEdit, cxMemo, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, ADODB, DBClient, BtnEdit, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, cxCalendar;

type
  TfrmDyeFormulaSampleInput = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label15: TLabel;
    Label4: TLabel;
    Label3: TLabel;
    Label6: TLabel;
    Label17: TLabel;
    Label14: TLabel;
    Label10: TLabel;
    Label7: TLabel;
    Label2: TLabel;
    Label23: TLabel;
    DlyDate: TDateTimePicker;
    comeDate: TDateTimePicker;
    Request: TEdit;
    C_Code: TEdit;
    Panel3: TPanel;
    ToolBar2: TToolBar;
    TBAddDtl: TToolButton;
    TBDelDtl: TToolButton;
    cxgrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    TV1ColPos: TcxGridDBColumn;
    TV1chnName: TcxGridDBColumn;
    TV1ColType: TcxGridDBColumn;
    TV1ClrMethod: TcxGridDBColumn;
    TV1CraftType: TcxGridDBColumn;
    TV1Firm: TcxGridDBColumn;
    TV1Handle: TcxGridDBColumn;
    TV1Note: TcxGridDBColumn;
    cxgrid1L1: TcxGridLevel;
    custName: TBtnEditA;
    Saleser: TEdit;
    C_Composition: TEdit;
    Lights: TBtnEditA;
    Note: TMemo;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DFSType: TBtnEditA;
    TV1Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBAddDtlClick(Sender: TObject);
    procedure TBDelDtlClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure custNameBtnClick(Sender: TObject);
    procedure DFSTypeBtnClick(Sender: TObject);
    procedure LightsBtnClick(Sender: TObject);
    procedure TV1ColTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV1ClrMethodPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV1CraftTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SCTypeDropDown(Sender: TObject);
  private
    procedure InitGrid();
    function Savedata(): boolean;

    { Private declarations }
  public
    FDFSMID: string;
    { Public declarations }
  end;

var
  frmDyeFormulaSampleInput: TfrmDyeFormulaSampleInput;

implementation

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

function TfrmDyeFormulaSampleInput.Savedata(): boolean;
var
  MaxMid, MaxSid: string;
begin
  result := false;
  ADOQueryCmd.Connection.BeginTrans;
  try
    if trim(FDFSMID) = '' then
    begin
      if GetLSNo(ADOQueryTemp, MaxMid, 'FX', 'Dye_Formula_Sample_Main', 4, 1) = False then
        raise Exception.Create('取小样编号失败！');
    end
    else
      MaxMid := FDFSMID;

    with AdoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from Dye_Formula_Sample_Main where DFSMID=''' + trim(MaxMid) + '''');
      open;
      if Trim(FDFSMID) = '' then
      begin
        append;
        Fieldbyname('DFSMID').Value := trim(MaxMid);
        Fieldbyname('Fillid').Value := Trim(dcode);
        Fieldbyname('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        Fieldbyname('Editid').Value := Trim(dcode);
        Fieldbyname('Editer').Value := Trim(DName);
        Fieldbyname('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
      end;

      RTSetsavedata(ADOQueryCmd, 'Dye_Formula_Sample_Main', panel1, 0);

      Fieldbyname('CustNo').Value := CustName.TxtCode;
      post;
    end;

    with CDS_1 do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        if Fieldbyname('DFSSID').AsString = '' then
        begin
          if GetLSNo(ADOQueryTemp, MaxSid, 'FS', 'Dye_Formula_Sample_Sub', 4, 1) = False then
            raise Exception.Create('取小样明细编号失败！');
        end
        else
          MaxSid := Fieldbyname('DFSSID').AsString;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.add('select * from Dye_Formula_Sample_Sub where DFSMID=''' + trim(MaxMid) + ''' ');
          sql.add('and DFSSID=''' + trim(MaxSid) + '''');
          open;

          if IsEmpty then
          begin
            append;
          end
          else
          begin
            edit;
          end;

          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'Dye_Formula_Sample_Sub', 0);
          Fieldbyname('DFSMID').Value := trim(MaxMid);
          Fieldbyname('DFSSID').Value := trim(MaxSid);
          Post;

        end;

        with ADOQueryTemp do
        begin
          close;
          sql.Clear;
          sql.Add('select * from Dye_Formula_Sample_Sub A ');
          sql.Add('inner join Dye_Formula_Sample_Main B on B.DFSMID=A.DFSMID  ');
          sql.Add('where A.C_ColorNo=''' + trim(CDS_1.Fieldbyname('C_ColorNo').AsString) + ''' ');
          open;
        end;
        if ADOQueryTemp.RecordCount > 1 then
          raise Exception.Create('色号重复！');

        next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_1.EnableControls;
    FDFSMID := MaxMid;
    result := true;
  except
    CDS_1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmDyeFormulaSampleInput.SCTypeDropDown(Sender: TObject);
begin
  if not CDS_1.IsEmpty then
  begin
    Application.MessageBox('明细为空才能改!', '提示', 0);
    Exit;
  end;
end;

procedure TfrmDyeFormulaSampleInput.InitGrid();
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Dye_Formula_Sample_Main where DFSMID=''' + trim(FDFSMID) + '''');
    open;
  end;
  SCSHData(ADOQueryTemp, Panel1, 0);
  CustName.TxtCode := ADOQueryTemp.Fieldbyname('CustNo').AsString;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from Dye_Formula_Sample_Sub where DFSMID=''' + trim(FDFSMID) + '''');
    open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);
end;

procedure TfrmDyeFormulaSampleInput.FormDestroy(Sender: TObject);
begin
  frmDyeFormulaSampleInput := nil;
end;

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

procedure TfrmDyeFormulaSampleInput.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmDyeFormulaSampleInput.FormCreate(Sender: TObject);
begin
  comeDate.DateTime := now();
  Dlydate.DateTime := now();
  SClearData(Panel1, 0);
end;

procedure TfrmDyeFormulaSampleInput.FormShow(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmDyeFormulaSampleInput.TBAddDtlClick(Sender: TObject);
begin

  CDS_1.Append;
//  CDS_1.Fieldbyname('P_ColorNo').Value := trim(fmaxNo);
  CDS_1.Post;

end;

procedure TfrmDyeFormulaSampleInput.TBDelDtlClick(Sender: TObject);
begin

  if CDS_1.IsEmpty then
    Exit;

  if Trim(CDS_1.fieldbyname('DFSSID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update Dye_Formula_Large_Sub set DelId=' + quotedstr(DCode) + ',Deler=' + quotedstr(DName) + ',Deltime=getdate() where DFSSID=' + quotedstr(trim(CDS_1.fieldByName('DFSSID').AsString)));
      sql.Add('insert into Dye_Formula_Large_Sub_Del select * from Dye_Formula_Large_Sub where DFSSID=' + quotedstr(trim(CDS_1.fieldByName('DFSSID').AsString)));
      sql.Add('delete Dye_Formula_Large_Sub where DFSSID=' + quotedstr(trim(CDS_1.fieldByName('DFSSID').AsString)));
      execsql;
    end;
  end;
  CDS_1.Delete;

end;

procedure TfrmDyeFormulaSampleInput.TBSaveClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if trim(DFSType.Text) = '' then
  begin
    application.MessageBox('小样类型不能为空！', '提示信息', 0);
    exit;
  end;

  DFSType.SetFocus;

  if savedata() then
  begin
    application.MessageBox('保存成功！', '提示信息');
  end
  else
  begin
    application.MessageBox('保存失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyeFormulaSampleInput.TV1ClrMethodPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ClrMethod';
      flagname := '染色方式';
      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.Fieldbyname('ClrMethod').Value := Trim(ClientDataSet1.Fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDyeFormulaSampleInput.TV1ColTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ColType';
      flagname := '颜色类别';
      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.Fieldbyname('ColType').Value := Trim(ClientDataSet1.Fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDyeFormulaSampleInput.TV1CraftTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'CraftType';
      flagname := '工艺类型';
      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.Fieldbyname('CraftType').Value := Trim(ClientDataSet1.Fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDyeFormulaSampleInput.custNameBtnClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        CustName.Text := Trim(CDS_1.fieldbyname('CoName').AsString);
        CustName.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.Saleser.Text := Trim(CDS_1.fieldbyname('Saleser').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmDyeFormulaSampleInput.DFSTypeBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'DFSType';
      flagname := '小样类型';
      if ShowModal = 1 then
      begin
        DFSType.Text := Trim(ClientDataSet1.Fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDyeFormulaSampleInput.LightsBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'LightsType';
      flagname := '对色灯光';
      if ShowModal = 1 then
      begin
        Lights.Text := Trim(ClientDataSet1.Fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

