unit U_FPWSLIST;

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, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, cxDropDownEdit, 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
  TfrmFPWSCX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    TBfind: TToolButton;
    ADOQueryMain: TADOQuery;
    TBexcel: TToolButton;
    RM1: TRMGridReport;
    RMDB_MD: TRMDBDataSet;
    Label3: TLabel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    SUBWXFPNO: TEdit;
    AdoPrint: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column13: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    CDS_SUB: TClientDataSet;
    DS_SUB: TDataSource;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Label1: TLabel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v1YSKeZhong: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Label5: TLabel;
    Label7: TLabel;
    orderno: TEdit;
    Label8: TLabel;
    Tv1Column9: TcxGridDBColumn;
    SQZT: TComboBox;
    Label9: TLabel;
    Tv1Column10: TcxGridDBColumn;
    Label11: TLabel;
    BEGDATE2: TDateTimePicker;
    ENDDATE2: TDateTimePicker;
    CheckBox1: TCheckBox;
    JSBZ: TComboBox;
    khname: TComboBox;
    YWY: TComboBox;
    Tv1Column11: TcxGridDBColumn;
    Label10: TLabel;
    ToolButton1: TToolButton;
    Tv1Column12: TcxGridDBColumn;
    Tv1MXOrderNoSummary: TcxGridDBColumn;
    CheckBox2: TCheckBox;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    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 TBfindClick(Sender: TObject);
    procedure TBexcelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FeeTypeChange(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBChkClick(Sender: TObject);
    procedure TBNochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure CheckBox1Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function YSData(): Boolean;
  public
    fSYRName: string;
    fflag: integer;
    canshu1: string;
    { Public declarations }

  end;

var
  frmFPWSCX: TfrmFPWSCX;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_SDMANAGEINPUT, U_FjList10;

{$R *.dfm}
function TfrmFPWSCX.YSData(): Boolean;
var
  CRID, YFID, Price, PriceUnit, OrderUnit, FComTaiTou: string;
begin
  Result := False;

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName=''' + Trim(CDS_HZ.fieldbyname('factoryname').asstring) + '''');
    sql.Add(' and YFDefFlag1=0 ');
    Open;
  end;
  if not ADOQueryTemp.IsEmpty then
  begin
    CRID := ADOQueryTemp.fieldbyname('CRID').AsString;
  end
  else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID := ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value := StrToInt(CRID);
      FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('factoryname').asstring);
      FieldByName('ZdyStr1').Value := '应收收';
      Post;
    end;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete from YF_Money_CR where MainID=''' + Trim(CDS_HZ.fieldbyname('CRNO').asstring) + ''' and YFName=''扣损''  and isnull(mainID,'''')<>'''' ');
    execsql;
  end;

  if GetLSNo(ADOQueryCmd, YFID, 'CS', 'YF_Money_CR', 3, 1) = False then
  begin
    Application.MessageBox('取应收最大号失败!', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where 1=2');
    Open;
  end;
  with ADOQueryCmd do
  begin
    Append;
    FieldByName('YFID').Value := Trim(YFID);
    FieldByName('YFTypeId').Value := Trim(CDS_HZ.fieldbyname('CRNO').asstring);
    FieldByName('CRID').Value := StrToInt(CRID);
    FieldByName('Filler').Value := Trim(DName);
    FieldByName('CRType').Value := '应收款登记';
    FieldByName('CRFlag').Value := '应收收';
    FieldByName('QtyFlag').Value := 1;
    FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('factoryName').asstring);
    FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', CDS_HZ.fieldbyname('CRTime').AsDateTime));
    FieldByName('YFType').Value := '自动生成';
    FieldByName('Price').Value := 0;
    FieldByName('PS').Value := -1 * 1;
    FieldByName('Qty').Value := -1 * CDS_HZ.fieldbyname('Qty').AsFloat;
    FieldByName('Price').Value := CDS_HZ.fieldbyname('Price').AsFloat;
    FieldByName('money').Value := CDS_HZ.fieldbyname('money').AsFloat;
    FieldByName('BBmoney').Value := CDS_HZ.fieldbyname('money').AsFloat;
    FieldByName('P_CodeName').Value := CDS_HZ.fieldbyname('C_CodeName').AsString;
    FieldByName('P_Color').Value := CDS_HZ.fieldbyname('C_Color').AsString;
    FieldByName('HuiLv').Value := 1;
    FieldByName('BZType').Value := '人民币';
    FieldByName('QtyUnit').Value := CDS_HZ.fieldbyname('QtyUnit').AsString;
    FieldByName('ComTaiTou').Value := Trim(CDS_HZ.fieldbyname('factoryName').asstring);
    FieldByName('YFName').Value := '扣损';
    FieldByName('MainId').Value := Trim(CDS_HZ.fieldbyname('CRNO').asstring);
    FieldByName('subID').Value := '';
    FieldByName('SYRName').Value := '宏强现货';
    FieldByName('note').Value := CDS_HZ.fieldbyname('Note').AsString;
    FieldByName('status').Value := '0';
    Post;
  end;

  Result := True;
end;

procedure TfrmFPWSCX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      SQL.Clear;
      SQL.Add('EXEC P_VIEW_FPDSCX_CS');

      if CheckBox2.Checked = True then
      begin
        sql.Add(' @begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
        sql.Add(',@Enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));

      end
      else
      begin
        sql.Add(' @begdate='''' ');
        sql.Add(',@Enddate='''' ');
      end;

      if CheckBox1.Checked = True then
      begin
        sql.Add(',@begdate2=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate2.DateTime))));
        sql.Add(',@Enddate2=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate2.DateTime + 1))));

      end;

      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    TBfind.Click;
  end;
end;

procedure TfrmFPWSCX.FormDestroy(Sender: TObject);
begin
  frmFPWSCX := nil;
end;

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

procedure TfrmFPWSCX.TBCloseClick(Sender: TObject);
begin

  WriteCxGrid(Self.Caption + Tv1.Name, Tv1);
  Close;
end;

procedure TfrmFPWSCX.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    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;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmFPWSCX.FormShow(Sender: TObject);
var
  fsj, fsj2, fsj3: string;
begin
  fsj := 'select distinct name=JSBZ  from jhd_main where valid=''Y''     ';
  SInitComBoxBySql(ADOQueryCmd, JSBZ, False, fsj);
  fsj2 := 'select distinct name=khname  from jhd_main where valid=''Y''  order by khname   ';
  SInitComBoxBySql(ADOQueryCmd, khname, False, fsj2);

  fsj3 := 'select distinct name=zdyname from KH_ZDY where Type=''jhdywy''    ';
  SInitComBoxBySql(ADOQueryCmd, YWY, False, fsj3);
  ReadCxGrid(Self.Caption + Tv1.Name, Tv1);

  initGrid();
end;

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

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

procedure TfrmFPWSCX.TBexcelClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmFPWSCX.TBAddClick(Sender: TObject);
begin
  try
    frmSDMANAGEINPUT := TfrmSDMANAGEINPUT.Create(Application);
    with frmSDMANAGEINPUT do
    begin
      fkeyNo := '';
      fflag := self.fflag;
      fSyrname := self.fSYRName;
      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmSDMANAGEINPUT.Free;
  end;
end;

procedure TfrmFPWSCX.FeeTypeChange(Sender: TObject);
begin
  TBfind.Click;
end;

procedure TfrmFPWSCX.TBEditClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  try
    frmSDMANAGEINPUT := TfrmSDMANAGEINPUT.Create(Application);
    with frmSDMANAGEINPUT do
    begin
      fkeyNo := Trim(Self.CDS_HZ.FieldByName('yfid').AsString);
      fflag := self.fflag;
      fSyrname := self.fSYRName;
      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmSDMANAGEINPUT.Free;
  end;
end;

procedure TfrmFPWSCX.ToolButton1Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JHDYWY';
      flagname := '业务员';
      if ShowModal = 1 then
      begin
//        FtySYRName.Text := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFPWSCX.TBChkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  ADOQueryCmd.Connection.BeginTrans;
  try

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update YF_Money_CR SET status=''1'' ');
      sql.Add(' ,Chker=''' + Trim(DName) + '''');
      sql.Add(' ,Chktime=''' + formatdateTIme('yyyy-MM-dd', CDS_HZ.fieldbyname('CRTime').AsDateTime) + '''');
      sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
      execsql;

    end;

    ADOQueryCmd.Connection.CommitTrans;
   // application.MessageBox('审核撤销成功！','提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmFPWSCX.TBNochkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update YF_Money_CR SET status=''0'',Chker=null,Chktime=null  ');
      sql.Add('where YFID =' + quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
      execsql;
    end;

   // application.MessageBox('审核撤销成功！','提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('审核撤销失败！', '提示信息', 0);
  end;
end;

procedure TfrmFPWSCX.cxTabControl1Change(Sender: TObject);
begin

  initGrid();
end;

procedure TfrmFPWSCX.TBPrintClick(Sender: TObject);
var
  filepath: string;
  i: integer;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if CDS_HZ.IsEmpty then
    exit;

  filepath := ExtractFilePath(Application.ExeName) + 'report\成品扣损单.rmf';

  if not FileExists(Pchar(filepath)) then
  begin
    application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
    exit;
  end;

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select * from BP_Problem');
    sql.Add('where CRNo=' + quotedstr(trim(CDS_HZ.fieldbyname('CRNo').AsString)));
    open;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(AdoPrint.fieldbyname('CRNO').AsString);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
    if FileExists(fImagePath) then
      DeleteFile(fImagePath);
    Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
  except
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    exit;
  end;
  RMVariables['QRBARCODE'] := fImagePath;
  RMVariables['dtxz'] := FormatDateTime('yyyy-mm-dd', date());
  RMVariables['zdr'] := trim(DName);
  RM1.LoadFromFile(filepath);
  RM1.ShowReport;

end;

procedure TfrmFPWSCX.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_SUB);
  SInitCDSData20(ADOQueryTemp, CDS_SUB);
end;

procedure TfrmFPWSCX.FormCreate(Sender: TObject);
begin
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime - 30;

  Enddate2.DateTime := SGetServerDate(ADOQueryTemp);
  begdate2.DateTime := Enddate2.DateTime - 30;
end;

procedure TfrmFPWSCX.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*,B.SDNO');
    SQL.Add(',CRTIME    ');

    SQL.Add(',GSDYSJE=(SELECT sum(HXNUM) from YF_Money_CR_SUB e  INNER JOIN YF_Money_CR f ON e.YFID=f.YFID where f.sdno=b.sdno)');
    SQL.Add(',OrdDate=(select OrdDate from jyorder_main c where a.mxorderno=c.orderno)');
    sql.Add(' from YF_Money_CR_SUB A INNER JOIN YF_Money_CR B ON A.YFID=B.YFID where SUBWXFPNO=' + Quotedstr(Trim(CDS_HZ.fieldbyname('SUBWXFPNO').AsString)));
    SQL.Add('ORDER BY SDNO');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_SUB);
  SInitCDSData20(ADOQueryTemp, CDS_SUB);
end;

procedure TfrmFPWSCX.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if (AViewInfo.GridRecord.Values[Tv1Column7.Index] = '1') then
  begin
    ACanvas.Brush.Color := $008080FF;
  end;
  if (AViewInfo.GridRecord.Values[Tv1Column11.Index] = '1') then
  begin
    ACanvas.Brush.Color := $0080FF80;
  end;
end;

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

end.

