unit U_JiangLiaoSet;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, ADODB, DBClient,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGrid, cxPC, StdCtrls, ComCtrls, ExtCtrls,
  ToolWin, cxCheckBox, Menus, cxTextEdit, RM_Common, RM_Class,
  RM_GridReport, RM_System, RM_Dataset, RM_e_Xls, cxContainer, cxMaskEdit,
  cxButtonEdit;

type
  TfrmJiangLiaoSet = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    OrderNo: TEdit;
    cxTabControl1: TcxTabControl;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    DSOrdNo: TDataSource;
    ADOCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DSSub: TDataSource;
    CDS_Sub: TClientDataSet;
    ToolButton1: TToolButton;
    PopupMenu3: TPopupMenu;
    MenuItem3: TMenuItem;
    MenuItem4: TMenuItem;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    RMDBMain: TRMDBDataSet;
    RM1: TRMGridReport;
    ADOQueryPrint: TADOQuery;
    RMXLSExport1: TRMXLSExport;
    Label10: TLabel;
    CustomNoName: TEdit;
    Label11: TLabel;
    Label12: TLabel;
    cxGridPopupMenu3: TcxGridPopupMenu;
    TvOrdNo: TcxGridDBTableView;
    cxGrid4Level1: TcxGridLevel;
    cxGrid4: TcxGrid;
    vOrderBakColumn1: TcxGridDBColumn;
    vOrderBakColumn2: TcxGridDBColumn;
    vOrderBakColumn3: TcxGridDBColumn;
    vOrderBakColumn4: TcxGridDBColumn;
    vOrderBakColumn5: TcxGridDBColumn;
    RMDBZY: TRMDBDataSet;
    RMDBFZ: TRMDBDataSet;
    SWFBColor: TComboBox;
    vOrdNoColumn1: TcxGridDBColumn;
    Label18: TLabel;
    ZuHeID: TEdit;
    vOrdNoColumn2: TcxGridDBColumn;
    vOrdNoColumn3: TcxGridDBColumn;
    Panel2: TPanel;
    Label4: TLabel;
    Label5: TLabel;
    Label14: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    MCQty: TEdit;
    WFBCpSpec: TcxButtonEdit;
    WKMS: TcxButtonEdit;
    SWFBHW: TcxButtonEdit;
    BtnSave: TButton;
    BtnPrint: TButton;
    BtnChkNo: TButton;
    BtnChkOk: TButton;
    Panel3: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGrid1Level4: TcxGridLevel;
    cxGrid3: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column1: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    ADOTemp: TADOQuery;
    DSJYFZ: TDataSource;
    CDS_JYFZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    v1Column2: TcxGridDBColumn;
    Panel4: TPanel;
    Panel5: TPanel;
    Panel8: TPanel;
    MCNote: TRichEdit;
    Panel6: TPanel;
    OrderNoSL: TRichEdit;
    ToolButton2: TToolButton;
    v2Column2: TcxGridDBColumn;
    Label6: TLabel;
    Label13: TLabel;
    MCQty10: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure MenuItem3Click(Sender: TObject);
    procedure MenuItem4Click(Sender: TObject);
    procedure BtnSaveClick(Sender: TObject);
    procedure TvOrdNoCellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure MCQtyKeyPress(Sender: TObject; var Key: Char);
    procedure BtnChkOkClick(Sender: TObject);
    procedure BtnChkNoClick(Sender: TObject);
    procedure BtnPrintClick(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure SWFBCodeNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure WKMSPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxButtonEdit2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure MCQtyChange(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure MCQty10Change(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    ChkInt:Integer;
    procedure InitGridMain();
    procedure InitGrid();
    procedure MCData();
    procedure InitGridMainSel();
  public
    { Public declarations }
  end;

var
  frmJiangLiaoSet: TfrmJiangLiaoSet;

implementation
uses
  U_DataLink,U_Fun,U_GetPGJBOneTwoInList,U_ZDYHelp;

{$R *.dfm}

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

procedure TfrmJiangLiaoSet.FormDestroy(Sender: TObject);
begin
  frmJiangLiaoSet:=nil;
end;

procedure TfrmJiangLiaoSet.InitGrid();
begin
  try
    ADOTemp.DisableControls;
    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add('select * from WFBYCL_JiangYe where MCID='''+Trim(CDS_Main.fieldbyname('MCID').AsString)+'''');
      end else
        sql.Add('select * from WFBYCL_JiangYe where ZuHeID='''+Trim(CDS_Main.fieldbyname('ZuHeID').AsString)+'''');
      sql.Add(' and JYType=''主要浆液'' ');
      sql.Add(' order by OrderNo');
      Open;
    end;
    SCreateCDS20(ADOTemp,CDS_Sub);
    SInitCDSData20(ADOTemp,CDS_Sub);
    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add('select * from WFBYCL_JiangYe where MCID='''+Trim(CDS_Main.fieldbyname('MCID').AsString)+'''');
      end else
        sql.Add('select * from WFBYCL_JiangYe where ZuHeID='''+Trim(CDS_Main.fieldbyname('ZuHeID').AsString)+'''');
      sql.Add(' and JYType=''辅助浆液'' ');
      sql.Add(' order by OrderNo');
      Open;
    end;
    SCreateCDS20(ADOTemp,CDS_JYFZ);
    SInitCDSData20(ADOTemp,CDS_JYFZ);
  finally
    ADOTemp.EnableControls;
  end;
end;

procedure TfrmJiangLiaoSet.InitGridMain();
begin
  //BegDate.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select A.*,OrderNo=(select OrderNo from WFBOrder_Main B where B.MainId=A.MainId), ');
      sql.Add(' OrderQty=(select Sum(SOrdQty) from WFBOrder_Sub WS where WS.MainId=A.MainId and WS.SWFBColor=A.SWFBColor),');
      SQL.Add(' OrderUnit=(select OrdUnit from WFBOrder_Main B where B.MainId=A.MainId),');
      sql.Add(' OrderNoSL=dbo.F_Get_WFBOrder_SubStr(A.ZuHeId,''OrderNoSL''),');
      sql.Add(' CustomNoName=(select ZdyName from KH_Zdy B inner join WFBOrder_Main C on B.ZdyNo=C.CustomNo');
      sql.Add(' where C.MainId=A.MainId)');
      sql.Add(' from WFBYCL_MainIdColor_JiangYe A ');
      SQL.Add(' inner join WFBOrder_Main C on A.MainId=C.MainId  where C.ChkStatus=''审核通过'' ');
      if cxTabControl1.TabIndex=0 then
        SQL.Add(' and not exists(select * from WFBYCL_JiangYe WJY where WJY.ZuHeId=A.ZuHeId)')
      else
      if cxTabControl1.TabIndex>0 then
      begin
        sql.Add('and  exists(select * from WFBYCL_JiangYe WJY where WJY.ZuHeId=A.ZuHeId) and A.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add('and A.FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
procedure TfrmJiangLiaoSet.InitGridMainSel();
begin
  //BegDate.SetFocus;
 { try
    ADOTemp.DisableControls;
    with ADOTemp do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      SQL.Add(' select A.*,OrderNo=(select OrderNo from WFBOrder_Main B ');
      sql.Add('  where B.MainId=A.MainId), ');
      sql.Add(' CustomNoName=(select ZdyName from KH_Zdy B inner join WFBOrder_Main C on B.ZdyNo=C.CustomNo');
      sql.Add('  where C.MainId=A.MainId)');
      sql.Add(' from WFBYCL_MainIdColor_JiangYe A ');
      sql.Add(' where A.ZuHeId='''+Trim(CDS_Main.fieldbyname('ZuHeId').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Sel);
    SInitCDSData20(ADOQueryMain,CDS_Sel);
  finally
    ADOQueryMain.EnableControls;
  end;  }
end;

procedure TfrmJiangLiaoSet.ToolButton1Click(Sender: TObject);
begin
  InitGridMain();
end;

procedure TfrmJiangLiaoSet.FormShow(Sender: TObject);
var
  fsj:String;
begin
  fsj:='select ZdyName name from KH_Zdy where Type=''WFBColor''  ';
  SInitComBoxBySql(ADOTemp,SWFBColor,False,fsj);
  EndDate.DateTime:=SGetServerDate(ADOTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
  ReadCxGrid('浆料配置New',Tv1,'无纺布订单');
  ReadCxGrid('浆料配置FZ',Tv2,'无纺布订单');
  ReadCxGrid('浆料配置颜色',TvOrdNo,'无纺布订单');
  InitGridMain();
  if Trim(DParameters1)='审核' then
  begin
    BtnSave.Visible:=False;
    vOrderBakColumn1.Visible:=False;
    v1Column1.Options.Focusing:=False;
    v1Column2.Options.Focusing:=False;
    v2Column1.Options.Focusing:=False;
    v1Column5.Options.Focusing:=False;
    v2Column4.Options.Focusing:=False;
    MCQty.ReadOnly:=True;
    MCQty10.ReadOnly:=True;
  end else
  begin
    BtnChkOk.Visible:=False;
    BtnChkNo.Visible:=False;
    vOrderBakColumn1.Visible:=True;
  end;

  //InitGrid();
end;

procedure TfrmJiangLiaoSet.cxTabControl1Change(Sender: TObject);
begin
  InitGridMain();
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,CDS_Main);
  SInitCDSData20(ADOQueryMain,CDS_Main);
  InitGrid();
  MCQty.Text:=Trim(CDS_Main.fieldbyname('MCQty').AsString);
  MCQty10.Text:=Trim(CDS_Main.fieldbyname('MCQtySJ').AsString);
  MCNote.Text:=Trim(CDS_Main.fieldbyname('MCNote').AsString);
  if cxTabControl1.TabIndex>0 then
  begin
    Label7.Visible:=True;
    Label8.Visible:=True;
    Label9.Visible:=True;
    Label7.Caption:='制单人：'+Trim(CDS_Main.fieldbyname('Filler').AsString);
    Label8.Caption:='审核人：'+Trim(CDS_Main.fieldbyname('Chker').AsString);
    Label9.Caption:='审核状态：'+Trim(CDS_Main.fieldbyname('ChkStatus').AsString);
  end else
  begin
    Label7.Visible:=False;
    Label8.Visible:=False;
    Label9.Visible:=False;
  end;
end;

procedure TfrmJiangLiaoSet.OrderNoChange(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,CDS_Main);
  SInitCDSData20(ADOQueryMain,CDS_Main);
end;

procedure TfrmJiangLiaoSet.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('浆料配置New',Tv1,'无纺布订单');
  WriteCxGrid('浆料配置FZ',Tv2,'无纺布订单');
  WriteCxGrid('浆料配置颜色',TvOrdNo,'无纺布订单');
end;

procedure TfrmJiangLiaoSet.MenuItem3Click(Sender: TObject);
begin
  if BtnSave.Visible=False then Exit;
  try
    frmGetPGJBOneTwoInList:=TfrmGetPGJBOneTwoInList.Create(Application);
    with frmGetPGJBOneTwoInList do
    begin
      if ShowModal=1 then
      begin
        with ClientDataSet2 do
        begin
          First;
          while not Eof do
          begin
            with CDS_Sub do
            begin
              Append;
              CDS_Sub.FieldByName('YCLCode').Value:=Trim(ClientDataSet2.fieldbyname('YCLCode').AsString);
              CDS_Sub.FieldByName('YCLName').Value:=Trim(ClientDataSet2.fieldbyname('YCLName').AsString);
              //CDS_Sub.FieldByName('CRID').Value:=Trim(ClientDataSet2.fieldbyname('CRID').AsString);
              Post;
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmGetPGJBOneTwoInList.Free;
  end;
end;

procedure TfrmJiangLiaoSet.MenuItem4Click(Sender: TObject);
begin
  if BtnSave.Visible=False then Exit;
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.FieldByName('YJID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete WFBYCL_JiangYe where YJID='''+Trim(CDS_Sub.FieldByName('YJID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_Sub.Delete;
  {if CDS_Sub.IsEmpty=False then
    MCQty.Text:=FloatToStr(Tv1.DataController.Summary.FooterSummaryValues[0])
  else
    MCQty.Text:='0'; }
end;

procedure TfrmJiangLiaoSet.BtnSaveClick(Sender: TObject);
var
  maxno,FMCQty,FColor,FZuHeId:string;
begin
  if ChkInt<>1 then
  begin
    if Trim(CDS_Main.fieldbyname('ChkStatus').AsString)='审核通过' then
    begin
      Application.MessageBox('已审核通过，数据不能修改！','提示',0);
      Exit;
    end;
  end else
  begin
    if Trim(CDS_Main.fieldbyname('ChkStatus').AsString)='审核通过' then
    begin
      if Application.MessageBox('已审核通过，确定要重新审核吗？','提示',32+4)<>IDYES then
      begin
        ChkInt:=2;
        Exit;
      end;
    end;
  end;


  if CDS_Main.IsEmpty then Exit;
  if CDS_Sub.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选中数据！','提示',0);
    Exit;
  end;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if CDS_Main.FieldByName('SSel').AsBoolean=True then
      begin
        if Trim(FColor)='' then
          FColor:=Trim(CDS_Main.fieldbyname('SWFBColor').AsString)
        else
        if Trim(CDS_Main.fieldbyname('SWFBColor').AsString)<>FColor then
        begin
          CDS_Main.EnableControls;
          Application.MessageBox('颜色不同不能合并配制！','提示',0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  CDS_Main.EnableControls;
  if Trim(MCQty.Text)='' then
  begin
    Application.MessageBox('配置数量不能为空！','提示',0);
    Exit;
  end;
  try
    BegDate.SetFocus;
    ADOCmd.Connection.BeginTrans;
    if GetLSNo(ADOCmd,FZuHeId,'ZH','WFBYCL_MainIdColor_JiangYe',3,1)=False then
    begin
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('取组合ID号失败！','提示',0);
      Exit;
    end;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        if Trim(CDS_Sub.fieldbyname('YJID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'YJ','WFBYCL_JiangYe',3,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(CDS_Sub.fieldbyname('YJID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from WFBYCL_JiangYe where YJID='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_Sub.fieldbyname('YJID').AsString)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillTime').Value:=SGetServerDateTime(ADOTemp);
          end else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
          end;
          if Trim(CDS_Sub.fieldbyname('MCID').AsString)<>'' then
          begin
            FieldByName('MCID').Value:=Trim(CDS_Sub.fieldbyname('MCID').AsString);
            FieldByName('MainId').Value:=Trim(CDS_Sub.fieldbyname('MainId').AsString);
            FieldByName('SWFBColor').Value:=Trim(CDS_Sub.fieldbyname('SWFBColor').AsString);
          end else
          begin
            FieldByName('MCID').Value:='HB';
            FieldByName('MainId').Value:='HB';
            FieldByName('SWFBColor').Value:='HB';
          end;

          FieldByName('YJID').Value:=Trim(maxno);
          FieldByName('JYType').Value:='主要浆液';

          FieldByName('YCLCode').Value:=Trim(CDS_Sub.fieldbyname('YCLCode').AsString);
          FieldByName('YCLName').Value:=Trim(CDS_Sub.fieldbyname('YCLName').AsString);
          If Trim(CDS_Sub.fieldbyname('YGQty').AsString)<>'' then
            FieldByName('YGQty').Value:=Trim(CDS_Sub.fieldbyname('YGQty').AsString)
          else
            FieldByName('YGQty').Value:=0;
          If Trim(CDS_Sub.fieldbyname('SJQty').AsString)<>'' then
            FieldByName('SJQty').Value:=CDS_Sub.fieldbyname('SJQty').Value
          else
            FieldByName('SJQty').Value:=0;
          If Trim(CDS_Sub.fieldbyname('OrderNo').AsString)<>'' then
            FieldByName('OrderNo').Value:=CDS_Sub.fieldbyname('OrderNo').Value
          else
            FieldByName('OrderNo').Value:=99;
          If Trim(CDS_Sub.fieldbyname('PBQty').AsString)<>'' then
            FieldByName('PBQty').Value:=CDS_Sub.fieldbyname('PBQty').Value
          else
            FieldByName('PBQty').Value:=0;
          FieldByName('ZuHeId').Value:=Trim(FZuHeId);
          Post;
        end;
        with CDS_Sub do
        begin
          Edit;
          FieldByName('YJID').Value:=Trim(maxno);
          Post;
        end;
        Next;
      end;
    end;
    with CDS_JYFZ do
    begin
      First;
      while not eof do
      begin
        if Trim(CDS_JYFZ.fieldbyname('YJID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'YF','WFBYCL_JiangYe',3,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(CDS_JYFZ.fieldbyname('YJID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from WFBYCL_JiangYe where YJID='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_JYFZ.fieldbyname('YJID').AsString)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillTime').Value:=SGetServerDateTime(ADOTemp);
          end else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
          end;
          if Trim(CDS_JYFZ.fieldbyname('MCID').AsString)<>'' then
          begin
            FieldByName('MCID').Value:=Trim(CDS_JYFZ.fieldbyname('MCID').AsString);
            FieldByName('MainId').Value:=Trim(CDS_JYFZ.fieldbyname('MainId').AsString);
            FieldByName('SWFBColor').Value:=Trim(CDS_JYFZ.fieldbyname('SWFBColor').AsString);
          end else
          begin
            FieldByName('MCID').Value:='HB';
            FieldByName('MainId').Value:='HB';
            FieldByName('SWFBColor').Value:='HB';
          end;
          FieldByName('YJID').Value:=Trim(maxno);
          FieldByName('JYType').Value:='辅助浆液';
          FieldByName('YCLCode').Value:=Trim(CDS_JYFZ.fieldbyname('YCLCode').AsString);
          FieldByName('YCLName').Value:=Trim(CDS_JYFZ.fieldbyname('YCLName').AsString);
          If Trim(CDS_JYFZ.fieldbyname('SJQty').AsString)<>'' then
            FieldByName('SJQty').Value:=CDS_JYFZ.fieldbyname('SJQty').Value
          else
            FieldByName('SJQty').Value:=0;
          If Trim(CDS_JYFZ.fieldbyname('OrderNo').AsString)<>'' then
            FieldByName('OrderNo').Value:=CDS_JYFZ.fieldbyname('OrderNo').Value
          else
            FieldByName('OrderNo').Value:=0;
          If Trim(CDS_Sub.fieldbyname('PBQty').AsString)<>'' then
            FieldByName('PBQty').Value:=CDS_Sub.fieldbyname('PBQty').Value
          else
            FieldByName('PBQty').Value:=0;
          FieldByName('ZuHeId').Value:=Trim(FZuHeId);
          Post;
        end;
        with CDS_JYFZ do
        begin
          Edit;
          FieldByName('YJID').Value:=Trim(maxno);
          Post;
        end;
        Next;
      end;
    end;
    if Trim(MCQty.Text)='' then
      FMCQty:='0'
    else
      FMCQty:=MCQty.Text;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if CDS_Main.FieldByName('SSel').AsBoolean=True then
        begin
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from WFBYCL_MainIdColor_JiangYe where MCID='''+Trim(CDS_Main.fieldbyname('MCID').AsString)+'''');
            Open;
          end;
          with ADOCmd do
          begin
            Edit;
            FieldByName('MCQty').Value:=FMCQty;
            if Trim(MCQty10.Text)='' then
             FieldByName('MCQtySJ').Value:=0
            else
              FieldByName('MCQty').Value:=StrToFloat(MCQty10.Text);
            FieldByName('MCNote').Value:=Trim(MCNote.Text);
            FieldByName('WFBCpSpec').Value:=Trim(WFBCpSpec.Text);
            FieldByName('WKMS').Value:=Trim(WKMS.Text);
            FieldByName('SWFBHW').Value:=Trim(SWFBHW.Text);
            FieldByName('ZuHeId').Value:=Trim(FZuHeId);
            if Trim(CDS_Main.fieldbyname('ChkStatus').AsString)='审核不通过' then
            begin
               FieldByName('ChkStatus').Value:='已修改';
               FieldByName('Chker').Value:='';
               with CDS_Main do
               begin
                 Edit;
                 FieldByName('ChkStatus').Value:='已修改';

                 Post;
               end;
            end;
            if Trim(CDS_Main.fieldbyname('Filler').AsString)='' then
            begin
              fieldbyname('Filler').Value:=Trim(DName);
              fieldbyname('FillTime').Value:=SGetServerDateTime(ADOTemp);
              with CDS_Main do
              begin
                Edit;
                FieldByName('Filler').Value:=Trim(DName);
                Post;
              end;
            end else
            begin
              fieldbyname('Editer').Value:=Trim(DName);
              fieldbyname('EditTime').Value:=SGetServerDateTime(ADOTemp);
            end;
            Post;
          end;
          with CDS_Main do
          begin
            Edit;
            FieldByName('ZuHeId').Value:=Trim(FZuHeId);
            Post;
          end;
          if cxTabControl1.TabIndex=0 then
          begin
            CDS_Main.Delete;
          end else
            Next;
        end else
          Next;
      end;
    end;
    CDS_Main.EnableControls;

    ADOCmd.Connection.CommitTrans;
    if ChkInt<>1 then
      Application.MessageBox('保存成功！','提示',0);
    InitGrid();
    Exit;
  except
    ADOCmd.Connection.RollbackTrans;
    if ChkInt<>1 then
      Application.MessageBox('保存失败!','提示',0)
    else
      Application.MessageBox('操作异常!','提示',0);
    Exit;
  end;
end;

procedure TfrmJiangLiaoSet.TvOrdNoCellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
  var
    FFHBID,maxno:string;
begin
  FFHBID:=Trim(CDS_Main.fieldbyname('ZuHeID').AsString);
  maxno:=Trim(CDS_Main.fieldbyname('MCID').AsString);
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    if cxTabControl1.TabIndex=0 then
    begin
      sql.Add('select * from WFBYCL_JiangYe where MCID='''+Trim(maxno)+'''');
    end else
      sql.Add('select * from WFBYCL_JiangYe where ZuHeID='''+Trim(FFHBID)+'''');
    sql.Add(' and JYType=''主要浆液'' ');
    sql.Add(' order by OrderNo');
    Open;
  end;
  SCreateCDS20(ADOTemp,CDS_Sub);
  SInitCDSData20(ADOTemp,CDS_Sub);
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    if cxTabControl1.TabIndex=0 then
    begin
      sql.Add('select * from WFBYCL_JiangYe where MCID='''+Trim(maxno)+'''');
    end else
      sql.Add('select * from WFBYCL_JiangYe where ZuHeID='''+Trim(FFHBID)+'''');
    sql.Add(' and JYType=''辅助浆液'' ');
    sql.Add(' order by OrderNo');
    Open;
  end;
  SCreateCDS20(ADOTemp,CDS_JYFZ);
  SInitCDSData20(ADOTemp,CDS_JYFZ);

  with ADOTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select Top 1*,');
    sql.Add(' OrderNoSL=dbo.F_Get_WFBOrder_SubStr(A.ZuHeId,''OrderNoSL'')');
    if Trim(FFHBID)<>'' then
      sql.Add(' from WFBYCL_MainIdColor_JiangYe A where ZuHeId='''+Trim(FFHBID)+'''')
    else
      sql.Add(' from WFBYCL_MainIdColor_JiangYe A where MCId='''+Trim(CDS_Main.fieldbyname('MCID').AsString)+'''')  ;
    Open;
  end;
  MCQty.Text:=Trim(ADOTemp.fieldbyname('MCQty').AsString);
  MCQty10.Text:=Trim(ADOTemp.fieldbyname('MCQtySJ').AsString);
  MCNote.Text:=Trim(ADOTemp.fieldbyname('MCNote').AsString);
  WKMS.Text:=Trim(ADOTemp.fieldbyname('WKMS').AsString);
  SWFBHW.Text:=Trim(ADOTemp.fieldbyname('SWFBHW').AsString);
  WFBCpSpec.Text:=Trim(ADOTemp.fieldbyname('WFBCpSpec').AsString);
  OrderNoSL.Text:=Trim(ADOTemp.fieldbyname('OrderNoSL').AsString);
  if cxTabControl1.TabIndex>0 then
  begin
    Label7.Visible:=True;
    Label8.Visible:=True;
    Label9.Visible:=True;
    Label7.Caption:='制单人：'+Trim(ADOTemp.fieldbyname('Filler').AsString);
    Label8.Caption:='审核人：'+Trim(ADOTemp.fieldbyname('Chker').AsString);
    Label9.Caption:='审核状态：'+Trim(ADOTemp.fieldbyname('ChkStatus').AsString);
  end else
  begin
    Label7.Visible:=False;
    Label8.Visible:=False;
    Label9.Visible:=False;
  end;



end;
procedure TfrmJiangLiaoSet.MCData();
begin
  MCQty.Text:=Trim(CDS_Main.fieldbyname('MCQty').AsString);
  MCNote.Text:=Trim(CDS_Main.fieldbyname('MCNote').AsString);
  if cxTabControl1.TabIndex>0 then
  begin
    Label7.Visible:=True;
    Label8.Visible:=True;
    Label9.Visible:=True;
    Label7.Caption:='制单人：'+Trim(CDS_Main.fieldbyname('Filler').AsString);
    Label8.Caption:='审核人：'+Trim(CDS_Main.fieldbyname('Chker').AsString);
    Label9.Caption:='审核状态：'+Trim(CDS_Main.fieldbyname('ChkStatus').AsString);
  end else
  begin
    Label7.Visible:=False;
    Label8.Visible:=False;
    Label9.Visible:=False;
  end;
end;

procedure TfrmJiangLiaoSet.MCQtyKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in['0'..'9','.',#13,#8]) then
  begin
    Key:=#0;
  end;
end;

procedure TfrmJiangLiaoSet.BtnChkOkClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex=0 then Exit;
  ChkInt:=1;
 // BtnSave.Click;
  if ChkInt=2 then Exit;
  if Trim(DName)=Trim(CDS_Main.fieldbyname('Filler').AsString) then
  begin
    Application.MessageBox('不能审核自己的数据！','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOCmd.Connection.BeginTrans;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFBYCL_MainIdColor_JiangYe Set Chker='''+Trim(DName)+'''');
      SQL.Add(',ChkTime=getdate(),ChkStatus=''审核通过'' ');
      sql.Add('where ZuHeId='''+Trim(CDS_Main.fieldbyname('ZuHeId').AsString)+'''');
      ExecSQL;
    end;
    ADOCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功！','提示',0);
    with CDS_Main do
    begin
      Edit;
      FieldByName('ChkStatus').Value:='审核通过';
      FieldByName('Chker').Value:=Trim(DName);
      Post;
    end;
    MCData();
    ChkInt:=0;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败！','提示',0);
  end;

end;

procedure TfrmJiangLiaoSet.BtnChkNoClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex=0 then Exit;
  ChkInt:=1;
 // BtnSave.Click;
  if ChkInt=2 then Exit;
  if Trim(DName)=Trim(CDS_Main.fieldbyname('Filler').AsString) then
  begin
    Application.MessageBox('不能审核自己的数据！','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOCmd.Connection.BeginTrans;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFBYCL_MainIdColor_JiangYe Set Chker='''+Trim(DName)+'''');
      SQL.Add(',ChkTime=getdate(),ChkStatus=''审核不通过'' ');
      sql.Add('where ZuHeId='''+Trim(CDS_Main.fieldbyname('ZuHeId').AsString)+'''');
      ExecSQL;
    end;
    ADOCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功！','提示',0);
    with CDS_Main do
    begin
      Edit;
      FieldByName('ChkStatus').Value:='审核不通过';
      FieldByName('Chker').Value:=Trim(DName);
      Post;
    end;
    MCData();
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败！','提示',0);
  end;
end;

procedure TfrmJiangLiaoSet.BtnPrintClick(Sender: TObject);
var
   fPrintFile:string;
begin
  if Trim(CDS_Main.fieldbyname('ChkStatus').AsString)<>'审核通过' then
  begin
    Application.MessageBox('未审核通过，不能打印数据！','提示',0);
    Exit;
  end;
  with ADOQueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Print_JiangYe :ZuHeId');
    Parameters.ParamByName('ZuHeId').Value:=Trim(CDS_Main.fieldbyname('ZuHeId').AsString);
    Open;
  end;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\浆料配置单10.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\浆料配置单.rmf'),'提示',0);
  end;
end;

procedure TfrmJiangLiaoSet.MenuItem1Click(Sender: TObject);
begin
  if BtnSave.Visible=False then Exit;
  try
    frmGetPGJBOneTwoInList:=TfrmGetPGJBOneTwoInList.Create(Application);
    with frmGetPGJBOneTwoInList do
    begin
      if ShowModal=1 then
      begin
        with ClientDataSet2 do
        begin
          First;
          while not Eof do
          begin
            with CDS_JYFZ do
            begin
              Append;
              CDS_JYFZ.FieldByName('YCLCode').Value:=Trim(ClientDataSet2.fieldbyname('YCLCode').AsString);
              CDS_JYFZ.FieldByName('YCLName').Value:=Trim(ClientDataSet2.fieldbyname('YCLName').AsString);
              //CDS_Sub.FieldByName('CRID').Value:=Trim(ClientDataSet2.fieldbyname('CRID').AsString);
              Post;
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmGetPGJBOneTwoInList.Free;
  end;
end;

procedure TfrmJiangLiaoSet.MenuItem2Click(Sender: TObject);
begin
  if BtnSave.Visible=False then Exit;
  if CDS_JYFZ.IsEmpty then Exit;
  if Trim(CDS_JYFZ.FieldByName('YJID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete WFBYCL_JiangYe where YJID='''+Trim(CDS_JYFZ.FieldByName('YJID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_JYFZ.Delete;
end;

procedure TfrmJiangLiaoSet.SWFBCodeNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WFBCpSpec';
      flagname:='产品型号';
      if ShowModal=1 then
      begin
        Self.WFBCpSpec.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJiangLiaoSet.WKMSPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WKMS';
      flagname:='网孔目数';
      if ShowModal=1 then
      begin
        Self.WKMS.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJiangLiaoSet.cxButtonEdit2PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WFBHX';
      flagname:='花型';
      if ShowModal=1 then
      begin
        Self.SWFBHW.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJiangLiaoSet.v1Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FMCQty:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(MCQty.Text)='' then
  begin
    FMCQty:='0'
  end else
  begin
    FMCQty:=Trim(MCQty.Text);
  end;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with CDS_Sub do
  begin
    Edit;
    FieldByName('SJQty').Value:=StrToFloat(mvalue)*StrToFloat(FMCQty)/100;
    FieldByName('PBQty').Value:=StrToFloat(mvalue);
    Post;
  end;
end;

procedure TfrmJiangLiaoSet.MCQtyChange(Sender: TObject);
var
  mvalue,FMCQty:string;
begin
  if( (MCQty.Focused) and (MCQty.ReadOnly=False) )then
  begin
    if Trim(MCQty.Text)='' then
    begin
      FMCQty:='0'
    end else
    begin
      FMCQty:=Trim(MCQty.Text);
    end;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        if Trim(fieldbyname('PBQty').AsString)<>'' then
          mvalue:=Trim(fieldbyname('PBQty').AsString)
        else
          mvalue:='0';
        FieldByName('SJQty').Value:=StrToFloat(mvalue)*StrToFloat(FMCQty)/100;
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
  end;

end;

procedure TfrmJiangLiaoSet.ToolButton2Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex=0 then Exit;
  if Trim(CDS_Main.FieldByName('ChkStatus').AsString)='审核通过' then Exit;
  if Application.MessageBox('确定要执行此操作吗?','提示',32+4)<>IDYES then Exit;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update WFBYCL_JiangYe Set ZuHeId=NULL where MCID='''+Trim(CDS_Main.fieldbyname('MCID').AsString)+'''');
    sql.Add('Update WFBYCL_MainIdColor_JiangYe Set ZuHeId=NULL where MCID='''+Trim(CDS_Main.fieldbyname('MCID').AsString)+'''');
    ExecSQL;
  end;
  CDS_Main.Delete;

end;

procedure TfrmJiangLiaoSet.MCQty10Change(Sender: TObject);
var
  mvalue,FMCQty:string;
begin
  if( (MCQty10.Focused) and (MCQty10.ReadOnly=False) )then
  begin
    if Trim(MCQty10.Text)='' then
    begin
      FMCQty:='0'
    end else
    begin
      FMCQty:=Trim(MCQty10.Text);
    end;
    CDS_JYFZ.DisableControls;
    with CDS_JYFZ do
    begin
      First;
      while not Eof do
      begin
        Edit;
        if Trim(fieldbyname('PBQty').AsString)<>'' then
          mvalue:=Trim(fieldbyname('PBQty').AsString)
        else
          mvalue:='0';
        FieldByName('SJQty').Value:=StrToFloat(mvalue)*StrToFloat(FMCQty)/100;
        Post;
        Next;
      end;
    end;
    CDS_JYFZ.EnableControls;
  end;

end;

procedure TfrmJiangLiaoSet.v2Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FMCQty:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(MCQty10.Text)='' then
  begin
    FMCQty:='0'
  end else
  begin
    FMCQty:=Trim(MCQty10.Text);
  end;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with CDS_JYFZ do
  begin
    Edit;
    FieldByName('SJQty').Value:=StrToFloat(mvalue)*StrToFloat(FMCQty)/100;
    FieldByName('PBQty').Value:=StrToFloat(mvalue);
    Post;
  end;
end;

end.
