unit U_YSQtyEdit;
                                               
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;

type
  TfrmYSQtyEdit = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ADOQueryMain: TADOQuery;
    Label3: TLabel;
    FactoryName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Qty: TcxGridDBColumn;
    v2QtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    CRTime: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Note: TcxGridDBColumn;
    v2P_CodeName: TcxGridDBColumn;
    v2ps: TcxGridDBColumn;
    ToolButton6: TToolButton;
    Label1: TLabel;
    CKNO: TEdit;
    v2P_MF: TcxGridDBColumn;
    v2P_KZ: TcxGridDBColumn;
    v2P_Color: TcxGridDBColumn;
    v2P_HX: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton6Click(Sender: TObject);

  private
    { Private declarations }
    procedure InitGrid();
    function  SaveData():Boolean;
  public
    { Public declarations }
    FCKNO,FYFID:String;

  end;
var
  frmYSQtyEdit: TfrmYSQtyEdit;
  
implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_RTFun,U_KHListSelJJ;

{$R *.dfm}
procedure TfrmYSQtyEdit.InitGrid();
begin
  ToolBar1.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered:=False;
      sql.Add(' select A.*,QtyStr=dbo.[F_Get_ShuZiStr](A.Qty),MoneyStr=dbo.[F_Get_ShuZiStr](A.Money) ');
      sql.Add(',FactoryName10=dbo.getPinYin(A.FactoryName)+RTrim(A.FactoryName)');
      sql.Add(' from YS_Money_CR A');
      if Trim(FCKNO)<>'' then
      begin
        sql.Add('where A.CKNO='''+Trim(FCKNO)+'''');
        sql.Add(' and A.CRType=''应收款登记''  ');
      end else
      if Trim(FYFID)<>'' then
      begin
        sql.Add('where A.YFID='''+Trim(FYFID)+'''');
        sql.Add(' and A.CRType=''应收款登记''  ');
      end;

      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
    SCSHDataNew(ADOQueryMain,Panel1,2);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmYSQtyEdit.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ModalResult:=1;
end;

procedure TfrmYSQtyEdit.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应收款修改',Tv2,'财务管理');
  Close;
end;
procedure TfrmYSQtyEdit.FormShow(Sender: TObject);
begin
  ReadCxGrid('应收款修改',Tv2,'财务管理');
  InitGrid();
end;


function TfrmYSQtyEdit.SaveData():Boolean;
var
  maxId,CRID:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxId,'YS','YS_Money_CR',4,1)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_KC where FactoryName='''+Trim(CDS_HZ.fieldbyname('FactoryName').AsString)+''' ');
      sql.Add(' and KCType=''KH'' ');
      Open;
    end;
    if ADOQueryCmd.IsEmpty=False then
    begin
      CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YS_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
      CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YS_Money_KC where 1<>1');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('CRID').Value:=StrToInt(CRID);
        FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
        FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
        FieldByName('ZdyStr1').Value:='应收收';
        Post;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value:=Trim(maxId);
      FieldByName('CRID').Value:=StrToInt(CRID);
      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YS_Money_CR',7);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='应收款登记';
      FieldByName('CRFlag').Value:='应收收';
      FieldByName('QtyFlag').Value:=1;
      FieldByName('FactoryNo').Value:=Trim(CDS_HZ.fieldbyname('FactoryNo').AsString);
      FieldByName('FactoryName').Value:=Trim(CDS_HZ.fieldbyname('FactoryName').AsString);
      FieldByName('CRTime').Value:=formatdateTIme('yyyy-MM-dd',CDS_HZ.fieldbyname('CRTime').AsDateTime);
      Post;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('YFId').Value:=Trim(maxId);
      FieldByName('CRID').Value:=StrToInt(CRID);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;

procedure TfrmYSQtyEdit.cxGridDBColumn5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FPrice,FMoney,FShuiDian,FHSPrice,FHSMoney,FJSMoney,FHZFee,FFieldName:String;
begin
  {mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    if Trim(mvalue)<>'' then
      FieldByName(FFieldName).Value:=Trim(mvalue)
    else
      FieldByName(FFieldName).Value:=Null;
    Post;
  end;

  FQty:=Trim(CDS_HZ.fieldbyname('Qty').AsString);
  if Trim(FQty)='' then FQty:='0';

  FPrice:=Trim(CDS_HZ.fieldbyname('Price').AsString);
  if Trim(FPrice)='' then FPrice:='0';
  FQty:=Trim(CDS_HZ.fieldbyname('Qty').AsString);
  if Trim(FQty)='' then FQty:='0';

  FHZFee:=Trim(CDS_HZ.fieldbyname('HZFee').AsString);
  if Trim(FHZFee)='' then FHZFee:='0';

  FMoney:=Trim(CDS_HZ.fieldbyname('Money').AsString);
  if Trim(FMoney)='' then FMoney:='0';

  if Trim(FFieldName)<>'Money' then
  begin
    FMoney:=FloatToStr(StrToFloat(FQty)*StrToFloat(FPrice)+StrToFloat(FHZFee));
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('Money').Value:=StrToFloat(FMoney);
    Post;
  end;
  tv2.Controller.EditingController.ShowEdit(); }
end;

procedure TfrmYSQtyEdit.cxGridDBColumn6PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.CDS_HZ.Edit;
        Self.CDS_HZ.FieldByName('QtyUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSQtyEdit.ToolButton6Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  ToolBar1.SetFocus;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YS_Money_CR where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Edit;
          RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'YS_Money_CR',7);
          FieldByName('Editer').Value:=Trim(DName);
          FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          Post;
        end;
        Next;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
  except
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

end.


