unit U_YSCR;

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,
  cxNavigator, dxBarBuiltInMenu;

type
  TfrmYSCR = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    YFName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    FactoryName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    TBADd: TToolButton;
    VHL: TcxGridDBColumn;
    VBZ: TcxGridDBColumn;
    VYB: TcxGridDBColumn;
    VBB: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    cxTabControl1: TcxTabControl;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    v1note: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    CDS_YFS: TClientDataSet;
    DS_YFS: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    Label5: TLabel;
    ranchang: TEdit;
    Label7: TLabel;
    FtySYRName: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Tv2Column4: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Panel2: TPanel;
    ComboBox1: TComboBox;
    CDS_LABEL: TClientDataSet;
    ADOQueryPrint: TADOQuery;
    CDS_PRINT: TClientDataSet;
    Label4: TLabel;
    Panel3: TPanel;
    ToolButton5: TToolButton;
    ComboBox2: TComboBox;
    Label8: TLabel;
    Button1: TButton;
    Button2: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TBADdClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure YFGSTypeChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ComboBox2DropDown(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure SetStatus();
    procedure PrintMD(MStatus: string);
  public

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

var
  frmYSCR: TfrmYSCR;

implementation

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

{$R *.dfm}

procedure TfrmYSCR.PrintMD(MStatus: string);
var
  fImagePath2, fPrintFile, fPrintFile10, FMainID, FKHName, FFPNo: string;
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Trim(CDS_HZ.fieldbyname('FPNO').AsString) = '' then
  begin
    Application.MessageBox('发票为空不能打印', '提示', 0);
    Exit;
  end;

  FMainID := '';

  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);

  if CDS_Label.Locate('LMName', trim(ComboBox1.Text), []) then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec ' + trim(CDS_Label.fieldbyname('LMSql').AsString) + ' @FPNO=' + quotedstr(Trim(CDS_HZ.fieldbyname('FPNO').AsString)));
//      sql.add('exec ' + trim(CDS_Label.fieldbyname('LMSql').AsString) + ' @CKOrdNo=' + quotedstr(Trim(CDS_Main.fieldbyname('CKOrdNo').AsString)));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryPrint, CDS_PRINT);
    SInitCDSData20(ADOQueryPrint, CDS_PRINT);
  end;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin

    RM1.LoadFromFile(fPrintFile);
    RMVariables['ZDR'] := trim(DName);
    RM1.ShowReport;

  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmYSCR.SetStatus();
var
  i: integer;
begin

  Tchk.Visible := false;
  TNochk.Visible := false;
  TBDel.Visible := false;
  case cxTabControl1.TabIndex of
    0:
      begin
        Tchk.Visible := true;
        TBDel.Visible := true;
      end;
    1:
      begin
        TNochk.Visible := true;
      end;
  end;

//  IF fmanage='高权限' then
//  begin
//
//
//  end
//  else
//  begin
//
//  end;
end;

procedure TfrmYSCR.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* ');
      SQL.Add(',CHUANDATE=(SELECT TOP 1 chuandate FROM JYOrder_BaoGuan_Main B  WHERE A.DCNO=B.DCNO )');
      SQL.Add(',FPNO= cast((select DISTINCT B.a4fpno+'';'' from JYOrder_BaoGuan_Main B where A.DCNO=B.DCNO AND VALID=''Y'' for xml path('''')) as varchar(200))');


      SQL.Add(',SHFS=(SELECT TOP 1 SHFS FROM JYOrder_BaoGuan_Main B  WHERE A.DCNO=B.DCNO )');
      SQL.Add('from YF_Money_CR A');
      sql.Add(' where A.CRTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
      sql.Add(' and CRTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      sql.Add(' and A.CRType=''应收款登记''  ');
      if cxTabControl1.TabIndex < 2 then
        sql.Add(' and A.status=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);

    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select count(*)gs FROM FHSQ_MAIN A WHERE  A.FHPSHZ>0 AND Status=''未完成'' and SQDate>''2022-08-16'' and xstype=''外销''  ');
      Open;
    end;
    Label4.Caption := '已发货未生成条数为：' + Trim(ADOQueryTemp.fieldbyname('gs').AsString);

  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmYSCR.FormDestroy(Sender: TObject);
begin
  frmYSCR := nil;
end;

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

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

procedure TfrmYSCR.TBDelClick(Sender: TObject);
var
  FBBMoney, CRID: string;
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('delete YF_Money_CR where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
            sql.Add('delete YF_Money_CR_Sub where YFID=''' + Trim(CDS_HZ.fieldbyname('YFID').AsString) + '''');
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYSCR.FormShow(Sender: TObject);
var
  fsj: string;
begin
  fsj := 'select distinct(LMName) name from Lbael_Map where LMType=''单证''  ';
  SInitComBoxBySql(ADOQueryCmd, ComboBox1, true, fsj);

  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    Sql.Add('select * from Lbael_Map where LMType=''单证''');

    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Label);
  SInitCDSData20(ADOQueryTemp, CDS_Label);
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '财务管理');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '财务管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime;
//  fsj := 'select distinct(YFGSType) name from YF_Money_CR where CRType=''应收款登记'' ';
//  SInitComBoxBySql(ADOQueryCmd, YFGSType, False, fsj);
  SetStatus();
  InitGrid();
end;

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

procedure TfrmYSCR.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 TfrmYSCR.FactoryNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYSCR.Button2Click(Sender: TObject);
begin
  Panel3.Visible := False;
end;

procedure TfrmYSCR.TBADdClick(Sender: TObject);
begin
  try
    frmYSKInput := TfrmYSKInput.Create(self);
    with frmYSKInput do
    begin
      FYFID := '';
      if showmodal = 1 then
      begin
        initGrid();
      end;
    end;
  finally
    frmYSKInput.free;
  end;
end;

procedure TfrmYSCR.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv2, ADOQueryMain, '应收款列表');
end;

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

procedure TfrmYSCR.FormCreate(Sender: TObject);
begin
  cxGrid2.Align := alclient;
end;

procedure TfrmYSCR.TchkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET status=''1'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYSCR.TnochkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          if Fieldbyname('HZFPHX').AsFloat > 0 then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            EnableControls;
            application.MessageBox('不能撤销已核销数据！', '提示信息', 0);
            exit;
          end;
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET status=''0'' ');
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYSCR.ToolButton3Click(Sender: TObject);
begin
  try
    frmYSKInput := TfrmYSKInput.Create(self);
    with frmYSKInput do
    begin
      FYFID := Trim(CDS_HZ.fieldbyname('YFID').AsString);
      ToolButton1.Visible := false;
      if showmodal = 1 then
      begin
        initGrid();
      end;
    end;
  finally
    frmYSKInput.free;
  end;
end;

procedure TfrmYSCR.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 TfrmYSCR.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 TfrmYSCR.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from YF_Money_CR_SUB A');
    sql.Add('where YFID=' + Quotedstr(Trim(CDS_HZ.fieldbyname('YFID').AsString)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_YFS);
  SInitCDSData20(ADOQueryTemp, CDS_YFS);
end;

procedure TfrmYSCR.YFGSTypeChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYSCR.Tv1DblClick(Sender: TObject);
begin
  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 TfrmYSCR.ToolButton4Click(Sender: TObject);
begin
  PrintMD('Report');
end;

procedure TfrmYSCR.ComboBox2DropDown(Sender: TObject);
begin
  if CDS_HZ.IsEmpty = False then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT distinct A4FPNO FROM JYOrder_BaoGuan_Main B  WHERE b.DCNO=' + QuotedStr(Trim(CDS_HZ.fieldbyname('dcno').AsString)));

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

end;

procedure TfrmYSCR.Button1Click(Sender: TObject);
var
  CDCNO: string;
begin

  if CDS_HZ.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin

        if Fieldbyname('ssel').AsBoolean then
        begin
          CDCNO := Trim(CDS_HZ.fieldbyname('DCNO').AsString);
          if trim(CDCNO) <> Trim(CDS_HZ.fieldbyname('DCNO').AsString) then
          begin
            Application.MessageBox('不能选择多个订舱单号!', '提示', 0);
            ADOQueryCmd.Connection.RollbackTrans;
            Exit;
          end;
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update YF_Money_CR SET WXFPNO=' + QuotedStr(ComboBox2.Text));
            sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;
    application.MessageBox('操作成功！', '提示信息', 0);
    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
    Panel3.Visible := False;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYSCR.ToolButton5Click(Sender: TObject);
begin
  Panel3.Visible := True;
end;

end.

