unit U_PBOutSFQDList;

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, cxTextEdit;

type
  TfrmPBOutSFQDList = 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;
    MovePanel2: TMovePanel;
    Label3: TLabel;
    FHkhpm: TEdit;
    ADOQueryDH: TADOQuery;
    ADOQueryPrint: TADOQuery;
    Label14: TLabel;
    CustomerNoName: TEdit;
    CDS_Juan: TClientDataSet;
    CDS_JuanPRT: TClientDataSet;
    RMDBMX: TRMDBDataSet;
    CDS_Main: TClientDataSet;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column7: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    ToolButton3: TToolButton;
    CheckBox1: TCheckBox;
    cxGrid1: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource2: TDataSource;
    TV2Column1: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    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 FHkhpmChange(Sender: TObject);
    procedure CheJianChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column4PropertiesChange(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure v1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure v1Column2CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
  private
    procedure InitGrid();
    procedure Initsub();
    function YSData(Order_Main10: TClientDataSet): Boolean;
    procedure GetLotData();
    procedure GetLotDataPT();
    { Private declarations }
  public
    { Public declarations }
  end;

//var
//  frmPBOutHZList:TfrmPBOutHZList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_PBOuGLtListMX;

{$R *.dfm}

procedure TfrmPBOutSFQDList.FormDestroy(Sender: TObject);
begin
//  frmPBOutHZList:=nil;
end;

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

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

procedure TfrmPBOutSFQDList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('SELECT *,WFPS=ISNULL(SQPS,0)-ISNULL(FHPS,0) from FHSQ_Main A  INNER JOIN FHSQ_Sub B ON A.FSID=B.FSID ');
      sql.Add(' WHERE  sqdate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
      sql.Add(' and sqdate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      if CheckBox1.Checked = True then
      begin
        SQL.Add('and (ISNULL(SQPS,0)-ISNULL(FHPS,0))>0');
      end;





//      SQL.Add('	select  convert(varchar(10),CRTIME,120) CRTime,C.KHNAME,E.C_Code,E.C_CodeName,ISNULL(c.price,0)price,C_SPEC,MF,KZ');
//      SQL.Add(',E.C_Color,COUNT(MJID) PS ,SUM(A.KGQty) KGQty,A.CKOrdNo,ISNULL(FHTT,FHTAITOU)FHTT,ISNULL(RC,ranchang)RC,isnull(KHTT,shouhuotaitou)KHTT,ISNULL(a.Note,D.NOTE)NOTE');
//      SQL.Add('from CK_PBCP_CR A inner join Cloth_Sub E on A.MainId=E.MainId inner join Cloth_Main C ON A.MAINID=C.MAINID');
//      SQL.Add('LEFT JOIN FHSQ_MAIN D ON D.FSID=A.CKOrdNo');
//      SQL.Add('	where A.CRFlag=''出库''');
//      sql.Add(' and CRTIME>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//      sql.Add(' and CRTIME<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
//      SQL.Add('group by CKOrdNo,convert(varchar(10),CRTime,120),C.KHNAME,C_Color,C_Code,C_CodeName,C_SPEC,MF,KZ,ISNULL(c.price,0),ISNULL(FHTT,FHTAITOU),ISNULL(RC,ranchang),isnull(KHTT,shouhuotaitou),ISNULL(a.Note,D.NOTE)');
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmPBOutSFQDList.Initsub();
begin
  try
    ADOQueryCmd.DisableControls;
    with ADOQueryCmd do
    begin
      Filtered := False;
      Close;
      sql.Clear;

      SQL.Add('select convert(varchar(10),crtime,120)crtime,sum(mjmaoz+isnull(fhkongjia,0))mjmaoz,count(*)js,khpm from WFB_MJJY_PB a     ');
      sql.Add(' join CK_PBCP_CR b on a.mjid=b.mjid and b.crflag=''出库'' where FHsubID=' + QuotedStr(Trim(CDS_Main.fieldbyname('ssid').AsString)));
      sql.Add('group by convert(varchar(10),crtime,120),khpm');
//      ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryCmd, CDS_Juan);
    SInitCDSData20(ADOQueryCmd, CDS_Juan);
  finally
    ADOQueryCmd.EnableControls;
  end;
end;

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

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

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

procedure TfrmPBOutSFQDList.FormShow(Sender: TObject);
begin
//  ReadCxGrid(Self.Caption, Tv1, '坯布仓库99');
  ReadCxGrid(Self.Caption + '1', Tv1, '坯布仓库99');
  ReadCxGrid(Self.Caption + '2', Tv2, '坯布仓库99');
end;

procedure TfrmPBOutSFQDList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  SelExportData(Tv1, ADOQueryMain, '实发清单');
end;

procedure TfrmPBOutSFQDList.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 TfrmPBOutSFQDList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBOutSFQDList.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 TfrmPBOutSFQDList.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 TfrmPBOutSFQDList.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 TfrmPBOutSFQDList.FHkhpmChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmPBOutSFQDList.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
begin

end;

procedure TfrmPBOutSFQDList.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 D.ConNo orderno,count(B.MJXH) ps,sum(A.KgQty) mz,C.C_Color,C.BatchNoHZ');
//    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(' inner join Cloth_Main D on D.MainId=C.MainId');
//    SQL.Add(' where A.CKOrdNo='+quotedstr(Trim(CDS_Main.fieldbyname('CKOrdNo').AsString)));
//    SQL.Add('	group by D.ConNo,C.C_Color,C.BatchNoHZ ');
//    SQL.Add('	order by D.ConNo,C.C_Color,C.BatchNoHZ');
//    Open;
//  end;
  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 TfrmPBOutSFQDList.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 TfrmPBOutSFQDList.ConNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBOutSFQDList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if (AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('DFH').Index]) > 0 then
    ACanvas.Brush.Color := $4763FF;
end;

procedure TfrmPBOutSFQDList.v1Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_MAIN do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate CK_PBCP_CR ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' where CKOrdNo=' + QuotedStr(CDS_MAIN.fieldbyname('CKOrdNo').AsString));
      SQL.ADD('AND isnull((SELECT C_CODE FROM Cloth_Sub WHERE SUBID=CK_PBCP_CR.SUBID),'''')=' + QUOTEDSTR(CDS_MAIN.fieldbyname('C_Code').AsString));
      SQL.ADD('AND isnull((SELECT C_CODEname FROM Cloth_Sub WHERE SUBID=CK_PBCP_CR.SUBID),'''')=' + QUOTEDSTR(CDS_MAIN.fieldbyname('C_CODEname').AsString));
      SQL.ADD('AND isnull((SELECT C_Color FROM Cloth_Sub WHERE SUBID=CK_PBCP_CR.SUBID),'''')=' + QUOTEDSTR(CDS_MAIN.fieldbyname('C_Color').AsString));
      SQL.ADD('AND ISNULL(FHTT,'''')=' + QUOTEDSTR(CDS_MAIN.fieldbyname('FHTT').AsString));
      SQL.ADD('AND ISNULL(RC,'''')=' + QUOTEDSTR(CDS_MAIN.fieldbyname('rc').AsString));
//      ShowMessage(sql.Text);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv1.Controller.EditingController.ShowEdit();
  except
    tv1.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmPBOutSFQDList.ToolButton3Click(Sender: TObject);
var
  maxno: string;
begin
  try
    frmPBOuGLtListMX := TfrmPBOuGLtListMX.Create(Application);
    with frmPBOuGLtListMX do
    begin
//      PState := 0;
//      FMainId := '';
      CkOrdNo.Text := Self.CDS_Main.FIELDBYNAME('CkOrdNo').AsString;
      if ShowModal = 1 then
      begin
//        InitGrid();
      end;
    end;
  finally
    frmPBOuGLtListMX.Free;
  end;
end;

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

procedure TfrmPBOutSFQDList.v1Column2CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if ARow1.Values[v1Column7.Index] = ARow2.Values[v1Column7.Index] then
    AAreEqual := True
  else
    AAreEqual := False;
end;

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

  initsub();
end;

end.

