unit U_YarnInEdit;

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, ComObj,
  dxScrollbarAnnotations, dxSkinsCore, dxSkinsDefaultPainters, cxImageList,
  dxSkinOffice2013White, dxSkinSharpPlus, dxSkinWXI, cxCurrencyEdit;

type
  TfrmYarnInEdit = class(TfrmBaseInput)
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1IOTime: TcxGridDBColumn;
    Tv1Y_Name: TcxGridDBColumn;
    Tv2Qty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Tv1IOType: TcxGridDBColumn;
    DS_1: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    Tv1Note: TcxGridDBColumn;
    Tv1BYIOID: TcxGridDBColumn;
    Tv1QtyUnit: TcxGridDBColumn;
    Tv1BatchNo: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Piece: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Tv1Y_Composition: TcxGridDBColumn;
    Tv1Y_Color: TcxGridDBColumn;
    Tv1Y_Code: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    OpenDialog1: TOpenDialog;
    CDS_LM: TClientDataSet;
    Tv1FromCoName: TcxGridDBColumn;
    ToolButton5: TToolButton;
    Tv1FF: TcxGridDBColumn;
    Tv1dbWeight: TcxGridDBColumn;
    Tv1dbNumber: TcxGridDBColumn;
    Tv1singleWeight: TcxGridDBColumn;
    Tv1SXCPM: TcxGridDBColumn;
    Tv1SXCBH: TcxGridDBColumn;
    Tv1FromPurNo: TcxGridDBColumn;
    Tv1StkCoName: TcxGridDBColumn;
    Tv1ZS: TcxGridDBColumn;
    Tv1FromConNo: TcxGridDBColumn;
    Tv1Denier: TcxGridDBColumn;
    Tv1FromConMId: TcxGridDBColumn;
    Tv1FromConSId: TcxGridDBColumn;
    Tv1FromOrdNo: TcxGridDBColumn;
    Tv1FromMainId: TcxGridDBColumn;
    Tv1FromSubId: TcxGridDBColumn;
    Tv1FromPurMId: TcxGridDBColumn;
    Tv1FromPurSId: TcxGridDBColumn;
    Tv1FromCoNo: TcxGridDBColumn;
    Tv1StkCoNo: 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 v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1FromConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId, canshu3, FSTKName, FKHName: string;
  end;

var
  frmYarnInEdit: TfrmYarnInEdit;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_YarnInfoSel, U_CompanySel,
  U_YarnPurchasePlanSel, U_PurchaseContractSel, U_TradeSalesContractSel;

{$R *.dfm}

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

procedure TfrmYarnInEdit.TBAddClick(Sender: TObject);
begin
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      FSTKName := Self.FSTKName;
      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('QtyUnit').Value := 'Kg';
          FieldByName('Brand').Value := CDS_1.fieldbyname('Brand').value;
          FieldByName('Y_Type').Value := CDS_1.fieldbyname('Y_Type').value;
          FieldByName('Y_Code').Value := CDS_1.fieldbyname('Y_Code').value;
          FieldByName('Y_Name').Value := CDS_1.fieldbyname('Y_Name').value;
          FieldByName('Y_Spec').Value := CDS_1.fieldbyname('Y_Spec').value;
          FieldByName('Y_Composition').Value := CDS_1.fieldbyname('Y_Composition').value;
          FieldByName('Denier').Value := CDS_1.fieldbyname('Denier').value;
          FieldByName('Y_Color').Value := CDS_1.fieldbyname('Y_Color').value;

          FieldByName('ZS').Value := CDS_1.fieldbyname('ZS').value;

          FieldByName('FF').Value := CDS_1.fieldbyname('FF').value;
          FieldByName('ND').Value := CDS_1.fieldbyname('ND').value;
          FieldByName('SXCBH').Value := CDS_1.fieldbyname('SX_SupplierNo').value;
          FieldByName('SXCPM').Value := CDS_1.fieldbyname('SX_Supplier').value;
        end;
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;

end;

function TfrmYarnInEdit.SaveCKData(): Boolean;
var
  Maxno, MBYIOID: 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_Yarn_IO where BYIOID=''' + Trim(CDS_Sub.fieldbyname('BYIOID').AsString) + '''');
          open;
        end;
        MBYIOID := Trim(ADOQueryTemp.fieldbyname('BYIOID').AsString);
        if Trim(MBYIOID) = '' then
        begin
          if not GetLSNo(ADOQueryCmd, Maxno, 'YR', 'BS_Yarn_IO', 3, 1) then
            raise Exception.Create('取入库编号失败！');
        end
        else
        begin
          Maxno := Trim(MBYIOID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Yarn_IO where BYIOID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MBYIOID) = '' 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_Yarn_IO', 0);
//          FieldByName('FromCoNo').Value := CDS_Sub.fieldbyname('FromCoNo').Value;
//          FieldByName('StkCoNo').Value := CDS_Sub.fieldbyname('StkCoNo').Value;
//          FieldByName('FromOrdNo').Value := CDS_Sub.fieldbyname('FromOrdNo').value;
//          FieldByName('FromMainId').Value := CDS_Sub.fieldbyname('FromMainId').value;
//          FieldByName('FromSubId').Value := CDS_Sub.fieldbyname('FromSubId').value;
//          FieldByName('FromPurMId').Value := CDS_Sub.fieldbyname('FromPurMId').Value;
//          FieldByName('FromPurSId').Value := CDS_Sub.fieldbyname('FromPurSId').Value;
//          FieldByName('FromPurNo').Value := CDS_Sub.fieldbyname('FromPurNo').Value;


          FieldByName('fromconmid').Value := CDS_Sub.fieldbyname('fromconmid').value;
          FieldByName('fromconsid').Value := CDS_Sub.fieldbyname('fromconsid').value;

          FieldByName('BYIOID').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_Yarn_IO Set StkPiece=(select Sum(isnull(Piece,0)*IOQtyFlag) from BS_Yarn_IO A where A.STKID=BS_Yarn_IO.BYIOID)');
          sql.Add(',STKQty=(select Sum(isnull(Qty,0)*IOQtyFlag) from BS_Yarn_IO A where A.STKID=BS_Yarn_IO.BYIOID )');
          sql.Add('where BYIOID=''' + Trim(Maxno) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Yarn_IO where BYIOID=''' + 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('BYIOID').Value := Trim(Maxno);
        Post;
        Next;
      end;
    end;

    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;

    Result := True;
  except
    Result := False;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmYarnInEdit.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmYarnInEdit.FormDestroy(Sender: TObject);
begin
  inherited;
  frmYarnInEdit := nil;
end;

procedure TfrmYarnInEdit.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(FSTKName + '入库登记', Tv1, '纱线仓库');

  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_Yarn_IO A');
    sql.Add(' where BYIOID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);
end;

procedure TfrmYarnInEdit.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('BYIOID').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('BYIOID').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('BYIOID').AsString)));
        sql.Add(',''删除''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(Trim(CDS_Sub.fieldbyname('BYIOID').AsString)));
        sql.Add(')  ');

        Sql.Add('exec P_BS_Yarn_In_Del ');
        Sql.Add(' @BYIOIDS=' + quotedstr(Trim(CDS_Sub.fieldbyname('BYIOID').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 TfrmYarnInEdit.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 CDS_Sub.Locate('Y_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 TfrmYarnInEdit.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(FSTKName + '入库登记', Tv1, '纱线仓库');

end;

procedure TfrmYarnInEdit.ToolButton2Click(Sender: TObject);
begin
  try
    frmYarnPurchasePlanSel := TfrmYarnPurchasePlanSel.Create(Application);
    with frmYarnPurchasePlanSel do
    begin
      if ShowModal = 1 then
      begin
        with CDS_1 do
        begin
          First;
          while not Eof do
          begin
            if CDS_1.fieldbyname('SSel').AsBoolean then
            begin
              with Self.CDS_Sub do
              begin
                Append;

                FieldByName('IOType').Value := '采购入库';
                FieldByName('FromCoName').Value := CDS_1.fieldbyname('SellName').value;//来自单位  供应商
                FieldByName('FromCoNo').Value := CDS_1.fieldbyname('SellNo').value;
                //入库织造单
//                FieldByName('FromOrdNo').Value := CDS_1.fieldbyname('FromOrdNo').value;
//                FieldByName('FromMainId').Value := CDS_1.fieldbyname('FromMainId').value;
//                FieldByName('FromSubId').Value := CDS_1.fieldbyname('FromSubId').value;
//                出库织造单
//                FieldByName('ToOrdNo').Value := CDS_1.fieldbyname('PurNo').value;
//                FieldByName('ToMainId').Value := CDS_1.fieldbyname('PurNo').value;
//                FieldByName('ToSubId').Value := CDS_1.fieldbyname('PurNo').value;
                //入库坯布合同号
                FieldByName('FromConNo').Value := CDS_1.fieldbyname('ConNo').value;
                FieldByName('fromConMId').Value := CDS_1.fieldbyname('fromConMId').value;
                FieldByName('fromConSId').Value := CDS_1.fieldbyname('fromConSId').value;
                //出库坯布合同号
//                 FieldByName('ToOrdNo').Value := CDS_1.fieldbyname('PurNo').value;
//                FieldByName('ToMainId').Value := CDS_1.fieldbyname('PurNo').value;
//                FieldByName('ToSubId').Value := CDS_1.fieldbyname('PurNo').value;
                //入库采购计划单号
                FieldByName('FromPurNo').Value := CDS_1.fieldbyname('PurNo').value;
                FieldByName('FromPurMId').Value := CDS_1.fieldbyname('PurMId').value;
                FieldByName('FromPurSId').Value := CDS_1.fieldbyname('PurSId').value;

                FieldByName('Y_Code').Value := CDS_1.fieldbyname('Y_Code').value;
                FieldByName('Y_Name').Value := CDS_1.fieldbyname('Y_Name').value;
                FieldByName('Y_Composition').Value := CDS_1.fieldbyname('Y_Composition').value;
                FieldByName('Y_Color').Value := CDS_1.fieldbyname('Y_Color').value;
                FieldByName('Denier').Value := CDS_1.fieldbyname('Denier').value;
                FieldByName('ZS').Value := CDS_1.fieldbyname('ZS').value;
                FieldByName('FF').Value := CDS_1.fieldbyname('FF').value;
                FieldByName('Qty').Value := CDS_1.fieldbyname('Qty').value;
                FieldByName('Price').Value := CDS_1.fieldbyname('Price').value;
                FieldByName('QtyUnit').Value := CDS_1.fieldbyname('QtyUnit').value;
                FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
//                FieldByName('StkCoNo').Value := Trim(CDS_1.fieldbyname('BuyNo').AsString);  //存放单位
//                FieldByName('StkCoName').Value := CDS_1.fieldbyname('BuyName').value;
                FieldByName('StkCoNo').Value := '本厂仓库';  //存放单位
                FieldByName('StkCoName').Value := '本厂仓库';












//                FieldByName('FromOrdNo').Value := CDS_1.fieldbyname('PurNo').value;
//                FieldByName('FromMainId').Value := CDS_1.fieldbyname('PurMId').value;
//                FieldByName('FromSubId').Value := CDS_1.fieldbyname('PurSId').value;

//                FieldByName('FromPurMId').Value := CDS_1.fieldbyname('PurMId').value;
//                FieldByName('FromPurSId').Value := CDS_1.fieldbyname('PurSId').value;

              end;
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmYarnPurchasePlanSel.Free;
  end;
end;

procedure TfrmYarnInEdit.ToolButton3Click(Sender: TObject);
begin
  try
    frmPurchaseContractSel := TfrmPurchaseContractSel.Create(Application);
    with frmPurchaseContractSel do
    begin
      FPrtType := '纱线';
      if ShowModal = 1 then
      begin
        with frmPurchaseContractSel.CDS_1 do
        begin
          DisableControls;
          First;
          while not eof do
          begin
            if frmPurchaseContractSel.CDS_1.fieldbyname('SSel').AsBoolean then
            begin
              with Self.CDS_Sub do
              begin
                append;
                FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
                FieldByName('BYIOID').Value := null;

                if Trim(frmPurchaseContractSel.CDS_1.fieldbyname('Sellno').AsString) = '采购' then
                  FieldByName('IOType').Value := '采购入库'
                else
                  FieldByName('IOType').Value := '加工入库';

                FieldByName('StkCoNo').Value := '本厂仓库';
                FieldByName('StkCoName').Value := '本厂仓库';
                FieldByName('FromCoNo').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('Sellno').AsString);
                FieldByName('FromCoName').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('Sellname').AsString);

                FieldByName('FromPurNo').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('ConNo').AsString);
                FieldByName('FromPurMId').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('ConMId').AsString);
                FieldByName('FromPurSId').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('ConSId').AsString);

                FieldByName('FromOrdNo').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('FromOrderNo').AsString);
                FieldByName('FromMainId').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('FromMainId').AsString);
                FieldByName('FromSubId').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('FromSubId').AsString);

                FieldByName('FromTPNID').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('FromTPNID').AsString);
                FieldByName('Y_Code').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('C_Code').AsString);
                FieldByName('Y_Name').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('C_Name').AsString);
                FieldByName('Y_Spec').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('C_Spec').AsString);
                FieldByName('Y_Composition').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('C_Composition').AsString);
                FieldByName('Y_Color').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('C_Color').AsString);
                FieldByName('QtyUnit').Value := Trim(frmPurchaseContractSel.CDS_1.fieldbyname('ConUnit').AsString);
              end;
            end;
            Next;
          end;
          EnableControls;
        end;
      end;
    end;
  finally
    frmPurchaseContractSel.Free;
  end;
end;

procedure TfrmYarnInEdit.ToolButton4Click(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, t1, t2, t3, FFID: string;
begin

  try
    excelApp := CreateOleObject('Excel.Application');
    openDialog1.Filter := '*.CSV;*.xls';
    if opendialog1.Execute then
    begin
      WorkBook := excelApp.WorkBooks.Open(OpenDialog1.FileName);
    end
    else
      exit;
    excelApp.Visible := false;
    ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
  except
    application.MessageBox('加载EXCEL错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  CDS_LM.EmptyDataSet;

  for j := 0 to Tv1.ColumnCount - 1 do
  begin
    with CDS_LM do
    begin
      Append;
      FieldByName('LCode').Value := trim(Tv1.Columns[j].DataBinding.FieldName);
      FieldByName('LName').Value := trim(Tv1.Columns[j].Caption);
      Post;
    end;
  end;

  try
    for i := 1 to 50 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[1, i].value) = '' then
        continue;
      if CDS_LM.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[1, i].value), []) then
      begin
        with CDS_LM do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    for i := 2 to ExcelRowCount do
    begin
      with CDS_Sub do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if CDS_LM.FieldByName('LXH').AsInteger > 0 then
            CDS_Sub.fieldbyname(CDS_LM.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_LM.FieldByName('LXH').AsInteger].Value;
          CDS_LM.Next;
        end;
        Post;
      end;
    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    exit;
  end;

end;

procedure TfrmYarnInEdit.ToolButton5Click(Sender: TObject);
var
  MIOType, MFromCoNo, MFromCoName, MStkCoNo, MStkCoName, MStkPosition, MSupplier: string;
begin

  MIOType := CDS_Sub.FieldByName('IOType').AsString;
  MFromCoNo := CDS_Sub.FieldByName('FromCoNo').AsString;
  MFromCoName := CDS_Sub.FieldByName('FromCoName').AsString;
  MStkCoNo := CDS_Sub.FieldByName('StkCoNo').AsString;
  MStkCoName := CDS_Sub.FieldByName('StkCoName').AsString;
  MStkPosition := CDS_Sub.FieldByName('StkPosition').AsString;
  MSupplier := CDS_Sub.FieldByName('Supplier').AsString;
  with CDS_Sub do
  begin
    DisableControls;
    while not eof do
    begin
      Edit;
      CDS_Sub.FieldByName('IOType').Value := MIOType;
      CDS_Sub.FieldByName('FromCoNo').Value := MFromCoNo;
      CDS_Sub.FieldByName('FromCoName').Value := MFromCoName;
      CDS_Sub.FieldByName('StkCoNo').Value := MStkCoNo;
      CDS_Sub.FieldByName('StkCoName').Value := MStkCoName;
      CDS_Sub.FieldByName('StkPosition').Value := MStkPosition;
      CDS_Sub.FieldByName('Supplier').Value := MSupplier;
      Post;
      Next;
    end;
    EnableControls;
  end;
end;

procedure TfrmYarnInEdit.Tv1Column10PropertiesButtonClick(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);
          Post;
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmYarnInEdit.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 := 'StkCoName';
      flagname := '存放单位';
      MainType := FSTKName;
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('StkCoName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYarnInEdit.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      FSTKName := Self.FSTKName;
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('Brand').Value := CDS_1.fieldbyname('Brand').value;
          FieldByName('Y_Code').Value := CDS_1.fieldbyname('Y_Code').value;
          FieldByName('Y_Name').Value := CDS_1.fieldbyname('Y_Name').value;
          FieldByName('Y_Spec').Value := CDS_1.fieldbyname('Y_Spec').value;
          FieldByName('Y_Composition').Value := CDS_1.fieldbyname('Y_Composition').value;
          FieldByName('Denier').Value := CDS_1.fieldbyname('Denier').value;
          FieldByName('Y_Color').Value := CDS_1.fieldbyname('Y_Color').value;
        end;
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
end;

procedure TfrmYarnInEdit.Tv1Column3PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, mFieldName: string;
  mQty, mPiece, mdbWeight: Double;        //数量，箱件，单包重量
  mdbNumber, msingleWeight: Double;        //单包个数，单个筒重

//  mCP_QTY, mCP_TPXS: Double;                 //成品数量，投坯系数
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  mFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with CDS_Sub do
  begin
    Edit;
    FieldByName(mFieldName).Value := StrToFloat(Trim(mvalue));
    Post;
//    ShowMessage(Fieldbyname('TaxRate').AsString);
    mQty := Fieldbyname('Qty').AsFloat;
    mPiece := Fieldbyname('Piece').AsFloat;

    mdbNumber := Fieldbyname('dbNumber').AsFloat;


//    mCP_QTY := Fieldbyname('CP_QTY').AsFloat;
//    mCP_TPXS := Fieldbyname('CP_TPXS').AsFloat;
  end;

//  if mCP_QTY <> 0 then
//  begin
//    FQty := RoundFloat((mCP_TPXS + 1) * mCP_QTY, 0);
//  end;
  if mPiece <> 0 then
  begin
    mdbWeight := mQty / mPiece;
  end
  else
  begin
    mdbWeight := 0;
  end;

  if mdbNumber <> 0 then
  begin
    msingleWeight := mdbWeight / mdbNumber;
  end
  else
  begin
    msingleWeight := 0;
  end;

  with CDS_Sub do
  begin
    Edit;
    FieldByName('Qty').Value := mQty;
//    FieldByName('ConPrice').Value := FConPrice;
    FieldByName('Piece').Value := mPiece;

    FieldByName('dbNumber').Value := mdbNumber;
    FieldByName('dbWeight').Value := mdbWeight;
    FieldByName('singleWeight').Value := msingleWeight;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmYarnInEdit.Tv1FromConNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  FConMainId, FMPRTCode: string;
begin
  try
    frmTradeSalesContractSel := TfrmTradeSalesContractSel.Create(Application);
    with frmTradeSalesContractSel do
    begin

      FLX := '梭织生产单';

      if ShowModal = 1 then
      begin
        with 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);
          Post;
        end;

      end;
    end;
  finally
    frmTradeSalesContractSel.Free;
  end;
end;

procedure TfrmYarnInEdit.Tv1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXQTYUNIT';
      flagname := '单位';
      MainType := FSTKName;
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('QtyUnit').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYarnInEdit.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);

          if Trim(CDS_Sub.fieldbyname('IOType').AsString) = '采购入库' then
            FieldByName('Supplier').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);

        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

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

end.

