unit U_TradeSampleClothInEdit;

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, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu, U_BaseInput,
  System.ImageList, Vcl.ImgList, ComObj, dxSkinWXI, dxScrollbarAnnotations,
  dxSkinBasic, dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkroom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinOffice2016Colorful,
  dxSkinOffice2016Dark, dxSkinOffice2019Black, dxSkinOffice2019Colorful,
  dxSkinOffice2019DarkGray, dxSkinOffice2019White, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus, dxSkinSilver,
  dxSkinSpringtime, dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld,
  dxSkinTheBezier, dxSkinValentine, dxSkinVisualStudio2013Blue,
  dxSkinVisualStudio2013Dark, dxSkinVisualStudio2013Light, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue, cxContainer, dxCore, cxDateUtils,
  cxMaskEdit, Vcl.Menus, cxImageList;

type
  TfrmTradeSampleClothInEdit = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    CDS_LM: TClientDataSet;
    OpenDialog1: TOpenDialog;
    Panel1: TPanel;
    Label12: TLabel;
    Label1: TLabel;
    IOType: TcxComboBox;
    IOTime: TcxDateEdit;
    Label14: TLabel;
    FromCoName: TcxButtonEdit;
    ToolBar2: TToolBar;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1FactoryName: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    v1SPName: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    v1SPSpec: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    v1SPMF: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    GroupBox1: TGroupBox;
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    Tv2Column1: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel3: TPanel;
    Label3: TLabel;
    QtyMX: TEdit;
    edtMJXH: TEdit;
    chkMJJH: TCheckBox;
    Button1: TButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOMX: TADOQuery;
    CDS_MX: TClientDataSet;
    DSMX: TDataSource;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: 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 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 Tv1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton3Click(Sender: TObject);
    procedure FromCoNameDblClick(Sender: TObject);
    procedure FromCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure QtyMXKeyPress(Sender: TObject; var Key: Char);
    procedure Tv2DblClick(Sender: TObject);
    procedure chkMJJHClick(Sender: TObject);
    procedure Tv2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Tv1Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    { Private declarations }
    procedure initMX();
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId, canshu3, FSTKName, FKHName: string;
  end;

var
  frmTradeSampleClothInEdit: TfrmTradeSampleClothInEdit;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_TradePlanSel, U_ClothInfoSel,U_PictureUpload,
  U_TatClothInfoSel, U_LabelPrint;

{$R *.dfm}

procedure TfrmTradeSampleClothInEdit.Button1Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_MX.IsEmpty then
    Exit;
  if not CDS_MX.Locate('SSel', True, []) then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  WSql := '';
  CDS_MX.DisableControls;
  with CDS_MX do
  begin
    First;
    while not Eof do
    begin
      if CDS_MX.fieldbyname('SSel').AsBoolean then
      begin
        if WSql <> '' then
        begin
          WSql := WSql + ',' + QuotedStr(Trim(CDS_MX.fieldbyname('CIID').AsString));
        end
        else
        begin
          WSql := QuotedStr(Trim(CDS_MX.fieldbyname('CIID').AsString));
        end;
      end;
      Next;
    end;
  end;
//  ShowMessage(WSql);
  CDS_MX.Locate('SSel', True, []);
  CDS_MX.EnableControls;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeLabelPrint';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmTradeSampleClothInEdit.chkMJJHClick(Sender: TObject);
begin
  if chkMJJH.Checked then
  begin
    if CDS_MX.IsEmpty then
      edtMJXH.Text := '1'
    else
      edtMJXH.Text := IntToStr(1 + CDS_MX.RecordCount);
  end
  else
  begin
    edtMJXH.Text := '';
  end;
end;

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

procedure TfrmTradeSampleClothInEdit.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('QtyUnit').Value := 'M';
      Post;
    end;
  end;

end;

function TfrmTradeSampleClothInEdit.SaveCKData(): 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);
          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('IOType').Value := trim(IOType.text);
          FieldByName('IOTIme').Value := IOTIme.Date;

          FieldByName('CustNo').Value := CDS_Sub.fieldbyname('CustNo').Value;
          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('BCIOID').Value := Trim(Maxno);
          FieldByName('STKID').Value := Trim(Maxno);
          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 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;
        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 TfrmTradeSampleClothInEdit.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradeSampleClothInEdit.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(FSTKName + '入库登记2', Tv1, '贸易汇总仓库');

  IOTime.Date := SGetServerDate(ADOQueryTemp);

  with CDS_LM do
  begin
    FieldDefs.Clear;
    FieldDefs.Add('LXH', ftInteger, 0);
    FieldDefs.Add('lCode', ftString, 40);
    FieldDefs.Add('LName', ftString, 40);
    close;
    CreateDataSet;
  end;

  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;
  SCSHData(ADOQueryTemp, Panel1, 0);
  FromCoName.Properties.LookupItems.Text := Trim(ADOQueryTemp.FieldByName('FromCoNo').AsString);
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);

  InitMX();
end;

procedure TfrmTradeSampleClothInEdit.FromCoNameDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

procedure TfrmTradeSampleClothInEdit.FromCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        self.FromCoName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.FromCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradeSampleClothInEdit.QtyMXKeyPress(Sender: TObject; var Key: Char);
var
  fMJID: string;
  MMJXH: Integer;
begin
  if Key = #13 then
  begin

    if CDS_Sub.IsEmpty then
    begin
      application.MessageBox('产品信息没有输入，不能输入明细信息！', '提示信息', MB_ICONERROR);
      exit;
    end;

    if Trim(edtMJXH.Text) = '' then
    begin
      application.MessageBox('请输入卷号！', '提示信息', MB_ICONERROR);
      exit;
    end;

    if StrToFloatdef(Trim(QtyMX.Text), 0) = 0 then
    begin
      application.MessageBox('米数不能为0！', '提示信息', MB_ICONERROR);
      exit;
    end;

    MMJXH := StrToIntDef(edtMJXH.Text, 1);

    if chkMJJH.Checked then
    begin
      edtMJXH.Text := IntToStr(MMJXH + 1);
    end
    else
    begin
      edtMJXH.Text := '';
    end;

    if GetLSNo(ADOQueryCmd, fMJID, 'YB', 'BS_Cloth_IO_MX', 4, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    try
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select *  from BS_Cloth_IO_MX A');
        sql.Add('where 1=2');
        open;
        append;
        Fieldbyname('CIID').Value := trim(fMJID);
        Fieldbyname('PieceNo').Value := MMJXH;
        Fieldbyname('Meter').Value := StrToFloatDef(QtyMX.Text, 0);
        Fieldbyname('BCIOID').Value := trim(CDS_Sub.fieldbyname('BCIOID').AsString);
        post;
      end;

      with CDS_MX do
      begin
        append;
        Fieldbyname('CIID').Value := trim(fMJID);
        Fieldbyname('PieceNo').Value := MMJXH;
        Fieldbyname('Meter').Value := strtofloatdef(QtyMX.Text, 0);
        Fieldbyname('BCIOID').Value := trim(CDS_Sub.fieldbyname('BCIOID').AsString);
        post;
      end;

      with CDS_Sub do
      begin
        edit;
        if CDS_MX.IsEmpty then
        begin
          Fieldbyname('Piece').Value := 0;
          Fieldbyname('Qty').Value := 0;
        end
        else
        begin
          Fieldbyname('Piece').Value := tv2.DataController.Summary.FooterSummaryValues[0];
          Fieldbyname('Qty').Value := tv2.DataController.Summary.FooterSummaryValues[4];
        end;
        post;
      end;
      QtyMX.Text := '';
      if not chkMJJH.Checked then
        edtMJXH.SetFocus;
    except
    end;
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from BS_Cloth_IO_MX where CIID=''' + trim(fMJID) + '''');
      open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      application.MessageBox('保存失败！', '提示信息', MB_ICONERROR);
      exit;
    end;
  end;
end;

procedure TfrmTradeSampleClothInEdit.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('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_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 TfrmTradeSampleClothInEdit.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
//  if CDS_Sub.Locate('IOTime', null, []) = True then
//  begin
//    Application.MessageBox('入库时间不能为空!', '提示', 0);
//    Exit;
//  end;
//  if CDS_Sub.Locate('IOType', null, []) = True then
//  begin
//    Application.MessageBox('入库类型不能为空!', '提示', 0);
//    Exit;
//  end;


  if trim(IOType.text) = '' then
  begin
    Application.MessageBox('入库类型不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_Sub.Locate('C_Name', 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('QtyUnit', null, []) = True then
  begin
    Application.MessageBox('数量单位不能为空!', '提示', 0);
    Exit;
  end;

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

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

procedure TfrmTradeSampleClothInEdit.ToolButton3Click(Sender: TObject);
var
  i: Integer;
begin
  CDS_Sub.EnableControls;
  frmClothInfoSel := TfrmClothInfoSel.Create(self);
  with frmClothInfoSel do
  begin
    IsMultipleSelection := false;
    if showmodal = 1 then
    begin
      with CDS_Selok do
      begin
        DisableControls;
        CDS_Sub.DisableControls;
        First;
        while not Eof do
        begin

          CDS_Sub.Append;
          CDS_Sub.fieldbyname('C_Code').Value := frmClothInfoSel.CDS_Selok.fieldbyname('C_Code').asstring;
          CDS_Sub.fieldbyname('C_Name').Value := frmClothInfoSel.CDS_Selok.fieldbyname('C_Name').asstring;
          CDS_Sub.fieldbyname('C_EName').Value := frmClothInfoSel.CDS_Selok.fieldbyname('C_EName').asstring;
          CDS_Sub.fieldbyname('C_Spec').Value := frmClothInfoSel.CDS_Selok.fieldbyname('C_Spec').asstring;
          CDS_Sub.fieldbyname('C_Composition').Value := frmClothInfoSel.CDS_Selok.fieldbyname('C_Composition').asstring;
          CDS_Sub.fieldbyname('C_EComposition').Value := frmClothInfoSel.CDS_Selok.fieldbyname('C_EComposition').asstring;
          CDS_Sub.fieldbyname('C_Color').Value := frmClothInfoSel.CDS_Selok.fieldbyname('C_Color').asstring;
          CDS_Sub.fieldbyname('C_Width').Value := frmClothInfoSel.CDS_Selok.fieldbyname('C_Width').asstring;
          CDS_Sub.fieldbyname('C_GramWeight').Value := frmClothInfoSel.CDS_Selok.fieldbyname('C_GramWeight').asstring;
          CDS_Sub.fieldbyname('GC_Name').Value := frmClothInfoSel.CDS_Selok.fieldbyname('GC_Name').asstring;
          CDS_Sub.fieldbyname('CraftCode').Value := frmClothInfoSel.CDS_Selok.fieldbyname('CraftCode').asstring;
          CDS_Sub.fieldbyname('CraftName').Value := frmClothInfoSel.CDS_Selok.fieldbyname('CraftName').asstring;
          CDS_Sub.fieldbyname('CraftEName').Value := frmClothInfoSel.CDS_Selok.fieldbyname('CraftEName').asstring;
          CDS_Sub.fieldbyname('C_YarnSpec').Value := frmClothInfoSel.CDS_Selok.fieldbyname('C_YarnSpec').asstring;
          CDS_Sub.FieldByName('QtyUnit').Value := 'M';
          CDS_Sub.Post;

          Next;
        end;
        EnableControls;
        CDS_Sub.EnableControls;
      end;
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmTradeSampleClothInEdit.Tv1Column16PropertiesButtonClick(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('CustNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('CustName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradeSampleClothInEdit.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;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrdGYS';
      flagname := '供应商';
      V1Name.Caption := '供应商';
      V1Note.Caption := '供应商编号';
 //     MainType:=Trim(DName);
      fnote := True;
      if ShowModal = 1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('StkCoNo').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Sub.FieldByName('StkCoName').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradeSampleClothInEdit.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('FromCoNo').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('FtyNo').AsString);
          FieldByName('FromCoName').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('FtyName').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('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);
        end;
      end;
    end;
  finally
    frmTradePlanSel.Free;
  end;

end;

procedure TfrmTradeSampleClothInEdit.Tv1Column5PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmPictureUpload := TfrmPictureUpload.create(self);
    with frmPictureUpload do
    begin
      fFlileFlag :='RKTP'; //self.fFlileFlag;
      FDataId := Trim(self.CDS_Sub.fieldbyname('YPRKTP').AsString);
      FPictureName := Trim(self.CDS_Sub.fieldbyname('YPRKTP').AsString);
      FWidth := 450;
      FHeight := 600;
      FTFType := 'RKTP';
      if showmodal = 1 then
      begin
        with self.CDS_Sub do
        begin
          Edit;
          Fieldbyname('YPRKTP').Value := frmPictureUpload.FPictureName;
          Post;
        end;
      end;
    end;
  finally
    frmPictureUpload.free;
  end;
end;

procedure TfrmTradeSampleClothInEdit.Tv1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmTatClothInfoSel := TfrmTatClothInfoSel.Create(Application);
    with frmTatClothInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          edit;
          FieldByName('C_Code').Value := trim(frmTatClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
          FieldByName('C_Name').Value := trim(frmTatClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);

          FieldByName('C_Spec').Value := trim(frmTatClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
          FieldByName('C_Composition').Value := trim(frmTatClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);

          Fieldbyname('C_Color').value := trim(frmTatClothInfoSel.CDS_1.Fieldbyname('C_Color').AsString);
          Fieldbyname('C_ColorNo').value := trim(frmTatClothInfoSel.CDS_1.Fieldbyname('C_ColorNo').AsString);

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

          FieldByName('C_GramWeight').Value := trim(frmTatClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
//          FieldByName('BBNO').Value := trim(frmTatClothInfoSel.CDS_1.Fieldbyname('BBNO').AsString);
        end;

      end;
    end;
  finally
    frmTatClothInfoSel.Free;
  end;

end;

procedure TfrmTradeSampleClothInEdit.Tv2Column2PropertiesEditValueChanged(Sender: TObject);
begin
  var mvalue, FFieldName, FCIID: string;
  begin

    mvalue := TcxTextEdit(Sender).EditingText;
    FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
    FCIID := Trim(CDS_MX.fieldbyname('CIID').AsString);
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select X.* from BS_Cloth_IO_MX X  where X.CIID=''' + trim(CDS_MX.fieldbyName('CIID').AsString) + '''');
      open;
    end;

    if trim(ADOQueryTemp.fieldbyName('CIIOFlag').AsString) = '已出库' then
    begin
      Application.MessageBox('已出库不能修改数据!', '提示', 0);
      InitMX();
      mvalue := '';
    end;

    if mvalue <> '' then
    begin
      with CDS_MX do
      begin
        Edit;
        FieldByName(FFieldName).Value := mvalue;
        Post;
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' update BS_Cloth_IO_MX set Meter = ' + mvalue);
        sql.Add(' where  CIID= ' + quotedstr(Trim(CDS_MX.FieldByName('CIID').Asstring)));

        sql.Add(' update Trade_Cloth_IO set Meter = ' + mvalue);
        sql.Add(' where  StkId= ' + quotedstr(Trim(CDS_MX.FieldByName('CIID').Asstring)));

        sql.Add(' update Trade_Cloth_Stock set stkMeter=' + mvalue);
        sql.Add(' where  StkId= ' + quotedstr(Trim(CDS_MX.FieldByName('CIID').Asstring)));
        ExecSQL;
      end;

      with CDS_Sub do
      begin
        edit;
        if CDS_MX.IsEmpty then
        begin
          Fieldbyname('Piece').Value := 0;
          Fieldbyname('Qty').Value := 0;
        end
        else
        begin
          Fieldbyname('Piece').Value := tv2.DataController.Summary.FooterSummaryValues[0];
          Fieldbyname('Qty').Value := tv2.DataController.Summary.FooterSummaryValues[4];
        end;
        post;
      end;
    end;

    tv2.Controller.EditingController.ShowEdit();
    CDS_MX.Locate('CIID', FCIID, []);
  end;
end;

procedure TfrmTradeSampleClothInEdit.initMX();
begin
  if CDS_Sub.IsEmpty then
    Exit;
  try
    with ADOMX do
    begin
      close;
      sql.Clear;
      sql.Add('select * from BS_Cloth_IO_MX A');
      sql.Add('where BCIOID=' + quotedstr(trim(CDS_Sub.fieldByName('BCIOID').AsString)));
      open;
    end;
    SCreateCDS(ADOMX, CDS_MX);
    SInitCDSData(ADOMX, CDS_MX);
  except

  end;

  if chkMJJH.Checked then
  begin
    if CDS_MX.IsEmpty then
      edtMJXH.Text := '1'
    else
      edtMJXH.Text := IntToStr(1 + CDS_MX.RecordCount);
  end
  else
  begin
    edtMJXH.Text := '';
  end;

end;

procedure TfrmTradeSampleClothInEdit.Tv2DblClick(Sender: TObject);
begin
  if CDS_MX.IsEmpty then
    exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select X.*,Y.*  from BS_Cloth_IO_MX X Inner join Trade_Cloth_IO Y on X.CIID=Y.StkID where X.CIID=''' + trim(CDS_MX.fieldbyName('CIID').AsString) + '''');
    open;
  end;

  if trim(ADOQueryTemp.fieldbyName('CIIOFlag').AsString) = '已出库' then
  begin
    Application.MessageBox('已出库不能删除数据!', '提示', 0);
    exit;
  end
  else
  begin

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        Sql.Add('exec P_Trade_Cloth_ReIn ');
        Sql.Add(' @StkIds=' + quotedstr(trim(CDS_MX.FieldByName('CIID').AsString)));
        Sql.Add(',@IOIDS=' + quotedstr(trim(ADOQueryTemp.FieldByName('IOID').AsString)));
        Sql.Add(',@FillID=' + quotedstr(trim(DCode)));
        Sql.Add(',@Filler=' + quotedstr(trim(DName)));
//      ShowMessage(sql.Text);
        Open;
      end;

      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      begin
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
        sql.Add(' ' + quotedstr(trim(DName)));
        sql.Add(',getdate() ');
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(trim('布匹入库撤销')));
        sql.Add(',' + quotedstr(trim(CDS_MX.FieldByName('CIID').AsString)));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');

        sql.Add('delete from BS_Cloth_IO_MX where CIID=''' + trim(CDS_MX.fieldbyName('CIID').AsString) + '''');
        sql.Add('delete from Trade_Cloth_Stock where CIID=''' + trim(CDS_MX.fieldbyName('CIID').AsString) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_MX.Delete;
      with CDS_Sub do
      begin
        edit;
        if CDS_MX.IsEmpty then
        begin
          Fieldbyname('Piece').Value := 0;
          Fieldbyname('Qty').Value := 0;
        end
        else
        begin
          Fieldbyname('Piece').Value := tv2.DataController.Summary.FooterSummaryValues[0];
          Fieldbyname('Qty').Value := tv2.DataController.Summary.FooterSummaryValues[1];
        end;
        post;
      end;

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

  end;

end;

procedure TfrmTradeSampleClothInEdit.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;

  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrdGYS';
      flagname := '供应商';
      V1Name.Caption := '供应商';
      V1Note.Caption := '供应商编号';
 //     MainType:=Trim(DName);
      fnote := True;
      if ShowModal = 1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('FromCoNo').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Sub.FieldByName('FromCoName').Value := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradeSampleClothInEdit.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 TfrmTradeSampleClothInEdit.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'StkPosition';
      flagname := '库位';
      MainType := FSTKName;
      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 TfrmTradeSampleClothInEdit.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;

end.

