unit U_TradeInspEdit;

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, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, dxSkinWXI, dxScrollbarAnnotations,
  cxContainer, cxMaskEdit, cxDropDownEdit;

type
  TfrmTradeInspEdit = class(TForm)
    ToolBar1: TToolBar;
    ToolButton2: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CIID: TcxTextEdit;
    ADOTmp: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_MJ: TClientDataSet;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    Label2: TLabel;
    GrossWeight: TcxTextEdit;
    Label4: TLabel;
    Label6: TLabel;
    Meter: TcxTextEdit;
    Ma: TLabel;
    Label7: TLabel;
    MJQty1: TcxTextEdit;
    Label5: TLabel;
    Label10: TLabel;
    Label12: TLabel;
    Label14: TLabel;
    NetWeight: TcxTextEdit;
    PieceNo: TcxTextEdit;
    Label15: TLabel;
    Label11: TLabel;
    Yardage: TcxTextEdit;
    ToolButton4: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    TV1Column1: TcxGridDBColumn;
    Label3: TLabel;
    grade: TcxComboBox;
    TV1Column2: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure tv2CDWZPropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesEditValueChanged(Sender: TObject);
    procedure TV1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveData(): Boolean;
    procedure GetJYXK(FJYIP: string);
  public
    { Public declarations }
  end;

var
  frmTradeInspEdit: TfrmTradeInspEdit;

implementation

uses
  U_RTFun, U_ZDYHelp, U_DataLink;

{$R *.dfm}
procedure TfrmTradeInspEdit.GetJYXK(FJYIP: string);
begin
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from Trade_Cloth_Inspect A  ');
    sql.Add(' where A.CIID=''' + Trim(FJYIP) + '''');
    Open;
  end;
  if ADOTmp.IsEmpty then
  begin
    CIID.Text := '';
    Label2.Caption := '';
    Label2.Visible := False;
    Application.MessageBox('条码错误!', '提示', 0);
    Exit;
  end;
  PieceNo.Text := Trim(ADOTmp.fieldbyname('PieceNo').AsString);

  GrossWeight.Text := Trim(ADOTmp.fieldbyname('GrossWeight').AsString);
  NetWeight.Text := Trim(ADOTmp.fieldbyname('NetWeight').AsString);
  Meter.Text := Trim(ADOTmp.fieldbyname('Meter').AsString);
  Yardage.Text := Trim(ADOTmp.fieldbyname('Yardage').AsString);

  grade.ItemIndex := grade.Properties.Items.IndexOf(Trim(ADOTmp.fieldbyname('grade').AsString));
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from Trade_Cloth_Inspect_Flaw A   where A.CIID=''' + Trim(CIID.Text) + '''');
    Open;
  end;
  Label2.Caption := Trim(CIID.Text);
  Label2.Visible := True;
  CIID.Text := '';
  SCreateCDS(ADOTmp, Order_MJ);
  SInitCDSData(ADOTmp, Order_MJ);

end;

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

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

procedure TfrmTradeInspEdit.FormDestroy(Sender: TObject);
begin
  frmTradeInspEdit := nil;
end;

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

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

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

procedure TfrmTradeInspEdit.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption), Tv1, '印染检验管理');
end;

procedure TfrmTradeInspEdit.TV1Column1PropertiesButtonClick(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('CIFName').value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

function TfrmTradeInspEdit.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('CIFName').AsString) = '' then
          raise Exception.Create(pchar('疵点名称不能为空！'));

        if Trim(Order_MJ.fieldbyname('CIFID').AsString) = '' then
        begin
          if GetLSNo(ADOTmp, maxno, 'JC', 'Trade_Cloth_Inspect_Flaw', 5, 1) = False then
            raise Exception.Create(pchar('取子流水号失败！'));
        end
        else
        begin
          maxno := Trim(Order_MJ.fieldbyname('CIFID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add(' select * from Trade_Cloth_Inspect_Flaw ');
          sql.Add(' where CIFID=''' + Trim(maxno) + ''' ');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_MJ.fieldbyname('CIFID').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('CIID').Value := Trim(Label2.Caption);
          FieldByName('CIFID').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_MJ, 'Trade_Cloth_Inspect_Flaw', 0);
          Post;
        end;

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

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' UPdate Trade_Cloth_Inspect  Set  ');
      sql.Add(' PieceNo=''' + Trim(PieceNo.Text) + ''' ');
      sql.Add(',GrossWeight=' + FloatToStr(Strtofloatdef(GrossWeight.Text, 0)));
      sql.Add(',NetWeight=' + FloatToStr(Strtofloatdef(NetWeight.Text, 0)));
      sql.Add(',Meter=' + FloatToStr(Strtofloatdef(Meter.Text, 0)));
      sql.Add(',Yardage=' + FloatToStr(Strtofloatdef(Yardage.Text, 0)));

      sql.Add(',Grade=' + QuotedStr(grade.Text));

      sql.Add(' where CIID=''' + Trim(Label2.Caption) + '''');
      ExecSQL;
    end;
   //////////////////       更新卷号疵点信息       //////////////////
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Trade_Insp_Up_pack @CIID=''' + Trim(Label2.Caption) + '''');
      sql.Add(',@InspType=' + QuotedStr('单卷修改'));
      sql.Add(',@RulePieceNo=' + QuotedStr('手动生成'));     //卷号生成规则
      execsql;
    end;
//////////////////       更新卷号疵点信息       //////////////////
///
//////////////////       出入库及库存       //////////////////
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Trade_Insp_In_Stk @CIID=''' + Trim(Label2.Caption) + '''');
      execsql;
    end;
//////////////////       出入库及库存       //////////////////
///
//////////////////       更新进度       //////////////////
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Trade_Need_Up(UOperation,UType,UDataId) values(''检验'',''检验编辑'',' + quotedstr(Trim(Label2.Caption)) + ')  ');
      execsql;
    end;
//////////////////       更新进度       //////////////////
///
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTradeInspEdit.ToolButton2Click(Sender: TObject);
begin
  if Trim(Label2.Caption) = '' then
    Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    Exit;
  end;
end;

procedure TfrmTradeInspEdit.TBCloseClick(Sender: TObject);
begin
  Close;

end;

procedure TfrmTradeInspEdit.FormShow(Sender: TObject);
begin
  ReadCxGrid(trim(Self.Caption), Tv1, '印染检验管理');
  if CIID.Text <> '' then
  begin
    GetJYXK(CIID.Text);
  end;

end;

end.

