unit U_PBCKMdList;

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;

type
  TfrmPBCKMdList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: 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;
    FSID: TEdit;
    MovePanel2: TMovePanel;
    Label5: TLabel;
    ConNo: TEdit;
    Label8: TLabel;
    CRType: TComboBox;
    ADOQueryDH: TADOQuery;
    ADOQueryPrint: TADOQuery;
    Label14: TLabel;
    SHDanWei: TEdit;
    CDS_Juan: TClientDataSet;
    CDS_JuanPRT: TClientDataSet;
    RMDBMX: TRMDBDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column8: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column5: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    ToolButton1: TToolButton;
    CDS_Main: TClientDataSet;
    ComboBox1: TComboBox;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v1Column3: TcxGridDBColumn;
    v1Column4: 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 ToolButton1Click(Sender: TObject);
    procedure v1Column4PropertiesChange(Sender: TObject);
    procedure BegDateChange(Sender: TObject);
    procedure FSIDKeyPress(Sender: TObject; var Key: Char);
    procedure CRTypeChange(Sender: TObject);
  private
    procedure InitGrid();
    function YSData(Order_Main10: TClientDataSet): Boolean;
    procedure GetLotData();
    procedure GetLotDataPT();
    { Private declarations }
  public
    { Public declarations }
    FZZType: string;
  end;

//var
//  frmPBCKMdList:TfrmPBCKMdList;

implementation

uses
  U_DataLink, U_Fun, U_RTFun, U_ZDYHelp, U_GYSListPBSel;

{$R *.dfm}

procedure TfrmPBCKMdList.FormDestroy(Sender: TObject);
begin
//  frmPBCKMdList:=nil;
end;

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

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

procedure TfrmPBCKMdList.InitGrid();
var
  fwhere, Pwhere: string;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      Sql.Add(' select A.*  from FHSQ_Main A');
      sql.Add(' where  FHDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
      sql.Add(' and FHDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      SQL.Add(' and isnull(A.Chker,'''')<>'''' ');
      if Pwhere <> '' then
        sql.Add(Pwhere);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmPBCKMdList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('成品出库ListYX' + '1', Tv1, '坯布仓库');
  Close;
end;

procedure TfrmPBCKMdList.FormShow(Sender: TObject);
begin
  ReadCxGrid('成品出库ListYX', Tv1, '坯布仓库');
end;

procedure TfrmPBCKMdList.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 TfrmPBCKMdList.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;

function TfrmPBCKMdList.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 TfrmPBCKMdList.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if CDS_Main.IsEmpty then
    EXIT;
  if (trim(ComboBox1.Text) = '普通码单') then
  begin

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD1 @CKOrdNo=' + quotedstr(Trim(CDS_Main.fieldbyname('FSID').AsString)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
    SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);
  end;
  if (trim(ComboBox1.Text) = '成品出库汇总单') then
  begin

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD2 @CKOrdNo=' + quotedstr(Trim(CDS_Main.fieldbyname('FSID').AsString)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
    SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);
  end;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['SHDanWei'] := Trim(CDS_Main.fieldbyname('SHDanWei').AsString);
    RMVariables['MJDate'] := CDS_Main.fieldbyname('FHDate').Value;
    RMVariables['CRType'] := CDS_Main.fieldbyname('CRType').AsString;
    RMVariables['FHPSHZ'] := CDS_Main.fieldbyname('FHPSHZ').Value;
    RMVariables['FHQtyHZ'] := CDS_Main.fieldbyname('FHQtyHZ').Value;
    RMVariables['FHKgQtyHZ'] := CDS_Main.fieldbyname('FHKgQtyHZ').Value;
    RMVariables['FHNetKGQtyHZ'] := CDS_Main.fieldbyname('FHNetKGQtyHZ').Value;
    RMVariables['FSID'] := Trim(CDS_Main.fieldbyname('FSID').AsString);
    RMVariables['FHTaiTou'] := Trim(CDS_Main.fieldbyname('FHTaiTou').AsString);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmPBCKMdList.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 TfrmPBCKMdList.BegDateChange(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmPBCKMdList.CRTypeChange(Sender: TObject);
begin
  InitGrid();
end;

end.

