unit U_TradeClothTotalInInput;

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, StdCtrls, ExtCtrls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseInput, System.ImageList, Vcl.ImgList, cxCheckBox, cxPC,
  dxScrollbarAnnotations, cxImageList, cxDBLookupComboBox;

type
  TfrmTradeClothTotalInInput = class(TfrmBaseInput)
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1SPName: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    v1Column4: TcxGridDBColumn;
    DS_1: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    v1Column12: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1SPSpec: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1SPMF: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column6: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    GeMeiOutputSel: TToolButton;
    ADO_PM: TADOQuery;
    DS_PM: TDataSource;
    ADO_YS: TADOQuery;
    DS_YS: TDataSource;
    ADO_DW: TADOQuery;
    DS_DW: TDataSource;
    ToolButton4: 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 v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure GeMeiOutputSelClick(Sender: TObject);
    procedure v1SPNamePropertiesEditValueChanged(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Tv1Column6PropertiesEditValueChanged(Sender: TObject);
    procedure v1QtyUnitPropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }
    function SaveData(MSaveType: string): Boolean;
    function CheckData(): Boolean;
    procedure SetStatus();
  public
    { Public declarations }
    FBCId, canshu3, FSTKName, FKHName, FBCIOID: string;
  end;

var
  frmTradeClothTotalInInput: TfrmTradeClothTotalInInput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_TradePlanSel, U_ClothInfoSel,
  U_PurchaseContractSel, U_TradeSalesContractSel, U_GeMeiOutputSel,
  U_TradeMarketListSEL;

{$R *.dfm}

function TfrmTradeClothTotalInInput.CheckData(): Boolean;
var
  mvalue, FReal: double;
begin
  try
    if CDS_Sub.IsEmpty then
      raise Exception.Create('明细不能为空！');

    if CDS_Sub.Locate('IOTime', null, []) = True then
      raise Exception.Create('入库时间不能为空！');

    if CDS_Sub.Locate('IOType', null, []) = True then
      raise Exception.Create('入库类型不能为空！');
    if CDS_Sub.Locate('C_Degree', null, []) = True then
      raise Exception.Create('类型不能为空！');
    if CDS_Sub.Locate('FromCoName', null, []) = True then
      raise Exception.Create('来自单位不能为空！');

    if CDS_Sub.Locate('C_Name', null, []) = True then
      raise Exception.Create('品名不能为空！');

    if CDS_Sub.Locate('Piece', null, []) = True then
      raise Exception.Create('匹数不能为空！');

    if CDS_Sub.Locate('C_ColorNo', null, []) = True then
      raise Exception.Create('色号不能为空！');

    if CDS_Sub.Locate('C_ColorNo', '', []) = True then
      raise Exception.Create('色号不能为空！');

    if CDS_Sub.Locate('C_Color', null, []) = True then
      raise Exception.Create('颜色不能为空！');

    if CDS_Sub.Locate('C_Color', '', []) = True then
      raise Exception.Create('颜色不能为空！');
    if CDS_Sub.Locate('Qty', null, []) = True then
      raise Exception.Create('数量不能为空！');

    if CDS_Sub.Locate('QtyUnit', null, []) = True then
      raise Exception.Create('数量单位不能为空！');

    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

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

procedure TfrmTradeClothTotalInInput.TBAddClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty = False then
  begin
    CopyAddRowCDS(CDS_Sub);
    with CDS_Sub do
    begin
      Edit;
      FieldByName('BCIOID').Value := null;
      FieldByName('StkCoNo').Value := '本厂仓库';
      FieldByName('StkCoName').Value := '本厂仓库';

      post;
    end;
  end
  else
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
      FieldByName('StkCoNo').Value := '本厂仓库';
      FieldByName('StkCoName').Value := '本厂仓库';
      FieldByName('FromCoNo').Value := 'G0001';
      FieldByName('FromCoName').Value := '染厂供应商';
//      FieldByName('QtyUnit').Value := 'Kg';
      FieldByName('C_Degree').AsString := Trim('坯布');
      FieldByName('IOType').Value := '采购入库';
      Post;
    end;
  end;

end;

function TfrmTradeClothTotalInInput.SaveData(MSaveType: string): Boolean;
var
  Maxno, MBCIOID: 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, 'MR', '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);
            FieldByName('BCIONO').Value := Trim(Maxno);
            FieldByName('BCIOID').Value := Trim(Maxno);
            FieldByName('BCIONO').Value := Trim(Maxno);
            FieldByName('STKID').Value := Trim(Maxno);
            FieldByName('CIID').Value := Trim(Maxno);
          end
          else
          begin
            Edit;
            FieldByName('Editid').Value := Trim(Dcode);
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
            FieldByName('status').value := '1';
          end;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'BS_Cloth_IO', 0);
          FieldByName('FromCoNo').Value := CDS_Sub.fieldbyname('FromCoNo').Value;
          FieldByName('StkCoNo').Value := CDS_Sub.fieldbyname('StkCoNo').Value;

          FieldByName('FromMainId').Value := CDS_Sub.fieldbyname('FromMainId').Value;
          FieldByName('FromSubId').Value := CDS_Sub.fieldbyname('FromSubId').Value;

          FieldByName('FromConMId').Value := CDS_Sub.fieldbyname('FromConMId').Value;
          FieldByName('FromConSId').Value := CDS_Sub.fieldbyname('FromConSId').Value;

          FieldByName('FromPurMId').Value := CDS_Sub.fieldbyname('FromPurMId').Value;
          FieldByName('FromPurSId').Value := CDS_Sub.fieldbyname('FromPurSId').Value;

          FieldByName('FromTPNID').Value := CDS_Sub.fieldbyname('FromTPNID').Value;

          FieldByName('TMMID').Value := CDS_Sub.fieldbyname('TMMID').Value;
          FieldByName('TMSID').Value := CDS_Sub.fieldbyname('TMSID').Value;
//          FieldByName('TMMNO').Value := CDS_Sub.fieldbyname('TMMNO').Value;

          FieldByName('STKName').Value := FSTKName;
          FieldByName('IOFlag').Value := '入库';
          FieldByName('IOQtyFlag').Value := 1;
          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(Maxno) + '''');
          ExecSQL;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update BS_Cloth_IO Set status = 1');

          sql.Add(' where BCIOID=''' + Trim(FBCIOID) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select stkQty,StkPiece from BS_Cloth_IO where BCIOID=''' + Trim(Maxno) + '''');
          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('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;

        if (MSaveType = '检验出库') or (MSaveType = '直接发货') then
        begin
          with ADOQueryCmd do
          begin
            Close;
            Sql.Clear;
            Sql.Add('exec P_BS_Cloth_OutBYIn ');
            Sql.Add(' @BCIOID=' + quotedstr(Trim(Maxno)));
            Sql.Add(' ,@OutType=' + quotedstr(MSaveType));
            Open;
          end;
          if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
            raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
        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 TfrmTradeClothTotalInInput.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradeClothTotalInInput.FormShow(Sender: TObject);
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) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);

  SetStatus();

  with ADO_PM do
  begin
    Close;
    sql.Clear;
    sql.Add('  SELECT C_Code,C_Name FROM Bs_Cloth_Info  ');
    Open;
  end;
  with ADO_YS do
  begin
    Close;
    sql.Clear;
    sql.add(' SELECT distinct B.C_Color,B.C_ColorNo  FROM   BS_Cloth_Info_Sub B ');
 //    showmessage(sql.text);
    Open;
  end;
  with ADO_DW do
  begin
    Close;
    sql.Clear;
    sql.add(' SELECT distinct CICUNIT  FROM   Bs_Cloth_Info_Coefficient B ');

 //    showmessage(sql.text);
    Open;
  end;
end;

procedure TfrmTradeClothTotalInInput.GeMeiOutputSelClick(Sender: TObject);
begin
  try
    frmGeMeiOutputSel := TfrmGeMeiOutputSel.Create(Application);
    with frmGeMeiOutputSel do
    begin
      FSTKName := Self.FSTKName;
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Append;
          FBCIOID := frmGeMeiOutputSel.CDS_1.FieldByName('BCIOID').AsString;
          FieldByName('BCIONO').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('BCIONO').AsString;
          FieldByName('FromCoName').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('FromCoName').AsString;
          FieldByName('C_Code').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_Code').AsString;
          FieldByName('C_Name').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_Name').AsString;
          FieldByName('C_EName').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_EName').AsString;
          FieldByName('C_Spec').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_Spec').AsString;
          FieldByName('C_Width').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_Width').AsString;
          FieldByName('C_GramWeight').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_GramWeight').AsString;
          FieldByName('FtyPCId').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('FtyPCId').AsString;
          FieldByName('Piece').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('Pieceint').AsString;
          FieldByName('Qty').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('Qty').AsString;
          FieldByName('QtyUnit').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('QtyUnit').AsString;
          FieldByName('Workshop').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('Workshop').AsString;
          FieldByName('C_ColorDepth').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_ColorDepth').AsString;
          FieldByName('C_Color').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_Color').AsString;
          FieldByName('C_ColorNo').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_ColorNo').AsString;
          FieldByName('C_StyleNo').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_StyleNo').AsString;
          FieldByName('C_Composition').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_Composition').AsString;
          FieldByName('BatchNo').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('BatchNo').AsString;
          FieldByName('C_Pattern').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_Pattern').AsString;
          FieldByName('Note').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('Note').AsString;
          FieldByName('C_EColor').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_EColor').AsString;
          FieldByName('CraftCode').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('CraftCode').AsString;
          FieldByName('CraftName').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('CraftName').AsString;
          FieldByName('CraftEName').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('CraftEName').AsString;
          FieldByName('GC_Name').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('GC_Name').AsString;
          FieldByName('C_Degree').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('C_Degree').AsString;
          //FieldByName('StkConNo').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('fromConNo').AsString;


          Post;

        end;
      end;
    end;
  finally
    frmGeMeiOutputSel.Free;
  end;

end;

procedure TfrmTradeClothTotalInInput.SetStatus();
begin
  if FSTKName = '新余' then
    GeMeiOutputSel.Visible := True;

end;

procedure TfrmTradeClothTotalInInput.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;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Fin_Flow_Judge ');
      Sql.Add(' @FFIDS=' + quotedstr(Trim(CDS_Sub.fieldbyname('BCIOID').AsString)));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      Open;
    end;
    if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
    begin
      Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
      exit;
    end;
    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('BCIONO').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_In_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
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmTradeClothTotalInInput.TBSaveClick(Sender: TObject);
begin

  if CheckData() = false then
    exit;

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

procedure TfrmTradeClothTotalInInput.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(FSTKName + '入库登记', Tv1, '贸易汇总仓库');
end;

procedure TfrmTradeClothTotalInInput.ToolButton2Click(Sender: TObject);
begin
  if CheckData() = false then
    exit;

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

procedure TfrmTradeClothTotalInInput.ToolButton3Click(Sender: TObject);
begin
  if CheckData() = false then
    exit;

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

procedure TfrmTradeClothTotalInInput.ToolButton4Click(Sender: TObject);
begin
  try
    frmTradeMarketListSEL := TfrmTradeMarketListSEL.Create(Application);
    with frmTradeMarketListSEL do
    begin

      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Append;

          FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
          FieldByName('StkCoNo').Value := '本厂仓库';
          FieldByName('StkCoName').Value := '本厂仓库';
          FieldByName('FromCoNo').Value := 'G0001';
          FieldByName('FromCoName').Value := '染厂供应商';
          FieldByName('C_Degree').AsString := Trim('坯布');

//          FBCIOID := frmGeMeiOutputSel.CDS_1.FieldByName('BCIOID').AsString;
          FieldByName('BCIONO').AsString := frmTradeMarketListSEL.CDS_1.FieldByName('tmmno').AsString;
//          FieldByName('FromCoName').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('FromCoName').AsString;
          FieldByName('C_Code').AsString := frmTradeMarketListSEL.CDS_1.FieldByName('C_Code').AsString;
          FieldByName('C_Name').AsString := frmTradeMarketListSEL.CDS_1.FieldByName('C_Name').AsString;

          FieldByName('C_Spec').AsString := frmTradeMarketListSEL.CDS_1.FieldByName('C_Spec').AsString;
          FieldByName('C_Width').AsString := frmTradeMarketListSEL.CDS_1.FieldByName('C_Width').AsString;
          FieldByName('C_GramWeight').AsString := frmTradeMarketListSEL.CDS_1.FieldByName('C_GramWeight').AsString;
//          FieldByName('FtyPCId').AsString := frmGeMeiOutputSel.CDS_1.FieldByName('FtyPCId').AsString;
          FieldByName('Piece').AsString := '0';
          FieldByName('Qty').AsString := '0';
          FieldByName('QtyUnit').AsString := frmTradeMarketListSEL.CDS_1.FieldByName('OrderUnit').AsString;

          FieldByName('C_Color').AsString := frmTradeMarketListSEL.CDS_1.FieldByName('C_Color').AsString;
          FieldByName('C_ColorNO').AsString := frmTradeMarketListSEL.CDS_1.FieldByName('C_ColorNO').AsString;

          FieldByName('tmmid').AsString := frmTradeMarketListSEL.CDS_1.FieldByName('tmmid').AsString;
          FieldByName('tmsid').AsString := frmTradeMarketListSEL.CDS_1.FieldByName('tmsid').AsString;
//          FieldByName('TMMNO').AsString := frmTradeMarketListSEL.CDS_1.FieldByName('TMMNO').AsString;

          Post;

        end;
      end;
    end;
  finally
    frmTradeMarketListSEL.Free;
  end;
end;

procedure TfrmTradeClothTotalInInput.Tv1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmTradeSalesContractSel := TfrmTradeSalesContractSel.Create(Application);
    with frmTradeSalesContractSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;

          FieldByName('FromConNo').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConNo').AsString);
          FieldByName('FromConMId').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConMId').AsString);
          FieldByName('FromConSId').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('ConSId').AsString);

          FieldByName('C_Code').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_Code').AsString);
          FieldByName('C_Name').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_Name').AsString);
          FieldByName('C_Spec').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_Spec').AsString);
          FieldByName('C_Composition').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_Composition').AsString);
          FieldByName('C_Width').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_Width').AsString);
          FieldByName('C_GramWeight').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_GramWeight').AsString);
          FieldByName('C_Color').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_Color').AsString);
          FieldByName('C_ColorNo').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_ColorNo').AsString);
          FieldByName('C_Pattern').Value := Trim(frmTradeSalesContractSel.CDS_1.fieldbyname('C_Pattern').AsString);
          Fieldbyname('CraftCode').value := frmTradeSalesContractSel.CDS_1.Fieldbyname('CraftCode').value;
          Fieldbyname('CraftName').value := frmTradeSalesContractSel.CDS_1.Fieldbyname('CraftName').value;
          Fieldbyname('CraftEName').value := frmTradeSalesContractSel.CDS_1.Fieldbyname('CraftEName').value;
          Fieldbyname('GC_Name').value := frmTradeSalesContractSel.CDS_1.Fieldbyname('GC_Name').value;
        end;
      end;
    end;
  finally
    frmTradeSalesContractSel.Free;
  end;
end;

procedure TfrmTradeClothTotalInInput.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('StkCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('StkCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmTradeClothTotalInInput.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmTradePlanSel := TfrmTradePlanSel.Create(Application);
    with frmTradePlanSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FromConNo').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('ConNo').AsString);
          FieldByName('FromConMId').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('ConMId').AsString);
          FieldByName('FromConSId').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('ConSId').AsString);

          FieldByName('FromOrdNo').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('OrderNo').AsString);
          FieldByName('FromMainId').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('MainId').AsString);
          FieldByName('FromSubId').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('SubId').AsString);

          FieldByName('FromCoNo').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('FtyNo').AsString);
          FieldByName('FromCoName').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('FtyName').AsString);

          FieldByName('C_Code').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Code').AsString);
          FieldByName('C_Name').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Name').AsString);
          FieldByName('C_Spec').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Spec').AsString);
          FieldByName('C_Composition').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Composition').AsString);
          FieldByName('C_Width').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Width').AsString);
          FieldByName('C_GramWeight').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_GramWeight').AsString);
          FieldByName('C_Color').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Color').AsString);
          FieldByName('C_ColorNo').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_ColorNo').AsString);
          FieldByName('C_Pattern').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Pattern').AsString);
          Fieldbyname('CraftCode').value := frmTradePlanSel.CDS_1.Fieldbyname('CraftCode').value;
          Fieldbyname('CraftName').value := frmTradePlanSel.CDS_1.Fieldbyname('CraftName').value;
          Fieldbyname('CraftEName').value := frmTradePlanSel.CDS_1.Fieldbyname('CraftEName').value;
          Fieldbyname('GC_Name').value := frmTradePlanSel.CDS_1.Fieldbyname('GC_Name').value;
        end;
      end;
    end;
  finally
    frmTradePlanSel.Free;
  end;
end;

procedure TfrmTradeClothTotalInInput.Tv1Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  if Trim(CDS_Sub.FieldByName('C_Name').AsString) = '' then
  begin
    Application.MessageBox('请先输入品名!', '提示', 0);
    //ModalResult:=1;
    with CDS_Sub do
    begin
      Edit;
      FieldByName('C_ColorNo').AsString := '';
      Post;
    end;
    Exit;
  end;

  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;

  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    ADO_YS.Locate('C_ColorNo', mvalue, []);
    Edit;
    FieldByName('C_Color').Value := ADO_YS.FieldByName('C_Color').Value;
//    FieldByName('C_Code').Value := ADO_pm.FieldByName('C_Code').Value;
    Post;
  end;
end;

procedure TfrmTradeClothTotalInInput.Tv1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('C_Code').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
          FieldByName('C_Name').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
          FieldByName('C_Spec').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
          FieldByName('C_Composition').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
          FieldByName('C_Width').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
          FieldByName('C_GramWeight').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);

          Fieldbyname('C_Pattern').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Pattern').AsString);

          Fieldbyname('CraftCode').value := frmClothInfoSel.CDS_1.Fieldbyname('CraftCode').value;
          Fieldbyname('CraftName').value := frmClothInfoSel.CDS_1.Fieldbyname('CraftName').value;
          Fieldbyname('CraftEName').value := frmClothInfoSel.CDS_1.Fieldbyname('CraftEName').value;
          Fieldbyname('GC_Name').value := frmClothInfoSel.CDS_1.Fieldbyname('GC_Name').value;

          Fieldbyname('C_ColorNo').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_ColorNo').AsString);
          Fieldbyname('C_Color').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_Color').AsString);
        end;

      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;
end;

procedure TfrmTradeClothTotalInInput.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if not ADO_PM.IsEmpty then
  begin
    if ADO_PM.FieldByName('C_Name').AsString <> CDS_sub.FieldByName('C_Name').AsString then
    begin
      with ADO_PM do
      begin
        Close;
        sql.Clear;
        sql.Add(' SELECT C_Code,C_Name FROM  Bs_Cloth_Info ');
        //sql.Add(' where C_Code= ' + QuotedStr(CDS_sub.FieldByName('C_Code').AsString));
        Open;
      end;
    end;
  end
  else

    with ADO_PM do
    begin
      Close;
      sql.Clear;
      sql.Add(' SELECT C_Code,C_Name FROM  Bs_Cloth_Info  ');
      //sql.Add(' where C_Code= ' + QuotedStr(CDS_sub.FieldByName('C_Code').AsString));
      Open;
    end;
end;

procedure TfrmTradeClothTotalInInput.v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FromCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('FromCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);

          FieldByName('SalesER').Value := Trim(CDS_1.fieldbyname('SalesER').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmTradeClothTotalInInput.v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JWLYCLName';
      flagname := '品名';
      MainType := FSTKName;
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('C_Name').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmTradeClothTotalInInput.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'StkPosition';
      flagname := '库位';
      MainType := '贸易布匹';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('StkPosition').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradeClothTotalInInput.v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C_Spec';
      flagname := '规格';
      MainType := FSTKName;
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('C_Spec').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradeClothTotalInInput.v1QtyUnitPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
//  if Trim(CDS_Sub.FieldByName('C_Name').AsString) = '' then
//  begin
//    Application.MessageBox('请先输入品名!', '提示', 0);
//    //ModalResult:=1;
//    with CDS_Sub do
//    begin
//      Edit;
//      FieldByName('QtyUnit').AsString := '';
//      Post;
//    end;
//    Exit;
//  end;
//
//  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
//  mvalue := TcxTextEdit(Sender).EditingText;
//
//  with CDS_Sub do
//  begin
//    Edit;
//    FieldByName(FFieldName).Value := Trim(mvalue);
//    Post;
//    ADO_dw.Locate('CICUnit', mvalue, []);
//    Edit;
//    FieldByName('QtyUnit').Value := ADO_YS.FieldByName('CICUnit').Value;
////    FieldByName('C_Code').Value := ADO_pm.FieldByName('C_Code').Value;
//    Post;
//  end;
end;

procedure TfrmTradeClothTotalInInput.v1SPNamePropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;

  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    ADO_pm.Locate('C_Name', mvalue, []);
    Edit;
    FieldByName('C_Name').Value := ADO_pm.FieldByName('C_Name').Value;
    FieldByName('C_Code').Value := ADO_pm.FieldByName('C_Code').Value;
    Post;
  end;
  with ADO_YS do
  begin
    Close;
    sql.Clear;
    sql.Add(' SELECT DISTINCT A.C_Code,B.C_Color,B.C_ColorNo FROM  Bs_Cloth_Info A inner join BS_Cloth_Info_Sub B on A.BCIID=B.BCIID ');
    sql.Add(' where A.C_Code= ' + QuotedStr(CDS_Sub.FieldByName('C_Code').AsString));
    SQL.ADD(' order by B.C_ColorNo');
    Open;
  end;
  with ADO_DW do
  begin
    Close;
    sql.Clear;
    sql.Add(' SELECT  * FROM V_QTYUNIT A ');
    sql.Add(' where A.C_Code= ' + QuotedStr(CDS_Sub.FieldByName('C_Code').AsString));

    Open;
  end;
  if ADO_DW.RecordCount = 1 then
  begin
    with CDS_Sub do
    begin
      Edit;
      FieldByName('qtyunit').Value := Trim(ADO_DW.FieldByName('CICUnit').Value);
      Post;

    end;
  end;

end;

end.

