unit U_JYOrderOther_Main;

interface

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

type
  TFrmJYOrderOther = class(TForm)
    Panel1: TPanel;
    OrderNo: TEdit;
    Label1: TLabel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    v1OrderNo: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1PRTCodeName: TcxGridDBColumn;
    Order_Sub: TClientDataSet;
    Order_Main: TClientDataSet;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DataSource2: TDataSource;
    ADOQuerySub: TADOQuery;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel2: TPanel;
    ToolBar3: TToolBar;
    ToolButton11: TToolButton;
    ToolButton12: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TVQty: TcxGridDBColumn;
    TVWeight: TcxGridDBColumn;
    TVLength: TcxGridDBColumn;
    TVHeight: TcxGridDBColumn;
    TVwidth: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel3: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton11: TSpeedButton;
    SpeedButton12: TSpeedButton;
    SpeedButton49: TSpeedButton;
    ToolButton1: TToolButton;
    procedure TBCloseClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure TV2CustomDrawIndicatorCell(Sender: TcxGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo;
      var ADone: Boolean);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton12Click(Sender: TObject);
    procedure SpeedButton49Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    Procedure InitData();
    Procedure Savedata();
    function JsXj(str1:string):string;
    { Private declarations }
  public
    fdatabase:string;
    { Public declarations }
  end;

var
  FrmJYOrderOther: TFrmJYOrderOther;

implementation

uses U_Fun,U_DataLink;
{$R *.dfm}

function  TFrmJYOrderOther.JsXj(str1:string):string;
begin
  {str1:=trim(str1);
  result:=str1;
  str1:=StringReplace(str1,'.','',[rfReplaceAll]);
  IF length(str1)<1 then exit;

  try
    //IF length(str1)>1 then
    //str1:=inttostr(strtoint(copy(str1,1,length(str1)-1)))+'.'+copy(str1,length(str1),1)
    //else
    str1:=copy(str1,length(str1),1);
    result:=str1;
  except
  end;}
end;

Procedure TFrmJYOrderOther.Savedata();
var
  FMainid: string;
begin

  try
  with order_Sub do
  begin
    first;
    while not eof do
    begin
      if Trim(Order_Sub.fieldbyname('Mainid').AsString)='' then
      begin
        if GetLSNo(ADOQuerySub,FMainid,'DD','JYOrder_Other',4,1)=False then
        begin
          Application.MessageBox('取子流水号失败！','提示',0);
          Exit;
        end;
      end else
      begin
        FMainid:=Trim(Order_Sub.fieldbyname('Mainid').AsString);
      end;
      with ADOQuerySub do
      begin
        close;
        sql.clear;
        sql.add('select * from JYOrder_Other ');
        sql.Add('where OrderNo='''+trim(Order_Main.fieldbyname('OrderNo').AsString)+''' ');
        sql.add('and PRTCodeName='''+trim(Order_Main.fieldbyname('PRTCodeName').AsString)+''' ');
        sql.add('and Mainid='''+trim(FMainid)+'''');
        open;
        if isempty then
        append
        else
        edit;
        fieldbyname('Mainid').asstring:=Trim(FMainid);
        fieldbyname('OrderNo').asstring:=Trim(Order_Main.fieldbyname('OrderNo').AsString);
        fieldbyname('PRTCodeName').asstring:=Trim(Order_Main.fieldbyname('PRTCodeName').AsString);
        fieldbyname('Qty').Value:=Order_Sub.fieldbyname('Qty').AsFloat;
        fieldbyname('Weight').Value:=Order_Sub.fieldbyname('Weight').AsFloat;
        fieldbyname('Length').Value:=Order_Sub.fieldbyname('Length').AsFloat;
        fieldbyname('width').Value:=Order_Sub.fieldbyname('width').AsFloat;
        fieldbyname('Height').Value:=Order_Sub.fieldbyname('Height').AsFloat;
        post;
      end;
      next;
    end;
  end;
  application.MessageBox('保存成功','提示');
  except
  application.MessageBox('保存失败','提示',0);
  end;
end;

Procedure TFrmJYOrderOther.InitData();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      close;
      sql.Clear;
      sql.add('select A.OrderNo,A.CustomerNoName,isnull(B.PRTCodeName,'''') as PRTCodeName  from JYOrder_Main A ');
      sql.add('Inner join JYOrder_Sub B on B.Mainid=A.Mainid ');
      sql.add('where 1=1 ');
      sql.add('Group by A.OrderNo,A.CustomerNoName,B.PRTCodeName');
      open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;  
end;

procedure TFrmJYOrderOther.TBCloseClick(Sender: TObject);
begin
  close;
  WriteCxGrid(self.Caption+Tv1.Name,Tv1,'坯布投坯1');
  WriteCxGrid(self.Caption+Tv2.Name,Tv2,'坯布投坯2');
end;

procedure TFrmJYOrderOther.OrderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TFrmJYOrderOther.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TFrmJYOrderOther.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+Tv1.Name,Tv1,'坯布投坯1');
  ReadCxGrid(self.Caption+Tv2.Name,Tv2,'坯布投坯2');

  IF trim(fdatabase)='查询'then
  begin
    ToolBar3.Visible:=false;
    tv2.OptionsData.Editing:=false;
  end;
  InitData();
end;

procedure TFrmJYOrderOther.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitData();
end;

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

procedure TFrmJYOrderOther.FormDestroy(Sender: TObject);
begin
  FrmJYOrderOther:=nil;
end;

procedure TFrmJYOrderOther.ToolButton2Click(Sender: TObject);
begin
  with Order_Sub do
  begin
    append;
    post;
  end;
end;

procedure TFrmJYOrderOther.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  if Order_Main.IsEmpty then exit;
  with ADOQuerySub do
  begin
    close;
    sql.clear;
    sql.Add('select * from JYOrder_Other ');
    sql.add('where OrderNo='''+trim(Order_Main.fieldbyname('OrderNo').AsString)+''' ');
    sql.add('and PRTCodeName='''+trim(Order_Main.fieldbyname('PRTCodeName').AsString)+''' ');
    open;
  end;
  SCreateCDS20(ADOQuerySub,Order_Sub);
  SInitCDSData20(ADOQuerySub,Order_Sub);
end;

procedure TFrmJYOrderOther.ToolButton3Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then exit;
  if application.MessageBox('确定要删除吗？','提示',1)=2 then exit;
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.add('delete from JYOrder_Other where Mainid='''+trim(Order_Sub.fieldbyname('Mainid').AsString)+'''');
    execsql;
  end;
  order_Sub.Delete;
end;

procedure TFrmJYOrderOther.TV2CustomDrawIndicatorCell(
  Sender: TcxGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
  FBounds := AViewInfo.Bounds;
  if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clDefault, clDefault, [bBottom, bLeft, bRight], 1);
    FValue :=IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index+1);
    InflateRect(FBounds, -3, -2);
    ACanvas.Font.Color := clDefault;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

procedure TFrmJYOrderOther.SpeedButton1Click(Sender: TObject);
var
  fsj:string;
begin
  fsj:=Trim(TSpeedButton(Sender).Hint);
  showmessage(Trim(TSpeedButton(Sender).Hint));
  if Trim(fsj)='' then Exit;
  fsj:=Trim(TcxTextEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).EditingText);
  TcxTextEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).EditingText:=fsj+Trim(TSpeedButton(Sender).Caption);
  TcxTextEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).SelectAll;
end;

procedure TFrmJYOrderOther.SpeedButton12Click(Sender: TObject);
var
  fsj:string;
begin
  fsj:=Trim(TcxTextEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).EditingText);
  if Trim(fsj)='' then Exit;
  TcxTextEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).EditingText:=Copy(fsj,1,Length(fsj)-1);
  TcxTextEdit(FindComponent(Trim(TSpeedButton(Sender).Hint))).SelectAll;
end;

procedure TFrmJYOrderOther.SpeedButton49Click(Sender: TObject);
begin
  Panel3.Visible:=False;
end;

procedure TFrmJYOrderOther.ToolButton1Click(Sender: TObject);
begin
 if order_Sub.IsEmpty then exit;
  Savedata();
 // initData();
end;

end.
