unit U_MLManage_FHLR;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, ADODB, DBClient, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, ComCtrls, ToolWin,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxContainer, cxTextEdit,
  cxMaskEdit, cxDropDownEdit, cxCalendar, StdCtrls, ExtCtrls, BtnEdit,
  cxButtonEdit;

type
  TfrmMLManage_FHLR = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    CDS_Sub: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DataSource2: TDataSource;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Panel1: TPanel;
    GSNo: TEdit;
    Label1: TLabel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Order_GX: TClientDataSet;
    DataSource1: TDataSource;
    Panel3: TPanel;
    ToolBar3: TToolBar;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    MLDLNo: TBtnEditA;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1BPCode: TcxGridDBColumn;
    V1BPName: TcxGridDBColumn;
    V1BPMF: TcxGridDBColumn;
    V1BPKZ: TcxGridDBColumn;
    V1BPJS: TcxGridDBColumn;
    v1BPMiDu: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ML_Col: TClientDataSet;
    DataSource3: TDataSource;
    cxGridPopupMenu3: TcxGridPopupMenu;
    TMLAdd: TToolButton;
    Label3: TLabel;
    MLNo: TEdit;
    Label4: TLabel;
    GXName: TEdit;
    Label5: TLabel;
    GXNo: TEdit;
    V1Zhengfan: TcxGridDBColumn;
    DLHXK: TBtnEditA;
    Ord_HX: TClientDataSet;
    DataSource4: TDataSource;
    cxGridPopupMenu4: TcxGridPopupMenu;
    V1ZXHNo: TcxGridDBColumn;
    Label6: TLabel;
    MLName: TEdit;
    JSName: TBtnEditA;
    Label2: TLabel;
    MLMF: TEdit;
    MLKZ: TEdit;
    Panel6: TPanel;
    Label7: TLabel;
    GYLX: TEdit;
    GYLXName: TEdit;
    Label8: TLabel;
    GYName: TEdit;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure TAddClick(Sender: TObject);
    procedure MLDLNoBtnClick(Sender: TObject);
    procedure TMLAddClick(Sender: TObject);
    procedure DLHXKBtnClick(Sender: TObject);
    procedure JSNameBtnClick(Sender: TObject);
    procedure V1ZXHNoPropertiesEditValueChanged(Sender: TObject);
    procedure V1ZhengfanPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    Fint:Integer;
    procedure InitSubGrid();
    function  SaveData():Boolean;
    procedure BHCZhu();
    { Private declarations }
  public
    FCYID,FCYCode,FCPID,FCPNO,FBPID,FGYName,FML,FGYLX,FGSNo,FHXBPID,FGSName:String;
    FGYLXName:string;
    { Public declarations }
  end;

var
  frmMLManage_FHLR: TfrmMLManage_FHLR;

implementation
uses
  U_DataLink,U_Fun,U_BPZdy_LR,U_BPZdy_SH,U_MLMange_ADDCX,U_BPZdy_HXK,U_ZDYHelp,U_BPZdy_LRPB;

{$R *.dfm}

procedure TfrmMLManage_FHLR.InitSubGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from ML_Manage where MLID='''+trim(FCYID)+'''');
      Open;
    end;
    if ADOQueryMain.IsEmpty=false then
    begin
      SCSHDataNew(ADOQueryMain,Panel1,2);
      SCSHDataNew(ADOQueryMain,Panel3,2);
      FBPID:=Trim(ADOQueryMain.FieldByName('MBPID').Value);
    end;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*,isnull(B.BPCode,C.MLNo) BPCode,isnull(B.BPName,C.MLName) BPName');
      sql.Add(' ,isnull(B.BPMF,C.MLMF) BPMF,isnull(B.BPKZ,C.MLKZ) BPKZ');
      sql.Add(',B.BPJS,B.BPWS,B.BPMiDu,B.BPUnit');
      SQL.Add(' from ML_Manage_PB A left join BP_ZDY B on B.BPID=A.BPID ');
      sql.Add(' left join ML_Manage C on C.MLID=A.BPID');
      sql.Add('where A.MLID='''+Trim(FCYID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Sub);
    SInitCDSData20(ADOQueryMain,CDS_Sub);
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.*,B.* from ML_Manage_GX A inner join BP_ZDY B on B.BPID=A.BPID ');
      sql.Add('where MLID='''+Trim(FCYID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_GX);
    SInitCDSData20(ADOQueryMain,Order_GX);
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from BP_ZDY_Sub ');
      sql.Add('where BPCode='''+Trim(MLDLNo.Text)+''' and BPflag=''SHK''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ML_Col);
    SInitCDSData20(ADOQueryMain,ML_Col);
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add('select A.*,B.* from BP_ZDY_Sub A ');
      Sql.Add('inner join KH_Zdy_Attachment B on A.HXATID=B.ATID and B.Type=''CPHX''');
      sql.Add(' where A.BPCode='''+trim(DLHXK.Text)+'''');
      open;
    end;
    SCreateCDS20(ADOQueryMain,Ord_HX);
    SInitCDSData20(ADOQueryMain,Ord_HX);
    MLNo.Text:='F';
    FML:='F';
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmMLManage_FHLR.FormShow(Sender: TObject);
var
  fsj:String;
begin
  ReadCxGrid('面料管理1',Tv1,'样品管理');
  //ReadCxGrid('面料管理3',Tv3,'样品管理');
  InitSubGrid();
end;

procedure TfrmMLManage_FHLR.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('面料管理1',Tv1,'样品管理');
  //WriteCxGrid('面料管理3',Tv3,'样品管理');
  Close;
end;

function TfrmMLManage_FHLR.SaveData():Boolean;
var
  maxId,maxno:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if FCYID='' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'ML','ML_Manage',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
    end
    else
    begin
      maxno:=Trim(FCYID);
    end;
    //GSNo.Text:=Trim(maxno);
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from ML_Manage where MLID='''+Trim(FCYID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if isempty then
      begin
        Append;
      end
      else
      begin
        Edit;
      end;
      FieldByName('MLID').Value:=Trim(maxno);
      //FieldByName('GSNo').Value:=Trim(maxno);
      FieldByName('MBPID').Value:=Trim(FBPID);
      FieldByName('MLType').Value:='复合';
      SSetsaveSqlNew(ADOQueryCmd,'ML_Manage',panel1,2);
      SSetsaveSqlNew(ADOQueryCmd,'ML_Manage',panel3,2);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ML_Manage where GSNo='''+Trim(GSNo.Text)+'''');
      Open;
    end;
    if ADOQueryCmd.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('公司编号重复!','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ML_Manage where MLNo='''+Trim(MLNo.Text)+'''');
      Open;
    end;
    if ADOQueryCmd.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('客户编号重复!','提示',0);
      Exit;
    end;
    FCYID:=Trim(maxno);
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_Sub.fieldbyname('MPID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxId,'MP','ML_Manage_PB',4,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('区最大号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxId:=Trim(CDS_Sub.fieldbyname('MPID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from ML_Manage_PB where MPID='''+Trim(maxId)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if isempty then
          begin
            Append;
          end else
          begin
            Edit;
          end;
          FieldByName('MPID').Value:=Trim(maxId);
          FieldByName('MLID').Value:=Trim(FCYID);
          FieldByName('ZXHNo').Value:=CDS_Sub.fieldbyname('ZXHNo').AsInteger;
          FieldByName('BPID').Value:=Trim(CDS_Sub.fieldbyname('BPID').AsString);
          FieldByName('Zhengfan').Value:=Trim(CDS_Sub.fieldbyname('Zhengfan').AsString);
          Post;
        end;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except;
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmMLManage_FHLR.BHCZhu();
var FBH,FCP,FBHName:string;
begin
  CDS_Sub.DisableControls;
  with CDS_Sub do
  begin
    First;
    while not eof do
    begin
      if FieldByName('ZXHNo').AsInteger=1 then
      begin
        MLNo.Text:='F'+Trim(fieldbyname('BPCode').AsString);
      end;
      if FBH='' then
      FBH:=trim(CDS_Sub.fieldbyname('BPCode').AsString)
      else
      FBH:=FBH+'+'+trim(CDS_Sub.fieldbyname('BPCode').AsString);
      if FBHName='' then
      FBHName:=trim(CDS_Sub.fieldbyname('BPName').AsString)
      else
      FBHName:=FBHName+'+'+trim(CDS_Sub.fieldbyname('BPName').AsString);
      Next;
    end;
  end;
  CDS_Sub.EnableControls;
  GSNo.Text:='('+Trim(FBH)+')'+'F';
  GYName.Text:='('+Trim(FBHName)+')'+'复合';
end;

procedure TfrmMLManage_FHLR.ToolButton1Click(Sender: TObject);
var FBH:string;
begin
  {if Trim(MLNo.Text)='' then
  begin
    Application.MessageBox('编号不能为空!','提示',0);
    Exit;
  end; }
  if CDS_Sub.Locate('ZXHNo',null,[])=true then
  begin
    application.MessageBox('序号不能为空','提示');
    exit;
  end;
  if Trim(MLName.Text)='' then
  begin
    Application.MessageBox('品名不能为空!','提示',0);
    Exit;
  end;
  //BHCZhu();
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;

procedure TfrmMLManage_FHLR.ToolButton2Click(Sender: TObject);
begin
  frmBPZDY_LRPB:=TfrmBPZDY_LRPB.create(self);
  with frmBPZDY_LRPB do
  begin
    flag:='PBName';
    flagName:='坯布';
    ViewFlag:=true;
    if ShowModal=1 then
    begin
      with CDS_Sub do
      begin
        append;
        FieldByName('ZXHNo').AsInteger:=CDS_Sub.RecordCount+1;
        FieldByName('BPCode').Value:=Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
        FieldByName('BPID').Value:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
        FieldByName('BPName').Value:=Trim(ClientDataSet1.fieldbyname('BPName').AsString);
        FieldByName('BPMF').Value:=Trim(ClientDataSet1.fieldbyname('BPMF').AsString);
        FieldByName('BPKZ').Value:=Trim(ClientDataSet1.fieldbyname('BPKZ').AsString);
        FieldByName('BPJS').Value:=Trim(ClientDataSet1.fieldbyname('BPJS').AsString);
        FieldByName('BPWS').Value:=Trim(ClientDataSet1.fieldbyname('BPWS').AsString);
        FieldByName('BPMiDu').Value:=Trim(ClientDataSet1.fieldbyname('BPMiDu').AsString);
        post;
      end;
      BHCZhu();
    end;
    free;
  end;
end;

procedure TfrmMLManage_FHLR.ToolButton3Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then exit;
  if CDS_Sub.fieldbyname('MPID').AsString<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗','提示',1)=2 then exit;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from ML_Manage_PB where MPID='''+trim(CDS_Sub.fieldbyname('MPID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_Sub.Delete;
  BHCZhu();
end;

procedure TfrmMLManage_FHLR.ToolButton5Click(Sender: TObject);
begin
  if Order_GX.IsEmpty then exit;
  if Order_GX.fieldbyname('GXID').AsString<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗','提示',1)=2 then exit;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from ML_Manage_GX where GXID='''+trim(CDS_Sub.fieldbyname('GXID').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_GX.Delete;
end;

procedure TfrmMLManage_FHLR.TAddClick(Sender: TObject);
begin
  frmBPZDY_LR:=TfrmBPZDY_LR.create(self);
  with frmBPZDY_LR do
  begin
    flag:='GXName';
    flagName:='工序';
    ViewFlag:=true;
    V1BPMF.Visible:=false;
    V1BPKZ.Visible:=false;
    V1BPJS.Visible:=false;
    //V1BPWS.Visible:=false;
    v1BPMiDu.Caption:='序号';
    ToolBar2.Visible:=false;
    cxGrid2.Visible:=false;
    if ShowModal=1 then
    begin
      with Order_GX do
      begin
        append;
        FieldByName('XHNo').Value:=Trim(ClientDataSet1.fieldbyname('BPMiDu').AsString);
        FieldByName('BPCode').Value:=Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
        FieldByName('BPName').Value:=Trim(ClientDataSet1.fieldbyname('BPName').AsString);
        FieldByName('BPID').Value:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
        post;
      end;
    end;
    free;
  end;
end;

procedure TfrmMLManage_FHLR.MLDLNoBtnClick(Sender: TObject);
begin
  frmBPZDY_SH:=TfrmBPZDY_SH.Create(self);
  with frmBPZDY_SH do
  begin
    flag:='SHK';
    flagName:='色号库';
    ViewFlag:=true;
    if ShowModal=1 then
    begin
      MLDLNo.Text:=Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
      FBPID:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
      ML_GYS.DisableControls;
      with ML_GYS do
      begin
        first;
        while not eof do
        begin
          with ML_Col do
          begin
            Append;
            FieldByName('BPSeHao').Value:=Trim(ML_GYS.fieldbyname('BPSeHao').AsString);
            FieldByName('BPCol').Value:=Trim(ML_GYS.fieldbyname('BPCol').AsString);
            FieldByName('BPColEng').Value:=Trim(ML_GYS.fieldbyname('BPColEng').AsString);
            FieldByName('BPNote').Value:=Trim(ML_GYS.fieldbyname('BPNote').AsString);
            post;
          end;
          Next;
        end;
      end;
      ML_GYS.EnableControls;
    end;
    free;
  end;
end;

procedure TfrmMLManage_FHLR.TMLAddClick(Sender: TObject);
begin
  frmMLMange_ADDCX:=TfrmMLMange_ADDCX.Create(self);
  with frmMLMange_ADDCX do
  begin
    if ShowModal=1 then
    begin
      with CDS_Sub do
      begin
        Append;
        FieldByName('ZXHNo').AsInteger:=CDS_Sub.RecordCount+1;
        FieldByName('BPCode').Value:=CDS_Main.fieldbyname('MLNo').AsString;
        FieldByName('BPID').Value:=Trim(CDS_Main.fieldbyname('MLID').AsString);
        FieldByName('BPName').Value:=CDS_Main.fieldbyname('MLName').AsString;
        FieldByName('BPMF').Value:=CDS_Main.fieldbyname('MLMF').AsString;
        FieldByName('BPKZ').Value:=CDS_Main.fieldbyname('MLKZ').AsString;
        post;
      end;
      GYLX.Text:=CDS_Main.fieldbyname('GYLX').AsString+'→F';
      GYLXName.Text:=CDS_Main.fieldbyname('GYLXName').AsString+'→复合';
      BHCZhu();
    end;
    free;
  end;
end;

procedure TfrmMLManage_FHLR.DLHXKBtnClick(Sender: TObject);
begin
  frmBPZDY_HXK:=TfrmBPZDY_HXK.Create(self);
  with frmBPZDY_HXK do
  begin
    flag:='HXK';
    flagName:='花型库';
    ViewFlag:=true;
    if ShowModal=1 then
    begin
      DLHXK.Text:=Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
      FHXBPID:=Trim(ClientDataSet1.fieldbyname('BPID').AsString);
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select A.*,B.* from BP_ZDY_Sub A ');
        Sql.Add('inner join KH_Zdy_Attachment B on A.HXATID=B.ATID and B.Type=''CPHX''');
        sql.Add(' where A.BPCode='''+trim(DLHXK.Text)+'''');
        open;
      end;
      SCreateCDS20(ADOQueryCmd,Ord_HX);
      SInitCDSData20(ADOQueryCmd,Ord_HX);
    end;
    free;
  end;
end;

procedure TfrmMLManage_FHLR.JSNameBtnClick(Sender: TObject);
begin
  frmBPZDY_LR:=TfrmBPZDY_LR.Create(self);
  with frmBPZDY_LR do
  begin
    flag:='JSName';
    flagName:='胶水';
    Panel2.Visible:=false;
    cxGrid3.Visible:=false;
    cxGrid1.Align:=alClient;
    V1BPJS.Visible:=false;
    v1BPMiDu.Visible:=false;
    V1BPKZ.Visible:=false;
    V1BPMF1.Visible:=false;
    ViewFlag:=true;
    if ShowModal=1 then
    begin
      JSName.Text:=Trim(ClientDataSet1.fieldbyname('BPName').AsString);
    end;
    free;
  end;
end;

procedure TfrmMLManage_FHLR.V1ZXHNoPropertiesEditValueChanged(
  Sender: TObject);
var mavlue:string;
begin
  mavlue:=TcxTextEdit(Sender).EditingText;
  if mavlue='' then mavlue:='0';
  with CDS_Sub do
  begin
    edit;
    FieldByName('ZXHNo').Value:=mavlue;
  end;
  BHCZhu();
end;

procedure TfrmMLManage_FHLR.V1ZhengfanPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='zhengfan';
    flagname:='正反';
    if ShowModal=1 then
    begin
      with CDS_Sub do
      begin
        edit;
        FieldByName('Zhengfan').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

end.
