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;
    Label1: TLabel;
    MJID: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    tv2CDType: TcxGridDBColumn;
    tv2CDWZ: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    Tv2CDQty: TcxGridDBColumn;
    Tv2CDReason: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOTmp: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_MJ: TClientDataSet;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    Label2: TLabel;
    MJGW: TEdit;
    Label4: TLabel;
    M: TLabel;
    Label6: TLabel;
    MJLen: TEdit;
    RadioGroup1: TRadioGroup;
    Label10: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    MJNW: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure MJIDKeyPress(Sender: TObject; var Key: Char);
    procedure tv2CDTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure tv2CDWZPropertiesEditValueChanged(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    function SaveData(): Boolean;
  public
    { Public declarations }
  end;

var
  frmMJEdit: TfrmMJEdit;

implementation

uses
  U_Fun, U_ZDYHelp, U_DataLink;

{$R *.dfm}

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

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

procedure TfrmMJEdit.MJIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from FinishedCloth_CR where MJID=''' + Trim(MJID.Text) + ''' and CRFlag=''入库''');
      Open;
    end;
    if ADOTmp.IsEmpty = False then
    begin
      Application.MessageBox('已产生入库数据不能编辑!', '提示', 0);
      Exit;
    end;
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select A.*  from FinishedCloth_Inspect A');
      sql.Add(' where A.MJID=''' + Trim(MJID.Text) + '''');
      Open;
    end;
    if ADOTmp.IsEmpty then
    begin
      MJID.Text := '';
      Label2.Caption := '';
      Label2.Visible := False;
      Application.MessageBox('条码错误!', '提示', 0);
      Exit;
    end;
    MJGW.Text := Trim(ADOTmp.fieldbyname('MJGW').AsString);
    MJLen.Text := Trim(ADOTmp.fieldbyname('MJLen').AsString);
    MJNW.Text := Trim(ADOTmp.fieldbyname('MJNW').AsString);

    if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString)) = '正品' then
    begin
      RadioGroup1.ItemIndex := 0
    end
    else if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString)) = '次品' then
    begin
      RadioGroup1.ItemIndex := 1;
    end;
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from FinishedCloth_Inspect_CD A   where A.MJID=''' + Trim(MJID.Text) + '''');
      Open;
    end;
    Label2.Caption := Trim(MJID.Text);
    Label2.Visible := True;
    MJID.Text := '';
    SCreateCDS20(ADOTmp, Order_MJ);
    SInitCDSData20(ADOTmp, Order_MJ);

  end;
end;

procedure TfrmMJEdit.tv2CDTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'WFBCD';
      flagname := '疵点';
      if ShowModal = 1 then
      begin
        Self.Order_MJ.Edit;
        Self.Order_MJ.FieldByName('CDName').value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMJEdit.tv2CDWZPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, CDBeg, CDEnd: string;
  FFReal: Double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  with Order_MJ do
  begin
    Edit;
    FieldByName('CDBeg').Value := mvalue;
    Post;
  end;
  CDBeg := Trim(Order_MJ.fieldbyname('CDBeg').AsString);
  CDEnd := Trim(Order_MJ.fieldbyname('CDEnd').AsString);
  if (Trim(CDBeg) <> '') and (Trim(CDEnd) <> '') then
  begin
    if TryStrToFloat(CDBeg, FFReal) and TryStrToFloat(CDBeg, FFReal) then
    begin
      if StrToFloat(CDEnd) < StrToFloat(CDBeg) then
      begin
        Application.MessageBox('终止位置小于起始位置!', '提示', 0);
        Exit;
      end;
      with Order_MJ do
      begin
        Edit;
        FieldByName('CDQty').Value := StrToFloat(CDEnd) - StrToFloat(CDBeg);
        Post;
      end;
    end
    else
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end;
end;

procedure TfrmMJEdit.v2Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, CDBeg, CDEnd: string;
  FFReal: Double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  with Order_MJ do
  begin
    Edit;
    FieldByName('CDEnd').Value := mvalue;
    Post;
  end;
  CDBeg := Trim(Order_MJ.fieldbyname('CDBeg').AsString);
  CDEnd := Trim(Order_MJ.fieldbyname('CDEnd').AsString);
  if (Trim(CDBeg) <> '') and (Trim(CDEnd) <> '') then
  begin
    if TryStrToFloat(CDBeg, FFReal) and TryStrToFloat(CDBeg, FFReal) then
    begin
      if StrToFloat(CDEnd) < StrToFloat(CDBeg) then
      begin
        Application.MessageBox('终止位置小于起始位置!', '提示', 0);
        Exit;
      end;
      with Order_MJ do
      begin
        Edit;
        FieldByName('CDQty').Value := StrToFloat(CDEnd) - StrToFloat(CDBeg);
        Post;
      end;
    end
    else
    begin
      Application.MessageBox('非法数字!', '提示', 0);
      Exit;
    end;
  end;
end;

procedure TfrmMJEdit.ToolButton1Click(Sender: TObject);
begin
  if Trim(Label2.Caption) = '' then
    Exit;
  Order_MJ.Append;
  Order_MJ.Post;
end;

procedure TfrmMJEdit.ToolButton3Click(Sender: TObject);
begin
  if Trim(Label2.Caption) = '' then
    Exit;
  if Order_MJ.IsEmpty then
    Exit;
  if Trim(Order_MJ.fieldbyname('MCID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete FinishedCloth_Inspect_CD where MCID=''' + Trim(Order_MJ.fieldbyname('MCID').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_MJ.Delete;
end;

function TfrmMJEdit.SaveData(): Boolean;
var
  maxno: string;
  FFreal: Double;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存疵点表
    with Order_MJ do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_MJ.fieldbyname('MCID').AsString) = '' then
        begin
          if GetLSNo(ADOTmp, maxno, 'MC', 'FinishedCloth_Inspect_CD', 5, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_MJ.fieldbyname('MCID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from FinishedCloth_Inspect_CD  ');
          sql.Add(' where MCID=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_MJ.fieldbyname('MCID').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MJId').Value := Trim(Label2.Caption);
          FieldByName('MCID').Value := Trim(maxno);
          SSetSaveDataCDSNew(ADOCmd, Tv2, Order_MJ, 'FinishedCloth_Inspect_CD', 0);
          Post;
        end;

        Order_MJ.Edit;
        Order_MJ.FieldByName('MCID').Value := Trim(maxno);
        Order_MJ.FieldByName('MJID').Value := Trim(Label2.Caption);
        Next;
      end;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate WFB_MJJY Set  ');
      sql.Add(',MJLen=' + FloatToStr(StrToFloatDef(MJLen.Text, 0)));
      sql.Add(',MJGW=' + FloatToStr(StrToFloatDef(MJGW.Text, 0)));
      sql.Add(',MJNW=' + FloatToStr(StrToFloatDef(MJNW.Text, 0)));
      sql.Add(',MJTare=' + FloatToStr(StrToFloatDef(MJGW.Text, 0) - StrToFloatDef(MJNW.Text, 0)));
      if RadioGroup1.ItemIndex = 0 then
      begin
        sql.Add(',MJType=''正品'' ');
      end
      else if RadioGroup1.ItemIndex = 1 then
      begin
        sql.Add(',MJType=''次品'' ');
      end;
      sql.Add(' where MJID=''' + Trim(Label2.Caption) + '''');
      ExecSQL;
    end;
//    with ADOCmd do
//    begin
//      close;
//      sql.Clear;
//      sql.Add('update WFB_MJJY SET CDList=dbo.F_Get_Order_SubStr(MJID,''MJCDHZSL'')');
//      sql.Add('where MJID=''' + Trim(Label2.Caption) + ''' ');
//      execsql;
//    end;

     //出入库及库存
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_FinishedCloth_CRK @MJID=''' + Trim(Label2.Caption) + '''');
      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 Trim(Label2.Caption) = '' then
    Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    Exit;
  end;
end;

procedure TfrmMJEdit.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('布匹管理', Tv2, '染厂检验管理');
end;

procedure TfrmMJEdit.FormShow(Sender: TObject);
begin
  ReadCxGrid('布匹管理', Tv2, '染厂检验管理');
end;

end.

