unit U_ItemizedListList;

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, System.JSON, uSZHN_JSON;

type
  TfrmItemizedListList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    cxgrdpmn1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    TIMNo: TcxTextEdit;
    Order_Main: TClientDataSet;
    TBCK: TToolButton;
    ADOQuerySub: TADOQuery;
    ToolButton4: TToolButton;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    pm1: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    Label1: TLabel;
    OOPCoName: TcxTextEdit;
    cxgrdpmn2: TcxGridPopupMenu;
    CDS_WXTS: TClientDataSet;
    ToolButton2: TToolButton;
    N2: TMenuItem;
    GPM_3: TcxGridPopupMenu;
    frmFrameDateSel1: TfrmFrameDateSel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    VC_PRTPs: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TV1Column5: TcxGridDBColumn;
    ToolButton1: TToolButton;
    TV1Column6: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    TV1Column12: TcxGridDBColumn;
    TV1Column13: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    TV1Column15: TcxGridDBColumn;
    TV1Column16: TcxGridDBColumn;
    Label4: TLabel;
    C_Name: TcxTextEdit;
    Label5: TLabel;
    Label6: TLabel;
    C_Color: TcxTextEdit;
    C_ColorNo: TcxTextEdit;
    Label2: TLabel;
    FtyPCId: TcxTextEdit;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure OrderNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure TBCKClick(Sender: TObject);
    procedure TIMNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton10Click(Sender: TObject);
    procedure JGTypeChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure TV1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    procedure SetStatus();
    procedure InitImage(fwbid: string);
    procedure SendMessageQR(MDateId, KeyWord1, KeyWord2, KeyWord3: string);
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2: string;
    fFlileFlag: string;
    FNOTE: string;
    FOrdType: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_RPFun, U_ZDYHelp, U_LabelPrint, U_AttachmentUpload,
  U_ItemizedListInPut;

{$R *.dfm}

procedure TfrmItemizedListList.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 TfrmItemizedListList.SetStatus();
begin
  tbadd.Enabled := false;
  TBEdit.Enabled := False;
  TBDel.Enabled := False;
  tchk.Enabled := false;
  tnochk.Enabled := false;
  TBDel.Enabled := False;

  case cxTabControl1.TabIndex of
    0:
      begin
        tbadd.Enabled := True;
        TBEdit.Enabled := True;
        TBDel.Enabled := True;
        TBDel.Enabled := True;
      end;
    1:
      begin
        TBDel.Enabled := True;
        tchk.Enabled := True;

      end;

  end;

end;

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

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

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

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

end;

procedure TfrmItemizedListList.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmItemizedListList.InitGrid();
var
  SqlStr: string;
begin

  SqlStr := ' select   *,JSAmmount=Price*TISQty    from Trade_ItemizedList_Main A inner join  Trade_ItemizedList_Sub B on A.TIMId=B.TIMId ';
  SqlStr := SqlStr + ' where A.TIMDate>=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)));
  SqlStr := SqlStr + ' and  A.TIMDate<' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)));

  case cxTabControl1.TabIndex of
    0:
      begin
        SqlStr := SqlStr + ' and  isnull(status,''0'')=''0'' ';
      end;
    1:
      begin
        SqlStr := SqlStr + ' and  isnull(status,''0'')=''9'' ';
      end;
  end;
  SqlStr := SqlStr + ' order by  A.filltime desc';
//     ShowMessage(SqlStr);
  InitCDSData(ADOQueryMain, Order_Main, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'TISId');
end;

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

  SetStatus();
  InitGrid();
end;

procedure TfrmItemizedListList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

//  if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
//  begin
//    Application.MessageBox('不能操作他人的数据!', '提示', 0);
//    Exit;
//  end;

  if cxTabControl1.TabIndex <> 0 then
    Exit;
  try
    frmItemizedListInPut := TfrmItemizedListInPut.Create(Application);
    with frmItemizedListInPut do
    begin

      PState := 1;
      FTIMID := Trim(Self.Order_Main.fieldbyname('TIMId').AsString);
      FTMMNo := Trim(Self.Order_Main.fieldbyname('TIMNo').AsString);
      canshu1 := self.canshu1;
      frmItemizedListInPut.fordtype := self.fordtype;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmItemizedListInPut.Free;
  end;

end;

procedure TfrmItemizedListList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    while Order_Main.Locate('SSel', true, []) do
    begin
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        Sql.Add('exec P_Trade_ItemizedList_Sub_Del ');
        Sql.Add(' @TISId=' + quotedstr(trim(Order_Main.FieldByName('TISId').AsString)));
        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;
      Order_Main.Delete;
    end;
    ADOQueryCmd.Connection.CommitTrans;

//    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;
end;

procedure TfrmItemizedListList.TBPrintClick(Sender: TObject);
var
  MFiltration, FTIMID: string;
  MPrintJson: string;
  JSONObject: TJSONObject; // JSON类
begin
  FTIMID := Trim(Self.Order_Main.fieldbyname('TIMID').AsString);
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select Filtration= SUBSTRING(CAST((select distinct '','' +A.BCIOID from BS_Cloth_IO A    ');
    sql.Add(' where A.IOFlag=''入库'' and TIMID=  ' + quotedstr(Trim(FTIMID)));
    sql.Add('  for xml path('''') ) AS VARCHAR(max)) , 2, 8000)');
    open;
  end;

  MFiltration := Trim(ADOQueryTemp.FieldByName('Filtration').AsString);
//  MFiltration := Trim(Self.Order_Main.fieldbyname('TIMNo').AsString);
  MPrintJson := '              {"LMType": "TradeFinishClothInPrt1"    ';
  MPrintJson := MPrintJson + ' ,"PreviewPrint": true      ';
  MPrintJson := MPrintJson + ' ,"PrtArgs": [ { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '"  } ] }    ';
//  FromPrintFr3(Application, PChar(DConString), PChar(MPrintJson));
  JSONObject := TJSONObject.ParseJSONValue(FromPrintFr3(Application, PChar(DConString), PChar(MPrintJson))) as TJSONObject;
  if JSONObject.B['succsee'] then
  begin
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        sql.add('update Trade_ItemizedList_Sub set  PrtCount=PrtCount+1');
        sql.Add(' where  TIMID=  ' + quotedstr(Trim(FTIMID)));
        ExecSQL;
      end;

      ADOQueryCmd.Connection.CommitTrans;

    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

    end;
  end;
end;

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

procedure TfrmItemizedListList.TBAddClick(Sender: TObject);
begin

  try
    frmItemizedListInPut := TfrmItemizedListInPut.Create(Application);
    with frmItemizedListInPut do
    begin
      PState := 0;
      FTIMID := '';
      canshu1 := self.canshu1;
      frmItemizedListInPut.fordtype := self.fordtype;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmItemizedListInPut.Free;
  end;
end;

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

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

procedure TfrmItemizedListList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmItemizedListList.Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id, id10: Integer;
begin
  {try
    if Tv1.GroupedItemCount=0 then
    begin
      Id:=Tv1.GetColumnByFieldName('DeliveryDate').Index-tv1.GroupedItemCount;
      Id10:=Tv1.GetColumnByFieldName('SubStatus').Index-tv1.GroupedItemCount;
      if Trim(VarToStr(ARecord.Values[id]))='' then Exit;
      if Id<0 then Exit;
      if ARecord.Values[id10]='完成' then exit;
      if (ARecord.Values[id]-DQdate)>=4 then Exit;
      if ((ARecord.Values[id]-DQdate)>=0) and ((ARecord.Values[id]-DQdate)<4) then
        AStyle:=DataLink_.QHuangSe
      else
      if ARecord.Values[id]-DQdate<0 then
      begin
        AStyle:=DataLink_OrderManage.FenHongS;
      end;
    end else
    begin

    end;
  except
  end; }
end;

procedure TfrmItemizedListList.v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { Id:=TV1.GetColumnByFieldName('DeliveryDate').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('SubStatus').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id10]='完成' then Exit;
  if AViewInfo.GridRecord.Values[Id]-SGetServerDate(ADOQueryTemp)>=4 then Exit;
  if ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))>=0) and ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))<4) then
    ACanvas.Brush.Color:=clYellow
  else
  if (AViewInfo.GridRecord.Values[id])-(SGetServerDate(ADOQueryTemp)<0) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='Purple' then
  begin
    ACanvas.Brush.Color:=clPurple;
  end else
  if AViewInfo.GridRecord.Values[Id]='Olive' then
  begin
    ACanvas.Brush.Color:=clOlive;
  end else
  if AViewInfo.GridRecord.Values[Id]='Teal' then
  begin
    ACanvas.Brush.Color:=clTeal;
  end else
  if AViewInfo.GridRecord.Values[Id]='Background' then
  begin
    ACanvas.Brush.Color:=clBackground;
  end;   }
end;

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

  try
    frmItemizedListInPut := TfrmItemizedListInPut.Create(Application);
    with frmItemizedListInPut do
    begin
      PState := 1;
      FTIMID := Trim(Self.Order_Main.fieldbyname('TIMId').AsString);
      FTMMNo := Trim(Self.Order_Main.fieldbyname('TIMNo').AsString);
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := True;
      N2.Visible := False;
      frmItemizedListInPut.fordtype := self.fordtype;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmItemizedListInPut.Free;
  end;

end;

procedure TfrmItemizedListList.TIMNoKeyPress(Sender: TObject; var Key: Char);
begin

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

procedure TfrmItemizedListList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmItemizedListList.TV1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  // 到齐
  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('PrtCount').Index] = 0 then
  begin
    ACanvas.Brush.Color := $7280FA;
  end;
end;

procedure TfrmItemizedListList.TV1DblClick(Sender: TObject);
begin
  inherited;
  TBEdit.Click;
end;

procedure TfrmItemizedListList.ToolButton4Click(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('TIMId').AsString);
      fType := '指示单';
      if ShowModal = 1 then
      begin

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

procedure TfrmItemizedListList.ToolButton5Click(Sender: TObject);
var
  MFiltration, MTISIds: string;
  MPrintJson: string;
  JSONObject: TJSONObject; // JSON类
begin
  MTISIds := SelCDSKey(Order_Main, ['TISId'])[0];
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select Filtration= SUBSTRING(CAST((select distinct '','' +A.BCIOID from BS_Cloth_IO A    ');
    sql.Add(' where A.IOFlag=''入库''  ');
    sql.Add(' and exists(SELECT * from [dbo].[F_Tool_SplitString](' + quotedstr(MTISIds) + ','','') SP where SP.RTValue=A.TISId )  ');
    sql.Add(' for xml path('''') ) AS VARCHAR(max)) , 2, 8000)');
    open;
  end;

  MFiltration := Trim(ADOQueryTemp.FieldByName('Filtration').AsString);

//  MFiltration := SelCDSKey(Order_Main, ['TISId'])[0];
  MPrintJson := '              {"LMType": "TradeFinishClothInPrt1"    ';
  MPrintJson := MPrintJson + ' ,"PreviewPrint": true      ';
  MPrintJson := MPrintJson + ' ,"PrtArgs": [ { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '"  } ] }    ';
//  FromPrintFr3(Application, PChar(DConString), PChar(MPrintJson));
  JSONObject := TJSONObject.ParseJSONValue(FromPrintFr3(Application, PChar(DConString), PChar(MPrintJson))) as TJSONObject;
  if JSONObject.B['succsee'] then
  begin
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        sql.add('update Trade_ItemizedList_Sub set  PrtCount=PrtCount+1');
        sql.add('where  exists(select * from [dbo].[F_Tool_SplitString](' + quotedstr(MFiltration) + ','','') X where X.RTVALUE=Trade_ItemizedList_Sub.TISId  )');
        ExecSQL;
      end;

      ADOQueryCmd.Connection.CommitTrans;

    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

    end;
  end;
end;

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

procedure TfrmItemizedListList.TchkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 1 then
  begin
    Application.MessageBox('只能在待审核状态下进行审核操作!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('是否核实好数据？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_ItemizedList_Main SET status=''9'',chker=''' + trim(DName) + ''',Chktime=getdate() ');
      sql.Add('where TIMId=' + quotedstr(trim(Order_Main.fieldbyname('TIMId').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('TIMNo').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

  except
    application.MessageBox('计划单审核失败！', '提示信息', 0);
  end;
  TBRafresh.Click;
end;

procedure TfrmItemizedListList.TnochkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if cxTabControl1.TabIndex <> 2 then
  begin
    Application.MessageBox('只能在已审核状态下进行审核操作!', '提示', 0);
    Exit;
  end;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Trade_ItemizedList_Main SET status=''1'',chker=null,Chktime=null ');
      sql.Add('where TIMId=' + quotedstr(trim(Order_Main.fieldbyname('TIMId').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('TIMNo').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    TBRafresh.Click;
  except
    application.MessageBox('计划单撤销审核失败！', '提示信息', 0);
  end;
end;

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

procedure TfrmItemizedListList.N3Click(Sender: TObject);
begin
  SelOKNo(Order_Main, True); //全选
end;

procedure TfrmItemizedListList.N4Click(Sender: TObject);
begin
  SelOKNo(Order_Main, False); //全弃
end;

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

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

procedure TfrmItemizedListList.ToolButton10Click(Sender: TObject);
begin
  WriteCxGrid('指示单列表FF1', Tv1, '生产指示单管理');
  application.MessageBox('保存成功！', '提示信息', 0);
end;

procedure TfrmItemizedListList.ToolButton1Click(Sender: TObject);
var
  MFiltration: string;
  MPrintJson: string;
  JSONObject: TJSONObject; // JSON类
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select Filtration= SUBSTRING(CAST((select distinct '','' +A.BCIOID from BS_Cloth_IO A    ');
    sql.Add(' where A.IOFlag=''入库''     ');
    sql.Add(' and  EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(SelCDSKey(Order_Main, ['TISId'])[0]) + ','','')  X where X.RTValue=A.TISId ) ');
    sql.Add('  for xml path('''') ) AS VARCHAR(1000)) , 2, 1000)');
//    ShowMessage(SQL.TEXT);
    open;
  end;

  MFiltration := Trim(ADOQueryTemp.FieldByName('Filtration').AsString);

  MPrintJson := '              {"LMType": "TradeFinishClothInPrt1"    ';
  MPrintJson := MPrintJson + ' ,"PreviewPrint": true      ';
  MPrintJson := MPrintJson + ' ,"PrtArgs": [ { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '"  } ] }    ';
//  FromPrintFr3(Application, PChar(DConString), PChar(MPrintJson));
  JSONObject := TJSONObject.ParseJSONValue(FromPrintFr3(Application, PChar(DConString), PChar(MPrintJson))) as TJSONObject;
  if JSONObject.B['succsee'] then
  begin
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;

        sql.add('update Trade_ItemizedList_Sub set  PrtCount=PrtCount+1');
        sql.add('where  exists(select * from [dbo].[F_Tool_SplitString](' + quotedstr(MFiltration) + ','','') X where X.RTVALUE=Trade_ItemizedList_Sub.TISId  )');

        ExecSQL;
      end;

      ADOQueryCmd.Connection.CommitTrans;

    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);

    end;
  end;
end;

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

procedure TfrmItemizedListList.ToolButton3Click(Sender: TObject);
begin
  TcxGridToExcel(Trim(Self.Caption), cxGrid1);
end;

end.

