unit U_TatWBCardInPut;

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, dxCore, cxDateUtils;

type
  TfrmTatWBCardInPut = 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;
    cxGridDBColumn9: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    ToolButton1: TToolButton;
    ToolButton8: TToolButton;
    ToolButton2: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    GPM_2: TcxGridPopupMenu;
    Label7: TLabel;
    Label1: TLabel;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    WBCNO: TcxTextEdit;
    WB_Code: TcxButtonEdit;
    Label5: TLabel;
    WB_Name: TcxTextEdit;
    Label11: TLabel;
    WB_YarnQty: 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;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    v1Column18: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1SPName: TcxGridDBColumn;
    v1SPSpec: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    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;
    TV3Column1: TcxGridDBColumn;
    Label38: TLabel;
    GYNo: TcxButtonEdit;
    Label47: TLabel;
    CustName: TcxButtonEdit;
    Label8: TLabel;
    CONNO: TcxTextEdit;
    Label14: TLabel;
    Label20: TLabel;
    Label39: TLabel;
    WB_Width: TcxTextEdit;
    WB_ReedNo: TcxTextEdit;
    GKBH: TcxButtonEdit;
    Label22: TLabel;
    Label23: TLabel;
    BSQTY: TcxTextEdit;
    DCRQTY: TcxTextEdit;
    Label37: TLabel;
    C_KCQTY: TcxTextEdit;
    Label41: TLabel;
    WB_ReedNote: TcxTextEdit;
    Label12: TLabel;
    Label13: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    F_MAXKESHU: TcxTextEdit;
    F_YarnStripQty: TcxTextEdit;
    F_SJKESHU: TcxTextEdit;
    F_YUSHU: TcxTextEdit;
    Label17: TLabel;
    C_CODE: TcxTextEdit;
    Label19: TLabel;
    WB_TYPE: TcxTextEdit;
    BegDate: TcxDateEdit;
    Label18: TLabel;
    Label40: TLabel;
    ordqty: TcxTextEdit;
    TV3ZS: TcxGridDBColumn;
    TV3BatchNO: TcxGridDBColumn;
    TV3dbNumber: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    Label42: TLabel;
    WB_YarnKCQty: TcxTextEdit;
    Label43: TLabel;
    BCRFQty: TcxTextEdit;
    Label44: TLabel;
    Label45: TLabel;
    Label46: TLabel;
    Label48: TLabel;
    Label49: TLabel;
    Label50: TLabel;
    Label51: TLabel;
    TV3Supplier: TcxGridDBColumn;
    TV3IOTime: 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 GYNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure GYNoDblClick(Sender: TObject);
    procedure GKBHPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV3BatchNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  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: string;

    { Public declarations }
  end;

var
  frmTatWBCardInPut: TfrmTatWBCardInPut;

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, U_WeaveSel, U_YarnStkSel;

{$R *.dfm} procedure TfrmTatWBCardInPut.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 TfrmTatWBCardInPut.InitCardGrop();
begin

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from V_Tat_WB_CardGrop A where A.WBCNO=''' + Trim(FWBCNO) + '''');
    Open;
  end;
  SCSHData(ADOQueryTemp, Panel1, 1);
  GYNo.Properties.LookupItems.Text := Trim(ADOQueryTemp.fieldbyname('GYSubid').AsString);
  CustName.Properties.LookupItems.Text := Trim(ADOQueryTemp.fieldbyname('CustNo').AsString);
//  WBFtyName.Properties.LookupItems.Text := Trim(ADOQueryTemp.fieldbyname('WBFtyNo').AsString);

end;

function TfrmTatWBCardInPut.TestData(): Boolean;
begin
  try

    if Trim(FWBCNO) = '' then
    begin
      if GetLSNo(ADOQueryTemp, FWBCNO, 'QJ', 'Tat_WB_Card', 3, 1) = False then
      begin
        raise Exception.Create('迁经单号失败!');
      end;
      WBCNO.Text := FWBCNO;
    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 trim(WB_ProcessType.Text) <> '本厂整经' then
      if trim(WBFtyName.Text) = '' 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('类型不能为空！');
    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatWBCardInPut.InitCard();
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*  ');
    sql.Add('from Tat_WB_Card A   ');
    sql.Add('where isnull(Status,''0'')<>''-1'' and WBCNO=' + quotedstr(Trim(FWBCNO)));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Card);
  SInitCDSData(ADOQueryTemp, CDS_Card);
end;

procedure TfrmTatWBCardInPut.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 TfrmTatWBCardInPut.WBFtyNameDblClick(Sender: TObject);
begin
  self.WBFtyName.Properties.LookupItems.Text := '';
  self.WBFtyName.Text := '';
end;

procedure TfrmTatWBCardInPut.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 TfrmTatWBCardInPut.WB_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmWBSpecSel := TfrmWBSpecSel.Create(Application);
    with frmWBSpecSel do
    begin

      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.gkbh.Text := Trim(frmWBSpecSel.CDS_1.fieldbyname('gkbh').AsString);

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

procedure TfrmTatWBCardInPut.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 TfrmTatWBCardInPut.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 TfrmTatWBCardInPut.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 TfrmTatWBCardInPut.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmTatWBCardInPut.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(Trim(self.Caption) + 'Tv3', Tv3, '坯布轴卡');
  ReadCxGrid(Trim(self.Caption) + 'Tv2', Tv2, '坯布轴卡');
  InitCardGrop();
  InitCard();

  InitCard3();
end;

procedure TfrmTatWBCardInPut.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 TfrmTatWBCardInPut.GYNoDblClick(Sender: TObject);
begin
  inherited;
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
  TcxButtonEdit(Sender).Text := '';
end;

procedure TfrmTatWBCardInPut.GYNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  try
    frmWeaveSel := TfrmWeaveSel.Create(Application);
    with frmWeaveSel do
    begin
      FISSJ := '否';
      if ShowModal = 1 then
      begin
        Self.GYNo.Text := CDS_1.FieldByName('OrderNo').asstring;
        Self.GYNo.Properties.LookupItems.Text := CDS_1.FieldByName('Subid').asstring;
        Self.CustName.Text := CDS_1.FieldByName('CustName').asstring;

        Self.WB_Code.Text := CDS_1.FieldByName('WB_Code').asstring;
        Self.CONNO.Text := Trim(CDS_1.fieldbyname('CONNO').AsString);
        Self.C_CODE.Text := Trim(CDS_1.fieldbyname('C_CODE').AsString);
        Self.BegDate.Date := CDS_1.fieldbyname('ORDDATE').AsDateTime;
        Self.ordqty.Text := Trim(CDS_1.fieldbyname('ordqty2').AsString);
        with ADOQueryTemp do
        begin
          close;
          sql.Clear;
          sql.Add(' select A.* ');
          sql.Add(' from Tat_WB_Spec A');
          sql.Add(' where WB_Code=''' + trim(WB_Code.Text) + ''' ');
          Open;
        end;

        Self.WB_Code.Text := Trim(ADOQueryTemp.fieldbyname('WB_Code').AsString);
        Self.WB_Name.Text := Trim(ADOQueryTemp.fieldbyname('WB_Name').AsString);
        Self.WB_YarnQty.Text := ADOQueryTemp.fieldbyname('WB_YarnQty').asstring;

        Self.F_MAXKESHU.Text := ADOQueryTemp.fieldbyname('F_MAXKESHU').asstring;
        Self.F_SJKESHU.Text := ADOQueryTemp.fieldbyname('F_SJKESHU').asstring;
        Self.F_YarnStripQty.Text := ADOQueryTemp.fieldbyname('F_YarnStripQty').asstring;
        Self.F_YUSHU.Text := ADOQueryTemp.fieldbyname('F_YUSHU').asstring;
        Self.GKBH.Text := ADOQueryTemp.fieldbyname('GKBH').asstring;
        Self.BSQTY.Text := ADOQueryTemp.fieldbyname('BSQTY').asstring;
        Self.DCRQTY.Text := ADOQueryTemp.fieldbyname('DCRQTY').asstring;
        Self.C_KCQTY.Text := ADOQueryTemp.fieldbyname('C_KCQTY').asstring;

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

        Self.Note.Text := Trim(ADOQueryTemp.fieldbyname('Note').AsString);
        Self.InitCard3();

      end;
    end;
  finally
    frmWeaveSel.Free;
  end;
end;

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

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

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

procedure TfrmTatWBCardInPut.TBSaveClick(Sender: TObject);
begin
  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 TfrmTatWBCardInPut.KaiKa(MWBType, MWBBelong, MWBBelongCoNo, MWBBelongCoName: string; MCardQty: Integer; MWB_YarnLen: Double);
var
  MWBCID: string;
  i, j: integer;
  fDate: string;
  fMonth: string;
begin
  if MCardQty = 0 then
    EXIT;

  try
    ADOQueryCmd.Connection.BeginTrans;

    fDate := FormatDateTime('yy', SGetServerDate(ADOQueryTemp));
    fMonth := FormatDateTime('mm', 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_CODE');
      SQL.Add(' ,BSQTY,DCRQTY,C_KCQTY');
      SQL.Add(' ,BegDate,F_SJKESHU,F_YarnStripQty,F_YUSHU,ordqty');
      sql.Add(' ) values');
      for i := 1 to MCardQty do
      begin
        if GetLSNo(ADOQueryTemp, MWBCID, 'JZ' + fDate + fMonth, 'Tat_WB_Card', 3, 0) = False then
        begin
          raise Exception.Create('取布票流水号失败!');
        end;

        if i > 1 then
          sql.Add(',');
        sql.Add(' (' + Quotedstr(Trim(WBCNO.Text)));
        sql.Add(',' + Quotedstr(Trim(WBCNO.Text)));
        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)));
        sql.Add(',' + (Trim(WB_YarnQty.Text)));
        sql.Add(',' + (Trim(F_SJKESHU.Text)));
        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)));//GYSubid
        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_CODE.Text)));

        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(',' + Quotedstr(Trim(C_KCQTY.Text)));

        sql.Add(',' + Quotedstr(Trim(BegDate.Text)));

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

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

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

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

      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 TfrmTatWBCardInPut.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 + ',' + QuotedStr(Trim(CDS_Card.fieldbyname('WBCID').AsString));
        end
        else
        begin
          WSql := QuotedStr(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;
      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 (' + WSql + ')');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

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

procedure TfrmTatWBCardInPut.ToolButton3Click(Sender: TObject);
begin
  Note.SetFocus;
  try
    CDS_Card.DisableControls;
    CDS_Card.First;
    while not CDS_Card.Eof do
    begin
      ADOQueryCmd.Connection.BeginTrans;
      try
        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);
          RTSetsavedata(ADOQueryCmd, 'Tat_WB_Card', Panel1, 1);
          RTSetSaveDataCDS(ADOQueryCmd, Tv2, CDS_Card, 'Tat_WB_Card', 0);
          post;
        end;
        ADOQueryCmd.Connection.CommitTrans;
      except
        ADOQueryCmd.Connection.RollbackTrans;
        raise Exception.Create(PChar(Exception(ExceptObject).Message));
      end;
      CDS_Card.Next;
    end;
    CDS_Card.EnableControls;
  except
    CDS_Card.EnableControls;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatWBCardInPut.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 TfrmTatWBCardInPut.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 TfrmTatWBCardInPut.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 TfrmTatWBCardInPut.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;

procedure TfrmTatWBCardInPut.TV3BatchNOPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmYarnStkSel := TfrmYarnStkSel.Create(Application);
    with frmYarnStkSel do
    begin
//      FstkName := Self.FstkName;
      if ShowModal = 1 then
      begin
        frmYarnStkSel.CDS_Main.DisableControls;
        with frmYarnStkSel.CDS_Main do
        begin

          with Self.CDS_3 do
          begin
            Edit;
            FieldByName('BatchNo').Value := frmYarnStkSel.CDS_Main.fieldbyname('BatchNo').Value;
            FieldByName('singleWeight').Value := frmYarnStkSel.CDS_Main.fieldbyname('singleWeight').Value;
            FieldByName('dbNumber').Value := frmYarnStkSel.CDS_Main.fieldbyname('dbNumber').Value;
            FieldByName('Supplier').Value := frmYarnStkSel.CDS_Main.fieldbyname('Supplier').Value;
            FieldByName('IOTime').Value := frmYarnStkSel.CDS_Main.fieldbyname('IOTime').Value;
            Post;
          end;
        end;
        frmYarnStkSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmYarnStkSel.Free;
  end;

end;

end.

