unit U_BPZdy_LRPM;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ToolWin, ComCtrls, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, ImgList, StdCtrls, ExtCtrls,
  cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxButtonEdit, BtnEdit,
  cxTL, cxMaskEdit, cxInplaceContainer, cxDBTL, cxTLData, cxContainer,
  cxCurrencyEdit, cxCheckBox, cxSplitter, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, cxTLdxBarBuiltInMenu;

type
  TfrmBPZDY_LRPM = class(TForm)
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    TBClose: TToolButton;
    ADOConnection1: TADOConnection;
    ThreeImgList: TImageList;
    DataSource2: TDataSource;
    ML_GYS: TClientDataSet;
    DataSource3: TDataSource;
    ML_GX: TClientDataSet;
    ADOQuery6: TADOQuery;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    Panel4: TPanel;
    DataSource4: TDataSource;
    ADOQueryTree: TADOQuery;
    Order_Tree: TClientDataSet;
    Panel6: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1BPCode: TcxGridDBColumn;
    V1BPName: TcxGridDBColumn;
    V1BPMF1: TcxGridDBColumn;
    V1BPKZ: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    Panel7: TPanel;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    ToolBar3: TToolBar;
    ToolPBLE: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    BPNameM: TEdit;
    BPCodeM: TEdit;
    V1BPNote: TcxGridDBColumn;
    V1BPPrice: TcxGridDBColumn;
    V1BPCF: TcxGridDBColumn;
    V1BPMiDu: TcxGridDBColumn;
    V1BPJS: TcxGridDBColumn;
    V1CPName: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBSaveClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BPNamehange(Sender: TObject);
    procedure v1note1PropertiesEditValueChanged(Sender: TObject);
    procedure V1BPCodePropertiesEditValueChanged(Sender: TObject);
    procedure V1BPNamePropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure BPCodeMChange(Sender: TObject);
    procedure Tv3CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxDBTreeList1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    FBPID, FCPID, FCPNo, FCPName: string;
    procedure InitGrid();
    procedure SaveData();
    procedure initTree();
    { Private declarations }
  public
    flag, flagname, snote, MainType, FGYName, Fint, canshu1: string;
    fnote, fnote1, forderno, fZdyFlag, ViewFlag, fHelpType: Boolean;
    PPSTE: integer;
    { Public declarations }
  end;

var
  frmBPZDY_LRPM: TfrmBPZDY_LRPM;

implementation

uses
  U_DataLink, U_Fun10;

{$R *.dfm}
procedure TfrmBPZDY_LRPM.initTree();
begin
  with ADOQueryTree do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select *');
    sql.Add(', Case when isnull(CPNo,'''')<>'''' then RTrim(CPNo)+''/''+CPName else CPName end as CP ');
    SQL.Add('from CP_TypePM order by CPlevel,CPOrder,CPName');
    ShowMessage(sql.text);
    Open;
  end;
  SCreateCDS20(ADOQueryTree, Order_Tree);
  SInitCDSData20(ADOQueryTree, Order_Tree);
  cxDBTreeList1.Items[0].Expand(true);
end;

procedure TfrmBPZDY_LRPM.SaveData();
var
  maxno: string;
begin
  if FGYName <> '' then
    exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while not eof do
      begin
        if Trim(ClientDataSet1.FieldByName('BPID').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxno, 'BP', 'PM_ZDY', 3, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet1.EnableControls;
            Application.MessageBox('取最大编号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(ClientDataSet1.fieldbyname('BPID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from PM_ZDY where BPID=''' + Trim(maxno) + '''');
          Open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.Append;
        end
        else
        begin
          ADOQueryCmd.Edit;
        end;
        ADOQueryCmd.FieldByName('BPID').Value := Trim(maxno);
        SSetSaveDataCDSNew(ADOQueryCmd, Tv1, ClientDataSet1, 'PM_ZDY', 3);
        ADOQueryCmd.FieldByName('BPType').Value := flagName;
        ADOQueryCmd.FieldByName('BPFlag').Value := flag;
        ADOQueryCmd.FieldByName('valid').Value := 'Y';
        ADOQueryCmd.Post;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.add('select * from PM_ZDY where BPCode=''' + Trim(ClientDataSet1.fieldbyname('BPCode').AsString) + '''');
          sql.Add(' and BPFlag=''' + trim(flag) + '''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty = False then
        begin
          if ADOQueryTemp.RecordCount > 1 then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('delete PM_ZDY where BPID=''' + Trim(ClientDataSet1.fieldbyname('BPID').AsString) + '''');
              ExecSQL;
            end;
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet1.EnableControls;
            Application.MessageBox('编号重复！', '提示', 0);
            Exit;
          end;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.add('select * from PM_ZDY where BPName=''' + Trim(ClientDataSet1.fieldbyname('BPName').AsString) + '''');
          sql.add(' and isnull(BPMF,'''')=''' + Trim(ClientDataSet1.fieldbyname('BPMF').AsString) + '''');
          sql.add(' and isnull(BPKZ,'''')=''' + Trim(ClientDataSet1.fieldbyname('BPKZ').AsString) + '''');
          sql.add(' and isnull(BPJS,'''')=''' + Trim(ClientDataSet1.fieldbyname('BPJS').AsString) + '''');
          sql.add(' and isnull(BPWS,'''')=''' + Trim(ClientDataSet1.fieldbyname('BPWS').AsString) + '''');
          sql.add(' and isnull(BPMiDu,'''')=''' + Trim(ClientDataSet1.fieldbyname('BPMiDu').AsString) + '''');
          sql.add(' and isnull(BPUnit,'''')=''' + Trim(ClientDataSet1.fieldbyname('BPUnit').AsString) + '''');
          sql.Add(' and BPFlag=''' + trim(flag) + '''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty = False then
        begin
          if ADOQueryTemp.RecordCount > 1 then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('delete PM_ZDY where BPID=''' + Trim(ClientDataSet1.fieldbyname('BPID').AsString) + '''');
              ExecSQL;
            end;
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet1.EnableControls;
            Application.MessageBox('名称门幅克重经纱纬纱重复！', '提示', 0);
            Exit;
          end;
        end;
        ClientDataSet1.Edit;
        ClientDataSet1.FieldByName('BPID').Value := Trim(maxno);
        //ClientDataSet1.Post;
        Next;
      end;
      ClientDataSet1.EnableControls;
    end;
    TV1.OptionsData.Editing := false;
    TV1.OptionsSelection.CellSelect := false;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

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

procedure TfrmBPZDY_LRPM.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Select_PM  ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet1);
    SInitCDSData20(ADOQueryMain, ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmBPZDY_LRPM.TBSaveClick(Sender: TObject);
var
  maxno: string;
begin
  if ClientDataSet1.IsEmpty then
    Exit;
  Toolbar1.SetFocus;
  if ClientDataSet1.Locate('BPCode', null, []) then
  begin
    Application.MessageBox('编号不能为空！', '提示', 0);
    Exit;
  end;
  if ClientDataSet1.Locate('BPCode', '', []) then
  begin
    Application.MessageBox('编号不能为空！', '提示', 0);
    Exit;
  end;
  if ClientDataSet1.Locate('BPName', null, []) then
  begin
    Application.MessageBox('名称不能为空！', '提示', 0);
    Exit;
  end;
  if ClientDataSet1.Locate('BPName', '', []) then
  begin
    Application.MessageBox('名称不能为空！', '提示', 0);
    Exit;
  end;
  SaveData();
  Application.MessageBox('保存成功！', '提示', 0);
end;

procedure TfrmBPZDY_LRPM.TBCloseClick(Sender: TObject);
begin
  panel1.SetFocus;
  WriteCxGrid('泡棉选择', TV1, '自定义数据');
  Close;
end;

procedure TfrmBPZDY_LRPM.FormShow(Sender: TObject);
begin
  initTree();
  InitGrid();
  ReadCxGrid('泡棉选择', TV1, '自定义数据');
  frmBPZDY_LRPM.Caption := Trim(flagname);
  ToolButton1.Visible := true;

end;

procedure TfrmBPZDY_LRPM.BPNamehange(Sender: TObject);
var
  fsj: string;
begin
  if Trim(BPNameM.Text) <> '' then
  begin
    fsj := ' BPName like ''' + '%' + Trim(BPNameM.Text) + '%' + '''';
  end;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, fsj);
    SCreateCDS20(ADOQueryMain, ClientDataSet1);
    SInitCDSData20(ADOQueryMain, ClientDataSet1);
  end;
end;

procedure TfrmBPZDY_LRPM.v1note1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPMiDu').Value := mvalue;
    Post;
  end;
end;

procedure TfrmBPZDY_LRPM.V1BPCodePropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPCode').Value := mvalue;
  end;
end;

procedure TfrmBPZDY_LRPM.V1BPNamePropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('BPName').Value := mvalue;
  end;
end;

procedure TfrmBPZDY_LRPM.ToolButton4Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if ClientDataSet1.IsEmpty then
    exit;
  if ClientDataSet1.FieldByName('BPID').AsString = '' then
  begin
    application.MessageBox('请保存主信息', '提示');
    exit;
  end;
  with ML_GYS do
  begin
    Append;
    post;
  end;
end;

procedure TfrmBPZDY_LRPM.BPCodeMChange(Sender: TObject);
var
  fsj: string;
begin
  if Trim(BPCodeM.Text) <> '' then
  begin
    fsj := ' BPCode like ''' + '%' + Trim(BPCodeM.Text) + '%' + '''';
  end;
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, fsj);
    SCreateCDS20(ADOQueryMain, ClientDataSet1);
    SInitCDSData20(ADOQueryMain, ClientDataSet1);
  end;
end;

procedure TfrmBPZDY_LRPM.Tv3CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  i: Integer;
begin
  if ML_GX.IsEmpty then
    exit;
  ML_GX.Delete;
  i := 1;
  ML_GX.DisableControls;
  with ML_GX do
  begin
    first;
    while not eof do
    begin
      Edit;
      FieldByName('BPMiDu').Value := i;
      i := i + 1;
      next;
    end;
  end;
  ML_GX.EnableControls;
end;

procedure TfrmBPZDY_LRPM.ToolButton1Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  FInt := '1';
  ModalResult := 1;
end;

procedure TfrmBPZDY_LRPM.cxDBTreeList1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmBPZDY_LRPM.FormDestroy(Sender: TObject);
begin
  frmBPZDY_LRPM := nil;
end;

procedure TfrmBPZDY_LRPM.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
end;

end.

