unit U_TradePlanCard;

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, Vcl.Clipbrd,  dxScrollbarAnnotations,
  FrameDateSel, cxContainer, cxMaskEdit, cxDropDownEdit, Vcl.Printers;

type
  TfrmTradePlanCard = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADO_2: TADOQuery;
    ADOQueryTemp: TADOQuery;
    Label3: TLabel;
    ConNo: TcxTextEdit;
    Label2: TLabel;
    C_Name: TcxTextEdit;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    Label4: TLabel;
    C_Code: TcxTextEdit;
    ToolButton2: TToolButton;
    Label5: TLabel;
    OrderNo: TcxTextEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton3: TToolButton;
    N3: TMenuItem;
    ToolButton5: TToolButton;
    frmFrameDateSel1: TfrmFrameDateSel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    v1Column16: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v3Column8: TcxGridDBColumn;
    Tv2Column7: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Tv2Column4: TcxGridDBColumn;
    Tv2Column5: TcxGridDBColumn;
    Tv2Column6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxTabControl1: TcxTabControl;
    ToolBar2: TToolBar;
    ToolButton8: TToolButton;
    ToolButton10: TToolButton;
    ADO_1: TADOQuery;
    GPM_2: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    Label1: TLabel;
    Label6: TLabel;
    CraftCode: TcxTextEdit;
    GC_Name: TcxTextEdit;
    RM1: TRMGridReport;
    RMDB_1: TRMDBDataSet;
    ADOQuery1: TADOQuery;
    ToolButton1: TToolButton;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column8: TcxGridDBColumn;
    Tv2Column9: TcxGridDBColumn;
    ComboBox_Print: TcxComboBox;
    ADOQueryPrint: TADOQuery;
    Label7: TLabel;
    Tv2Column10: TcxGridDBColumn;
    Tv2Column11: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Label8: TLabel;
    cxComboBox1: TcxComboBox;
    Tv2Column12: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure GC_NameKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton8Click(Sender: TObject);
  private
    FInt, PFInt: Integer;
    FFMainID, BPFlag: string;
    procedure InitGrid1();
    procedure InitGrid2();
    procedure PrtData(MCIID: string);
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LabelPrint, U_TradeCardInPut,
  U_TradeCardInStk, U_PrintLabFun;


{$R *.dfm}
procedure TfrmTradePlanCard.PrtData(MCIID: string);
var
  fPrintFile, FLabVolume: string;
  Txt, fImagePath, Txt2, fImagePath2: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' EXEC P_Trade_Card_Prt1 ');
    SQL.Add(' @Filtration=''' + Trim(MCIID) + '''');
//    ShowMessage(sql.Text);
    Open;
  end;
  if ADOQueryPrint.IsEmpty then
  begin
    application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
    exit;
  end;
  FLabVolume := Trim(ADOQueryPrint.fieldbyname('LabVolume').AsString);
  ExportFtErpFile(FLabVolume + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + FLabVolume + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.DefaultCopies := 1;
    RM1.PrintReport;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update Trade_Plan_Card         ');
      sql.Add('set PRTer=' + quotedstr(DName));
      sql.Add(', PRTCount=PRTCount+1');
      sql.Add(', PRTDate=getdate()');
      sql.Add(' where  EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MCIID) + ','','') X where Trade_Plan_Card.PCID=X.RTValue )  ');
      ExecSQL;
    end;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmTradePlanCard.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
  WriteCxGrid('DCG13', Tv1, '坯布生产2');
end;

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

procedure TfrmTradePlanCard.InitGrid1();
var
  Msql, mbegdate, menddate: string;
  SqlStr: string;
begin
  SqlStr := 'select * from Trade_Plan_Main A   ';
  SqlStr := SqlStr + ' left join Trade_Plan_Sub B on A.MainID=B.MainID   ';
  SqlStr := SqlStr + ' left join Trade_Plan_Fty C on A.MainID=C.MainID   ';
  SqlStr := SqlStr + ' left join Trade_Cloth_Inspect_Rule D on A.MainID=D.RuleID  ';
  SqlStr := SqlStr + ' where A.OrdDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)) + '''';
  SqlStr := SqlStr + ' and A.OrdDate<''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)) + '''';
  SqlStr := SqlStr + ' and  OrdType in (''大货单'',''打样单'',''开发单'',''采购单'')';
  SqlStr := SqlStr + ' and ISNULL(A.status,'''')<>''10'' ';
//  ShowMessage(SqlStr);
  InitCDSData(ADO_1, CDS_1, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'SubId');
end;

procedure TfrmTradePlanCard.InitGrid2();
var
  SqlStr: string;
begin

  case cxTabControl1.TabIndex of
    0:
      begin
        SqlStr := 'select * from Trade_Plan_Card A   ';
      end;
    1:
      begin
        SqlStr := 'select * from Trade_Plan_Card_Del A   ';
      end;
  end;
  SqlStr := SqlStr + ' where A.SubId=' + QuotedStr(Trim(CDS_1.FieldByName('SubId').AsString));

  InitCDSData(ADO_2, CDS_2, Tv2, SqlStr, '', '');
end;

procedure TfrmTradePlanCard.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_2, true)
end;

procedure TfrmTradePlanCard.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_2, false);
end;

procedure TfrmTradePlanCard.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_2.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmTradePlanCard.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid1();
end;

procedure TfrmTradePlanCard.ToolButton10Click(Sender: TObject);
begin
//  PrtData(SelCDSKey(CDS_2, ['PCID'])[0]);
  if cxTabControl1.TabIndex <> 0 then
    Exit;

  PrintServerLabel(ADOQueryPrint, StrToIntDef(cxComboBox1.text, 1), 1, ComboBox_Print.ItemIndex, SelCDSKey(CDS_2, ['PCID'])[0]);
  InitGrid2();
end;

procedure TfrmTradePlanCard.ToolButton1Click(Sender: TObject);
begin

  try
    frmTradeCardInStk := TfrmTradeCardInStk.Create(Application);
    with frmTradeCardInStk do
    begin
      FMainId := Trim(Self.CDS_1.fieldbyname('MainId').AsString);
      FSubId := Trim(Self.CDS_1.fieldbyname('SubId').AsString);
      if ShowModal = 1 then
      begin
        InitGrid2();
      end;
    end;
  finally
    frmTradeCardInStk.Free;
  end;

end;

procedure TfrmTradePlanCard.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '坯布生产2');
end;

procedure TfrmTradePlanCard.ToolButton3Click(Sender: TObject);
begin

  try
    frmTradeCardInPut := TfrmTradeCardInPut.Create(Application);
    with frmTradeCardInPut do
    begin
      FMainId := Trim(Self.CDS_1.fieldbyname('MainId').AsString);
      FSubId := Trim(Self.CDS_1.fieldbyname('SubId').AsString);
      if ShowModal = 1 then
      begin
        InitGrid2();
      end;
    end;
  finally
    frmTradeCardInPut.Free;
  end;

end;

procedure TfrmTradePlanCard.ToolButton4Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_2.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 1 then
    Exit;
  if CDS_2.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  WSql := SelCDSKey(CDS_2, ['PCID'])[0];

  try
    ADOQueryCmd.Connection.BeginTrans;
//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('insert into Trade_Need_Up(UOperation,UType,UDataId) values(''检验'',''检验删除'',' + quotedstr(MCIIDS) + ')  ');
//      ExecSQL;
//    end;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add(' exec P_Trade_Card_DelRevoked ');
      Sql.Add(' @PCIDS=' + 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;
    InitGrid2();
    application.MessageBox('删除成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

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

  WSql := SelCDSKey(CDS_2, ['PCID'])[0];

  try
    ADOQueryCmd.Connection.BeginTrans;
//    with ADOQueryCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('insert into Trade_Need_Up(UOperation,UType,UDataId) values(''检验'',''检验删除'',' + quotedstr(MCIIDS) + ')  ');
//      ExecSQL;
//    end;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Trade_Card_Del ');
      Sql.Add('@PCIDS=' + 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;
    InitGrid2();
    application.MessageBox('删除成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmTradePlanCard.TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitGrid2();
end;

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

procedure TfrmTradePlanCard.FormShow(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);
  frmFrameDateSel1.BegDate.Date := frmFrameDateSel1.EndDate.Date - 1;
  ReadCxGrid(trim(self.Caption), Tv1, '坯布生产2');
  ComboBox_Print.Properties.Items.Assign(printer.Printers);
  InitGrid1();
end;

procedure TfrmTradePlanCard.GC_NameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid1();
  end;

end;

procedure TfrmTradePlanCard.cxTabControl1Change(Sender: TObject);
begin
  InitGrid2();
end;

end.

