unit U_TradeSampleClothOutEdit;

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, ExtCtrls, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, U_BaseInput, System.ImageList, Vcl.ImgList,
  dxScrollbarAnnotations, cxContainer, cxMaskEdit, dxCore, cxDateUtils,
  cxImageList, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmTradeSampleClothOutEdit = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    Tv1Column1: 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;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Panel1: TPanel;
    Label11: TLabel;
    BCIONO: TcxTextEdit;
    Label12: TLabel;
    IOType: TcxComboBox;
    IOTime: TcxDateEdit;
    Label1: TLabel;
    Label14: TLabel;
    ToCoName: TcxButtonEdit;
    ToolBar2: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    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 ToolButton1Click(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FormCreate(Sender: TObject);
    procedure Tv1Column16PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column19PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column18PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    function SaveData(): Boolean;
    procedure JSOutQty();
  public
    { Public declarations }
    FBCIOIDS, FStkName, FKHName: string;
  end;

var
  frmTradeSampleClothOutEdit: TfrmTradeSampleClothOutEdit;

implementation

uses
  U_DataLink, U_RTFun, U_CustomFun, U_ZDYHelp, U_TradeSampleClothStkSel,
  U_CompanySel;

{$R *.dfm}
procedure TfrmTradeSampleClothOutEdit.JSOutQty();
var
  MCJXS: Double;
begin
  if (Trim(CDS_Sub.FieldByName('SampleType').AsString) <> '挂架') and (Trim(CDS_Sub.FieldByName('SampleType').AsString) <> '样卡') then
  begin
    MCJXS := 0;
    if Trim(CDS_Sub.FieldByName('MakeType').AsString) = '样卡' then
      MCJXS := 0.05;
    if Trim(CDS_Sub.FieldByName('MakeType').AsString) = '挂架' then
      MCJXS := 0.25;
    with CDS_Sub do
    begin
      edit;
      FieldByName('Qty').value := MCJXS * CDS_Sub.FieldByName('MakeQty').AsInteger;
      Post;
    end;
  end;

end;

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

procedure TfrmTradeSampleClothOutEdit.TBAddClick(Sender: TObject);
begin
  try
    frmTradeSampleClothStkSel := TfrmTradeSampleClothStkSel.Create(Application);
    with frmTradeSampleClothStkSel do
    begin
      FStkIdS := CustomSelCDSKey(SELF.CDS_Sub, ['StkId'])[0];
      FstkName := Self.FstkName;
      if ShowModal = 1 then
      begin
        frmTradeSampleClothStkSel.CDS_Main.DisableControls;
        with frmTradeSampleClothStkSel.CDS_Main do
        begin
          First;
          while frmTradeSampleClothStkSel.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('STKID').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('STKID').Value;
              FieldByName('FromCoNo').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('FromCoNo').Value;
              FieldByName('FromCoName').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('FromCoName').Value;
              FieldByName('StkCoNo').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('StkCoNo').Value;
              FieldByName('StkCoName').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('StkCoName').Value;
              FieldByName('ToCoNo').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('StkCoNo').Value;
              FieldByName('ToCoName').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('StkCoName').Value;
              FieldByName('C_Pack').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_Pack').Value;
              FieldByName('C_Code').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_Code').Value;
              FieldByName('C_Name').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_Name').Value;
              FieldByName('C_Spec').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_Spec').Value;
              FieldByName('QtyUnit').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('QtyUnit').Value;
              FieldByName('Qty').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('STKQTY').Value;
              FieldByName('Piece').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('StkPiece').Value;
              FieldByName('BatchNo').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('BatchNo').Value;
              FieldByName('C_Width').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_Width').Value;
              FieldByName('C_GramWeight').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_GramWeight').Value;
              FieldByName('FromOrdNo').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('FromOrdNo').Value;
              FieldByName('FromMainId').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('FromMainId').Value;
              FieldByName('FromSubId').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('FromSubId').Value;
              FieldByName('ToOrdNo').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('FromOrdNo').Value;
              FieldByName('ToMainId').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('FromMainId').Value;
              FieldByName('ToSubId').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('FromSubId').Value;
              FieldByName('Workshop').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('Workshop').Value;
              FieldByName('PieceWeight').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('PieceWeight').Value;
              FieldByName('C_ColorDepth').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_ColorDepth').Value;
              FieldByName('C_Color').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_Color').Value;
              FieldByName('C_ColorNo').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_ColorNo').Value;
              FieldByName('FtyPCId').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('FtyPCId').Value;
              FieldByName('C_Pattern').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_Pattern').Value;
              FieldByName('C_Composition').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_Composition').Value;
              FieldByName('C_EComposition').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_EComposition').Value;
              FieldByName('C_StyleNo').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('C_StyleNo').Value;
              FieldByName('CraftCode').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('CraftCode').Value;
              FieldByName('CraftName').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('CraftName').Value;
              FieldByName('CraftEName').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('CraftEName').Value;
              FieldByName('GC_Name').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('GC_Name').Value;
              FieldByName('SampleType').Value := frmTradeSampleClothStkSel.CDS_Main.fieldbyname('SampleType').Value;
              Post;
            end;
            frmTradeSampleClothStkSel.CDS_Main.Delete;
          end;
        end;
        frmTradeSampleClothStkSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmTradeSampleClothStkSel.Free;
  end;

end;

function TfrmTradeSampleClothOutEdit.SaveData(): Boolean;
var
  MBCIOID, Maxno: 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 BS_Cloth_IO where BCIOID=''' + Trim(CDS_Sub.fieldbyname('BCIOID').AsString) + '''');
          Open;
        end;
        MBCIOID := Trim(ADOQueryTemp.fieldbyname('BCIOID').AsString);
        if Trim(MBCIOID) = '' then
        begin
          if not GetLSNo(ADOQueryCmd, Maxno, 'MC', 'BS_Cloth_IO', 4, 1) then
            raise Exception.Create('取染色坯布出库编号失败！');
        end
        else
        begin
          Maxno := Trim(MBCIOID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Cloth_IO where BCIOID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MBCIOID) = '' then
          begin
            Append;
            FieldByName('Fillid').Value := Trim(Dcode);
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editid').Value := Trim(Dcode);
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'BS_Cloth_IO', 0);
          RTSetsavedata(ADOQueryCmd, 'BS_Cloth_IO', Panel1, 0);
          FieldByName('ToCoNo').Value := Trim(ToCoName.Properties.LookupItems.Text);
          FieldByName('STKName').Value := FSTKName;
          FieldByName('BCIOID').Value := Trim(Maxno);

          FieldByName('IOFlag').Value := '出库';
          FieldByName('IOQtyFlag').Value := -1;
          FieldByName('STKID').Value := CDS_Sub.fieldbyname('STKID').Value;
          FieldByName('FromCoNo').Value := CDS_Sub.fieldbyname('FromCoNo').Value;
          FieldByName('FromCoName').Value := CDS_Sub.fieldbyname('FromCoName').Value;
          FieldByName('StkCoNo').Value := CDS_Sub.fieldbyname('StkCoNo').Value;
          FieldByName('ToCoNo').Value := CDS_Sub.fieldbyname('ToCoNo').Value;
          FieldByName('FromOrdNo').Value := CDS_Sub.fieldbyname('FromOrdNo').Value;
          FieldByName('FromMainId').Value := CDS_Sub.fieldbyname('FromMainId').Value;
          FieldByName('FromSubId').Value := CDS_Sub.fieldbyname('FromSubId').Value;

          FieldByName('ToOrdNo').Value := CDS_Sub.fieldbyname('ToOrdNo').Value;
          FieldByName('ToMainId').Value := CDS_Sub.fieldbyname('ToMainId').Value;
          FieldByName('ToSubId').Value := CDS_Sub.fieldbyname('ToSubId').Value;

          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update BS_Cloth_IO Set StkPiece=(select Sum(isnull(Piece,0)*IOQtyFlag) from BS_Cloth_IO A where A.STKID=BS_Cloth_IO.BCIOID)');
          sql.Add(',STKQty=(select Sum(isnull(Qty,0)*IOQtyFlag) from BS_Cloth_IO A where A.STKID=BS_Cloth_IO.BCIOID )');
          sql.Add('where BCIOID=''' + Trim(CDS_Sub.fieldbyname('STKID').AsString) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Cloth_IO where BCIOID=''' + Trim(CDS_Sub.fieldbyname('STKID').AsString) + '''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('STKQty').Value < 0 then
          raise Exception.Create('出库数量大于入库数量不能保存！');

        if ADOQueryTemp.FieldByName('StkPiece').Value < 0 then
          raise Exception.Create('出库匹数大于入库匹数不能保存！');
        with ADOQueryCmd do
        begin
          Close;
          Sql.Clear;
          Sql.Add('exec P_BS_Cloth_Move ');
          Sql.Add(' @BCIOID=' + quotedstr(Trim(Maxno)));
          Open;
        end;
        if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
          raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
          sql.Add('values(''贸易样布出库''    ');
          sql.Add(',' + quotedstr(Maxno));
          sql.Add(',''保存''   ');
          sql.Add(',' + quotedstr(DName));
          sql.Add(',' + quotedstr(trim(self.Caption)));
          sql.Add(',' + quotedstr(Maxno));
          sql.Add(')  ');
          ExecSQL;
        end;
        Edit;
        FieldByName('BCIOID').Value := Trim(Maxno);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmTradeSampleClothOutEdit.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradeSampleClothOutEdit.FormCreate(Sender: TObject);
begin
  inherited;
  IOTime.Date := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmTradeSampleClothOutEdit.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;
  ReadCxGrid(FSTKName + '出库登记', Tv1, '贸易汇总仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from BS_Cloth_IO A');
//    sql.Add(' where BCIOID=''' + Trim(FBCId) + '''');

    sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(FBCIOIDS) + ','','') X where A.BCIOID=X.RTValue )  ');
    Open;
  end;
  SCSHData(ADOQueryTemp, Panel1, 0);
  ToCoName.Properties.LookupItems.Text := Trim(ADOQueryTemp.FieldByName('ToCoNo').AsString);
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);

end;

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

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
        sql.Add('values(''贸易样布出库''    ');
        sql.Add(',' + quotedstr(Trim(CDS_Sub.fieldbyname('BCIOID').AsString)));
        sql.Add(',''删除''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(Trim(CDS_Sub.fieldbyname('BCIOID').AsString)));
        sql.Add(')  ');

        Sql.Add('exec P_BS_Cloth_Out_Del ');
        Sql.Add(' @BCIOIDS=' + quotedstr(Trim(CDS_Sub.fieldbyname('BCIOID').AsString)));
        Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
        Sql.Add(',@DName=' + quotedstr(Trim(DName)));
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    end;
  end
  else
    CDS_Sub.Delete;
end;

procedure TfrmTradeSampleClothOutEdit.TBSaveClick(Sender: TObject);
var
  Maxno: string;
begin
  if CDS_Sub.IsEmpty then
    Exit;
//  if CDS_Sub.Locate('IOTime', null, []) = True then
//  begin
//    Application.MessageBox('出库时间不能为空!', '提示', 0);
//    Exit;
//  end;
  if IOType.Text = '' then
  begin
    Application.MessageBox('出库类型不能为空!', '提示', 0);
    Exit;
  end;
 { if CDS_Sub.Locate('CRType;LCKNO', VarArrayOf(['加工出库', Null]), [loPartialKey]) then
  begin
    Application.MessageBox('加工出库的数据流程卡号不能为空!','提示',0);
    Exit;
  end;  }
//  if CDS_Sub.Locate('Piece', null, []) = True then
//  begin
//    Application.MessageBox('匹数不能为空!', '提示', 0);
//    Exit;
//  end;
  if CDS_Sub.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  if not GetLSNo(ADOQueryCmd, Maxno, 'BCO', 'BS_Cloth_IO', 4, 1) then
  begin
    Application.MessageBox('取出库单号失败!', '提示', 0);
    Exit;
  end
  else
    BCIONO.Text := Maxno;

  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmTradeSampleClothOutEdit.ToCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        self.ToCoName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.ToCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradeSampleClothOutEdit.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(fSTKName + '出库登记', Tv1, '贸易汇总仓库');

end;

procedure TfrmTradeSampleClothOutEdit.Tv1Column16PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  JSOutQty();

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

procedure TfrmTradeSampleClothOutEdit.Tv1Column18PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
    mvalue := '0';

  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  JSOutQty();

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

procedure TfrmTradeSampleClothOutEdit.Tv1Column19PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;

  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  JSOutQty();

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

procedure TfrmTradeSampleClothOutEdit.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
//      if Trim(Self.CDS_Sub.fieldbyname('IOType').AsString) = '销售出库' then
      if IOType.text = '销售出库' then
        FCoType := '客户'
      else
        FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ToCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('ToCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradeSampleClothOutEdit.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('BCIOID').AsString) <> '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Cloth_IO where BCIOID=''' + Trim(CDS_Sub.fieldbyname('BCIOID').AsString) + '''');
      Open;
    end;
    FFSPID := Trim(ADOQueryTemp.fieldbyname('BCIOID').AsString);
    if Trim(FFSPID) <> '' then
    begin
      IOType.Enabled := False;
    end
    else
    begin
      IOType.Enabled := True;
    end;
  end
  else
  begin
    IOType.Enabled := True;
  end;
end;

end.

