unit U_GDKHInPut;

interface

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

type
  TfrmGDKHInPut = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    v1PRTColor: TcxGridDBColumn;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    v1Column2: TcxGridDBColumn;
    Label54: TLabel;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label7: TLabel;
    Label3: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label2: TLabel;
    Label6: TLabel;
    Label25: TLabel;
    Label26: TLabel;
    Label41: TLabel;
    Label43: TLabel;
    Label51: TLabel;
    Label53: TLabel;
    Label57: TLabel;
    JGJQ: TDateTimePicker;
    OrderNo: TEdit;
    DLYDate: TDateTimePicker;
    OrdDate: TDateTimePicker;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    MPRTCode: TEdit;
    MPRTCodeName: TEdit;
    MPRTSpec: TEdit;
    MPRTMF: TEdit;
    GXType: TEdit;
    YWY: TEdit;
    HCKZ: TEdit;
    MPRTKZ: TEdit;
    GDY: TEdit;
    Factory: TEdit;
    Label4: TLabel;
    ZQty: TEdit;
    Label5: TLabel;
    OrderUnit: TEdit;
    v1Column1: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure v1Column4PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column22PropertiesChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column27PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    { Private declarations }
  public
    PState,CopyInt,PriceFlag:Integer;
    FMainId,FGKMainId,FFMainId,FOrderNo:String;
    FXS:Integer;
    canshu1:string;
    fFlileFlag:string;
    { Public declarations }
  end;

var
  frmGDKHInPut: TfrmGDKHInPut;
  newh:hwnd;
implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_ZDYHelpSel, U_ZdyAttachment, U_ZdyAttachGYS, U_GDKBRKListSel,
  getpic;

{$R *.dfm}

procedure TfrmGDKHInPut.TBCloseClick(Sender: TObject);
begin
  OrderNo.SetFocus;
  Close;
  WriteCxGrid(trim(Self.Caption),Tv1,'生产指示单管理');
end;

procedure TfrmGDKHInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.MainId,A.OrderNo,B.DLYDate,B.OrdDate,MPRTSpec,ConNo,MPRTCode,MPRTCodeName,HCKZ,MPRTMF,MPRTKZ  ');
    SQL.Add(',ZQty=(select SUM(PRTOrderQty) from JYOrder_Sub X where X.MainId=A.MainId)');
    SQL.Add('	,OrderUnit=(select MAX(OrderUnit) from JYOrder_Sub X where X.MainId=A.MainId)');
    SQL.Add('	,A.YWY,A.Factory,A.GXType,A.GDY,A.JGJQ');
    SQL.Add('  from GDKH_Main A left join JYOrder_Main B on A.MainId=B.MainId');
    sql.Add(' Where A.GKMainID='''+Trim(FGKMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,ScrollBox1,2);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from  GDKH_Sub ');
    sql.Add(' Where GKMainID='''+Trim(FGKMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  
end;

procedure TfrmGDKHInPut.ZDYHelp(FButn:TcxButtonEdit;LType:string);
begin
end;

procedure TfrmGDKHInPut.FormShow(Sender: TObject);
begin
  readCxGrid(trim(Self.Caption),Tv1,'生产指示单管理');
  InitData();
  if trim(canshu1)<>'高权限' then
  begin
    v1Column24.Options.Focusing:=false;
    v1Column25.Options.Focusing:=false;
    v1Column1.Options.Focusing:=false;
    v1Column26.Options.Focusing:=false;
  end;
end;



procedure TfrmGDKHInPut.FormCreate(Sender: TObject);
begin
//  canshu1:=trim(DParameters1);
end;

procedure TfrmGDKHInPut.v1Column4PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate GDKH_Sub ');
    sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    SQL.Add(' where GkSubID='''+Trim(Order_Sub.fieldbyname('GkSubID').AsString)+'''');
    ExecSQL;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmGDKHInPut.v1Column22PropertiesChange(Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate GDKH_Sub ');
    sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
    SQL.Add(' where GkSubID='''+Trim(Order_Sub.fieldbyname('GkSubID').AsString)+'''');
    ExecSQL;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmGDKHInPut.ToolButton3Click(Sender: TObject);
var
  FGKSubID:string;
begin
  try
    frmGDKBRKListSel:=TfrmGDKBRKListSel.Create(Application);
    with frmGDKBRKListSel do
    begin
      frmGDKBRKListSel.FMainId:=Self.FMainId;

      if ShowModal=1 then
      begin
        if GetLSNo(ADOCmd,FGKSubID,'GKS','GDKH_Sub',3,1)=False then
        begin
          Application.MessageBox('取跟单考核最大号失败!','提示',0);
          Exit;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into GDKH_Sub(GkMainID,GkSubID,SubID,GDSJSL,HCtime,GDTPSL,GDColor,GDGH,GDMF,GDKZ) values('
          +quotedstr(Trim(FGKMainID))
          +','+quotedstr(Trim(FGKSubID))
          +','+quotedstr(Trim(CDS_Main.FieldByName('OrdSubIdRK').AsString))
          +','+quotedstr(Trim(FloatToStr(CDS_Main.FieldByName('qty').AsFloat)))
          +','+quotedstr(Trim(FormatDateTime('yyyy-MM-dd',CDS_Main.FieldByName('CRTime').AsDateTime)))
          +','+quotedstr(Trim(FloatToStr(CDS_Main.FieldByName('tpqty').AsFloat)))
          +','+quotedstr(Trim(CDS_Main.FieldByName('PRTColor').AsString))
          +','+quotedstr(Trim(CDS_Main.FieldByName('RCGangNo').AsString))
          +','+quotedstr(Trim(CDS_Main.FieldByName('SPMF').AsString))
          +','+quotedstr(Trim(CDS_Main.FieldByName('SPKZ').AsString))
          +')');
          ExecSQL;
        end;
        with Order_Sub do
        begin
          Append;
          fieldbyname('GkMainID').value:= Trim(FGKMainID);
          fieldbyname('GkSubID').value:= Trim(FGkSubID);
          fieldbyname('SubID').value:= Trim(CDS_Main.FieldByName('OrdSubIdRK').AsString);
          fieldbyname('GDSJSL').value:= CDS_Main.FieldByName('qty').AsFloat;
          fieldbyname('GDTPSL').value:= CDS_Main.FieldByName('tpqty').AsFloat;
          fieldbyname('HCtime').value:= Trim(FormatDateTime('yyyy-MM-dd',CDS_Main.FieldByName('CRTime').AsDateTime));
          fieldbyname('GDColor').value:= Trim(CDS_Main.FieldByName('PRTColor').AsString);
          fieldbyname('GDGH').value:= Trim(CDS_Main.FieldByName('RCGangNo').AsString);
          fieldbyname('GDMF').value:= Trim(CDS_Main.FieldByName('SPMF').AsString);
          fieldbyname('GDKZ').value:= Trim(CDS_Main.FieldByName('SPKZ').AsString);
          Post;
        end;
      end;
    end;
  finally
    frmGDKBRKListSel.Free;
  end;
end;

procedure TfrmGDKHInPut.ToolButton1Click(Sender: TObject);
var
  FGKSubID:string;
begin
  if GetLSNo(ADOCmd,FGKSubID,'GKS','GDKH_Main',3,1)=False then
  begin
    Application.MessageBox('取跟单考核最大号失败!','提示',0);
    Exit;
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into GDKH_Sub(GkMainID,GkSubID) values('
    +quotedstr(Trim(FGKMainID))
    +','+quotedstr(Trim(FGKSubID))
//    +','+quotedstr(Trim(ADOQueryTemp.FieldByName('SubID').AsString))     ,SubID
    +')');
    ExecSQL;
  end;
  with Order_Sub do
  begin
    Append;
    fieldbyname('GkMainID').value:= Trim(FGKMainID);
    fieldbyname('GkSubID').value:= Trim(FGkSubID);
    Post;
  end;
end;

procedure TfrmGDKHInPut.ToolButton2Click(Sender: TObject);
begin
 with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete GDKH_Sub where GkSubID='+quotedstr(Trim(Order_Sub.fieldbyname('GkSubID').AsString)) );
    ExecSQL;
  end;
  Order_Sub.Delete;
end;

procedure TfrmGDKHInPut.v1Column15PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate GDKH_Sub ');
    sql.Add(' Set '+FFieldName+'='+quotedstr(Trim(mvalue)));
    SQL.Add(' where GkSubID='''+Trim(Order_Sub.fieldbyname('GkSubID').AsString)+'''');
    ExecSQL;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmGDKHInPut.v1Column27PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
var
  mvalue,FFieldName,FCaption:string;
  FWZ:Integer;
begin
  FFieldName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  FCaption:=Trim( Tv1.Controller.FocusedColumn.Caption);
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=FFieldName+'1';
      flagname:=FCaption;
      if ShowModal=1 then
      begin
        mvalue:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        with Order_Sub do
        begin
          Edit;
          FieldByName(FFieldName).Value:=Trim(mvalue);
          Post;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate GDKH_Sub ');
          sql.Add(' Set '+FFieldName+'='+quotedstr(Trim(mvalue)));
          SQL.Add(' where GkSubID='''+Trim(Order_Sub.fieldbyname('GkSubID').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    Tv1.Controller.EditingController.ShowEdit();
    frmZDYHelp.Free;
  end;

end;

end.
