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, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, cxCheckBox, cxDropDownEdit, cxContainer, cxMaskEdit,
  dxScrollbarAnnotations, cxMemo, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmClothInfoInput = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label5: TLabel;
    Label15: TLabel;
    Label22: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label3: TLabel;
    C_Code: TcxTextEdit;
    C_Name: TcxTextEdit;
    Label7: TLabel;
    ToolButton6: TToolButton;
    BCIID: TcxTextEdit;
    Label6: TLabel;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    GPM_1: TcxGridPopupMenu;
    Label9: TLabel;
    C_GramWeight: TcxButtonEdit;
    C_Composition: TcxButtonEdit;
    C_Spec: TcxButtonEdit;
    Label11: TLabel;
    C_FromName: TcxButtonEdit;
    C_Width: TcxButtonEdit;
    C_Pattern: TcxButtonEdit;
    C_Color: TcxButtonEdit;
    Label8: TLabel;
    C_ColorNo: TcxButtonEdit;
    Label10: TLabel;
    C_CodeOld: TcxTextEdit;
    Label12: TLabel;
    BookNo: TcxTextEdit;
    Label13: TLabel;
    BookPage: TcxTextEdit;
    Panel1: TPanel;
    ToolBar2: TToolBar;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    CINote: TcxMemo;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CYColorKeyPress(Sender: TObject; var Key: Char);
    procedure CYJGGYBtnDnClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure SetXH();
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure C_GramWeightDblClick(Sender: TObject);
    procedure ParentCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure C_FromNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  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_ClothInfoSel;

{$R *.dfm}
procedure TfrmClothInfoInput.SetXH();
var
  i: Integer;
begin
  with CDS_1 do
  begin
    First;
    i := 1;
    while not Eof do
    begin
      with CDS_1 do
      begin
        Edit;
        FieldByName('XHNoTemp').Value := i;
        Post;
      end;
      i := i + 1;
      Next;
    end;
  end;

  with CDS_1 do
  begin
    CDS_1.IndexFieldNames := '';
    First;
    i := 1;
    while not Eof do
    begin
      with CDS_1 do
      begin
        Edit;
        FieldByName('SerialNo').Value := FieldByName('XHNoTemp').AsInteger;
        Post;
      end;
      i := i + 1;
      Next;
    end;
    CDS_1.IndexFieldNames := 'SerialNo';
  end;
end;

procedure TfrmClothInfoInput.InitGrid();
begin

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

  SCSHData(ADOQueryTemp, ScrollBox1, 2);
  SCSHData(ADOQueryTemp, ScrollBox1, 0);
  C_FromName.Properties.LookupItems.Text := Trim(ADOQueryTemp.fieldbyname('FromBCIID').AsString); //坯布编号
  C_FromName.Text := Trim(ADOQueryTemp.fieldbyname('C_FromName').AsString);    //坯布名称

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Bs_Cloth_Process ');
    sql.Add(' where BCIID=''' + Trim(FBCIID) + '''');
    sql.Add(' order by SerialNo ');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);

end;

procedure TfrmClothInfoInput.C_FromNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin
      FCTType := '坯布';
      if ShowModal = 1 then
      begin
        C_FromName.Properties.LookupItems.Text := 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.C_GramWeightDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

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

procedure TfrmClothInfoInput.FormShow(Sender: TObject);
begin
  InitGrid();
  if CopyInt = 1 then
  begin
    FBCIID := '';
    BCIID.text := '';
    C_Code.text := '';
  end;
end;

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

function TfrmClothInfoInput.SaveData(): Boolean;
var
  MaxBCIID, MaxBCPID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
////////////////       保存主表          //////////////////////
    if Trim(FBCIID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, MaxBCIID, 'Y', 'BS_Cloth_Info', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

      BCIID.Text := trim(MaxBCIID);
    end
    else
    begin
      MaxBCIID := 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(MaxBCIID);
      if trim(C_Code.Text) = '' then
        C_Code.Text := Trim(MaxBCIID);
      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(MaxBCIID) + '''');
      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(MaxBCIID);
      FieldByName('FromID').Value := Trim(C_FromName.Properties.LookupItems.Text);   // 坯布编号
      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) + '''');
      Open;
      if FieldByName('AA').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result := False;
        Application.MessageBox('编号重复！', '提示', 0);
        Exit;
      end;
    end;
////////////////       保存主表          //////////////////////
///
////////////////       保存子表          //////////////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_1.fieldbyname('BCPID').AsString) = '' then
        begin
          if GetLSNo(ADOQueryCmd, MaxBCPID, 'P', 'Bs_Cloth_Process', 4, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          MaxBCPID := Trim(CDS_1.fieldbyname('BCPID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Bs_Cloth_Process where   BCPID=''' + Trim(MaxBCPID) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if ADOQueryCmd.IsEmpty then
            Append
          else
            Edit;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'Bs_Cloth_Process', 0);
          FieldByName('BCPID').Value := Trim(MaxBCPID);
          FieldByName('BCIID').Value := Trim(MaxBCIID);

          Post;
        end;
        with CDS_1 do
        begin
          Edit;
          FieldByName('BCPID').Value := Trim(MaxBCPID);
          Post;
        end;
        Next;
      end;
    end;
////////////////       保存子表          //////////////////////
///
///
///
    ADOQueryCmd.Connection.CommitTrans;
    FBCIID := MaxBCIID;
    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;
  SetXH();
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmClothInfoInput.ToolButton2Click(Sender: TObject);
var
  i: Integer;
begin
  i := CDS_1.RecordCount;      //当前的dataset有多少行记录
  i := i + 1;
  CopyAddRow(Tv1, CDS_1);
  with CDS_1 do
  begin
    Edit;
    FieldByName('SerialNo').Value := i;
    FieldByName('FtyWastage').Value := 0;
    Post;
  end;
  SetXH();
end;

procedure TfrmClothInfoInput.ToolButton3Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  if Trim(CDS_1.fieldbyname('BCPID').AsString) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Bs_Cloth_Process where BCPID=''' + Trim(CDS_1.fieldbyname('BCPID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_1.Delete;

  SetXH();
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.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.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'FtyType';
      flagname := '类型';
      if ShowModal = 1 then
      begin
        CDS_1.Edit;
        CDS_1.fieldbyname('FtyType').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        CDS_1.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

end.

