unit U_TradeDispatchedSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxCurrencyEdit, cxImage, cxDBEdit, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxPC, cxSplitter, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, ShellAPI, cxDropDownEdit, cxCheckBox, RM_e_Graphic, RM_e_Jpeg,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, Math,
  dxDateRanges, IdExplicitTLSClientServerBase, U_BaseHelp, Vcl.Clipbrd,
  U_BaseList, System.ImageList, Vcl.ImgList, dxScrollbarAnnotations,
  FrameDateSel;

type
  TfrmTradeDispatchedSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    TMMNo: TcxTextEdit;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    IdFTP1: TIdFTP;
    v1Column11: TcxGridDBColumn;
    ORDER_SUB: TClientDataSet;
    Label1: TLabel;
    CustName: TcxTextEdit;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Label2: TLabel;
    C_Name: TcxTextEdit;
    ToolButton2: TToolButton;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    frmFrameDateSel1: TfrmFrameDateSel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TMMNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure TMMNoKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
    procedure JGTypeChange(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure fhClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
  private
    DQdate: TDateTime;
    MSubIds: string;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitGridFH();
    procedure InitSub();
    procedure InitPH();
    procedure SetStatus();
    procedure InitImage(fwbid: string);
    function SaveData(): Boolean;

    procedure SendMessageQR(MDateId, KeyWord1, KeyWord2, KeyWord3: string);
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2: string;
    fFlileFlag: string;
    FNOTE: string;
    { Public declarations }
  end;

var
  frmTradeDispatchedSel: TfrmTradeDispatchedSel;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LabelPrint, U_AttachmentUpload,
  U_TradeMarketInPut, U_TradeMarketDisSel, U_TradeMarketDisScan;

{$R *.dfm}

procedure TfrmTradeDispatchedSel.InitImage(fwbid: string);
begin

end;

procedure TfrmTradeDispatchedSel.SetStatus();
begin

end;

procedure TfrmTradeDispatchedSel.InitSub();
begin

end;

procedure TfrmTradeDispatchedSel.InitPH();
begin

end;

procedure TfrmTradeDispatchedSel.JGTypeChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTradeDispatchedSel.FormDestroy(Sender: TObject);
begin
  inherited;
//  frmProductOrderList:=nil;
end;

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

procedure TfrmTradeDispatchedSel.FormCreate(Sender: TObject);
begin
  inherited;
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);

end;

procedure TfrmTradeDispatchedSel.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradeDispatchedSel.InitGrid();
var
  STRwhere, Pwhere: string;
begin
  STRwhere := '';
  Pwhere := SGetFilters(Panel1, 1, 2);

  begin
    if trim(Pwhere) <> '' then
      STRwhere := STRwhere + ' and ' + trim(Pwhere);
  end;
  STRwhere := STRwhere + ' and isnull(A.Status,''0'')=''9'' ';
  if cxTabControl1.TabIndex = 0 then
  begin
    STRwhere := STRwhere + ' and isnull(substatus,''0'')=''' + inttostr(cxTabControl1.TabIndex) + '''';
  end;
  if (cxTabControl1.TabIndex >= 1) and (cxTabControl1.TabIndex < 3) then
    STRwhere := STRwhere + ' and isnull(substatus,''0'')=''' + inttostr(cxTabControl1.TabIndex + 1) + '''';

  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.add(' select   *  ');
      sql.Add(' ,PHRollNum=(select count(X.STKID) from Trade_Market_Distribution X where X.TMMID=A.TMMID and X.TMSID=B.TMSID)');
      sql.Add(' ,PHQty=(select sum(STKMeter) from Trade_Market_Distribution X inner join Trade_Cloth_Stock XX on X.stkid=XX.stkid where X.TMMID=A.TMMID and X.TMSID=B.TMSID)');
      sql.add(' from Trade_Market_Sub B');
      sql.add(' inner join Trade_Market_Main A on B.TMMID=A.TMMID ');

      sql.add(' where A.DlyDate>=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date))));
      sql.add(' and  A.DlyDate<' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.EndDate.Date + 1))));

      if canshu1 = '录入' then
      begin
        sql.add(' and  A.Filler= ' + QuotedStr(Trim(DName)));
      end;
      sql.add('     ' + STRwhere);

      sql.Add(' and ordtype=''正常单'' ');

      sql.add(' order by  filltime desc');
//      ShowMessage(SQL.Text);
      Open;
    end;

    SCreateCDS(ADOQueryMain, Order_Main);
    SInitCDSData(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;

  end;
end;

procedure TfrmTradeDispatchedSel.SendMessageQR(MDateId, KeyWord1, KeyWord2, KeyWord3: string);
begin

end;

procedure TfrmTradeDispatchedSel.InitGridFH();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec Order_QryList :TMMId,:WSql');
      Parameters.ParamByName('WSql').Value := ' and FillTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)) + '''' + ' and FillTime<''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.EndDate.Date + 1)) + '''';
      Open;
    end;
    SCreateCDS(ADOQueryMain, Order_Main);
    SInitCDSData(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmTradeDispatchedSel.InitForm();
begin
  ReadCxGrid('指示单列表FF1', Tv1, '生产指示单管理');
  frmFrameDateSel1.BegDate.Date := SGetServerDate(ADOQueryTemp) - 180;
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);

  SetStatus();
  InitGrid();
end;

function TfrmTradeDispatchedSel.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Trade_Market_Main where TMMId=''' + Trim(Order_Main.fieldbyname('TMMId').AsString) + '''');
      sql.Add('delete Trade_Market_Sub where TMMId=''' + Trim(Order_Main.fieldbyname('TMMId').AsString) + '''');
      sql.Add('delete Trade_Plan_Fty where TMMId=''' + Trim(Order_Main.fieldbyname('TMMId').AsString) + '''');

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('指示单删除')));
      sql.Add(',' + quotedstr(trim('指示单号：' + trim(Order_Main.FieldByName('TMMNo').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmTradeDispatchedSel.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmTradeDispatchedSel.FormShow(Sender: TObject);
begin
  inherited;
  InitForm();
end;

procedure TfrmTradeDispatchedSel.TBTPClick(Sender: TObject);
var
  FQty, FQty1, FMxQty, FPQty, FMxQtyS, FPQtyS: string;
begin
end;

procedure TfrmTradeDispatchedSel.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTradeDispatchedSel.TMMNoKeyPress(Sender: TObject; var Key: Char);
begin

  if Key = #13 then
  begin
    initgrid();
  end;
end;

procedure TfrmTradeDispatchedSel.fhClick(Sender: TObject);
var
  MStkIds, maxno: string;
  i, fRoll: integer;
  fNum: double;
begin

end;

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

procedure TfrmTradeDispatchedSel.N1Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmTradeDispatchedSel.N3Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, True); //全选
end;

procedure TfrmTradeDispatchedSel.N4Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, False); //全弃
end;

procedure TfrmTradeDispatchedSel.ComboBox1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTradeDispatchedSel.CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmTradeDispatchedSel.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid('指示单列表FF1', Tv1, '生产指示单管理');

end;

procedure TfrmTradeDispatchedSel.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  if Order_Main.IsEmpty then
    exit;

  ModalResult := 1;
end;

function TfrmTradeDispatchedSel.SaveData(): Boolean;
begin

end;

end.

