unit U_PBCGRKList_PBKC;

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;

type
  TfrmPBCGRKList_PBKC = 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;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    v1MXKCPiQty: TcxGridDBColumn;
    Label34: TLabel;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    KHName: TEdit;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1Column17: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label2: TLabel;
    MYName: TEdit;
    v1MXKCQty: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label1: TLabel;
    Note: TEdit;
    labMYType: TLabel;
    Button2: TButton;
    v1JCDanWeiJC: TcxGridDBColumn;
    Label3: TLabel;
    JCDanWei: TEdit;
    Button1: TButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    V2MYName: TcxGridDBColumn;
    V2MYCode: TcxGridDBColumn;
    V2PS: TcxGridDBColumn;
    V2Qty: TcxGridDBColumn;
    V2KgQty: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    PB_MX: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    cxSplitter1: TcxSplitter;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    Button3: TButton;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    MYCode: TEdit;
    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 ToolButton1Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure KHNameChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure MYCodeChange(Sender: TObject);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmPBCGRKList_PBKC: TfrmPBCGRKList_PBKC;

implementation
uses
  U_DataLink,U_RTFun, U_QCRKInPut,U_ZDYHelp,U_LLRKInPut,U_GYSList,U_RCCKInPut,
  U_LLRKInPutMH,U_LLRKInPutX,U_LLRKInPut_CG,U_LLRKInPutMH_CG,U_LLRKInPutMH_PBCG,
  U_LLRKInPutMH_PBKCCG,U_BPZdy_LRPB;

{$R *.dfm}

procedure TfrmPBCGRKList_PBKC.FormDestroy(Sender: TObject);
begin
  frmPBCGRKList_PBKC:=nil;
end;

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

procedure TfrmPBCGRKList_PBKC.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  canshu1:=Trim(DParameters1);
end;

procedure TfrmPBCGRKList_PBKC.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('坯布库存1',Tv2,'贸易生产管理');
  WriteCxGrid('坯布库存明细',Tv1,'贸易生产管理');
  Close;
end;

procedure TfrmPBCGRKList_PBKC.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select AA.* from');
      SQL.Add(' (select A.MYCode,A.MYName,Sum(MXKCPiQty) PS,Sum(MXKCKgQty) KgQty,Sum(MXKCQty) Qty');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' where isnull(CRType,'''')=''采购入库''');
      SQL.Add(' and isnull(MYType,'''')=''采购坯布''');
      sql.Add(' group by A.MYCode,A.MYName) AA ');
      sql.Add(' where AA.PS>0');
      //ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmPBCGRKList_PBKC.InitForm();
begin
  ReadCxGrid('坯布库存1',Tv2,'贸易生产管理');
  ReadCxGrid('坯布库存明细',Tv1,'贸易生产管理');
  InitGrid();
end;

procedure TfrmPBCGRKList_PBKC.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;

function TfrmPBCGRKList_PBKC.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      //First;
     // while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' insert into CK_MYSC_CR_Log  ');
          sql.Add(' select * from CK_MYSC_CR where MYID='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
          sql.Add(' UPdate CK_MYSC_CR_Log Set DelTime=getdate(),DelerCode='''+Trim(DCode)+''',Deler='''+Trim(DName)+'''');
          sql.Add(' where MYID='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
          sql.Add(' Delete CK_MYSC_CR  where MYId='''+Trim(Order_Main.fieldbyname('MYId').AsString)+'''');
          sql.Add(' delete from TP_File where WBID='''+trim(Order_Main.fieldbyname('MYId').AsString)+''' ');
          sql.Add(' and TFType=''采购坯布''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmPBCGRKList_PBKC.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(Self.Caption,cxGrid1);
end;

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

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

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

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

procedure TfrmPBCGRKList_PBKC.ToolButton1Click(Sender: TObject);
var
  FFMYType:String;
begin
  FFMYType:=Trim(PB_MX.fieldbyname('MYType').AsString);
  if Trim(FFMYType)='采购坯布' then
  begin
    try
      frmLLRKInPutMH_PBKCCG:=TfrmLLRKInPutMH_PBKCCG.Create(Application);
      with frmLLRKInPutMH_PBKCCG do
      begin
        PState:=1;
        FMainId:=Trim(Self.PB_MX.fieldbyname('MYId').AsString);
        FMYType:=Trim(Self.PB_MX.fieldbyname('MYType').AsString);
        FMYTypeFlag:=Trim(Self.PB_MX.fieldbyname('MYTypeFlag').AsString);
        if ShowModal=1 then
        begin
          InitGrid();
        end;
      end;
    finally
      frmLLRKInPutMH_PBKCCG.Free;
    end;
  end;
end;

procedure TfrmPBCGRKList_PBKC.CustomerNoNameChange(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 TfrmPBCGRKList_PBKC.N2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_MYSC_CR_MX where MYID='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已产生明细数据，不能删除!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    //Order_Main.Delete;
  end;
end;

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

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

procedure TfrmPBCGRKList_PBKC.ToolButton4Click(Sender: TObject);
var
  fPrintFile:string;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('Chker').AsString)='' then exit;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\检测报告.rmf';
  RMXLSExport2:= TRMXLSExport.Create(RMXLSExport2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Test_Report where TRId='''+Trim(Order_Main.fieldbyname('TRId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['OrderUnit']:=Order_Main.fieldbyname('OrderUnit').Value;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end  else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检测报告.rmf'),'提示',0);
  end;
end;

procedure TfrmPBCGRKList_PBKC.Button2Click(Sender: TObject);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text:='坯布厂';
      KHType.TxtCode:='坯布厂';
      if ShowModal=1 then
      begin
        Self.KHName.Text:=Trim(frmGYSList.Order_Main.fieldbyname('KHName').AsString);
        SDofilter(ADOQueryCmd,SGetFilters(Panel1,3,4));
        SCreateCDS20(ADOQueryCmd,PB_MX);
        SInitCDSData20(ADOQueryCmd,PB_MX);
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmPBCGRKList_PBKC.Button1Click(Sender: TObject);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text:='染厂';
      KHType.TxtCode:='染厂';
      if ShowModal=1 then
      begin
        Self.JCDanWei.Text:=Trim(frmGYSList.Order_Main.fieldbyname('KHName').AsString);
        SDofilter(ADOQueryCmd,SGetFilters(Panel1,3,4));
        SCreateCDS20(ADOQueryCmd,PB_MX);
        SInitCDSData20(ADOQueryCmd,PB_MX);
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmPBCGRKList_PBKC.Tv1DblClick(Sender: TObject);
begin
  ToolButton1.Click;
end;

procedure TfrmPBCGRKList_PBKC.KHNameChange(Sender: TObject);
begin
  SDofilter(ADOQueryCmd,SGetFilters(Panel1,3,4));
  SCreateCDS20(ADOQueryCmd,PB_MX);
  SInitCDSData20(ADOQueryCmd,PB_MX);
end;

procedure TfrmPBCGRKList_PBKC.N1Click(Sender: TObject);
begin
  frmRCCKInPut:=TfrmRCCKInPut.Create(self);
  with frmRCCKInPut do
  begin
    if ShowModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmPBCGRKList_PBKC.Button3Click(Sender: TObject);
begin
  frmBPZDY_LRPB:=TfrmBPZDY_LRPB.create(self);
  with frmBPZDY_LRPB do
  begin
    flag:='PBName';
    flagName:='坯布';
    ViewFlag:=true;
    Panel4.Align:=alClient;
    ToolBar3.Visible:=false;
    Button1.Visible:=False;
    if ShowModal=1 then
    begin
      MYName.text:=Trim(ClientDataSet1.fieldbyname('BPName').AsString);
    end;
    free;
  end;
end;

procedure TfrmPBCGRKList_PBKC.MYCodeChange(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 TfrmPBCGRKList_PBKC.TV2FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  try
    ADOQueryCmd.DisableControls;
    with ADOQueryCmd do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      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(' from CK_MYSC_CR A  ');
      sql.Add(' where A.MYCode='''+trim(Order_Main.fieldbyname('MYCode').AsString)+''' ');
      sql.Add(' and A.MYName='''+trim(Order_Main.fieldbyname('MYName').AsString)+''' ');
      sql.Add(' and isnull(CRType,'''')=''采购入库'' ');
      SQL.Add(' and isnull(MYType,'''')=''采购坯布''');
      sql.Add(' and isnull(A.MXKCPiQty,0)>0');
      Open;
    end;
    SCreateCDS20(ADOQueryCmd,PB_MX);
    SInitCDSData20(ADOQueryCmd,PB_MX);
    if Order_Main.fieldbyname('KgQty').AsFloat=0 then
    begin
      v1Column17.Visible:=false;
    end
    else
    begin
      v1Column17.Visible:=true;
    end;
    if Order_Main.fieldbyname('Qty').AsFloat=0 then
    begin
      v1MXKCQty.Visible:=false;
    end
    else
    begin
      v1MXKCQty.Visible:=true;
    end;
  finally
    ADOQueryCmd.EnableControls;
  end;
end;

end.
