unit U_CPCKMdList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System,
  RM_GridReport, cxCheckBox, Menus, MovePanel, BtnEdit, cxCalendar,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, cxPC,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmCPCKMdList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main1: TClientDataSet;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    Label10: TLabel;
    FHMainid: TEdit;
    MovePanel2: TMovePanel;
    Label3: TLabel;
    Label5: TLabel;
    P_CodeName: TEdit;
    Conno: TEdit;
    Label8: TLabel;
    SQType: TComboBox;
    ADOQueryDH: TADOQuery;
    ADOQueryPrint: TADOQuery;
    Label12: TLabel;
    P_Code: TEdit;
    Label14: TLabel;
    KHName: TEdit;
    CDS_Juan: TClientDataSet;
    CDS_JuanPRT: TClientDataSet;
    RMDBMX: TRMDBDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column8: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column5: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    Label4: TLabel;
    BatchNo: TEdit;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ComboBox1: TComboBox;
    ToolButton2: TToolButton;
    v1Column4: TcxGridDBColumn;
    CDS_Main: TClientDataSet;
    Tv1Column1: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Label6: TLabel;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Label7: TLabel;
    SHDWName: TEdit;
    RMDBMX2: TRMDBDataSet;
    RM2: TRMGridReport;
    CDS_JuanPRT2: TClientDataSet;
    ToolButton3: TToolButton;
    IdHTTP1: TIdHTTP;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure ConnoKeyPress(Sender: TObject; var Key: Char);
    procedure CheJianChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column4PropertiesChange(Sender: TObject);
    procedure ConnoChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure SetStatus();
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
  private
    procedure InitGrid();
    function YSData(Order_Main10: TClientDataSet): Boolean;
    procedure GetLotData();
    procedure GetLotDataPT();

    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCPCKMdList: TfrmCPCKMdList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_FinishedClothOutList_MX;

{$R *.dfm}

procedure TfrmCPCKMdList.FormDestroy(Sender: TObject);
begin
  frmCPCKMdList := nil;
end;

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

procedure TfrmCPCKMdList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCPCKMdList.SetStatus();
var
  i: Integer;
begin
  v1Column21.Visible := False;
  Tv1Column1.Visible := False;
  case cxTabControl1.TabIndex of
    0:
      begin
        v1Column21.Visible := False;
        Tv1Column1.Visible := False;
      end;

    1:
      begin
        v1Column21.Visible := True;
        Tv1Column1.Visible := True;
      end;
  end;
end;

procedure TfrmCPCKMdList.InitGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('update SQ_FH_Sub set p_code=p_codename where isnull(p_code,'''')=''''');
    ExecSQL;
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      case cxTabControl1.TabIndex of
        0:   /////////////////////////汇总//////////////////////////////
          begin
            SQL.Add('SELECT AA.*,C.KHName,C.SYRName,C.ywy,C.DeliveryDate,C.Note,D.P_Spec,D.P_MF,D.P_KZ,D.P_Color ');
            SQL.Add(',E.SQDate,E.SQType,E.Filler,E.Chker,E.ChkTime,E.FHDate,e.ranchang,e.SHDWNAMe FROM(');
            Sql.Add('  select dyflag,DCFLAG,A.FHMainid,A.SHDWName,B.FromMainId,B.FromSubId,B.P_Code,b.P_CodeName,B.SQPiQty,ISTS=cast(isnull(b.ISTS,''0'') as bit),b.fhsubid');
            Sql.Add(' ,FHPiQtyHZ=(select count(JyLen) from CK_Cloth_CR X where X.CKNO=A.FHMainId and X.P_Code=B.P_Code)');
            Sql.Add(' ,FHQtyHZ=(select sum(JyLen) from CK_Cloth_CR X where X.CKNO=A.FHMainId and X.P_Code=B.P_Code )');
            sql.Add('  from SQ_FH_Main A LEFT JOIN SQ_FH_Sub B ON B.FHMainId=A.FHMainId  left join CK_Cloth_CR C on C.CKNO=A.FHMainId');
            sql.Add(' where A.FHDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
            sql.Add(' and A.FHDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
            sql.Add(' and (select count(JSLen) from CK_Cloth_CR X where X.CKNO=A.FHMainId and X.P_Code=B.P_Code)>0 ');
            SQL.Add(' GROUP BY A.FHMainid,A.SHDWName,B.FromMainId,B.FromSubId,B.P_Code,b.P_CodeName,B.SQPiQty,ISTS,b.fhsubid,DCFLAG,dyflag');
            SQL.Add(')AA LEFT JOIN PD_Plan_Main C ON C.MainId=AA.FromMainId');
            sql.Add(' LEFT JOIN PD_Plan_Sub D ON D.SubId=AA.FromSubId');
            SQL.Add('left JOIN SQ_FH_Main E on AA.FHMainId =E.FHMainId');
            SQL.Add('where AA.FHPiQtyHZ>0 ');
            sql.add('order by fhmainid');
          end;
        1:      /////////////////////////明细//////////////////////////////
          begin
//            SQL.Add('SELECT AA.*,C.KHName,C.SYRName,C.ywy,C.DeliveryDate,C.Note,D.P_Code,D.P_CodeName,D.P_Spec,D.P_MF,D.P_KZ,D.P_Color ');
//            SQL.Add(',E.SQDate,E.SQType,E.Filler,E.Chker,E.ChkTime,E.FHDate FROM(');
//            Sql.Add('  select A.FHMainid,B.FromMainId,B.FromSubId,C.BatchNo,C.WXPH,B.P_Code,B.SQPiQty');
//            Sql.Add(' ,FHPiQtyHZ=(select count(JSLen) from CK_Cloth_CR X where X.CKNO=A.FHMainId and X.BatchNo=C.BatchNo and X.WXPH=C.WXPH and X.P_Code=B.P_Code)');
//            Sql.Add(' ,FHQtyHZ=(select sum(JSLen) from CK_Cloth_CR X where X.CKNO=A.FHMainId and X.BatchNo=C.BatchNo and X.WXPH=C.WXPH and X.P_Code=B.P_Code)');
//            sql.Add('  from SQ_FH_Main A LEFT JOIN SQ_FH_Sub B ON B.FHMainId=A.FHMainId  left join CK_Cloth_CR C on C.CKNO=A.FHMainId');
//            sql.Add(' where A.FHDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//            sql.Add(' and A.FHDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
//            sql.Add(' and FHPiQtyHZ>0 ');
//            SQL.Add(' GROUP BY A.FHMainid,B.FromMainId,B.FromSubId,C.BatchNo,C.WXPH,B.P_Code,B.SQPiQty');
//            SQL.Add(')AA LEFT JOIN PD_Plan_Main C ON C.MainId=AA.FromMainId');
//            sql.Add(' LEFT JOIN PD_Plan_Sub D ON D.SubId=AA.FromSubId');
//            SQL.Add('left JOIN SQ_FH_Main E on AA.FHMainId =E.FHMainId');
//            SQL.Add('where AA.FHPiQtyHZ>0 ');
          end;
      end;

      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPCKMdList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmCPCKMdList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmCPCKMdList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Self.Caption + '2', Tv1, '坯布仓库2');
  Close;
end;

procedure TfrmCPCKMdList.FormShow(Sender: TObject);
begin
  ReadCxGrid(Self.Caption, Tv1, '坯布仓库2');
  ReadCxGrid(Self.Caption + '2', Tv1, '坯布仓库2');
  SetStatus();
end;

procedure TfrmCPCKMdList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin

      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('UPDATE SQ_FH_Sub set DCFLAG=''已导出''  where fhsubid=''' + Trim(CDS_Main.fieldbyname('fhsubid').AsString) + '''');
        execsql;
      end;

      Next;
    end;
  end;

  SelExportData(Tv1, ADOQueryMain, '');
end;

procedure TfrmCPCKMdList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmCPCKMdList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCPCKMdList.GetLotDataPT();
var
  FPCode, FColor, FBaoNo, FHZ, fPrintFile, FPlanNo, PCodeColor, JSKHConNo: string;
  i, ii, j, z, bb, hh: Integer;
  XJ: Double;
begin
  if CDS_Main.IsEmpty then
    Exit;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select defStr0=Cast('''' as varchar(20)), defstr1=Cast(0 as decimal(18,2)),defstr2=Cast(0 as decimal(18,2))');
    sql.add(',defstr3=Cast(0 as decimal(18,2)),defstr4=Cast(0 as decimal(18,2)),defstr5=Cast(0 as decimal(18,2))');
    sql.add(',defstr6=Cast(0 as decimal(18,2)),defstr7=Cast(0 as decimal(18,2)),defstr8=Cast(0 as decimal(18,2))');
    sql.add(',defstr9=Cast(0 as decimal(18,2)),defstr10=Cast(0 as decimal(18,2))');
    sql.Add(',DefJ1=Cast(0 as int),DefJ2=Cast(0 as int),DefJ3=Cast(0 as int),DefJ4=Cast(0 as int)');
    sql.Add(',DefJ5=Cast(0 as int),DefJ6=Cast(0 as int),DefJ7=Cast(0 as int),DefJ8=Cast(0 as int)');
    sql.Add(',DefJ9=Cast(0 as int),DefJ10=Cast(0 as int)');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint, CDS_JuanPRT);
  SInitCDSData20(ADOQueryPrint, CDS_JuanPRT);
  if CDS_JuanPRT.IsEmpty = False then
  begin
    CDS_JuanPRT.Delete;
  end;
  with CDS_JuanPRT do
  begin
    for z := 1 to 10 do
    begin
      Append;
      FieldByName('defStr0').Value := IntToStr(z);
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.KGQty,JQty=1 ');
    sql.Add(' from CK_PBCP_CR A  ');
    sql.Add(' where 1=1 ');
    sql.Add(' and A.CRFlag=''出库'' ');
    SQL.Add(' and CKOrdNo=''' + Trim(CDS_Main.fieldbyname('CKOrdNo').AsString) + '''');
    Open;
  end;
  ii := ADOQueryTemp.RecordCount;
  ii := ii div 100;
  ii := ii + 1;
  bb := ii;

  i := 1;
  j := 1;
  hh := 0;
  with ADOQueryTemp do
  begin
    First;
    while not Eof do
    begin
      if i < 11 then
      begin
        CDS_JuanPRT.Locate('defstr0', IntToStr(i + hh * 10), []);
        with CDS_JuanPRT do
        begin
          Edit;
          FieldByName('defstr' + Trim(inttostr(j))).Value := ADOQueryTemp.fieldbyname('KGQty').Value;
          FieldByName('DefJ' + Trim(inttostr(j))).Value := 1;
          Post;
        end;
        i := i + 1;
      end
      else
      begin
        j := j + 1;
        if j < 11 then
        begin
          CDS_JuanPRT.Locate('defstr0', IntToStr(1 + hh * 10), []);
          with CDS_JuanPRT do
          begin
            Edit;
            FieldByName('defstr' + Trim(inttostr(j))).Value := ADOQueryTemp.fieldbyname('KGQty').Value;
            FieldByName('DefJ' + Trim(inttostr(j))).Value := 1;
            Post;
          end;
          i := 2;
        end
        else
        begin
          if bb = ii then
          begin
            hh := 1;
          end
          else
          begin
            hh := hh + 1;
          end;
          if hh < ii then
          begin
            with CDS_JuanPRT do
            begin
              for z := 1 + hh * 10 to 10 + hh * 10 do
              begin
                Append;
                FieldByName('defStr0').Value := IntToStr(z);
                Post;
              end;
            end;
            bb := -1;
            CDS_JuanPRT.Locate('defstr0', Trim(IntToStr(1 + hh * 10)), []);
            with CDS_JuanPRT do
            begin
              Edit;
              FieldByName('defstr1').Value := ADOQueryTemp.fieldbyname('KGQty').Value;
              FieldByName('DefJ' + Trim(inttostr(j - (10)))).Value := 1;
              Post;
            end;
            i := 2;
            j := 1;
          end;
        end;
      end;
      Next;
    end;
  end;
  CDS_JuanPRT.DisableControls;
  with CDS_JuanPRT do
  begin
    First;
    while not Eof do
    begin
      CDS_JuanPRT.Edit;
      if (CDS_JuanPRT.FieldByName('defstr0').AsInteger mod 10) <> 0 then
        CDS_JuanPRT.FieldByName('defstr0').Value := IntToStr(CDS_JuanPRT.FieldByName('defstr0').AsInteger mod 10)
      else
        CDS_JuanPRT.FieldByName('defstr0').Value := '10';
      CDS_JuanPRT.Post;
      Next;
    end;
  end;
  CDS_JuanPRT.EnableControls;
end;

procedure TfrmCPCKMdList.GetLotData();
var
  XHLie, XHHang: Integer;
  FHZ: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select AA.* ');
    sql.Add('from(select gangno=B.CarNo,B.MJXH,MJMaoZ=A.KgQty,A.MainId,C.C_Color,C.BatchNoHZ');
    sql.Add(' ,GQty=(select sum(MJMaoZ) from CK_PBCP_CR CR inner join WFB_MJJY_PB XH on CR.MJID=XH.MJID  ');
    sql.add('       where CR.CKOrdNo=A.CKOrdNo and XH.CarNo=B.CarNo and XH.MainId=B.MainId )  ');
    sql.Add(' ,GPS=(select count(*) from CK_PBCP_CR CR inner join WFB_MJJY_PB XH on CR.MJID=XH.MJID  ');
    sql.add('      where CR.CKOrdNo=A.CKOrdNo and XH.CarNo=B.CarNo and XH.MainId=B.MainId)  ');
    sql.add(' from CK_PBCP_CR A');
    sql.Add(' inner join  WFB_MJJY_PB B on A.MJID=B.MJID ');
    sql.Add(' inner join Cloth_Sub C on B.SubId=C.SubId');
    sql.Add(' where A.CKOrdNo=''' + Trim(CDS_Main.fieldbyname('CKOrdNo').AsString) + '''');
    sql.Add(' )AA order by MainId,Cast(GangNo as int),MJXH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Juan);
  SInitCDSData20(ADOQueryTemp, CDS_Juan);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select');
    sql.Add(' PCode=Cast('''' as varchar(20)),ColorNo=Cast('''' as varchar(20)),');
    sql.Add(' GangNo=Cast('''' as varchar(20)),MainId=Cast('''' as varchar(20)),');
    sql.Add(' LenQty0=Cast(0.00 as decimal(18,1)),LenQty1=Cast(0.00 as decimal(18,1)),');
    sql.Add(' LenQty2=Cast(0.00 as decimal(18,1)),LenQty3=Cast(0.00 as decimal(18,1)),');
    sql.Add(' LenQty4=Cast(0.00 as decimal(18,1)),LenQty5=Cast(0.00 as decimal(18,1)),');
    sql.Add(' LenQty6=Cast(0.00 as decimal(18,1)),LenQty7=Cast(0.00 as decimal(18,1)),');
    sql.Add(' LenQty8=Cast(0.00 as decimal(18,1)),LenQty9=Cast(0.00 as decimal(18,1)),');
    sql.Add(' LenQty10=Cast(0.00 as decimal(18,1)),LenQty11=Cast(0.00 as decimal(18,1)),');
    sql.Add(' MJPS0=Cast(0 as int),MJPS1=Cast(0 as int),');
    sql.Add(' MJPS2=Cast(0 as int),MJPS3=Cast(0 as int),');
    sql.Add(' MJPS4=Cast(0 as int),MJPS5=Cast(0 as int),');
    sql.Add(' MJPS6=Cast(0 as int),MJPS7=Cast(0 as int),');
    sql.Add(' MJPS8=Cast(0 as int),MJPS9=Cast(0 as int),');
    sql.Add(' MJPS10=Cast(0 as int),MJPS11=Cast(0 as int),');
    sql.Add(' JuanQty=Cast(0 as int),GPS=Cast(0 as int),GQty=Cast(0 as decimal(18,1)),');
    sql.Add(' GPrice=Cast(0 as decimal(18,2)),GMoney=Cast(0 as decimal(18,2)),');
    sql.Add(' PCodeColorNo=cast('''' as varchar(50)),Hang=Cast(0 as int),');
    sql.Add(' PCodeColorNoG=cast('''' as varchar(50))');
    sql.Add(' ,BatchNoHZ=cast('''' as varchar(50))');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
  SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);
  if CDS_JuanPRT.IsEmpty = False then
  begin
    CDS_JuanPRT.Delete;
  end;
  CDS_Juan.DisableControls;
  with CDS_Juan do
  begin
    First;
    while not Eof do
    begin
      with CDS_JuanPRT do
      begin
        if CDS_JuanPRT.IsEmpty then
        begin
          XHLie := 0;
          XHHang := 0;
          CDS_JuanPRT.Append;
          CDS_JuanPRT.FieldByName('GangNo').Value := CDS_Juan.fieldbyname('GangNo').Value;
          CDS_JuanPRT.FieldByName('BatchNoHZ').Value := CDS_Juan.fieldbyname('BatchNoHZ').Value;
          CDS_JuanPRT.FieldByName('MainId').Value := CDS_Juan.fieldbyname('MainId').Value;
          CDS_JuanPRT.FieldByName('PCodeColorNoG').Value := '      ' + Trim(CDS_Juan.fieldbyname('MainId').AsString) + '#' + Trim(CDS_Juan.fieldbyname('gangno').AsString) + '   ';
          CDS_JuanPRT.FieldByName('MJPS' + Inttostr(XHLie)).Value := 1;
          CDS_JuanPRT.FieldByName('Hang').Value := XHHang;
          CDS_JuanPRT.FieldByName('LenQty' + Trim(IntToStr(XHLie))).Value := CDS_Juan.fieldbyname('MJMaoZ').Value;
          CDS_JuanPRT.FieldByName('GPS').Value := CDS_Juan.fieldbyname('GPS').Value;
          CDS_JuanPRT.FieldByName('GQty').Value := CDS_Juan.fieldbyname('GQty').Value;
          CDS_JuanPRT.Post;
          FHZ := Trim(CDS_Juan.fieldbyname('MainId').AsString) + Trim(CDS_Juan.fieldbyname('gangno').AsString);
        end
        else
        begin
          if (Trim(CDS_Juan.fieldbyname('MainId').AsString) + Trim(CDS_Juan.fieldbyname('GangNo').AsString)) <> FHZ then
          begin
            XHLie := 0;
            XHHang := 0;
            CDS_JuanPRT.Append;
            CDS_JuanPRT.FieldByName('GangNo').Value := CDS_Juan.fieldbyname('GangNo').Value;
            CDS_JuanPRT.FieldByName('BatchNoHZ').Value := CDS_Juan.fieldbyname('BatchNoHZ').Value;
            CDS_JuanPRT.FieldByName('MainId').Value := CDS_Juan.fieldbyname('MainId').Value;
            CDS_JuanPRT.FieldByName('PCodeColorNoG').Value := '      ' + Trim(CDS_Juan.fieldbyname('MainId').AsString) + '#' + Trim(CDS_Juan.fieldbyname('gangno').AsString) + '   ';
            CDS_JuanPRT.FieldByName('MJPS' + Inttostr(XHLie)).Value := 1;
            CDS_JuanPRT.FieldByName('Hang').Value := XHHang;
            CDS_JuanPRT.FieldByName('LenQty' + Trim(IntToStr(XHLie))).Value := CDS_Juan.fieldbyname('MJMaoZ').Value;
            CDS_JuanPRT.FieldByName('GPS').Value := CDS_Juan.fieldbyname('GPS').Value;
            CDS_JuanPRT.FieldByName('GQty').Value := CDS_Juan.fieldbyname('GQty').Value;
            CDS_JuanPRT.Post;
            FHZ := Trim(CDS_Juan.fieldbyname('MainId').AsString) + Trim(CDS_Juan.fieldbyname('GangNo').AsString);
          end
          else
          begin
            XHLie := XHLie + 1;
            if XHLie < 10 then
            begin
              CDS_JuanPRT.Locate('Mainid;GangNo;Hang', VarArrayOf([Trim(CDS_Juan.fieldbyname('Mainid').AsString), Trim(CDS_Juan.fieldbyname('GangNo').AsString), XHHang]), [loPartialKey]);
              with CDS_JuanPRT do
              begin
                Edit;
                CDS_JuanPRT.FieldByName('GangNo').Value := CDS_Juan.fieldbyname('GangNo').Value;
                CDS_JuanPRT.FieldByName('BatchNoHZ').Value := CDS_Juan.fieldbyname('BatchNoHZ').Value;
                CDS_JuanPRT.FieldByName('Mainid').Value := CDS_Juan.fieldbyname('Mainid').Value;
                CDS_JuanPRT.FieldByName('PCodeColorNoG').Value := '      ' + Trim(CDS_Juan.fieldbyname('Mainid').AsString) + '#' + Trim(CDS_Juan.fieldbyname('gangno').AsString) + '   ';
                CDS_JuanPRT.FieldByName('MJPS' + Inttostr(XHLie)).Value := 1;
                CDS_JuanPRT.FieldByName('Hang').Value := XHHang;
                CDS_JuanPRT.FieldByName('LenQty' + Trim(IntToStr(XHLie))).Value := CDS_Juan.fieldbyname('MJMaoZ').Value;
                CDS_JuanPRT.FieldByName('GPS').Value := CDS_Juan.fieldbyname('GPS').Value;
                CDS_JuanPRT.FieldByName('GQty').Value := CDS_Juan.fieldbyname('GQty').Value;
                Post;
              end;
            end
            else
            begin
              XHLie := 0;
              XHHang := XHHang + 1;
              CDS_JuanPRT.Append;
              CDS_JuanPRT.FieldByName('GangNo').Value := CDS_Juan.fieldbyname('GangNo').Value;
              CDS_JuanPRT.FieldByName('BatchNoHZ').Value := CDS_Juan.fieldbyname('BatchNoHZ').Value;
              CDS_JuanPRT.FieldByName('Mainid').Value := CDS_Juan.fieldbyname('Mainid').Value;
              //CDS_JuanPRT.FieldByName('PCodeColorNo').Value:='      '+Trim(CDS_Juan.fieldbyname('PCode').AsString)+'#'
                                                         //+Trim(CDS_Juan.fieldbyname('ColorNo').AsString);
              CDS_JuanPRT.FieldByName('PCodeColorNoG').Value := '      ' + Trim(CDS_Juan.fieldbyname('Mainid').AsString) + '#' + Trim(CDS_Juan.fieldbyname('gangno').AsString) + '   ';
              CDS_JuanPRT.FieldByName('MJPS' + Inttostr(XHLie)).Value := 1;
              CDS_JuanPRT.FieldByName('Hang').Value := XHHang;
              CDS_JuanPRT.FieldByName('LenQty' + Trim(IntToStr(XHLie))).Value := CDS_Juan.fieldbyname('MJMaoZ').Value;
              CDS_JuanPRT.FieldByName('GPS').Value := CDS_Juan.fieldbyname('GPS').Value;
              CDS_JuanPRT.FieldByName('GQty').Value := CDS_Juan.fieldbyname('GQty').Value;
              CDS_JuanPRT.Post;
            end;
          end;
        end
      end;
      Next;
    end;
  end;
  CDS_Juan.EnableControls;
end;

procedure TfrmCPCKMdList.orderNoChange(Sender: TObject);
begin
  if Length(ConNo.Text) < 4 then
    Exit;
  TBFind.Click;
end;

function TfrmCPCKMdList.YSData(Order_Main10: TClientDataSet): Boolean;
var
  CRID, YFID, Price, PriceUnit, OrderUnit: string;
begin
  Result := False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select Top 1 * from JYOrder_Sub Where Mainid=''' + Trim(Order_Main10.fieldbyname('MainId').AsString) + '''');
    //sql.Add(' and PRTPrice>0');
    Open;
  end;
  {Price:=ADOQueryTemp.fieldbyname('PRTPrice').AsString;
  if Trim(Price)='' then
  begin
    Price:='0';
  end; }
  PriceUnit := Trim(ADOQueryTemp.fieldbyname('PriceUnit').AsString);
  OrderUnit := Trim(ADOQueryTemp.fieldbyname('OrderUnit').AsString);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName=''' + Trim(Order_Main10.fieldbyname('KHName').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    CRID := ADOQueryTemp.fieldbyname('CRID').AsString;
  end
  else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID := ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value := StrToInt(CRID);
      FieldByName('FactoryName').Value := Trim(Order_Main10.fieldbyname('KHName').AsString);
      FieldByName('ZdyStr1').Value := '应收收';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName=''' + Trim(Order_Main10.fieldbyname('KHName').AsString) + '''');
    sql.Add(' and YFTypeId=''' + Trim(Order_Main10.fieldbyname('Mainid').AsString) + '''');
    sql.Add(' and Price=''' + Trim(Order_Main10.fieldbyname('PRTPrice').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd, YFID, 'CS', 'YF_Money_CR', 3, 1) = False then
    begin
      Application.MessageBox('取坯布应付最大号失败!', '提示', 0);
      Exit;
    end;
    {with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value:=Trim(YFID);
      FieldByName('YFTypeId').Value:=Trim(Order_Main10.fieldbyname('MainId').AsString);
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='应收款登记';
      FieldByName('CRFlag').Value:='应收收';
      FieldByName('QtyFlag').Value:=1;
      FieldByName('FactoryName').Value:=Trim(Order_Main10.fieldbyname('KHName').AsString);
      FieldByName('CRTime').Value:=SGetServerDateTime(ADOQueryTemp);
      FieldByName('YFType').Value:='自动生成';
      FieldByName('Price').Value:=Price;
      //FieldByName('HuiLv').Value:=1;
      FieldByName('BZType').Value:=Trim(PriceUnit);
      FieldByName('QtyUnit').Value:=Trim(OrderUnit);
      FieldByName('YFName').Value:='销售金额';
      FieldByName('MainId').Value:=Trim(Order_Main10.fieldbyname('Mainid').AsString);
      Post;
    end; }
  end
  else
  begin
    YFID := Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(Qty),0) from CK_BanCP_CR A  ');
    sql.Add(' inner join JYOrder_Sub B on A.SubId=B.SubId where');
    SQL.Add(' A.MainId=YF_Money_CR.YFTypeId and A.CRType=''正常出库'' and CPType=''正品''  ');
    SQL.Add(' and B.PRTPrice=YF_Money_CR.Price)');
    sql.Add(',PS=(select isnull(count(*),0) from CK_BanCP_CR A  ');
    sql.Add(' inner join JYOrder_Sub B on A.SubId=B.SubId where');
    SQL.Add(' A.MainId=YF_Money_CR.YFTypeId and A.CRType=''正常出库'' and CPType=''正品''  ');
    SQL.Add(' and B.PRTPrice=YF_Money_CR.Price )');
    sql.Add(' where YFTypeId=''' + Trim(Order_Main10.fieldbyname('Mainid').AsString) + '''');
    sql.Add(' and Price=' + Order_Main10.fieldbyname('PRTPrice').AsString);
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
    sql.Add(' where YFID=''' + Trim(YFID) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID=' + CRID);
    ExecSQL;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFId=''' + Trim(YFID) + '''');
    Open;
  end;
  if ADOQueryTemp.FieldByName('Qty').Value = 0 then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from YF_Money_CR where YFID=''' + Trim(YFID) + '''');
      ExecSQL;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where CRId=' + CRID);
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from YF_Money_KC where CRId=' + CRID);
      ExecSQL;
    end;
  end;

  Result := True;
end;

procedure TfrmCPCKMdList.P_CodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCPCKMdList.ConnoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(Trim(ConNo.Text)) < 4 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        SQL.Add('select A.*,B.ConNo,C.C_CodeName,C.C_Code,C.C_Color,D.APXH ');
        sql.add('from CK_PBCP_CR A ');
        Sql.add(' inner join Cloth_Main B on A.MainId=B.MainId');
        Sql.add(' inner join Cloth_Sub C on A.MainId=C.MainId');
        Sql.add(' inner join WFB_MJJY_PB D on A.MJId=D.MJId');
        sql.add('where B.ConNo like :ConNo');
        SQL.Add(' and CRFlag=''出库'' ');
        Parameters.ParamByName('ConNo').Value := '%' + Trim(ConNo.Text) + '%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain, CDS_Main);
      SInitCDSData20(ADOQueryMain, CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmCPCKMdList.CheJianChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCPCKMdList.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if CDS_Main.IsEmpty then
    EXIT;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
//  if ComboBox1.Text = '成品码单1' then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.add('exec P_CPCK_Print_CKMD1 ' + quotedstr(Trim(CDS_Main.fieldbyname('FHMainID').AsString)));
//      sql.add(', ' + quotedstr(Trim(CDS_Main.fieldbyname('BatchNo').AsString)));
//      sql.add(', ' + quotedstr(Trim(CDS_Main.fieldbyname('WXPH').AsString)));
//      sql.add(', ' + quotedstr(Trim(CDS_Main.fieldbyname('FromMainId').AsString)));
//     // ShowMessage(SQL.Text);
//      Open;
//    end;
//    SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
//    SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);
//  end;

  if (trim(ComboBox1.Text) = '成品码单2') or (trim(ComboBox1.Text) = '成品码单1') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_CPCK_Print_CKMD2 ' + quotedstr(Trim(CDS_Main.fieldbyname('FHMainID').AsString)));
      sql.add(', ' + quotedstr(Trim(CDS_Main.fieldbyname('P_Code').AsString)));

      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
    SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_CPCK_Print_CKMD2_2 ' + quotedstr(Trim(CDS_Main.fieldbyname('FHMainID').AsString)));
      sql.add(', ' + quotedstr(Trim(CDS_Main.fieldbyname('P_Code').AsString)));

      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_JuanPRT2);
    SInitCDSData20(ADOQueryTemp, CDS_JuanPRT2);
  end;
  if (trim(ComboBox1.Text) = '成品码单3') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_CPCK_Print_CKMD_jt ' + quotedstr(Trim(CDS_Main.fieldbyname('FHMainID').AsString)));
//      sql.add(', ' + quotedstr(Trim(CDS_Main.fieldbyname('BatchNo').AsString)));
//      sql.add(', ' + quotedstr(Trim(CDS_Main.fieldbyname('WXPH').AsString)));
      sql.add(', ' + quotedstr(Trim(CDS_Main.fieldbyname('P_Code').AsString)));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
    SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('UPDATE SQ_FH_Sub set DYFLAG=''已打印''  where  fhmainid=(select fhmainid from SQ_FH_Sub e where  e.fhsubid=''' + Trim(CDS_Main.fieldbyname('fhsubid').AsString) + '''');
    sql.Add(')');
    execsql;
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
//    RMVariables['OrdNo'] := Trim(CDS_Main.fieldbyname('OrdNo').AsString)；
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmCPCKMdList.ToolButton2Click(Sender: TObject);
var
  fPrintFile: string;
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if CDS_Main.Fieldbyname('SSel').AsBoolean then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value := Trim(CDS_Main.fieldbyname('CKOrdNo').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Main.EnableControls;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select MAX(A.FHDate) MJDate,A.SHDanWei,A.CRType,A.TruckId,B.PrtCode,B.PrtCodeName,B.prtspec,C.CustomerNoName');
    sql.Add(' ,G.ConNo,F.C_Color,F.BatchNoHZ,count(E.MJXH) ps,sum(D.KgQty) mz');
    sql.Add(' from FHSQ_Main A inner join JYOrderCon_Sub_PB B on A.ConSubId=B.SubId');
    sql.Add(' inner join JYOrderCon_Main_PB C on B.MainId=C.MainId');
    sql.Add(' inner join CK_PBCP_CR D on D.CKOrdNo=A.FSId');
    sql.Add(' inner join  WFB_MJJY_PB E on E.MJID=D.MJID');
    sql.Add(' inner join Cloth_Sub F on F.SubId=E.SubId');
    sql.Add(' inner join Cloth_Main G on G.MainId=E.MainId');
    SQL.Add(' where A.FSId in (SELECT distinct SubId FROM TBSubID where Dname=' + quotedstr(Trim(DCode)) + ')');
    SQL.Add('	group by A.SHDanWei,A.CRType,A.TruckId,B.PrtCode,B.PrtCodeName,B.prtspec,C.CustomerNoName,G.ConNo,F.C_Color,F.BatchNoHZ');
    SQL.Add('	order by A.SHDanWei,A.CRType,A.TruckId,B.PrtCode,B.PrtCodeName,B.prtspec,C.CustomerNoName,G.ConNo,F.C_Color,F.BatchNoHZ');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
  SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\出库码单.rmf';
  if FileExists(fPrintFile) then
  begin
//    RMVariables['ranchang']:=Trim(CDS_Main.fieldbyname('ranchang').AsString);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmCPCKMdList.v1Column4PropertiesChange(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxCheckBox(Sender).EditValue;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    CDS_Main.FieldByName('SSel').value := mvalue;
    post;
  end;
end;

procedure TfrmCPCKMdList.ConnoChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmCPCKMdList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  Tabint: integer;
  FFHMainid, FP_Code, FFromMainId: string;
begin
  try
    frmFinishedClothOutList_MX := TfrmFinishedClothOutList_MX.Create(self);
    with frmFinishedClothOutList_MX do
    begin
      CKNo.text := Trim(self.CDS_Main.fieldbyname('FHMainID').AsString);
      P_Code.text := Trim(self.CDS_Main.fieldbyname('P_Code').AsString);
      if showmodal = 1 then
      begin
        initGrid();
      end;
    end;
  finally
    frmFinishedClothOutList_MX.free;
  end;


//  Tabint := cxTabControl1.TabIndex;
//  case Tabint of
//    0:
//      begin
//        FFHMainid := Trim(Self.CDS_Main.fieldbyname('FHMainid').AsString);
//        FFromMainId := Trim(Self.CDS_Main.fieldbyname('FromMainId').AsString);
//        cxTabControl1.TabIndex := 1;
//        try
//          ADOQueryMain.DisableControls;
//          with ADOQueryMain do
//          begin
//            Close;
//            sql.Clear;
//            SQL.Add('SELECT AA.*,C.KHName,C.SYRName,C.ywy,C.DeliveryDate,C.Note,D.P_Code,D.P_CodeName,D.P_Spec,D.P_MF,D.P_KZ,D.P_Color ');
//            SQL.Add(',E.SQDate,E.SQType,E.Filler,E.Chker,E.ChkTime,E.FHDate FROM(');
//            Sql.Add('  select A.FHMainid,B.FromMainId,B.FromSubId,C.BatchNo,C.WXPH,B.P_Code,B.SQPiQty');
//            Sql.Add(' ,FHPiQtyHZ=(select count(JYLen) from CK_Cloth_CR X where X.CKNO=A.FHMainId and X.BatchNo=C.BatchNo and X.WXPH=C.WXPH and X.MainID=B.FromMainId)');
//            Sql.Add(' ,FHQtyHZ=(select sum(JYLen) from CK_Cloth_CR X where X.CKNO=A.FHMainId and X.BatchNo=C.BatchNo and X.WXPH=C.WXPH and X.MainID=B.FromMainId)');
//            sql.Add('  from SQ_FH_Main A LEFT JOIN SQ_FH_Sub B ON B.FHMainId=A.FHMainId  left join CK_Cloth_CR C on C.CKNO=A.FHMainId');
//            sql.Add(' where A.FHDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//            sql.Add(' and A.FHDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
//            sql.Add(' and FHPiQtyHZ>0 ');
//            sql.Add(' and  A.FHMainid=' + quotedstr(Trim(FFHMainid)));
//            sql.Add(' and  B.FromMainId=' + quotedstr(Trim(FFromMainId)));
//            SQL.Add(' GROUP BY A.FHMainid,B.FromMainId,B.FromSubId,C.BatchNo,C.WXPH,B.P_Code,B.SQPiQty');
//            SQL.Add(')AA LEFT JOIN PD_Plan_Main C ON C.MainId=AA.FromMainId');
//            sql.Add(' LEFT JOIN PD_Plan_Sub D ON D.SubId=AA.FromSubId');
//            SQL.Add('left JOIN SQ_FH_Main E on AA.FHMainId =E.FHMainId');
//            SQL.Add('where AA.FHPiQtyHZ>0 ');
//
////            ShowMessage(sql.text);
//            Open;
//          end;
//
//          SCreateCDS20(ADOQueryMain, CDS_Main);
//          SInitCDSData20(ADOQueryMain, CDS_Main);
//        finally
//          ADOQueryMain.EnableControls;
//        end;
//      end;
//  end;
end;

procedure TfrmCPCKMdList.ToolButton3Click(Sender: TObject);
var
  FBJNO: string;
  fgdy: string;
begin
  if CDS_Main.IsEmpty then
    exit;

  try
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add(' SELECT wxid FROM BS_Company_Main WHERE NAME=(SELECT SHDWName FROM SQ_FH_Main B WHERE B.FHMainId=' + QuotedStr(CDS_Main.fieldbyname('fhmainid').AsString));
      sql.Add(')and cotype=''客户'' AND VALID=''Y''');
      Open;
    end;
    if ADOQueryTemp.FieldByName('wxid').AsString = '' then
    begin
      Application.MessageBox('该客户没有绑定微信', '提示', 0);
      exit;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;

      sql.Add('exec P_Do_WXInsert_MD ');
      sql.Add('@CKORDNO=' + quotedstr(trim(CDS_Main.fieldbyname('fhsubid').AsString)));

      execsql;
    end;
    GetHTTP(IdHTTP1, 'http://www.rightsoft.top/chenfeng/api/message/send/OrderDelivery');

  finally

    application.MessageBox('推送成功！', '提示信息');
  end;
end;

procedure TfrmCPCKMdList.ToolButton4Click(Sender: TObject);
begin
  if CDS_Main.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.FieldByName('SSel').AsBoolean = True then
      begin
        with ADOQueryTemp do
        begin
          close;
          sql.Clear;
//          sql.Add(' SELECT wxid FROM BS_Company_Main WHERE NAME=(SELECT SHDWName FROM SQ_FH_Main B WHERE B.FHMainId=' + QuotedStr(CDS_Main.fieldbyname('fhmainid').AsString));
//          sql.Add(')and cotype=''客户'' AND VALID=''Y''');

          SQL.Add('EXEC P_push_check @SHDWNAME=' + QuotedStr(CDS_Main.fieldbyname('SHDWNAME').AsString));
          OPEN;
        end;
        if ADOQueryTemp.FieldByName('cs').AsFloat = 0 then
        begin
          Application.MessageBox('该客户没有绑定微信', '提示', 0);
          CDS_Main.EnableControls;
          exit;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;

          sql.Add('exec P_Do_WXInsert_MD ');
          sql.Add('@CKORDNO=' + quotedstr(trim(CDS_Main.fieldbyname('fhsubid').AsString)));

          execsql;
        end;
        GetHTTP(IdHTTP1, 'http://www.rightsoft.top/chenfeng/api/message/send/OrderDelivery');

        CDS_Main.Delete;
      end
      else
        Next;
    end;
  end;
  CDS_Main.EnableControls;
  InitGrid();
end;

procedure TfrmCPCKMdList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if '已打印' = AViewInfo.GridRecord.Values[Tv1Column7.Index] then
    ACanvas.Brush.Color := $0080FF80;
end;

end.

