unit U_TradeDispatchedList;

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_BaseList, Vcl.Clipbrd,
  dxScrollbarAnnotations, cxProgressBar, FrameDateSel, dxSkinsCore,
  dxSkinsDefaultPainters;

type
  TfrmTradeDispatchedList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxgrdpmn1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    TMMNo: TcxTextEdit;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    TBCK: TToolButton;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    TBFJ: TToolButton;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    v1Column11: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    ORDER_SUB: TClientDataSet;
    PopupMenu2: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    Label1: TLabel;
    CustName: TcxTextEdit;
    Tv1Column6: TcxGridDBColumn;
    cxgrdpmn2: TcxGridPopupMenu;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Label2: TLabel;
    C_Name: TcxTextEdit;
    CDS_WXTS: TClientDataSet;
    ToolButton2: TToolButton;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    GPM_3: TcxGridPopupMenu;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    ADO_PH: TADOQuery;
    DS_PH: TDataSource;
    CDS_PH: TClientDataSet;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    TBPrint1: TToolButton;
    ADOQueryPrint: TADOQuery;
    RMDB_1: TRMDBDataSet;
    RM1: TRMGridReport;
    ToolButton1: TToolButton;
    frmFrameDateSel1: TfrmFrameDateSel;
    ToolButton7: TToolButton;
    Panel3: TPanel;
    ToolBar2: TToolBar;
    ToolButton9: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    ToolButton3: TToolButton;
    cxSplitter1: TcxSplitter;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TMMNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure TBCKClick(Sender: TObject);
    procedure TMMNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TBFJClick(Sender: TObject);
    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 N2Click(Sender: TObject);
    procedure TBPrint1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  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;

implementation

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

{$R *.dfm}

procedure TfrmTradeDispatchedList.InitImage(fwbid: string);
begin
  ADOQueryImage.close;
  if fwbid = '' then
    exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(trim(fwbid)));
    open;
  end;
end;

procedure TfrmTradeDispatchedList.SetStatus();
begin

//
//  ToolButton3.Visible := false;
//  case cxTabControl1.TabIndex of
//    0:
//      begin
//        del.Visible := true;
//        phwc.Visible := true;
//        Panel2.Visible := true;
//        ToolButton4.Visible := true;
//      end;
//    1:
//      begin
//        fh.Visible := true;
//        ToolButton5.Visible := true;
//
//        Tdbchexiao.Visible := true;
//      end;
//    2:
//      begin
//        TBPrint1.Visible := true;
//        ToolButton3.Visible := True;
//      end;
//  end;
//  application.ProcessMessages;

end;

procedure TfrmTradeDispatchedList.InitSub();
begin
  ADOQuerySub.Close;
  if Order_Main.IsEmpty then
    exit;
  ADOQuerySub.DisableControls;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select *  ');
    sql.Add(' from Trade_Market_Sub A');
    sql.Add('where TMMId =' + quotedstr((Order_Main.fieldbyname('TMMId').AsString)));
    open;
  end;
  SCreateCDS(ADOQuerySub, ORDER_SUB);
  SInitCDSData(ADOQuerySub, ORDER_SUB);

  ADOQuerySub.EnableControls;
  TV2.DataController.Filter.Clear;
end;

procedure TfrmTradeDispatchedList.InitPH();
var
  SqlStr: string;
begin

  if Order_Main.IsEmpty then
    EXIT;
  SqlStr := 'select A.BCIONO,IOTime=convert(varchar(10),A.IOTime,120) ,Qty=sum(A.Qty) ,Piece=sum(A.Piece)   ';
  SqlStr := SqlStr + 'from BS_Cloth_IO A                                                              ';
  SqlStr := SqlStr + 'where A.IOFlag=''出库'' and A.TMSID=' + quotedstr(trim(Order_Main.fieldbyname('TMSID').asstring));
  SqlStr := SqlStr + 'group by A.BCIONO,convert(varchar(10),A.IOTime,120)  ';

  InitCDSData(ADO_PH, CDS_PH, Tv2, SqlStr, '', '');

end;

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

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

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

procedure TfrmTradeDispatchedList.FormCreate(Sender: TObject);
begin
  inherited;

  DQdate := SGetServerDate(ADOQueryTemp);

end;

procedure TfrmTradeDispatchedList.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradeDispatchedList.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.TMMDate>=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date))));
      sql.add(' and  A.TMMDate<' + 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 TfrmTradeDispatchedList.SendMessageQR(MDateId, KeyWord1, KeyWord2, KeyWord3: string);
begin
//  with ADOQueryTemp do
//  begin
//    Close;
//    SQL.Clear;
//    sql.Add('select distinct A.* from SY_User A inner join SY_Purview B on A.UserID=B.UserID  where A.Valid=''Y'' ');
//    SQL.Add('and isnull(A.wxid,'''')<>''''');
//    SQL.Add(' and exists(select 1 from SY_ModuleSub X where X.ModuleID=B.ModuleID and X.ModuleSubID=B.ModuleSubID and X.FormName=''生产计划单审核'' )');
////    showmessage(sql.text);
//    Open;
//  end;
//  SCreateCDS(ADOQueryTemp, CDS_WXTS);
//  SInitCDSData(ADOQueryTemp, CDS_WXTS);
//
//  if CDS_WXTS.IsEmpty then
//  begin
//    Application.MessageBox('用户未绑定微信,提交失败！', '提示信息', 0);
//    Exit;
//  end;
  CDS_WXTS.First;
  while not CDS_WXTS.eof do
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from WeixinMessage where 1<>1');
      Open;
      Append;
      FieldByName('MessageType').Value := 'OrderToAudit';
      FieldByName('MessageName').Value := '计划单待审核';
      FieldByName('Title').Value := '您有新的生产计划单待审核';
      FieldByName('URL').Value := 'subpages/plan/view?id=' + Trim(MDateId);
      FieldByName('OpenId').Value := Trim(CDS_WXTS.FieldByName('wxid').AsString);
      FieldByName('Status').Value := '0';
      FieldByName('CreateDate').Value := FormatDateTime('yyyymmdd', SGetServerDateTime(ADOQueryTemp));
      FieldByName('CreateTime').Value := FormatDateTime('HH:mm:ss', SGetServerDateTime(ADOQueryTemp));
      FieldByName('BusinessId').Value := Trim(MDateId);
      FieldByName('KeyWord1').Value := Trim(KeyWord1);
      FieldByName('KeyWord2').Value := Trim(KeyWord2);
      FieldByName('KeyWord3').Value := Trim(KeyWord3);
      FieldByName('Remark').Value := '点击可查询详情！';
      Post;
    end;
    CDS_WXTS.Next;
  end;

  GetHTTP('http://aotang.rightsoft.top/api/message/send/OrderToAudit');
end;

procedure TfrmTradeDispatchedList.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 TfrmTradeDispatchedList.InitForm();
begin
  ReadCxGrid('指示单列表FF1', Tv1, '生产指示单管理');
  ReadCxGrid('指示单列表FFTv21', Tv2, '生产指示单管理');
  frmFrameDateSel1.BegDate.Date := SGetServerDate(ADOQueryTemp) - 180;
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);

  SetStatus();
  InitGrid();
end;

function TfrmTradeDispatchedList.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 TfrmTradeDispatchedList.TBPrintClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradePlanPrint';
      FFiltration1 := Trim(Order_Main.fieldbyname('TMMId').AsString);
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

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

procedure TfrmTradeDispatchedList.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 TfrmTradeDispatchedList.FormShow(Sender: TObject);
begin
  inherited;
  canshu1 := self.fParameters1;
  fFlileFlag := UserDataFlag + 'HX';
  cxTabControl1.TabIndex := 0;
  InitForm();
end;

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

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

procedure TfrmTradeDispatchedList.TBCKClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

  try
    frmTradeMarketInPut := TfrmTradeMarketInPut.Create(Application);
    with frmTradeMarketInPut do
    begin
      PState := 1;
      FTMMId := Trim(Self.Order_Main.fieldbyname('TMMId').AsString);
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := True;
      N2.Visible := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTradeMarketInPut.Free;
  end;

end;

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

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

procedure TfrmTradeDispatchedList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin

  if Order_Main.IsEmpty then
    exit;
  InitPH();
end;

procedure TfrmTradeDispatchedList.TBFJClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      if Trim(canshu1) <> '查询' then
        FEditAuthority := True;
      fkeyNO := Trim(Self.Order_Main.fieldbyname('TMMId').AsString);
      fType := '指示单';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;
end;

procedure TfrmTradeDispatchedList.TBPrint1Click(Sender: TObject);
begin
  inherited;
  if Order_Main.IsEmpty then
    Exit;
  MSubIds := '';
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while Order_Main.Locate('SSel', True, []) do
    begin

      MSubIds := MSubIds + (Trim(Order_Main.fieldbyname('TMSID').AsString)) + ',';

      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  Order_Main.EnableControls;

  MSubIds := copy(MSubIds, 1, Length(MSubIds) - 1);

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'DistributionPrint';
      FFiltration1 := Trim(MSubIds);
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

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

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

procedure TfrmTradeDispatchedList.N2Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(ORDER_SUB.fieldbyname(TV2.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

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

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

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

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

procedure TfrmTradeDispatchedList.ToolButton7Click(Sender: TObject);
begin
  inherited;
  if cds_ph.IsEmpty then
    exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from Trade_Market_Sub ');
      sql.Add('where TMMID=' + quotedstr(trim(Order_Main.fieldbyname('TMMID').AsString)));
      sql.Add('and TMSID=' + quotedstr(trim(Order_Main.fieldbyname('TMSID').AsString)));
      open;
      edit;
      fieldbyname('substatus').Value := '2';
      post;
    end;
    application.MessageBox('配货确认成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('配货确认失败！', '提示信息', 0);
  end;
end;

procedure TfrmTradeDispatchedList.ToolButton9Click(Sender: TObject);
begin
  inherited;
  if CDS_PH.IsEmpty then
    Exit;
  MSubIds := Trim(Self.CDS_PH.fieldbyname('BCIONO').AsString);

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'DistributionPrint11';
      FFiltration1 := Trim(MSubIds);
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmTradeDispatchedList.ToolButton1Click(Sender: TObject);
begin
  inherited;
  try
    frmTradeMarketDisScan := TfrmTradeMarketDisScan.Create(Application);
    with frmTradeMarketDisScan do
    begin
      show;
    end;
  finally
  end;
end;

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

procedure TfrmTradeDispatchedList.ToolButton3Click(Sender: TObject);
begin
  try
    frmTradeClothTotalPHList := TfrmTradeClothTotalPHList.Create(Application);
    with frmTradeClothTotalPHList do
    begin
      FBCIONO := Trim(Self.CDS_PH.fieldbyname('BCIONO').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTradeClothTotalPHList.Free;
  end;
end;

function TfrmTradeDispatchedList.SaveData(): Boolean;
var
  phID: string;
begin
  Result := false;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from Trade_Market_Distribution ');
      sql.Add('where TMMID=' + quotedstr(trim(Order_Main.fieldbyname('TMMID').AsString)));
      sql.Add('and TMSID=' + quotedstr(trim(Order_Main.fieldbyname('TMSID').AsString)));
      execsql;
    end;

    with CDS_PH do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        if fieldbyname('stkmeter').AsFloat = 0 then
          continue;
        if GetLSNo(ADOQueryTemp, phID, 'PH', 'Trade_Market_Distribution', 4, 1) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取配货单号失败！', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from Trade_Market_Distribution ');
          sql.Add('where TMMID=' + quotedstr(trim(Order_Main.fieldbyname('TMMID').AsString)));
          sql.Add('and TMSID=' + quotedstr(trim(Order_Main.fieldbyname('TMSID').AsString)));
          open;
        end;
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('TMDID').Value := trim(phID);
        ADOQueryCmd.FieldByName('TMMID').Value := trim(Order_main.fieldbyname('TMMID').AsString);
        ADOQueryCmd.FieldByName('TMSID').Value := trim(Order_main.fieldbyname('TMSID').AsString);
        ADOQueryCmd.FieldByName('STKID').Value := trim(CDS_PH.fieldbyname('STKID').AsString);
        ADOQueryCmd.FieldByName('Filler').Value := trim(DName);
        ADOQueryCmd.Post;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from Trade_Cloth_Stock ');
          sql.Add('where StkID=' + quotedstr(trim(CDS_PH.fieldbyname('StkID').AsString)));
          open;
          edit;
          fieldbyname('Disstatus').Value := '1';
          post;
        end;
        next;
      end;
      first;
      EnableControls;

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from Trade_Market_Sub ');
        sql.Add('where TMMID=' + quotedstr(trim(Order_Main.fieldbyname('TMMID').AsString)));
        sql.Add('and TMSID=' + quotedstr(trim(Order_Main.fieldbyname('TMSID').AsString)));
        open;
        edit;
        fieldbyname('substatus').Value := '2';
        post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

end.

