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;
    cxGridLevel1: TcxGridLevel;
    ADOTmp: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_MJ: TClientDataSet;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    Label2: TLabel;
    MJMaoZ: TEdit;
    Label4: TLabel;
    M: TLabel;
    Label6: TLabel;
    MJLen: TEdit;
    RadioGroup1: TRadioGroup;
    Ma: TLabel;
    Label7: TLabel;
    MJQty1: TEdit;
    Label5: TLabel;
    Label8: TLabel;
    MJFK: TEdit;
    Label9: TLabel;
    Label10: TLabel;
    Label3: TLabel;
    Label11: TLabel;
    MJSJKZ: TEdit;
    ToolButton4: TToolButton;
    Label13: TLabel;
    MJQty2: TEdit;
    Label12: TLabel;
    Label14: TLabel;
    MJQty4: TEdit;
    MJXH: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    MJStr4: TEdit;
    v2Column1: TcxGridDBColumn;
    MJQty5: TEdit;
    Label17: TLabel;
    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);
    procedure ToolButton4Click(Sender: TObject);
    procedure MDblClick(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 A.*,AOrddefstr3=(select AOrddefstr3 from JYOrder_Sub_AnPai B where B.APID=A.APID),  ');
      sql.Add('AOrddefstr2=(select AOrddefstr2 from JYOrder_Sub_AnPai B where B.APID=A.APID) from WFB_MJJY 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;
    MJXH.Text:=Trim(ADOTmp.fieldbyname('MJXH').AsString);
    MJstr4.Text:=Trim(ADOTmp.fieldbyname('MJstr4').AsString);
    MJMaoZ.Text:=Trim(ADOTmp.fieldbyname('MJMaoZ').AsString);
    MJLen.Text:=Trim(ADOTmp.fieldbyname('MJLen').AsString);
    MJQty1.Text:=Trim(ADOTmp.fieldbyname('MJQty1').AsString);
    MJQty2.Text:=Trim(ADOTmp.fieldbyname('MJQty2').AsString);
    MJQty4.Text:=Trim(ADOTmp.fieldbyname('MJQty4').AsString);
    MJQty5.Text:=Trim(ADOTmp.fieldbyname('MJQty5').AsString);
    MJFK.Text:=Trim(ADOTmp.fieldbyname('MJFK').AsString);
    MJSJKZ.Text:=Trim(ADOTmp.fieldbyname('MJSJKZ').AsString);
    M.Caption:=Trim(Trim(ADOTmp.fieldbyname('MJTypeOther').AsString));
    Ma.Caption:=Trim(Trim(ADOTmp.fieldbyname('MJStr1').AsString));
   { if Trim(m.Caption)='' then
    begin
      m.Caption:=Trim(ADOTmp.fieldbyname('AOrddefstr3').AsString);
    end;
    if Trim(Ma.Caption)='' then
    begin
      Ma.Caption:=Trim(ADOTmp.fieldbyname('AOrddefstr2').AsString);
    end; }
    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 else
    if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString))='留样' then
    begin
      RadioGroup1.ItemIndex:=2;
    end;
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from WFB_MJJY_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 WFB_MJJY_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','WFB_MJJY_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 WFB_MJJY_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,'WFB_MJJY_CD',0);
          IF trim(Order_MJ.fieldbyname('CDName').AsString)='测幅宽' then
           FieldByName('koufenQty').Value:=0
           else
          FieldByName('koufenQty').Value:=Order_MJ.fieldbyname('koufenQty').AsFloat;
          Post;
        end;

        Order_MJ.Edit;
        Order_MJ.FieldByName('MCID').Value:=Trim(maxno);
        Order_MJ.FieldByName('MJID').Value:=Trim(Label2.Caption);
        Next;
      end;
    end;
    if Trim(MJMaoZ.Text)<>'' then
    begin
      if TryStrToFloat(MJMaoZ.Text,FFreal)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('重量非法数字!','提示',0);
        Exit;
      end;
    end;
    if Trim(MJLen.Text)<>'' then
    begin
      if TryStrToFloat(MJLen.Text,FFreal)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('长度非法数字!','提示',0);
        Exit;
      end;
    end;
    if Trim(MJSJKZ.Text)<>'' then
    begin
      if TryStrToFloat(MJSJKZ.Text,FFreal)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('克重非法数字!','提示',0);
        Exit;
      end;
    end;
    if Trim(MJQty2.Text)<>'' then
    begin
      if TryStrToFloat(MJQty2.Text,FFreal)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('赠送数量非法数字!','提示',0);
        Exit;
      end;
    end;
    if Trim(MJQty4.Text)<>'' then
    begin
      if TryStrToFloat(MJQty4.Text,FFreal)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('净重非法数字!','提示',0);
        Exit;
      end;
    end;
    if Trim(MJQty5.Text)<>'' then
    begin
      if TryStrToFloat(MJQty5.Text,FFreal)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('片数非法数字!','提示',0);
        Exit;
      end;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate WFB_MJJY Set MJTypeOther='''+Trim(M.Caption)+'''');
      sql.Add(',MJStr1='''+Trim(Ma.Caption)+'''');
      sql.Add(',MJStr4='''+Trim(MJStr4.Text)+'''');
      sql.Add(',MJXH='''+Trim(MJXH.Text)+'''');
      if Trim(MJLen.Text)<>'' then
      begin
        sql.Add(',MJLen='+Trim(MJLen.Text));
      end else
      begin
        sql.Add(',MJLen=0');
      end;
      if Trim(MJQty1.Text)<>'' then
      begin
        sql.Add(',MJQty1='+Trim(MJQty1.Text));
      end else
      begin
        sql.Add(',MJQty1=0');
      end;
      if Trim(MJQty2.Text)<>'' then
      begin
        sql.Add(',MJQty2='+Trim(MJQty2.Text));
      end else
      begin
        sql.Add(',MJQty2=0');
      end;
      if Trim(MJFK.Text)<>'' then
      begin
        sql.Add(',MJFK='+Trim(MJFK.Text));
      end else
      begin
        sql.Add(',MJFK=0');
      end;
      if Trim(MJMaoZ.Text)<>'' then
      begin
        sql.Add(',MJMaoZ='+Trim(MJMaoZ.Text));
      end else
      begin
        sql.Add(',MJMaoZ=0');
      end;
      if Trim(MJSJKZ.Text)<>'' then
      begin
        sql.Add(',MJSJKZ='+Trim(MJSJKZ.Text));
      end else
      begin
        sql.Add(',MJSJKZ=0');
      end;
      if Trim(MJQty4.Text)<>'' then
      begin
        sql.Add(',MJQty4='+Trim(MJQty4.Text));
      end else
      begin
        sql.Add(',MJQty4=0');
      end;
      if Trim(MJQty5.Text)<>'' then
      begin
        sql.Add(',MJQty5='+Trim(MJQty5.Text));
      end else
      begin
        sql.Add(',MJQty5=0');
      end;
      if RadioGroup1.ItemIndex=0 then
      begin
        sql.Add(',MJType=''正品'' ');
      end else
      if RadioGroup1.ItemIndex=1 then
      begin
        sql.Add(',MJType=''次品'' ');
      end else
      if RadioGroup1.ItemIndex=2 then
      begin
        sql.Add(',MJType=''多拼'' ');
      end;
      sql.Add(' where MJID='''+Trim(Label2.Caption)+'''');

      sql.Add('UPdate CK_BanCP_KC Set KCQtyUnit='''+Trim(M.Caption)+'''');
      if Trim(MJLen.Text)<>'' then
      begin
        sql.Add(',KCQty='+Trim(MJLen.Text));
      end else
      begin
        sql.Add(',KCQty=0');
      end;
      if Trim(MJMaoZ.Text)<>'' then
      begin
        sql.Add(',KCKGQty='+Trim(MJMaoZ.Text));
      end else
      begin
        sql.Add(',KCKGQty=0');
      end;
      sql.Add(' where MJID='''+Trim(Label2.Caption)+''' and KCQty>0');

      sql.Add('UPdate CK_BanCP_CR Set QtyUnit='''+Trim(M.Caption)+'''');
      if Trim(MJLen.Text)<>'' then
      begin
        sql.Add(',Qty='+Trim(MJLen.Text));
      end else
      begin
        sql.Add(',Qty=0');
      end;
      if Trim(MJMaoZ.Text)<>'' then
      begin
        sql.Add(',KGQty='+Trim(MJMaoZ.Text));
      end else
      begin
        sql.Add(',KGQty=0');
      end;
      if RadioGroup1.ItemIndex=0 then
      begin
        sql.Add(',CPType=''正品'' ');
      end else
      if RadioGroup1.ItemIndex=1 then
      begin
        sql.Add(',CPType=''次品'' ');
      end else
      if RadioGroup1.ItemIndex=2 then
      begin
        sql.Add(',CPType=''多拼'' ');
      end;
      sql.Add(' where 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;

procedure TfrmMJEdit.ToolButton4Click(Sender: TObject);
begin
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_BanCP_CR where MJID='''+Trim(Label2.Caption)+'''');
    Open;
  end;
  if ADOTmp.IsEmpty=False then
  begin
    Application.MessageBox('已产生入库数据不能删除!','提示',0);
    Exit;
  end else
  begin
    if Application.MessageBox('确定要作废数据吗？作废后数据不能恢复。','提示',32+4)=IDYES then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete WFB_MJJY where MJID='''+Trim(Label2.Caption)+'''');
        sql.Add('delete WFB_MJJY_CD where MJID='''+Trim(Label2.Caption)+'''');
        ExecSQL;
      end;
      Label2.Caption:='';
    end;
  end;
end;

procedure TfrmMJEdit.MDblClick(Sender: TObject);
begin
  IF trim(M.Caption)='M' then M.Caption:='Y'
  ELSE
  M.Caption:='M';
end;

end.
