unit U_WJGCKInPut;

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

type
  TfrmWJGCKInPut = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column9: 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;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    btn1: TToolButton;
    v1Column19: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    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 v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure btn1Click(Sender: TObject);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column4PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId: string;
  end;

var
  frmWJGCKInPut: TfrmWJGCKInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ClothContractListHZCX, U_ZDYHelp, U_ZdyAttachGYS,
  U_ProductOrderListSel;

{$R *.dfm}

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

procedure TfrmWJGCKInPut.TBAddClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty = False then
  begin
    CopyAddRowCDS(CDS_Sub);
    with CDS_Sub do
    begin
      Edit;
      FieldByName('SPID').Value := null;
      post;
    end;
  end
  else
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
      Post;
    end;
  end;
end;

function TfrmWJGCKInPut.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, 'WR', '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('FactoryNo').Value := Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('FromFactoryNo').Value := Trim(CDS_Sub.fieldbyname('FromFactoryNo').AsString);
          FieldByName('ToFactoryNo').Value := Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
          FieldByName('QCOrderNo').Value := Trim(CDS_Sub.fieldbyname('OrderNo').AsString);
          FieldByName('ORDMainIdRK').Value := Trim(CDS_Sub.fieldbyname('ORDMainIdRK').AsString);
          FieldByName('ORDSubIdRK').Value := Trim(CDS_Sub.fieldbyname('ORDSubIdRK').AsString);
          FieldByName('CRFlag').Value := '入库';
          FieldByName('CRQtyFlag').Value := 1;
          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;
//          with ADOQueryTemp do
//          begin
//            Close;
//            sql.Clear;
//            sql.Add('SELECT * FROM JYORDER_MAIN A WHERE A.MAINID=' + QuotedStr(Trim(CDS_Sub.fieldbyname('ORDMainIdRK').AsString)));
//            sql.Add('and (JGFactoryName=' + QuotedStr(Trim(CDS_Sub.fieldbyname('FactoryName').AsString)));
//            SQL.Add('OR JGFactoryName2= ' + QuotedStr(Trim(CDS_Sub.fieldbyname('FactoryName').AsString)));
//            SQL.Add('OR JGFactoryName3= ' + QuotedStr(Trim(CDS_Sub.fieldbyname('FactoryName').AsString)));
//            SQL.Add(')');
//            Open;
//          end;
//          if ADOQueryTemp.IsEmpty = False then
//          begin
//            if ADOQueryTemp.fieldbyname('JGFactoryName').AsString = Trim(CDS_Sub.fieldbyname('FactoryName').AsString) then
//            begin
//              if ADOQueryTemp.fieldbyname('JGPRICE1').AsString <> '' then
//              begin
//                FieldByName('PRICE').Value := ADOQueryTemp.fieldbyname('JGPRICE1').AsString;
//              end;
//
//            end
//            else if ADOQueryTemp.fieldbyname('JGFactoryName2').AsString = Trim(CDS_Sub.fieldbyname('FactoryName').AsString) then
//            begin
//              if ADOQueryTemp.fieldbyname('JGPRICE2').AsString <> '' then
//              begin
//                FieldByName('PRICE').Value := ADOQueryTemp.fieldbyname('JGPRICE2').AsString;
//              end;
//
//            end
//            else
//            begin
//              if ADOQueryTemp.fieldbyname('JGPRICE3').AsString <> '' then
//              begin
//                FieldByName('PRICE').Value := ADOQueryTemp.fieldbyname('JGPRICE3').AsString;
//              end;
//            end;
//          end;
          Post;
        end;
//        with ADOQueryCmd do
//        begin
//          close;
//          sql.Clear;
//          sql.Add('update  CK_SXPB_CR SET ChkStatus=''1'',chker=''' + trim(DName) + ''',Chktime=getdate() ');
//          sql.Add('where SPID=' + quotedstr(trim(Maxno)));
//          execsql;
//        end;
        if CDS_Sub.FieldByName('ISJs').AsString = '是' then
        begin
//          with ADOQueryTemp do
//          begin
//            Close;
//            sql.Clear;
//            sql.Add('select  A.YFID from YF_Money_CR_Sub A INNER  JOIN YF_Money_CR B ON A.YFID=B.YFID where yfpzno=' + QuotedStr(Trim(Maxno)));
//            sql.Add('and B.status=1');
//            Open;
//          end;
//          if ADOQueryTemp.IsEmpty = FALSE then
//          begin
//            Result := False;
//            Application.MessageBox('财务数据已审核，请先撤销!', '提示', 0);
//            ADOQueryCmd.Connection.RollbackTrans;
//            Exit;
//          end;
//          with ADOQueryTemp do
//          begin
//            Close;
//            sql.Clear;
//            sql.Add('EXEC P_Do_CWSC ''应付'',' + quotedstr(trim(Maxno)));
//
//            ExecSQL;
//
//          end;
        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)');
          sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
          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);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;

end;

procedure TfrmWJGCKInPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('外加工入库登记', Tv1, '外加工仓库0326');
  Close;
end;

procedure TfrmWJGCKInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid('外加工入库登记', Tv1, '外加工仓库0326');
  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;
   // v1Column15.Options.Focusing:=False;
    v1Column13.Options.Focusing := False;
    v1Column14.Options.Focusing := False;
    v2Column1.Options.Focusing := False;
    v1Column8.Options.Focusing := False;
    v1Column7.Options.Focusing := False;
    v1Column1.Options.Focusing := False;
    v1Column10.Options.Focusing := False;
    v1Column11.Options.Focusing := False;
    Exit;
  end
  else
  begin
    v1Column4.Options.Focusing := True;
    //v1Column15.Options.Focusing:=True;
    v1Column13.Options.Focusing := True;
    v1Column14.Options.Focusing := True;
    v2Column1.Options.Focusing := True;
    v1Column8.Options.Focusing := True;
    v1Column7.Options.Focusing := True;
    v1Column1.Options.Focusing := True;
    v1Column10.Options.Focusing := True;
    v1Column11.Options.Focusing := True;
  end;
end;

procedure TfrmWJGCKInPut.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 TfrmWJGCKInPut.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('SPName', null, []) = True then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;
//  if CDS_Sub.Locate('OrderNo',null,[])=True then
//  begin
//    Application.MessageBox('订单号不能为空!','提示',0);
//    Exit;
//  end;
  if CDS_Sub.Locate('ISCP', null, []) = True then
  begin
    Application.MessageBox('是否成品不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('ToFactoryName', null, []) = 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('PiQty', null, []) = 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('CRType;FromFactoryName', VarArrayOf(['生产退回', Null]), [loPartialKey]) = True then
  begin
    Application.MessageBox('生产退回的数据，来自工厂不能为空!', '提示', 0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmWJGCKInPut.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXKuWei';
      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 TfrmWJGCKInPut.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(frmZdyAttachGYS.CDS_HZ.fieldbyname('CoCode').AsString);
          FieldByName('FactoryName').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('CoName').AsString);
          FieldByName('FromFactoryNo').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('CoCode').AsString);
          FieldByName('FromFactoryName').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('CoName').AsString);
          Post;
        end;

      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

procedure TfrmWJGCKInPut.v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PBName';
      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;
//  try
//    frmZDYHelp:=TfrmZDYHelp.Create(Application);
//    with frmZDYHelp do
//    begin
//      flag:='WJGName';
//      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 TfrmWJGCKInPut.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JGGONGYI';
      flagname := '加工工艺';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('JGGONGYI').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWJGCKInPut.v1Column11PropertiesButtonClick(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('ToFactoryNo').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('coCode').AsString);
          FieldByName('ToFactoryName').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('coName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmWJGCKInPut.v1Column16PropertiesButtonClick(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('FromFactoryNo').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyCode').AsString);
          FieldByName('FromFactoryName').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmWJGCKInPut.v1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ORDMainIdRK').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('ORDSubIdRK').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('QCOrderNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
          FieldByName('CustomerNoName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('CustomerNoName').AsString);
          FieldByName('spcode').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCode').AsString);
//          FieldByName('SPName').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
//          FieldByName('SOrddefstr1').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('SOrddefstr1').AsString);
          FieldByName('SPColor').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
          FieldByName('SPHX').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTHX').AsString);

          FieldByName('SPCode').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCode').AsString);
          FieldByName('SPName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
          FieldByName('SPSpec').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTSpec').AsString);
          FieldByName('SPCF').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCF').AsString);
          FieldByName('SPMF').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTMF').AsString);
          FieldByName('SPKZ').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTKZ').AsString);
        end;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

procedure TfrmWJGCKInPut.btn1Click(Sender: TObject);
var
  MMaxno: string;
begin
  try
    frmProductOrderListSel := TfrmProductOrderListSel.Create(Application);
    with frmProductOrderListSel do
    begin
      if ShowModal = 1 then
      begin
        with frmProductOrderListSel.Order_Main do
        begin
          first;
          DisableControls;
          while not eof do
          begin
            if fieldbyname('ssel').AsBoolean then
            begin
              if GetLSNo(ADOQueryCmd, MMaxno, 'DR', 'CK_SXPB_CR', 3, 1) = False then
              begin
                Application.MessageBox('取待检布入库编号失败!', '提示', 0);
                Exit;
              end;
              with Self.CDS_Sub do
              begin
                Append;
                FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
                FieldByName('SPID').Value := Trim(MMaxno);
                FieldByName('CustomerNoName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('CustomerNoName').AsString);
                FieldByName('SPCode').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCode').AsString);
                FieldByName('ORDMainIdRK').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MainId').AsString);
                FieldByName('ORDSubIdRK').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubId').AsString);
                FieldByName('qcOrderNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
                FieldByName('OrderNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
                FieldByName('SPColor').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
                FieldByName('SPHX').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTHX').AsString);
                FieldByName('SPName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
                FieldByName('SPSpec').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTSpec').AsString);
                FieldByName('SPCF').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCF').AsString);
                FieldByName('SPMF').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTMF').AsString);
                FieldByName('SPKZ').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTKZ').AsString);
//                FieldByName('Note').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubNote').AsString);
                Post;
              end;
            end;
            next;
          end;
          first;
          EnableControls;
        end;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

procedure TfrmWJGCKInPut.v1Column5PropertiesEditValueChanged(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin

end;

procedure TfrmWJGCKInPut.v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin

  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'WJGTYPE';
      flagname := '外加工入库类型';

      if ShowModal = 1 then
      begin
        with CDS_Sub do
        begin
          Edit;
          CDS_Sub.FieldByName('CRTYPE').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmWJGCKInPut.Tv1Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FXGFEE, FSJFEE, FMONEY: 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;
    FQty := FieldByName('Qty').AsFloat;
    FPrice := FieldByName('Price').AsFloat;
    FXGFEE := FieldByName('XIAOGANGFEI').AsFloat;
    FSJFEE := FieldByName('shangjifei').AsFloat;
    FMONEY := FieldByName('MONEY').AsFloat;
    Post;
  end;
  FMONEY := strtofloat(format('%.2f', [FQty * FPrice + FXGFEE + FSJFEE]));
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    FieldbyName('MONEY').Value := FMONEY;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

end.

