unit U_CPBCKInPut;

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;

type
  TfrmCPBCKInPut = 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;
    v1Column19: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    Button1: TButton;
    GroupBox1: TGroupBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    CheckBox7: TCheckBox;
    CheckBox8: TCheckBox;
    CheckBox9: TCheckBox;
    Button2: TButton;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    TvMX: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    CDS_MX: TClientDataSet;
    DS_MX: TDataSource;
    ToolButton5: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: 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 v1Column18PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TvMXCustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure TvMXKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
    procedure InitMXGrid(MCRNO: string);
  public
    { Public declarations }
    FBCId: string;
  end;

var
  frmCPBCKInPut: TfrmCPBCKInPut;

implementation

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

{$R *.dfm}
procedure TfrmCPBCKInPut.InitMXGrid(MCRNO: string);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_SXPB_CRMX where CRNO=' + quotedstr(Trim(MCRNO)));
    sql.Add(' order by MXID');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MX);
  SInitCDSData20(ADOQueryTemp, CDS_MX);
end;

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

procedure TfrmCPBCKInPut.TBAddClick(Sender: TObject);
var
  MMaxno: string;
begin
  if GetLSNo(ADOQueryCmd, MMaxno, 'DR', 'CK_SXPB_CR', 3, 1) = False then
  begin
    Application.MessageBox('取待检布入库编号失败!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.IsEmpty = False then
  begin
    CopyAddRowCDS(CDS_Sub);
    with CDS_Sub do
    begin
      Edit;
      FieldByName('SPID').Value := Trim(MMaxno);
      post;
    end;
  end
  else
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('SPID').Value := Trim(MMaxno);
      FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
      FieldByName('crtype').Value := '加工完成';
      Post;
    end;
  end;

end;

function TfrmCPBCKInPut.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, 'DR', 'CK_SXPB_CR', 3, 1) = False then
//          begin
//            Result := False;
//            ADOQueryCmd.Connection.RollbackTrans;
//            Application.MessageBox('取待检布入库编号失败!', '提示', 0);
//            Exit;
//          end;
          Maxno := Trim(CDS_Sub.fieldbyname('SPID').AsString);
        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('ZYTYPE').Value := Trim(CDS_Sub.fieldbyname('ZYTYPE').AsString);

          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('ORDMainIdRK').Value := Trim(CDS_Sub.fieldbyname('ORDMainIdRK').AsString);
          FieldByName('ORDSubIdRK').Value := Trim(CDS_Sub.fieldbyname('ORDSubIdRK').AsString);

          FieldByName('CustomerNoName').Value := Trim(CDS_Sub.fieldbyname('CustomerNoName').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;
          if Trim(CDS_Sub.fieldbyname('OrderNo').AsString) <> '' then
          begin
            FieldByName('QCOrderNo').Value := Trim(CDS_Sub.fieldbyname('OrderNo').AsString);
          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)');
          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);
        //FieldByName('CRID').Value:=Trim(FCRID);
        Post;
//
//        if Trim(CDS_Sub.fieldbyname('CRType').AsString)='客户退货' then
//        begin
//         if not YSData(CDS_Sub) then
//         begin
//           ADOQueryCmd.Connection.RollbackTrans;
//           Application.MessageBox('保存异常!','提示',0);
//           exit;
//         end;
//        end;

        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;

end;

procedure TfrmCPBCKInPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('待检布入库登记', Tv1, '待检布仓库0327');
  Close;
end;

procedure TfrmCPBCKInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid('待检布入库登记', Tv1, '待检布仓库0327');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(',OrderNO=(select OrderNO from JYOrder_Main JM where JM.MainId=A.ORDMainIdRK)');
    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 TfrmCPBCKInPut.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 TfrmCPBCKInPut.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('ToFactoryName', null, []) = True then
  begin
    Application.MessageBox('存放工厂不能为空!', '提示', 0);
    Exit;
  end;

  if trim(CDS_Sub.fieldbyname('CRType').AsString) <> '客户退货' then
  begin
    if CDS_Sub.Locate('FactoryName', null, []) = True then
    begin
      Application.MessageBox('加工厂不能为空!', '提示', 0);
      Exit;
    end;
  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 TfrmCPBCKInPut.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 TfrmCPBCKInPut.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 TfrmCPBCKInPut.v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  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 TfrmCPBCKInPut.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'KuWei';
      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 TfrmCPBCKInPut.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 TfrmCPBCKInPut.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 TfrmCPBCKInPut.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('OrderNo').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
          FieldByName('CustomerNoName').Value := Trim(frmProductOrderListSel.Order_Main.fieldbyname('CustomerNoName').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 TfrmCPBCKInPut.v1Column18PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'KuWei';
      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 TfrmCPBCKInPut.ToolButton1Click(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('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);
                FieldByName('crtype').Value := '加工完成';
                Post;
              end;
            end;
            next;
          end;
          first;
          EnableControls;
        end;
      end;
    end;
  finally
    frmProductOrderListSel.Free;
  end;
end;

procedure TfrmCPBCKInPut.Button2Click(Sender: TObject);
var
  CRType, FactoryName, RCGangNo, ToFactoryName, KuWei, QtyUnit, Note: string;
  i, PiQty: Integer;
  qty: Double;
begin
  try
    if CDS_Sub.IsEmpty then
      Exit;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      CRType := Trim(CDS_Sub.fieldbyname('CRType').AsString);
      FactoryName := Trim(CDS_Sub.fieldbyname('FactoryName').AsString);
      RCGangNo := Trim(CDS_Sub.fieldbyname('RCGangNo').AsString);
      ToFactoryName := Trim(CDS_Sub.fieldbyname('ToFactoryName').AsString);
      KuWei := Trim(CDS_Sub.fieldbyname('KuWei').AsString);
      PiQty := CDS_Sub.fieldbyname('PiQty').AsInteger;
      qty := CDS_Sub.fieldbyname('Qty').AsFloat;
      QtyUnit := Trim(CDS_Sub.fieldbyname('QtyUnit').AsString);
      Note := Trim(CDS_Sub.fieldbyname('Note').AsString);
    end;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        Edit;
        if CheckBox1.Checked then
          FieldByName('CRType').Value := CRType;
        if CheckBox2.Checked then
          FieldByName('FactoryName').Value := FactoryName;
        if CheckBox3.Checked then
          FieldByName('RCGangNo').Value := RCGangNo;
        if CheckBox4.Checked then
          FieldByName('ToFactoryName').Value := ToFactoryName;
        if CheckBox5.Checked then
          FieldByName('KuWei').Value := KuWei;
        if CheckBox6.Checked then
          FieldByName('PiQty').Value := PiQty;
        if CheckBox7.Checked then
          FieldByName('Qty').Value := qty;
        if CheckBox8.Checked then
          FieldByName('QtyUnit').Value := QtyUnit;
        if CheckBox9.Checked then
          FieldByName('Note').Value := Note;
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
  except
    Application.MessageBox('替换异常!', '提示', 0);
  end;
end;

procedure TfrmCPBCKInPut.ToolButton2Click(Sender: TObject);
begin
  Panel4.Visible := True;
end;

procedure TfrmCPBCKInPut.Image2Click(Sender: TObject);
begin
  Panel4.Visible := False;
end;

procedure TfrmCPBCKInPut.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to GroupBox1.ControlCount - 1 do
  begin
    if GroupBox1.Controls[i] is TCheckBox then
    begin
      TCheckBox(GroupBox1.Controls[i]).Checked := true;
    end;
  end;
end;

procedure TfrmCPBCKInPut.ToolButton3Click(Sender: TObject);
var
  i: Integer;
  MaxNo, MMXID, MCRNO: string;
begin
  MCRNO := Trim(CDS_Sub.fieldbyname('SPID').AsString);
  if GetLSNo(ADOQueryTemp, MaxNo, 'MCM', 'CK_SXPB_CRMX', 3, 1) = False then
  begin
    Application.MessageBox('取最大号失败!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    for i := 1 to 10 do
    begin
      MMXID := Trim(MaxNo) + Trim(inttostr(i));
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('insert into  CK_SXPB_CRMX (MXID,CRNO,MXQty)');
        sql.Add('values(' + quotedstr(MMXID));
        sql.Add(',' + quotedstr(Trim(MCRNO)));
        sql.Add(',0');
        sql.Add(')');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitMXGrid(MCRNO);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('添加失败!', '提示', 0);
  end;

end;

procedure TfrmCPBCKInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

  InitMXGrid(Trim(CDS_Sub.fieldbyname('SPID').AsString));

end;

procedure TfrmCPBCKInPut.cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(TvMX.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_MX do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update CK_SXPB_CRMX set ' + FFieldName + ' = ' + mvalue);
    sql.Add('  where MXID=' + quotedstr(Trim(CDS_MX.FieldByName('MXID').AsString)));
    ExecSQL;
  end;
  TvMX.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPBCKInPut.ToolButton4Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete CK_SXPB_CRMX   where MXID=' + quotedstr(Trim(CDS_MX.FieldByName('MXID').AsString)));
    ExecSQL;
  end;
  CDS_MX.Delete;
end;

procedure TfrmCPBCKInPut.TvMXCustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
  FBounds := AViewInfo.Bounds;
  if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bBottom, bLeft, bRight], 1);
    FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index + 1);
    InflateRect(FBounds, -1, -1);    //Platform   specific.   May   not   work   on   Linux.
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

procedure TfrmCPBCKInPut.TvMXKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  TvMX.OptionsData.Editing := False;
  if Key = 13 then
    Key := 40;
  TvMX.OptionsData.Editing := True;
end;

procedure TfrmCPBCKInPut.ToolButton5Click(Sender: TObject);
var
  MMaxno: string;
begin
  try
    frmWJGCKKCSelList := TfrmWJGCKKCSelList.Create(Application);
    with frmWJGCKKCSelList do
    begin
      cptype := '是';
      ToolButton1.Visible := True;
      if ShowModal = 1 then
      begin
        frmWJGCKKCSelList.CDS_Main.DisableControls;
        with frmWJGCKKCSelList.CDS_Main do
        begin
          First;
          while frmWJGCKKCSelList.CDS_Main.Locate('SSel', True, []) do
          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('spid').Value := Trim(MMaxno);
              FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('ORDMainIdRK').Value := Trim(frmWJGCKKCSelList.CDS_Main.fieldbyname('ORDMainIdRK').AsString);
              FieldByName('ORDSubIdRK').Value := Trim(frmWJGCKKCSelList.CDS_Main.fieldbyname('ORDSubIdRK').AsString);
              FieldByName('ORDMainIdCK').Value := Trim(frmWJGCKKCSelList.CDS_Main.fieldbyname('ORDMainIdRK').AsString);

              FieldByName('CustomerNoName').Value := Trim(frmWJGCKKCSelList.CDS_Main.fieldbyname('KHNAME1').AsString);
              FieldByName('ORDSubIdCK').Value := Trim(frmWJGCKKCSelList.CDS_Main.fieldbyname('ORDSubIdRK').AsString);
              FieldByName('OrderNo').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('QCOrderNo').Value;
              FieldByName('QCOrderNo').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('QCOrderNo').Value;
              FieldByName('CKOrderNo').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('QCOrderNo').Value;
              FieldByName('SPColor').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('SPColor').AsString;
              FieldByName('SPHX').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('SPHX').AsString;
              FieldByName('FactoryName').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('FactoryName').Value;
              FieldByName('FactoryNo').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('FactoryNo').Value;
              FieldByName('SSType').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('SSType').Value;
              FieldByName('spcode').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('spcode').Value;
              FieldByName('SPName').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('SPName').Value;
              FieldByName('SPSpec').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('SPSpec').Value;
              FieldByName('SPCF').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('SPCF').Value;
              FieldByName('SPMF').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('SPMF').Value;
              FieldByName('SPKZ').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('SPKZ').Value;
              FieldByName('FromFactoryName').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('ToFactoryName').Value;
              FieldByName('FromFactoryNo').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('ToFactoryNo').Value;
              FieldByName('ToFactoryName').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('ToFactoryName').Value;
              FieldByName('ToFactoryNo').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('ToFactoryNo').Value;
              FieldByName('QtyUnit').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('QtyUnit').Value;
              FieldByName('PiQty').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('MXKCPiQty').Value;
              FieldByName('Qty').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('MXKCQty').Value;
              FieldByName('FZSPID').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('SPID').Value;
              FieldByName('ZYTYPE').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('SPID').Value;
              FieldByName('NOTE').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('note').AsString;

              FieldByName('GANGNO').Value := frmWJGCKKCSelList.CDS_Main.fieldbyname('gangno').AsString;
              FieldByName('CRtype').Value := '加工完成';
//              ShowMessage(frmWJGCKKCSelList.CDS_Main.fieldbyname('SPID').AsString);
            end;
            frmWJGCKKCSelList.CDS_Main.Delete;
          end;
        end;
        frmWJGCKKCSelList.CDS_Main.EnableControls;

      end;
    end;
  finally
    frmWJGCKKCSelList.Free;
  end;
end;

procedure TfrmCPBCKInPut.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          edit;
//          FieldByName('ToFactoryNo').Value := Trim(CDS_HZ.fieldbyname('coCode').AsString);
          FieldByName('CustomerNoName').Value := Trim(CDS_HZ.fieldbyname('coName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

end.

