unit U_ShaRKInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, ComCtrls, ToolWin,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxButtonEdit, cxTextEdit, StdCtrls, ExtCtrls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle,
  dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary,
  dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin,
  dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

type
  TfrmShaRKInPut = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    v1Column4: TcxGridDBColumn;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Column12: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Tv1Column16: TcxGridDBColumn;
    ToolButton3: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column6PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1Column7PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column3PropertiesChange(Sender: TObject);
    procedure v1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId, canshu3: string;
  end;

var
  frmShaRKInPut: TfrmShaRKInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ZdyAttachGYS, U_FLOrderListSel,
  U_FLManageSEL;

{$R *.dfm}

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

procedure TfrmShaRKInPut.TBAddClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty = False then
  begin
    CopyAddRowCDS(CDS_Sub);
    with CDS_Sub do
    begin
      Edit;
      FieldByName('SPID').Value := null;
      FieldByName('PIQTY').Value := 0;
      FieldByName('QTY').Value := 0;
      FieldByName('MHJS').Value := 0;
      FieldByName('ZHS').Value := 0;
      post;
    end;
  end
  else
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('SPID').Value := null;
      FieldByName('zhs').Value := 0;
      FieldByName('michang').Value := 0;
      FieldByName('ZHS').Value := 0;
      FieldByName('PIQTY').Value := 0;
      FieldByName('QTY').Value := 0;
      FieldByName('MHJS').Value := 0;
      FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
      FieldByName('QtyUnit').Value := '';
      Post;
    end;
  end;

end;

function TfrmShaRKInPut.SaveCKData(): Boolean;
var
  FCRID, Maxno, FFSPID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
          open;
        end;
        FFSPID := Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
        if Trim(FFSPID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, Maxno, 'JR', 'CK_SXPB_CR', 3, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取机物料入库编号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          Maxno := Trim(FFSPID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FFSPID) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('SPID').Value := Trim(Maxno);
          FieldByName('FZSPID').Value := Trim(Maxno);
          FieldByName('CKName').Value := '机物料';

          FieldByName('ORDMainIdRK').Value := Trim(CDS_Sub.fieldbyname('ORDMainIdRK').AsString);
          FieldByName('ORDSubIdRK').Value := Trim(CDS_Sub.fieldbyname('ORDSubIdRK').AsString);

          FieldByName('FactoryNo').Value := Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('CPID').Value := Trim(CDS_Sub.fieldbyname('CPID').AsString);
          FieldByName('CRFlag').Value := '入库';
          FieldByName('CRQtyFlag').Value := 1;
          //FieldByName('CRID').Value:=FCRID;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_SXPB_CR', 2);
          if Trim(FFSPID) = '' then
            FieldByName('Filler').Value := Trim(DName)
          else
          begin
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID and A.ZFFlag=0)');
          sql.Add(',MXKCPiQty=(select sum(isnull(zhs,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID  and A.ZFFlag=0)');
          sql.Add(' where SPID=''' + Trim(Maxno) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID=''' + Trim(Maxno) + '''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('MXKCQty').Value < 0 then
        begin
          CDS_Sub.DisableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('入库数小于出库数不能保存!', '提示', 0);
          Exit;
        end;
        Edit;
        FieldByName('SPID').Value := Trim(Maxno);
        //FieldByName('CRID').Value:=Trim(FCRID);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;

end;

procedure TfrmShaRKInPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('机物料入库登记', Tv1, '机物料仓库9');
  Close;
end;

procedure TfrmShaRKInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid('机物料入库登记', Tv1, '机物料仓库9');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from CK_SXPB_CR A');
    sql.Add(' where SPID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);
  if CDS_Sub.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_SXPB_CR where FZSPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
    sql.Add(' and CRQtyFlag=-1');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    v1Column4.Options.Focusing := False;
    v1Column14.Options.Focusing := False;
    v2Column1.Options.Focusing := False;
    v1Column8.Options.Focusing := False;
    v1Column15.Options.Focusing := False;
    Exit;
  end
  else
  begin
    v1Column4.Options.Focusing := True;
    v1Column14.Options.Focusing := True;
    v2Column1.Options.Focusing := True;
    v1Column8.Options.Focusing := True;
    v1Column15.Options.Focusing := False;
  end;
end;

procedure TfrmShaRKInPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('SPID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where FZSPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
      sql.Add(' and CRQtyFlag=-1');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已有出库记录，不能删除!', '提示', 0);
      Exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete CK_SXPB_CR where SPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end
  else
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmShaRKInPut.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if CDS_Sub.Locate('CRTime', null, []) = True then
  begin
    Application.MessageBox('入库时间不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType', null, []) = True then
  begin
    Application.MessageBox('入库类型不能为空!', '提示', 0);
    Exit;
  end;
//  if CDS_Sub.Locate('SXPinPai',null,[])=True then
//  begin
//    Application.MessageBox('品牌不能为空!','提示',0);
//    Exit;
//  end;
  if CDS_Sub.Locate('SPName', null, []) = True then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_Sub.Locate('JWLTYPE;QtyUnit', VarArrayOf(['成品', '盒']), [loPartialKey]) = True then
  begin
    Application.MessageBox('成品单位只能选箱!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('JWLTYPE;QtyUnit', VarArrayOf(['成品', '卡']), [loPartialKey]) = True then
  begin
    Application.MessageBox('成品单位只能选箱!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('JWLTYPE;QtyUnit', VarArrayOf(['成品', '件']), [loPartialKey]) = True then
  begin
    Application.MessageBox('成品单位只能选箱!', '提示', 0);
    Exit;
  end;
//  if CDS_Sub.Locate('FactoryName', null, []) = True then
//  begin
//    Application.MessageBox('工厂不能为空!', '提示', 0);
//    Exit;
//  end;
//  if CDS_Sub.Locate('FactoryName', '', []) = True then
//  begin
//    Application.MessageBox('供应商不能为空!', '提示', 0);
//    Exit;
//  end;
  if CDS_Sub.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('QtyUnit', null, []) = True then
  begin
    Application.MessageBox('数量单位不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('JWLTYPE', null, []) = True then
  begin
    Application.MessageBox('类型不能为空!', '提示', 0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmShaRKInPut.v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('CoName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmShaRKInPut.v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmflManageSEL := TfrmflManageSEL.Create(Application);
    with frmflManageSEL do
    begin
      if ShowModal = 1 then
      begin

        with Self.CDS_Sub do
        begin
          edit;
          FieldByName('SPname').Value := CDS_Main.FieldByName('CYNoLX').Value;
          FieldByName('SPCode').Value := CDS_Main.FieldByName('CYNo').Value;
          FieldByName('SPEname').Value := CDS_Main.FieldByName('CYEName').Value;
          FieldByName('SPEColor').Value := Trim(CDS_Main.fieldbyname('CYColor').AsString);

          FieldByName('SPHX').Value := CDS_Main.FieldByName('CYHX').Value;

          FieldByName('MICHANG').Value := CDS_Main.FieldByName('MC').Value;

          FieldByName('MCUNIT').Value := CDS_Main.FieldByName('MCUNIT').Value;
          FieldByName('JSUNIT').Value := CDS_Main.FieldByName('JSUNIT').Value;

          FieldByName('QtyUnit').Value := CDS_Main.FieldByName('CYPUNIT').Value;
          FieldByName('SPCF').Value := CDS_Main.FieldByName('CYCF').Value;

          FieldByName('CYID').Value := CDS_Main.FieldByName('CYID').Value;
          FieldByName('SPCOLOR').Value := CDS_Main.FieldByName('CYColor').Value;


//          Self.PCNote.Text := trim(CDS_Main.FieldByName('MC').AsString);
//          Self.bzyq.Text := trim(CDS_Main.FieldByName('BZCC').AsString);
          Post;
        end;

      end;
    end;
  finally
    frmflManageSEL.Free;
  end;
//  try
//    frmZDYHelp := TfrmZDYHelp.Create(Application);
//    with frmZDYHelp do
//    begin
//      flag := 'JWLYCLName';
//      flagname := '品名';
//      if ShowModal = 1 then
//      begin
//        with Self.CDS_Sub do
//        begin
//          Edit;
//          FieldByName('SPName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
//        end;
//      end;
//    end;
//  finally
//    frmZDYHelp.Free;
//  end;

end;

procedure TfrmShaRKInPut.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JWLKuWei';
      flagname := '库位';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('KuWei').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_SXPB_CR where FZSPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
    sql.Add(' and CRQtyFlag=-1 and ZFFlag=0 ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    v1Column4.Options.Editing := False;
    v1Column14.Options.Editing := False;
    v1Column15.Options.Editing := False;
    v2Column1.Options.Editing := False;
    v1Column8.Options.Editing := False;
    v1Column1.Options.Editing := False;
    v1Column3.Options.Editing := False;

  end
  else
  begin
    v1Column4.Options.Editing := True;
    v1Column14.Options.Editing := True;
    v1Column15.Options.Editing := True;
    v2Column1.Options.Editing := True;
    v1Column8.Options.Editing := True;
    v1Column1.Options.Editing := True;
    v1Column3.Options.Editing := True;

  end;
end;

procedure TfrmShaRKInPut.v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JWLSPSpec';
      flagname := '规格';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SPSpec').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPut.v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JWLPinPai';
      flagname := '机物料品牌';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SXPinPai').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPut.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JWLUnit';
      flagname := '机物料数量单位';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('QtyUnit').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPut.v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SQPerson';
      flagname := '申请人';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SQPerson').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPut.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmFLOrderListSel := TfrmFLOrderListSel.Create(Application);
    with frmFLOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ORDMainIdRK').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('ORDSubIdRK').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('OrderNo').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
          FieldByName('FactoryName').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('jgFactoryName').AsString);

          FieldByName('SPname').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('MPRTCODENAME').AsString);
          FieldByName('SPCOLOR').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
          FieldByName('SPEColor').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('PRTEColor').AsString);
          FieldByName('SPEName').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('MPRTCode').AsString);
          FieldByName('SPHX').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('PRTColorNoKH').AsString);
          FieldByName('SPCode').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('CYNo').AsString);
          FieldByName('SPCF').Value := frmFLOrderListSel.Order_Main.fieldbyname('CYCF').Value;
          FieldByName('piQty').Value := frmFLOrderListSel.Order_Main.fieldbyname('MXHS').Value;
          FieldByName('MHJS').Value := frmFLOrderListSel.Order_Main.fieldbyname('MHJS').Value;
          FieldByName('MICHANG').Value := frmFLOrderListSel.Order_Main.fieldbyname('MC').Value;
          FieldByName('MCUNIT').Value := frmFLOrderListSel.Order_Main.fieldbyname('MCUNIT').Value;
          FieldByName('DDMS').Value := frmFLOrderListSel.Order_Main.fieldbyname('MS').Value;
          FieldByName('cyid').Value := frmFLOrderListSel.Order_Main.fieldbyname('cyid').Value;
          FieldByName('QtyUnit').Value := frmFLOrderListSel.Order_Main.fieldbyname('CYPUNIT').Value;
          FieldByName('JSUNIT').Value := frmFLOrderListSel.Order_Main.fieldbyname('JSUNIT').Value;
        end;
      end;
    end;
  finally
    frmFLOrderListSel.Free;
  end;
end;

procedure TfrmShaRKInPut.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JSUnit';
      flagname := '机物料件数单位';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('jsUnit').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPut.Tv1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MCUnit';
      flagname := '机物料米长数量单位';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('mcUnit').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPut.v2Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FMC, FMHJS, FZHS, FDDMS, FChaZhi, fmxhs: double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with CDS_Sub do
  begin
    Edit;
    FZHS := FieldByName('ZHS').AsFloat;
    FMC := FieldByName('MICHANG').AsFloat;
    FMHJS := FieldByName('MHJS').AsFloat;
    FDDMS := FieldByName('DDMS').ASFLOAT;
    fmxhs := FieldByName('piqty').ASFLOAT;
    Post;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '箱' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * fmxhs * FMC * FMHJS]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '盒' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * FMC * FMHJS]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '件' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * FMC]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmShaRKInPut.ToolButton1Click(Sender: TObject);
begin
  try
    frmFLOrderListSel := TfrmFLOrderListSel.Create(Application);
    with frmFLOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        while Order_Main.Locate('SSel', True, []) do
        begin
          with Self.CDS_Sub do
          begin
            Append;
            FieldByName('SPID').Value := null;
            FieldByName('PIQTY').Value := 0;
//            FieldByName('QTY').Value := frmFLOrderListSel.Order_Main.fieldbyname('PRTOrderQty').Value;

            FieldByName('michang').Value := 0;
            FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
            FieldByName('QtyUnit').Value := '';
            FieldByName('ORDMainIdRK').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('MainId').AsString);
            FieldByName('ORDSubIdRK').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('SubId').AsString);
            FieldByName('OrderNo').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
            FieldByName('FactoryName').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('jgFactoryName').AsString);

            FieldByName('SPname').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('MPRTCODENAME').AsString);
            FieldByName('SPCOLOR').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
            FieldByName('SPEColor').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('PRTEColor').AsString);
            FieldByName('SPEName').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('MPRTCode').AsString);
            FieldByName('SPHX').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('PRTColorNoKH').AsString);
            FieldByName('SPCode').Value := Trim(frmFLOrderListSel.Order_Main.fieldbyname('CYNo').AsString);
            FieldByName('SPCF').Value := frmFLOrderListSel.Order_Main.fieldbyname('CYCF').Value;
            FieldByName('piQty').Value := frmFLOrderListSel.Order_Main.fieldbyname('MXHS').Value;
            FieldByName('MHJS').Value := frmFLOrderListSel.Order_Main.fieldbyname('MHJS').Value;
            FieldByName('MICHANG').Value := frmFLOrderListSel.Order_Main.fieldbyname('MC').Value;
            FieldByName('MCUNIT').Value := frmFLOrderListSel.Order_Main.fieldbyname('MCUNIT').Value;
            FieldByName('DDMS').Value := frmFLOrderListSel.Order_Main.fieldbyname('MS').Value;
            FieldByName('cyid').Value := frmFLOrderListSel.Order_Main.fieldbyname('cyid').Value;
            FieldByName('QtyUnit').Value := frmFLOrderListSel.Order_Main.fieldbyname('CYPUNIT').Value;
            FieldByName('JSUNIT').Value := frmFLOrderListSel.Order_Main.fieldbyname('JSUNIT').Value;
          end;
          Order_Main.Delete;
        end;

      end;
    end;
  finally
    frmFLOrderListSel.Free;
  end;

end;

procedure TfrmShaRKInPut.Tv1Column7PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  Fqty, FChaZhi, FDDMS: Double;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    Fqty := FieldByName('qty').ASFLOAT;
    FDDMS := FieldByName('DDMS').ASFLOAT;

    FChaZhi := FDDMS - Fqty;
    Edit;
    FieldByName('ChaZhi').Value := FChaZhi;
    Post;
  end;
end;

procedure TfrmShaRKInPut.ToolButton2Click(Sender: TObject);
var
  FValue, FFValue, FColumn, FFColumn: string;
  FQty, FMC, FMHJS, FZHS, FDDMS, FChaZhi, fmxhs: double;
begin
  ToolBar1.SetFocus;
  if CDS_Sub.IsEmpty then
    Exit;
  if Application.MessageBox('确定要一键替换吗', '提示', 1) = 2 then
    exit;
  FColumn := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  FFColumn := Tv1.Controller.FocusedColumn.Summary.GroupFormat;
  FValue := Trim(CDS_Sub.fieldbyname(FColumn).AsString);
  if Trim(FFColumn) <> '' then
  begin
    FFValue := Trim(CDS_Sub.fieldbyname(FFColumn).AsString);
  end;
  with CDS_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      Edit;
      if FValue = '' then
      begin
        CDS_Sub.FieldByName(FColumn).Value := null;
      end
      else
      begin
        CDS_Sub.FieldByName(FColumn).Value := FValue;
      end;

      if Trim(FFColumn) <> '' then
      begin
        if FFValue = '' then
        begin
          CDS_Sub.FieldByName(FFColumn).Value := null;
        end
        else
        begin
          CDS_Sub.FieldByName(FFColumn).Value := FFValue;
        end;
      end;
      if (FColumn = 'ZHS') or (FColumn = 'piQty') or (FColumn = 'piQty') or (FColumn = 'MHJS') or (FColumn = 'MICHANG') or (FColumn = 'qtyunit') then
      begin

        if CDS_Sub.FieldByName('qtyunit').AsString = '箱' then
        begin

          FQty := strtofloat(format('%.2f', [FieldByName('ZHS').AsFloat * FieldByName('piqty').ASFLOAT * FieldByName('MICHANG').AsFloat * FieldByName('MHJS').AsFloat]));

          FieldbyName('QTY').Value := FQty;

        end;
        if CDS_Sub.FieldByName('qtyunit').AsString = '盒' then
        begin

          FQty := strtofloat(format('%.2f', [FieldByName('ZHS').AsFloat * FieldByName('MICHANG').AsFloat * FieldByName('MHJS').AsFloat]));

          FieldbyName('QTY').Value := FQty;

        end;
        if (CDS_Sub.FieldByName('qtyunit').AsString = '件') or (CDS_Sub.FieldByName('qtyunit').AsString = '卡') then
        begin

          FQty := strtofloat(format('%.2f', [FieldByName('MICHANG').AsFloat * FieldByName('zhs').AsFloat]));

          FieldbyName('QTY').Value := FQty;

        end;
//        CDS_Main.FieldByName('money').Value := CDS_Main.FieldByName('price').AsFloat * CDS_Main.FieldByName('qty').AsFloat;
      end;
      Post;

      Next;
    end;
    EnableControls;
  end;
end;

procedure TfrmShaRKInPut.v1Column3PropertiesChange(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FMC, FMHJS, FZHS, FDDMS, FChaZhi, fmxhs: double;
begin
  if CDS_Sub.FieldByName('qtyunit').AsString = '箱' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * fmxhs * FMC * FMHJS]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
//      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '盒' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * FMC * FMHJS]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
//      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '件' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * FMC]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
//      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmShaRKInPut.v1Column3PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FMC, FMHJS, FZHS, FDDMS, FChaZhi, fmxhs: double;
begin
  with CDS_Sub do
  begin
    Edit;
    FZHS := FieldByName('ZHS').AsFloat;
    FMC := FieldByName('MICHANG').AsFloat;
    FMHJS := FieldByName('MHJS').AsFloat;
    FDDMS := FieldByName('DDMS').ASFLOAT;
    fmxhs := FieldByName('piqty').ASFLOAT;
    Post;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '箱' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * fmxhs * FMC * FMHJS]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
//      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '盒' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * FMC * FMHJS]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
//      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;
  if CDS_Sub.FieldByName('qtyunit').AsString = '件' then
  begin

    FQty := strtofloat(format('%.2f', [FZHS * FMC]));
    FChaZhi := FDDMS - FQty;
    with CDS_Sub do
    begin
      Edit;
//      FieldByName(FFieldName).Value := Trim(mvalue);
      FieldbyName('QTY').Value := FQty;
      FieldByName('ChaZhi').Value := FChaZhi;
      Post;
    end;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmShaRKInPut.ToolButton3Click(Sender: TObject);
var
  i: Integer;
  FE: Boolean;
begin
  FE := False;
  try
    frmflManageSEL := TfrmflManageSEL.Create(Application);
    with frmflManageSEL do
    begin
      if ShowModal = 1 then
      begin

        while CDS_Main.Locate('SSel', True, []) do
        begin

          with Self.CDS_Sub do
          begin
            Append;
            FieldByName('SPID').Value := null;
            FieldByName('zhs').Value := 0;
            FieldByName('michang').Value := 0;
            FieldByName('ZHS').Value := 0;
            FieldByName('PIQTY').Value := 0;
            FieldByName('QTY').Value := 0;
            FieldByName('MHJS').Value := 0;
            FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
            FieldByName('QtyUnit').Value := '';
            FieldByName('SPname').Value := CDS_Main.FieldByName('CYNoLX').Value;
            FieldByName('SPCode').Value := CDS_Main.FieldByName('CYNo').Value;
            FieldByName('SPEname').Value := CDS_Main.FieldByName('CYEName').Value;
            FieldByName('SPEColor').Value := Trim(CDS_Main.fieldbyname('CYColor').AsString);

            FieldByName('SPHX').Value := CDS_Main.FieldByName('CYHX').Value;

            FieldByName('MICHANG').Value := CDS_Main.FieldByName('MC').Value;

            FieldByName('MCUNIT').Value := CDS_Main.FieldByName('MCUNIT').Value;
            FieldByName('JSUNIT').Value := CDS_Main.FieldByName('JSUNIT').Value;

            FieldByName('QtyUnit').Value := CDS_Main.FieldByName('CYPUNIT').Value;
            FieldByName('SPCF').Value := CDS_Main.FieldByName('CYCF').Value;

            FieldByName('CYID').Value := CDS_Main.FieldByName('CYID').Value;
            FieldByName('SPCOLOR').Value := CDS_Main.FieldByName('CYColor').Value;

            Post;
          end;
          CDS_Main.Delete;
        end;

      end;
    end;
  finally
    frmflManageSEL.Free;
  end;
end;

end.

