unit U_YFCRMXList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxPC, cxDropDownEdit, Menus, cxLookAndFeels, cxLookAndFeelPainters,
  dxBarBuiltInMenu, cxNavigator, DateUtils, dxSkinsCore, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter;

type
  TfrmYFCRMXList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    MXOrderNo: TEdit;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    CDS_YFS: TClientDataSet;
    DS_YFS: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label4: TLabel;
    FACTTYPE: TComboBox;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label5: TLabel;
    FactoryName: TEdit;
    v1Column6: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    btn1: TToolButton;
    lbl1: TLabel;
    CPCode: TEdit;
    lbl2: TLabel;
    CPName: TEdit;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Tv1Column4: TcxGridDBColumn;
    ComboBox1: TComboBox;
    Label1: TLabel;
    Qty: TEdit;
    Label2: TLabel;
    qtyunit: TComboBox;
    Label7: TLabel;
    KXType: TComboBox;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure MXOrderNoChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure FACTTYPEChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure chk1Click(Sender: TObject);
    procedure FACTTYPEDropDown(Sender: TObject);
    procedure Tv1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
    procedure InitKXType();
  public

    { Public declarations }
    FCYID: string;
    fmanage: string;
  end;

var
  frmYFCRMXList: TfrmYFCRMXList;

implementation

uses
  U_DataLink, U_Fun, U_SXCKNewList, U_YSSCList, U_YsScXsList, U_YSKInput,
  U_XXFPHXList, U_YFCRMXchildList, U_BPRKList;

{$R *.dfm}
procedure TfrmYFCRMXList.SetStatus();
begin
  if cxTabControl1.TabIndex = 0 then
  begin
    Tv1Column1.Options.Editing := true;
  end
  else
  begin
    Tv1Column1.Options.Editing := False;
  end;
end;

procedure TfrmYFCRMXList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;

      SQL.Add('SELECT  CRTime , FactoryName,CFFactoryName,status   ');
      sql.Add(',KXType,MXOrderNo,CPCode,CPName,CPSpec,qtyunit,FACTtYPE,MDTIME  ');
      sql.Add(',sum(PS) PS,sum(Qty) Qty,sum(MxMoney) MxMoney,price= case when sum(Qty)>0 then sum(MxMoney)/sum(Qty) else 0 end ');
      sql.Add('FROM ( select  A.CRTime , A.FactoryName,B.CFFactoryName ');
      sql.Add(',status=(case when A.status=''1'' then ''已确认'' else ''未确认'' end )');
      sql.Add(',B.KXType,B.MXOrderNo,B.CPCode,B.CPName,B.CPSpec,B.qtyunit  ');
      sql.Add(',B.PS , Qty, MxMoney');
      sql.Add(',FACTtYPE=(SELECT TOP 1 FACTTYPE FROM Company  C WHERE A.FactoryName=C.CoName and valid=''Y'') ');
      SQL.Add(',MDTIME=(CASE WHEN KXTYPE=''收货'' then convert(varchar(10),(SELECT TOP 1 FILLTIME FROM XH_Cloth_DH z WHERE z.DHID=B.YFPZNO),120 )   ');
      sql.Add(' else convert(varchar(10),(SELECT TOP 1 FILLTIME FROM CK_SXPB_CR T WHERE T.SPID=B.YFPZNO),120)END)');
      sql.Add(' from YF_Money_CR A');
      sql.Add(' inner join  YF_Money_CR_SUB B on A.yfid=B.yfid ');
      SQL.Add('WHERE 1=1 ');
      sql.Add(' and A.CRType=''应付款登记''  ');

      if cxTabControl1.TabIndex < 2 then
        sql.Add(' and A.status=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
      sql.Add(')aa ');

      if ComboBox1.Text = '码单日期' then
      begin
        sql.Add(' where MDTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
        sql.Add(' and MDTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      end
      else
      begin
        sql.Add(' where crTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
        sql.Add(' and crTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      end;
      sql.Add(' group by status,CRTime , FactoryName ');
      sql.Add(',KXType,MXOrderNo,CFFactoryName,CPCode,CPName,CPSpec,qtyunit,FACTtYPE,MDTIME  ');

      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_hz);
    SInitCDSData20(ADOQueryMain, CDS_hz);
    InitKXType();
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmYFCRMXList.FormDestroy(Sender: TObject);
begin
  frmYFCRMXList := nil;
end;

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

procedure TfrmYFCRMXList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '财务管理');

  Close;
end;

procedure TfrmYFCRMXList.FormShow(Sender: TObject);
var
  fsj: string;
begin

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '财务管理');

  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := StartOfTheMonth(StartOfTheMonth(Enddate.DateTime) - 1);

//  fsj := 'select distinct(YFGSType) name from YF_Money_CR where CRType=''应付款登记'' ';
//  SInitComBoxBySql(ADOQueryCmd, YFGSType, False, fsj);

  SetStatus();
  InitGrid();
end;

procedure TfrmYFCRMXList.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

procedure TfrmYFCRMXList.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_hz);
    SInitCDSData20(ADOQueryMain, CDS_hz);
  end;
end;

procedure TfrmYFCRMXList.MXOrderNoChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYFCRMXList.Button2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if Trim(CDS_HZ.fieldbyname('SubID').AsString) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete YCL_SG_HZ where SubID=''' + Trim(CDS_HZ.fieldbyname('SubID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmYFCRMXList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '应付款列表');
end;

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

procedure TfrmYFCRMXList.N1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := TRUE;
      post;
      next;
    end;
    First;
    EnableControls;
  end;

end;

procedure TfrmYFCRMXList.N2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := FALSE;
      post;
      next;
    end;
    First;
    EnableControls;
  end;

end;

procedure TfrmYFCRMXList.FACTTYPEChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYFCRMXList.Tv1DblClick(Sender: TObject);
begin

  frmYFCRMXchildList := TfrmYFCRMXchildList.create(self);
  with frmYFCRMXchildList do
  begin
    Enddate.DateTime := self.Enddate.DateTime;
    begdate.DateTime := self.begdate.DateTime;
    cxTabControl1.TabIndex := self.cxTabControl1.TabIndex;
    FCRTime := TRIM(self.CDS_hz.Fieldbyname('CRTime').AsString);
    FFactoryName := TRIM(self.CDS_hz.Fieldbyname('FactoryName').AsString);
    FKXType := TRIM(self.CDS_hz.Fieldbyname('KXType').AsString);
    FMXOrderNo := TRIM(self.CDS_hz.Fieldbyname('MXOrderNo').AsString);
    FCPCode := TRIM(self.CDS_hz.Fieldbyname('CPCode').AsString);
    FCPName := TRIM(self.CDS_hz.Fieldbyname('CPName').AsString);
    FCPSpec := TRIM(self.CDS_hz.Fieldbyname('CPSpec').AsString);
    Fqtyunit := TRIM(self.CDS_hz.Fieldbyname('qtyunit').AsString);

    if showmodal = 1 then
    begin

    end;
    free;
  end;

//  frmXXFPHXList := TfrmXXFPHXList.create(self);
//  with frmXXFPHXList do
//  begin
//    FTONO := TRIM(CDS_YFS.Fieldbyname('YFSID').AsString);
//    FFromNo := '';
//    if showmodal = 1 then
//    begin
//
//    end;
//    free;
//  end;
end;

procedure TfrmYFCRMXList.chk1Click(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

procedure TfrmYFCRMXList.FACTTYPEDropDown(Sender: TObject);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select distinct facttype from Company where CoType=''供应商''  ');
    Open;
  end;
  FACTTYPE.Items.Clear;
  while not ADOQueryTemp.eof do
  begin
    FACTTYPE.Items.Add(ADOQueryTemp.fieldbyname('facttype').AsString);
    ADOQueryTemp.next;
  end;

end;

procedure TfrmYFCRMXList.Tv1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  if cxTabControl1.TabIndex = 0 then
  begin
    mvalue := TcxTextEdit(Sender).EditingText;
    FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := mvalue;
      Post;

    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
//    sql.Add('update YF_MONEY_CR_sub set  ' + FFieldName + '=' + (Trim(mvalue)));
//    sql.Add('where YFSID=' + quotedstr(Trim(CDS_HZ.fieldbyname('YFSID').AsString)));

      sql.Add('exec P_YFFK_UP_price ');
      sql.Add('@status=' + quotedstr(Trim(IntToStr(cxTabControl1.TabIndex))));
      sql.Add(',@CRTime=' + quotedstr(Trim(CDS_HZ.fieldbyname('CRTime').AsString)));
      sql.Add(',@FactoryName=' + quotedstr(Trim(CDS_HZ.fieldbyname('FactoryName').AsString)));
      sql.Add(',@kxtype=' + quotedstr(Trim(CDS_HZ.fieldbyname('kxtype').AsString)));
      sql.Add(',@MXOrderNo=' + quotedstr(Trim(CDS_HZ.fieldbyname('MXOrderNo').AsString)));
      sql.Add(',@CFFactoryName=' + quotedstr(Trim(CDS_HZ.fieldbyname('CFFactoryName').AsString)));
      sql.Add(',@CPCode=' + quotedstr(Trim(CDS_HZ.fieldbyname('CPCode').AsString)));
      sql.Add(',@CPName=' + quotedstr(Trim(CDS_HZ.fieldbyname('CPName').AsString)));
      sql.Add(',@CPSpec=' + quotedstr(Trim(CDS_HZ.fieldbyname('CPSpec').AsString)));
      sql.Add(',@qtyunit=' + quotedstr(Trim(CDS_HZ.fieldbyname('qtyunit').AsString)));
      sql.Add(',@price=' + (Trim(mvalue)));
//     ShowMessage(sql.Text);
      ExecSQL;
    end;

    with CDS_HZ do
    begin
      Edit;
      FieldByName('MxMoney').Value := RoundFloat((FieldByName('price').AsFloat * FieldByName('qty').AsFloat), 2);
      Post;
    end;
    tv1.Controller.EditingController.ShowEdit();
  end;

end;

procedure TfrmYFCRMXList.btn1Click(Sender: TObject);
begin
  try
    frmBPRKList := TfrmBPRKList.create(self);
    with frmBPRKList do
    begin
      with frmBPRKList do
      begin
        if showmodal = 1 then
        begin

        end;
      end;
    end;
  finally
    frmBPRKList.free;
  end;
//  try
//    ADOQueryMain.DisableControls;
//    with ADOQueryMain do
//    begin
//      Filtered := False;
//      Close;
//      sql.Clear;
//      sql.Add(' EXEC P_Select_YSSC  ');
//      sql.add(' @Begdate=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
//      sql.Add(' ,@enddate=''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
//      sql.Add(' ,@CWFlag=0');
//      Open;
//    end;
//
//  finally
//    ADOQueryMain.EnableControls;
//  end;
end;

procedure TfrmYFCRMXList.ComboBox1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmYFCRMXList.InitKXType();
begin
   with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT DISTINCT KXType FROM (');
    sql.Add('  select B.KXType');
    sql.Add('  from YF_Money_CR A');
    sql.Add('  inner join YF_Money_CR_SUB B on A.yfid=B.yfid');
    sql.Add('  WHERE 1=1');
    sql.Add('  and A.CRType=''应付款登记''');

    if cxTabControl1.TabIndex < 2 then
      sql.Add('  and A.status=''' + inttostr(cxTabControl1.TabIndex) + '''');

    if ComboBox1.Text = '码单日期' then
    begin
      sql.Add('  and (CASE WHEN B.KXTYPE=''收货'' then convert(varchar(10),(SELECT TOP 1 FILLTIME FROM XH_Cloth_DH z WHERE z.DHID=B.YFPZNO),120)');
      sql.Add('   else convert(varchar(10),(SELECT TOP 1 FILLTIME FROM CK_SXPB_CR T WHERE T.SPID=B.YFPZNO),120)END)');
      sql.Add('      >=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + '''');
      sql.Add('  and (CASE WHEN B.KXTYPE=''收货'' then convert(varchar(10),(SELECT TOP 1 FILLTIME FROM XH_Cloth_DH z WHERE z.DHID=B.YFPZNO),120)');
      sql.Add('   else convert(varchar(10),(SELECT TOP 1 FILLTIME FROM CK_SXPB_CR T WHERE T.SPID=B.YFPZNO),120)END)');
      sql.Add('      <''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + '''');
    end
    else
    begin
      sql.Add('  and A.crTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + '''');
      sql.Add('  and A.crTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + '''');
    end;

    sql.Add(') t WHERE KXType IS NOT NULL ORDER BY KXType');
    Open;
  end;

  KXType.Items.Clear;
  while not ADOQueryTemp.eof do
  begin
    KXType.Items.Add(ADOQueryTemp.fieldbyname('KXType').AsString);
    ADOQueryTemp.next;
  end;
end;

end.

