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, cxCheckBox;

type
  TfrmMJEditPB = 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;
    MJMaoZ: TEdit;
    Label4: TLabel;
    RadioGroup1: TRadioGroup;
    Label10: TLabel;
    ToolButton4: TToolButton;
    RadioGroup2: TRadioGroup;
    RadioGroup3: TRadioGroup;
    Label14: TLabel;
    AddMoney: TEdit;
    Label15: TLabel;
    DelMoney: TEdit;
    Label5: TLabel;
    JXPerson: TComboBox;
    Label8: TLabel;
    SCPerson: TComboBox;
    Label3: TLabel;
    JBStr: TEdit;
    Label9: TLabel;
    JBSCPerson: TComboBox;
    Label11: TLabel;
    JBZhuanQty: TEdit;
    Label13: TLabel;
    LBDate: TDateTimePicker;
    Label16: TLabel;
    Label17: TLabel;
    MJJingZ: TEdit;
    Label18: TLabel;
    Label19: TLabel;
    MJPiZ: TEdit;
    Label20: TLabel;
    ClothType: TComboBox;
    v2Column3: TcxGridDBColumn;
    Label7: TLabel;
    Label12: TLabel;
    JBSCPerson2: TComboBox;
    JBZhuanQty2: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DSMoney: TDataSource;
    CDS_Money: TClientDataSet;
    Label6: TLabel;
    KuWei: TEdit;
    v2Column4: TcxGridDBColumn;
    Label21: TLabel;
    v2Column5: TcxGridDBColumn;
    Label22: TLabel;
    APBatchNo: 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 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);
    procedure JBStrClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxGridDBColumn2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveData(): Boolean;
    procedure InitSCPerson();
  public
    Fint: Integer;
    FFFLage: string;
    { Public declarations }
  end;

var
  frmMJEditPB: TfrmMJEditPB;

implementation

uses
  U_Fun, U_ZDYHelp, U_DataLink, U_SCPerson;

{$R *.dfm}

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

procedure TfrmMJEditPB.FormDestroy(Sender: TObject);
begin
  frmMJEditPB := nil;
end;

procedure TfrmMJEditPB.MJIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin

    with ADOTmp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select A.*  ');
      sql.Add(' from WFB_MJJY_PB 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;

    if Trim(ADOTmp.fieldbyname('LBDate').AsString) <> '' then
    begin
      LBDate.Checked := True;
      LBDate.Date := ADOTmp.fieldbyname('LBDate').Value;
    end
    else
    begin
      LBDate.Date := Now;
      LBDate.Checked := False;
    end;
    APBatchNo.Text := Trim(ADOTmp.fieldbyname('APBatchNo').AsString);
    MJMaoZ.Text := Trim(ADOTmp.fieldbyname('MJMaoZ').AsString);
    //MJLen.Text:=Trim(ADOTmp.fieldbyname('MJLen').AsString);
    MJJingZ.Text := Trim(ADOTmp.fieldbyname('MJJingZ').AsString);
    KuWei.Text := Trim(ADOTmp.fieldbyname('KuWei').AsString);
    MJPiZ.Text := Trim(ADOTmp.fieldbyname('MJPiZ').AsString);
    SCPerson.ItemIndex := SCPerson.Items.IndexOf(Trim(ADOTmp.fieldbyname('SCPerson').AsString));
    JXPerson.ItemIndex := JXPerson.Items.IndexOf(Trim(ADOTmp.fieldbyname('JXPerson').AsString));
    ClothType.ItemIndex := ClothType.Items.IndexOf(Trim(ADOTmp.fieldbyname('ClothType').AsString));
    AddMoney.Text := Trim(ADOTmp.fieldbyname('AddMoney').AsString);
    DelMoney.Text := Trim(ADOTmp.fieldbyname('DelMoney').AsString);
    JBStr.Text := Trim(ADOTmp.fieldbyname('JBStr').AsString);
    JBZhuanQty.Text := Trim(ADOTmp.fieldbyname('JBZhuanQty').AsString);
    JBSCPerson.ItemIndex := JBSCPerson.Items.IndexOf(Trim(ADOTmp.fieldbyname('JBSCPerson').AsString));
    JBZhuanQty2.Text := Trim(ADOTmp.fieldbyname('JBZhuanQty2').AsString);
    JBSCPerson2.ItemIndex := JBSCPerson2.Items.IndexOf(Trim(ADOTmp.fieldbyname('JBSCPerson2').AsString));
    DelMoney.Text := Trim(ADOTmp.fieldbyname('DelMoney').AsString);
   // M.Caption:=Trim(Trim(ADOTmp.fieldbyname('MJTypeOther').AsString));
    {if Trim(m.Caption)='' then
    begin
      m.Caption:='M';
    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;
    if Trim(Trim(ADOTmp.fieldbyname('MJstr1').AsString)) = '半匹' then
    begin
      RadioGroup2.ItemIndex := 0;
    end;
    if Trim(Trim(ADOTmp.fieldbyname('MJstr1').AsString)) = '整匹' then
    begin
      RadioGroup2.ItemIndex := 1;
    end;
    if Trim(Trim(ADOTmp.fieldbyname('MJstr3').AsString)) = '浅色' then
    begin
      RadioGroup3.ItemIndex := 2;
    end;
    if Trim(Trim(ADOTmp.fieldbyname('MJstr3').AsString)) = '深色' then
    begin
      RadioGroup3.ItemIndex := 0;
    end
    else if Trim(Trim(ADOTmp.fieldbyname('MJstr3').AsString)) = '漂白' then
    begin
      RadioGroup3.ItemIndex := 1;
    end
    else
    begin
      RadioGroup3.ItemIndex := 3;
    end;
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from WFB_MJJY_JiangFa A   where A.MJID=''' + Trim(MJID.Text) + '''');
      Open;
    end;
    SCreateCDS20(ADOTmp, CDS_Money);
    SInitCDSData20(ADOTmp, CDS_Money);
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from WFB_MJJY_CD_PB 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 TfrmMJEditPB.tv2CDTypePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'WFBCPBZZ';
      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 TfrmMJEditPB.ToolButton1Click(Sender: TObject);
begin
  if Trim(Label2.Caption) = '' then
    Exit;
  if Fint = 1 then
  begin
    Order_MJ.Append;
    Order_MJ.Post;
  end
  else
  begin
    CDS_Money.Append;
    CDS_Money.Post;
  end;

end;

procedure TfrmMJEditPB.ToolButton3Click(Sender: TObject);
begin
  if Trim(Label2.Caption) = '' then
    Exit;
  if Fint = 1 then
  begin
    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_PB where MCID=''' + Trim(Order_MJ.fieldbyname('MCID').AsString) + '''');
        ExecSQL;
      end;
    end;
    Order_MJ.Delete;
  end
  else if Fint = 2 then
  begin
    if CDS_Money.IsEmpty then
      Exit;
    if Trim(CDS_Money.fieldbyname('MFID').AsString) <> '' then
    begin
      if Application.MessageBox('确定要删除奖罚数据吗？', '提示', 32 + 4) <> IDYES then
        Exit;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into WFB_MJJY_JiangFa_DelLog ');
        sql.Add('select * from  WFB_MJJY_JiangFa where MFID=''' + Trim(CDS_Money.fieldbyname('MFID').AsString) + '''');
        sql.Add('delete WFB_MJJY_JiangFa where MFID=''' + Trim(CDS_Money.fieldbyname('MFID').AsString) + '''');
        sql.Add(' update WFB_MJJY_JiangFa_DelLog Set Editer=''' + Trim(DName) + ''',EditTime=getdate() ');
        sql.Add(' where MFID=''' + Trim(CDS_Money.fieldbyname('MFID').AsString) + '''');
        ExecSQL;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate WFB_MJJY_PB Set AddMoney=(select Sum(AddMoney) from WFB_MJJY_JiangFa A where A.MJID=WFB_MJJY_PB.MJID)');
        sql.Add(',DelMoney=(select Sum(DelMoney) from WFB_MJJY_JiangFa A where A.MJID=WFB_MJJY_PB.MJID)');
        sql.Add(' where MJID=''' + Trim(CDS_Money.fieldbyname('MJID').AsString) + '''');
        ExecSQL;
      end;
    end;
    CDS_Money.Delete;
  end;

end;

function TfrmMJEditPB.SaveData(): Boolean;
var
  maxno, FFOrderNo, FCarNO: string;
  FFreal: Double;
begin

  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY_PB where MJID=''' + Trim(Label2.Caption) + '''');
    open;
  end;
  FFOrderNo := Trim(ADOTmp.fieldbyname('FOrderNo').AsString);
  FCarNO := Trim(ADOTmp.fieldbyname('CarNO').AsString);
  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_PB', 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_PB  ');
          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);
          Post;
        end;

        Order_MJ.Edit;
        Order_MJ.FieldByName('MCID').Value := Trim(maxno);
        Order_MJ.FieldByName('MJID').Value := Trim(Label2.Caption);
        Next;
      end;
    end;
    ///保存奖罚表
    with CDS_Money do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_Money.fieldbyname('MFID').AsString) = '' then
        begin
          if GetLSNo(ADOTmp, maxno, 'MF', 'WFB_MJJY_JiangFa', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(CDS_Money.fieldbyname('MFID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from WFB_MJJY_JiangFa  ');
          sql.Add(' where MFID=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_Money.fieldbyname('MFID').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('MJId').Value := Trim(Label2.Caption);
          FieldByName('MFID').Value := Trim(maxno);
          SSetSaveDataCDSNew(ADOCmd, Tv1, CDS_Money, 'WFB_MJJY_JiangFa', 0);
          Post;
        end;

        CDS_Money.Edit;
        CDS_Money.FieldByName('MFID').Value := Trim(maxno);
        CDS_Money.FieldByName('MJID').Value := Trim(Label2.Caption);
        Next;
      end;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate WFB_MJJY_PB Set AddMoney=(select Sum(AddMoney) from WFB_MJJY_JiangFa A where A.MJID=WFB_MJJY_PB.MJID)');
      sql.Add(',DelMoney=(select Sum(DelMoney) from WFB_MJJY_JiangFa A where A.MJID=WFB_MJJY_PB.MJID)');
      sql.Add(' where MJID=''' + Trim(Label2.Caption) + '''');
      ExecSQL;
    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(MJJingZ.Text) <> '' then
    begin
      if TryStrToFloat(MJJingZ.Text, FFreal) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('净重非法数字!', '提示', 0);
        Exit;
      end;
    end;
    if Trim(MJPiZ.Text) <> '' then
    begin
      if TryStrToFloat(MJMaoZ.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_PB Set ');
      sql.Add(' SCPerson=''' + Trim(SCPerson.Text) + '''');
      sql.Add(',JXPerson=''' + Trim(JXPerson.Text) + '''');
      sql.Add(',ClothType=''' + Trim(ClothType.Text) + '''');
      sql.Add(',KuWei=''' + Trim(KuWei.Text) + '''');
      sql.Add(',APBatchNo=''' + Trim(APBatchNo.Text) + '''');
      sql.Add(',Editer=''' + Trim(DName) + '''');
      sql.Add(',EditTime=getdate()');
      if Trim(MJJingZ.Text) <> '' then
      begin
        sql.Add(',MJJingZ=' + Trim(MJJingZ.Text));
        sql.Add(',MJMaoZ=' + Trim(MJJingZ.Text));
      end
      else
      begin
        sql.Add(',MJJingZ=0');
      end;
      if Trim(MJPiZ.Text) <> '' then
      begin
        sql.Add(',MJPiZ=' + Trim(MJPiZ.Text));
      end
      else
      begin
        sql.Add(',MJPiZ=0');
      end;

      {if Trim(MJMaoZ.Text)<>'' then
      begin
        sql.Add(',MJMaoZ='+Trim(MJMaoZ.Text));
      end else
      begin
        sql.Add(',MJMaoZ=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;
      if RadioGroup2.ItemIndex = 0 then
      begin
        sql.Add(',mjstr1=''半匹'' ');
      end;
      if RadioGroup2.ItemIndex = 1 then
      begin
        sql.Add(',mjstr1=''整匹'' ');
      end;
      if RadioGroup2.ItemIndex = 2 then
      begin
        sql.Add(',mjstr1='''' ');
      end;

      if RadioGroup3.ItemIndex = 0 then
      begin
        sql.Add(',mjstr3=''深色'' ');
      end;
      if RadioGroup3.ItemIndex = 1 then
      begin
        sql.Add(',mjstr3=''漂白'' ');
      end;
      if RadioGroup3.ItemIndex = 2 then
      begin
        sql.Add(',mjstr3=''浅色'' ');
      end;
      if RadioGroup3.ItemIndex = 3 then
      begin
        sql.Add(',mjstr3='''' ');
      end;

      if Trim(JBStr.Text) <> '' then
      begin
        sql.Add(',JBStr=''√'' ');
        sql.Add(',JBSCPerson=''' + Trim(JBSCPerson.Text) + '''');
        sql.Add(',JBZhuanQty=' + Trim(JBZhuanQty.Text));
        sql.Add(',JBSCPerson2=''' + Trim(JBSCPerson2.Text) + '''');
        if Trim(JBZhuanQty2.Text) <> '' then
          sql.Add(',JBZhuanQty2=' + Trim(JBZhuanQty2.Text))
        else
          sql.Add(',JBZhuanQty2=Null ');
      end
      else
      begin
        sql.Add(',JBStr=Null ');
        sql.Add(',JBSCPerson=Null ');
        sql.Add(',JBZhuanQty=Null ');
        sql.Add(',JBSCPerson2=Null ');
        sql.Add(',JBZhuanQty2=Null ');
        //sql.Add(',MJQty4=Null ');
      end;
      sql.Add(' where MJID=''' + Trim(Label2.Caption) + '''');
      if Trim(FFFLage) <> '已出库' then
      begin
        sql.Add('UPdate CK_PBCP_KC Set KCQtyUnit='''' ');
        if Trim(MJJingZ.Text) <> '' then
        begin
          sql.Add(',KCKGQty=' + Trim(MJJingZ.Text));
        end
        else
        begin
          sql.Add(',KCKGQty=0');
        end;
        sql.Add(' where MJID=''' + Trim(Label2.Caption) + ''' and KCKGQty>0');

        sql.Add('UPdate CK_PBCP_CR Set QtyUnit='''' ');
        if Trim(MJJingZ.Text) <> '' then
        begin
          sql.Add(',KGQty=' + Trim(MJJingZ.Text));
        end
        else
        begin
          sql.Add(',KGQty=0');
        end;
        sql.Add(',CPType=''' + Trim(ClothType.Text) + '''');
        sql.Add(' where MJID=''' + Trim(Label2.Caption) + '''');
      end;
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('UPdate WFB_MJJY_PB Set ');
      sql.Add(' CDQK=dbo.F_Get_Order_SubStr_PB(WFB_MJJY_PB.MJID,''MJCDHZFM'') ');
      sql.Add(',HZYJZP=(select sum(YJZP) from WFB_MJJY_CD_PB A where A.MJID=WFB_MJJY_PB.MJID)');
      sql.Add(' ,ZZPerson=dbo.F_Get_Order_SubStr_PB(WFB_MJJY_PB.MJID,''ZZPerson'') ');
      sql.Add(',CDGS=(select Count(*) from WFB_MJJY_CD_PB A where A.MJID=WFB_MJJY_PB.MJID)');
      sql.Add(',CDHZQty=(select sum(CDQty) from WFB_MJJY_CD_PB A where A.MJID=WFB_MJJY_PB.MJID)');
      sql.Add(',CDHZKgQty=(select sum(CDKgQty) from WFB_MJJY_CD_PB A where A.MJID=WFB_MJJY_PB.MJID)');
      sql.Add(',APXH=(select XHInt from JYCon_Sub_AnPai A where A.APID=WFB_MJJY_PB.MJID)');
      sql.Add(' where MJID=''' + Trim(Label2.Caption) + '''');
      ExecSQL;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('update WFB_MJJY_PB set ZZPersonCode=X.UserID from SY_User X where X.Udept=''挡车工'' and X.UserName=WFB_MJJY_PB.SCPerson and WFB_MJJY_PB.MJID=''' + Trim(Label2.Caption) + '''');
      SQL.Add('update WFB_MJJY_PB set JBSCPersonCode=X.UserID from SY_User X where X.Udept=''挡车工'' and X.UserName=WFB_MJJY_PB.JBSCPerson and WFB_MJJY_PB.MJID=''' + Trim(Label2.Caption) + '''');
      SQL.Add('update WFB_MJJY_PB set JBSCPerson2Code=X.UserID from SY_User X where X.Udept=''挡车工'' and X.UserName=WFB_MJJY_PB.JBSCPerson2 and WFB_MJJY_PB.MJID=''' + Trim(Label2.Caption) + '''');
      ExecSQL;
    end;

    //保存出库
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Do_PBJYCRK  @MJID=' + quotedstr(Trim(Label2.Caption)));
      ExecSQL;
    end;

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

procedure TfrmMJEditPB.ToolButton2Click(Sender: TObject);
var
  mvalue: Double;
begin
  if Trim(Label2.Caption) = '' then
    Exit;
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_PBCP_CR where CRFlag=''出库'' and MJID=''' + Trim(Label2.Caption) + '''');
    Open;
  end;
  if ADOTmp.IsEmpty = False then
  begin
    Application.MessageBox('已产生出库数据不能修改!', '提示', 0);
    Exit;
  end;
  if Trim(JBStr.Text) <> '' then
  begin
    if (Trim(JBSCPerson.Text) = '') and (Trim(JBSCPerson2.Text) = '') then
    begin
      Application.MessageBox('没有选择交班人!', '提示', 0);
      Exit;
    end;
    if Trim(JBSCPerson.Text) = Trim(SCPerson.Text) then
    begin
      Application.MessageBox('交班人1与挡车工不能相同!', '提示', 0);
      Exit;
    end;
    if Trim(JBSCPerson2.Text) = Trim(SCPerson.Text) then
    begin
      Application.MessageBox('交班人2与挡车工不能相同!', '提示', 0);
      Exit;
    end;
    if (Trim(JBZhuanQty.Text) = '') and (Trim(JBZhuanQty2.Text) = '') then
    begin
      Application.MessageBox('没有输入交班转数!', '提示', 0);
      Exit;
    end;
    if Trim(JBSCPerson2.Text) <> '' then
    begin
      if Trim(JBZhuanQty2.Text) = '' then
      begin
        Application.MessageBox('交班转数2不能为空!', '提示', 0);
        Exit;
      end;
    end;
    if Trim(JBSCPerson.Text) <> '' then
    begin
      if Trim(JBZhuanQty.Text) = '' then
      begin
        Application.MessageBox('交班转数1不能为空!', '提示', 0);
        Exit;
      end;
    end;
    if Trim(JBZhuanQty2.Text) <> '' then
    begin
      if TryStrToFloat(JBZhuanQty2.Text, mvalue) = False then
      begin
        Application.MessageBox('交班转数2非法数字!', '提示', 0);
        Exit;
      end;
      if Trim(JBSCPerson2.Text) = '' then
      begin
        Application.MessageBox('交班人2能为空!', '提示', 0);
        Exit;
      end;
      //FJBZhuanQty2:=Trim(JBZhuanQty2.Text);
    end
    else
    begin
      //FJBZhuanQty2:='0';
    end;
    if Trim(JBZhuanQty.Text) <> '' then
    begin
      if TryStrToFloat(JBZhuanQty.Text, mvalue) = False then
      begin
        Application.MessageBox('交班转数1非法数字!', '提示', 0);
        Exit;
      end;
      if Trim(JBSCPerson.Text) = '' then
      begin
        Application.MessageBox('交班人1能为空!', '提示', 0);
        Exit;
      end;
      //FJBZhuanQty:=Trim(JBZhuanQty.Text);
    end
    else
    begin
      //FJBZhuanQty:='0';
    end;
   { with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select ZhuanQty,PiKgQty from CLoth_Sub where Mainid='''+Trim(FMainId)+'''');
      Open;
    end;

    if (StrToInt(FJBZhuanQty)+StrToInt(FJBZhuanQty2))>(StrToFloat(MJJingZ.Text)*1.00/ADOTmp.FieldByName('PiKgQty').AsFloat)*ADOTmp.FieldByName('ZhuanQty').AsInteger then
    begin
      Application.MessageBox('交班转数填写错误!','提示',0);
      Exit;
    end;  }
  end;
  MJID.SetFocus;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    Exit;
  end;
end;

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

procedure TfrmMJEditPB.FormShow(Sender: TObject);
var
  key: char;
begin
  ReadCxGrid('布匹管理', Tv2, '染厂检验管理');
  FFFLage := '';
  if Trim(Label2.Caption) <> '' then
  begin
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_PBCP_CR where MJID=''' + Trim(Label2.Caption) + '''');
      Open;
    end;
    if ADOTmp.RecordCount > 1 then
    begin
      MJPiZ.ReadOnly := True;
      MJMaoZ.ReadOnly := True;
      MJJingZ.ReadOnly := True;
      FFFLage := '已出库';
    end
    else
    begin
      MJPiZ.ReadOnly := False;
      MJMaoZ.ReadOnly := False;
      MJJingZ.ReadOnly := False;
    end;
  end;
  key := #13;
  MJIDKeyPress(MJID, key);
end;

procedure TfrmMJEditPB.ToolButton4Click(Sender: TObject);
begin
  if CDS_Money.IsEmpty = False then
  begin
    Application.MessageBox('已有奖罚，不能作废!', '提示', 0);
    Exit;
  end;
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_PBCP_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('insert into WFB_MJJY_PB_DelLog ');
        sql.Add('select * from WFB_MJJY_PB where MJID=''' + Trim(Label2.Caption) + '''');
        ExecSQL;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' Update WFB_MJJY_PB_DelLog Set Note=Isnull(Note,'''')+''→删除人:''+''' + Trim(DName) + '''');
        sql.Add('+'' ''+Convert(Varchar(20),getdate(),120)');
        sql.Add(' where MJID=''' + Trim(Label2.Caption) + '''');
        ExecSQL;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete WFB_MJJY_PB where MJID=''' + Trim(Label2.Caption) + '''');
        sql.Add('delete WFB_MJJY_CD_PB where MJID=''' + Trim(Label2.Caption) + '''');
        ExecSQL;
      end;
      Label2.Caption := '';
    end;
  end;
end;

procedure TfrmMJEditPB.MDblClick(Sender: TObject);
begin
  {if Trim(M.Caption)='M' then
  begin
    M.Caption:='Y';
  end else
  begin
    M.Caption:='M';
  end; }
end;

procedure TfrmMJEditPB.JBStrClick(Sender: TObject);
begin
  if Trim(JBStr.Text) = '' then
  begin
    JBStr.Text := '√';
  end
  else
  begin
    JBStr.Text := '';
    JBSCPerson.Text := '';
    JBZhuanQty.Text := '';
  end;
end;

procedure TfrmMJEditPB.FormCreate(Sender: TObject);
begin
  InitSCPerson();
end;

procedure TfrmMJEditPB.InitSCPerson();
var
  fsj: string;
begin
  //fsj := 'select Name=ZdyName from KH_Zdy where Type=''SCPerson'' ';
  fsj := 'select Name =UserName from SY_User where Udept=''挡车工'' ';
  SInitComBoxBySql(ADOTmp, JBSCPerson, True, fsj);
  JBSCPerson.ItemIndex := -1;
  SInitComBoxBySql(ADOTmp, JBSCPerson2, True, fsj);
  JBSCPerson2.ItemIndex := -1;
  fsj := 'select Name =UserName from SY_User where Udept=''挡车工'' ';
  SInitComBoxBySql(ADOTmp, SCPerson, True, fsj);
  SCPerson.ItemIndex := -1;
  fsj := 'select Name=ZdyName from KH_Zdy where Type=''JXPerson'' ';
  SInitComBoxBySql(ADOTmp, JXPerson, True, fsj);
  JXPerson.ItemIndex := -1;
  fsj := 'select Name=ZdyName from KH_Zdy where Type=''ClothType'' ';
  SInitComBoxBySql(ADOTmp, ClothType, True, fsj);
  ClothType.ItemIndex := -1;
end;

procedure TfrmMJEditPB.Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  Fint := 1;
end;

procedure TfrmMJEditPB.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  Fint := 2;
end;

procedure TfrmMJEditPB.v2Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  try
//    frmZDYHelp := TfrmZDYHelp.Create(Application);
//    with frmZDYHelp do
//    begin
//      flag := 'SCPerson';
//      flagname := '责任人';
//      if ShowModal = 1 then
//      begin
//        with Self.Order_MJ do
//        begin
//          Edit;
//          FieldByName('ZZPerson').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
//        end;
//      end;
//    end;
//  finally
//    frmZDYHelp.Free;
//  end;
  try
    frmSCPerson := TfrmSCPerson.Create(self);
    with frmSCPerson do
    begin
      FlagStr := 'SCPerson';
      if ShowModal = 1 then
      begin
        with Self.Order_MJ do
        begin
          Edit;
          FieldByName('ZZPerson').Value := Trim(frmSCPerson.FSDPerson);
        end;
      end;
    end;
  finally
    frmSCPerson.Free;
  end;
end;

procedure TfrmMJEditPB.cxGridDBColumn2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmSCPerson := TfrmSCPerson.Create(self);
    with frmSCPerson do
    begin
      FlagStr := 'SCPerson';
      if ShowModal = 1 then
      begin
        with Self.CDS_Money do
        begin
          Edit;
          FieldByName('ZZPerson').Value := Trim(frmSCPerson.FSDPerson);
        end;
      end;
    end;
  finally
    frmSCPerson.Free;
  end;
//  try
//    frmZDYHelp := TfrmZDYHelp.Create(Application);
//    with frmZDYHelp do
//    begin
//      flag := 'SCPerson';
//      flagname := '责任人';
//      if ShowModal = 1 then
//      begin
//        with Self.CDS_Money do
//        begin
//          Edit;
//          FieldByName('ZZPerson').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
//        end;
//      end;
//    end;
//  finally
//    frmZDYHelp.Free;
//  end;
end;

end.

