unit U_TradeCardInPut;

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, cxPC, MovePanel, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu,   dxDateRanges,
  U_BaseList, Vcl.Menus, U_BaseInput, System.ImageList, Vcl.ImgList, StrUtils,
   dxScrollbarAnnotations, cxContainer, cxMaskEdit, cxDropDownEdit,
  Vcl.Printers;

type
  TfrmTradeCardInPut = class(TfrmBaseInput)
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    PM_1: TPopupMenu;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v3Column8: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel3: TPanel;
    Label1: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label6: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    C_Name: TcxTextEdit;
    C_Code: TcxTextEdit;
    ConNo: TcxTextEdit;
    GC_Name: TcxTextEdit;
    CraftCode: TcxTextEdit;
    C_Color: TcxTextEdit;
    C_Pattern: TcxTextEdit;
    C_ColorConfirm: TcxTextEdit;
    C_ColorNo: TcxTextEdit;
    OrderNo: TcxTextEdit;
    ToolBar1: TToolBar;
    ToolButton12: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    ADO_1: TADOQuery;
    RM1: TRMGridReport;
    RMDB_1: TRMDBDataSet;
    ToolButton2: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    ComboBox_Print: TcxComboBox;
    Label4: TLabel;
    ToolButton3: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    FWSql: string;
    procedure InitOrd();
    procedure InitGrid1();
    procedure PrtData(MCIID: string);
    procedure SaveCard(MCardQty: Integer; MBatchNo: string);
    { Private declarations }
  public
    FMainId, FSubId: string;
    { Public declarations }
  end;

var
  frmTradeCardInPut: TfrmTradeCardInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_InputBoxCard, U_LabelPrint, U_TradePlanSel,
  U_PrintLabFun;
  {$R *.dfm}

procedure TfrmTradeCardInPut.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 TfrmTradeCardInPut.InitOrd();
begin

  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,B.*');
    sql.Add(' ,YAPPS=ISNULL((select count(*) from Trade_Plan_Card X where X.MainId=A.MainId),0)');
    sql.Add(' ,WAPPS=OrdPiece-ISNULL((select count(*) from Trade_Plan_Card X where X.MainId=A.MainId),0)');
    sql.Add(' from Trade_Plan_Main A');
    sql.Add(' inner join Trade_Plan_Sub B on A.MainId=B.MainId ');
    sql.Add(' where B.SubId=' + QuotedStr(FSubId));
//    ShowMessage(sql.Text);
    Open;
  end;
  SCSHData(ADOQueryMain, Panel3, 0);

end;

procedure TfrmTradeCardInPut.InitGrid1();
var
  fwhere: string;
begin
  try
    ADO_1.DisableControls;
    with ADO_1 do
    begin
      Close;
      sql.Clear;
      sql.Add(' select *');
      sql.Add(' from Trade_Plan_Card A where Subid =''' + trim(FSubid) + '''');
      Open;
    end;
    SCreateCDS(ADO_1, CDS_1);
    SInitCDSData(ADO_1, CDS_1);
  finally
    ADO_1.EnableControls;
  end;
end;

procedure TfrmTradeCardInPut.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradeCardInPut := nil;
end;

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

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

end;

procedure TfrmTradeCardInPut.TBRafreshClick(Sender: TObject);
begin
  InitGrid1();

end;

procedure TfrmTradeCardInPut.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '指示单管理');
  ComboBox_Print.Properties.Items.Assign(printer.Printers);
  InitOrd();
  InitGrid1();
end;

procedure TfrmTradeCardInPut.cxTabControl1Change(Sender: TObject);
begin
  InitGrid1();
end;

procedure TfrmTradeCardInPut.ToolButton12Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '指示单管理');
end;

procedure TfrmTradeCardInPut.ToolButton1Click(Sender: TObject);
begin

  try
    frmInputBoxCard := TfrmInputBoxCard.Create(Application);
    with frmInputBoxCard do
    begin
      case ShowModal of
        1:
          begin
            SaveCard(StrToIntDef(TTE_CardQty.Text, 0), TTE_BatchNo.Text);
            InitGrid1();
          end;
        2:
          begin
            SaveCard(StrToIntDef(TTE_CardQty.Text, 0), TTE_BatchNo.Text);
            PrintServerLabel(ADOQueryPrint, 1, 1, ComboBox_Print.ItemIndex, FWSql);
            InitGrid1();
          end;
      end;
    end;
  finally
    frmInputBoxCard.Free;
  end;

end;

procedure TfrmTradeCardInPut.ToolButton2Click(Sender: TObject);
begin
//  PrtData(SelCDSKey(CDS_1, ['PCID'])[0]);

  PrintServerLabel(ADOQueryPrint, 1, 0, ComboBox_Print.ItemIndex, SelCDSKey(CDS_1, ['PCID'])[0]);
  InitGrid1();
end;

procedure TfrmTradeCardInPut.ToolButton3Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;

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

  WSql := SelCDSKey(CDS_1, ['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;
    InitGrid1();
    application.MessageBox('删除成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmTradeCardInPut.SaveCard(MCardQty: Integer; MBatchNo: string);
var
  maxno, maxPCId, WSql: string;
  i, j, MPCNo: integer;
begin

  if MCardQty = 0 then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select PCNo=isnull(Max(PCNo),0)  ');
      sql.Add('from Trade_Plan_Card A ');
      sql.Add('where A.Subid=' + Quotedstr(Trim(FSubid)));

      sql.Add('and A.BatchNo=' + Quotedstr(Trim(MBatchNo)));
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
      MPCNo := 1
    else
      MPCNo := ADOQueryTemp.FieldByName('PCNo').AsInteger + 1;
//////////////////            插入布票               //////////////////////
    if GetLSNo(ADOQueryTemp, maxno, 'PC', 'Trade_Plan_Mach', 2, 1) = False then
    begin
      raise Exception.Create('取布票流水号失败!');
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' insert into Trade_Plan_Card(MainId,SubId,PCId,PCNo,PCDate,Filler,BatchNo) values');
      for j := 1 to MCardQty do
      begin
        maxPCId := maxno + RightStr('00' + inttostr(j), 3);
        if j > 1 then
          sql.Add(',');
        sql.Add(' (' + Quotedstr(Trim(FMainId)));
        sql.Add(',' + Quotedstr(Trim(FSubId)));
        sql.Add(',' + Quotedstr(maxPCId));
        sql.Add(',' + Quotedstr(IntToStr(MPCNo)));
        sql.Add(',getdate() ');
        sql.Add(',' + Quotedstr(Trim(DName)));
        sql.Add(',' + Quotedstr(Trim(MBatchNo)));
        sql.Add(' )');
        MPCNo := MPCNo + 1;

        if WSql <> '' then
        begin
          WSql := WSql + ',' + (Trim(maxPCId));
        end
        else
        begin
          WSql := (Trim(maxPCId));
        end;
      end;
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    FWSql := WSql;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    if GetLSNo(ADOQueryTemp, maxPCId, 'PC', 'Trade_Plan_Mach', 3, 1) = False then
    begin
//      raise Exception.Create('取布票流水号失败!');
    end;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

  InitGrid1();
end;

end.

