unit U_TatWBPlanCard;

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,
  BtnEdit, RM_BarCode, cxPC, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, U_BaseList, Vcl.Menus, cxContainer,
  dxScrollbarAnnotations, cxMemo, FrameDateSel, dxSkinsCore,
  dxSkinsDefaultPainters, cxProgressBar, dxSkinOffice2013White, dxSkinSharpPlus,
  dxSkinWXI;

type
  TfrmTatWBPlanCard = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryPrint: TADOQuery;
    Label1: TLabel;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    TV1Column8: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    V2Column1: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    TV1Column12: TcxGridDBColumn;
    TV1Column13: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    TV1Column15: TcxGridDBColumn;
    TV1Column16: TcxGridDBColumn;
    TV1Column17: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    WBCNO: TcxTextEdit;
    Label2: TLabel;
    WBCIDS: TcxTextEdit;
    Label3: TLabel;
    WB_Code: TcxTextEdit;
    DS_2: TDataSource;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    ADO_2: TADOQuery;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: 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;
    WB_YarnArrNote: TcxMemo;
    Frame11: TfrmFrameDateSel;
    TV2Column2: TcxGridDBColumn;
    dscds_3: TClientDataSet;
    ds3: TDataSource;
    TV1Column23: TcxGridDBColumn;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    cxGridDBColumn34: TcxGridDBColumn;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridDBColumn37: TcxGridDBColumn;
    cxGridDBColumn38: TcxGridDBColumn;
    cxGridDBColumn39: TcxGridDBColumn;
    cxGridDBColumn40: TcxGridDBColumn;
    cxGridDBColumn41: TcxGridDBColumn;
    cxGridDBColumn42: TcxGridDBColumn;
    cxGridDBColumn43: TcxGridDBColumn;
    cxGridDBColumn44: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    CDS_4: TClientDataSet;
    DS_4: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Tv4Column1: TcxGridDBColumn;
    Tv4Column2: TcxGridDBColumn;
    Tv4Column3: TcxGridDBColumn;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    TV3Column4: TcxGridDBColumn;
    TV3Column5: TcxGridDBColumn;
    TV3Column1: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    TV3Column3: TcxGridDBColumn;
    TV3Column6: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    TabSheet2: TTabSheet;
    cxGrid5: TcxGrid;
    Tv5: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    CDS_5: TClientDataSet;
    DS_5: TDataSource;
    ADO_5: TADOQuery;
    Tv5Column1: TcxGridDBColumn;
    Tv5Column2: TcxGridDBColumn;
    Tv5Column3: TcxGridDBColumn;
    Tv5Column4: TcxGridDBColumn;
    Tv5Column5: TcxGridDBColumn;
    Label4: TLabel;
    WB_Name: TcxTextEdit;
    TV2Column1: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    ADO_4: TADOQuery;
    TV1Column2: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    ToolButton4: TToolButton;
    TV1Column3: TcxGridDBColumn;
    ToolButton7: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure WBCNOPropertiesChange(Sender: TObject);
    procedure TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure Tv4FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
  private
    FInt, PFInt: Integer;
    FFMainID, BPFlag: string;
    procedure InitGrid();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LabelPrint, U_TatWBCardInPut,
  U_TatWBCardInPutOld, U_RPFun;

{$R *.dfm}

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

procedure TfrmTatWBPlanCard.TBCloseClick(Sender: TObject);
begin
  Close;

end;

procedure TfrmTatWBPlanCard.InitGrid();
begin
  FFMainID := '';
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*  ');
      sql.Add(',OrderNoS=SUBSTRING(CAST((select distinct '','' +Y.OrderNo from Tat_WB_Card_Link X inner join Tat_plan_Main Y on X.mainid=Y.mainid where X.WBCNO=A.WBCNO for xml path('''') ) AS VARCHAR(100)) , 2, 100)         ');
      sql.Add(',ConNoS=SUBSTRING(CAST((select distinct '','' +Y.ConNo from Tat_WB_Card_Link X inner join Tat_plan_Sub Y on X.Subid=Y.Subid where X.WBCNO=A.WBCNO for xml path('''') ) AS VARCHAR(100)) , 2, 100)         ');
      sql.Add(',C_NameS=SUBSTRING(CAST((select distinct '','' +Y.C_Name from Tat_WB_Card_Link X inner join Tat_plan_Sub Y on X.Subid=Y.Subid where X.WBCNO=A.WBCNO for xml path('''') ) AS VARCHAR(100)) , 2, 100)         ');
      sql.Add(',C_CodeoS=SUBSTRING(CAST((select distinct '','' +Y.C_Code from Tat_WB_Card_Link X inner join Tat_plan_Sub Y on X.Subid=Y.Subid where X.WBCNO=A.WBCNO for xml path('''') ) AS VARCHAR(100)) , 2, 100)         ');
      sql.Add(',HZWB_YarnLen=(select Sum(WB_YarnLen) from Tat_WB_Card X where A.WBCNO =X.WBCNO) ');
      sql.Add(' from  V_Tat_WB_CardGrop A ');

      sql.Add(' where  1=1');
      sql.add(' and A.WB_Date>=''' + Trim(FormatDateTime('yyyy-MM-dd', Frame11.BegDate.Date)) + '''');
      sql.Add(' and A.WB_Date<''' + Trim(FormatDateTime('yyyy-MM-dd', Frame11.enddate.Date + 1)) + '''');
      sql.Add(' and WB_Type  not in (''浆轴'',''稀轴'')');
//      showmessage(Sql.text);

//      ShowMessage(sql.text);
//      case cxTabControl1.TabIndex of
//        0:
//          begin
//            sql.Add(' and PRTCount=0');
//          end;
//        1:
//          begin
//            sql.Add(' and PRTCount>0');
//            sql.add(' and A.Filltime>=''' + Trim(FormatDateTime('yyyy-MM-dd', Frame11.BegDate.Date)) + '''');
//            sql.Add(' and A.Filltime<''' + Trim(FormatDateTime('yyyy-MM-dd', Frame11.enddate.Date + 1)) + '''');
//          end;
//        2:
//          begin
//
//          end;
//      end;

      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmTatWBPlanCard.N1Click(Sender: TObject);
begin
  SelOKNo(cds_1, true)
end;

procedure TfrmTatWBPlanCard.N2Click(Sender: TObject);
begin
  SelOKNo(cds_1, false);
end;

procedure TfrmTatWBPlanCard.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmTatWBPlanCard.ToolButton1Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_4.IsEmpty then
    Exit;

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

  WSql := '';
  CDS_4.DisableControls;

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

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

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FPreviewPrint := False;
      FLMType := 'TatWBCard';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
        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 TfrmTatWBPlanCard.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '经轴管理');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '经轴管理');
  WriteCxGrid(trim(self.Caption) + 'Tv3', Tv3, '经轴管理');
  WriteCxGrid(trim(self.Caption) + 'Tv4', Tv4, '经轴管理');
end;

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

  WSql := SelCDSKey(CDS_4, ['WBCID'])[0];

  Tv1.OnFocusedRecordChanged := TV1FocusedRecordChanged;
  try
    ADOQueryCmd.Connection.BeginTrans;
    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;
    InitGrid();
    application.MessageBox('删除成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTatWBPlanCard.ToolButton4Click(Sender: TObject);
begin
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FPreviewPrint := False;
      FLMType := 'WBJH';
      FFiltration1 := Trim(Self.CDS_1.fieldbyname('WBCNO').AsString);
      if ShowModal = 1 then
      begin

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

procedure TfrmTatWBPlanCard.ToolButton5Click(Sender: TObject);
begin

  try
    frmTatWBCardInPut := TfrmTatWBCardInPut.Create(Application);
    with frmTatWBCardInPut do
    begin
      FWBCNO := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTatWBCardInPut.Free;
  end;
end;

procedure TfrmTatWBPlanCard.ToolButton6Click(Sender: TObject);
var
  i: Integer;
begin
  try
    frmTatWBCardInPut := TfrmTatWBCardInPut.Create(Application);
    with frmTatWBCardInPut do
    begin
      for i := 0 to frmTatWBCardInPut.panel1.ControlCount - 1 do
      begin
        frmTatWBCardInPut.panel1.Controls[i].Enabled := false;
      end;
      frmTatWBCardInPut.cxGrid3.Enabled := True;
      FWBCNO := Trim(Self.CDS_1.fieldbyname('WBCNO').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTatWBCardInPut.Free;
  end;
end;

procedure TfrmTatWBPlanCard.ToolButton7Click(Sender: TObject);
var
  i: integer;
  MPCID: string;
  MPrintJson: string;
begin

  if CDS_4.IsEmpty then
    Exit;
  if CDS_4.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  i := 0;
  with CDS_4 do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      if Fieldbyname('Ssel').AsBoolean then
      begin
        if i = 0 then
          MPCID := trim(CDS_4.fieldbyname('WBCID').AsString);
//        PrintCar(trim(CDS_2.fieldbyname('PCID').AsString));
        MPrintJson := '{ "LBName": "整经轴卡","IsPreview": false, "PrtArgs": [ {"SqlStr": "EXEC P_TatWB_Card_Prt @Filtration=' +  (MPCID) + ' " }, {"SqlStr": "EXEC P_TatWB_Card_Prt2 @Filtration=' +  (MPCID) + '" } ] }';

        FunPrintRmf(Application, PChar(DConString), PChar(MPrintJson));
        i := i + 1;
      end;
      Next;
    end;
    EnableControls;
  end;
  SelOKNoFiler(Tv4, False);
  CDS_4.Locate('WBCID', MPCID, []);
end;

procedure TfrmTatWBPlanCard.TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  WB_YarnArrNote.Text := Trim(CDS_1.fieldbyname('WB_YarnArrNote').AsString);

  with ADO_4 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*   ');
    sql.add(' ,jgType=(SELECT top 1 jgType from Tat_Wb_IO X where X.WBCID=A.WBCID and X.IOFlag =''入库'' and X.STKName =''经轴'' )');
    sql.add(' from Tat_WB_Card A ');
    sql.Add(' where WBCNO=''' + Trim(CDS_1.fieldbyname('WBCNO').AsString) + '''');
//    ShowMessage(sql.text);
    Open;
  end;
  SCreateCDS(ADO_4, CDS_4);
  SInitCDSData(ADO_4, CDS_4);

end;

procedure TfrmTatWBPlanCard.Tv4FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADO_2 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*   ');
    sql.add(' from Tat_WB_Formula A inner join Tat_WB_Spec B on A.WBSID=B.WBSID');
    sql.Add(' where WB_Code=''' + Trim(CDS_4.fieldbyname('WB_Code').AsString) + '''');
//    showmessage(sql.text);
    Open;
  end;
  SCreateCDS(ADO_2, CDS_2);
  SInitCDSData(ADO_2, CDS_2);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' SELECT A.QTY,A.StaffName,A.WbNo,WBCID=A.PCID ,A.FillTime ');
    sql.add(' ,rktime=(SELECT top 1 filltime from Tat_Wb_IO X where X.WBCID=A.PCID and X.IOFlag =''入库'' and X.STKName =''经轴'' )');
    sql.add(' ,Cktime=(SELECT top 1 filltime from Tat_Wb_IO X where X.WBCID=A.PCID and X.IOFlag =''出库'' and X.STKName =''经轴'')');
    sql.add(' ,rkqTY=(SELECT Sum(Qty) from Tat_Wb_IO X where X.WBCID=A.PCID and X.IOFlag =''入库'' and X.STKName =''经轴'')');
    sql.add(' ,CkqTY=(SELECT Sum(Qty) from Tat_Wb_IO X where X.WBCID=A.PCID and X.IOFlag =''出库'' and X.STKName =''经轴'')');
    sql.add(' from Tat_Plan_OutPut A  ');
    sql.Add(' where  PCID=''' + Trim(CDS_4.fieldbyname('WBCID').AsString) + '''');
//    showmessage(sql.text);
    Open;
  end;
  SCreateCDS(ADOQueryTemp, dscds_3);
  SInitCDSData(ADOQueryTemp, dscds_3);

  with ADO_5 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,B.* ');
    sql.Add(' from Tat_WB_IO A inner join Tat_WB_Card B on A.WBCID=B.WBCID ');
    sql.Add(' where B.WBCID=''' + Trim(CDS_4.fieldbyname('WBCID').AsString) + '''');
    sql.Add(' and isnull(stkName,'''') =''经轴'' ');
//    showmessage(sql.text);
    Open;
  end;
  SCreateCDS(ADO_5, CDS_5);
  SInitCDSData(ADO_5, CDS_5);
end;

procedure TfrmTatWBPlanCard.WBCNOPropertiesChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmTatWBPlanCard.FormShow(Sender: TObject);
begin
  inherited;
  Frame11.EndDate.Date := SGetServerDate(ADOQueryTemp);
  Frame11.BegDate.Date := Frame11.EndDate.Date - 30;

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '经轴管理');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '经轴管理');
  ReadCxGrid(trim(self.Caption) + 'Tv3', Tv3, '经轴管理');
  ReadCxGrid(trim(self.Caption) + 'Tv4', Tv4, '经轴管理');
  InitGrid();
end;

procedure TfrmTatWBPlanCard.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

end.

