unit U_CPFHList;

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, cxTextEdit;

type
  TfrmCPFHList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    Label34: TLabel;
    FactoryName: TEdit;
    Label4: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    labMYType: TLabel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    Label9: TLabel;
    YWY: TEdit;
    v1Column23: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    Panel3: TPanel;
    Image1: TImage;
    SpeedButton8: TSpeedButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    v1Ssel: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    v1Column2: 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 TBDelClick(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 TBEditClick(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image1Click(Sender: TObject);
    procedure SpeedButton8Click(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitGridKH();
    procedure InitGridSub();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmCPFHList: TfrmCPFHList;

implementation
uses
  U_DataLink,U_RTFun, U_XiShouInPut,U_ZDYHelp,U_XiShouInPutWKC,U_XiaoShouDataOldListQB,U_CKProductBCPOutList,
  U_XiaoShouDataNewListQB;

{$R *.dfm}

procedure TfrmCPFHList.FormDestroy(Sender: TObject);
begin
  frmCPFHList:=nil;
end;

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

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

procedure TfrmCPFHList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('销售码单登记YXMX',Tv1,'贸易生产管理');
  WriteCxGrid('销售码单登记YXZY',Tv2,'贸易生产管理');
  Close;
end;

procedure TfrmCPFHList.InitGrid();
begin
  try
    ADOQueryCmd.DisableControls;
    with ADOQueryCmd do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.* ');
      sql.Add(',KHNameJC=(select KHNameJC from KH_Main KM where KM.KHNO=A.KHNO)');
      sql.Add(',JQty=(select Count(*) from FHSQ_Sub FS where FS.SMID=A.SMID)');
      sql.Add(',HSQtyM=(select sum(FHMQty) from FHSQ_Sub FS where FS.SMID=A.SMID)');
      sql.Add(',HSQtyKg=(select sum(FHKgQty) from FHSQ_Sub FS where FS.SMID=A.SMID)');
      sql.Add(',BZType=(select Top 1 BZType from FHSQ_Sub FS where FS.SMID=A.SMID)');
      sql.Add(',HZFee=(select sum(FeeMoney) from FHSQ_Sub FS where FS.SMID=A.SMID)');
      sql.Add(',Money=isnull((select sum(FeeMoney) from FHSQ_Sub FS where FS.SMID=A.SMID),0)');
      sql.Add('+isnull((select sum(FHMooney) from FHSQ_Sub FS where FS.SMID=A.SMID),0)');
      sql.Add('from FHSQ_Main A');
      Open;
    end;
    SCreateCDS20(ADOQueryCmd,Order_Main);
    SInitCDSData20(ADOQueryCmd,Order_Main);
  finally
    ADOQueryCmd.EnableControls;
  end;
end;

procedure TfrmCPFHList.InitGridSub();
begin
  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select AA.*,B.* ');
      sql.Add(',KHNameJC=(select KHNameJC from KH_Main KM where KM.KHNO=B.FactoryNo)');
      sql.Add(' from(');
      sql.Add(' select A.SMID,BZType,JQty=sum(JQty),HSQty=sum(HSQty),HZFee=sum(HZFee),Money=sum(Money)');
      sql.Add(' from CK_YCL_CR A ');
      sql.Add(' inner join CK_YCL_CR_Main B on A.SMID=B.SMID ');
      sql.Add(' where 1=1');
      if CheckBox1.Checked=true then
      begin
        sql.add(' and B.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and B.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+'''');
      end
      else
      begin
        sql.add(' and B.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',Now))+'''');
        sql.Add(' and B.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',Now+1))+'''');
      end;
      sql.Add(' and isnull(B.CKName,'''')=''销售出库'' ');
      sql.Add(' and isnull(B.FactoryNo,'''')='''+Trim(ClientDataSet2.fieldbyname('FactoryNo').AsString)+'''');
      sql.Add(' and isnull(B.CRType,'''')='''+Trim(ClientDataSet2.fieldbyname('CRType').AsString)+'''');
      sql.Add(' and isnull(B.YWY,'''')='''+Trim(ClientDataSet2.fieldbyname('YWY').AsString)+'''');
      if ClientDataSet2.IsEmpty then
      begin
        sql.Add(' and 1=2');
      end;
      sql.Add(' Group by A.SMID,BZType )AA');
      sql.Add(' inner join CK_YCL_CR_Main B on AA.SMID=B.SMID ');
      SQL.Add(' order by B.CRTime desc');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,Order_Main);
    SInitCDSData20(ADOQueryTemp,Order_Main);
  finally
    ADOQueryTemp.EnableControls;
  end;
end;

procedure TfrmCPFHList.InitGridKH();
begin
 
end;


procedure TfrmCPFHList.InitForm();
begin
  BegDate.Date:=StrToDate(FormatDateTime('yyyy-MM',SGetServerDate(ADOQueryTemp))+'-01');
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('销售码单登记YXMX',Tv1,'贸易生产管理');
  ReadCxGrid('销售码单登记YXZY',Tv2,'贸易生产管理');
  InitGridKH();
end;

procedure TfrmCPFHList.TBFindClick(Sender: TObject);
begin
  if ADOQueryCmd.Active=False then Exit;
  SDofilter(ADOQueryCmd,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryCmd,Order_Main);
  SInitCDSData20(ADOQueryCmd,Order_Main);
end;

procedure TfrmCPFHList.TBDelClick(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 in');
    sql.Add('(select SPID from CK_YCL_CR where SMID='''+Trim(Order_Main.fieldbyname('SMID').AsString)+''')');
    sql.Add(' and (isnull(ChkerP,'''')<>'''' ');
    sql.Add(' or isnull(ChkerQ,'''')<>'''') ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('单价或数量已审核不能删除!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' delete YS_Money_CR where YFTypeId in');
      sql.Add('(select SPID from CK_YCL_CR where SMID='''+Trim(Order_Main.fieldbyname('SMID').AsString)+''')');
      sql.Add(' delete CK_YCL_CR where SMID='''+Trim(Order_Main.fieldbyname('SMID').AsString)+'''');
      sql.Add(' delete CK_YCL_CR_Main where SMID='''+Trim(Order_Main.fieldbyname('SMID').AsString)+'''');

      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除异常!','提示',0);
  end;

end;

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

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

procedure TfrmCPFHList.TBAddClick(Sender: TObject);
begin
  try
    frmXiaoShouDataNewListQB:=TfrmXiaoShouDataNewListQB.Create(Application);
    with frmXiaoShouDataNewListQB do
    begin
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmXiaoShouDataNewListQB.Free;
  end;
end;

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

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

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

procedure TfrmCPFHList.ToolButton1Click(Sender: TObject);
var
  FFMYType:String;
begin
  if Order_Main.IsEmpty then Exit;
  FFMYType:=Trim(Order_Main.fieldbyname('CRType').AsString);
  if Trim(FFMYType)='老系统销售' then
  begin
    try
      frmXiShouInPut:=TfrmXiShouInPut.Create(Application);
      with frmXiShouInPut do
      begin
        TBSave.Visible:=False;
        PState:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('SMID').AsString);
        Label21.Caption:=FFMYType;
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmXiShouInPut.Free;
    end;
  end else
  begin
    try
      frmXiShouInPutWKC:=TfrmXiShouInPutWKC.Create(Application);
      with frmXiShouInPutWKC do
      begin
        TBSave.Visible:=False;
        PState:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('SMID').AsString);
        Label21.Caption:=FFMYType;
        if ShowModal=1 then
        begin
       
        end;
      end;
    finally
      frmXiShouInPutWKC.Free;
    end;
  end;

end;


procedure TfrmCPFHList.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,ClientDataSet2);
  SInitCDSData20(ADOQueryMain,ClientDataSet2);
end;

procedure TfrmCPFHList.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,True);
end;

procedure TfrmCPFHList.N1Click(Sender: TObject);
begin
  speedbutton8.click;
end;

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

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

procedure TfrmCPFHList.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 TfrmCPFHList.Tv1DblClick(Sender: TObject);
begin
  frmCKProductBCPOutList:=TfrmCKProductBCPOutList.Create(self);
  with frmCKProductBCPOutList do
  begin
    FCKNO:=trim(Order_Main.fieldbyname('CKNo').AsString);
    if ShowModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmCPFHList.TBEditClick(Sender: TObject);
var
  FFMYType:String;
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFTypeId in');
    sql.Add('(select SPID from CK_YCL_CR where SMID='''+Trim(Order_Main.fieldbyname('SMID').AsString)+''')');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已经生成应收款不能修改!','提示',0);
    Exit;
  end;
  FFMYType:=Trim(Order_Main.fieldbyname('CRType').AsString);
  if Trim(FFMYType)='老系统销售' then
  begin
    try
      frmXiShouInPut:=TfrmXiShouInPut.Create(Application);
      with frmXiShouInPut do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('SMID').AsString);
        Label21.Caption:=FFMYType;
        Button1.Visible:=False;
        if ShowModal=1 then
        begin
          InitGridSub();
        end;
      end;
    finally
      frmXiShouInPut.Free;
    end;
  end else
  begin
    try
      frmXiShouInPutWKC:=TfrmXiShouInPutWKC.Create(Application);
      with frmXiShouInPutWKC do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('SMID').AsString);
        Label21.Caption:=FFMYType;
        if ShowModal=1 then
        begin
          InitGridSub();
        end;
      end;
    finally
      frmXiShouInPutWKC.Free;
    end;
  end;

end;


procedure TfrmCPFHList.Tv2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitGridSub();
end;

procedure TfrmCPFHList.Image1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  Panel3.Visible:=false;
end;

procedure TfrmCPFHList.SpeedButton8Click(Sender: TObject);
var
  fPrintFile:string;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('ssel',True,[])=false then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
    ExecSQL;
  end;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\销售码单新.rmf';
  RMXLSExport2:= TRMXLSExport.Create(RMXLSExport2);
  with Order_Main do
  begin
    first;
    while not Eof do
    begin
      if FieldByName('Ssel').AsBoolean=true then
      begin
        with ADOQuery1 do
        begin
          Filtered:=False;
          Close;
          sql.Clear;
          sql.Add(' select distinct isnull(D.MJID,C.MJID) MJID');
          sql.Add(' from CK_YCL_CR A ');
          sql.Add(' inner join CK_YCL_CR_Main B on A.SMID=B.SMID ');
          sql.Add(' left join CP_InOut1 C on C.InOutNo=B.CKNo');
          sql.Add(' left join CK_BanCP_CR D on D.CKOrdNo=B.CKNo');
          sql.add(' where B.CKNO='''+Trim(Order_Main.fieldbyname('CKNO').AsString)+'''');
          Open;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('SELECT * FROM TBSubID where 1=2 ');
          open;
        end;
        with ADOQuery1 do
        begin
          first;
          while not eof do
          begin
            ADOQueryTemp.append;
            ADOQueryTemp.fieldbyname('SubId').Value:=Trim(ADOQuery1.fieldbyname('MJID').AsString);
            ADOQueryTemp.fieldbyname('Dname').Value:=Trim(DCode);
            ADOQueryTemp.post;
            next;
          end;
        end;
      end;
      next;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMD3 ');
    sql.add('@mainID='+quotedstr(Trim('')));
    sql.add(',@DName='+quotedstr(Trim(DCode)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  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.


