unit U_ClothSCHZListSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxCheckBox, cxPC, cxContainer, cxMaskEdit;

type
  TfrmClothSCHZListSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    ConNo: TEdit;
    Label5: TLabel;
    C_CodeName: TEdit;
    Order_Main: TClientDataSet;
    Label10: TLabel;
    C_Code: TEdit;
    Panel2: TPanel;
    cxGrid3: TcxGrid;
    TvPBMX: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn43: TcxGridDBColumn;
    cxGridDBColumn44: TcxGridDBColumn;
    cxGridDBColumn45: TcxGridDBColumn;
    cxGridDBColumn46: TcxGridDBColumn;
    cxGridDBColumn47: TcxGridDBColumn;
    cxGridDBColumn51: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    CDS_YuanLiao: TClientDataSet;
    DataSource2: TDataSource;
    CDS_PBMX: TClientDataSet;
    DataSourcePBMX: TDataSource;
    ADOQuery1: TADOQuery;
    vPBMXColumn1: TcxGridDBColumn;
    ToolButton2: TToolButton;
    vPBMXColumn2: TcxGridDBColumn;
    vPBMXColumn3: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    vPBMXColumn4: TcxGridDBColumn;
    vPBMXColumn5: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    Label4: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label11: TLabel;
    Label9: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label20: TLabel;
    Label23: TLabel;
    Label24: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    Label28: TLabel;
    Label25: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    Label32: TLabel;
    Label33: TLabel;
    Label34: TLabel;
    Label39: TLabel;
    Label49: TLabel;
    Label35: TLabel;
    Label36: TLabel;
    Label51: TLabel;
    Label53: TLabel;
    Label50: TLabel;
    Label52: TLabel;
    Label54: TLabel;
    Label55: TLabel;
    Label56: TLabel;
    Label57: TLabel;
    Label58: TLabel;
    Label59: TLabel;
    ConNo1: TEdit;
    QDTime: TDateTimePicker;
    C_CodeName1: TcxButtonEdit;
    KZQty: TEdit;
    CarType: TcxButtonEdit;
    MFQty: TEdit;
    CompanyName: TcxButtonEdit;
    ZhuanQty: TEdit;
    DayQty: TEdit;
    DeliveryDate: TDateTimePicker;
    C_Color: TEdit;
    PiKgQty: TEdit;
    PiQty: TEdit;
    C_Qty: TEdit;
    C_Code1: TEdit;
    PiZhong: TEdit;
    ZhenCun: TcxButtonEdit;
    ZongZhenQty: TEdit;
    ChouZhen: TcxButtonEdit;
    PiKgQtyFD: TEdit;
    C_Spec: TEdit;
    GZGSType: TComboBox;
    ShaChang: TEdit;
    C_Note: TRichEdit;
    TaiTouFlag: TComboBox;
    C_HX: TEdit;
    KZ: TEdit;
    MF: TEdit;
    SJCarNO: TEdit;
    HuaGao: TEdit;
    HuaKuan: TEdit;
    BuJia: TEdit;
    ChiLun: TEdit;
    ZhuanSu: TEdit;
    ZhenTongGD: TEdit;
    LuShu: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column3: TcxGridDBColumn;
    v2YCLName: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2YCLSpec: TcxGridDBColumn;
    v2PinPai: TcxGridDBColumn;
    v2GYSName: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column12: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure Tv1DblClick(Sender: TObject);
  private
    FInt,PFInt:Integer;
    canshu1:string;
    procedure InitGrid();
    procedure InitForm();
    procedure InitRZData();
    { Private declarations }
  public
    RZConNO:String;
    { Public declarations }
  end;

var
  frmClothSCHZListSel: TfrmClothSCHZListSel;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp;

{$R *.dfm}

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

procedure TfrmClothSCHZListSel.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('坯布生产计划单ZSel',Tv1,'坯布生产管理');
end;

procedure TfrmClothSCHZListSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select * ');
      sql.Add('from Cloth_Main A inner join CLoth_Sub B on A.Mainid=B.MainId where  ');  //A.YLOK=1
      sql.Add('  EndFlag=0 ');
      if Trim(RZConNO)<>'' then
      begin
        sql.Add(' and isnull(A.ConNo,'''')='''+Trim(RZConNO)+'''');
      end else
      begin
        sql.Add(' and A.QDTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.QDTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
      end;
      sql.Add(' order by QDTime desc ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  {if Trim(RZConNO)<>'' then
  begin
    InitRZData();
  end;}
end;

procedure TfrmClothSCHZListSel.InitForm();
begin
  ReadCxGrid('坯布生产计划单ZSel',Tv1,'坯布生产管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-45;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
 { if Trim(RZConNO)<>'' then
  begin
    ToolButton2.Visible:=False;
  end;  }
end;

procedure TfrmClothSCHZListSel.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;


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

procedure TfrmClothSCHZListSel.ConNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmClothSCHZListSel.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmClothSCHZListSel.conPress(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 * from Cloth_Main A inner join CLoth_Sub B on A.Mainid=B.MainId');
        sql.Add(' where A.ConNo like '''+'%'+Trim(ConNo.Text)+'%'+'''');
        sql.Add(' and A.YLOK=1 and EndFlag=0');

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

procedure TfrmClothSCHZListSel.FormDestroy(Sender: TObject);
begin
  frmClothSCHZListSel:=nil;
end;

procedure TfrmClothSCHZListSel.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
 // InitRZData();
end;
procedure TfrmClothSCHZListSel.InitRZData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' exec Cloth_QryList :MainId,:WSql');
    if Order_Main.IsEmpty=False then
    begin
      ADOQuery1.Parameters.ParamByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
      ADOQuery1.Parameters.ParamByName('WSql').Value:='  ';
    end else
    begin
      ADOQuery1.Parameters.ParamByName('MainId').Value:='';
      ADOQuery1.Parameters.ParamByName('WSql').Value:=' and 1<>1 ';
    end;
    Open;
  end;
  SCSHData(ADOQuery1,ScrollBox1,0);
  SCSHData(ADOQuery1,ScrollBox1,2);
  ConNo1.Text:=Trim(ADOQuery1.fieldbyname('ConNo').AsString);
  C_CodeName1.Text:=Trim(ADOQuery1.fieldbyname('C_CodeName').AsString);
  C_Code1.Text:=Trim(ADOQuery1.fieldbyname('C_Code').AsString);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(',YLQty=Cast(C_Qty*PBQty*0.01*(1+isnull(ZPSH,0)*0.01) as decimal(18,1))');
    sql.Add(',SQQty=(select sum(SQQty) from CaiGou_SQ SQ where SQ.ConNo=C.ConNo and SQ.SPName=A.YCLName ');
    sql.Add('       and SQ.Valid=''Y'' and isnull(SQ.ConNo,'''')<>'''')');
    sql.Add(',DHQty=(select sum(Qty) from CK_SXPB_CR SQ where SQ.PBNO=C.ConNo and SQ.SPName=A.YCLName ');
    sql.Add('         and isnull(SQ.PBNO,'''')<>'''' and ZFFlag=0 and CKName=''纱线'' and CRFlag=''入库'')');
    sql.Add(',CKQty=(select sum(Qty) from CK_SXPB_CR SQ where SQ.PBNO=C.ConNo and SQ.SPName=A.YCLName ');
    sql.Add('         and isnull(SQ.PBNO,'''')<>'''' and ZFFlag=0 and CKName=''纱线'' and CRFlag=''出库'')');
    sql.Add(' from Cloth_YCL_PB A ');
    sql.Add(' inner join Cloth_Sub B on A.SubId=B.SubId');
    sql.Add(' where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_YuanLiao);
  SInitCDSData20(ADOQuery1,CDS_YuanLiao);
end;
procedure TfrmClothSCHZListSel.Tv2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select MX.*  ');
    sql.Add(',YLQty10=Cast(C_Qty*A.PBQty*0.01*(1+isnull(ZPSH,0)*0.01)*MX.PBQty*0.01*(1+isnull(WLSH,0)*0.01) as decimal(18,1))');
    sql.Add(' from Cloth_YCL_PB A');
    sql.Add(' inner join Cloth_Sub B on A.SubId=B.SubId');
    sql.Add(' inner join Cloth_YCL_PB_MX MX on A.PBID=MX.PBID');
    SQL.Add(' where MX.PBID='''+Trim(CDS_YuanLiao.fieldbyname('PBID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PBMX);
  SInitCDSData20(ADOQueryTemp,CDS_PBMX);
end;

procedure TfrmClothSCHZListSel.ToolButton2Click(Sender: TObject);
begin
  {if (CDS_YuanLiao.Locate('SSel',True,[])=False) and (CDS_PBMX.Locate('SSel',True,[])=False) then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end; }
  if Order_Main.IsEmpty then Exit;
  ModalResult:=1;
end;

procedure TfrmClothSCHZListSel.Tv1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { IF AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('JHDFlag').Index]=True then
    ACanvas.Brush.Color:=clSkyBlue;    }
end;

procedure TfrmClothSCHZListSel.Tv1DblClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  ModalResult:=1;
end;

end.
