unit U_PBKCList;

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, cxButtonEdit,
  cxDropDownEdit, cxPC, Buttons, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, cxTextEdit, dxSkinsCore,  
     dxSkinDarkRoom,
    
     
     
    
    
    
    
   dxSkinOffice2013White, 
     dxSkinSharpPlus,
   dxSkinSpringTime,  
   dxSkinsDefaultPainters, 
     dxSkinscxPCPainter;

type
  TfrmPBKCList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    v1Column1: TcxGridDBColumn;
    Label34: TLabel;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    KHNameJC: TEdit;
    v1Column17: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label2: TLabel;
    MYName: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label1: TLabel;
    Note: TEdit;
    v1MYMDPrice: TcxGridDBColumn;
    v1MYMDMoney: TcxGridDBColumn;
    v1JCDanWeiJC: TcxGridDBColumn;
    Label3: TLabel;
    JCDanWeiJC: TEdit;
    v1filler: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label6: TLabel;
    PBBatchNo: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Label7: TLabel;
    MYCode: TEdit;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    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 FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure Tv1Column2PropertiesEditValueChanged(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

var
  frmPBKCList: TfrmPBKCList;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmPBKCList.FormDestroy(Sender: TObject);
begin
  frmPBKCList := nil;
end;

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

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

procedure TfrmPBKCList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('坯布库存查询1', Tv1, '贸易生产管理');
  Close;
end;

procedure TfrmPBKCList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select AA.* ');
      sql.Add(' ,CASE WHEN(RSQty <> 0) THEN ((isnull(Qty,0)-isnull(THQty,0)-isnull(ZCQty,0))/isnull(RSQty,0) -1)*100 ELSE null END AS SL ');
      sql.Add(' from (select A.* ');
      sql.Add(',RSQty=(select SUM(Qty) from CK_MYSC_CR X where X.CRType=''加工码单'' and X.PBBatchNo=A.PBBatchNo and isnull(MYTypeFlag,'''') =''染色'' ) ');
      sql.Add(',RSKGQty=(select SUM(KGQty) from CK_MYSC_CR X where X.CRType=''加工码单'' and X.PBBatchNo=A.PBBatchNo and isnull(MYTypeFlag,'''') =''染色'') ');
      sql.Add(',HJGQty=(select SUM(Qty) from CK_MYSC_CR X where X.CRType=''加工码单'' and X.PBBatchNo=A.PBBatchNo and isnull(MYTypeFlag,'''') <> ''染色'' ) ');
      sql.Add(',HJGKGQty=(select SUM(KGQty) from CK_MYSC_CR X where X.CRType=''加工码单'' and X.PBBatchNo=A.PBBatchNo and isnull(MYTypeFlag,'''') <>''染色'') ');
      sql.Add(',THQty=(select SUM(Qty) from CK_MYSC_CR X where X.CRType=''坯布退货'' and X.PBBatchNo=A.PBBatchNo  ) ');
      sql.Add(',THKGQty=(select SUM(KGQty) from CK_MYSC_CR X where X.CRType=''坯布退货'' and X.PBBatchNo=A.PBBatchNo ) ');
      sql.Add(',ZCQty=(select SUM(Qty) from CK_MYSC_CR X where X.CRType=''坯布转厂'' and X.PBBatchNo=A.PBBatchNo  ) ');
      sql.Add(',ZCKGQty=(select SUM(KGQty) from CK_MYSC_CR X where X.CRType=''坯布转厂'' and X.PBBatchNo=A.PBBatchNo ) ');
      sql.Add(',KHNameJC=(select Top 1 B.KHNameJC from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.KHName)');
      sql.Add(',JCDanWeiJC=(select Top 1 B.KHNameJC from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.JCDanWei)');
      sql.Add(',JCKHNo=(select Top 1 B.KHNo from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.JCDanWei)');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' where 1=1 ');
      sql.Add(' and isnull(CRType,'''')=''采购入库'' ');
      SQL.Add('and isnull(MYType,'''')=''采购坯布''');
      SQL.Add(' and isnull(PBBatchNo,'''')<>'''') AA ');
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmPBKCList.InitForm();
begin
  ReadCxGrid('坯布库存查询1', Tv1, '贸易生产管理');
  InitGrid();
end;

procedure TfrmPBKCList.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 TfrmPBKCList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

procedure TfrmPBKCList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  TBFind.Click;
end;

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

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

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

procedure TfrmPBKCList.CustomerNoNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBKCList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmPBKCList.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBKCList.Tv1Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    with Order_Main do
    begin
      Edit;
      FieldByName('YGSunHao').Value := null;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate CK_MYSC_CR Set YGSunHao=Null where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
      ExecSQL;
    end;
  end
  else
  begin
    with Order_Main do
    begin
      Edit;
      FieldByName('YGSunHao').Value := mvalue;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate CK_MYSC_CR Set YGSunHao=' + mvalue + ' where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
      ExecSQL;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' exec P_Update_PBKC :PBBatchNo,:JGCNO');
    Parameters.ParamByName('PBBatchNo').Value := Trim(Order_Main.fieldbyname('PBBatchNo').AsString);
    Parameters.ParamByName('JGCNO').Value := Trim(Order_Main.fieldbyname('JCNO').AsString);
    ExecSQL;
  end;
end;

end.

