unit U_YuDingHS_FZLR;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxGridCustomPopupMenu,
  cxGridPopupMenu, cxCurrencyEdit;

type
  TfrmYuDingHSDZLR = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    CDS_Type: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOCmd: TADOQuery;
    Tadd: TToolButton;
    TDel: TToolButton;
    cxGrid3: TcxGrid;
    TV1: TcxGridDBTableView;
    V3FPNo: TcxGridDBColumn;
    V1OrderNo: TcxGridDBColumn;
    V1HWName: TcxGridDBColumn;
    V1CHDate: TcxGridDBColumn;
    V1Qty: TcxGridDBColumn;
    V1QtyUnit: TcxGridDBColumn;
    V1BZType: TcxGridDBColumn;
    V1HYMoney: TcxGridDBColumn;
    V1HaiYunMoney: TcxGridDBColumn;
    V1BXMoney: TcxGridDBColumn;
    V1JCMoney: TcxGridDBColumn;
    V1RZMoney: TcxGridDBColumn;
    V1ZFMoney: TcxGridDBColumn;
    V1DZZMoney: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure FormCreate(Sender: TObject);
    procedure v1PRTHXPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TaddClick(Sender: TObject);
    procedure V1QtyUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V1HWNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TDelClick(Sender: TObject);
    procedure V1FKTypePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V1CJTypePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    procedure InitData();
    function  SaveData():Boolean;
    function  SaveDataSubOne():Boolean;
    function  SaveDataMain():Boolean;
    { Private declarations }
  public
    PState,CopyInt:Integer;
    FMainId,FFMainId,FHSID:String;
    FXS:Integer;
    { Public declarations }
  end;

var
  frmYuDingHSDZLR: TfrmYuDingHSDZLR;
  newh:hwnd;
implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_ZDYHelpSel,U_ZdyAttachment,U_YuDingFP_CX;

{$R *.dfm}

procedure TfrmYuDingHSDZLR.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('业务核算生产',Tv1,'业务核算管理');
end;

procedure TfrmYuDingHSDZLR.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from YWHS_DZ ');
    sql.Add('where DZID='''+Trim(FHSID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
end;

procedure TfrmYuDingHSDZLR.FormShow(Sender: TObject);
begin
  readCxGrid('业务核算生产',Tv1,'业务核算管理');
  InitData();
end;

function TfrmYuDingHSDZLR.SaveData():Boolean;
var
  maxno:String;
  fconNO,fmxType:string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('DZID').AsString)='' then
        begin
          if GetLSNo(ADOTemp,maxno,'DZ','YWHS_DZ',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('DZID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YWHS_DZ where DZID='''+Trim(FHSID)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('DZID').AsString)='' then
            Append
          else
            Edit;
          FieldByName('DZID').Value:=Trim(MaxNo);
          FieldByName('HSID').Value:=Trim(Order_Sub.Fieldbyname('HSID').AsString);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'YWHS_DZ',0);
          fieldbyname('Qty').Value:=Order_Sub.Fieldbyname('Qty').AsFloat;
          fieldbyname('HYMoney').Value:=Order_Sub.Fieldbyname('HYMoney').AsFloat;
          fieldbyname('HaiYunMoney').Value:=Order_Sub.Fieldbyname('HaiYunMoney').AsFloat;
          fieldbyname('BXMoney').Value:=Order_Sub.Fieldbyname('BXMoney').AsFloat;
          fieldbyname('JCMoney').Value:=Order_Sub.Fieldbyname('JCMoney').AsFloat;
          fieldbyname('RZMoney').Value:=Order_Sub.Fieldbyname('RZMoney').AsFloat;
          fieldbyname('ZFMoney').Value:=Order_Sub.Fieldbyname('ZFMoney').AsFloat;
          fieldbyname('DZZMoney').Value:=Order_Sub.Fieldbyname('DZZMoney').AsFloat;
          FieldByName('filler').Value:=Trim(DName);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('DZID').Value:=Trim(maxno);
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
    Result:=False;
  end;
end;

function TfrmYuDingHSDZLR.SaveDataMain():Boolean;
begin

end;

function TfrmYuDingHSDZLR.SaveDataSubOne():Boolean;
begin
  
end;

procedure TfrmYuDingHSDZLR.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Order_Sub.Locate('FPNo','',[])=true then
  begin
    Application.MessageBox('发票号不能为空！','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('Qty',NULL,[])=true then
  begin
    Application.MessageBox('数量不能为空！','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;

procedure TfrmYuDingHSDZLR.v1PRTColorPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdColor';
      flagname:='颜色';
      V1Name.Caption:='中文名称';
      V1Note.Caption:='英文名称';
      fnote:=True;
      MainType:=Trim(DName);
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTColor').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        Self.Order_Sub.FieldByName('SOrdDefStr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYuDingHSDZLR.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('SubId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrderCon_Sub where SubId='''+Trim(Order_Sub.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmYuDingHSDZLR.v1OrderUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
var
  mvalue,FMF,FKZ,FXS,FOrdUnit:string;
  FReal:Double;
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('OrderUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYuDingHSDZLR.FormCreate(Sender: TObject);
begin
  cxgrid3.Align:=alclient;
end;

procedure TfrmYuDingHSDZLR.v1PRTHXPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PRTHX';
      flagname:='花型';
      if ShowModal=1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTHX').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYuDingHSDZLR.TaddClick(Sender: TObject);
begin
  frmYuDingFP_CX:=TfrmYuDingFP_CX.create(self);
  with frmYuDingFP_CX do
  begin
    if ShowModal=1 then
    begin
      with Order_Sub do
      begin
        Append;
        FieldByName('HSID').Value:=Trim(Order_Main.fieldbyname('HSID').AsString);
        FieldByName('FPNo').Value:=Trim(Order_Main.fieldbyname('FPNo').AsString);
        FieldByName('OrderNo').Value:=Trim(Order_Main.fieldbyname('OrderNo').AsString);
        FieldByName('HWName').Value:=Trim(Order_Main.fieldbyname('HWName').AsString);
        FieldByName('CHDate').Value:=Trim(Order_Main.fieldbyname('CHDate').AsString);
        FieldByName('Qty').Value:=Trim(Order_Main.fieldbyname('Qty').AsString);
        FieldByName('QtyUnit').Value:=Trim(Order_Main.fieldbyname('QtyUnit').AsString);
        FieldByName('BZType').Value:=Trim(Order_Main.fieldbyname('BZType').AsString);
        post;
      end;
    end;
    Free;
  end;
end;

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

procedure TfrmYuDingHSDZLR.V1HWNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='HWName';
      flagname:='货物名称';
      if ShowModal=1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('HWName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYuDingHSDZLR.TDelClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  Order_Sub.Delete;
end;

procedure TfrmYuDingHSDZLR.V1FKTypePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FKType';
      flagname:='付款方式';
      if ShowModal=1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('FKType').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYuDingHSDZLR.V1CJTypePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CJType';
      flagname:='成交方式';
      if ShowModal=1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('CJType').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
