unit U_ProductOrderNewList_GDJD;

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, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, cxPC, cxCurrencyEdit, Mask, DBCtrls, cxContainer, cxTextEdit,
  cxDBEdit, cxButtonEdit;

type
  TfrmProductOrderNewList_GDJD = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    FactoryName: TEdit;
    Label2: TLabel;
    Label5: TLabel;
    GenDanName: TEdit;
    Label8: TLabel;
    SPName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    Label11: TLabel;
    OrdDefStr1: TEdit;
    Label6: TLabel;
    ISPH: TComboBox;
    PrtColor: TEdit;
    Label7: TLabel;
    orderType: TComboBox;
    Label24: TLabel;
    khConNo: TEdit;
    Label10: TLabel;
    Label1: TLabel;
    Label12: TLabel;
    Than: TComboBox;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOQuery1: TADOQuery;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    ADOQueryMain: TADOQuery;
    Label13: TLabel;
    SPSpec: TEdit;
    DataSource3: TDataSource;
    Order_WJG: TClientDataSet;
    ADOQuery2: TADOQuery;
    cxGridPopupMenu3: TcxGridPopupMenu;
    Label14: TLabel;
    cxGrid3: TcxGrid;
    TV1: TcxGridDBTableView;
    V3FromGX: TcxGridDBColumn;
    V3FactoryName: TcxGridDBColumn;
    V3TPQty: TcxGridDBColumn;
    V3PiQty: TcxGridDBColumn;
    V3Qty: TcxGridDBColumn;
    V3BCPCKPS: TcxGridDBColumn;
    V3BCPCKQty: TcxGridDBColumn;
    V3SunHao: TcxGridDBColumn;
    V3Blv: TcxGridDBColumn;
    V3QtyUnit: TcxGridDBColumn;
    V3ToFactoryName: TcxGridDBColumn;
    V3YJSH: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel2: TPanel;
    V3OrderNO: TcxGridDBColumn;
    V3ConNo: TcxGridDBColumn;
    V3SPName: TcxGridDBColumn;
    V3SPSpec: TcxGridDBColumn;
    V3GenDanName: TcxGridDBColumn;
    Tprint: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    Order_Print: TClientDataSet;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure v1Column3CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure ComboBox2Change(Sender: TObject);
    procedure ThanChange(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure YWYKeyPress(Sender: TObject; var Key: Char);
    procedure SPNameKeyPress(Sender: TObject; var Key: Char);
    procedure TprintClick(Sender: TObject);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
        { Private declarations }
  public

    canshu1:string;
    { Public declarations }
  end;

var
  frmProductOrderNewList_GDJD: TfrmProductOrderNewList_GDJD;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}
procedure TfrmProductOrderNewList_GDJD.FormDestroy(Sender: TObject);
begin
  frmProductOrderNewList_GDJD:=nil;
end;

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

procedure TfrmProductOrderNewList_GDJD.FormCreate(Sender: TObject);
begin
  canshu1:=trim(DParameters1);
  DQdate:=SGetServerDate(ADOQuery1);
end;

procedure TfrmProductOrderNewList_GDJD.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption+tv1.Name+'1',Tv1,'生产指示单管理');
end;

procedure TfrmProductOrderNewList_GDJD.InitGrid();
var
  strwhere,Tthan:string;
begin
  Panel2.Visible:=true;
  Panel2.Refresh;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.add('select AA.*,SunHao=TPQty-Qty,Blv=(TPQty-Qty)/TPQty*100 ');
      sql.add(',YJSH=(select Top 1 C.PSSH from JYOrder_Process C ');
      sql.add('where C.Mainid=AA.OrdMainidRK and C.PSName=AA.FromGX) ');
      sql.add('from (select A.ConNo,A.OrderNo,A.FactoryName,A.FromGX,A.ToFactoryName,A.SPName,A.SPSpec,A.GenDanName,A.OrdMainidRK,min(A.PXDate) PXDate,');
      sql.add('TPQty=(select Sum(B.Qty) from CK_SXPB_CR B where B.OrdMainidCK=A.OrdMainidRK and B.TOGX=A.FromGX ');
      sql.add('and B.ToFactoryName=A.TOFactoryName and B.FromFactoryName=A.FactoryName and B.CRFlag=''出库''),');
      sql.add('Sum(A.PiQty) PiQty,Sum(A.Qty) Qty,A.QtyUnit');
      sql.add(',BCPCKPS=Sum(BCPCKPS),BCPCKQty=Sum(BCPCKQty)');
      sql.add(' from (select D.ConNo,D.OrderNo,C.PiQty,C.Qty,C.QtyUnit,C.TOFactoryName,B.SPName,isnull(B.SPSpec,D.MPRTSpec) SPSpec,');
      sql.Add(' B.GenDanName,C.FactoryName,C.OrdMainidRK,C.FromGX,Sum(B.PiQty) BCPCKPS,Sum(B.Qty) BCPCKQty');
      sql.Add(' ,PXDate=min(C.CRTime)');
      sql.add(' from CK_SXPB_CR B inner join CK_SXPB_CR C on B.FromFactoryName=C.TOFactoryName ');
      sql.Add(' inner join JYOrder_Main D on D.Mainid=B.OrdMainidCK');
      sql.Add(' and B.GenDanName=C.GenDanName and B.OrdMainidRK=C.OrdMainidRK and C.FromGX=B.FromGX and C.FactoryName=B.FactoryName ');
      SQL.ADD('where D.Filltime>='''+trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+''' ');
      SQL.ADD('and D.Filltime<'''+trim(FormatDateTime('yyyy-MM-dd',endDate.Date+1))+''' ');
      sql.Add('and isnull(C.CKName,'''')=''外加工'' and ');
      SQL.add('isnull(B.CKName,'''')=''外加工'' and C.CRFlag=''入库'' and B.CRFlag=''出库'' ');
      SQL.ADD('group by D.ConNo,D.OrderNo,C.PiQty,C.Qty,C.QtyUnit,C.TOFactoryName,B.SPName,isnull(B.SPSpec,D.MPRTSpec),B.GenDanName,C.FactoryName,C.OrdMainidRK,C.FromGX) A ');
      sql.add('group by A.ConNo,A.OrderNo,A.FactoryName,A.FromGX,A.ToFactoryName,A.SPName,A.SPSpec,A.GenDanName,A.OrdMainidRK,A.QtyUnit) AA');
      sql.Add(' order by AA.PXDate,FactoryName,FromGX');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
    //TBFind.Click;
  end;
  Panel2.Visible:=false;
end;


procedure TfrmProductOrderNewList_GDJD.InitForm();
begin
  readCxGrid(self.Caption+tv1.Name+'1',Tv1,'生产指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQuery1)-15;
  EndDate.DateTime:=SGetServerDate10(ADOQuery1);
 // InitGrid();
end;

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

procedure TfrmProductOrderNewList_GDJD.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'生产指示单列表');
end;

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

procedure TfrmProductOrderNewList_GDJD.OrderNoChange(Sender: TObject);
begin
 TBFind.Click;
end;

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

procedure TfrmProductOrderNewList_GDJD.CheckBox1Click(Sender: TObject);
begin
   TBFind.Click;
end;

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

procedure TfrmProductOrderNewList_GDJD.OrderNoKeyPress(Sender: TObject;
  var Key: Char);
var Tthan:string;
begin
  Tthan:='';
  if Key=#13 then
  begin
    if trim(than.Text)='大于' then
    begin
      Tthan:='1';
    end;
    if trim(than.Text)='小于或等于' then
    begin
      Tthan:='2';
    end;
    if Length(Trim(OrderNo.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.add('exec P_View_order_Jd :WSql,:Than') ;
        parameters.ParamByName('WSql').Value:=' and OrderNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''';
        parameters.ParamByName('Than').Value:=Tthan;
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderNewList_GDJD.v1Column3CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
{  iF AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('RK_KGQty').Index]>
  AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('HC_Qty').Index]*1.05 then
  begin
    ACanvas.Brush.Color:=Clred;
  end;  }
end;

procedure TfrmProductOrderNewList_GDJD.ComboBox2Change(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmProductOrderNewList_GDJD.ThanChange(Sender: TObject);
begin
  initgrid();
end;

procedure TfrmProductOrderNewList_GDJD.ConNoKeyPress(Sender: TObject;
  var Key: Char);
var Tthan:string;
begin
  Tthan:='';
  if Key=#13 then
  begin
    if trim(than.Text)='大于' then
    begin
      Tthan:='1';
    end;
    if trim(than.Text)='小于或等于' then
    begin
      Tthan:='2';
    end;
    if Length(Trim(ConNo.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.add('exec P_View_order_Jd :WSql,:Than') ;
        parameters.ParamByName('WSql').Value:=' and [dbo].[F_Get_Order_SubStr](A.MainId,''ConNo'') like '''+'%'+Trim(ConNo.Text)+'%'+'''';
        parameters.ParamByName('Than').Value:=Tthan;
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderNewList_GDJD.YWYKeyPress(Sender: TObject;
  var Key: Char);
var Tthan:string;
begin
 
end;

procedure TfrmProductOrderNewList_GDJD.SPNameKeyPress(Sender: TObject;
  var Key: Char);
var
  Tthan,FName:string;
begin
  FName:=TEdit(Sender).Name;
  if Key=#13 then
  begin
    Tthan:='';
    if trim(than.Text)='大于' then
    begin
      Tthan:='1';
    end;
    if trim(than.Text)='小于或等于' then
    begin
      Tthan:='2';
    end;
    if Length(Trim(TEdit(Sender).Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.add('exec P_View_order_Jd :WSql,:Than') ;
        parameters.ParamByName('WSql').Value:=' and '+FName+'  like '''+'%'+Trim(TEdit(Sender).Text)+'%'+''' '
                                              +'and filltime>='''+trim(formatdatetime('yyyy-MM-dd',Begdate.DateTime))+''' '
                                              +'and filltime<'''+trim(formatdatetime('yyyy-MM-dd',enddate.DateTime+1))+'''';
        parameters.ParamByName('Than').Value:=Tthan;
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmProductOrderNewList_GDJD.TprintClick(Sender: TObject);
var fPrintFile:string;
begin
  if Order_Main.IsEmpty then exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\跟单损耗表.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\跟单损耗表.rmf'),'提示',0);
  end;  
end;

end.
