unit U_TatWBPlanCardInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxTextEdit, cxCheckBox, cxPC,
  Menus, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu,
  dxDateRanges, BtnEdit, U_BaseInput, System.ImageList, Vcl.ImgList,
  dxScrollbarAnnotations, cxImageList, dxSkinsCore, dxSkinOffice2013White,
  dxSkinSharpPlus, dxSkinsDefaultPainters, dxSkinWXI, cxContainer, cxMaskEdit,
  cxDropDownEdit, cxMemo;

type
  TfrmTatWBPlanCardInPut = class(TfrmBaseInput)
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    RMXLSExport1: TRMXLSExport;
    Panel2: TPanel;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N3: TMenuItem;
    Panel4: TPanel;
    Panel1: TPanel;
    Label8: TLabel;
    Label2: TLabel;
    Label6: TLabel;
    Label4: TLabel;
    WB_YarnQty: TEdit;
    edtCardQty: TEdit;
    WB_Spec: TEdit;
    WB_YarnLen: TEdit;
    Note: TRichEdit;
    GPM_2: TcxGridPopupMenu;
    Label7: TLabel;
    WBData: TDateTimePicker;
    Label10: TLabel;
    FtyCoName: TBtnEditC;
    Label18: TLabel;
    TWPId: TEdit;
    Label19: TLabel;
    Label12: TLabel;
    WBNo: TBtnEditC;
    Panel3: TPanel;
    cxPageControl1: TcxPageControl;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    v2Column10: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel5: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    ToolButton2: TToolButton;
    TBClose: TToolButton;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    GPM_1: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    Label1: TLabel;
    Label30: TLabel;
    WB_ProcessType: TcxComboBox;
    WBFtyName: TcxButtonEdit;
    Label3: TLabel;
    WBType1: TcxButtonEdit;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn6: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    GPM_3: TcxGridPopupMenu;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    Panel6: TPanel;
    Label15: TLabel;
    Label5: TLabel;
    Label9: TLabel;
    Label16: TLabel;
    Label11: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label17: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    Label37: TLabel;
    Label38: TLabel;
    Label47: TLabel;
    Label27: TLabel;
    Label39: TLabel;
    WBCNO: TcxTextEdit;
    WB_Type: TcxComboBox;
    WB_Code: TcxButtonEdit;
    WB_Name: TcxTextEdit;
    cxTextEdit1: TcxTextEdit;
    WB_Move: TcxTextEdit;
    WB_ReedNote: TcxTextEdit;
    WB_YarnStripQty: TcxTextEdit;
    WB_Width: TcxTextEdit;
    WB_Arg_SDWZ: TcxTextEdit;
    WB_YarnArr: TcxTextEdit;
    WB_YarnStripRemainder: TcxTextEdit;
    WB_Arg_PXDS: TcxTextEdit;
    WB_Arg_QJZL: TcxTextEdit;
    WB_YarnArrNote: TcxMemo;
    WB_YarnDensity: TcxTextEdit;
    GYNo: TcxButtonEdit;
    CustName: TcxButtonEdit;
    WB_ReedNo: TcxTextEdit;
    GKBH: TcxButtonEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FtyCoNameBtnUpClick(Sender: TObject);
    procedure FtyCoNameBtnDnClick(Sender: TObject);
    procedure WB_CodeBtnDnClick(Sender: TObject);
    procedure WB_MachNoBtnUpClick(Sender: TObject);
    procedure WBNoBtnUpClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    function TestData(): Boolean;
    procedure InitYarn();
    { Private declarations }
  public
    FTWPId, FWB_Type: string;
    { Public declarations }
  end;

var
  frmTatWBPlanCardInPut: TfrmTatWBPlanCardInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LabelPrint, U_CompanySel, U_YarnInfoSel,
  U_WarpBeamInfoSel;

{$R *.dfm}
procedure TfrmTatWBPlanCardInPut.InitYarn();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  ');
    sql.Add(' from Tat_WB_Plan  A ');
    sql.Add(' where  A.TWPId=''' + Trim(FTWPId) + '''');
    Open;
  end;
  if FWB_Type = '经轴' then
  begin
    WB_Spec.Text := ADOQueryTemp.FieldByName('WB_Spec').AsString;
    edtCardQty.Text := ADOQueryTemp.FieldByName('WB_Qty').AsString;
    WB_YarnLen.Text := ADOQueryTemp.FieldByName('WB_YarnLen').AsString;
    WB_YarnQty.Text := ADOQueryTemp.FieldByName('WB_YarnQty').AsString;

  end
  else
  begin
    WB_Spec.Text := ADOQueryTemp.FieldByName('WB_Spec').AsString;
    WB_YarnQty.Text := ADOQueryTemp.FieldByName('WB_Qty_YJMJZGS').AsString;
    edtCardQty.Text := ADOQueryTemp.FieldByName('WB_Qty_YJJZZS').AsString;
    WB_YarnLen.Text := ADOQueryTemp.FieldByName('WB_Qty_YJMJZMS').AsString;

  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  ');
    sql.Add(' 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);
  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*   ');
    sql.Add(' from Tat_WB_Card A');
    sql.Add(' where TWPId=''' + Trim(FTWPId) + '''');
    SQL.Add('and WB_Type = ' + QUOTEDSTR(FWB_Type));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_3);
  SInitCDSData(ADOQueryTemp, CDS_3);
end;

function TfrmTatWBPlanCardInPut.TestData(): Boolean;
begin
  try

    if StrToIntDef(WB_YarnQty.Text, 0) = 0 then
      raise Exception.Create('头纹不能为空！');

    if StrToIntDef(WB_YarnLen.Text, 0) = 0 then
      raise Exception.Create('整经长度不能为空！');

    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatWBPlanCardInPut.WBNoBtnUpClick(Sender: TObject);
begin
  try
    frmWarpBeamInfoSel := TfrmWarpBeamInfoSel.Create(Application);
    with frmWarpBeamInfoSel do
    begin
      if ShowModal = 1 then
      begin

        Self.WBNo.Text := Trim(frmWarpBeamInfoSel.CDS_Main.fieldbyname('WBNo').AsString);

      end;
    end;
  finally
    frmWarpBeamInfoSel.Free;
  end;
end;

procedure TfrmTatWBPlanCardInPut.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmTatWBPlanCardInPut.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(Trim(self.Caption) + 'Tv2', Tv2, '坯布轴卡');
  WBData.DateTime := SGetServerDate(ADOQueryTemp);

  TWPId.Text := FTWPId;

  if FWB_Type = '经轴' then
    TV1.OptionsData.Editing := False;

  InitYarn();
end;

procedure TfrmTatWBPlanCardInPut.FtyCoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).TxtCode := '';
  TBtnEditC(Sender).Text := '';
end;

procedure TfrmTatWBPlanCardInPut.FtyCoNameBtnUpClick(Sender: TObject);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        FtyCoName.TxtCode := Trim(CDS_1.fieldbyname('CoCode').AsString);
        FtyCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

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

procedure TfrmTatWBPlanCardInPut.TBSaveClick(Sender: TObject);
var
  MWBCID: string;
  i, j: integer;
begin
  if TestData() = false then
    exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
//////////////////            插入轴卡               //////////////////////
    if GetLSNo(ADOQueryTemp, MWBCID, 'ZK1', 'Tat_WB_Card', 3, 1) = False then
    begin
      raise Exception.Create('取布票流水号失败!');
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' insert into Tat_WB_Card(WBCID,WBNo,TWPId,WB_Spec,WB_YarnQty,WB_YarnLen ');
      sql.Add(' ,WBData,FtyCoNo,FtyCoName');
      sql.Add(' ,WB_Type,Note,FillId,Filler,WBType,WB_ProcessType,WBFtyName) values');

      for i := 1 to StrToIntdef(edtCardQty.Text, 1) do
      begin
        if i > 1 then
          sql.Add(',');
        sql.Add(' (' + Quotedstr(Trim(MWBCID) + IntToStr(i)));
        sql.Add(',' + Quotedstr(Trim(WBNO.Text)));
        sql.Add(',' + Quotedstr(Trim(FTWPId)));
        sql.Add(',' + Quotedstr(Trim(WB_Spec.Text)));
        sql.Add(',' + (Trim(WB_YarnQty.Text)));
        sql.Add(',' + (Trim(WB_YarnLen.Text)));
        sql.Add(',' + Quotedstr(Trim(FormatDateTime('yyyy-MM-dd', WBData.DateTime))));
        sql.Add(',' + Quotedstr(Trim(FtyCoName.TxtCode)));
        sql.Add(',' + Quotedstr(Trim(FtyCoName.Text)));
        sql.Add(',' + Quotedstr(FWB_Type));
        sql.Add(',' + Quotedstr(Trim(Note.Text)));
        sql.Add(',' + Quotedstr(Trim(DCode)));
        sql.Add(',' + Quotedstr(Trim(DName)));
        sql.Add(',' + Quotedstr(Trim(WBType1.Text)));
        sql.Add(',' + Quotedstr(Trim(WB_ProcessType.Text)));
        sql.Add(',' + Quotedstr(Trim(WBFtyName.Text)));
        sql.Add(' )');

      end;

      for i := 1 to StrToIntdef(edtCardQty.Text, 1) do
      begin
        sql.Add(' insert into Tat_WB_Card_Yarn(YRId,WBCID,Y_Code,Y_Name,Brand,Y_Color,Y_Ratio');
        sql.Add(',Filler) values');
        CDS_1.DisableControls;
        CDS_1.first;
        j := 1;
        while not CDS_1.eof do
        begin
          if j > 1 then
            sql.Add(',');
          sql.Add(' (' + Quotedstr(Trim(MWBCID) + IntToStr(i) + IntToStr(j)));
          sql.Add(',' + Quotedstr(Trim(MWBCID) + IntToStr(i)));
          sql.Add(',' + Quotedstr(Trim(CDS_1.FieldByName('Y_Code').AsString)));
          sql.Add(',' + Quotedstr(Trim(CDS_1.FieldByName('Y_Name').AsString)));
          sql.Add(',' + Quotedstr(Trim(CDS_1.FieldByName('Brand').AsString)));
          sql.Add(',' + Quotedstr(Trim(CDS_1.FieldByName('Y_Color').AsString)));
          sql.Add(',' + Quotedstr(Trim(CDS_1.FieldByName('Y_Ratio').AsString)));
          sql.Add(',' + Quotedstr(Trim(DName)));
          sql.Add(' )');

          CDS_1.Next;
          j := j + 1;
        end;
        CDS_1.enableControls;
      end;
//      ShowMessage(sql.Text);
      ExecSQL;
    end;

//////////////////            插入轴卡               //////////////////////
    ADOQueryCmd.Connection.CommitTrans;
    ModalResult := 1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatWBPlanCardInPut.ToolButton1Click(Sender: TObject);
begin
  CDS_1.Delete;
end;

procedure TfrmTatWBPlanCardInPut.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(Trim(self.Caption) + 'Tv2', Tv2, '坯布轴卡');
end;

procedure TfrmTatWBPlanCardInPut.WB_CodeBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';

end;

procedure TfrmTatWBPlanCardInPut.WB_MachNoBtnUpClick(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;

end.

