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;
    Label2: TLabel;
    Label4: TLabel;
    Label3: TLabel;
    C_Code: TEdit;
    C_Name: TEdit;
    C_Color: TEdit;
    C_Pattern: TEdit;
    Label7: TLabel;
    C_Width: TBtnEditC;
    C_GramWeight: TBtnEditC;
    C_Composition: TBtnEditC;
    Label12: TLabel;
    C_EName: TEdit;
    ToolButton6: TToolButton;
    BCIID: TEdit;
    Label6: TLabel;
    C_Spec: TBtnEditC;
    Label8: TLabel;
    C_FromName: TBtnEditC;
    Panel1: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    ToolBar3: TToolBar;
    ToolButton2: TToolButton;
    ToolButton5: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    CDS_GX: TClientDataSet;
    DataSource2: TDataSource;
    ADOQuery1: TADOQuery;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Label10: TLabel;
    C_composite2: TBtnEditC;
    Label9: TLabel;
    C_composite1: TBtnEditC;
    C_compositeNO1: TEdit;
    C_compositeNO2: TEdit;
    Tv1Column7: TcxGridDBColumn;
    Edit1: TEdit;
    Label11: TLabel;
    CINote: TEdit;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv1Column10: TcxGridDBColumn;
    Label13: TLabel;
    CKPrice: TEdit;
    Tv1Column11: TcxGridDBColumn;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CYColorKeyPress(Sender: TObject; var Key: Char);
    procedure CYJGGYBtnDnClick(Sender: TObject);
    procedure C_WidthBtnDnClick(Sender: TObject);
    procedure C_WidthBtnUpClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure C_FromNameBtnUpClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure C_composite1BtnUpClick(Sender: TObject);
    procedure C_composite2BtnUpClick(Sender: TObject);
    procedure C_composite1BtnDnClick(Sender: TObject);
    procedure C_composite2BtnDnClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    canshu1: string;
    Fint: Integer;
    procedure InitGrid();
    function SaveData(): Boolean;
    { Private declarations }
  public
    FBCIID, FCTID, FCTName, FCTNO: string;
    CopyInt: Integer;

    { Public declarations }
  end;

var
  frmClothInfoInput: TfrmClothInfoInput;

implementation

uses
  U_DataLink, U_RTFun, U_iniParam, U_ZDYHelp, U_AttachmentUpload, U_ClothInfoSel,
  U_PatternSel, U_ClothTypeSel, U_DictSel, U_CloInfoFileUp;

{$R *.dfm}

procedure TfrmClothInfoInput.InitGrid();
begin

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

    SCSHData(ADOQueryMain, ScrollBox1, 2);
    SCSHData(ADOQueryMain, ScrollBox1, 0);
    C_FromName.TxtCode := Trim(ADOQueryMain.fieldbyname('FromBCIID').AsString);
    C_FromName.Text := Trim(ADOQueryMain.fieldbyname('C_FromName').AsString);

    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from BS_Cloth_Glide where BCIID=''' + Trim(FBCIID) + '''');
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_GX);
    SInitCDSData(ADOQueryMain, CDS_GX);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmClothInfoInput.FormShow(Sender: TObject);
begin
  self.edit1.text := FCTName + ' (' + FCTNO + ')';
  InitGrid();
  if CopyInt = 1 then
  begin
    FBCIID := '';
    BCIID.text := '';
    C_Code.text := '';

    with CDS_GX do
    begin
      first;
      while not eof do
      begin
        with CDS_GX do
        begin
          edit;
          FieldByName('GlideNo').Value := '';
          post;
        end;
        next;
      end;
    end;
  end;
end;

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

function TfrmClothInfoInput.SaveData(): Boolean;
var
  maxId, maxno, GlideNo: 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 ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete BS_Cloth_Info_Link where ToID=''' + Trim(maxId) + '''');
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from BS_Cloth_Info_Link  where 1=2');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('ToID').Value := Trim(maxId);
      FieldByName('FromID').Value := Trim(C_FromName.TxtCode);
      Post;
    end;

    with CDS_GX do
    begin
      First;
      while not eof do
      begin
        if CDS_GX.FieldByName('GlideNo').AsString = '' then
        begin
          if GetLSNo(ADOQueryCmd, GlideNo, 'G', 'BS_Cloth_Glide', 4, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          GlideNo := CDS_GX.FieldByName('GlideNo').AsString;
        end;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from BS_Cloth_Glide where GlideNo=''' + trim(GlideNo) + ''' ');
          Open;
        end;

        with ADOQueryCmd do
        begin
          if ADOQueryCmd.IsEmpty then
          begin
            append;
            FieldByName('BCIID').value := maxId;
            FieldByName('GlideNo').value := GlideNo;
            FieldByName('Filler').value := Trim(DName);
          end
          else
          begin
            edit;
            FieldByName('Editer').value := Trim(DName);
            FieldByName('EDITTIME').Value := SGetServerDateTime(ADOQueryTemp);
          end;

          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_GX, 'BS_Cloth_Glide', 0);
          FieldByName('Price').Value := cds_gx.FieldByName('Price').Value;
          post;
        end;

        with CDS_GX do
        begin
          edit;
          FieldByName('GlideNo').Value := GlideNo;
          FieldByName('BCIID').value := maxId;
          post;
        end;

        Next;
      end;

    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      SQL.Add('exec P_BS_CloInfo_Code @BCIID=''' + maxId + ''' ');
      open;
    end;

    C_Code.Text := ADOQueryCmd.FieldByName('C_Code').AsString;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from BS_Cloth_Info where C_Code=''' + Trim(C_Code.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 C_Width.Text = '' then
  begin
    Application.MessageBox('门幅不能为空！', '提示', 0);
    Exit;
  end;
  if C_GramWeight.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;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmClothInfoInput.ToolButton2Click(Sender: TObject);
begin
  try
    frmDictSel := TfrmDictSel.Create(Application);
    with frmDictSel do
    begin
      flag := 'GYName';
      flagname := '工序名称';
      f1note := true;
      V1Note.Caption := '工序编号';

      f2note := true;
      V2Note.Caption := '工序编号';

      tv1.OnCellDblClick := nil;

      tbadd.Visible := false;
      TBDel.Visible := false;
      TBEdit.Visible := false;
      TBSave.Visible := false;
      if ShowModal = 1 then
      begin
        with CDS_GX do
        begin
          Append;
          FieldByName('Glideidx').Value := cds_gx.RecordCount + 1;
          FieldByName('ProcessName').Value := Trim(ClientDataSet1.fieldbyname('DictName').AsString);
          FieldByName('ProcessNO').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);

          FieldByName('ProcessTypeName').Value := Trim(ClientDataSet2.fieldbyname('ITEMText').AsString);
          FieldByName('ProcessTypeNO').Value := Trim(ClientDataSet2.fieldbyname('ITEMNote').AsString);
          post;
        end;

      end;
    end;
  finally
    frmDictSel.Free;
  end;
end;

procedure TfrmClothInfoInput.ToolButton3Click(Sender: TObject);
begin
  if self.FBCIID = '' then
  begin
    if C_Name.Text = '' then
    begin
      Application.MessageBox('产品名称不能为空！', '提示', 0);
      Exit;
    end;
    if C_Width.Text = '' then
    begin
      Application.MessageBox('门幅不能为空！', '提示', 0);
      Exit;
    end;
    if C_GramWeight.Text = '' then
    begin
      Application.MessageBox('克重不能为空！', '提示', 0);
      Exit;
    end;
    if SaveData() then
    begin

    end;
  end;
  try
    frmCloInfoFileUp := TfrmCloInfoFileUp.Create(Application);
    with frmCloInfoFileUp do
    begin
      frmCloInfoFileUp.Code.Text := self.C_Code.Text;
      frmCloInfoFileUp.FBCIID := self.FBCIID;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
//        Self.CDS_1.Locate('BCIID', BCIID, []);
      end;
    end;
  finally
    frmCloInfoFileUp.Free;
  end;
end;

procedure TfrmClothInfoInput.ToolButton5Click(Sender: TObject);
begin
  with CDS_GX do
  begin
    if IsEmpty then
      Exit;
    if RecNo >= 1 then
    begin
      //Prior;
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value - 1;
      Post;
      Prior;
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value + 1;
      Post;
    end;
    Prior;
  end;
end;

procedure TfrmClothInfoInput.C_composite1BtnDnClick(Sender: TObject);
begin
  C_composite1.Text := '';
  C_composite1.TxtCode := '';
  C_compositeNo1.text := '';
end;

procedure TfrmClothInfoInput.C_composite1BtnUpClick(Sender: TObject);
begin
  try
    frmClothInfoSel := TfrmClothInfoSel.create(self);
    with frmClothInfoSel do
    begin
      FCTType := '通用';
      if showmodal = 1 then
      begin
        C_composite1.Text := trim(cds_1.fieldbyname('C_Name').asstring);
        C_compositeNo1.Text := trim(cds_1.fieldbyname('C_Code').asstring);
      end;
    end;
  finally
    frmClothInfoSel.free;
  end;
end;

procedure TfrmClothInfoInput.C_composite2BtnDnClick(Sender: TObject);
begin
  C_composite2.Text := '';
  C_composite2.TxtCode := '';
  C_compositeNo2.text := '';
end;

procedure TfrmClothInfoInput.C_composite2BtnUpClick(Sender: TObject);
begin
  try
    frmClothInfoSel := TfrmClothInfoSel.create(self);
    with frmClothInfoSel do
    begin
      FCTType := '通用';
      if showmodal = 1 then
      begin
        C_composite2.Text := trim(cds_1.fieldbyname('C_Name').asstring);
        C_compositeNo2.Text := trim(cds_1.fieldbyname('C_Code').asstring);
      end;
    end;
  finally
    frmClothInfoSel.free;
  end;
end;

procedure TfrmClothInfoInput.C_FromNameBtnUpClick(Sender: TObject);
begin
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin
      FCTType := '坯布';
      if ShowModal = 1 then
      begin
        C_FromName.TxtCode := Trim(CDS_1.fieldbyname('BCIID').AsString);
        C_FromName.Text := Trim(CDS_1.fieldbyname('C_Name').AsString);
      end;
    end;
  finally
    frmClothInfoSel.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;

  if Key in ['a'..'z'] then
  begin
    Key := char(ord(Key) - 32);
  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.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;

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

procedure TfrmClothInfoInput.ToolButton8Click(Sender: TObject);
var
  k: integer;
begin
  if CDS_GX.IsEmpty then
    exit;
  if CDS_GX.fieldbyname('GlideNo').AsString <> '' then
  begin
    with ADOQueryCmd do
    begin
      close;
      SQL.Clear;
      sql.Add('delete BS_Cloth_Glide where GlideNo=''' + CDS_GX.fieldbyname('GlideNo').AsString + ''' ');
      ExecSQL;
    end;
  end;

  CDS_GX.Delete;
  CDS_GX.IndexFieldNames := 'GlideIdx';
  CDS_GX.DisableControls;
  CDS_GX.First;
  k := 1;
  while not CDS_GX.Eof do
  begin
    CDS_GX.Edit;
    CDS_GX.FieldByName('GlideIdx').Value := k;
    CDS_GX.Post;
    inc(k);
    CDS_GX.Next;
  end;
  CDS_GX.First;
  CDS_GX.EnableControls;
end;

procedure TfrmClothInfoInput.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmDictSel := TfrmDictSel.Create(Application);
    with frmDictSel do
    begin
      flag := 'GYName';
      flagname := '工序名称';
      f1note := true;
      V1Note.Caption := '工序编号';

      f2note := true;
      V2Note.Caption := '工序编号';

      tbadd.Visible := false;
      TBDel.Visible := false;
      TBEdit.Visible := false;
      TBSave.Visible := false;
      if ShowModal = 1 then
      begin
        with CDS_GX do
        begin
          edit;
          FieldByName('ProcessName').Value := Trim(ClientDataSet1.fieldbyname('DictName').AsString);
          FieldByName('ProcessNO').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);

          FieldByName('ProcessTypeName').Value := Trim(ClientDataSet2.fieldbyname('ITEMText').AsString);
          FieldByName('ProcessTypeNO').Value := Trim(ClientDataSet2.fieldbyname('ITEMNote').AsString);
          post;
        end;
      end;
    end;
  finally
    frmDictSel.Free;
  end;
end;

procedure TfrmClothInfoInput.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmPatternSel := TfrmPatternSel.Create(Application);
    with frmPatternSel do
    begin
      ftype := Trim(Cds_GX.fieldbyname('ProcessName').AsString);
      ftype2 := Trim(Cds_GX.fieldbyname('ProcessTypeName').AsString);
      if ShowModal = 1 then
      begin
        with CDS_GX do
        begin
          edit;
          FieldByName('PatternName').Value := Trim(Cds_1.fieldbyname('C_Name').AsString);
          FieldByName('PatternNO').Value := Trim(Cds_1.fieldbyname('C_Code').AsString);

          FieldByName('Price').Value := Trim(Cds_1.fieldbyname('Price').AsString);
          FieldByName('Source').Value := Trim(Cds_1.fieldbyname('C_Source').AsString);
          FieldByName('TechnologyNote').Value := Trim(Cds_1.fieldbyname('C_TechnologyNote').AsString);
          FieldByName('CINote').Value := Trim(Cds_1.fieldbyname('CINote').AsString);
          post;
        end;
      end;
    end;
  finally
    frmPatternSel.Free;
  end;
end;

end.

