unit U_CKProductBCPOutList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridDBTableView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxDropDownEdit, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxCheckBox, Menus,
  MovePanel, RM_E_llPDF, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmCKProductBCPOutList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    MPRTCodeName: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label5: TLabel;
    orderNo: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    v1Column3: TcxGridDBColumn;
    CDS_PRT: TClientDataSet;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1Column4: TcxGridDBColumn;
    Label10: TLabel;
    CkOrdNo: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    RMDBMX: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    TBCKCX: TToolButton;
    v1Column11: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    MovePanel2: TMovePanel;
    CDS_CX: TClientDataSet;
    v1Column12: TcxGridDBColumn;
    CDS_BaoNo: TClientDataSet;
    CDS_Juan: TClientDataSet;
    CDS_BaoPRT: TClientDataSet;
    DS_BaoPRT: TDataSource;
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column2: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Tv3Column1: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    Tv3Column4: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    v3Column9: TcxGridDBColumn;
    v3Column10: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    v3Column12: TcxGridDBColumn;
    DS_JuanPRT: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    PRTColor: TComboBox;
    Label11: TLabel;
    AOrdDefStr1: TComboBox;
    RMDBHZ: TRMDBDataSet;
    RMDBMain10: TRMDBDataSet;
    ComboBox1: TComboBox;
    RM3: TRMGridReport;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    cxGridPopupMenu4: TcxGridPopupMenu;
    RMDBWXPTPrint: TRMDBDataSet;
    CDS_WXPT: TClientDataSet;
    DataSource2: TDataSource;
    CDS_WXPTPrint: TClientDataSet;
    MJXHM: TEdit;
    Label12: TLabel;
    CheckBox1: TCheckBox;
    Panel4: TPanel;
    Label13: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column17: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Panel2: TPanel;
    Label14: TLabel;
    THMJID: TEdit;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    conNo: TEdit;
    Label15: TLabel;
    Label16: TLabel;
    customerNoName: TEdit;
    Label17: TLabel;
    apid: TEdit;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    PRTKuanNO: TEdit;
    Label18: TLabel;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
//    RMllPDFExport1: TRMllPDFExport;
    v1rkplace: TcxGridDBColumn;
    RKplace: TEdit;
    Label19: TLabel;
    RMDBPRT: TRMDBDataSet;
    v1Column24: TcxGridDBColumn;
    Label20: TLabel;
    PRTHX: TEdit;
    OrdPerson2: TEdit;
    Label21: TLabel;
    v1Column25: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    RKNO: TEdit;
    Label22: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBCKCXClick(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure PRTColorChange(Sender: TObject);
    procedure AOrdDefStr1Change(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image2Click(Sender: TObject);
    procedure Panel2DblClick(Sender: TObject);
    procedure THMJIDKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
        FLeft,FTop:Integer;
    procedure InitGrid();
    function  YSData(Order_Main10:TClientDataSet):Boolean;

    procedure Print_WXPT();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCKProductBCPOutList: TfrmCKProductBCPOutList;

implementation
uses
  U_DataLink,U_Fun, U_JYMJIDPRINT;

{$R *.dfm}
procedure TfrmckproductBCPOutList.Print_WXPT();
var
  j,z:Integer;
  FGangNo,FHZ:String;
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMD1 ');
    sql.add('@mainID='+quotedstr(Trim(CDS_Main.FieldByName('CKOrdNo').AsString)));
    sql.add(',@DName='+quotedstr(Trim(DCode)));
    Open;
  end;
  SCreateCDS20(ADOQueryCmd,CDS_WXPT);
  SInitCDSData20(ADOQueryCmd,CDS_WXPT);
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('select MainId=Cast('''' as varchar(20)),SubId=Cast('''' as varchar(20)),PONO=Cast('''' as varchar(20)),InvNo=Cast('''' as varchar(20)),');
    sql.Add(' PRTColor=Cast('''' as varchar(20)),GangNo=Cast('''' as varchar(20)),BaoNo=Cast('''' as varchar(20)),MZQty=Cast(0 as decimal(18,2)),');
    sql.Add(' RollNo0=Cast('''' as varchar(20)),RollNo1=Cast('''' as varchar(20)),RollNo2=Cast('''' as varchar(20)),ITEM=Cast('''' as varchar(20)),');
    sql.Add(' LenQty0=Cast(0 as decimal(18,2)),LenQty1=Cast(0 as decimal(18,2)),LenQty2=Cast(0 as decimal(18,2)),');
    sql.Add(' KGQty0=Cast(0 as decimal(18,2)),KGQty1=Cast(0 as decimal(18,2)),KGQty2=Cast(0 as decimal(18,2)),');
    sql.Add(' MKGQty0=Cast(0 as decimal(18,2)),MKGQty1=Cast(0 as decimal(18,2)),MKGQty2=Cast(0 as decimal(18,2)),');
    sql.Add(' ZS0=Cast(0 as decimal(18,2)),Zs1=Cast(0 as decimal(18,2)),Zs2=Cast(0 as decimal(18,2)),');
    sql.add(' CRTime=cast(null as datetime),JQty=Cast(0 as decimal(18,2)),XH=Cast(0 as int),SumlenQty=Cast(0 as decimal(18,2)),SumRoll=Cast(0 as decimal(18,2)),');
    sql.Add(' SumRollGanNo=Cast(0 as decimal(18,2)),SumRollColor=Cast(0 as decimal(18,2)),SumZJ=Cast(0 as decimal(18,2)),');
    sql.add(' QtyUnit=cast('''' as varchar(20))');
    Open;
  end;
  SCreateCDS20(ADOQueryCmd,CDS_WXPTPrint);
  SInitCDSData20(ADOQueryCmd,CDS_WXPTPrint);
  if CDS_WXPTPrint.IsEmpty=False then
  begin
    CDS_WXPTPrint.Delete;
  end;
  j:=0;z:=0;FGangNo:='';
  CDS_WXPT.DisableControls;
  CDS_WXPTPrint.DisableControls;
  with CDS_WXPT do
  begin
    First;
    while not Eof do
    begin
      with CDS_WXPTPrint do
      begin
        if CDS_WXPTPrint.IsEmpty then
        begin
          CDS_WXPTPrint.Append;
          CDS_WXPTPrint.FieldByName('MZQty').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
          CDS_WXPTPrint.FieldByName('INVNO').Value:=CDS_WXPT.fieldbyname('orderno').Value;
          CDS_WXPTPrint.FieldByName('GangNo').Value:=CDS_WXPT.fieldbyname('GangNo').Value;
          CDS_WXPTPrint.FieldByName('BaoNo').Value:=CDS_WXPT.fieldbyname('BaoNo').Value;
          CDS_WXPTPrint.FieldByName('PONO').Value:=CDS_WXPT.fieldbyname('PONO').Value;
          CDS_WXPTPrint.FieldByName('QtyUnit').Value:=trim(CDS_WXPT.fieldbyname('QtyUnit').Asstring);
          CDS_WXPTPrint.FieldByName('CRTime').Value:=CDS_WXPT.fieldbyname('CRTime').asdatetime;
          CDS_WXPTPrint.FieldByName('PRTColor').Value:=CDS_WXPT.fieldbyname('PRTColor').Value;
          CDS_WXPTPrint.FieldByName('XH').Value:=0;
          CDS_WXPTPrint.FieldByName('LenQty0').Value:=CDS_WXPT.fieldbyname('qty').AsFloat;
          CDS_WXPTPrint.FieldByName('Zs0').Value:=CDS_WXPT.fieldbyname('MjQty2').AsFloat;
          CDS_WXPTPrint.FieldByName('RollNo0').Value:=CDS_WXPT.fieldbyname('RollNo').Value;
          CDS_WXPTPrint.FieldByName('MKGQty0').Value:=CDS_WXPT.fieldbyname('KGQty').AsFloat;
          CDS_WXPTPrint.FieldByName('KGQty0').Value:=CDS_WXPT.fieldbyname('MJqty4').AsFloat;
          CDS_WXPTPrint.FieldByName('Sumlenqty').Value:=CDS_WXPT.fieldbyname('Sumlenqty').AsFloat;
          CDS_WXPTPrint.FieldByName('sumroll').Value:=CDS_WXPT.fieldbyname('sumroll').AsFloat;
          CDS_WXPTPrint.FieldByName('SumRollGanNo').Value:=CDS_WXPT.fieldbyname('SumRollGanNo').AsFloat;
          CDS_WXPTPrint.FieldByName('SumRollColor').Value:=CDS_WXPT.fieldbyname('SumRollColor').AsFloat;
          CDS_WXPTPrint.FieldByName('SumZJ').Value:=CDS_WXPT.fieldbyname('SumZJ').AsFloat;
          CDS_WXPTPrint.FieldByName('SubId').Value:=CDS_WXPT.fieldbyname('SubId').Value;
          CDS_WXPTPrint.FieldByName('MainId').Value:=CDS_WXPT.fieldbyname('MainId').Value;
          CDS_WXPTPrint.Post;
          FHZ:=Trim(CDS_WXPT.fieldbyname('PRTColor').AsString)+Trim(CDS_WXPT.fieldbyname('Baono').AsString)+Trim(CDS_WXPT.fieldbyname('GangNo').AsString);
          Z:=0;
          j:=0;
        end
        else
        begin
          if (Trim(CDS_WXPT.fieldbyname('PRTColor').AsString)+Trim(CDS_WXPT.fieldbyname('Baono').AsString)+Trim(CDS_WXPT.fieldbyname('GangNo').AsString))<>FHZ then
          begin
            CDS_WXPTPrint.Append;
            CDS_WXPTPrint.FieldByName('MZQty').Value:=trim(CDS_WXPT.fieldbyname('KGQty').Asstring);
            CDS_WXPTPrint.FieldByName('INVNO').Value:=trim(CDS_WXPT.fieldbyname('orderno').Asstring);
            CDS_WXPTPrint.FieldByName('GangNo').Value:=trim(CDS_WXPT.fieldbyname('GangNo').Asstring);
            CDS_WXPTPrint.FieldByName('BaoNo').Value:=Trim(CDS_WXPT.fieldbyname('BaoNo').Asstring);
            CDS_WXPTPrint.FieldByName('PONO').Value:=trim(CDS_WXPT.fieldbyname('PONO').Asstring);
            CDS_WXPTPrint.FieldByName('QtyUnit').Value:=Trim(CDS_WXPT.fieldbyname('QtyUnit').Asstring);
            CDS_WXPTPrint.FieldByName('CRTime').Value:=CDS_WXPT.fieldbyname('CRTime').asdatetime;
            CDS_WXPTPrint.FieldByName('PRTColor').Value:=trim(CDS_WXPT.fieldbyname('PRTColor').Asstring);
            CDS_WXPTPrint.FieldByName('XH').Value:=J+1;
            CDS_WXPTPrint.FieldByName('LenQty0').Value:=CDS_WXPT.fieldbyname('qty').AsFloat;
            CDS_WXPTPrint.FieldByName('Zs0').Value:=CDS_WXPT.fieldbyname('MjQty2').AsFloat;
            CDS_WXPTPrint.FieldByName('RollNo0').Value:=trim(CDS_WXPT.fieldbyname('RollNo').Asstring);
            CDS_WXPTPrint.FieldByName('MKGQty0').Value:=CDS_WXPT.fieldbyname('KGQty').AsFloat;
            CDS_WXPTPrint.FieldByName('KGQty0').Value:=CDS_WXPT.fieldbyname('MJqty4').AsFloat;
            CDS_WXPTPrint.FieldByName('Sumlenqty').Value:=CDS_WXPT.fieldbyname('Sumlenqty').AsFloat;
            CDS_WXPTPrint.FieldByName('sumroll').Value:=CDS_WXPT.fieldbyname('sumroll').AsFloat;
            CDS_WXPTPrint.FieldByName('SumRollGanNo').Value:=CDS_WXPT.fieldbyname('SumRollGanNo').AsFloat;
            CDS_WXPTPrint.FieldByName('SumRollColor').Value:=CDS_WXPT.fieldbyname('SumRollColor').AsFloat;
            CDS_WXPTPrint.FieldByName('SumZJ').Value:=CDS_WXPT.fieldbyname('SumZJ').AsFloat;
            CDS_WXPTPrint.FieldByName('SubId').Value:=CDS_WXPT.fieldbyname('SubId').Asstring;
            CDS_WXPTPrint.FieldByName('MainId').Value:=CDS_WXPT.fieldbyname('MainId').Asstring;
            CDS_WXPTPrint.Post;
            Z:=0;
            j:=j+1;
            FHZ:=Trim(CDS_WXPT.fieldbyname('PRTColor').AsString)+Trim(CDS_WXPT.fieldbyname('Baono').AsString)+Trim(CDS_WXPT.fieldbyname('GangNo').AsString);
            end
          else
          begin
            if Z<3 then
            begin
              CDS_WXPTPrint.Locate('XH',j,[]);
              with CDS_WXPTPrint do
              begin
                Edit;
                FieldByName('MZQty').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
                FieldByName('INVNO').Value:=CDS_WXPT.fieldbyname('orderno').Value;
                FieldByName('GangNo').Value:=CDS_WXPT.fieldbyname('GangNo').Value;
                CDS_WXPTPrint.FieldByName('PONO').Value:=CDS_WXPT.fieldbyname('PONO').Value;
                CDS_WXPTPrint.FieldByName('QtyUnit').Value:=trim(CDS_WXPT.fieldbyname('QtyUnit').Asstring);
                CDS_WXPTPrint.FieldByName('CRTime').Value:=CDS_WXPT.fieldbyname('CRTime').asdatetime;
                FieldByName('BaoNO').Value:=CDS_WXPT.fieldbyname('BaoNO').Value;
                CDS_WXPTPrint.FieldByName('PRTColor').Value:=CDS_WXPT.fieldbyname('PRTColor').Value;
                FieldByName('LenQty'+Trim(IntToStr(Z))).Value:=CDS_WXPT.fieldbyname('qty').AsFloat;
                FieldByName('Zs'+Trim(IntToStr(Z))).Value:=CDS_WXPT.fieldbyname('MJQty2').AsFloat;
                CDS_WXPTPrint.FieldByName('RollNo'+Trim(IntToStr(Z))).Value:=CDS_WXPT.fieldbyname('RollNo').Value;
                CDS_WXPTPrint.FieldByName('MKGQty'+Trim(IntToStr(Z))).Value:=CDS_WXPT.fieldbyname('KGQty').AsFloat;
                CDS_WXPTPrint.FieldByName('KGQty'+Trim(IntToStr(Z))).Value:=CDS_WXPT.fieldbyname('MJqty4').AsFloat;
                CDS_WXPTPrint.FieldByName('Sumlenqty').Value:=CDS_WXPT.fieldbyname('Sumlenqty').AsFloat;
                CDS_WXPTPrint.FieldByName('sumroll').Value:=CDS_WXPT.fieldbyname('sumroll').AsFloat;
                CDS_WXPTPrint.FieldByName('SumRollGanNo').Value:=CDS_WXPT.fieldbyname('SumRollGanNo').AsFloat;
                CDS_WXPTPrint.FieldByName('SumRollColor').Value:=CDS_WXPT.fieldbyname('SumRollColor').AsFloat;
                CDS_WXPTPrint.FieldByName('SumZJ').Value:=CDS_WXPT.fieldbyname('SumZJ').AsFloat;
                FieldByName('SubId').Value:=CDS_WXPT.fieldbyname('SubId').Value;
                FieldByName('MainId').Value:=CDS_WXPT.fieldbyname('MainId').Value;
                Post;
              end;
            end
            else
            begin
              CDS_WXPTPrint.Append;
              CDS_WXPTPrint.FieldByName('MZQty').Value:=CDS_WXPT.fieldbyname('KGQty').Value;
              CDS_WXPTPrint.FieldByName('INVNO').Value:=CDS_WXPT.fieldbyname('orderno').Value;
              CDS_WXPTPrint.FieldByName('GangNo').Value:=CDS_WXPT.fieldbyname('GangNo').Value;
              CDS_WXPTPrint.FieldByName('BaoNO').Value:=CDS_WXPT.fieldbyname('BaoNO').Value;
              CDS_WXPTPrint.FieldByName('PONO').Value:=CDS_WXPT.fieldbyname('PONO').Value;
              CDS_WXPTPrint.FieldByName('QtyUnit').Value:=trim(CDS_WXPT.fieldbyname('QtyUnit').Asstring);
              CDS_WXPTPrint.FieldByName('CRTime').Value:=CDS_WXPT.fieldbyname('CRTime').asdatetime;
              CDS_WXPTPrint.FieldByName('PRTColor').Value:=CDS_WXPT.fieldbyname('PRTColor').Value;
              CDS_WXPTPrint.FieldByName('XH').Value:=J+1;
              CDS_WXPTPrint.FieldByName('LenQty0').Value:=CDS_WXPT.fieldbyname('qty').AsFloat;
              CDS_WXPTPrint.FieldByName('ZS0').Value:=CDS_WXPT.fieldbyname('MJQty2').AsFloat;
              CDS_WXPTPrint.FieldByName('RollNo0').Value:=CDS_WXPT.fieldbyname('RollNo').Value;
              CDS_WXPTPrint.FieldByName('MKGQty0').Value:=CDS_WXPT.fieldbyname('kgQty').AsFloat;
              CDS_WXPTPrint.FieldByName('KGQty0').Value:=CDS_WXPT.fieldbyname('MJqty4').AsFloat;
              CDS_WXPTPrint.FieldByName('Sumlenqty').Value:=CDS_WXPT.fieldbyname('Sumlenqty').AsFloat;
              CDS_WXPTPrint.FieldByName('sumroll').Value:=CDS_WXPT.fieldbyname('sumroll').AsFloat;
              CDS_WXPTPrint.FieldByName('SumRollGanNo').Value:=CDS_WXPT.fieldbyname('SumRollGanNo').AsFloat;
              CDS_WXPTPrint.FieldByName('SumRollColor').Value:=CDS_WXPT.fieldbyname('SumRollColor').AsFloat;
              CDS_WXPTPrint.FieldByName('SumZJ').Value:=CDS_WXPT.fieldbyname('SumZJ').AsFloat;
              CDS_WXPTPrint.FieldByName('SubId').Value:=CDS_WXPT.fieldbyname('SubId').Value;
              CDS_WXPTPrint.FieldByName('MainId').Value:=CDS_WXPT.fieldbyname('MainId').Value;
              CDS_WXPTPrint.Post;
              Z:=0;
              j:=j+1;
            end;
          end;
        end
      end;
      Z:=Z+1;
      Next;
    end;
  end;
  CDS_WXPT.EnableControls;
  CDS_WXPTPrint.EnableControls;
end;


procedure TfrmCKProductBCPOutList.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPOutList:=nil;
end;


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

procedure TfrmCKProductBCPOutList.FormCreate(Sender: TObject);
begin
  BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCKProductBCPOutList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,B.customerNoName,B.OrdPerson2,B.OrderNo,B.conNO,B.MPRTCodeName,B.MPRTSpec,B.MprtkuanNo,C.PrtkuanNo,C.PRTColor,C.SOrddefstr4,B.MPRTMF,B.MPRTKZ,D.MJXH,C.PRTPrice,C.PRTHX ');
      sql.Add(',C.SOrddefstr4,D.MJQty3,D.MJQty4');
      sql.Add(',khconNo=(select Top 1 KHConNo from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
      sql.Add(',MPRTECodeName=(select Top 1 MPRTCodeName from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
      sql.Add(',isnull(customerNoName,B.OrderNo) KHName');
      sql.Add(',D.mjstr4 as AOrdDefStr1,cast(D.MJXH as varchar) as MJXHM ');
      sql.Add(',RKNO=(select Top 1 rkOrdID from CK_BanCP_CR X where X.MJID=A.MJID and X.CRflag=''入库'')');
      sql.add('from CK_BanCP_CR A ');
      Sql.add(' inner join JYOrder_Main B on A.MainId=B.MainId');
      Sql.add(' inner join JYOrder_Sub C on A.SubId=C.SubId');
      Sql.add(' inner join WFB_MJJY D on A.MJId=D.MJId');
    //  sql.Add(' inner join JYOrder_Sub_AnPai E on A.APID=E.APID');
      sql.add('where A.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+' '+FormatDateTime('HH:mm',BegTime.Time))+'''');
      sql.Add(' and A.FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime)+' '+FormatDateTime('HH:mm',endTime.Time))+'''');
      SQL.Add(' and A.CRType=''正常出库'' ');
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmCKProductBCPOutList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmCKProductBCPOutList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  end;
end;

procedure TfrmCKProductBCPOutList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('仓库出库列表',Tv1,'成品仓库');
  WriteCxGrid('仓库出库列表2',Tv3,'成品仓库');
  Close;
end;

procedure TfrmCKProductBCPOutList.FormShow(Sender: TObject);
begin
  ReadCxGrid('仓库出库列表',Tv1,'成品仓库');
  ReadCxGrid('仓库出库列表2',Tv3,'成品仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Cast('''' as varchar(20)) MainId,Cast('''' as varchar(80)) KHName,Cast(0 as decimal(18,4)) PRTPrice ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_CX);
  SInitCDSData20(ADOQueryTemp,CDS_CX);
  CDS_CX.Delete;
  //InitGrid();
  if Trim(DParameters2)='管理' then
  begin
    TBCKCX.Visible:=True;
    ToolButton1.Visible:=true;
  end;
  if Trim(DParameters2)='业务员' then
  begin
    OrdPerson2.Text:=Dname;
    OrdPerson2.Enabled:=false;
  end;
end;

procedure TfrmCKProductBCPOutList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  SelExportData(Tv1,ADOQueryMain,'');
end;

procedure TfrmCKProductBCPOutList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCKProductBCPOutList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutList.TBPrintClick(Sender: TObject);
var
  fPrintFile,fPrintFile10,FMainID:String;
begin
  if CDS_Main.IsEmpty then Exit;
 {
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据','提示',0);
    Exit;
  end;

  CDS_Main.Locate('SSel',True,[]);          }
  IF pos('出库码单',trim(ComboBox1.Text))>0 then
  begin
    if CDS_Main.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择数据','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT * FROM TBSubID where 1=2 ');
      open;
    end;
    FMainID:='';
    CDS_Main.DisableControls;
    try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
      If Fieldbyname('Ssel').AsBoolean then
      begin
        IF FMainID='' then
        begin
          FMainID:=Trim(CDS_Main.fieldbyname('customerNoName').AsString);
        end
        else
        begin
          IF Trim(CDS_Main.fieldbyname('customerNoName').AsString)<>FMainID then
          begin
            application.MessageBox('选择的不是同一个客户的指示单，不能一起打印！','提示信息',0);
            ADOQueryCmd.Connection.RollbackTrans;
            EnableControls;
            exit;
          end;
        end;
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value:=Trim(CDS_Main.fieldbyname('MJID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value:=Trim(DCode);
          ADOQueryCmd.post;
      end;
      Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    except
    ADOQueryCmd.Connection.RollbackTrans;
    end;
    CDS_Main.EnableControls;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd20 ');
      sql.add('@mainID='+quotedstr(Trim(CDS_Main.fieldbyname('BCID').AsString)));
      sql.add(',@DName='+quotedstr(Trim(DCode)));
      sql.add(',@flag=''0'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_HZ);
    SInitCDSData20(ADOQueryTemp,CDS_HZ);


  end
  else
  begin
    with ADOQueryCmd do
    begin
    Close;
    sql.Clear;
    sql.Add(' select MPRTTYpe,istax from JYOrdercon_Main A  ');
    sql.Add(' inner join JYOrder_Main B on B.conNO=A.conNO ');
    sql.Add(' where B.MainId='+quotedstr(trim(CDS_Main.fieldbyname('MainID').AsString)));
    open;
    IF not IsEmpty then
    begin
      IF trim(fieldbyname('MPRTTYpe').AsString)='外销' then
      begin
        IF pos('谷甄',trim(ComboBox1.Text))<1 then
        begin
          Application.MessageBox('此合同属于外销合同，请选择谷甄码单！','提示',0);
          exit;
        end;
      end;
      IF trim(fieldbyname('MPRTTYpe').AsString)='内销' then
      begin
        IF fieldbyname('istax').AsBoolean then
        begin
          IF pos('谷甄',trim(ComboBox1.Text))<1 then
          begin
            Application.MessageBox('此合同属于内销含税合同，请选择谷甄码单！','提示',0);
            exit;
          end;
        end
        else
        begin
          IF pos('现金',trim(ComboBox1.Text))<1 then
          begin
            Application.MessageBox('此合同属于内销不含税合同，请选择现金码单！','提示',0);
            exit;
          end;
        end;
      end;
      IF trim(fieldbyname('MPRTTYpe').AsString)='加工' then
      begin
        IF fieldbyname('istax').AsBoolean then
        begin
          IF pos('常发',trim(ComboBox1.Text))<1 then
          begin
            Application.MessageBox('此合同属于加工含税合同，请选择常发码单！','提示',0);
            exit;
          end;
        end
        else
        begin
          IF pos('现金',trim(ComboBox1.Text))<1 then
          begin
            Application.MessageBox('此合同属于加工不含税合同，请选择现金码单！','提示',0);
            exit;
          end;
        end;
      end;
    end;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd10 ');
      sql.add('@mainID='+quotedstr(Trim(CDS_Main.fieldbyname('BCID').AsString)));
      sql.add(',@DName='+quotedstr(Trim(DCode)));
      sql.add(',@flag=''0'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_HZ);
    SInitCDSData20(ADOQueryTemp,CDS_HZ);
  end;

    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(ComboBox1.Text)+'.rmf' ;
    if FileExists(fPrintFile) then
    begin
      RM3.LoadFromFile(fPrintFile);
      RM3.ShowReport;
    end
    else
    begin
      Application.MessageBox(PChar('没有找'+fPrintFile),'提示',0);
    end;
end;



procedure TfrmCKProductBCPOutList.orderNoChange(Sender: TObject);
begin
  if Length(orderNo.Text)<4 then Exit;
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

procedure TfrmCKProductBCPOutList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,False);
end;

procedure TfrmCKProductBCPOutList.TBCKCXClick(Sender: TObject);
var
  FFMainId,FPrice:String;
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销数据吗？','提示',32+4)<>IDYES then Exit;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      //First;
      while CDS_Main.Locate('SSel',True,[])=True do
      begin
        IF trim(CDS_Main.Fieldbyname('Filler').AsString)<>trim(DName) then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('不能撤销其他人的数据！','提示',0);
          Exit;
        end;
        //if CDS_Main.FieldByName('SSel').AsBoolean=True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete CK_BanCP_CR where BCID='''+Trim(CDS_Main.fieldbyname('BCID').AsString)+'''');
            sql.Add('UPdate CK_BanCP_KC Set KCKgQty=(select KgQty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRType=''检验入库'') ');
            sql.Add(',KCQty=(select Qty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRType=''检验入库'') ');
            SQL.Add(' where CRID='+CDS_Main.fieldbyname('CRID').AsString);
            sql.Add('Update WFB_MJJY Set MJStr2=''已入库'' where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
            ExecSQL;
          end;
          if YSData(CDS_Main)=False then
          begin
            CDS_Main.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('撤销失败!','提示',0);
            Exit;
          end; 
          if Trim(FFMainId)='' then
          begin
            with CDS_CX do
            begin
              Append;
              FieldByName('Mainid').Value:=Trim(CDS_Main.fieldbyname('Mainid').AsString);
              //FieldByName('PRTPrice').Value:=CDS_Main.fieldbyname('PRTPrice').Value;
              FieldByName('KHName').Value:=Trim(CDS_Main.fieldbyname('KHName').AsString);
              Post;
            end;
            FFMainId:=Trim(CDS_Main.fieldbyname('Mainid').AsString);
            //FPrice:=Trim(CDS_Main.fieldbyname('PRTPrice').AsString);
          end else
          if Trim(FFMainId)<>Trim(CDS_Main.fieldbyname('Mainid').AsString) then
          begin
            with CDS_CX do
            begin
              Append;
              FieldByName('Mainid').Value:=Trim(CDS_Main.fieldbyname('Mainid').AsString);
              FieldByName('KHName').Value:=Trim(CDS_Main.fieldbyname('KHName').AsString);
              //FieldByName('PRTPrice').Value:=CDS_Main.fieldbyname('PRTPrice').Value;
              Post;
            end;
            FFMainId:=Trim(CDS_Main.fieldbyname('Mainid').AsString);
            //FPrice:=Trim(CDS_Main.fieldbyname('PRTPrice').AsString);
          end else
          begin
            {if Trim(FPrice)<>Trim(CDS_Main.fieldbyname('PRTPrice').AsString) then
            begin
              with CDS_CX do
              begin
                Append;
                FieldByName('Mainid').Value:=Trim(CDS_Main.fieldbyname('Mainid').AsString);
                FieldByName('KHName').Value:=Trim(CDS_Main.fieldbyname('KHName').AsString);
                FieldByName('PRTPrice').Value:=CDS_Main.fieldbyname('PRTPrice').Value;
                Post;
              end;
              FPrice:=Trim(CDS_Main.fieldbyname('PRTPrice').AsString);
            end; }
          end;
          CDS_Main.Delete;
        end;
      end;
    end;
    CDS_Main.EnableControls;
{    with CDS_CX do
    begin
      First;
      while not Eof do
      begin
        if YSData(CDS_CX)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('撤销失败!','提示',0);
          Exit;
        end;
        Next;
      end;
    end; }

    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible:=False;
    Exit;
  except
    MovePanel2.Visible:=False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！','提示',0);
  end;

end;
function TfrmCKProductBCPOutList.YSData(Order_Main10:TClientDataSet):Boolean;
var
  CRID,YFID,PriceUnit,OrderUnit,FComTaiTou:String;
  fprice:double;
begin
  Result:=False;
  with Order_Main10 do
  begin

    begin
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select Top 1 * from JYOrder_Sub Where mainID='''+Trim(Order_Main10.fieldbyname('mainID').AsString)+'''');
        Open;
      end;
      PriceUnit:=Trim(ADOQueryTemp.fieldbyname('PriceUnit').AsString);
      OrderUnit:=Trim(ADOQueryTemp.fieldbyname('OrderUnit').AsString);
      fprice:=ADOQueryTemp.fieldbyname('prtPrice').AsFloat;

      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main10.fieldbyname('KHName').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty=False then
      begin
        CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
      end else
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CRID set CRID=CRID+1');
          sql.Add('select * from YF_Money_CRID ');
          Open;
        end;
        CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YF_Money_KC where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('FactoryName').Value:=Trim(Order_Main10.fieldbyname('KHName').AsString);
          FieldByName('ZdyStr1').Value:='应收收';
          Post;
        end;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where FactoryName='''+Trim(Order_Main10.fieldbyname('KHName').AsString)+'''');
        sql.Add(' and maiNID='''+Trim(Order_Main10.fieldbyname('maiNID').AsString)+'''');
        sql.Add(' and CRTime='''+FormatDateTime('yyyy-MM-dd',Order_Main10.fieldbyname('CRTime').AsDateTime)+'''') ;
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd,YFID,'CS','YF_Money_CR',3,1)=False then
        begin
          Application.MessageBox('取成品应收最大号失败!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('YFID').Value:=Trim(YFID);
          FieldByName('YFTypeId').Value:=Trim(Order_Main10.fieldbyname('mainID').AsString);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRType').Value:='应收款登记';
          FieldByName('CRFlag').Value:='应收收';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('FactoryName').Value:=Trim(Order_Main10.fieldbyname('KHName').AsString);
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',data.Date));
          FieldByName('YFType').Value:='自动生成';
          FieldByName('Price').Value:=fprice;
          FieldByName('HuiLv').Value:=1;
          FieldByName('BZType').Value:=Trim(PriceUnit);
          FieldByName('QtyUnit').Value:=Trim(Order_Main10.fieldbyname('QtyUnit').AsString);
          FieldByName('ComTaiTou').Value:=Trim(FComTaiTou);
          FieldByName('YFName').Value:='销售金额';
          FieldByName('MainId').Value:=Trim(Order_Main10.fieldbyname('Mainid').AsString);
       //   FieldByName('subID').Value:=Trim(Order_Main10.fieldbyname('subID').AsString);
          Post;
        end;
      end  else
      begin
        YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(A.Qty),0) from CK_BanCP_CR A where A.MainID=YF_Money_CR.MainID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''正常出库'') ');
        sql.Add(',PS=(select isnull(count(A.mainID),0) from CK_BanCP_CR A where A.maiNID=YF_Money_CR.maiNID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''正常出库'')  ');
        sql.Add(' where mainID='''+Trim(Order_Main10.fieldbyname('mainID').AsString)+'''');
        sql.Add(' and CRTime='''+FormatDateTime('yyyy-MM-dd',Order_Main10.fieldbyname('CRTime').AsDateTime)+'''') ;
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where YFId='''+Trim(YFID)+'''');
        Open;
      end;
      if ADOQueryTemp.FieldByName('Qty').Value=0 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete from YF_Money_CR where YFID='''+Trim(YFID)+'''');
          ExecSQL;
        end;
      end;
      //Next;
    end;
  end;
  Result:=True;
end;

procedure TfrmCKProductBCPOutList.orderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(orderNo.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        SQL.Add('select A.*,B.customerNoName,B.OrdPerson2,B.OrderNo,B.conNO,B.MPRTCodeName,B.MPRTSpec,B.MprtkuanNo,C.PrtkuanNo,C.PRTColor,C.SOrddefstr4,B.MPRTMF,B.MPRTKZ,D.MJXH,C.PRTPrice,C.PRTHX ');
        sql.Add(',C.SOrddefstr4,D.MJQty3,D.MJQty4');
        sql.Add(',khconNo=(select Top 1 KHConNo from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
        sql.Add(',MPRTECodeName=(select Top 1 MPRTCodeName from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
        sql.Add(',isnull(customerNoName,B.OrderNo) KHName');
        sql.Add(',D.mjstr4 as AOrdDefStr1,cast(D.MJXH as varchar) as MJXHM ');
        sql.add('from CK_BanCP_CR A ');
        Sql.add(' inner join JYOrder_Main B on A.MainId=B.MainId');
        Sql.add(' inner join JYOrder_Sub C on A.SubId=C.SubId');
        Sql.add(' inner join WFB_MJJY D on A.MJId=D.MJId');
        sql.add('where B.OrderNo like '''+'%'+Trim(orderNo.Text)+'%'+'''');
        SQL.Add(' and CRType=''正常出库'' ');
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmCKProductBCPOutList.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Main.IsEmpty=False then
  begin
    InitOrderColor(Trim(CDS_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
    InitBCGangNo(Trim(CDS_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
  end;
end;

procedure TfrmCKProductBCPOutList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
  if CDS_Main.IsEmpty=False then
  begin
    //InitOrderColor(Trim(CDS_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
    InitBCGangNo(Trim(CDS_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
  end;
end;

procedure TfrmCKProductBCPOutList.AOrdDefStr1Change(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPOutList.CheckBox1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,CheckBox1.Checked);
end;

procedure TfrmCKProductBCPOutList.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  Panel4.Visible:=true;
  Panel4.Left:=FLeft;
  Panel4.Top:=FTop+110;
  Panel10.Caption:=Trim(TV1.Controller.FocusedColumn.Caption);
  RichEdit1.Text:=CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString;
  application.ProcessMessages;
end;

procedure TfrmCKProductBCPOutList.Tv1MouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft:=X;
  FTop:=Y;
end;

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

procedure TfrmCKProductBCPOutList.Panel2DblClick(Sender: TObject);
begin
  Panel2.Visible:=false;
end;

procedure TfrmCKProductBCPOutList.THMJIDKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(THMJID.Text)='' then
    begin
      Application.MessageBox('条码不能为空!','提示',0);
      Exit;
    end;

    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_BanCP_CR A ');
      sql.Add('where MJID='+quotedstr(trim(THMJID.Text)));
      sql.Add('and CRflag=''出库''');
      open;
    end;
    IF  ADOQueryTemp.IsEmpty then
    begin
      application.MessageBox('此卷未出库','提示信息',MB_ICONERROR);
      exit;
    end;

    try
       with ADOQueryCmd do
       begin
         Close;
         sql.Clear;
         sql.Add('delete CK_BanCP_CR where BCID='+quotedstr(ADOQueryTemp.fieldbyname('BCID').AsString));
         sql.Add('UPdate CK_BanCP_KC Set KCKgQty=(select KgQty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRType=''检验入库'') ');
         sql.Add(',KCQty=(select Qty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRType=''检验入库'') ');
         SQL.Add(' where CRID='+quotedstr(ADOQueryTemp.fieldbyname('CRID').AsString));
         sql.Add('Update WFB_MJJY Set MJStr2=''已入库'' where MJID='+quotedstr(ADOQueryTemp.fieldbyname('MJID').AsString));
         ExecSQL;
       end;
      IF CDS_Main.Locate('BCID',trim(ADOQueryTemp.fieldbyname('BCID').AsString),[]) then
      CDS_Main.Delete;
      THMJID.Text:='';
      THMJID.SetFocus;
      Exit;
    except
      THMJID.Text:='';
      THMJID.SetFocus;
      Application.MessageBox('撤销出库异常！','提示',0);
    end;
  end;
end;

procedure TfrmCKProductBCPOutList.ToolButton1Click(Sender: TObject);
begin
  Panel2.Visible:=true;
  THMJID.Text:='';
  THMJID.SetFocus;
end;

procedure TfrmCKProductBCPOutList.ToolButton3Click(Sender: TObject);
var
  fPrintFile:string;
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  with ADOqueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName='''+Trim(DCode)+'''');
    ExecSQL;
  end;
    with ADOqueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * FROM TBSubID where 1=2 ');
      open;
    end;
  ADOqueryCmd.Connection.BeginTrans;
  try
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('SSel').AsBoolean then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value:=Trim(fieldbyname('MJID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value:=Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOqueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  with ADOqueryPrint do
  begin
    close;
    sql.Clear;
    sql.Add(' exec P_Get_JYMJID_Main ');
    sql.Add(' @MJID='+quotedstr(''));
    sql.Add(',@flag=''0'' ');
    sql.Add(',@userID='+quotedstr(trim(DCode)));
    open;
  end;

  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\检验汇总信息.rmf';;
  if NOT FileExists(fPrintFile) then
  begin
    Application.MessageBox(PChar('没有找'+trim(fPrintFile)),'提示',MB_ICONERROR);
    exit;
  end;
  Rm2.LoadFromFile(fPrintFile);
  Rm2.ShowReport;
end;

procedure TfrmCKProductBCPOutList.ToolButton2Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  frmJYMJIDPRINT:=TfrmJYMJIDPRINT.create(self);
  with frmJYMJIDPRINT do
  begin
    fMJID:=trim(self.CDS_Main.fieldbyname('MJID').AsString);
    fflag:=1;
    show;
  end;
end;

end.
