unit U_YSFP_FHLR;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, ExtCtrls, StdCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxCalendar, cxButtonEdit, cxTextEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxGridBandedTableView, cxGridDBBandedTableView,
  DBClient, ADODB, BtnEdit, cxCheckBox, cxCurrencyEdit, cxDropDownEdit;

type
  TfrmYSFP_FHLR = class(TForm)
    Panel2: TPanel;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource1: TDataSource;
    Order_PB: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    TV1FPMoney: TcxGridDBColumn;
    v2FPNo: TcxGridDBColumn;
    v2XYZNo: TcxGridDBColumn;
    v2KHName: TcxGridDBColumn;
    v2CQDate: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2JIDDate: TcxGridDBColumn;
    v2DGDate: TcxGridDBColumn;
    v2JiaoDDate: TcxGridDBColumn;
    ADOQuery2: TADOQuery;
    TADD: TToolButton;
    TDEL: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure v2P_CodeNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column4PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2PRTColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2PRTMFPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2PRTKZPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TdelClick(Sender: TObject);
    procedure TADDClick(Sender: TObject);
    procedure v2XYZNoPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2FPNoPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2KHNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    procedure initgrid();
    procedure SaveData();
    { Private declarations }
  public
    FMainid,FFPID,FromID: string;       //关联合同
    { Public declarations }
  end;

var
  frmYSFP_FHLR: TfrmYSFP_FHLR;

implementation
Uses
    U_DataLink,U_ZDYHelp,U_Fun,U_YSCR_CX, U_ZDYHelpSel,U_YSFP_WXCX;
{$R *.dfm}

procedure TfrmYSFP_FHLR.SaveData();
var
  maxno,maxsubNo:String;
begin
  with Order_PB do
  begin
    First;
    while not Eof do
    begin
      if Trim(Order_PB.fieldbyname('FHID').AsString)='' then
      begin
        if GetLSNo(ADOQuery1,maxSubno,'FH','JYOrderFHXX_Main',4,1)=False then
        begin
          Application.MessageBox('取子流水号失败！','提示',0);
          Exit;
        end;
      end else
      begin
        maxSubno:=Trim(Order_PB.fieldbyname('FHID').AsString);
      end;
      with ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from JYOrderFHXX_Main where FHID='''+Trim(maxSubno)+'''');
        Open;
      end;
      with ADOQuery1 do
      begin
        if isempty then
        Append
        else
        Edit;
        FieldByName('FHID').Value:=Trim(maxSubno);
        RTSetSaveDataCDS(ADOQuery1,Tv1,Order_PB,'JYOrderFHXX_Main',0);
        fieldbyname('filler').Value:=trim(DName);
        fieldbyname('Filltime').Value:=SGetServerDate(ADOQuery2);
        fieldbyname('FPMoney').Value:=Order_PB.Fieldbyname('FPMoney').AsFloat;
        Post;
      end;
      Order_PB.Edit;
      Order_PB.FieldByName('FHID').Value:=Trim(maxSubno);
      Next;
    end;
  end;
  Application.MessageBox('保存成功！','提示',0);
  ModalResult:=1;
end;

procedure TfrmYSFP_FHLR.initgrid();
begin
  if FromID='1' then
  FFPID:='';
  with Adoquery1 do
  begin
    close;
    sql.clear;
    sql.add('select * from JYOrderFHXX_Main ');
    SQL.Add('where FHID='''+trim(FFPID)+'''');
    open;
  end;
  SCreateCDS20(Adoquery1,Order_PB);
  SInitCDSData20(Adoquery1,Order_PB);
end;

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

procedure TfrmYSFP_FHLR.FormDestroy(Sender: TObject);
begin
  frmYSFP_FHLR:=nil;
end;

procedure TfrmYSFP_FHLR.FormShow(Sender: TObject);
begin
  ReadCxGrid('外销CI发票1',Tv1,'财务管理');
  initgrid();
end;

procedure TfrmYSFP_FHLR.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('外销CI发票1',Tv1,'财务管理');
  Close;
end;

procedure TfrmYSFP_FHLR.v2P_CodeNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='P_CodeName';
      flagname:='品名';
      if ShowModal=1 then
      begin
        Self.Order_PB.Edit;
        Self.Order_PB.FieldByName('P_CodeName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSFP_FHLR.v2Column4PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='P_Spec';
      flagname:='规格';
      if ShowModal=1 then
      begin
        Self.Order_PB.Edit;
        Self.Order_PB.FieldByName('P_Spec').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSFP_FHLR.v2PRTColorPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PRTColor';
      flagname:='颜色';
      if ShowModal=1 then
      begin
        Self.Order_PB.Edit;
        Self.Order_PB.FieldByName('PRTColor').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSFP_FHLR.v2PRTMFPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PRTMF';
      flagname:='门幅';
      if ShowModal=1 then
      begin
        Self.Order_PB.Edit;
        Self.Order_PB.FieldByName('PRTMF').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSFP_FHLR.v2PRTKZPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PRTKZ';
      flagname:='克重';
      if ShowModal=1 then
      begin
        Self.Order_PB.Edit;
        Self.Order_PB.FieldByName('PRTKZ').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSFP_FHLR.v2Column1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_PB do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
  end;
  with order_PB do
  begin
    edit;
    fieldbyname('KPMoney').Value:=fieldbyname('Price').AsFloat*fieldbyname('Qty').AsFloat;
  end;
end;

procedure TfrmYSFP_FHLR.TBSaveClick(Sender: TObject);
begin
  toolbar1.SetFocus;
  
  SaveData();
end;

procedure TfrmYSFP_FHLR.TdelClick(Sender: TObject);
begin
  if order_PB.IsEmpty then exit;
  if order_PB.FieldByName('FHID').AsString<>'' then
  begin
    if application.MessageBox('确定要删除吗','提示',1)=2 then exit;
    with ADOQuery2 do
    begin
      close;
      sql.Clear;
      sql.Add('delete from JYOrderFHXX_Main ');
      sql.add('where FHID='''+trim(order_PB.FieldByName('FHID').AsString)+'''');
      execsql;
    end;
  end;
  Order_PB.Delete;
end;

procedure TfrmYSFP_FHLR.TADDClick(Sender: TObject);
begin
  with Order_PB do
  begin
    append;
    
    post;
  end;
end;

procedure TfrmYSFP_FHLR.v2XYZNoPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='XYZNo';
      flagname:='信用证号';
      if ShowModal=1 then
      begin
        Self.Order_PB.Edit;
        Self.Order_PB.FieldByName('XYZNo').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSFP_FHLR.v2FPNoPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FPNo';
      flagname:='发票号';
      if ShowModal=1 then
      begin
        Self.Order_PB.Edit;
        Self.Order_PB.FieldByName('FPNo').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSFP_FHLR.v2KHNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KHName';
      flagname:='客户';
      if ShowModal=1 then
      begin
        Self.Order_PB.Edit;
        Self.Order_PB.FieldByName('KHName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
