unit U_ClothInfoInput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ADODB, DBClient, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, ComCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, cxTextEdit,
  cxButtonEdit, StdCtrls, ExtCtrls, cxCurrencyEdit, BtnEdit, U_BaseList,
  cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters,
  cxNavigator, dxDateRanges, dxBarBuiltInMenu;

type
  TfrmClothInfoInput = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label5: TLabel;
    Label15: TLabel;
    Label22: TLabel;
    Label3: TLabel;
    C_Code: TEdit;
    C_Name: TEdit;
    C_Width: TBtnEditC;
    C_GramWeight: TBtnEditC;
    ToolButton6: TToolButton;
    BCIID: TEdit;
    Label6: TLabel;
    C_Spec: TBtnEditC;
    Label2: TLabel;
    C_Deal: TBtnEditC;
    Label4: TLabel;
    C_Color: TBtnEditC;
    Label7: TLabel;
    C_Composition: TBtnEditC;
    Label8: TLabel;
    Note: TBtnEditC;
    Label9: TLabel;
    C_KnitGramWeight: TBtnEditC;
    Label10: TLabel;
    Label11: TLabel;
    C_Yarn: TBtnEditC;
    C_Figure: TBtnEditC;
    lbl1: TLabel;
    C_KHName: TBtnEditC;
    C_KHCode: TEdit;
    lbl2: TLabel;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CYColorKeyPress(Sender: TObject; var Key: Char);
    procedure CYKZExit(Sender: TObject);
    procedure CYMFExit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CYJGGYBtnDnClick(Sender: TObject);
    procedure C_WidthBtnDnClick(Sender: TObject);
    procedure C_WidthBtnUpClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure C_KHNameBtnUpClick(Sender: TObject);
  private
    canshu1: string;
    Fint: Integer;
    procedure InitGrid();
    function SaveData(): Boolean;
    { Private declarations }
  public
    FBCIID, FCTID: string;
    CopyInt: Integer;

    { Public declarations }
  end;

var
  frmClothInfoInput: TfrmClothInfoInput;

implementation

uses
  U_DataLink, U_RTFun, U_iniParam, U_ZDYHelp, U_AttachmentUpload, U_CompanySel;

{$R *.dfm}

procedure TfrmClothInfoInput.InitGrid();
begin

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from BS_Cloth_Info where BCIID=''' + Trim(FBCIID) + '''');
      Open;
    end;

    SCSHData(ADOQueryMain, ScrollBox1, 2);
    SCSHData(ADOQueryMain, ScrollBox1, 0);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmClothInfoInput.FormShow(Sender: TObject);
begin
  InitGrid();
  if CopyInt = 1 then
  begin
    FBCIID := '';
    BCIID.text := '';
    C_Code.text := '';
    C_Spec.text := '';
  end;
//  if C_Code.text = '' then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add(' exec P_YP_Get_CPBH  ');
//      sql.Add('@CTID=' + quotedstr(Trim(FCTID)));
//
//      Open;
//    end;
//  end;
//  if trim(C_Code.Text) = '' then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add(' exec P_YP_Get_No @Str=''CP'' ');
//      Open;
//    end;
//    C_Code.Text := trim(ADOQueryTemp.FieldByName('NewC_Code').asstring);
//  end;
//  if FCPName = '��֯' then
//  begin
//    CYPUnit.text := 'KG';
//  end;
//  if FCPName = '��֯' then
//  begin
//    CYPUnit.text := 'M';
//  end;

end;

procedure TfrmClothInfoInput.TBCloseClick(Sender: TObject);
begin
  Close;
end;

function TfrmClothInfoInput.SaveData(): Boolean;
var
  maxId, maxno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FBCIID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'Y', 'BS_Cloth_Info', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('ȡ����ʧ�ܣ�', '��ʾ', 0);
        Exit;
      end;

      BCIID.Text := trim(maxId);
    end
    else
    begin
      maxId := Trim(FBCIID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from BS_Cloth_Info where BCIID=''' + Trim(FBCIID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FBCIID) = '' then
      begin
        Append;
        FieldByName('CTID').Value := Trim(FCTID);
        FieldByName('FILLID').Value := Trim(DCode);
        FieldByName('FILLER').Value := Trim(DName);
        FieldByName('FILLTIME').Value := SGetServerDateTime(ADOQueryTemp);
      end
      else
      begin
        Edit;
        FieldByName('EDITER').Value := Trim(DName);
        FieldByName('EDITTIME').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('BCIID').Value := Trim(maxId);
      if trim(C_Code.Text) = '' then
      C_Code.Text := Trim(maxId);
      RTSetsavedata(ADOQueryCmd, 'BS_Cloth_Info', ScrollBox1, 2);
      Post;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from BS_Cloth_Info where C_Code=''' + Trim(C_Code.Text) + '''');
//      ShowMessage(sql.text);
      Open;
      if FieldByName('AA').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result := False;
        Application.MessageBox('����ظ���', '��ʾ', 0);
        Exit;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    FBCIID := maxId;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('����ʧ��!', '��ʾ', 0);
  end;
end;

procedure TfrmClothInfoInput.CYJGGYBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
end;

procedure TfrmClothInfoInput.ToolButton1Click(Sender: TObject);
var
  MC_Code: string;
begin
  if C_Name.Text = '' then
  begin
    Application.MessageBox('��Ʒ���Ʋ���Ϊ�գ�', '��ʾ', 0);
    Exit;
  end;
  if trim(C_Code.Text) = '' then
  begin

    if GetLSNo(ADOQueryCmd, MC_Code, 'C', 'BS_Cloth_Info', 4, 0) = False then
    begin
      Application.MessageBox('ȡ���Ʒ���ʧ��!', '��ʾ', 0);
      Exit;
    end;
    C_Code.Text := MC_Code;

  end;

  //��ѯ�Ƿ�ѡȡ�����Ʒ�ĺ�ͬ������У���û�¹��������ĺ�ͬ�Ķ�Ӧ��Ϣ�޸ĵ�
//  with ADOQueryTemp do
//  begin
//    Close;
//    SQL.Clear;
//    sql.Add(' select A.*,B.* from BS_Contract_Sub A ');
//    sql.Add(' INNER JOIN BS_Contract_Main B on A.ConMId =B.ConMId ');
//    sql.Add(' where A.C_Code =''' + Trim(C_Code.text) + '''');
//    sql.Add(' and not EXISTS (select X.ConSId from Knit_Plan_Sub X where A.ConSId =X.ConSId) ');
//    Open;
//  end;
//
//  if ADOQueryTemp.IsEmpty = false then
//  begin
//    if Application.MessageBox('��ͬ���޸ĺ�ͬ��Ϣ���Ƿ񱣴棿', '��ʾ', 32 + 4) <> IDYES then
//    begin
//      Exit;
//    end
//    else
//    begin
//      with ADOQueryTemp do
//      begin
//        Close;
//        SQL.Clear;
//        sql.Add(' update BS_Contract_Sub set C_Deal=''' + trim(C_Deal.text) + '''');
//        sql.Add(' ,C_Name=''' + trim(C_Name.text) + '''');
//        sql.Add(' ,C_Width=''' + trim(C_Width.text) + '''');
//        sql.Add(' ,C_GramWeight=''' + trim(C_GramWeight.text) + '''');
//        sql.Add(' ,C_Composition=''' + trim(C_Composition.text) + '''');
//        sql.Add(' ,C_Color=''' + trim(C_Color.text) + '''');
//        sql.Add(' ,C_Figure=''' + trim(C_Figure.text) + '''');
//        sql.Add(' ,C_Yarn=''' + trim(C_Yarn.text) + '''');
//        sql.Add(' ,C_Spec=''' + trim(C_Spec.text) + '''');
//        sql.Add(' ,C_KnitGramWeight=''' + trim(C_KnitGramWeight.text) + '''');
//        sql.Add(' ,ConSNote=''' + trim(Note.text) + '''');
//        sql.Add(' from  BS_Contract_Sub A');
//        sql.Add(' where C_Code =''' + Trim(C_Code.text) + '''');
//        sql.Add(' and not EXISTS (select X.ConSId from Knit_Plan_Sub X where A.ConSId =X.ConSId) ');
////        ShowMessage(sql.text);
//        ExecSQL;
//      end;
//    end;
//
//  end;

  if SaveData() then
  begin
    Application.MessageBox('����ɹ���', '��ʾ', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmClothInfoInput.C_KHNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '�ͻ�';
      if ShowModal = 1 then
      begin
        C_KHName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmClothInfoInput.CYColorKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    SelectNext(ActiveControl as TWinControl, True, True);
  end;
end;

procedure TfrmClothInfoInput.CYKZExit(Sender: TObject);
begin
//  if pos('G/M2', trim(CYKZ.Text)) = 0 then
//  begin
//    if pos('G/M', trim(CYKZ.Text)) = 0 then
//    begin
//      CYKZ.Text := trim(CYKZ.Text) + 'G/M2';
//    end;
//  end;
end;

procedure TfrmClothInfoInput.C_WidthBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmClothInfoInput.C_WidthBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothInfoInput.CYMFExit(Sender: TObject);
begin
//  if pos('"', trim(CYMF.Text)) = 0 then
//  begin
//    if pos('CM', trim(CYMF.Text)) = 0 then
//      CYMF.Text := trim(CYMF.Text) + 'CM';
//  end;

end;

procedure TfrmClothInfoInput.FormCreate(Sender: TObject);
begin

//  LaYangDate.DateTime := SGetServerDateTime(ADOQueryTemp);
end;

procedure TfrmClothInfoInput.ToolButton6Click(Sender: TObject);
begin
  if trim(C_Code.Text) = '' then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      FEditAuthority := True;
      fkeyNO := trim(C_Code.Text);
      fType := '��Ʒ����';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;
end;

end.