unit U_ShaOutPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, ComCtrls, ToolWin,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxButtonEdit, cxTextEdit, cxSpinEdit,
  StdCtrls, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmShaOutPut = class(TForm)
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    ToolButton1: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column9PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column6PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId: string;
  end;

var
  frmShaOutPut: TfrmShaOutPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ShaKCSelList, U_KHList, U_FLOrderListSel;

{$R *.dfm}

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

procedure TfrmShaOutPut.TBAddClick(Sender: TObject);
begin
  try
    frmShaKCSelList := TfrmShaKCSelList.Create(Application);
    with frmShaKCSelList do
    begin
      ToolButton1.Visible := True;
      if ShowModal = 1 then
      begin
        frmShaKCSelList.CDS_Main.DisableControls;
        with frmShaKCSelList.CDS_Main do
        begin
          First;
          while frmShaKCSelList.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('FactoryName').Value := frmShaKCSelList.CDS_Main.fieldbyname('FactoryName').Value;
              FieldByName('FactoryNo').Value := frmShaKCSelList.CDS_Main.fieldbyname('FactoryNo').Value;
              FieldByName('SPName').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPName').Value;
              FieldByName('SPCode').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPCode').Value;
              FieldByName('CPID').Value := frmShaKCSelList.CDS_Main.fieldbyname('CPID').Value;
              FieldByName('QtyUnit').Value := frmShaKCSelList.CDS_Main.fieldbyname('QtyUnit').Value;
              FieldByName('Qty').Value := frmShaKCSelList.CDS_Main.fieldbyname('MXKCQty').Value;
              FieldByName('FZSPID').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPID').Value;
              FieldByName('BatchNo').Value := frmShaKCSelList.CDS_Main.fieldbyname('BatchNo').Value;
              FieldByName('SPHX').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPHX').Value;

              FieldByName('tpno').Value := frmShaKCSelList.CDS_Main.fieldbyname('tpno').Value;
              FieldByName('CRType').Value := '正常出库';

              FieldByName('jwltype').Value := frmShaKCSelList.CDS_Main.fieldbyname('jwltype').Value;
              FieldByName('zhs').Value := frmShaKCSelList.CDS_Main.fieldbyname('zhs').Value;
              FieldByName('piqty').Value := frmShaKCSelList.CDS_Main.fieldbyname('piqty').Value;
              FieldByName('MHJS').Value := frmShaKCSelList.CDS_Main.fieldbyname('MHJS').Value;
              FieldByName('MICHANG').Value := frmShaKCSelList.CDS_Main.fieldbyname('MICHANG').Value;
              FieldByName('mcunit').Value := frmShaKCSelList.CDS_Main.fieldbyname('mcunit').Value;
              FieldByName('JSUNIT').Value := frmShaKCSelList.CDS_Main.fieldbyname('JSUNIT').Value;
              FieldByName('orderno').Value := frmShaKCSelList.CDS_Main.fieldbyname('orderno').Value;
              FieldByName('ORDMainIdRK').Value := frmShaKCSelList.CDS_Main.fieldbyname('ORDMainIdRK').Value;
              FieldByName('ORDMainIdCK').Value := frmShaKCSelList.CDS_Main.fieldbyname('ORDMainIdRK').Value;
              FieldByName('ORDSubIdRK').Value := frmShaKCSelList.CDS_Main.fieldbyname('ORDSubIdRK').Value;
              FieldByName('ORDSubIdcK').Value := frmShaKCSelList.CDS_Main.fieldbyname('ORDSubIdRK').Value;
               FieldByName('NOTE').Value := frmShaKCSelList.CDS_Main.fieldbyname('NOTE').Value;
              FieldByName('CYID').Value := frmShaKCSelList.CDS_Main.fieldbyname('CYID').Value;
              FieldByName('SPCODE').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPCODE').Value;
              FieldByName('SPEColor').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPEColor').Value;
              FieldByName('SPEName').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPEName').Value;

            end;
            frmShaKCSelList.CDS_Main.Delete;
          end;
        end;
        frmShaKCSelList.CDS_Main.EnableControls;

      end;
    end;
  finally
    frmShaKCSelList.Free;
  end;
end;

function TfrmShaOutPut.SaveCKData(): Boolean;
var
  FCRID, Maxno, FSPID, MaxPYNo, FPYID, FFString: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
          Open;
        end;
        FSPID := Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
        if Trim(FSPID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, Maxno, 'JC', 'CK_SXPB_CR', 3, 1) = False then
          begin
            CDS_Sub.EnableControls;
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取机物料出库编号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          Maxno := Trim(FSPID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FSPID) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('SPID').Value := Trim(Maxno);
          FieldByName('FZSPID').Value := Trim(CDS_Sub.fieldbyname('FZSPID').AsString);
          FieldByName('FactoryNo').Value := Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_Sub.fieldbyname('FactoryName').AsString);
          FieldByName('ToFactoryNo').Value := Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
          FieldByName('CPID').Value := Trim(CDS_Sub.fieldbyname('CPID').AsString);
          FieldByName('ORDMainIdRK').Value := CDS_Sub.fieldbyname('ORDMainIdRK').Value;
          FieldByName('ORDSubIdRK').Value := CDS_Sub.fieldbyname('ORDSubIdRK').Value;
          FieldByName('ORDMainIdCK').Value := CDS_Sub.fieldbyname('ORDMainIdCK').Value;
          FieldByName('ORDSubIdCK').Value := CDS_Sub.fieldbyname('ORDSubIdCK').Value;
          FieldByName('CRFlag').Value := '出库';
          FieldByName('CRQtyFlag').Value := -1;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_SXPB_CR', 2);
          FieldByName('CKName').Value := '机物料';
          if Trim(FSPID) = '' then
            FieldByName('Filler').Value := Trim(DName)
          else
          begin
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID and A.ZFFlag=0)');
          sql.Add(',MXKCPiQty=(select sum(isnull(zhs,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID and A.ZFFlag=0)');
          sql.Add(' where SPID=''' + Trim(CDS_Sub.fieldbyname('FZSPID').AsString) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(CDS_Sub.fieldbyname('FZSPID').AsString) + '''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('MXKCQty').Value < 0 then
        begin
          FFString := Trim(CDS_Sub.fieldbyname('FZSPID').AsString);
          CDS_Sub.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('出库数大于库存数不能保存!', '提示', 0);
          CDS_Sub.Locate('FZSPID', FFString, []);
          Exit;
        end;
        Edit;
        FieldByName('SPID').Value := Trim(Maxno);
        Post;

        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;

end;

procedure TfrmShaOutPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('机物料出库登记', Tv1, '机物料仓库77');
  Close;
end;

procedure TfrmShaOutPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('机物料出库登记', Tv1, '机物料仓库77');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from CK_SXPB_CR A');
    sql.Add(' where SPID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);
end;

procedure TfrmShaOutPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('SPID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete CK_SXPB_CR where SPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
        sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnullQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID and A.ZFFlag=0)');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID and A.ZFFlag=0 )');
        sql.Add(' where SPID=''' + Trim(CDS_Sub.fieldbyname('FZSPID').AsString) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end;
  CDS_Sub.Delete;
end;

procedure TfrmShaOutPut.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;

  if CDS_Sub.Locate('JWLTYPE', null, []) = True then
  begin
    Application.MessageBox('类型不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRTime', null, []) = True then
  begin
    Application.MessageBox('出库时间不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType', null, []) = True then
  begin
    Application.MessageBox('出库类型不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  with CDS_Sub do
  begin
    First;
    while not Eof do
    begin
      if FieldByName('CRType').AsString = '销售出库' then
      begin
        if FieldByName('ToFactoryName').AsString = '' then
        begin
          Application.MessageBox('出到单位不能为空!', '提示', 0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  if CDS_Sub.Locate('JWLTYPE;QtyUnit', VarArrayOf(['成品', '盒']), [loPartialKey]) = True then
  begin
    Application.MessageBox('成品单位只能选箱!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('JWLTYPE;QtyUnit', VarArrayOf(['成品', '卡']), [loPartialKey]) = True then
  begin
    Application.MessageBox('成品单位只能选箱!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('JWLTYPE;QtyUnit', VarArrayOf(['成品', '件']), [loPartialKey]) = True then
  begin
    Application.MessageBox('成品单位只能选箱!', '提示', 0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmShaOutPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  FFSPID: string;
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('SPID').AsString) <> '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
      Open;
    end;
    FFSPID := Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
    if Trim(FFSPID) <> '' then
    begin
      v1CRType.Options.Editing := False;
    end
    else
    begin
      v1CRType.Options.Editing := True;
    end;
  end
  else
  begin
    v1CRType.Options.Editing := True;
  end;
end;

procedure TfrmShaOutPut.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'llPerson';
      flagname := '领料人';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('llPerson').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaOutPut.v1Column9PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'llDepartment';
      flagname := '领用部门';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('llDepartment').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaOutPut.v2Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FQty, FPrice, FCostPrice, FMoney, FBBMoney, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  FQty := Trim(CDS_Sub.fieldbyname('Qty').AsString);
  if Trim(FQty) = '' then
    FQty := '0';

  FPrice := Trim(CDS_Sub.fieldbyname('Price').AsString);
  if Trim(FPrice) = '' then
    FPrice := '0';

  FCostPrice := Trim(CDS_Sub.fieldbyname('CostPrice').AsString);
  if Trim(FCostPrice) = '' then
    FCostPrice := '0';

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

  FMoney := FloatToStr(StrToFloat(FQty) * StrToFloat(FPrice) + StrToFloat(FCostPrice));
  with CDS_Sub do
  begin
    Edit;
    FieldByName('Money').Value := StrToFloat(FMoney);
    Post;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmShaOutPut.v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmKHList := TfrmKHList.Create(Application);
    with frmKHList do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ToFactoryNo').Value := Trim(Order_Main.fieldbyname('CoCode').AsString);
          FieldByName('ToFactoryName').Value := Trim(Order_Main.fieldbyname('CoName').AsString);
            //Post;
        end;
      end;
    end;
  finally
    frmKHList.Free;
  end;

end;

procedure TfrmShaOutPut.Tv1Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FMC, FMHJS, FZHS, FDDMS, FChaZhi, fmxhs: double;
begin
  with CDS_Sub do
  begin
    Edit;
    FZHS := FieldByName('ZHS').AsFloat;
    FMC := FieldByName('MICHANG').AsFloat;
    FMHJS := FieldByName('MHJS').AsFloat;
    FDDMS := FieldByName('DDMS').ASFLOAT;
    fmxhs := FieldByName('piqty').ASFLOAT;
    Post;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '箱' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * fmxhs * FMC * FMHJS]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
//      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '盒' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * FMC * FMHJS]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
//      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '件' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * FMC]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
//      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmShaOutPut.Tv1Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FMC, FMHJS, FZHS, FDDMS, FChaZhi, fmxhs: double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with CDS_Sub do
  begin
    Edit;
    FZHS := FieldByName('ZHS').AsFloat;
    FMC := FieldByName('MICHANG').AsFloat;
    FMHJS := FieldByName('MHJS').AsFloat;
    FDDMS := FieldByName('DDMS').ASFLOAT;
    fmxhs := FieldByName('piqty').ASFLOAT;
    Post;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '箱' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * fmxhs * FMC * FMHJS]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '盒' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * FMC * FMHJS]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '件' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * FMC]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmShaOutPut.Tv1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmFLOrderListSel := TfrmFLOrderListSel.Create(Application);
    with frmFLOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ORDMainIdRK').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('ORDSubIdRK').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('OrderNo').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);

        end;
      end;
    end;
  finally
    frmFLOrderListSel.Free;
  end;
end;

procedure TfrmShaOutPut.ToolButton1Click(Sender: TObject);
var
  FValue, FFValue, FColumn, FFColumn: string;
  FQty, FMC, FMHJS, FZHS, FDDMS, FChaZhi, fmxhs: double;
begin
  ToolBar1.SetFocus;
  if CDS_Sub.IsEmpty then
    Exit;
  if Application.MessageBox('确定要一键替换吗', '提示', 1) = 2 then
    exit;
  FColumn := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  FFColumn := Tv1.Controller.FocusedColumn.Summary.GroupFormat;
  FValue := Trim(CDS_Sub.fieldbyname(FColumn).AsString);
  if Trim(FFColumn) <> '' then
  begin
    FFValue := Trim(CDS_Sub.fieldbyname(FFColumn).AsString);
  end;
  with CDS_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      Edit;
      if FValue = '' then
      begin
        CDS_Sub.FieldByName(FColumn).Value := null;
      end
      else
      begin
        CDS_Sub.FieldByName(FColumn).Value := FValue;
      end;

      if Trim(FFColumn) <> '' then
      begin
        if FFValue = '' then
        begin
          CDS_Sub.FieldByName(FFColumn).Value := null;
        end
        else
        begin
          CDS_Sub.FieldByName(FFColumn).Value := FFValue;
        end;
      end;
      if (FColumn = 'ZHS') or (FColumn = 'piQty') or (FColumn = 'piQty') or (FColumn = 'MHJS') or (FColumn = 'MICHANG') or (FColumn = 'qtyunit') then
      begin

        if CDS_Sub.FieldByName('qtyunit').AsString = '箱' then
        begin

          FQty := strtofloat(format('%.2f', [FieldByName('ZHS').AsFloat * FieldByName('piqty').ASFLOAT * FieldByName('MICHANG').AsFloat * FieldByName('MHJS').AsFloat]));

          FieldbyName('QTY').Value := FQty;

        end;
        if CDS_Sub.FieldByName('qtyunit').AsString = '盒' then
        begin

          FQty := strtofloat(format('%.2f', [FieldByName('ZHS').AsFloat * FieldByName('MICHANG').AsFloat * FieldByName('MHJS').AsFloat]));

          FieldbyName('QTY').Value := FQty;

        end;
        if (CDS_Sub.FieldByName('qtyunit').AsString = '件') or (CDS_Sub.FieldByName('qtyunit').AsString = '卡') then
        begin

          FQty := strtofloat(format('%.2f', [FieldByName('MICHANG').AsFloat * FieldByName('zhs').AsFloat]));

          FieldbyName('QTY').Value := FQty;

        end;
//        CDS_Main.FieldByName('money').Value := CDS_Main.FieldByName('price').AsFloat * CDS_Main.FieldByName('qty').AsFloat;
      end;
      Post;

      Next;
    end;
    EnableControls;
  end;
end;

end.

