unit U_YarnPurchasePlanList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, cxPC, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, U_BaseList, RM_e_Graphic, RM_e_Jpeg,
  RM_E_llPDF, Vcl.Clipbrd, dxScrollbarAnnotations, FrameDateSel, cxContainer,
  cxProgressBar, dxSkinsCore, dxSkinsDefaultPainters, dxSkinOffice2013White,
  dxSkinSharpPlus, dxSkinWXI;

type
  TfrmYarnPurchasePlanList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    v1OrdDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    CDS_2: TClientDataSet;
    v1CustomerNoName: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column1: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v1Column4: TcxGridDBColumn;
    pm2: TPopupMenu;
    Panel1: TPanel;
    Label4: TLabel;
    Label9: TLabel;
    BuyName: TcxTextEdit;
    PurNo: TcxTextEdit;
    cxTabControl1: TcxTabControl;
    tchk: TToolButton;
    Tnochk: TToolButton;
    cxSplitter1: TcxSplitter;
    DS_2: TDataSource;
    Tqx: TToolButton;
    Tnoqx: TToolButton;
    ToolButton5: TToolButton;
    v1Column10: TcxGridDBColumn;
    Tv1ISSC: TcxGridDBColumn;
    Tv1Mnote: TcxGridDBColumn;
    GPM_2: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    btnTijiao: TToolButton;
    btnCXTijiao: TToolButton;
    v1Column13: TcxGridDBColumn;
    Label3: TLabel;
    SellName: TcxTextEdit;
    pm1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    btnWC: TToolButton;
    btnWCCX: TToolButton;
    ADOQueryImage: TADOQuery;
    ToolButton7: TToolButton;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    frmFrameDateSel1: TfrmFrameDateSel;
    Tv1PurType: TcxGridDBColumn;
    Tv1YDHQty: TcxGridDBColumn;
    Label1: TLabel;
    Y_Name: TcxTextEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    Tv1CGQty: TcxGridDBColumn;
    ADO_1: TADOQuery;
    Tv1RKQty: TcxGridDBColumn;
    Tv1RKDate: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    Tv21Column18: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    Tv21Column16: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Tv21Column11: TcxGridDBColumn;
    Tv21Column14: TcxGridDBColumn;
    Tv21Column15: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Tv2currency: TcxGridDBColumn;
    Tv2TaxRate: TcxGridDBColumn;
    Tv1C_Degree: TcxGridDBColumn;
    Tv1Y_Code: TcxGridDBColumn;
    Tv1Y_Name: TcxGridDBColumn;
    Tv1Y_Composition: TcxGridDBColumn;
    Tv1Denier: TcxGridDBColumn;
    Tv1ZS: TcxGridDBColumn;
    Tv1FF: TcxGridDBColumn;
    Tv1Y_Color: TcxGridDBColumn;
    Tv1Qty: TcxGridDBColumn;
    Tv1QtyUnit: TcxGridDBColumn;
    Tv1Price: TcxGridDBColumn;
    Tv1TaxRate: TcxGridDBColumn;
    Tv1Amount: TcxGridDBColumn;
    Tv1currency: TcxGridDBColumn;
    Tv1SDlyDate: TcxGridDBColumn;
    Tv1SNote: TcxGridDBColumn;
    Tbdelete: TToolButton;
    Tv2Conno: TcxGridDBColumn;
    Tv1ConNo: TcxGridDBColumn;
    Tv2Tax: TcxGridDBColumn;
    Tv1Tax: TcxGridDBColumn;
    Tv1TaxInclude: TcxGridDBColumn;
    Tv2TaxInclude: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure BuyNameChange(Sender: TObject);
    procedure tchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TqxClick(Sender: TObject);
    procedure TnoqxClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure btnTijiaoClick(Sender: TObject);
    procedure btnCXTijiaoClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure btnWCClick(Sender: TObject);
    procedure btnWCCXClick(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure PurNoPropertiesChange(Sender: TObject);
    procedure TextEdit(Sender: TObject);
    procedure TbdeleteClick(Sender: TObject);
  private
    fFlileFlag: string;
    procedure InitGrid();
    procedure SetStatus();
    procedure InitSub();
    procedure InitImage();

    { Private declarations }
  public
    FFInt, FCloth: Integer;
    FAuthority, FGSTT, FSubIdS: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_YarnPurchasePlanPut, U_RTFun, U_AttachmentUpload, U_LabelPrint;

{$R *.dfm}

procedure TfrmYarnPurchasePlanList.InitImage();
begin

  ADOQueryImage.Close;
  if CDS_1.IsEmpty then
    exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(trim(CDS_1.fieldbyname('SubId').AsString)));
    sql.Add('and TFType=' + quotedstr(trim('HX')));
    open;
  end;
end;

procedure TfrmYarnPurchasePlanList.InitSub();
begin
  if CDS_1.IsEmpty then
    exit;
  with ADO_1 do
  begin
    close;
    sql.Clear;
    sql.Add('select *       ');
    sql.Add(' ,YDHQty=(select sum(Qty) from BS_Yarn_IO X where X.IOFlag=''入库''  and X.FromPurSId=A.PurSId)  ');
    sql.Add('from Pur_YarnPlan_sub A  ');
    sql.Add('where PurMId =' + quotedstr((CDS_1.fieldbyname('PurMId').AsString)));
    open;
  end;
  SCreateCDS(ADO_1, CDS_2);
  SInitCDSData(ADO_1, CDS_2);
  TV2.DataController.Filter.Clear;
end;

procedure TfrmYarnPurchasePlanList.N1Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, True); //全选
end;

procedure TfrmYarnPurchasePlanList.N2Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, False); //全弃
end;

procedure TfrmYarnPurchasePlanList.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmYarnPurchasePlanList.N4Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmYarnPurchasePlanList.N5Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True);
end;

procedure TfrmYarnPurchasePlanList.N6Click(Sender: TObject);
begin
  SelOKNo(CDS_1, False);

end;

procedure TfrmYarnPurchasePlanList.PurNoPropertiesChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYarnPurchasePlanList.SetStatus();
begin
  TBAdd.Enabled := false;
  tbedit.Enabled := false;
  ToolButton2.Enabled := false;
  tchk.Enabled := false;
  tnochk.Enabled := false;
  Tqx.Enabled := false;
  TNoqx.Enabled := false;
  TBPrint.Enabled := False;
  btnTijiao.Enabled := False;
  btnCXTijiao.Enabled := False;
  btnWC.Enabled := False;
  btnWCCX.Enabled := False;
  Tbdelete.Enabled := False;
  if Trim(FAuthority) = '录入' then
  begin
    ToolButton2.Enabled := true;
    TBAdd.Enabled := true;
    TBPrint.Enabled := True;

    Tv1SDlyDate.Visible := False;
    Tv1C_Degree.Visible := False;
    Tv1Y_Code.Visible := False;
    Tv1Y_Name.Visible := False;
    Tv1Y_Composition.Visible := False;
    Tv1Y_Color.Visible := False;
    Tv1Denier.Visible := False;
    Tv1ZS.Visible := False;
    Tv1FF.Visible := False;
    Tv1Qty.Visible := False;
    Tv1QtyUnit.Visible := False;
    Tv1Price.Visible := False;
    Tv1TaxRate.Visible := False;
    Tv1Amount.Visible := False;
    Tv1currency.Visible := False;
    Tv1SNote.Visible := False;
    Tv1RKDate.Visible := False;
    Tv1RKQty.Visible := False;
    Tv1ConNo.Visible := False;
    Tv1Tax.Visible := False;
    Tv1TaxInclude.Visible := False;

    case cxTabControl1.TabIndex of
      0:
        begin
          tbedit.Enabled := true;
          Tqx.Enabled := True;
          btnTijiao.Enabled := True;
        end;
      1:
        begin
          btnCXTijiao.Enabled := True;
        end;
      2:
        begin

          btnWC.Enabled := True;
        end;
      3:
        begin
          btnWCCX.Enabled := True;
        end;
      4:
        begin
          TNoqx.Enabled := True;
          Tbdelete.Enabled := True;
        end;
    end;
  end;
  if Trim(FAuthority) = '管理' then
  begin
    ToolButton2.Enabled := true;
    TBAdd.Enabled := true;
    TBPrint.Enabled := True;

    Tv1SDlyDate.Visible := False;
    Tv1C_Degree.Visible := False;
    Tv1Y_Code.Visible := False;
    Tv1Y_Name.Visible := False;
    Tv1Y_Composition.Visible := False;
    Tv1Y_Color.Visible := False;
    Tv1Denier.Visible := False;
    Tv1ZS.Visible := False;
    Tv1FF.Visible := False;
    Tv1Qty.Visible := False;
    Tv1QtyUnit.Visible := False;
    Tv1Price.Visible := False;
    Tv1TaxRate.Visible := False;
    Tv1Amount.Visible := False;
    Tv1currency.Visible := False;
    Tv1SNote.Visible := False;
    Tv1RKDate.Visible := False;
    Tv1RKQty.Visible := False;
    Tv1ConNo.Visible := False;
    Tv1Tax.Visible := False;
    Tv1TaxInclude.Visible := False;

    case cxTabControl1.TabIndex of
      0:
        begin
          tbedit.Enabled := true;
          Tqx.Enabled := true;
          btnTijiao.Enabled := True;
        end;
      1:
        begin

          btnCXTijiao.Enabled := True;
          tchk.Enabled := True;
        end;
      2:
        begin

          tnochk.Enabled := True;
          btnWC.Enabled := True;
        end;
      3:
        begin
          btnWCCX.Enabled := True;
        end;
      4:
        begin
          TNoqx.Enabled := true;
          Tbdelete.Enabled := True;
        end;
    end;
  end;

  if Trim(FAuthority) = '仓库' then
  begin
    ToolButton2.Enabled := true;
    TBAdd.Enabled := true;
    TBPrint.Enabled := True;
    case cxTabControl1.TabIndex of
      0:
        begin
          tbedit.Enabled := true;
          Tqx.Enabled := true;
          btnTijiao.Enabled := True;
        end;
      1:
        begin

          btnCXTijiao.Enabled := True;
          tchk.Enabled := True;
        end;
      2:
        begin

          tnochk.Enabled := True;
//          btnWC.Enabled := True;
        end;
      3:
        begin
//          btnWCCX.Enabled := True;
        end;
      4:
        begin
          TNoqx.Enabled := true;
          Tbdelete.Enabled := True;
        end;
    end;
  end;

  if Trim(FAuthority) = '会计' then
  begin
    ToolButton2.Enabled := true;
    TBAdd.Enabled := true;
    TBPrint.Enabled := True;
    case cxTabControl1.TabIndex of
      0:
        begin
          tbedit.Enabled := true;
          Tqx.Enabled := true;
          btnTijiao.Enabled := True;
        end;
      1:
        begin

          btnCXTijiao.Enabled := True;
//          tchk.Enabled := True;
        end;
      2:
        begin

//          tnochk.Enabled := True;
          btnWC.Enabled := True;
        end;
      3:
        begin
          btnWCCX.Enabled := True;
        end;
      4:
        begin
          TNoqx.Enabled := true;
          Tbdelete.Enabled := True;
        end;
    end;
  end;

  if Trim(FAuthority) = '查询' then
  begin
    TBPrint.Enabled := True;
    cxGrid2.Visible := False;

    Tv1Mnote.visible := False;
    Tv1ISSC.Visible := False;
    Tv1PurType.Visible := False;
    Tv1CGQty.Visible := False;
    Tv1YDHQty.Visible := False;

  end;
end;

procedure TfrmYarnPurchasePlanList.FormDestroy(Sender: TObject);
begin
  inherited;
//  frmContractListNX:=nil;
end;

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

procedure TfrmYarnPurchasePlanList.FormCreate(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);
  frmFrameDateSel1.BegDate.Date := frmFrameDateSel1.EndDate.Date - 90;

end;

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

procedure TfrmYarnPurchasePlanList.TbdeleteClick(Sender: TObject);
var
  mPurMIds: TStringList;
  i: Integer;
begin
  if CDS_1.IsEmpty then
    Exit;

  if Trim(CDS_1.fieldbyname('Filler').AsString) <> Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;

  // 收集选中的ID
  mPurMIds := TStringList.Create;
  try
    CDS_1.DisableControls;
    try
      CDS_1.First;
      while not CDS_1.Eof do
      begin
        if CDS_1.FieldByName('SSel').AsBoolean then
        begin
          mPurMIds.Add(QuotedStr(CDS_1.FieldByName('PurMId').AsString));
        end;
        CDS_1.Next;
      end;
    finally
      CDS_1.EnableControls;
    end;

    if mPurMIds.Count = 0 then
    begin
      ShowMessage('没有选择数据！');
      Exit;
    end;

    // 使用事务确保数据一致性
    ADOQueryCmd.Connection.BeginTrans;
    try
      with ADOQueryCmd do
      begin
        // 更新删除人
        SQL.Text := 'update Pur_YarnPlan_Main set Deler = ' + QuotedStr(Trim(DName)) + ' ,Deltime = ' + QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', SGetServerDateTime(ADOQueryTemp))) + ' where PurMId IN (' + string.Join(',', mPurMIds.ToStringArray) + ')';
        ExecSQL;
        SQL.Text := 'update Pur_YarnPlan_Sub set Deler = ' + QuotedStr(Trim(DName)) + ' ,Deltime = ' + QuotedStr(FormatDateTime('yyyy-mm-dd hh:nn:ss', SGetServerDateTime(ADOQueryTemp))) + ' where PurMId IN (' + string.Join(',', mPurMIds.ToStringArray) + ')';
        ExecSQL;
        //插入备份表
        SQL.Text := 'insert into Pur_YarnPlan_Main_Del select * from Pur_YarnPlan_Main ' + ' where PurMId IN (' + string.Join(',', mPurMIds.ToStringArray) + ')';
//        showMessage(SQL.Text);
        ExecSQL;

        SQL.Text := 'insert into Pur_YarnPlan_Sub_Del select * from Pur_YarnPlan_Sub ' + ' where PurMId IN (' + string.Join(',', mPurMIds.ToStringArray) + ')';
        ExecSQL;
        // 删除主子表
        SQL.Text := 'DELETE FROM Pur_YarnPlan_Main WHERE PurMId IN (' + string.Join(',', mPurMIds.ToStringArray) + ')';
        ExecSQL;
        SQL.Text := 'DELETE FROM Pur_YarnPlan_Sub WHERE PurMId IN (' + string.Join(',', mPurMIds.ToStringArray) + ')';
        ExecSQL;
      end;

      ADOQueryCmd.Connection.CommitTrans;

      // 从CDS中移除已删除的记录
      CDS_1.Filtered := False;
      for i := 0 to mPurMIds.Count - 1 do
      begin
        if CDS_1.Locate('PurMId', mPurMIds[i], []) then
          CDS_1.Delete;
      end;

      ShowMessageFmt('成功删除 %d 条记录', [mPurMIds.Count]);
      InitGrid();
    except
      on E: Exception do
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        raise Exception.Create('删除失败：' + E.Message);
      end;
    end;

  finally
    mPurMIds.Free;
  end;
end;

procedure TfrmYarnPurchasePlanList.InitGrid();
var
  SqlStr: string;
begin

  if Trim(FAuthority) = '查询' then
  begin
    SqlStr := 'select AA.*,BB.*    ';
    SqlStr := SqlStr + ' ,Y_Name=SUBSTRING(CAST((select distinct '','' +X.Y_Name from Pur_YarnPlan_Sub X where  X.PurMId=AA.PurMId for xml path('''') ) AS VARCHAR(1000)) , 2, 1000)';
    SqlStr := SqlStr + ' ,GXHZ=SUBSTRING(CAST((select distinct '','' +X.GX from Pur_YarnPlan_Sub X where  X.PurMId=AA.PurMId for xml path('''') ) AS VARCHAR(1000)) , 2, 1000)';

    SqlStr := SqlStr + ' ,StkName2HZ=SUBSTRING(CAST((select distinct '','' +X.StkName2 from Pur_YarnPlan_Sub X where  X.PurMId=AA.PurMId for xml path('''') ) AS VARCHAR(1000)) , 2, 1000)';
    SqlStr := SqlStr + ' ,SDlyDateHZ=SUBSTRING(CAST((select distinct '','' +cast (X.SDlyDate AS VarChar) from Pur_YarnPlan_Sub X where  X.PurMId=AA.PurMId for xml path('''') ) AS VARCHAR(1000)) , 2, 1000)';

    SqlStr := SqlStr + ' ,CGQty=(select sum(Qty) from Pur_YarnPlan_Sub X where  X.PurMId=AA.PurMId)  ';

    SqlStr := SqlStr + ' ,YDHQty=(select sum(Qty) from BS_Yarn_IO X where X.IOFlag=''入库''  and X.FromPurNo=AA.PurNo)  ';
    SqlStr := SqlStr + ' ,YDHDate=(select top 1 IOTime from BS_Yarn_IO X where X.IOFlag=''入库''  and X.FromPurNo=AA.PurNo order by IOTime desc )  ';

    SqlStr := SqlStr + ' ,RKQTy=(select sum(Y.BaoQty) from BS_Yarn_IO X INNER JOIN BS_Yarn_IO_MX Y on X.BYIOID =Y.BYIOID ';
    SqlStr := SqlStr + ' where X.IOFlag=''入库''  and X.FromPurNo=AA.PurNo and isnull(StkPosition,'''') <> '''' )  ';

    SqlStr := SqlStr + ' ,YDHQty=(select sum(Qty) from BS_Yarn_IO X where X.IOFlag=''入库''  and X.FromPurSId=BB.PurSId)  ';
    SqlStr := SqlStr + ' from Pur_YarnPlan_Main AA right join Pur_YarnPlan_sub BB on AA.PurMId = BB.PurMId ';
  end
  else
  begin
    SqlStr := 'select AA.*    ';
    SqlStr := SqlStr + ' ,Y_Name=SUBSTRING(CAST((select distinct '','' +X.Y_Name from Pur_YarnPlan_Sub X where  X.PurMId=AA.PurMId for xml path('''') ) AS VARCHAR(1000)) , 2, 1000)';
    SqlStr := SqlStr + ' ,GXHZ=SUBSTRING(CAST((select distinct '','' +X.GX from Pur_YarnPlan_Sub X where  X.PurMId=AA.PurMId for xml path('''') ) AS VARCHAR(1000)) , 2, 1000)';

    SqlStr := SqlStr + ' ,StkName2HZ=SUBSTRING(CAST((select distinct '','' +X.StkName2 from Pur_YarnPlan_Sub X where  X.PurMId=AA.PurMId for xml path('''') ) AS VARCHAR(1000)) , 2, 1000)';
    SqlStr := SqlStr + ' ,SDlyDateHZ=SUBSTRING(CAST((select distinct '','' +cast (X.SDlyDate AS VarChar) from Pur_YarnPlan_Sub X where  X.PurMId=AA.PurMId for xml path('''') ) AS VARCHAR(1000)) , 2, 1000)';

    SqlStr := SqlStr + ' ,CGQty=(select sum(Qty) from Pur_YarnPlan_Sub X where  X.PurMId=AA.PurMId)  ';

    SqlStr := SqlStr + ' ,YDHQty=(select sum(Qty) from BS_Yarn_IO X where X.IOFlag=''入库''  and X.FromPurNo=AA.PurNo)  ';
    SqlStr := SqlStr + ' ,YDHDate=(select top 1 IOTime from BS_Yarn_IO X where X.IOFlag=''入库''  and X.FromPurNo=AA.PurNo order by IOTime desc )  ';

    SqlStr := SqlStr + ' ,RKQTy=(select sum(Y.BaoQty) from BS_Yarn_IO X INNER JOIN BS_Yarn_IO_MX Y on X.BYIOID =Y.BYIOID ';
    SqlStr := SqlStr + ' where X.IOFlag=''入库''  and X.FromPurNo=AA.PurNo and isnull(StkPosition,'''') <> '''' )  ';

    SqlStr := SqlStr + ' from Pur_YarnPlan_Main AA  ';
  end;

  SqlStr := SqlStr + ' where isnull(LX,'''') <> ''织造单'' and PurDate>' + quotedstr(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date));
  SqlStr := SqlStr + ' and PurDate<=' + quotedstr(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1));

  case cxTabControl1.TabIndex of
    0:
      begin
        SqlStr := SqlStr + ' and  isnull(AA.status,''0'')=''0''';
      end;
    1:
      begin
        SqlStr := SqlStr + ' and  isnull(AA.status,''0'')=''1''';
      end;
    2:
      begin
        SqlStr := SqlStr + ' and  isnull(AA.status,''0'')=''9''';
      end;
    3:
      begin
        SqlStr := SqlStr + ' and  isnull(AA.status,''0'')=''10''';
      end;
    4:
      begin
        SqlStr := SqlStr + ' and  isnull(AA.status,''0'')=''-1''';
      end;
  end;

  InitCDSData(ADOQueryMain, CDS_1, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'PurNo');

end;

procedure TfrmYarnPurchasePlanList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmYarnPurchasePlanList.TBPrintClick(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'YarnPurPrint';
      FFiltration1 := Trim(Self.CDS_1.fieldbyname('PurMId').AsString);
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmYarnPurchasePlanList.TBEditClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if Trim(CDS_1.fieldbyname('Filler').AsString) <> Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;
  try
    frmYarnPurchasePlanPut := TfrmYarnPurchasePlanPut.Create(Application);
    with frmYarnPurchasePlanPut do
    begin
      PState := '修改';
      FPurMId := Trim(Self.CDS_1.fieldbyname('PurMId').AsString);
      FAuthority := self.FAuthority;
      FGSTT := self.FGSTT;
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmYarnPurchasePlanPut.Free;
  end;
end;

procedure TfrmYarnPurchasePlanList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmYarnPurchasePlanList.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmYarnPurchasePlanPut := TfrmYarnPurchasePlanPut.Create(Application);
    with frmYarnPurchasePlanPut do
    begin
      PState := '新增';
      FPurMId := '';
      FAuthority := self.FAuthority;
      FGSTT := self.FGSTT;
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmYarnPurchasePlanPut.Free;
  end;
end;

procedure TfrmYarnPurchasePlanList.FormShow(Sender: TObject);
begin
  inherited;
  fFlileFlag := UserDataFlag + 'HX';
  FAuthority := self.fParameters1;
  if (Trim(FAuthority) = '录入') or (Trim(FAuthority) = '管理') then
  begin
    readCxGrid(trim(Self.Caption) + 'tv1录入管理', Tv1, '销售合同录入管理');
  end
  else
  begin
    readCxGrid(trim(Self.Caption) + 'tv1查询', Tv1, '销售合同查询');
  end;

//  readCxGrid(trim(Self.Caption) + 'tv1', Tv1, '销售合同管理');
  readCxGrid(trim(Self.Caption) + 'tv2', Tv2, '销售合同管理');

  SetStatus();
  InitGrid();
end;

procedure TfrmYarnPurchasePlanList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if ToolButton1.Visible = False then
    Exit;
  ToolButton1.Click;
end;

procedure TfrmYarnPurchasePlanList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmYarnPurchasePlanList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmYarnPurchasePlanList.ToolButton1Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  try
    frmYarnPurchasePlanPut := TfrmYarnPurchasePlanPut.Create(Application);
    with frmYarnPurchasePlanPut do
    begin
      PState := '查看';
      FPurMId := Trim(Self.CDS_1.fieldbyname('PurMId').AsString);
      FGSTT := self.FGSTT;
      ToolBar2.Visible := False;
      TBSave.Visible := False;
//      ScrollBox1.Enabled := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmYarnPurchasePlanPut.Free;
  end;
end;

procedure TfrmYarnPurchasePlanList.ToolButton2Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  try
    frmYarnPurchasePlanPut := TfrmYarnPurchasePlanPut.Create(Application);
    with frmYarnPurchasePlanPut do
    begin
      PState := '复制';
      FPurMId := Trim(Self.CDS_1.fieldbyname('PurMId').AsString);
      FAuthority := self.FAuthority;
      fFlileFlag := self.fFlileFlag;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmYarnPurchasePlanPut.Free;
  end;
end;

procedure TfrmYarnPurchasePlanList.btnWCClick(Sender: TObject);
var
  MPurMIdS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  with CDS_1 do
  begin
    DisableControls;
    First;
    MPurMIdS := '';
    while not eof do
    begin
      if fieldbyname('SSel').AsBoolean then
      begin
        MPurMIdS := MPurMIdS + trim(fieldbyname('PurMId').AsString) + ',';
      end;
      next;
    end;
    MPurMIdS := copy(MPurMIdS, 1, Length(MPurMIdS) - 1);
    EnableControls;
  end;

  try
    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('采购计划ID：' + trim(MPurMIdS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');

      sql.Add(' exec P_Pur_YarnPlan_SetStatus ');
      sql.Add(' @PurMIdS=' + QuotedStr(MPurMIdS));
      sql.Add(',@Operation=''完成'' ');
      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)));
    application.MessageBox('完成成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmYarnPurchasePlanList.BuyNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmYarnPurchasePlanList.tchkClick(Sender: TObject);
var
  MPurMIdS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  with CDS_1 do
  begin
    DisableControls;
    First;
    MPurMIdS := '';
    while not eof do
    begin
      if fieldbyname('SSel').AsBoolean then
      begin
        MPurMIdS := MPurMIdS + trim(fieldbyname('PurMId').AsString) + ',';
      end;
      next;
    end;
    MPurMIdS := copy(MPurMIdS, 1, Length(MPurMIdS) - 1);
    EnableControls;
  end;

  try
    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('采购计划ID：' + trim(MPurMIdS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');

      sql.Add(' exec P_Pur_YarnPlan_SetStatus ');
      sql.Add(' @PurMIdS=' + QuotedStr(MPurMIdS));
      sql.Add(',@Operation=''审核'' ');
      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)));
    application.MessageBox('审核成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmYarnPurchasePlanList.TnochkClick(Sender: TObject);
var
  MPurMIdS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  with CDS_1 do
  begin
    DisableControls;
    First;
    MPurMIdS := '';
    while not eof do
    begin
      if fieldbyname('SSel').AsBoolean then
      begin
        MPurMIdS := MPurMIdS + trim(fieldbyname('PurMId').AsString) + ',';
      end;
      next;
    end;
    MPurMIdS := copy(MPurMIdS, 1, Length(MPurMIdS) - 1);
    EnableControls;
  end;

  try
    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('采购计划ID：' + trim(MPurMIdS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');

      sql.Add(' exec P_Pur_YarnPlan_SetStatus ');
      sql.Add(' @PurMIdS=' + QuotedStr(MPurMIdS));
      sql.Add(',@Operation=''撤销审核'' ');
      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)));
    application.MessageBox('审核成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmYarnPurchasePlanList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  TBRafresh.Click;
end;

procedure TfrmYarnPurchasePlanList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  if Trim(FAuthority) <> '查询' then
  begin
    InitSub();
  end;

end;

procedure TfrmYarnPurchasePlanList.TextEdit(Sender: TObject);
var
  mvalue, FFieldName: string;
  FSJPrice: Double;
begin

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

  if mvalue = '' then
  begin
    mvalue := '0';
  end;

  FSJPrice := RoundFloat(StrToFloat(mvalue) / CDS_2.FieldByName('Qty').asfloat, 2);
  with CDS_2 do
  begin
    Edit;
    FieldByName('SJMoney').Value := mvalue;
    FieldByName('SJPrice').Value := FSJPrice;
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('  update Pur_YarnPlan_Sub set SJMoney=' + mvalue + '');
    sql.Add('  , SJPrice=' + FloatToStr(FSJPrice) + '');
    sql.Add('  where PurSId=' + quotedstr(Trim(CDS_2.FieldByName('PurSId').AsString)));
//    ShowMessage(sql.text);
    ExecSQL;
  end;
  Tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmYarnPurchasePlanList.TqxClick(Sender: TObject);
var
  MPurMIdS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  with CDS_1 do
  begin
    DisableControls;
    First;
    MPurMIdS := '';
    while not eof do
    begin
      if fieldbyname('SSel').AsBoolean then
      begin
        MPurMIdS := MPurMIdS + trim(fieldbyname('PurMId').AsString) + ',';
      end;
      next;
    end;
    MPurMIdS := copy(MPurMIdS, 1, Length(MPurMIdS) - 1);
    EnableControls;
  end;

  try
    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('采购计划ID：' + trim(MPurMIdS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');

      sql.Add(' exec P_Pur_YarnPlan_SetStatus ');
      sql.Add(' @PurMIdS=' + QuotedStr(MPurMIdS));
      sql.Add(',@Operation=''取消'' ');
      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)));
    application.MessageBox('取消成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmYarnPurchasePlanList.TnoqxClick(Sender: TObject);
var
  MPurMIdS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  with CDS_1 do
  begin
    DisableControls;
    First;
    MPurMIdS := '';
    while not eof do
    begin
      if fieldbyname('SSel').AsBoolean then
      begin
        MPurMIdS := MPurMIdS + trim(fieldbyname('PurMId').AsString) + ',';
      end;
      next;
    end;
    MPurMIdS := copy(MPurMIdS, 1, Length(MPurMIdS) - 1);
    EnableControls;
  end;

  try
    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('采购计划ID：' + trim(MPurMIdS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');

      sql.Add(' exec P_Pur_YarnPlan_SetStatus ');
      sql.Add(' @PurMIdS=' + QuotedStr(MPurMIdS));
      sql.Add(',@Operation=''撤销取消'' ');
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
//      ShowMessage(sql.Text);
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    application.MessageBox('撤销取消成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmYarnPurchasePlanList.ToolButton5Click(Sender: TObject);   //附件
begin
  if CDS_1.IsEmpty then
  begin
    Application.MessageBox('请先选择数据！', '提示信息', MB_OK);
    Exit;
  end;

  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      fkeyNO := Trim(Self.CDS_1.fieldbyname('PurMId').AsString);
      fType := '合同';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;
end;

procedure TfrmYarnPurchasePlanList.ToolButton7Click(Sender: TObject);
begin
  if (Trim(FAuthority) = '录入') or (Trim(FAuthority) = '管理') then
  begin
    writeCxGrid(trim(Self.Caption) + 'tv1录入管理', Tv1, '销售合同录入管理');
  end
  else
  begin
    writeCxGrid(trim(Self.Caption) + 'tv1查询', Tv1, '销售合同查询');
  end;
//  writeCxGrid(trim(Self.Caption) + 'tv1', Tv1, '销售合同管理');
  writeCxGrid(trim(Self.Caption) + 'tv2', Tv2, '销售合同管理');
end;

procedure TfrmYarnPurchasePlanList.btnWCCXClick(Sender: TObject);
var
  MPurMIdS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  with CDS_1 do
  begin
    DisableControls;
    First;
    MPurMIdS := '';
    while not eof do
    begin
      if fieldbyname('SSel').AsBoolean then
      begin
        MPurMIdS := MPurMIdS + trim(fieldbyname('PurMId').AsString) + ',';
      end;
      next;
    end;
    MPurMIdS := copy(MPurMIdS, 1, Length(MPurMIdS) - 1);
    EnableControls;
  end;

  try
    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('采购计划ID：' + trim(MPurMIdS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');

      sql.Add(' exec P_Pur_YarnPlan_SetStatus ');
      sql.Add(' @PurMIdS=' + QuotedStr(MPurMIdS));
      sql.Add(',@Operation=''撤销完成'' ');
      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)));
    application.MessageBox('撤销完成成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmYarnPurchasePlanList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('status').Index] = '2' then
    ACanvas.Brush.Color := clLime;
end;

procedure TfrmYarnPurchasePlanList.btnTijiaoClick(Sender: TObject);
var
  MPurMIdS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  with CDS_1 do
  begin
    DisableControls;
    First;
    MPurMIdS := '';
    while not eof do
    begin
      if fieldbyname('SSel').AsBoolean then
      begin
        MPurMIdS := MPurMIdS + trim(fieldbyname('PurMId').AsString) + ',';
      end;
      next;
    end;
    MPurMIdS := copy(MPurMIdS, 1, Length(MPurMIdS) - 1);
    EnableControls;
  end;

  try
    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('采购计划ID：' + trim(MPurMIdS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');

      sql.Add(' exec P_Pur_YarnPlan_SetStatus ');
      sql.Add(' @PurMIdS=' + QuotedStr(MPurMIdS));
      sql.Add(',@Operation=''提交'' ');
      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)));
    application.MessageBox('提交成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmYarnPurchasePlanList.btnCXTijiaoClick(Sender: TObject);
var
  MPurMIdS: string;
begin
  if CDS_1.IsEmpty then
    exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
  with CDS_1 do
  begin
    DisableControls;
    First;
    MPurMIdS := '';
    while not eof do
    begin
      if fieldbyname('SSel').AsBoolean then
      begin
        MPurMIdS := MPurMIdS + trim(fieldbyname('PurMId').AsString) + ',';
      end;
      next;
    end;
    MPurMIdS := copy(MPurMIdS, 1, Length(MPurMIdS) - 1);
    EnableControls;
  end;

  try
    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('采购计划ID：' + trim(MPurMIdS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      sql.Add(' exec P_Pur_YarnPlan_SetStatus ');
      sql.Add(' @PurMIdS=' + QuotedStr(MPurMIdS));
      sql.Add(',@Operation=''撤销提交'' ');
      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)));
    application.MessageBox('撤销提交成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

end.

