unit U_TatWBPlanInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxDropDownEdit,
  StrUtils, cxPC, ShellAPI, cxCalendar, Math, cxLookAndFeels,
  cxLookAndFeelPainters, dxBarBuiltInMenu, cxNavigator, dxDateRanges,
  U_BaseInput, System.ImageList, Vcl.ImgList, Vcl.Menus, dxScrollbarAnnotations,
  cxImageList;

type
  TfrmTatWBPlanInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton8: TToolButton;
    Panel2: TPanel;
    cxPageControl1: TcxPageControl;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column10: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel5: TPanel;
    Panel6: TPanel;
    Label21: TLabel;
    ToolBar3: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    Label14: TLabel;
    Label5: TLabel;
    Label16: TLabel;
    Label47: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    DlyDate: TDateTimePicker;
    OrdNote: TRichEdit;
    WB_Spec: TEdit;
    CustName: TBtnEditC;
    OurCoName: TBtnEditC;
    WB_YarnQty: TEdit;
    WB_YarnTotalLen: TEdit;
    WB_Qty: TEdit;
    WB_YarnLen: TEdit;
    SizingAgentCost: TEdit;
    WB_Qty_JSPDMS: TEdit;
    WB_Qty_YJJZZS: TEdit;
    WB_Qty_YJMJZGS: TEdit;
    WB_Qty_YJMJZMS: TEdit;
    WB_Len: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure C_CodeKeyPress(Sender: TObject; var Key: Char);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure MCTypeBtnUpClick(Sender: TObject);
    procedure CustNameBtnUpClick(Sender: TObject);
    procedure CustNameBtnDnClick(Sender: TObject);
    procedure OurCoNameBtnUpClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    procedure InitData();
    function SaveData(): Boolean;

    { Private declarations }
  public
    PState, FCopyInt: Integer;
    FConNo, FTWPId, FSubId: string;
    { Public declarations }
  end;

var
  frmTatWBPlanInPut: TfrmTatWBPlanInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_CompanySel, U_YarnInfoSel;

{$R *.dfm}

procedure TfrmTatWBPlanInPut.TBCloseClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmTatWBPlanInPut.InitData();
var
  PBNO, PBFlag: string;
begin
  if PState = 1 then
  begin

  end;

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from Tat_WB_Plan A where A.TWPId=''' + Trim(FTWPId) + '''');
    Open;
  end;
  SCSHData(ADOQueryTemp, ScrollBox1, 0);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  from Tat_WB_Plan_Yarn A ');
    sql.Add(' where YRType=''大轴经丝'' and A.TWPId=''' + Trim(FTWPId) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);

  if FCopyInt = 1 then
  begin
    FConNo := '';
    FTWPId := '';
    FSubId := '';
  end;
end;

procedure TfrmTatWBPlanInPut.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmTatWBPlanInPut.SaveData(): Boolean;
var
  maxno, maxsubno, maxpbno, maxpzno, maxsjno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FTWPId) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxno, 'SZ', 'Tat_WB_Plan', 4, 1) = False then
        raise Exception.Create('生成流水号异常！');

    end
    else
    begin
      maxno := Trim(FTWPId);
    end;
//////////////////////////            保存主表              //////////////////////////
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Tat_WB_Plan where TWPId=''' + Trim(FTWPId) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FTWPId) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('TWPId').Value := Trim(maxno);
      RTSetsavedata(ADOQueryCmd, 'Tat_WB_Plan', ScrollBox1, 0);
      Post;
    end;
//////////////////////////            保存主表              //////////////////////////

//////////////////////////            保存经丝配比表              //////////////////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_1.fieldbyname('YRId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'WY', 'Tat_WB_Plan_Yarn', 4, 1) = False then
            raise Exception.Create('取配比子流水号失败！');
        end
        else
        begin
          maxpbno := Trim(CDS_1.fieldbyname('YRId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Tat_WB_Plan_Yarn where ');
          sql.Add('  YRId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_1.fieldbyname('YRId').AsString) = '' then
          begin
            Append;
            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('YRType').Value := '大轴经丝';
          FieldByName('TWPId').Value := Trim(maxno);
          FieldByName('YRId').Value := Trim(maxpbno);
          RTSetsavedataCDS(ADOQueryCmd, Tv2, CDS_1, 'Tat_WB_Plan_Yarn', 0);
          Post;
        end;
        CDS_1.Edit;
        CDS_1.FieldByName('TWPId').Value := Trim(maxno);
        CDS_1.FieldByName('YRId').Value := Trim(maxpbno);

        Next;
      end;
    end;
//////////////////////////            保存经丝配比表              //////////////////////////


    ADOQueryCmd.Connection.CommitTrans;
    FTWPId := Trim(maxno);
    FSubId := Trim(maxsubno);
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

  end;
end;

procedure TfrmTatWBPlanInPut.TBSaveClick(Sender: TObject);
var
  FReal: Double;
  maxConNo: string;
begin

  if Trim(CustName.Text) = '' then
  begin
    Application.MessageBox('客户不能为空!', '提示', 0);
    Exit;
  end;

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

procedure TfrmTatWBPlanInPut.ToolButton3Click(Sender: TObject);
begin
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with self.CDS_1 do
        begin
          Append;
          FieldByName('Brand').Value := frmYarnInfoSel.CDS_1.fieldbyname('Brand').Value;
          FieldByName('Y_Code').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Code').Value;
          FieldByName('Y_Name').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Name').Value;
          FieldByName('Y_Spec').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Spec').Value;
          FieldByName('Y_Color').Value := frmYarnInfoSel.CDS_1.fieldbyname('Y_Color').Value;
          FieldByName('Denier').Value := frmYarnInfoSel.CDS_1.fieldbyname('Denier').Value;
          FieldByName('Y_Ratio').Value := 0;
          Post;
        end;

      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

procedure TfrmTatWBPlanInPut.ToolButton4Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if Trim(CDS_1.fieldbyname('YRId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Tat_WB_Plan_Yarn where YRId=''' + Trim(CDS_1.fieldbyname('YRId').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_1.Delete;
end;

procedure TfrmTatWBPlanInPut.OurCoNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';
      if ShowModal = 1 then
      begin
        OurCoName.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
        OurCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
//        FtyCoName.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
//        FtyCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

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

procedure TfrmTatWBPlanInPut.MCTypeBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TBtnEditC(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
        TBtnEditC(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

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

procedure TfrmTatWBPlanInPut.CustNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).TxtCode := '';
  TBtnEditC(Sender).Text := '';
end;

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

end;

end.

