unit U_ProductOrderCarList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxCurrencyEdit, cxImage, cxDBEdit, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxPC, cxSplitter, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, ShellAPI, cxCheckBox, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu;

type
  TfrmProductOrderCarList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_Car: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    CDS_Car: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    Label4: TLabel;
    CustomerNoName: TEdit;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    Label8: TLabel;
    MPRTCodeName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    Label10: TLabel;
    MPRTSpec: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton4: TToolButton;
    Tchk: TToolButton;
    Tnochk: TToolButton;
    TRZ: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    carNo: TEdit;
    Label2: TLabel;
    PRTColor: TEdit;
    Label6: TLabel;
    N1: TMenuItem;
    N3: TMenuItem;
    ToolButton2: TToolButton;
    Panel2: TPanel;
    Panel4: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column14: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxTabControl2: TcxTabControl;
    PopupMenu2: TPopupMenu;
    DS_Ord: TDataSource;
    CDS_Ord: TClientDataSet;
    N4: TMenuItem;
    v1Column1: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    V2Column1: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure TRZClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure v1Column14PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure PopupMenu1Popup(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure cxTabControl1Change(Sender: TObject);
    procedure cxTabControl2Change(Sender: TObject);
    procedure TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
  private
    DQdate: TDateTime;
    FXZQty, FDQQty: Integer;
    ISPrint: Boolean;
    procedure InitGrid();
    procedure InitCar();
    procedure InitForm();
    procedure CXKP();
    function DelData(): Boolean;
    procedure SetStatus();
    procedure PrintCar(fmainID, fcarNO: string);

    { Private declarations }
  public
    FFInt, FCloth: Integer;
    fsyrName: string;
    canshu1: string;
    fFlileFlag: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_OrderInPut, U_Fun, U_FjList_RZ, U_FjList10, U_SysLogHelp,
  U_frmProductOrderCarEdit, U_ProductOrderCarSplit;
{$R *.dfm}

procedure TfrmProductOrderCarList.CXKP();
begin

  if CDS_Car.IsEmpty then
    exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_car where FromCarNo=''' + Trim(CDS_Car.fieldbyname('carNO').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已拆卡不能撤销数据!', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY where APID=''' + Trim(CDS_Car.fieldbyname('carNO').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已检验不能删除数据!', '提示', 0);
    Exit;
  end;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JYOrder_car SET status=''0'' ');
      sql.Add('where carNo=' + quotedstr(trim(CDS_Car.fieldbyname('carNo').AsString)));

      if trim(CDS_Car.fieldbyname('FromCarNo').AsString) = '' then
        sql.Add('exec P_Do_ZDKPCX ' + quotedstr(trim(CDS_Car.fieldbyname('carNo').AsString)));

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('流程卡撤销审核')));
      sql.Add(',' + quotedstr(trim('流程卡号：' + trim(CDS_Car.FieldByName('carNo').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('撤销审核成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('撤销审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderCarList.InitCar();
begin
  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add('select  A.*   ');
      sql.Add(',GlideList=cast( (select '' ''+ GlideName from JYOrder_Glide X where X.CarNo=A.CarNo order by X.GlideIdx for xml path('''')) as varchar(100))  ');
      sql.Add('from JYOrder_car A ');
      sql.Add('where A.mainID=' + Quotedstr(CDS_Ord.FieldByName('mainID').AsString));
      sql.Add('and A.subID=' + Quotedstr(CDS_Ord.FieldByName('subID').AsString));
//      if cxTabControl1.TabIndex < 2 then
//      begin
//        sql.Add(' and  isnull(A.status,''0'')=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
//      end;
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_Car);
    SInitCDSData20(ADOQueryTemp, CDS_Car);

    if not CDS_Car.IsEmpty then
    begin
      with CDS_Car do
      begin
        First;
        while not Eof do
        begin
          Edit;
          FieldByName('SSel').Value := True;
          Next;
        end;
      end;
    end;

  finally
    ADOQueryTemp.EnableControls;
  end;
  FXZQty := 0;
end;

procedure TfrmProductOrderCarList.PrintCar(fMainID, fCarNO: string);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_Print_Car ');
    sql.Add('@MainID=' + quotedstr(trim(fMainID)));
    sql.Add(',@carNo=' + quotedstr(trim(fCarNO)));
    Open;
  end;
  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);

  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(ADOQueryPrint.fieldbyname('carNO').AsString);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
    if FileExists(fImagePath) then
      DeleteFile(fImagePath);
    Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
  except
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    exit;
  end;
  ExportFtErpFile('流程卡.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\流程卡.rmf';

  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RMVariables['FDQQty'] := FDQQty;
    RMVariables['FXZQty'] := FXZQty;
    RM1.LoadFromFile(fPrintFile);
    if ISPrint then
      RM1.PrintReport
    else
      RM1.ShowReport;
    sleep(200);

  end
  else
    Application.MessageBox(PChar('没有找到' + trim(fPrintFile)), '提示信息', 0);
end;

procedure TfrmProductOrderCarList.SetStatus();
begin

//  tchk.Visible := false;
//  tnochk.Visible := false;
//  tbdel.Visible := false;

  if Trim(canshu1) = '录入' then
  begin

  end;
  if Trim(canshu1) = '审核' then
  begin

  end;
end;

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

procedure TfrmProductOrderCarList.FormCreate(Sender: TObject);
begin
  DQdate := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmProductOrderCarList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption + 'TV1', Tv1, '生产指示单管理');
  WriteCxGrid(self.Caption + 'TV2', Tv2, '生产指示单管理');
  Close;
end;

procedure TfrmProductOrderCarList.InitGrid();
var
  strwhere: string;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add('select  A.*,B.* ');
      sql.Add(',KKPS=(select SUM(RollNum) from JYOrder_Car X where X.mainID=A.MainID) ');
      sql.Add(',SHKKPS=(select SUM(RollNum) from JYOrder_Car X where X.mainID=A.MainID AND  X.status=''1'') ');
      sql.Add(' from JYOrder_Main A inner join JYOrder_Sub B on B.mainID=A.mainID ');

      case cxTabControl2.TabIndex of
        0:
          begin
            sql.Add('where not exists (select X.subID from JYOrder_car X where X.subID=B.subID)');
          end;
        1:
          begin
            sql.Add('where exists (select X.subID from JYOrder_car X where X.subID=B.subID)');
            sql.Add('and A.FILLTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
            sql.Add('and A.FILLTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
          end;
      end;
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Ord);
    SInitCDSData20(ADOQueryMain, CDS_Ord);
  finally
    ADOQueryMain.EnableControls;
  end;
  FXZQty := 0;
end;

procedure TfrmProductOrderCarList.InitForm();
begin
  ReadCxGrid(self.Caption + 'TV1', Tv1, '生产指示单管理');
  ReadCxGrid(self.Caption + 'TV2', Tv2, '生产指示单管理');

  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;

  SetStatus();
  InitGrid();
end;

procedure TfrmProductOrderCarList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, CDS_Ord);
  SInitCDSData20(ADOQueryMain, CDS_Ord);
end;

procedure TfrmProductOrderCarList.TBDelClick(Sender: TObject);
begin
  if CDS_Car.IsEmpty then
    Exit;
  while CDS_Car.Locate('SSel', True, []) do
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_car where FromCarNo=''' + Trim(CDS_Car.fieldbyname('carNO').AsString) + '''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已拆卡不能删除数据!', '提示', 0);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY where APID=''' + Trim(CDS_Car.fieldbyname('carNO').AsString) + '''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已检验不能删除数据!', '提示', 0);
      Exit;
    end;
    try
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  JYOrder_car SET status=''0'' ');
        sql.Add('where carNo=' + quotedstr(trim(CDS_Car.fieldbyname('carNo').AsString)));

        if trim(CDS_Car.fieldbyname('FromCarNo').AsString) = '' then
          sql.Add('exec P_Do_ZDKPCX ' + quotedstr(trim(CDS_Car.fieldbyname('carNo').AsString)));

        sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
        sql.Add(' ' + quotedstr(trim(DName)));
        sql.Add(',getdate() ');
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(trim('流程卡撤销审核')));
        sql.Add(',' + quotedstr(trim('流程卡号：' + trim(CDS_Car.FieldByName('carNo').AsString))));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;
    except
      application.MessageBox('撤销审核失败！', '提示信息', 0);
    end;

    with ADOQueryTemp 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 ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已扣匹不能删除数据!', '提示', 0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    if DelData() then
    begin

    end;
    CDS_Car.Delete;
  end;

end;

function TfrmProductOrderCarList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' delete JYOrder_car where carNO=''' + Trim(CDS_Car.fieldbyname('carNo').AsString) + '''');

      sql.Add('update  JYOrder_Main SET status=''0'',chker=null,Chktime=null,Priority=0 ');
      sql.Add('where mainID=' + quotedstr(trim(CDS_Car.fieldbyname('mainID').AsString)));
      sql.Add('and not exists( select carNo from JYOrder_car X where X.mainID=JYOrder_Main.mainID ) ');

      sql.Add(' insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('流程卡删除')));
      sql.Add(',' + quotedstr(trim('指示单号：' + trim(CDS_Car.FieldByName('carNo').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmProductOrderCarList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, self.Caption);
end;

procedure TfrmProductOrderCarList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmProductOrderCarList.OrderNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, CDS_Ord);
  SInitCDSData20(ADOQueryMain, CDS_Ord);
  FXZQty := 0;
end;

procedure TfrmProductOrderCarList.FormShow(Sender: TObject);
begin
  fFlileFlag := UserDataFlag + 'HX';
  InitForm();
end;

procedure TfrmProductOrderCarList.TBTPClick(Sender: TObject);
var
  FQty, FQty1, FMxQty, FPQty, FMxQtyS, FPQtyS: string;
begin
end;

procedure TfrmProductOrderCarList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmProductOrderCarList.Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id, id10: Integer;
begin

end;

procedure TfrmProductOrderCarList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Car, false);
  FXZQty := 0;
end;

procedure TfrmProductOrderCarList.N2Click(Sender: TObject);
begin
  if CDS_Car.isempty then
    exit;
  SelOKNo(CDS_Car, true);
  FXZQty := CDS_Car.RecordCount;
end;

procedure TfrmProductOrderCarList.ToolButton1Click(Sender: TObject);
begin
  if CDS_Ord.IsEmpty then
    Exit;
  try
    frmOrderInPut := TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState := 1;
      fMainID := Trim(Self.CDS_Ord.fieldbyname('MainId').AsString);
      FSubId := Trim(Self.CDS_Ord.fieldbyname('SubId').AsString);
      fsyrName := self.fsyrName;
      ToolBar2.Visible := False;
      ToolBar3.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := False;
      Tv1.OptionsSelection.CellSelect := False;
      fsyrname := self.fsyrName;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmOrderInPut.Free;
  end;
end;

procedure TfrmProductOrderCarList.ToolButton4Click(Sender: TObject);
begin
  if CDS_Ord.IsEmpty then
    Exit;
  try
    frmFjList10 := TfrmFjList10.Create(Application);
    with frmFjList10 do
    begin
      fkeyNO := Trim(Self.CDS_Ord.fieldbyname('MainId').AsString);
      fType := '指示单';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmFjList10.Free;
  end;
end;

procedure TfrmProductOrderCarList.TchkClick(Sender: TObject);
begin
  if CDS_Car.IsEmpty then
    exit;
  ISPrint := True;
  with CDS_Car do
  begin
    DisableControls;
    while CDS_Car.Locate('SSel', True, []) do
    begin
      if Trim(CDS_Car.fieldbyname('status').AsString) <> '1' then
      begin
        try
          ADOQueryCmd.Connection.BeginTrans;
          with ADOQueryTemp 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 ADOQueryTemp.IsEmpty = False then
          begin
            Application.MessageBox('已扣匹操作数据!', '提示', 0);
            Exit;
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select  KYPS=-RollNum+ISNULL((select SUM(FPPS-YYPS) from JYOrder_PBKC X where X.MainId=A.MainId and X.SubId=A.SubId),0)');
            sql.Add('from JYOrder_Car A where CarNo=' + quotedstr(Trim(CDS_Car.fieldbyname('carNO').AsString)));
//          ShowMessage(SQL.Text);
            Open;
          end;
          if ADOQueryTemp.fieldbyname('KYPS').AsInteger < 0 then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('可用匹数不足!', '提示', 0);
            CDS_Car.EnableControls;
            Exit;
          end;

          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  JYOrder_car SET status=''1'' ');
            sql.Add('where carNo=' + quotedstr(trim(CDS_Car.fieldbyname('carNo').AsString)));

            if trim(CDS_Car.fieldbyname('FromCarNo').AsString) = '' then
              sql.Add('exec P_Do_ZDKP ' + quotedstr(trim(CDS_Car.fieldbyname('carNo').AsString)));

            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' ' + quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(',' + quotedstr(trim(self.Caption)));
            sql.Add(',' + quotedstr(trim('流程卡审核')));
            sql.Add(',' + quotedstr(trim('流程卡号：' + trim(CDS_Car.FieldByName('carNo').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;

          ADOQueryCmd.Connection.CommitTrans;
        except
          ADOQueryCmd.Connection.RollbackTrans;
          CDS_Car.EnableControls;
          application.MessageBox('审核失败！', '提示信息', 0);
        end;
      end;

      FDQQty := FDQQty + 1;
      printcar(trim(CDS_Car.fieldbyname('mainID').AsString), trim(CDS_Car.fieldbyname('carNo').AsString));

      CDS_Car.Edit;
      CDS_Car.FieldByName('status').Value := '1';
      CDS_Car.FieldByName('SSel').Value := False;
    end;
    CDS_Car.EnableControls;

  end;

end;

procedure TfrmProductOrderCarList.TnochkClick(Sender: TObject);
begin
  if CDS_Car.IsEmpty then
    exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_car where FromCarNo=''' + Trim(CDS_Car.fieldbyname('carNO').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已拆卡不能撤销数据!', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY where APID=''' + Trim(CDS_Car.fieldbyname('carNO').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已检验不能删除数据!', '提示', 0);
    Exit;
  end;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  JYOrder_car SET status=''0'' ');
      sql.Add('where carNo=' + quotedstr(trim(CDS_Car.fieldbyname('carNo').AsString)));

      if trim(CDS_Car.fieldbyname('FromCarNo').AsString) = '' then
        sql.Add('exec P_Do_ZDKPCX ' + quotedstr(trim(CDS_Car.fieldbyname('carNo').AsString)));

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('流程卡撤销审核')));
      sql.Add(',' + quotedstr(trim('流程卡号：' + trim(CDS_Car.FieldByName('carNo').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('撤销审核成功！', '提示信息');
    TBRafresh.Click;
  except
    application.MessageBox('撤销审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmProductOrderCarList.TRZClick(Sender: TObject);
begin
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
  //  facction:='生产指示单删除';
    showmodal;
    free;
  end;
end;

procedure TfrmProductOrderCarList.N3Click(Sender: TObject);
begin
  if CDS_Car.IsEmpty then
    Exit;
  try
    frmProductOrderCarSplit := TfrmProductOrderCarSplit.Create(Application);
    with frmProductOrderCarSplit do
    begin
      fMainID := Trim(CDS_Car.fieldbyname('MainID').AsString);
      FSubId := Trim(CDS_Car.fieldbyname('SubId').AsString);
      fCarNO := Trim(CDS_Car.fieldbyname('CarNo').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmProductOrderCarSplit.Free;
  end;
end;

procedure TfrmProductOrderCarList.v1Column14PropertiesEditValueChanged(Sender: TObject);
begin
  if CDS_Car.fieldbyname('ssel').AsBoolean then
    FXZQty := FXZQty + 1
  else
    FXZQty := FXZQty - 1;
end;

procedure TfrmProductOrderCarList.ToolButton2Click(Sender: TObject);
begin
  if CDS_Car.IsEmpty then
    Exit;
  if CDS_Car.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  ISPrint := False;
  FDQQty := 0;
  with CDS_Car do
  begin
    DisableControls;
    while CDS_Car.Locate('SSel', True, []) do
    begin
      if Trim(CDS_Car.fieldbyname('status').AsString) <> '1' then
      begin
        try
          ADOQueryCmd.Connection.BeginTrans;
          with ADOQueryTemp 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 ADOQueryTemp.IsEmpty = False then
          begin
            Application.MessageBox('已扣匹操作数据!', '提示', 0);
            Exit;
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select  KYPS=-RollNum+ISNULL((select SUM(FPPS-YYPS) from JYOrder_PBKC X where X.MainId=A.MainId and X.SubId=A.SubId),0)');
            sql.Add('from JYOrder_Car A where CarNo=' + quotedstr(Trim(CDS_Car.fieldbyname('carNO').AsString)));
//          ShowMessage(SQL.Text);
            Open;
          end;
          if ADOQueryTemp.fieldbyname('KYPS').AsInteger < 0 then
          begin
            Application.MessageBox('可用匹数不足!', '提示', 0);
            CDS_Car.EnableControls;
            Exit;
          end;

          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  JYOrder_car SET status=''1'' ');
            sql.Add('where carNo=' + quotedstr(trim(CDS_Car.fieldbyname('carNo').AsString)));

            if trim(CDS_Car.fieldbyname('FromCarNo').AsString) = '' then
              sql.Add('exec P_Do_ZDKP ' + quotedstr(trim(CDS_Car.fieldbyname('carNo').AsString)));

            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' ' + quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(',' + quotedstr(trim(self.Caption)));
            sql.Add(',' + quotedstr(trim('流程卡审核')));
            sql.Add(',' + quotedstr(trim('流程卡号：' + trim(CDS_Car.FieldByName('carNo').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;

          ADOQueryCmd.Connection.CommitTrans;
        except
          ADOQueryCmd.Connection.RollbackTrans;
          CDS_Car.EnableControls;
          application.MessageBox('审核失败！', '提示信息', 0);
        end;
      end;

      FDQQty := FDQQty + 1;
      printcar(trim(CDS_Car.fieldbyname('mainID').AsString), trim(CDS_Car.fieldbyname('carNo').AsString));

      CDS_Car.Edit;
      CDS_Car.FieldByName('status').Value := '1';
      CDS_Car.FieldByName('SSel').Value := False;
    end;
    CDS_Car.EnableControls;
  end;

end;

procedure TfrmProductOrderCarList.PopupMenu1Popup(Sender: TObject);
begin
  if CDS_Car.isempty then
    exit;
  N3.Visible := True;
  if Trim(CDS_Car.fieldbyname('status').AsString) <> '1' then
  begin
    N3.Visible := False;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_car where FromCarNo=''' + Trim(CDS_Car.fieldbyname('carNO').AsString) + '''');
    Open;
  end;
  if not ADOQueryTemp.IsEmpty then
  begin
    N3.Visible := False;
  end;
end;

procedure TfrmProductOrderCarList.N4Click(Sender: TObject);
begin
  try
    frmfrmProductOrderCarEdit := TfrmfrmProductOrderCarEdit.Create(Application);
    with frmfrmProductOrderCarEdit do
    begin
      fMainID := trim(CDS_Ord.fieldbyname('MainID').AsString);
      FsubId := trim(CDS_Ord.fieldbyname('subID').AsString);
      fsyrName := self.fsyrName;
      if ShowModal = 1 then
      begin
        Self.cxTabControl2.TabIndex := 1;
        Self.InitGrid();
        Self.CDS_Ord.Locate('MainID', fMainID, []);
      end;
    end;
  finally
    frmfrmProductOrderCarEdit.Free;
  end;
end;

procedure TfrmProductOrderCarList.TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitCar();
end;

procedure TfrmProductOrderCarList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitCar();
end;

procedure TfrmProductOrderCarList.cxTabControl2Change(Sender: TObject);
begin

  InitGrid();
end;

procedure TfrmProductOrderCarList.TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  FKKPS, FSHKKPS: double;
begin

  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('KKPS').Index] = null then
    FKKPS := 0
  else
    FKKPS := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('KKPS').Index];
  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('SHKKPS').Index] = null then
    FSHKKPS := 0
  else
    FSHKKPS := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('SHKKPS').Index];

  if FKKPS <> FSHKKPS then
    ACanvas.Brush.Color := $00FFFF;

end;

end.

