unit U_BPShengChanListSXCX;

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;

type
  TfrmBPShengChanListSXCX = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    Panel1: TPanel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    ConNo: TEdit;
    Order_Main: TClientDataSet;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    TFind: TToolButton;
    ToolButton2: TToolButton;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1RSSH: TcxGridDBColumn;
    v1RSCKQty: TcxGridDBColumn;
    v1RSRKQty: TcxGridDBColumn;
    Label4: TLabel;
    C_CodeName: TEdit;
    v1RSTHQty: TcxGridDBColumn;
    v1RSSYQty: TcxGridDBColumn;
    v1WLCKQty: TcxGridDBColumn;
    v1WLTHQty: TcxGridDBColumn;
    v1WLSYQty: TcxGridDBColumn;
    v1WLRKQty: TcxGridDBColumn;
    v1WLSH: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure TFindClick(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
  private
    procedure InitForm();
    procedure Initgrid();
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmBPShengChanListSXCX: TfrmBPShengChanListSXCX;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}
procedure TfrmBPShengChanListSXCX.Initgrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select AA.*,RSSYQty=isnull(RSCKQty,0)-isnull(RSTHQty,0),WLSYQty=isnull(WLCKQty,0)-isnull(WLTHQty,0)');
      sql.add(',RSSH=(case when isnull(RSCKQty,0)-isnull(RSTHQty,0)=0 then 0 else (isnull(RSCKQty,0)-isnull(RSTHQty,0)-isnull(RSRKQty,0))/(isnull(RSCKQty,0)-isnull(RSTHQty,0))*100 end)');
      sql.add(',WLSH=(case when isnull(WLCKQty,0)-isnull(WLTHQty,0)=0 then 0 else (isnull(WLCKQty,0)-isnull(WLTHQty,0)-isnull(WLRKQty,0))/(isnull(WLCKQty,0)-isnull(WLTHQty,0))*100 end)');
      sql.add(' from (select A.ConNo,A.ComTaiTou,B.C_CodeName,B.ChouZhenYT,B.ConDefStr2,B.ConDefStr10,B.MFQty,B.KZQty,B.C_Qty ');
      sql.Add(',RSTHQty=(select SUM(C.Qty) from CK_SXPB_CR C where C.SCSubIdRK=B.SubId and isnull(C.CKName,'''')=''纱线'' and C.CRFlag=''入库'' and C.CRType=''染纱加工退回'') ');
      sql.Add(',RSCKQty=(select SUM(C.Qty) from CK_SXPB_CR C where C.SCSubIdCK=B.SubId and isnull(C.CKName,'''')=''纱线'' and C.CRFlag=''出库'' and C.CRType=''染纱加工出库'') ');
      sql.Add(',RSRKQty=(select SUM(C.Qty) from CK_SXPB_CR C where C.SCSubIdRK=B.SubId and isnull(C.CKName,'''')=''纱线'' and C.CRFlag=''入库'' and C.CRType=''染纱完成入库'') ');
      sql.Add(',WLTHQty=(select SUM(C.Qty) from CK_SXPB_CR C where C.SCSubIdRK=B.SubId and isnull(C.CKName,'''')=''纱线'' and C.CRFlag=''入库'' and C.CRType=''网络加工退回'') ');
      sql.Add(',WLCKQty=(select SUM(C.Qty) from CK_SXPB_CR C where C.SCSubIdCK=B.SubId and isnull(C.CKName,'''')=''纱线'' and C.CRFlag=''出库'' and C.CRType=''网络加工出库'') ');
      sql.Add(',WLRKQty=(select SUM(C.Qty) from CK_SXPB_CR C where C.SCSubIdRK=B.SubId and isnull(C.CKName,'''')=''纱线'' and C.CRFlag=''入库'' and C.CRType=''网络完成入库'') ');
      sql.Add('from Cloth_Main A ');
      sql.Add('Left join Cloth_Sub B on B.MainId=A.MainId ');
      sql.add('where A.ConType=''生产'' and A.QDTime>='''+trim(formatdatetime('yyyy-MM-dd',begdate.datetime))+''' ');
      sql.add('and A.QDTime<'''+trim(formatdatetime('yyyy-MM-dd',Enddate.datetime+1))+''') AA ');
      sql.add('where (isnull(RSCKQty,0)-isnull(RSTHQty,0)<>0 or isnull(WLCKQty,0)-isnull(WLTHQty,0)<>0)');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmBPShengChanListSXCX.FormDestroy(Sender: TObject);
begin
  frmBPShengChanListSXCX:=nil;
end;

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

procedure TfrmBPShengChanListSXCX.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
end;

procedure TfrmBPShengChanListSXCX.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('白坯生产单选择坯1',Tv1,'白坯生产单管理');
end;


procedure TfrmBPShengChanListSXCX.InitForm();
begin
  ReadCxGrid('白坯生产单选择坯1',Tv1,'白坯生产单管理');
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-15;
  initgrid();
end;

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

procedure TfrmBPShengChanListSXCX.ToolButton3Click(Sender: TObject);
begin
  BegDate.SetFocus;
  Initgrid();
end;

procedure TfrmBPShengChanListSXCX.Tv1DblClick(Sender: TObject);
begin
  ToolButton3.Click;
end;

procedure TfrmBPShengChanListSXCX.ConNoChange(Sender: TObject);
begin
  TFind.Click;
end;

procedure TfrmBPShengChanListSXCX.TFindClick(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmBPShengChanListSXCX.ConNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        sql.Add('select AA.*,PBRKZQty=isnull(PBRKQty,0)+isnull(PBRKQty1,0) from (select A.ConNo,A.ComTaiTou,B.C_CodeName,B.ChouZhenYT,B.ConDefStr2,B.ConDefStr10,B.MFQty,B.KZQty,B.C_Qty ');
        sql.Add(',SXRKQty=(select SUM(C.Qty) from CK_SXPB_CR C where C.SCSubIdRK=B.SubId and isnull(C.CKName,'''')=''纱线'' and C.CRFlag=''入库'') ');
        sql.Add(',SXCKQty=(select SUM(C.Qty) from CK_SXPB_CR C where C.SCSubIdCK=B.SubId and isnull(C.CKName,'''')=''纱线'' and C.CRFlag=''出库'' and C.CRType in (''织造加工出库'',''染沙加工出库'',''网络加工出库'')) ');
        sql.Add(',PBRKQty=(select SUM(C.Qty) from CK_SXPB_CR C where C.SCSubIdRK=B.SubId and isnull(C.CKName,'''')=''坯布'' and C.CRFlag=''入库'' and C.CRType=''加工完成'') ');
        sql.Add(',PBRKQty1=(select SUM(C.Qty) from CK_SXPB_CR C inner join JYOrder_Main JM on JM.MainId=C.ORDMainIdRK where JM.OrderNo=A.OrderNo and isnull(C.CKName,'''')=''坯布'' and C.CRFlag=''入库'' and C.CRType=''自生产入库'') ');
        sql.Add('from Cloth_Main A ');
        sql.Add('Left join Cloth_Sub B on B.MainId=A.MainId ');
        sql.add('where A.ConType=''生产'' and A.ConNo='''+'%'+trim(ConNo.Text)+'%'+''') AA');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmBPShengChanListSXCX.Tv1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var i:integer;
begin
  {i:=Tv1.GetColumnByFieldName('SXSH').Index;
  if ARecord.Values[i]>3 then
  begin
    AStyle:=DataLink_FMMD.Red;
  end;}
end;

end.
