unit U_MJEdit;

interface

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

type
  TfrmMJEdit = class(TForm)
    ToolBar1: TToolBar;
    ToolButton2: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOTmp: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_MJ: TClientDataSet;
    Label2: TLabel;
    JYKG: TEdit;
    Label4: TLabel;
    Label6: TLabel;
    JYLen: TEdit;
    Label10: TLabel;
    Label12: TLabel;
    Label14: TLabel;
    JYJZ: TEdit;
    JYNo: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    GangNo: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label1: TLabel;
    Label3: TLabel;
    JYPZ: TEdit;
    JYUnit: TComboBox;
    Label5: TLabel;
    Label7: TLabel;
    LBLen: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    function SaveData(): Boolean;
    procedure Initgrid();
  public
    FJYID: string;
    { Public declarations }
  end;

var
  frmMJEdit: TfrmMJEdit;

implementation

uses
  U_Fun, U_ZDYHelp, U_DataLink;

{$R *.dfm}
procedure TfrmMJEdit.Initgrid();
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JTJY_New where JYID=''' + trim(FJYID) + '''');
    open;
  end;
  SCSHDataNew(ADOQueryMain, Panel1, 0);
end;

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

procedure TfrmMJEdit.FormDestroy(Sender: TObject);
begin
  frmMJEdit := nil;
end;

function TfrmMJEdit.SaveData(): Boolean;
var
  maxno, FSubid, FMainId, FYFID: string;
  FFreal: Double;
begin
  try
    ADOCmd.Connection.BeginTrans;
    if Trim(JYKG.Text) <> '' then
    begin
      if TryStrToFloat(JYKG.Text, FFreal) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('毛重非法数字!', '提示', 0);
        Exit;
      end;
    end;
    if Trim(JYJZ.Text) <> '' then
    begin
      if TryStrToFloat(JYJZ.Text, FFreal) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('净重非法数字!', '提示', 0);
        Exit;
      end;
    end;
    if Trim(JYPZ.Text) <> '' then
    begin
      if TryStrToFloat(JYPZ.Text, FFreal) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('皮重非法数字!', '提示', 0);
        Exit;
      end;
    end;
    if Trim(JYLen.Text) <> '' then
    begin
      if TryStrToFloat(JYLen.Text, FFreal) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('长度非法数字!', '提示', 0);
        Exit;
      end;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JTJY_New Set JYNO=''' + Trim(JYNO.Text) + '''');
      sql.Add(',GangNo=''' + Trim(GangNo.Text) + '''');
      sql.Add(',JYKG=''' + Trim(JYKG.Text) + '''');
      if StrToFloatdef(Trim(LBLen.Text), 0) > 0 then
      begin
        sql.Add(',LBLen=''' + Trim(LBLen.Text) + '''');
      end
      else
      begin
        sql.Add(',LBLen=null');
      end;
      sql.Add(',JYJZ=''' + Trim(JYJZ.Text) + '''');
      sql.Add(',JYLen=''' + Trim(JYLen.Text) + '''');
      sql.Add(',JYUnit=''' + Trim(JYUnit.Text) + '''');
      sql.Add(',JYPZ=''' + Trim(JYPZ.Text) + '''');
      sql.Add(' where JYID=''' + trim(FJYID) + '''');
//      ShowMessage(sql.text);
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate CK_MLCP_CR Set MJXH=''' + Trim(JYNO.Text) + '''');
      sql.Add(',GangNo=''' + Trim(GangNo.Text) + '''');
      sql.Add(',KGQty=''' + Trim(JYKG.Text) + '''');
      sql.Add(',NetKGQty=''' + Trim(JYJZ.Text) + '''');
      sql.Add(',Qty=''' + Trim(JYLen.Text) + '''');
      sql.Add(',QtyUnit=''' + Trim(JYUnit.Text) + '''');
      sql.Add(' where MJID=''' + trim(FJYID) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JTJY_New where JYID=''' + Trim(FJYID) + '''');
      Open;
    end;
    FSubid := Trim(ADOCmd.fieldbyname('NewSubid').AsString);
    FMainId := Trim(ADOCmd.fieldbyname('NewMLid').AsString);
    if Trim(FSubid) <> '' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' exec P_Update_OrderSub :SubId');
        Parameters.ParamByName('SubId').Value := Trim(FSubid);
        ExecSQL;
      end;
    end;
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JTJY_New where JYID=''' + Trim(FJYID) + '''');
      Open;
    end;
    FYFID := Trim(ADOTmp.fieldbyname('YFID').AsString);
    if Trim(FYFID) <> '' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' exec P_Update_THData :YFID');
        Parameters.ParamByName('YFID').Value := Trim(FYFID);
        ExecSQL;
      end;
    end;
    //更新棉&无纺布耗用
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      //实际
      sql.Add('Update ML_OrderZuHe Set MLJYQty=(select sum(JYLen) from JTJY_New A where A.NewMLID=ML_OrderZuHe.NewMLID and A.JYType=''成品'') ');
      sql.Add(' where NewMLID=''' + Trim(FMainId) + '''');
      sql.Add(' and isnull(MLName,'''')<>'''' and SSGS=''自己'' ');

      ExecSQL;
    end;
    //更新棉&无纺布耗用

    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmMJEdit.ToolButton2Click(Sender: TObject);
begin
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmMJEdit.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmMJEdit.FormShow(Sender: TObject);
begin
  initgrid();
end;

end.

