unit U_PBCGRKList_CP;

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_CP = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: 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;
    v1Column1: TcxGridDBColumn;
    Label34: TLabel;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    KHNameJC: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1Column17: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label2: TLabel;
    MYName: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label1: TLabel;
    Note: TEdit;
    labMYType: TLabel;
    Panel2: TPanel;
    SpeedButton1: TSpeedButton;
    Image2: TImage;
    v1MYType: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    CheckBox1: TCheckBox;
    Button2: TButton;
    v1MYMDPrice: TcxGridDBColumn;
    v1MYMDMoney: TcxGridDBColumn;
    v1JCDanWeiJC: TcxGridDBColumn;
    Label3: TLabel;
    JCDanWeiJC: TEdit;
    Button1: TButton;
    Button3: TButton;
    v1filler: TcxGridDBColumn;
    Label4: TLabel;
    filler: TEdit;
    KHNo: TEdit;
    JCKHNo: TEdit;
    v1RKNo: TcxGridDBColumn;
    Label5: TLabel;
    CGName: TEdit;
    v1Column4: 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 TBAddClick(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 N1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmPBCGRKList_CP: TfrmPBCGRKList_CP;

implementation
uses
  U_DataLink,U_RTFun, U_QCRKInPut,U_ZDYHelp,U_LLRKInPut,U_GYSList,U_BPZdy_LRPB,
  U_LLRKInPutMH,U_LLRKInPutX,U_LLRKInPut_CG,U_LLRKInPutMH_CG,U_LLRKInPut_CPZS;

{$R *.dfm}

procedure TfrmPBCGRKList_CP.FormDestroy(Sender: TObject);
begin
  frmPBCGRKList_CP:=nil;
end;

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

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

procedure TfrmPBCGRKList_CP.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('采购入库2',Tv1,'贸易生产管理');
  Close;
end;

procedure TfrmPBCGRKList_CP.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain 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(',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 ');
      if CheckBox1.Checked=False then
      begin
        sql.Add(' and A.CRTime>='''+Trim(FormatDateTime('yyyy-MM',now)+'-01')+'''');
        sql.Add(' and A.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',now+1))+'''');
        BegDate.Visible:=false;
        EndDate.Visible:=false;
      end
      else
      begin
        sql.Add(' and A.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and A.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
        BegDate.Visible:=true;
        EndDate.Visible:=true;
      end;
      sql.Add(' and isnull(CRType,'''')=''采购入库'' ');
      SQL.Add('and isnull(MYType,'''')=''采购成品''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmPBCGRKList_CP.InitForm();
begin
  BegDate.Date:=StrToDate(formatdatetime('yyyy-MM',SGetServerDate(ADOQueryTemp))+'-01');
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('采购入库2',Tv1,'贸易生产管理');
  InitGrid();
end;

procedure TfrmPBCGRKList_CP.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_CP.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(' delete YS_Money_CR where YFTypeId='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
          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_CP.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(Self.Caption,cxGrid1);
end;

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

procedure TfrmPBCGRKList_CP.TBAddClick(Sender: TObject);
begin
  SpeedButton1.Click;
end;

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

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

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

procedure TfrmPBCGRKList_CP.ToolButton1Click(Sender: TObject);
var
  FFMYType:String;
begin
  FFMYType:=Trim(Order_Main.fieldbyname('MYType').AsString);
  try
    frmLLRKInPutMH_CPZS:=TfrmLLRKInPutMH_CPZS.Create(Application);
    with frmLLRKInPutMH_CPZS do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MYId').AsString);
      FMYType:=Trim(Self.Order_Main.fieldbyname('MYType').AsString);
      FMYTypeFlag:=Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      TBSave.Visible:=False;
      BtnDel.Visible:=false;
      labMYType.Caption:='(成品)采购';
      FML:='成品';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_CPZS.Free;
  end;
end;

procedure TfrmPBCGRKList_CP.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_CP.N2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFTypeId='''+Trim(Self.Order_Main.fieldbyname('MYId').AsString)+'''');
    sql.Add(' and ChkStatusP=''已审核''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=false then
  begin
    Application.MessageBox('应付款已审核不能操作','提示');
    exit;
  end;
  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;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFTypeId='''+trim(Order_Main.fieldbyname('MYID').AsString)+'''');
    sql.Add(' and (isnull(ChkerP,'''')<>'''' ');
    sql.Add(' or isnull(ChkerQ,'''')<>'''') ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=false then
  begin
    Application.MessageBox('应付款已审核,不能删除','提示');
    exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin

  end;
end;

procedure TfrmPBCGRKList_CP.N1Click(Sender: TObject);
var
  FFMYType:String;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(canshu1)<>'高权限' then
  begin
    if Order_Main.FieldByName('filltime').AsDateTime+1<Now then
    begin
      application.MessageBox('超过一天不能修改','提示');
      Exit;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFTypeId='''+Trim(Self.Order_Main.fieldbyname('MYId').AsString)+'''');
    sql.Add(' and ChkStatusP=''已审核''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=false then
  begin
    Application.MessageBox('应付款已审核不能操作','提示');
    exit;
  end;
  FFMYType:=Trim(Order_Main.fieldbyname('MYType').AsString); //加限制财务ChkerP=''已审核''
  try
    frmLLRKInPutMH_CPZS:=TfrmLLRKInPutMH_CPZS.Create(Application);
    with frmLLRKInPutMH_CPZS do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MYId').AsString);
      FMYType:=Trim(Self.Order_Main.fieldbyname('MYType').AsString);
      FMYTypeFlag:=Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      labMYType.Caption:='(成品)采购';
      FML:='成品';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_CPZS.Free;
  end;
end;

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

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

procedure TfrmPBCGRKList_CP.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_CP.Tv1DblClick(Sender: TObject);
begin
  ToolButton1.Click;
end;

procedure TfrmPBCGRKList_CP.Image2Click(Sender: TObject);
begin
  Panel2.Visible:=False;
end;

procedure TfrmPBCGRKList_CP.SpeedButton1Click(Sender: TObject);
begin
  Panel2.Visible:=False;
  try
    frmLLRKInPutMH_CPZS:=TfrmLLRKInPutMH_CPZS.Create(Application);
    with frmLLRKInPutMH_CPZS do
    begin
      PState:=0;
      FMainId:='';
      FMYType:='采购成品';
      FMYTypeFlag:='CGCP';
      labMYType.Caption:='(成品)采购';
      FML:='成品';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_CPZS.Free;
  end;
end;

procedure TfrmPBCGRKList_CP.SpeedButton2Click(Sender: TObject);
begin
  Panel2.Visible:=False;
  try
    frmLLRKInPut_CG:=TfrmLLRKInPut_CG.Create(Application);
    with frmLLRKInPut_CG do
    begin
      PState:=0;
      FMainId:='';
      FMYType:='采购面料';
      FMYTypeFlag:='CGM';
      labMYType.Caption:='(面料)采购';
      FML:='面料';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CG.Free;
  end;
end;

procedure TfrmPBCGRKList_CP.SpeedButton4Click(Sender: TObject);
begin
  Panel2.Visible:=False;
  try
    frmLLRKInPutMH_CG:=TfrmLLRKInPutMH_CG.Create(Application);
    with frmLLRKInPutMH_CG do
    begin
      PState:=0;
      FMainId:='';
      FMYType:='采购棉';
      FMYTypeFlag:='CGH';
      labMYType.Caption:='(棉)采购';
      FML:='棉';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_CG.Free;
  end;
end;

procedure TfrmPBCGRKList_CP.SpeedButton5Click(Sender: TObject);
begin
  Panel2.Visible:=False;
  try
    frmLLRKInPutMH_CG:=TfrmLLRKInPutMH_CG.Create(Application);
    with frmLLRKInPutMH_CG do
    begin
      PState:=0;
      FMainId:='';
      FMYType:='采购无纺布';
      FMYTypeFlag:='CGW';
      labMYType.Caption:='(无纺布)采购';
      FML:='无纺布';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_CG.Free;
  end;
end;

procedure TfrmPBCGRKList_CP.SpeedButton3Click(Sender: TObject);
begin
  Panel2.Visible:=False;
  try
    frmLLRKInPut_CG:=TfrmLLRKInPut_CG.Create(Application);
    with frmLLRKInPut_CG do
    begin
      PState:=0;
      FMainId:='';
      FMYType:='采购线';
      FMYTypeFlag:='CGX';
      labMYType.Caption:='(线)采购';
      FML:='线';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CG.Free;
  end;
end;

procedure TfrmPBCGRKList_CP.SpeedButton6Click(Sender: TObject);
begin
  Panel2.Visible:=False;
  try
    frmLLRKInPut_CG:=TfrmLLRKInPut_CG.Create(Application);
    with frmLLRKInPut_CG do
    begin
      PState:=0;
      FMainId:='';
      FMYType:='采购成品';
      FMYTypeFlag:='CGC';
      labMYType.Caption:='采购(成品)';
      FML:='成品';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CG.Free;
  end;
end;

procedure TfrmPBCGRKList_CP.Button2Click(Sender: TObject);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text:='坯布厂';
      KHType.TxtCode:='坯布厂';
      if ShowModal=1 then
      begin
        Self.KHNameJC.Text:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
        TBFind.Click;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmPBCGRKList_CP.Button1Click(Sender: TObject);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text:='染厂';
      KHType.TxtCode:='染厂';
      if ShowModal=1 then
      begin
        Self.JCDanWeiJC.Text:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
        TBFind.Click;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

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

end.
