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, ExtCtrls, MMSystem, cxCheckBox, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmShaOutPut = class(TForm)
    ToolBar1: TToolBar;
    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;
    v1Column13: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column6PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId, FDelStr: string;
  end;

var
  frmShaOutPut: TfrmShaOutPut;

implementation

uses
  U_DataLink, U_RTFun, U_ClothContractListHZCX, U_ZDYHelp, U_ShaKCSelList,
  U_GYSSelList, U_ZdyAttachGYS, U_ProductOrderListSel, U_BPShengChanListSel,
  U_ContractCGJGSelList, U_GYSList, U_ClothSCHZListSel, U_KHList;

{$R *.dfm}

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

function TfrmShaOutPut.SaveCKData(): Boolean;
var
  FCRID, Maxno, FSPID, MaxPYNo, FPYID, FFString, FQMID, maxMXId: 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, 'SC', '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('FromFactoryNo').Value := Trim(CDS_Sub.fieldbyname('FromFactoryNo').AsString);
          FieldByName('ToFactoryNo').Value := Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
          FieldByName('CPID').Value := Trim(CDS_Sub.fieldbyname('CPID').AsString);
          FieldByName('PBNO').Value := Trim(CDS_Sub.fieldbyname('PBNO').AsString);
          FieldByName('SQID').Value := CDS_Sub.fieldbyname('SQID').Value;
          FieldByName('Price').Value := CDS_Sub.fieldbyname('Price').Value;
          FieldByName('CRFlag').Value := '出库';
          FieldByName('CRQtyFlag').Value := -1;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_SXPB_CR', 2);
          FieldByName('CKName').Value := '纱线';
          if Trim(CDS_Sub.fieldbyname('CRType').AsString) = '平移出库' then
          begin
            FieldByName('PYID').Value := Trim(Maxno);
          end;
          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 ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add(' select A.* ');
          SQL.Add(' from CK_SXPB_CR A  ');
          sql.Add(' where isnull(Qty,0)<isnull(HCQty,0)');
          sql.Add(' and FromSPID=''' + Trim(Maxno) + '''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty = False then
        begin
          CDS_Sub.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox(PChar('纱线：' + Trim(CDS_Sub.fieldbyname('SPName').AsString) + ' 出库数量小于回仓数量不能保存！'), '提示', 0);
          exit;
        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(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;
        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;
        if Trim(CDS_Sub.fieldbyname('CRType').AsString) = '平移出库' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CR where PYID=''' + Trim(Maxno) + '''');
            sql.Add(' and isnull(CRType,'''')=''平移入库'' ');
            Open;
          end;
          FPYID := Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
          if Trim(FPYID) = '' then
          begin
            if GetLSNo(ADOQueryCmd, MaxPYNo, 'YR', 'CK_SXPB_CR', 3, 1) = False then
            begin
              Result := False;
              CDS_Sub.EnableControls;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取纱线平移入库编号失败!', '提示', 0);
              Exit;
            end;
          end
          else
          begin
            MaxPYNo := Trim(FPYID);
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(MaxPYNo) + '''');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(FPYID) = '' then
            begin
              Append;
            end
            else
            begin
              Edit;
            end;
            FieldByName('SPID').Value := Trim(MaxPYNo);
            FieldByName('FZSPID').Value := Trim(MaxPYNo);
            FieldByName('FactoryNo').Value := Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
            FieldByName('FromFactoryNo').Value := Trim(CDS_Sub.fieldbyname('FromFactoryNo').AsString);
            FieldByName('ToFactoryNo').Value := Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
            //FieldByName('CRID').Value:=CDS_Sub.fieldbyname('CRID').Value;
            FieldByName('PBNO').Value := Trim(CDS_Sub.fieldbyname('ToPBNO').AsString);
            FieldByName('FromPBNO').Value := Trim(CDS_Sub.fieldbyname('FromPBNO').AsString);
            FieldByName('CRFlag').Value := '入库';
            FieldByName('CRQtyFlag').Value := 1;
            RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_SXPB_CR', 2);
            FieldByName('CKName').Value := '纱线';
            FieldByName('CRType').Value := '平移入库';
            FieldByName('PYID').Value := Trim(Maxno);
            if Trim(FPYID) = '' 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)');
            sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
            sql.Add(' where SPID=''' + Trim(MaxPYNo) + '''');
            ExecSQL;
          end;

          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(MaxPYNo) + '''');
            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;
        end;
            //销售出库自动生成应收////
        if Trim(CDS_Sub.fieldbyname('CRType').AsString) = '销售出库' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' exec P_ZDYS_SX @SPID=''' + Trim(Maxno) + '''');
            ExecSQL;
          end;
        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('纱线出库登记2', Tv1, '纱线仓库');
  Close;
end;

procedure TfrmShaOutPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('纱线出库登记2', Tv1, '纱线仓库');
  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
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where FZSPID=');
      sql.Add('(select SPID from CK_SXPB_CR where PYID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
      sql.Add(' and isnull(CRType,'''')=''平移入库'')');
      sql.Add(' and CRQtyFlag=-1');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已有出库记录，不能删除!', '提示', 0);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where FromSPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
      sql.Add(' and ZFFlag=0');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已有回仓数据，不能删除!', '提示', 0);
      Exit;
    end;
    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.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('ToFactoryName', null, []) = True then
  begin
    Application.MessageBox('出到加工厂不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
//  if CDS_Sub.Locate('PiQty',null,[])=True then
//  begin
//    Application.MessageBox('箱/件数不能为空!','提示',0);
//    Exit;
//  end;
  {if CDS_Sub.Locate('CRType;ToPBNO',VarArrayOf(['蒸丝加工出库',Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('蒸丝加工出库出到白坯单号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;ToPBNO',VarArrayOf(['染纱加工出库',Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('染纱加工出库出到白坯单号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;ToPBNO',VarArrayOf(['并线加工出库',Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('并线加工出库出到白坯单号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;ToPBNO',VarArrayOf(['加捻加工出库',Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('加捻加工出库出到白坯单号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;ToPBNO',VarArrayOf(['织造加工出库',Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('织造加工出库出到白坯单号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;ToPBNO',VarArrayOf(['本厂织造出库',Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('本厂织造出库出到白坯单号不能为空!','提示',0);
    Exit;
  end;       }
  if SaveCKData() then
  begin
    if Trim(FDelStr) <> '有提示' then
      Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmShaOutPut.v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmContractCGJGSelList := TfrmContractCGJGSelList.Create(Application);
    with frmContractCGJGSelList do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ConNo').Value := frmContractCGJGSelList.Order_Main.fieldbyname('ConNo').Value;
          FieldByName('CGMainId').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('CGSubId').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('ToFactoryName').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('FactoryNoName').AsString);
          FieldByName('ToFactoryNo').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('FactoryNo').AsString);
          FieldByName('SPName').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('C_CodeName').AsString);
          FieldByName('SPSpec').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('C_Spec').AsString);
          FieldByName('SPCF').Value := Trim(frmContractCGJGSelList.Order_Main.fieldbyname('C_CF').AsString);
        end;
      end;
    end;
  finally
    frmContractCGJGSelList.Free;
  end;
end;

procedure TfrmShaOutPut.v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if ((trim(CDS_Sub.FieldByName('CRType').AsString) = '销售出库') or (trim(CDS_Sub.FieldByName('CRType').AsString) = '客户退货')) then
  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
  else
  begin
    try
      frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
      with frmZdyAttachGYS do
      begin
        if ShowModal = 1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('ToFactoryNo').Value := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
            FieldByName('ToFactoryName').Value := Trim(CDS_HZ.fieldbyname('CoName').AsString);
            //Post;
          end;
        end;
      end;
    finally
      frmZdyAttachGYS.Free;
    end;
  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;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where FromSPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + ''' and ZFFlag=0');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      v1Column11.Options.Editing := False;
      v1Column5.Options.Editing := False;
    end
    else
    begin
      v1Column11.Options.Editing := True;
      v1Column5.Options.Editing := True;
    end;
  end
  else
  begin
    v1CRType.Options.Editing := True;
  end;

end;

procedure TfrmShaOutPut.ToolButton1Click(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('PBNO').Value := frmShaKCSelList.CDS_Main.fieldbyname('PBNO').Value;
              FieldByName('FromPBNO').Value := frmShaKCSelList.CDS_Main.fieldbyname('PBNO').Value;
              FieldByName('FactoryName').Value := frmShaKCSelList.CDS_Main.fieldbyname('FactoryName').Value;
              FieldByName('FactoryNo').Value := frmShaKCSelList.CDS_Main.fieldbyname('FactoryNo').Value;
              FieldByName('SSType').Value := frmShaKCSelList.CDS_Main.fieldbyname('SSType').Value;
              FieldByName('SPName').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPName').Value;
              FieldByName('SPNameGYS').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPNameGYS').Value;
              FieldByName('SPCode').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPCode').Value;
              FieldByName('CPID').Value := frmShaKCSelList.CDS_Main.fieldbyname('CPID').Value;
              FieldByName('SPSpec').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPSpec').Value;
              FieldByName('SPCF').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPCF').Value;
              FieldByName('SXPinPai').Value := frmShaKCSelList.CDS_Main.fieldbyname('SXPinPai').Value;
              FieldByName('SPMF').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPMF').Value;
              FieldByName('SPKZ').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPKZ').Value;
              FieldByName('FromFactoryName').Value := frmShaKCSelList.CDS_Main.fieldbyname('ToFactoryName').Value;
              FieldByName('FromFactoryNo').Value := frmShaKCSelList.CDS_Main.fieldbyname('ToFactoryNo').Value;
              FieldByName('ToFactoryName').Value := frmShaKCSelList.CDS_Main.fieldbyname('ToFactoryName').Value;
              FieldByName('ToFactoryNo').Value := frmShaKCSelList.CDS_Main.fieldbyname('ToFactoryNo').Value;
              FieldByName('ToPBNo').Value := frmShaKCSelList.CDS_Main.fieldbyname('PBNO').Value;
              FieldByName('Price').Value := frmShaKCSelList.CDS_Main.fieldbyname('Price').Value;
              FieldByName('Money').Value := frmShaKCSelList.CDS_Main.fieldbyname('KCMoney').Value;
              FieldByName('SQID').Value := frmShaKCSelList.CDS_Main.fieldbyname('SQID').Value;
              FieldByName('QtyUnit').Value := frmShaKCSelList.CDS_Main.fieldbyname('QtyUnit').Value;
              FieldByName('PiQty').Value := frmShaKCSelList.CDS_Main.fieldbyname('MXKCPiQty').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('BaoType').Value := frmShaKCSelList.CDS_Main.fieldbyname('BaoType').Value;
              FieldByName('SPColor').Value := frmShaKCSelList.CDS_Main.fieldbyname('SPColor').Value;
            end;
            frmShaKCSelList.CDS_Main.Delete;
          end;
        end;
        frmShaKCSelList.CDS_Main.EnableControls;

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

procedure TfrmShaOutPut.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if Trim(Self.CDS_Sub.FieldByName('CRType').AsString) = '梭织织造出库' then
  begin
    try
      frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
      with frmProductOrderListSel do
      begin
        if ShowModal = 1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('ToPBNO').Value := frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').Value;
          end;
        end;
      end;
    finally
      frmProductOrderListSel.Free;
    end;
  end
  else
  begin
    try
      frmClothSCHZListSel := TfrmClothSCHZListSel.Create(Application);
      with frmClothSCHZListSel do
      begin
        if ShowModal = 1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('ToPBNO').Value := frmClothSCHZListSel.Order_Main.fieldbyname('ConNo').Value;
          end;
        end;
      end;
    finally
      frmClothSCHZListSel.Free;
    end;
  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;

end.

