unit U_TatWBCardInPut2;

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, cxContainer,
  cxMaskEdit, cxDropDownEdit, cxRichEdit, cxMemo, dxScrollbarAnnotations,
  dxSkinsCore, dxSkinsDefaultPainters, cxImageList, dxSkinOffice2013White,
  dxSkinSharpPlus, dxSkinWXI, dxCoreGraphics;

type
  TfrmTatWBCardInPut2 = class(TfrmBaseInput)
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    RMDB_1: TRMDBDataSet;
    Panel2: TPanel;
    DS_Card: TDataSource;
    CDS_Card: TClientDataSet;
    ADOQueryPrint: TADOQuery;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N3: TMenuItem;
    Panel4: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    ToolButton1: TToolButton;
    ToolButton8: TToolButton;
    ToolButton2: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label15: TLabel;
    GPM_2: TcxGridPopupMenu;
    Label7: TLabel;
    Label1: TLabel;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    TWPId: TcxTextEdit;
    WB_Code: TcxButtonEdit;
    Label5: TLabel;
    WB_Name: TcxTextEdit;
    Label11: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    WB_YarnQty: TcxTextEdit;
    Z_FJGENSHU: TcxTextEdit;
    WB_Width: TcxTextEdit;
    Label18: TLabel;
    Label19: TLabel;
    Z_FENJIAO: TcxTextEdit;
    Z_BINGSHU: TcxTextEdit;
    TV2Column2: TcxGridDBColumn;
    DS_3: TDataSource;
    GPM_3: TcxGridPopupMenu;
    CDS_3: TClientDataSet;
    ADO_3: TADOQuery;
    TV2Column18: TcxGridDBColumn;
    ToolButton3: TToolButton;
    TV2Column1: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    Panel3: TPanel;
    Label2: TLabel;
    Label6: TLabel;
    Label10: TLabel;
    Label24: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    Label28: TLabel;
    Label29: TLabel;
    WBType1: TcxButtonEdit;
    WB_YarnLen1: TcxTextEdit;
    edtCardQty1: TcxTextEdit;
    WBType2: TcxButtonEdit;
    edtCardQty2: TcxTextEdit;
    WBType3: TcxButtonEdit;
    edtCardQty3: TcxTextEdit;
    WB_YarnLen2: TcxTextEdit;
    WB_YarnLen3: TcxTextEdit;
    Label4: TLabel;
    Note: TcxMemo;
    Label21: TLabel;
    Label30: TLabel;
    WB_ProcessType: TcxComboBox;
    WBFtyName: TcxButtonEdit;
    Label9: TLabel;
    Label3: TLabel;
    WB_MachNo: TcxButtonEdit;
    WB_Macher: TcxButtonEdit;
    Label31: TLabel;
    WBBelong1: TcxComboBox;
    Label32: TLabel;
    WBBelongCoName1: TcxButtonEdit;
    Label33: TLabel;
    WBBelong2: TcxComboBox;
    Label34: TLabel;
    WBBelongCoName2: TcxButtonEdit;
    Label35: TLabel;
    WBBelong3: TcxComboBox;
    Label36: TLabel;
    WBBelongCoName3: TcxButtonEdit;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    TV2Column9: TcxGridDBColumn;
    Label38: TLabel;
    GYNo: TcxButtonEdit;
    Label47: TLabel;
    CustName: TcxButtonEdit;
    Label20: TLabel;
    Label39: TLabel;
    WB_ReedNo: TcxTextEdit;
    GKBH: TcxButtonEdit;
    WB_TYPE: TcxTextEdit;
    Label40: TLabel;
    C_NAME: TcxTextEdit;
    Label8: TLabel;
    Label12: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Z_SJKESHU1: TcxTextEdit;
    Z_SJKESHU2: TcxTextEdit;
    Z_ZHOUSHU1: TcxTextEdit;
    Z_ZHOUSHU2: TcxTextEdit;
    Label22: TLabel;
    Label23: TLabel;
    BSQTY: TcxTextEdit;
    DCRQTY: TcxTextEdit;
    Label37: TLabel;
    C_KCQTY: TcxTextEdit;
    Label41: TLabel;
    WB_ReedNote: TcxTextEdit;
    Label42: TLabel;
    C_CODE: TcxTextEdit;
    Label43: TLabel;
    XZ_GS: TcxTextEdit;
    Label44: TLabel;
    XZ_YarnQty: TcxTextEdit;
    Label45: TLabel;
    BCRFQty: TcxTextEdit;
    Label46: TLabel;
    Label48: TLabel;
    WB_YarnKCQty: TcxTextEdit;
    Label49: TLabel;
    Label50: TLabel;
    Label51: TLabel;
    Label52: TLabel;
    Label53: TLabel;
    Label54: TLabel;
    TV2Group: TcxGridDBColumn;
    TV2serialNo: TcxGridDBColumn;
    Timer1: TTimer;
    Group: TcxTextEdit;
    Label55: TLabel;
    TV2XZZJ: TcxGridDBColumn;
    TV2XZSJ: TcxGridDBColumn;
    TV2JZSJ: TcxGridDBColumn;
    TV2JZSZ: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure WBNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure WB_MachNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure WB_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TBSaveClick(Sender: TObject);
    procedure cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure WBFtyNameDblClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TV2Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV2Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV2Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure GYNoDblClick(Sender: TObject);
    procedure GKBHPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Timer1Timer(Sender: TObject);
  private
    function TestData(): Boolean;
    procedure PrtDate(MWBCID: string);
    procedure InitCard();
    procedure InitCardGrop();
    procedure InitCard3();
    procedure KaiKa(MWBType, MWBBelong, MWBBelongCoNo, MWBBelongCoName: string; MCardQty: Integer; MWB_YarnLen: Double);
    { Private declarations }
  public
    FWBCNO, FTWPId, FWB_Type: string;

    { Public declarations }
  end;

var
  frmTatWBCardInPut2: TfrmTatWBCardInPut2;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LabelPrint, U_WBTatPlanSel, U_WBSpecSel,
  U_WarpBeamInfoSel, U_WBTypeSel, U_CompanySel, U_TatGYSel, U_GKManageSel,
  U_TatPlanSel2;

{$R *.dfm} procedure TfrmTatWBCardInPut2.InitCard3();
begin
  with ADO_3 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* from Tat_WB_Formula A inner join Tat_WB_Spec B on A.WBSID=B.WBSID ');
    sql.Add(' where WB_Code=''' + Trim(WB_Code.Text) + '''');
    Open;
  end;
  SCreateCDS(ADO_3, CDS_3);
  SInitCDSData(ADO_3, CDS_3);
end;

procedure TfrmTatWBCardInPut2.InitCardGrop();
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from Tat_WB_Plan A where A.TWPId=''' + Trim(FTWPId) + '''');
    Open;
  end;
  SCSHData(ADOQueryTemp, Panel1, 1);
  if FWB_Type = '稀轴' then
  begin
    edtCardQty1.Text := Trim(ADOQueryTemp.fieldbyname('Z_BINGSHU').AsString);
    WB_YarnLen1.Text := Trim(ADOQueryTemp.fieldbyname('WB_Qty_YJMJZMS').AsString);
    XZ_YarnQty.Text := Trim(ADOQueryTemp.fieldbyname('WB_Qty_YJMJZMS').AsString);
    XZ_GS.Text := Trim(ADOQueryTemp.fieldbyname('WB_Qty_YJJZZS').AsString);
    edtCardQty1.Properties.ReadOnly := true;
    Label44.Caption := '计划稀轴经长';
    Label43.Caption := '计划稀轴个数';
    WB_ProcessType.Properties.Items.Clear;
    WB_ProcessType.Properties.Items.Add('本厂整经');
    WB_ProcessType.Properties.Items.Add('外厂整经');
    WB_ProcessType.ItemIndex := 0;
  end
  else
  begin
    edtCardQty1.Properties.ReadOnly := false;
    Label44.Caption := '计划织轴经长';
    Label43.Caption := '计划织轴个数';

    WB_ProcessType.Properties.Items.Clear;

    XZ_YarnQty.Text := Trim(ADOQueryTemp.fieldbyname('WB_YarnLen').AsString);
    XZ_GS.Text := Trim(ADOQueryTemp.fieldbyname('WB_Qty').AsString);
    WB_ProcessType.Properties.Items.Add('本厂浆纱');
    WB_ProcessType.Properties.Items.Add('浆纱采购');
    WB_ProcessType.Properties.Items.Add('外厂浆纱');
    WB_ProcessType.ItemIndex := 0;
  end;

  GYNo.Properties.LookupItems.Text := Trim(ADOQueryTemp.fieldbyname('GYSubid').AsString);
  CustName.Properties.LookupItems.Text := Trim(ADOQueryTemp.fieldbyname('CustNo').AsString);
end;

function TfrmTatWBCardInPut2.TestData(): Boolean;
begin
  try

    if Trim(FTWPId) = '' then
    begin
      if GetLSNo(ADOQueryTemp, FTWPId, 'QJ', 'Tat_WB_Card', 3, 1) = False then
      begin
        raise Exception.Create('迁经单号失败!');
      end;
      TWPId.Text := FTWPId;
    end;
//    if Trim(WB_ProcessType.Text) = '本厂整经' then
//    begin
//      if Trim(WB_Macher.Text) = '' then
//        raise Exception.Create('扦经工位不能为空！');
//    end;
    if trim(WB_Code.Text) = '' then
      raise Exception.Create('经编号不能为空！');
//    if StrToIntDef(edtCardQty.Text, 0) = 0 then
//      raise Exception.Create('排轴数不能为空！');


    if StrToIntDef(WB_YarnQty.Text, 0) = 0 then
      raise Exception.Create('头纹不能为空！');
//    if Trim(WB_YarnArr.Text) = '' then
//      raise Exception.Create('上排不能为空！');
//    if Trim(WB_Len.Text) = '' then
//      raise Exception.Create('轴长不能为空！');
//    if StrToIntDef(WB_YarnLen.Text, 0) = 0 then
//      raise Exception.Create('整经长度不能为空！');
//    if Trim(WB_ProcessType.Text) = '本厂整经' then
//    begin
//      if Trim(WB_MachNo.Text) = '' then
//        raise Exception.Create('迁经机不能为空！');
//    end;

//    if StrToIntDef(WB_ClothPiece.Text, 0) = 0 then
//      WB_ClothPiece.Text := '0';
    if Trim(WB_Type.Text) = '' then
      raise Exception.Create('类型不能为空！');

    if FWB_Type = '浆轴' then
    begin
      if trim(group.Text) = '' then
        raise Exception.Create('组号不能为空！');
    end;

    if (Trim(edtCardQty1.Text) = '') or (Trim(edtCardQty1.Text) = '0') then
      raise Exception.Create('轴数不能为空或0！');

    if trim(WBType1.Text) = '' then
      raise Exception.Create('盘头类型不能为空！');

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

procedure TfrmTatWBCardInPut2.Timer1Timer(Sender: TObject);
begin
  // 10秒时间到，重新启用按钮
  Timer1.Enabled := False;
  TBSave.Enabled := True;
//  ShowMessage('按钮已重新启用');
end;

procedure TfrmTatWBCardInPut2.InitCard();
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  ');
    sql.Add(',cast((CASE  WHEN EXISTS (SELECT 1 from Tat_WB_IO WHERE WBCID=A.WBCID  AND StkName=''经轴''  AND ioFlag=''入库''  AND ioType=''整经完成'' ) THEN 1 ELSE 0 END) as bit) AS XZZJ');
    sql.Add(',cast((CASE  WHEN EXISTS (SELECT 1 from Tat_WB_IO WHERE WBCID=A.WBCID  AND StkName=''经轴''  AND ioFlag=''出库''  AND ioType=''加工出库'' ) THEN 1 ELSE 0 END) as bit) AS XZSJ');
//    sql.Add(', cast(1 as bit) SJ');
    sql.Add(',cast((CASE  WHEN EXISTS (SELECT 1 from Tat_Plan_OutPut WHERE PCID=A.WBCID  AND GlideName=''上浆'' ) THEN 1 ELSE 0 END) as bit) AS JZSJ');
    sql.Add(',cast((CASE  WHEN EXISTS (SELECT 1 from Tat_Plan_OutPut WHERE PCID=A.WBCID  AND GlideName=''上轴'' ) THEN 1 ELSE 0 END) as bit) AS JZSZ');
    sql.Add('from Tat_WB_Card A   ');
    sql.Add('where isnull(Status,''0'')<>''-1'' and TWPId=' + quotedstr(Trim(FTWPId)));
    SQL.Add('AND WB_TYPE=' + quotedstr(Trim(FWB_Type)));
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Card);
  SInitCDSData(ADOQueryTemp, CDS_Card);
end;

procedure TfrmTatWBCardInPut2.PrtDate(MWBCID: string);
var
  fPrintFile, FFCYID: string;
  DPS, i, j: Integer;
begin

  ExportFtErpFile('轴卡标签.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\轴卡标签.rmf';

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update Tat_WB_Card set PrtFlag=PrtFlag+1 ');
    sql.Add(' where WBCID=' + quotedstr(MWBCID));
    ExecSQL;
  end;

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,D.*  ');
    sql.Add(' from PD_Plan_Main A inner join PD_Plan_Sub B on A.Mainid=B.MainId');
    sql.Add(' inner join  Tat_WB_Card_MS C  on C.Subid=B.Subid  ');
    sql.Add(' inner join  Tat_WB_Card D  on C.WBCID=D.WBCID  ');
    sql.Add(' where D.WBCID=' + quotedstr(MWBCID));
//    ShowMessage(SQL.Text);
    Open;
  end;

//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select Y.* from PD_Plan_Yarn Y INNER JOIN Tat_WB_Card_MS M ON M.MainId=Y.MainId where Y.PYType=''经丝'' and M.WBCID=' + Quotedstr(Trim(MWBCID)));
//    Open;
//  end;
//  SCreateCDS(ADOQueryTemp, CDS_PJS);
//  SInitCDSData(ADOQueryTemp, CDS_PJS);
//
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select Y.* from PD_Plan_Yarn Y INNER JOIN Tat_WB_Card_MS M ON M.MainId=Y.MainId where Y.PYType=''纬丝'' and M.WBCID=' + Quotedstr(Trim(MWBCID)));
//    Open;
//  end;
//  SCreateCDS(ADOQueryTemp, CDS_PWS);
//  SInitCDSData(ADOQueryTemp, CDS_PWS);

  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.DefaultCopies := 1;
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
    Exit;
  end;

end;

procedure TfrmTatWBCardInPut2.WBFtyNameDblClick(Sender: TObject);
begin
  self.WBFtyName.Properties.LookupItems.Text := '';
  self.WBFtyName.Text := '';
end;

procedure TfrmTatWBCardInPut2.WBNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmWBTypeSel := TfrmWBTypeSel.Create(Application);
    with frmWBTypeSel do
    begin
      if ShowModal = 1 then
      begin
        TcxTextEdit(Sender).Text := Trim(frmWBTypeSel.CDS_Main.fieldbyname('WBType').AsString);
      end;
    end;
  finally
    frmWBTypeSel.Free;
  end;
end;

procedure TfrmTatWBCardInPut2.WB_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmWBSpecSel := TfrmWBSpecSel.Create(Application);
    with frmWBSpecSel do
    begin
      fWB_TYPE := '整浆并';
      if ShowModal = 1 then
      begin
        Self.WB_Code.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_Code').AsString);
        Self.WB_Name.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_Name').AsString);
        Self.WB_YarnQty.Text := frmWBSpecSel.CDS_1.fieldbyname('WB_YarnQty').value;

        Self.WB_Width.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_Width').AsString);
        Self.WB_ReedNo.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_ReedNo').AsString);
        Self.WB_ReedNote.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('WB_ReedNote').AsString);

        Self.Note.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('Note').AsString);

        Self.gkbh.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('gkbh').AsString);
        Self.InitCard3();
      end;
    end;
  finally
    frmWBSpecSel.Free;
  end;
end;

procedure TfrmTatWBCardInPut2.WB_MachNoPropertiesButtonClick(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 := 'SalesCon';
      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 TfrmTatWBCardInPut2.CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        CustName.Properties.LookupItems.Text := Trim(CDS_1.fieldbyname('CoCode').AsString);
        CustName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTatWBCardInPut2.cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        tcxbuttonedit(Sender).Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        tcxbuttonedit(Sender).Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

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

procedure TfrmTatWBCardInPut2.FormShow(Sender: TObject);
begin
  inherited;

  ReadCxGrid(Trim(self.Caption) + 'Tv2', Tv2, '坯布轴卡');
  InitCardGrop();
  InitCard();

  InitCard3();
  WB_TYPE.Text := FWB_Type;

  if FWB_Type = '浆轴' then
  begin
    Group.Visible := True;
    Label55.Visible := True;

    TV2XZZJ.Visible := False;
    TV2XZSJ.Visible := False;
  end
  else
  begin
    TV2JZSZ.Visible := False;
    TV2JZSJ.Visible := False;
  end;

end;

procedure TfrmTatWBCardInPut2.GKBHPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmGKManageSel := tfrmGKManageSel.Create(Application);
    with frmGKManageSel do
    begin

      if ShowModal = 1 then
      begin
        SELF.GKBH.Text := CDS_HZ.FieldByName('GKBH').AsString;
        SELF.WB_ReedNo.Text := CDS_HZ.FieldByName('GKKH').AsString;
      end;
    end;
  finally
    frmGKManageSel.Free;
  end;
end;

procedure TfrmTatWBCardInPut2.GYNoDblClick(Sender: TObject);
begin
  inherited;
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
  TcxButtonEdit(Sender).Text := '';
end;

procedure TfrmTatWBCardInPut2.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Card, True);
end;

procedure TfrmTatWBCardInPut2.N3Click(Sender: TObject);
begin
  SelOKNo(CDS_Card, False);
end;

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

procedure TfrmTatWBCardInPut2.TBSaveClick(Sender: TObject);
begin

  TBSave.Enabled := False;

  // 启动定时器，10秒后重新启用按钮
  Timer1.Interval := 10000;  // 使用 Timer1
  Timer1.Enabled := True;

  if TestData() = false then
    exit;

  KaiKa(WBType1.Text, WBBelong1.Text, WBBelongCoName1.Properties.LookupItems.Text, WBBelongCoName1.Text, StrToIntdef(edtCardQty1.Text, 0), StrToFloatdef(WB_YarnLen1.Text, 0));
//  KaiKa(WBType2.Text, WBBelong2.Text, WBBelongCoName2.Properties.LookupItems.Text, WBBelongCoName2.Text, StrToIntdef(edtCardQty2.Text, 0), StrToFloatdef(WB_YarnLen2.Text, 0));
//  KaiKa(WBType3.Text, WBBelong3.Text, WBBelongCoName3.Properties.LookupItems.Text, WBBelongCoName3.Text, StrToIntdef(edtCardQty3.Text, 0), StrToFloatdef(WB_YarnLen3.Text, 0));
end;

procedure TfrmTatWBCardInPut2.KaiKa(MWBType, MWBBelong, MWBBelongCoNo, MWBBelongCoName: string; MCardQty: Integer; MWB_YarnLen: Double);
var
  MWBCID: string;
  i, j: integer;
  fDate: string;
  mGroup: string;
begin
  if MCardQty = 0 then
    EXIT;
  if (WB_ProcessType.Text = '浆轴采购') or (WB_ProcessType.Text = '外厂浆纱') then
  begin
    if WBFtyName.Text = '' then
    begin
      Application.MessageBox('请填写加工单位', '提示', 0);
      Exit;
    end;

  end;
  if FWB_Type = '稀轴' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.add(' select isnull(max(A.[Group]),0) as maxGroup from Tat_WB_Card A');
      sql.add(' where A.TWPId = ' + Quotedstr(Trim(FTWPId)));
      sql.add(' AND A.WB_Type = ' + Quotedstr(Trim(FWB_Type)));
//    showMessage(SQL.Text);
      Open;
    end;
    mGroup := intToStr(ADOQueryCmd.FieldByName('maxGroup').AsInteger + 1);
  end
  else if FWB_Type = '浆轴' then
  begin
    mGroup := Group.Text;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;

    fDate := FormatDateTime('yyMM', SGetServerDate(ADOQueryTemp));

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      //////////////////            插入轴卡               //////////////////////
      sql.Add(' insert into Tat_WB_Card(WBCNO,TWPId,WBCID,WBType,WB_ProcessType,WB_Code,WB_Name,WB_YarnQty,WB_YARNARR,WB_YarnLen,WB_Macher,WB_MachNo,WB_Type,Note,FillId,Filler');
      sql.Add(' ,WB_Width,WB_ReedNo,WB_ReedNote,WBFtyNo,WBFtyName,WBBelong,WBBelongCoNo,WBBelongCoName  ');
      sql.Add(' ,GYSubid,GYNo,CustNo,CustName,GKBH,C_NAME,C_CODE,Z_FENJIAO,Z_FJGENSHU,Z_BINGSHU');
      SQL.Add(' ,BSQTY,DCRQTY,C_KCQTY,XZ_GS,XZ_YarnQty,serialNo,[Group]');
      sql.Add(' ) values');
      for i := 1 to MCardQty do
      begin
        if FWB_Type = '稀轴' then
        begin
          if GetLSNo(ADOQueryTemp, MWBCID, 'XZK' + fDate, 'Tat_WB_Card',4, 0) = False then
          begin
            raise Exception.Create('取布票流水号失败!');
          end;
        end
        else
        begin
          if GetLSNo(ADOQueryTemp, MWBCID, 'JZK' + fDate, 'Tat_WB_Card', 4, 0) = False then
          begin
            raise Exception.Create('取布票流水号失败!');
          end;
        end;

        if i > 1 then
          sql.Add(',');
        sql.Add(' (' + Quotedstr(Trim(FTWPId)));
        sql.Add(',' + Quotedstr(Trim(FTWPId)));
        sql.Add(',' + Quotedstr(Trim(MWBCID)));
        sql.Add(',' + Quotedstr(Trim(MWBType)));
        sql.Add(',' + Quotedstr(Trim(WB_ProcessType.Text)));
        sql.Add(',' + Quotedstr(Trim(WB_Code.Text)));
        sql.Add(',' + Quotedstr(Trim(WB_Name.Text)));
        if FWB_Type = '稀轴' then
        begin
//          if (i = MCardQty) and (Z_ZHOUSHU2.Text <> '') and (Z_ZHOUSHU2.Text <> '0') then
          if (i > StrToInt(Z_ZHOUSHU1.Text)) and (Z_ZHOUSHU2.Text <> '') and (Z_ZHOUSHU2.Text <> '0') then
          begin
            sql.Add(',' + (Trim(Z_FJGENSHU.Text)));
            sql.Add(',' + (Trim(Z_SJKESHU2.Text)));
          end
          else
          begin
            sql.Add(',' + (Trim(Z_FJGENSHU.Text)));
            sql.Add(',' + (Trim(Z_SJKESHU1.Text)));
          end;
        end
        else
        begin
          sql.Add(',' + (Trim(WB_YarnQty.Text)));
          sql.Add(',' + (Trim(Z_SJKESHU1.Text)));
        end;

        sql.Add(',' + FloatToStr(MWB_YarnLen));
        sql.Add(',' + Quotedstr(Trim(WB_Macher.Text)));
        sql.Add(',' + Quotedstr(Trim(WB_MachNo.Text)));
        sql.Add(',' + Quotedstr(Trim(WB_Type.Text)));
        sql.Add(',' + Quotedstr(Trim(Note.Text)));
        sql.Add(',' + Quotedstr(Trim(DCode)));
        sql.Add(',' + Quotedstr(Trim(DName)));
        sql.Add(',' + Quotedstr(Trim(WB_Width.Text)));
        sql.Add(',' + Quotedstr(Trim(WB_ReedNo.Text)));
        sql.Add(',' + Quotedstr(Trim(WB_ReedNote.Text)));
        sql.Add(',' + Quotedstr(Trim(WBFtyName.Properties.LookupItems.Text)));
        sql.Add(',' + Quotedstr(Trim(WBFtyName.Text)));
        sql.Add(',' + Quotedstr(Trim(MWBBelong)));
        sql.Add(',' + Quotedstr(Trim(MWBBelongCoNo)));
        sql.Add(',' + Quotedstr(Trim(MWBBelongCoName)));
        sql.Add(',' + Quotedstr(Trim(GYNo.Properties.LookupItems.Text)));
        sql.Add(',' + Quotedstr(Trim(GYNo.Text)));
        sql.Add(',' + Quotedstr(Trim(CustName.Properties.LookupItems.Text)));
        sql.Add(',' + Quotedstr(Trim(CustName.Text)));
        sql.Add(',' + Quotedstr(Trim(GKBH.Text)));
        sql.Add(',' + Quotedstr(Trim(C_NAME.Text)));
        sql.Add(',' + Quotedstr(Trim(C_CODE.Text)));

   /////cai改
        if Z_FENJIAO.Text <> '' then
        begin
          sql.Add(',' + (Trim(Z_FENJIAO.Text)));
        end
        else
        begin
          sql.Add(',0');
        end;

        if Z_FJGENSHU.Text <> '' then
        begin
          sql.Add(',' + (Trim(Z_FJGENSHU.Text)));
        end
        else
        begin
          sql.Add(',0');
        end;

        if Z_BINGSHU.Text <> '' then
        begin
          sql.Add(',' + (Trim(Z_BINGSHU.Text)));
        end
        else
        begin
          sql.Add(',0');
        end;

        if BSQTY.Text <> '' then
        begin
          sql.Add(',' + (Trim(BSQTY.Text)));
        end
        else
        begin
          sql.Add(',0');
        end;

        if DCRQTY.Text <> '' then
        begin
          sql.Add(',' + (Trim(DCRQTY.Text)));
        end
        else
        begin
          sql.Add(',0');
        end;

//        sql.Add(',' + (Trim(Z_FENJIAO.Text)));
//        sql.Add(',' + (Trim(Z_FJGENSHU.Text)));
//        sql.Add(',' + (Trim(Z_BINGSHU.Text)));

//        sql.Add(',' + (Trim(BSQTY.Text)));
//        sql.Add(',' + (Trim(DCRQTY.Text)));
        sql.Add(',' + Quotedstr(Trim(C_KCQTY.Text)));
        if XZ_GS.Text <> '' then
        begin
          sql.Add(',' + (Trim(XZ_GS.Text)));
        end
        else
        begin
          sql.Add(',0');
        end;
        if XZ_YarnQty.Text <> '' then
        begin
          sql.Add(',' + (Trim(XZ_YarnQty.Text)));
        end
        else
        begin
          sql.Add(',0');
        end;

        sql.Add(',' + Trim(IntToStr(i)));
        sql.Add(',' + Trim(mGroup));

        sql.Add(' )');
      end;
     //////////////////            插入轴卡               //////////////////////
     ///
     //////////////////            插入轴卡计划单               //////////////////////
//      if not CDS_1.IsEmpty then
//      begin
//        with CDS_1 do
//        begin
//          First;
//          sql.Add(' delete Tat_WB_Card_Link where WBCNO=' + Quotedstr(Trim(FWBCNO)));
//
//          while not Eof do
//          begin
//            sql.Add(' insert into Tat_WB_Card_Link(MainId,SubID,WBCNO) values');
//            sql.Add(' (' + Quotedstr(Trim(CDS_1.FieldByName('MainId').AsString)));
//            sql.Add(',' + Quotedstr(Trim(CDS_1.FieldByName('SubID').AsString)));
//            sql.Add(',' + Quotedstr(Trim(FWBCNO)));
//            sql.Add(' )');
//            Next;
//          end;
//        end;
//      end;
      //////////////////            插入轴卡计划单               //////////////////////
//      showMessage(sql.Text);
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from V_Tat_WB_CardGrop A where A.WBCNO=''' + Trim(FWBCNO) + '''');
      Open;
    end;
    if ADOQueryTemp.RecordCount > 1 then
      raise Exception.Create('计划单信息不能变更!');

    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
  InitCard();
end;

procedure TfrmTatWBCardInPut2.ToolButton1Click(Sender: TObject);
var
  WSql: string;
begin

  if CDS_Card.IsEmpty then
    Exit;
  if CDS_Card.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  WSql := '';
  CDS_Card.DisableControls;

  with CDS_Card do
  begin
    First;
    while not Eof do
    begin
      if CDS_Card.fieldbyname('SSel').AsBoolean then
      begin
        if WSql <> '' then
        begin
          WSql := WSql + ',' + Trim(CDS_Card.fieldbyname('WBCID').AsString);
        end
        else
        begin
          WSql := Trim(CDS_Card.fieldbyname('WBCID').AsString);
        end;
      end;
      Next;
    end;
  end;

  CDS_Card.Locate('SSel', True, []);
  CDS_Card.EnableControls;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FPreviewPrint := False;
      if FWB_Type = '稀轴' then
      begin
        FLMType := 'TatWBCardXZ';
      end
      else if FWB_Type = '浆轴' then
      begin
        FLMType := 'TatWBCardJZ';
      end;


//      FLMType := 'TatWBCard';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update Tat_WB_Card         ');
          sql.Add(' set PRTer=' + quotedstr(DName));
          sql.Add(', PRTCount=PRTCount+1');
          sql.Add(', PRTDate=getdate()');
          sql.Add(' where WBCID in (' + quotedstr(WSql) + ')');
//          showMessage(sql.Text);

          ExecSQL;
        end;
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmTatWBCardInPut2.ToolButton2Click(Sender: TObject);
begin

  WriteCxGrid(Trim(self.Caption) + 'Tv2', Tv2, '坯布轴卡');
end;

procedure TfrmTatWBCardInPut2.ToolButton3Click(Sender: TObject);
begin
  Note.SetFocus;
  try
    CDS_Card.DisableControls;
    CDS_Card.First;
    while not CDS_Card.Eof do
    begin
      ADOQueryCmd.Connection.BeginTrans;
      try
        if CDS_Card.FieldByName('SSel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select * from Tat_WB_Card A');
            sql.Add('where WBCID=''' + trim(CDS_Card.fieldbyname('WBCID').AsString) + '''');
            open;
            edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);

            FieldByName('custNo').Value := Trim(custName.Properties.LookupItems.Text);
//          RTSetsavedata(ADOQueryCmd, 'Tat_WB_Card', Panel1, 1);
            RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_Card, 'Tat_WB_Card', 0);
            post;
          end;
        end;
        ADOQueryCmd.Connection.CommitTrans;
      except
        ADOQueryCmd.Connection.RollbackTrans;
        raise Exception.Create(PChar(Exception(ExceptObject).Message));
      end;
      CDS_Card.Next;
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from Tat_WB_Plan A where A.TWPId=''' + Trim(FTWPId) + '''');
      Open;
      Edit;
      RTSetsavedata(ADOQueryCmd, 'Tat_WB_Plan', Panel1, 1);
      Post;
    end;
    Application.MessageBox('保存修改成功！', '提示信息', MB_OK + MB_ICONINFORMATION);
    CDS_Card.EnableControls;
  except
    CDS_Card.EnableControls;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatWBCardInPut2.ToolButton8Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_Card.IsEmpty then
    Exit;
  if CDS_Card.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  WSql := '';
  CDS_Card.DisableControls;

  while CDS_Card.Locate('SSel', True, []) do
  begin
    if CDS_Card.fieldbyname('SSel').AsBoolean then
    begin
      if WSql <> '' then
      begin
        WSql := WSql + ',' + (Trim(CDS_Card.fieldbyname('WBCID').AsString));
      end
      else
      begin
        WSql := (Trim(CDS_Card.fieldbyname('WBCID').AsString));
      end;
    end;
    CDS_Card.Delete;
  end;
  CDS_Card.EnableControls;

  try
    ADOQueryCmd.Connection.BeginTrans;
//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('insert into Knit_Need_Up(UOperation,UType,UDataId) values(''检验'',''检验删除'',' + quotedstr(MCIIDS) + ')  ');
//      ExecSQL;
//    end;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_TatWB_Card_Del ');
      Sql.Add('@WBCIDS=' + quotedstr(Trim(WSql)));
      Sql.Add(',@DCode=' + Quotedstr(DCode));
      Sql.Add(',@DName=' + Quotedstr(DName));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;

    application.MessageBox('删除成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatWBCardInPut2.TV2Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmWBTypeSel := TfrmWBTypeSel.Create(Application);
    with frmWBTypeSel do
    begin
      if ShowModal = 1 then
      begin
        with CDS_Card do
        begin
          Edit;
          FieldByName('WBType').Value := Trim(frmWBTypeSel.CDS_Main.fieldbyname('WBType').AsString);
          Post;
        end;
      end;
    end;
  finally
    frmWBTypeSel.Free;
  end;
end;

procedure TfrmTatWBCardInPut2.TV2Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with CDS_Card do
        begin
          Edit;
          FieldByName('WBFtyNo').Value := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('WBFtyName').Value := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
          Post;
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTatWBCardInPut2.TV2Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with CDS_Card do
        begin
          Edit;
          FieldByName('WBBelongCoNo').Value := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('WBBelongCoName').Value := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
          Post;
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

end.

