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, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmMJEdit = class(TForm)
    ToolBar1: TToolBar;
    ToolButton2: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    JYID: 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;
    JYGW: TEdit;
    Label4: TLabel;
    M: TLabel;
    Label6: TLabel;
    JYLen: TEdit;
    RadioGroup1: TRadioGroup;
    Label10: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    JYNW: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    JYLen1: TEdit;
    Label11: TLabel;
    PerSon1: TEdit;
    Label9: TLabel;
    Label12: TLabel;
    JYLen2: TEdit;
    Label13: TLabel;
    PerSon2: TEdit;
    Label14: TLabel;
    Label15: TLabel;
    JYLen3: TEdit;
    Label16: TLabel;
    PerSon3: TEdit;
    Label17: TLabel;
    Label18: TLabel;
    JYLen4: TEdit;
    Label19: TLabel;
    PerSon4: TEdit;
    Label20: TLabel;
    Label21: TLabel;
    JYLen5: TEdit;
    Label22: TLabel;
    PerSon5: TEdit;
    Label23: TLabel;
    Label24: TLabel;
    BatchNO: TEdit;
    wxph: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure JYIDKeyPress(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);
    procedure tv2CDWZPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure JYLen1Exit(Sender: TObject);
    procedure PerSon1Click(Sender: TObject);
    procedure PerSon2Click(Sender: TObject);
    procedure PerSon3Click(Sender: TObject);
    procedure PerSon4Click(Sender: TObject);
    procedure PerSon5Click(Sender: TObject);
  private
    { Private declarations }
    function SaveData(): Boolean;
  public
    { Public declarations }
  end;

var
  frmMJEdit: TfrmMJEdit;

implementation

uses
  U_Fun, U_ZDYHelp, U_DataLink, U_SCPerson1;

{$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.JYIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_Cloth_CR where JYID=''' + Trim(JYID.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 JY_Cloth A');
      sql.Add(' where A.JYID=''' + Trim(JYID.Text) + '''');
//      ShowMessage(sql.text);
      Open;
    end;
    if ADOTmp.IsEmpty then
    begin
      JYID.Text := '';
      Label2.Caption := '';
      Label2.Visible := False;
      Application.MessageBox('条码错误!', '提示', 0);
      Exit;
    end;
    JYGW.Text := Trim(ADOTmp.fieldbyname('JYGW').AsString);
    JYLen.Text := Trim(ADOTmp.fieldbyname('JYLen').AsString);
    JYNW.Text := Trim(ADOTmp.fieldbyname('JYNW').AsString);
    PerSon1.Text := Trim(ADOTmp.fieldbyname('PerSon1').AsString);
    PerSon2.Text := Trim(ADOTmp.fieldbyname('PerSon2').AsString);
    PerSon3.Text := Trim(ADOTmp.fieldbyname('PerSon3').AsString);
    PerSon4.Text := Trim(ADOTmp.fieldbyname('PerSon4').AsString);
    PerSon5.Text := Trim(ADOTmp.fieldbyname('PerSon5').AsString);
    JYLen1.Text := Trim(ADOTmp.fieldbyname('JYLen1').AsString);
    JYLen2.Text := Trim(ADOTmp.fieldbyname('JYLen2').AsString);
    JYLen3.Text := Trim(ADOTmp.fieldbyname('JYLen3').AsString);
    JYLen4.Text := Trim(ADOTmp.fieldbyname('JYLen4').AsString);
    JYLen5.Text := Trim(ADOTmp.fieldbyname('JYLen5').AsString);

    batchno.Text := Trim(ADOTmp.fieldbyname('batchno').AsString);
    wxph.Text := Trim(ADOTmp.fieldbyname('wxph').AsString);
    if Trim(Trim(ADOTmp.fieldbyname('JYType').AsString)) = '正品' then
    begin
      RadioGroup1.ItemIndex := 0
    end
    else if Trim(Trim(ADOTmp.fieldbyname('JYType').AsString)) = '次品' then
    begin
      RadioGroup1.ItemIndex := 1;
    end;
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JY_Cloth_CD A   where A.JYID=''' + Trim(JYID.Text) + '''');
      Open;
    end;
    Label2.Caption := Trim(JYID.Text);
    Label2.Visible := True;
    JYID.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 := 'GreyCD';
      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('JCID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JY_Cloth_CD where JCID=''' + Trim(Order_MJ.fieldbyname('JCID').AsString) + '''');
//      ShowMessage(sql.text);
      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('JCID').AsString) = '' then
        begin
          if GetLSNo(ADOTmp, maxno, 'JC', 'JY_Cloth_CD', 5, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_MJ.fieldbyname('JCID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JY_Cloth_CD  ');
          sql.Add(' where JCID=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_MJ.fieldbyname('JCID').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('JYId').Value := Trim(Label2.Caption);
          FieldByName('JCID').Value := Trim(maxno);
          SSetSaveDataCDSNew(ADOCmd, Tv2, Order_MJ, 'JY_Cloth_CD', 0);
          Post;
        end;

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

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JY_Cloth Set  ');
      sql.Add('JYLen=' + FloatToStr(StrToFloatDef(JYLen.Text, 0)));
      sql.Add(',JYGW=' + FloatToStr(StrToFloatDef(JYGW.Text, 0)));
      sql.Add(',JYNW=' + FloatToStr(StrToFloatDef(JYNW.Text, 0)));

      sql.Add(',BatchNO=' + QuotedStr(BatchNO.Text));
      sql.Add(',wxph=' + QuotedStr(wxph.Text));

      sql.Add(',Person1=' + QuotedStr(Person1.Text));
      sql.Add(',Person2=' + QuotedStr(Person2.Text));
      sql.Add(',Person3=' + QuotedStr(Person3.Text));
      sql.Add(',Person4=' + QuotedStr(Person4.Text));
      sql.Add(',Person5=' + QuotedStr(Person5.Text));
      sql.Add(',JYLen1=' + FloatToStr(StrToFloatDef(JYLen1.Text, 0)));
      sql.Add(',JYLen2=' + FloatToStr(StrToFloatDef(JYLen2.Text, 0)));
      sql.Add(',JYLen3=' + FloatToStr(StrToFloatDef(JYLen3.Text, 0)));
      sql.Add(',JYLen4=' + FloatToStr(StrToFloatDef(JYLen4.Text, 0)));
      sql.Add(',JYLen5=' + FloatToStr(StrToFloatDef(JYLen5.Text, 0)));
      sql.Add(',JYTare=' + FloatToStr(StrToFloatDef(JYGW.Text, 0) - StrToFloatDef(JYNW.Text, 0)));
      if RadioGroup1.ItemIndex = 0 then
      begin
        sql.Add(',JYType=''正品'' ');
      end
      else if RadioGroup1.ItemIndex = 1 then
      begin
        sql.Add(',JYType=''次品'' ');
      end;
      sql.Add(' where JYID=''' + Trim(Label2.Caption) + '''');
      ExecSQL;
    end;

    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update JY_Cloth SET ');
      sql.Add('CDList=cast((select CDName+CAST(CDQty as varchar)+''M;'' from JY_Cloth_CD where JY_Cloth_CD.JYID=JY_Cloth.JYID for xml path('''')) as varchar(200))');
      sql.Add('where JYID=''' + Trim(Label2.Caption) + ''' ');
//      showmessage(sql.text);
      execsql;
    end;

     //出入库及库存
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_CK_Cloth_CRK @JYID=''' + 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;

procedure TfrmMJEdit.tv2CDWZPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmSCPerson1 := TfrmSCPerson1.Create(self);
    with frmSCPerson1 do
    begin
      FlagStr := 'SCPerson';
      if ShowModal = 1 then
      begin
        Order_MJ.Edit;
        Order_MJ.FieldByName('DCheKouK').Value := frmSCPerson1.FSDPerson;
      end;
    end;
  finally
    frmSCPerson1.Free;
  end;
end;

procedure TfrmMJEdit.JYLen1Exit(Sender: TObject);
begin
  JYLen.text := FloatToStr(StrToFloatDef(JYLen1.Text, 0) + StrToFloatDef(JYLen2.Text, 0) + StrToFloatDef(JYLen3.Text, 0) + StrToFloatDef(JYLen4.Text, 0) + StrToFloatDef(JYLen5.Text, 0));
end;

procedure TfrmMJEdit.PerSon1Click(Sender: TObject);
begin
  try
    frmSCPerson1 := TfrmSCPerson1.Create(self);
    with frmSCPerson1 do
    begin
      FlagStr := 'SCPerson';
      if ShowModal = 1 then
      begin
        PerSon1.text := frmSCPerson1.FSDPerson;
      end;
    end;
  finally
    frmSCPerson1.Free;
  end;
end;

procedure TfrmMJEdit.PerSon2Click(Sender: TObject);
begin
  try
    frmSCPerson1 := TfrmSCPerson1.Create(self);
    with frmSCPerson1 do
    begin
      FlagStr := 'SCPerson';
      if ShowModal = 1 then
      begin
        PerSon2.text := frmSCPerson1.FSDPerson;
      end;
    end;
  finally
    frmSCPerson1.Free;
  end;
end;

procedure TfrmMJEdit.PerSon3Click(Sender: TObject);
begin
  try
    frmSCPerson1 := TfrmSCPerson1.Create(self);
    with frmSCPerson1 do
    begin
      FlagStr := 'SCPerson';
      if ShowModal = 1 then
      begin
        PerSon3.text := frmSCPerson1.FSDPerson;
      end;
    end;
  finally
    frmSCPerson1.Free;
  end;
end;

procedure TfrmMJEdit.PerSon4Click(Sender: TObject);
begin
  try
    frmSCPerson1 := TfrmSCPerson1.Create(self);
    with frmSCPerson1 do
    begin
      FlagStr := 'SCPerson';
      if ShowModal = 1 then
      begin
        PerSon4.text := frmSCPerson1.FSDPerson;
      end;
    end;
  finally
    frmSCPerson1.Free;
  end;
end;

procedure TfrmMJEdit.PerSon5Click(Sender: TObject);
begin
  try
    frmSCPerson1 := TfrmSCPerson1.Create(self);
    with frmSCPerson1 do
    begin
      FlagStr := 'SCPerson';
      if ShowModal = 1 then
      begin
        PerSon5.text := frmSCPerson1.FSDPerson;
      end;
    end;
  finally
    frmSCPerson1.Free;
  end;
end;

end.

