unit U_frmProductOrderCarEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, BtnEdit, ComCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ToolWin,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGrid, ADODB, DBClient,
  cxCalendar, cxContainer, cxTextEdit, cxCurrencyEdit, cxDropDownEdit,
  cxCheckBox, Menus, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  cxButtonEdit;

type
  TfrmfrmProductOrderCarEdit = class(TForm)
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    Label2: TLabel;
    OrderNo: TBtnEditA;
    Label3: TLabel;
    customerNoName: TEdit;
    Label4: TLabel;
    MprtCodeName: TEdit;
    Label5: TLabel;
    prtColor: TEdit;
    Panel2: TPanel;
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    CRTime: TDateTimePicker;
    Label1: TLabel;
    Label7: TLabel;
    Button1: TButton;
    Button2: TButton;
    V1Column1: TcxGridDBColumn;
    V1Column2: TcxGridDBColumn;
    V1Column4: TcxGridDBColumn;
    CDS_Glide: TClientDataSet;
    DS_Glide: TDataSource;
    CDS_Car: TClientDataSet;
    DS_Car: TDataSource;
    ADO_Car: TADOQuery;
    ADO_Glide: TADOQuery;
    ADO_Order: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    V1Column5: TcxGridDBColumn;
    carnum: TcxCurrencyEdit;
    Button3: TButton;
    V1Column7: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    Label8: TLabel;
    PRTPs: TEdit;
    GroupBox2: TGroupBox;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton6: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    GroupBox3: TGroupBox;
    ToolBar2: TToolBar;
    ToolButton7: TToolButton;
    DS_PBKC: TDataSource;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    V3Column1: TcxGridDBColumn;
    V3Column2: TcxGridDBColumn;
    ADO_PBCK: TADOQuery;
    ToolButton8: TToolButton;
    CDS_PBCK: TClientDataSet;
    V3Column3: TcxGridDBColumn;
    V3Column4: TcxGridDBColumn;
    V3Column5: TcxGridDBColumn;
    Label9: TLabel;
    orderType: TEdit;
    Label10: TLabel;
    MPRTMF: TEdit;
    Label11: TLabel;
    MPRTKZ: TEdit;
    VC_KKMF: TcxGridDBColumn;
    VC_KKKZ: TcxGridDBColumn;
    V1Column10: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    Label6: TLabel;
    gongyi: TEdit;
    TV2Column4: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure OrderNoBtnClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TV1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure ToolButton6Click(Sender: TObject);
    procedure TV1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure V3Column4PropertiesEditValueChanged(Sender: TObject);
    procedure TV1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure VC_KKKZPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    fOrderType: string;
    procedure InitOrder();
    procedure InitCar();
    procedure InitGlide();
    procedure InitPBCK();
    { Private declarations }
  public
    fsyrName, fMainID, fsubID, fcarNo,fgongyi: string;

    { Public declarations }
  end;

var
  frmfrmProductOrderCarEdit: TfrmfrmProductOrderCarEdit;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_ProductOrderListSel, U_GXGL_List_DXCX,
  U_PBKcList;
{$R *.dfm}

procedure TfrmfrmProductOrderCarEdit.InitPBCK();
begin

  try
    with ADO_PBCK do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*');
      sql.Add(',KCPS=(select X.KCPS from CK_PB_CR X  where X.Valid=''Y'' and X.CRFlag=''入库'' and X.CRID=A.KCID)');
      sql.Add('from JYOrder_PBKC A ');
      sql.Add(' where A.MainID=' + QUOTEDSTR(Trim(FMainID)));
      sql.Add(' AND A.SUBID=' + QUOTEDSTR(Trim(FSUBID)));
//      ShowMessage(SQL.Text);
      open;
    end;
    SCreateCDS20(ADO_PBCK, CDS_PBCK);
    SInitCDSData20(ADO_PBCK, CDS_PBCK);
  finally

  end;
end;

procedure TfrmfrmProductOrderCarEdit.InitGlide();
begin
  try
    with Ado_Glide do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*');
      sql.Add('from JYOrder_Glide A ');
      sql.Add('where A.carNo=''' + trim(cds_Car.fieldbyname('carNo').AsString) + ''' ');
      open;
    end;
    SCreateCDS20(Ado_Glide, CDS_Glide);
    SInitCDSData20(Ado_Glide, CDS_Glide);
  finally
  end;
end;

procedure TfrmfrmProductOrderCarEdit.InitCar();
begin
  try
    with Ado_car do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*,oldRollNum=A.RollNum');
      sql.Add('from JYOrder_car A ');
      sql.Add('where A.MainID=''' + trim(fmainID) + ''' ');
      sql.Add('and A.subID=''' + trim(fsubID) + ''' ');
      open;
    end;
    SCreateCDS20(Ado_car, CDS_car);
    SInitCDSData20(Ado_car, CDS_car);
  finally
  end;
end;

procedure TfrmfrmProductOrderCarEdit.InitOrder();
begin
  try
    with Ado_Order do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*,B.*');
      sql.Add('from JYOrder_sub A ');
      sql.Add('inner join JYOrder_Main B on B.MainID=A.mainID ');
      sql.Add('where A.MainID=''' + trim(fMainID) + ''' ');
      sql.Add('and A.subID=''' + trim(fsubID) + ''' ');
      open;
    end;
    if not Ado_Order.IsEmpty then
    begin
      SCSHDataNew(Ado_Order, Panel1, 2);
      if trim(orderType.Text) = '正常单' then
      begin
        GroupBox3.Visible := False;
      end
      else
      begin
        GroupBox3.Visible := False;
      end;
    end;
  finally
  end;
end;

procedure TfrmfrmProductOrderCarEdit.FormDestroy(Sender: TObject);
begin
  frmfrmProductOrderCarEdit := nil;
end;

procedure TfrmfrmProductOrderCarEdit.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cahide;
end;

procedure TfrmfrmProductOrderCarEdit.FormCreate(Sender: TObject);
begin
  GroupBox1.Align := AlClient;
  CRTime.Date := DServerDate;
end;

procedure TfrmfrmProductOrderCarEdit.ToolButton2Click(Sender: TObject);
begin
  with CDS_Glide do
  begin
    if IsEmpty then
      Exit;
    if RecNo >= 1 then
    begin
      //Prior;
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value - 1;
      Post;
      Prior;
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value + 1;
      Post;
    end;
    Prior;
  end;
end;

procedure TfrmfrmProductOrderCarEdit.ToolButton3Click(Sender: TObject);
begin
  CDS_Glide.IndexFieldNames := 'GlideIdx';
  with CDS_Glide do
  begin
    if IsEmpty then
      Exit;
    Next;
    if not Eof then
      Prior
    else
    begin
      exit;
    end;
    if not Eof then
    begin
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value + 1;
      Post;
      Prior;
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value - 1;
      Post;
    end;
    Next;
  end;
end;

procedure TfrmfrmProductOrderCarEdit.ToolButton4Click(Sender: TObject);
var
  k: integer;
begin
  if cds_Glide.IsEmpty then
    exit;
  cds_Glide.Delete;
  cds_Glide.IndexFieldNames := 'GlideIdx';
  cds_Glide.DisableControls;
  cds_Glide.First;
  k := 1;
  while not cds_Glide.Eof do
  begin
    cds_Glide.Edit;
    cds_Glide.FieldByName('GlideIdx').Value := k;
    cds_Glide.Post;
    inc(k);
    cds_Glide.Next;
  end;
  cds_Glide.First;
  cds_Glide.EnableControls;
end;

procedure TfrmfrmProductOrderCarEdit.OrderNoBtnClick(Sender: TObject);
begin
  frmProductOrderListSel := TfrmProductOrderListSel.create(self);
  with frmProductOrderListSel do
  begin
    fsyrName := self.fsyrName;
    if ShowModal = 1 then
    begin
      fmainID := Order_Main.fieldbyname('mainID').asstring;
      fsubID := Order_Main.fieldbyname('subID').asstring;
      Fgongyi := Order_Main.fieldbyname('gongyi').asstring;
      InitOrder();
      InitCar();
      initGlide();
      InitPBCK();
    end;
    free;
  end;
end;

procedure TfrmfrmProductOrderCarEdit.FormShow(Sender: TObject);
begin
  InitOrder();
  InitCar();
  initGlide();
  InitPBCK();
end;

procedure TfrmfrmProductOrderCarEdit.Button1Click(Sender: TObject);
var
  i, k, Fcarxh: integer;
  maxId: string;
  MRollNum: Double;
begin
  if trim(OrderNo.Text) = '' then
  begin
    application.MessageBox('计划单号不能为空！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if CDS_Car.IsEmpty then
    Fcarxh := 0
  else
    Fcarxh := CDS_Car.RecordCount;

  if strtointdef(trim(carNum.text), 0) <= 1 then
    k := 1
  else
    k := strtointdef(trim(carNum.text), 0);

  MRollNum := RoundFloat(strtointdef(trim(PRTPs.text), 0) / k, 0);

  CDS_Car.Last;
  for i := 1 to k do
  begin
    Fcarxh := Fcarxh + 1;
    CopyAddRowCDS(CDS_Car);
    if GetLSNo(ADOQueryCmd, maxId, 'CZ', 'JYOrder_Car', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    CDS_Car.edit;
    CDS_Car.FieldByName('carNo').Value := maxId;
    CDS_Car.FieldByName('carxh').Value := Fcarxh;
    CDS_Car.FieldByName('CRTime').Value := CRTime.date;
    CDS_Car.FieldByName('Sflag').Value := '0';
    CDS_Car.FieldByName('KKMF').Value := Trim(MPRTMF.Text);
    CDS_Car.FieldByName('KKKZ').Value := Trim(MPRTKZ.Text);
    CDS_Car.FieldByName('RollNum').Value := MRollNum;
    CDS_Car.Post;
  end;
end;

procedure TfrmfrmProductOrderCarEdit.Button2Click(Sender: TObject);
begin
  if CDS_car.IsEmpty then
    exit;
  if CDS_car.Locate('RollNum', null, []) then
  begin
    application.MessageBox('开卡匹数不能为零！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if CDS_car.Locate('RollNum', 0, []) then
  begin
    application.MessageBox('开卡匹数不能为零！', '提示信息', MB_ICONERROR);
    exit;
  end;
  ADOQueryCmd.Connection.BeginTrans;
  try
    CDS_Car.DisableControls;
    CDS_Car.First;
    while not CDS_Car.Eof do
    begin
      if CDS_Car.FieldByName('GlideIDx').AsInteger = 0 then
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from JYOrder_Car A');
          sql.Add('where CarNO=''' + trim(CDS_Car.fieldbyname('CarNo').AsString) + '''');
          open;
          if IsEmpty then
          begin
            append;
            fieldbyname('Filler').Value := trim(DName);
          end
          else
          begin
            edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTmp);
          end;
          FieldByName('carNo').Value := trim(CDS_Car.fieldbyname('carNo').AsString);
          FieldByName('CrTime').Value := formatdateTime('yyyy-MM-dd', CDS_Car.fieldbyname('CrTime').AsDateTime);
          FieldByName('MainID').Value := trim(fMainID);
          FieldByName('subID').Value := trim(fsubID);
          FieldByName('carxh').Value := CDS_Car.fieldbyname('carxh').AsInteger;
          FieldByName('KKMF').Value := trim(CDS_Car.fieldbyname('KKMF').AsString);
          FieldByName('KKKZ').Value := trim(CDS_Car.fieldbyname('KKKZ').AsString);
          FieldByName('RollNum').Value := CDS_Car.fieldbyname('RollNum').AsInteger;
          FieldByName('Qty').Value := CDS_Car.fieldbyname('Qty').AsFloat;
          FieldByName('QtyUnit').Value := trim(CDS_Car.fieldbyname('QtyUnit').AsString);
          FieldByName('Note').Value := trim(CDS_Car.fieldbyname('Note').AsString);
          FieldByName('valid').Value := 'Y';
          FieldByName('KKGY').Value := Fgongyi;
          FieldByName('KKGH').Value := trim(CDS_Car.fieldbyname('KKGH').AsString);
          post;
        end;

      end;
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('exec P_Insert_Glide ' + quotedstr(OrderNo.Text) + ',' + quotedstr(Trim(trim(CDS_Car.fieldbyname('CarNo').AsString))));
        ExecSQL;
      end;
      CDS_Car.Next;
    end;

    CDS_Car.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('保存成功！', '提示信息');
    ModalResult := 1;
  except
    CDS_Car.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmfrmProductOrderCarEdit.Button3Click(Sender: TObject);
begin
  if CDS_car.IsEmpty then
    exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_PB_CR where Valid=''Y'' and  CRType=''自动扣匹''');
    sql.Add('and CarNo=' + quotedstr(Trim(CDS_car.fieldbyname('carNO').AsString)));
    Open;
  end;
  if ADOQueryTmp.IsEmpty = False then
  begin
    Application.MessageBox('已扣匹不能删除数据!', '提示', 0);
    Exit;
  end;
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY where APID=''' + Trim(CDS_Car.fieldbyname('carNO').AsString) + '''');
    Open;
  end;
  if ADOQueryTmp.IsEmpty = False then
  begin
    Application.MessageBox('已检验不能删除数据!', '提示', 0);
    Exit;
  end;
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' delete JYOrder_car where carNO=''' + Trim(CDS_car.fieldbyname('carNo').AsString) + '''');
      sql.Add(' exec P_Do_PbOut_Del @carNO=''' + Trim(CDS_car.fieldbyname('carNo').AsString) + '''');
      execsql;
    end;
    CDS_car.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmfrmProductOrderCarEdit.ToolButton5Click(Sender: TObject);
var
  maxID: string;
begin
  if CDS_Glide.IsEmpty then
    exit;
  CDS_Glide.First;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from JYOrder_Glide ');
      sql.Add('where carNO=''' + trim(cds_car.fieldbyname('carNo').asstring) + '''');
      execsql;
    end;
    CDS_Glide.DisableControls;
    CDS_Glide.First;
    while not CDS_Glide.Eof do
    begin
      if trim(CDS_Glide.FieldByName('GlideNo').asstring) = '' then
      begin
        if GetLSNo(ADOQueryCmd, maxID, 'CG', 'JYOrder_Glide', 4, 1) = False then
        begin
          Application.MessageBox('取最大号失败!', '提示', 0);
          Exit;
        end;
        CDS_Glide.Edit;
        CDS_Glide.FieldByName('GlideNo').Value := trim(maxID);
      end;
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from JYOrder_Glide A');
        sql.Add('where GlideNo=''' + trim(CDS_Glide.fieldbyname('GlideNo').AsString) + '''');
        open;
        if IsEmpty then
        begin
          append;
          fieldbyname('Filler').Value := trim(DName);
        end
        else
        begin
          edit;
          FieldByName('Editer').Value := Trim(DName);
          FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTmp);
        end;
        FieldByName('GlideNo').Value := trim(CDS_Glide.fieldbyname('GlideNo').AsString);
        FieldByName('Glideidx').Value := CDS_Glide.fieldbyname('GlideIdx').AsInteger;
        FieldByName('MainID').Value := trim(fMainID);
        FieldByName('subID').Value := trim(fsubID);
        FieldByName('carNo').Value := trim(CDS_car.fieldbyname('carNo').AsString);
        FieldByName('GlideName').Value := trim(CDS_Glide.fieldbyname('GlideName').AsString);
        FieldByName('RollNum').Value := CDS_Glide.fieldbyname('RollNum').AsInteger;
      //  FieldByName('Qty').Value:=CDS_Glide.fieldbyname('Qty').AsFloat;
//        FieldByName('QtyUnit').Value:=trim(CDS_Glide.fieldbyname('QtyUnit').AsString);
//        FieldByName('Note').Value := trim(CDS_Glide.fieldbyname('Note').AsString);
        FieldByName('MXPS').Value := CDS_Glide.fieldbyname('MXPS').AsInteger;
        FieldByName('kaipi').Value := CDS_Glide.fieldbyname('kaipi').Asstring;
   
        post;
      end;
      CDS_Glide.Next;
    end;
    CDS_Glide.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('保存成功！', '提示信息');
  except
    CDS_Glide.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmfrmProductOrderCarEdit.ToolButton1Click(Sender: TObject);
var
  maxId: string;
begin
  if CDS_Car.IsEmpty then
    exit;
  if not CDS_Car.Locate('Ssel', true, []) then
  begin
    Application.MessageBox('没有选择流程卡！', '提示信息', MB_ICONERROR);
    exit;
  end;
  CDS_Glide.Append;
    CDS_Glide.FieldByName('Glideidx').Value := CDS_Glide.RecordCount+1;
    CDS_Glide.FieldByName('kaipi').Value := '否';
    CDS_Glide.Post;
//  frmGXGL_List_DXCX := TfrmGXGL_List_DXCX.create(self);
//  with frmGXGL_List_DXCX do
//  begin
//    if showmodal = 1 then
//    begin
//      try
//        ADOQueryCmd.Connection.BeginTrans;
//        CDS_Car.DisableControls;
//        CDS_Car.First;
//        Order_FZ.DisableControls;
//        while not CDS_Car.Eof do
//        begin
//          if CDS_Car.fieldbyname('Ssel').AsBoolean then
//          begin
//            with ADOQueryCmd do
//            begin
//              close;
//              sql.Clear;
//              sql.Add('delete from JYOrder_Glide ');
//              sql.Add('where carNo=''' + trim(CDS_Car.fieldbyname('carNo').AsString) + '''');
//              execsql;
//            end;
//
//            Order_FZ.First;
//            while not Order_FZ.eof do
//            begin
//              if GetLSNo(ADOQueryCmd, maxId, 'CG', 'JYOrder_Glide', 4, 1) = False then
//              begin
//                Application.MessageBox('取最大号失败!', '提示', 0);
//                Exit;
//              end;
//              with ADOQueryCmd do
//              begin
//                close;
//                sql.Clear;
//                sql.Add('select * from JYOrder_Glide where 1=2');
//                open;
//
//                ADOQueryCmd.Append;
//                ADOQueryCmd.FieldByName('GlideNo').Value := maxId;
//                ADOQueryCmd.FieldByName('Glideidx').Value := Order_FZ.RecNo;
//                ADOQueryCmd.FieldByName('MainID').Value := trim(fMainID);
//                ADOQueryCmd.FieldByName('subID').Value := trim(fsubID);
//                ADOQueryCmd.FieldByName('carNo').Value := trim(CDS_Car.fieldbyname('carNo').AsString);
//                ADOQueryCmd.FieldByName('GlideName').Value := trim(Order_FZ.fieldbyname('PSName').AsString);
//          //    ADOQueryCmd.FieldByName('RollNum').Value:=CDS_Car.fieldbyname('RollNum').AsInteger;
//           //   ADOQueryCmd.FieldByName('Qty').Value:=CDS_Car.fieldbyname('Qty').AsFloat;
//                ADOQueryCmd.FieldByName('QtyUnit').Value := trim(CDS_Glide.fieldbyname('QtyUnit').AsString);
//           //   ADOQueryCmd.FieldByName('Note').Value:=trim(Order_FZ.fieldbyname('Note').AsString);
//                ADOQueryCmd.Post;
//              end;
//              Order_FZ.Next;
//            end;
//            Order_FZ.Next;
//          end;
//          CDS_Car.Next;
//        end;
//        CDS_Car.EnableControls;
//        ADOQueryCmd.Connection.CommitTrans;
//        initGlide();
//      except
//        CDS_Car.EnableControls;
//        ADOQueryCmd.Connection.RollbackTrans;
//        frmGXGL_List_DXCX.Free;
//        application.MessageBox('保存失败！', '提示信息', 0);
//      end;
//    end;
//    free;
//  end;
end;

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

end;

procedure TfrmfrmProductOrderCarEdit.ToolButton6Click(Sender: TObject);
begin
  if CDS_Car.IsEmpty then
    exit;
  frmGXGL_List_DXCX := TfrmGXGL_List_DXCX.create(self);
  with frmGXGL_List_DXCX do
  begin
    if showmodal = 1 then
    begin
      Order_FZ.First;
      while not Order_FZ.eof do
      begin
        CDS_Glide.Append;
        CDS_Glide.FieldByName('Glideidx').Value := CDS_Glide.RecordCount + 1;
        CDS_Glide.FieldByName('kaipi').Value := '否';
        CDS_Glide.FieldByName('GlideName').Value := Order_FZ.fieldbyname('PSName').AsString;
        CDS_Glide.Post;
        Order_FZ.next;
      end;
    end;
    free;
  end;

end;

procedure TfrmfrmProductOrderCarEdit.TV1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  InitGlide();
end;

procedure TfrmfrmProductOrderCarEdit.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Car, true);
end;

procedure TfrmfrmProductOrderCarEdit.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Car, true);
end;

procedure TfrmfrmProductOrderCarEdit.ToolButton7Click(Sender: TObject);
var
  maxno: string;
begin
  frmPBKcList := TfrmPBKcList.create(self);
  with frmPBKcList do
  begin
    fType := 10;
    if showmodal = 1 then
    begin
      with CDS_Main do
      begin
        while CDS_Main.Locate('SSel', True, []) do
        begin
          with ADOQueryCmd do
          begin
            sql.Add('select * from JYOrder_PBKC');
            sql.Add('where MainID=' + QuotedStr(trim(Self.fMainID)));
            sql.Add('and subID=' + QuotedStr(trim(Self.fsubID)));
            sql.Add('and KCID' + QuotedStr(Trim(CDS_Main.FieldByName('KCID').AsString)));
          end;
          if ADOQueryCmd.IsEmpty then
          begin
            if GetLSNo(ADOQueryCmd, maxno, 'YK', 'JYOrder_PBKC', 4, 1) = False then
            begin
              Application.MessageBox('生成流水号异常！', '提示', 0);
              exit;
            end;
            with ADOQueryCmd do
            begin
              Close;
              SQL.Clear;
              sql.Add('insert into JYOrder_PBKC(PBKCID,MainID,subID,KCID,FPPS ) ');
              sql.Add('values(' + QuotedStr(maxno));
              sql.Add(',' + QuotedStr(trim(Self.fMainID)));
              sql.Add(',' + QuotedStr(trim(Self.fsubID)));
              sql.Add(',' + QuotedStr(Trim(CDS_Main.FieldByName('KCID').AsString)));
              sql.Add(',' + QuotedStr(Trim(CDS_Main.FieldByName('KCPS').AsString)));
              sql.Add(' )');
              ExecSQL;
            end;
          end;
          CDS_Main.Delete;
        end;
      end;
    end;
    free;
  end;
  InitPBCK();
end;

procedure TfrmfrmProductOrderCarEdit.ToolButton8Click(Sender: TObject);
begin
  if CDS_PBCK.IsEmpty then
    exit;

  if CDS_PBCK.fieldbyname('YYPS').AsInteger > 0 then
    exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' delete JYOrder_PBKC where PBKCID=''' + Trim(CDS_PBCK.fieldbyname('PBKCID').AsString) + '''');
      sql.Add(' and  YYPS=0');
      execsql;
    end;
    CDS_PBCK.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmfrmProductOrderCarEdit.V3Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with CDS_PBCK do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate JYOrder_PBKC ');
    sql.Add(' Set FPPS=' + Trim(mvalue));
    sql.Add(' where PBKCID=' + quotedstr(Trim(CDS_PBCK.FIELDBYNAME('PBKCID').AsString)));
    ExecSQL;
  end;
  tv3.Controller.EditingController.ShowEdit();
end;

procedure TfrmfrmProductOrderCarEdit.TV1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  i: Integer;
begin
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_PB_CR where Valid=''Y'' and  CRType=''自动扣匹''');
    sql.Add('and CarNo=' + quotedstr(Trim(CDS_car.fieldbyname('carNO').AsString)));
    Open;
  end;
  if ADOQueryTmp.IsEmpty = False then
  begin
    for i := 0 to tv1.ColumnCount - 1 do
    begin
      if uppercase(tv1.Columns[i].DataBinding.FieldName) <> 'SSEL' then
        tv1.Columns[i].Options.Editing := false;
    end;
  end;
  VC_KKMF.Options.Editing := True;
  VC_KKKZ.Options.Editing := True;
end;

procedure TfrmfrmProductOrderCarEdit.VC_KKKZPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := 'VC_KKKZ/克重';
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

