unit U_JYOrderCDOne;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, MovePanel, cxTextEdit, cxContainer, cxCurrencyEdit,U_SMLB, BtnEdit,
  RM_e_main, RM_e_htm, RM_e_Graphic, RM_e_Jpeg, RM_e_txt, RM_e_csv,
  RM_E_llPDF, cxButtonEdit, cxPC;

type
  TfrmJYOrderCDOne = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    conNO: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    MJID: TEdit;
    Label2: TLabel;
    ZCDName: TEdit;
    Panel4: TPanel;
    Label11: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column1: TcxGridDBColumn;
    v1MJStr4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label4: TLabel;
    MJType: TComboBox;
    v1Column13: TcxGridDBColumn;
    Label5: TLabel;
    MPRTCodeName: TEdit;
    v1Column18: TcxGridDBColumn;
    Label12: TLabel;
    factoryName: TEdit;
    v1Column21: TcxGridDBColumn;
    Filler: TComboBox;
    TBManage: TToolButton;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBZF: TToolButton;
    ToolButton1: TToolButton;
    RM2: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    v1Column25: TcxGridDBColumn;
    TBAftChkHX: TToolButton;
    RadioGroup1: TRadioGroup;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    ToolButton2: TToolButton;
    CDS_CD: TClientDataSet;
    RMCD: TRMGridReport;
    RMDBCD: TRMDBDataSet;
    CDS_LS: TClientDataSet;
    CheckBox1: TCheckBox;
    ToolButton3: TToolButton;
    Panel2: TPanel;
    Label15: TLabel;
    cxCurrencyEdit1: TcxCurrencyEdit;
    Button1: TButton;
    Button2: TButton;
    v1MKXS: TcxGridDBColumn;
    CheckBox2: TCheckBox;
    MJStr4: TEdit;
    PrtColor: TEdit;
    Label18: TLabel;
    ComboBox1: TComboBox;
    RM3: TRMGridReport;
    v1Column31: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    v1Column33: TcxGridDBColumn;
    Panel3: TPanel;
    Check_bq: TCheckBox;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;
    Fbq: TBtnEditA;
    check_Fbz: TCheckBox;
    Button3: TButton;
    Button4: TButton;
    KHConNO: TEdit;
    Label19: TLabel;
    v1StyleNo: TcxGridDBColumn;
    StyleNo: TEdit;
    Label21: TLabel;
    Label22: TLabel;
    ISHZ: TComboBox;
    RMXLSExport1: TRMXLSExport;
    ISbao: TComboBox;
    Label23: TLabel;
    CDS_HZ: TClientDataSet;
    RMDBHZ: TRMDBDataSet;
    cxCurrencyEdit2: TcxCurrencyEdit;
    Label24: TLabel;
    TBBN: TToolButton;
    Label25: TLabel;
    ComboBox4: TComboBox;
    RMDBPRT: TRMDBDataSet;
    CDS_PRT: TClientDataSet;
    v1Column38: TcxGridDBColumn;
    PRTColorNo: TEdit;
    Label26: TLabel;
    prthx: TEdit;
    Label27: TLabel;
    Panel5: TPanel;
    Label28: TLabel;
    ComboBox5: TComboBox;
    Button5: TButton;
    Button6: TButton;
    v1ConPerson2: TcxGridDBColumn;
    v1SumQty: TcxGridDBColumn;
    ComboBox6: TComboBox;
    ADOQuery1: TADOQuery;
    Print_HZ: TClientDataSet;
    CDS_COL: TClientDataSet;
    RMDBCOL: TRMDBDataSet;
    cxSplitter1: TcxSplitter;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    tv2CDType: TcxGridDBColumn;
    tv2CDWZ: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    Tv2CDQty: TcxGridDBColumn;
    Tv2CDReason: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource2: TDataSource;
    Order_MJ: TClientDataSet;
    ADOTmp: TADOQuery;
    Panel6: TPanel;
    Label14: TLabel;
    Button7: TButton;
    Button8: TButton;
    FactoryName1: TEdit;
    Label16: TLabel;
    v1ZCDName: TcxGridDBColumn;
    Label20: TLabel;
    SOrddefstr4: TEdit;
    cxTabControl1: TcxTabControl;
    v1MJFK: TcxGridDBColumn;
    v1MJSJKZ: TcxGridDBColumn;
    TFH: TToolButton;
    v1FHStatus: TcxGridDBColumn;
    Label6: TLabel;
    Label13: TLabel;
    Label29: TLabel;
    Panel7: TPanel;
    Label30: TLabel;
    Button9: TButton;
    Button10: TButton;
    FHStatus1: TComboBox;
    v1MJStr2: TcxGridDBColumn;
    TOrder: TToolButton;
    v1filler: TcxGridDBColumn;
    Label17: TLabel;
    FHStatus0: TComboBox;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image2Click(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBManageClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBZFClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure TBAftChkHXClick(Sender: TObject);
    procedure Aorddefstr1Change(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure PRTColorChange(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure TOrderClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure FbqBtnClick(Sender: TObject);
    procedure MJIDKeyPress(Sender: TObject; var Key: Char);
    procedure APIDKeyPress(Sender: TObject; var Key: Char);
    procedure ISbaoChange(Sender: TObject);
    procedure TBBNClick(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure TADDClick(Sender: TObject);
    procedure TUpdateClick(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure Button8Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure MJStr4KeyPress(Sender: TObject; var Key: Char);
    procedure MPRTCodeNameKeyPress(Sender: TObject; var Key: Char);
    procedure conNOKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TFHClick(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
  private
    DQdate:TDateTime;
    FLeft,FTop:Integer;
    procedure InitGrid();
    procedure InitGrid10();
    procedure InitForm();
    function  Savedata():Boolean;
    procedure GetCDSData();
    procedure GetBGData();
    function YSData():Boolean;
    { Private declarations }
  public
    FFInt:Integer;
    FAPID,FMJType,FFiller,FMJStr2:String;
    FBeg,FEnd,canshu1:string;
    { Public declarations }
  end;

var
  frmJYOrderCDOne: TfrmJYOrderCDOne;

implementation
uses
  U_DataLink,U_OrderInPut,U_Fun,U_MJEdit, U_CPDBAO, U_CPGangNo,
  U_JYMJIDPRINT, U_ZDYHelp, U_JYOrderCDOne_LR, U_ProductOrderListSel_cx,U_ProductOrderListSel;

{$R *.dfm}

function TfrmJYOrderCDOne.YSData():Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit,FComTaiTou:String;
begin
  Result:=False;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select Top 1 * from JYOrderCon_Sub Where Subid='''+Trim(Order_Main.fieldbyname('ConID').AsString)+'''');
        Open;
      end;
      PriceUnit:=Trim(ADOQueryTemp.fieldbyname('PriceUnit').AsString);
      OrderUnit:=Trim(ADOQueryTemp.fieldbyname('OrderUnit').AsString);
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select ComTaiTou from JYOrder_Main where Mainid='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        Open;
      end;
      FComTaiTou:=Trim(ADOQueryTemp.fieldbyname('ComTaiTou').AsString);
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(FactoryName1.Text)+'''');
        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(FactoryName1.Text);
          FieldByName('ZdyStr1').Value:='应收收';
          Post;
        end;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where FactoryName='''+Trim(FactoryName1.Text)+'''');
        sql.Add(' and YFTypeId='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd,YFID,'HX','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_Main.fieldbyname('MJID').AsString);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('status').Value:='0';
          FieldByName('CRType').Value:='应收款登记';
          FieldByName('CRFlag').Value:='应收收';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('FactoryName').Value:=Trim(FactoryName1.Text);
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',now));
          FieldByName('YFType').Value:='自动生成';
          //FieldByName('Price').Value:=Order_Main10.fieldbyname('PRTPrice').Value;
          FieldByName('HuiLv').Value:=1;
          FieldByName('BZType').Value:=Trim(PriceUnit);
          FieldByName('QtyUnit').Value:=Trim(OrderUnit);
          FieldByName('ComTaiTou').Value:=Trim(FComTaiTou);
          FieldByName('YFName').Value:='销售金额';
          FieldByName('MainId').Value:=Trim(Order_Main.fieldbyname('ConID').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.MJQty4),0) from WFB_MJJY A  ');
        sql.Add(' inner join JYOrdercon_Sub B on A.SubId=B.Conid where');
        SQL.Add(' A.MJID=YF_Money_CR.YFTypeId and CPType=''次品'' )');
        sql.Add(' where YFTypeId='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
        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;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where CRId='+CRID);
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete from YF_Money_KC where CRId='+CRID);
          ExecSQL;
        end;
      end;
      Next;
    end;
  end;
  Result:=True;
end;

procedure TfrmJYOrderCDOne.FormDestroy(Sender: TObject);
begin
  frmJYOrderCDOne:=nil;
end;

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

procedure TfrmJYOrderCDOne.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  DQdate:=SGetServerDate(ADOQueryTemp);
  canshu1:=trim(DParameters1);
end;

procedure TfrmJYOrderCDOne.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('检验报告JF',Tv1,'检验管理');
end;

procedure TfrmJYOrderCDOne.InitGrid();
var
  i:integer;
  j:Integer;
  fsj:string;
begin
  fsj:=' where A.FillTime>='''+trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''' '
       +'and A.FillTime<'''+trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+''' and A.WCStatus=''已完成'' ';
  if (FBeg<>'') and (FEnd<>'') then
  begin
    fsj:=fsj+' and A.MJType='''+trim(FMJType)+''' and A.Filler='''+trim(FFiller)+''' '
             +' and isnull(A.MJStr2,'''')='''+trim(FMJStr2)+''' and A.APID='''+trim(FAPID)+'''';
  end
  else
  begin
    if trim(canshu1)='业务员' then
    begin
      fsj:=fsj+' and CC.YWY='''+trim(Dname)+'''';
    end;
    if cxtabControl1.TabIndex=0 then
    begin
      fsj:=fsj+' and isnull(A.MJStr2,'''')=''未入库'' ';
    end;
    if cxtabControl1.TabIndex=1 then
    begin
      fsj:=fsj+' and isnull(A.MJStr2,'''')<>''未入库'' ';
    end;
  end;
  if Trim(DParameters2)='次品' then
  begin
    fsj:=fsj+' and isnull(A.MJType,'''')=''次品'' ';
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec P_JYOrder_HZFX20 :begdate,:enddate,:PState,:FFSql ');
      Parameters.ParamByName('begdate').Value:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime);
      Parameters.ParamByName('enddate').Value:=FormatDateTime('yyyy-MM-dd',enddate.DateTime+1);
      Parameters.ParamByName('PState').Value:=1;
      Parameters.ParamByName('FFSql').Value:=fsj;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally;
    ADOQueryMain.EnableControls;
    //TBFind.Click;
  end;
end;

procedure TfrmJYOrderCDOne.InitGrid10();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec P_JYOrder_HZFX :begdate,:enddate,:PState ');
      Parameters.ParamByName('begdate').Value:='2012-01-01';
      Parameters.ParamByName('enddate').Value:='2011-01-01';
      Parameters.ParamByName('PState').Value:=3;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmJYOrderCDOne.InitForm();
var
  fsj:String;
begin
  ReadCxGrid('检验报告JF',Tv1,'检验管理');
  fsj:='select distinct(Filler) name from WFB_MJJY   ';
  SInitComBoxBySql(ADOQueryCmd,Filler,False,fsj);
  if (FBeg<>'') and (FEnd<>'') then
  begin
    BegDate.DateTime:=strtodatetime(FBeg);
    EndDate.DateTime:=strtodatetime(FEnd);
    InitGrid();
  end
  else
  begin
    BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-1;
    EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  end;
  //InitGrid();
end;

procedure TfrmJYOrderCDOne.TBFindClick(Sender: TObject);
var
  fsj,fwhere:String;
begin
  if ADOQueryMain.Active=False then Exit;

  fwhere:=SGetFilters(Panel1,1,2);
  fsj:='';
  IF trim(fsj)<>'' then
  begin
    IF fwhere='' then fwhere:=fsj
    else fwhere:=fwhere+' and '+fsj;
  end;
  SDofilter(ADOQueryMain,fwhere);
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;


procedure TfrmJYOrderCDOne.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'检验报告');
end;

procedure TfrmJYOrderCDOne.TBRafreshClick(Sender: TObject);
begin
  OrderNo.SetFocus;
  {MovePanel2.Left:=(self.Width-MovePanel2.Width) div 2;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;}
  InitGrid();
  //MovePanel2.Visible:=False;
end;

procedure TfrmJYOrderCDOne.OrderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmJYOrderCDOne.FormShow(Sender: TObject);
begin
  if Trim(DParameters2)='管理' then
  begin
    TBManage.Visible:=True;
    TBZF.Visible:=True;
    TBBN.Visible:=true;
    ToolButton1.Visible:=true;
    v1Column22.Visible:=true;
    TOrder.Visible:=true;
    TBAftChkHX.Visible:=True;
  end
  else
  begin
    v1Column22.Visible:=False;
    TOrder.Visible:=false;
  end;
  if Trim(DParameters2)='次品' then
  begin
    MJType.ItemIndex:=MJType.Items.IndexOf('次品');
    MJType.Enabled:=false;
  end;
  InitForm();
  RMCD.CanExport:=true
end;

procedure TfrmJYOrderCDOne.Panel10MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND,$F012,0);
end;

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

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

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

procedure TfrmJYOrderCDOne.TBManageClick(Sender: TObject);
begin
  IF  Order_Main.IsEmpty then exit;
  {with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_BanCP_CR where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Order_Main.EnableControls;
    Application.MessageBox('已产生入库数据不能删除!','提示',0);
    Exit;
  end;}
  if Order_Main.FieldByName('MJStr2').AsString<>'未入库' then
  begin
    application.MessageBox('此卷布已扫描入库,不能编辑','提示');
    exit;
  end;
  try
    frmMJEdit:=TfrmMJEdit.Create(Application);
    with frmMJEdit do
    begin
      MJID.Text:=Trim(Self.Order_Main.fieldbyname('MJID').AsString);
      with ADOTmp do
      begin
        Close;
        sql.Clear;
        SQL.Add('select A.*,OrdUnit=(select isnull(B.LengUnit,E.OrderUnit) from JYorder_Main B inner join JYorder_Sub E on B.Mainid=E.Mainid ');
        sql.Add(' where E.Mainid=A.Mainid and E.Subid=A.Subid )');
        sql.Add(' from WFB_MJJY A');
        sql.Add(' where A.MJID='''+Trim(MJID.Text)+'''');
        Open;
      end;
      if ADOTmp.IsEmpty then
      begin
        MJID.Text:='';
        Label2.Caption:='';
        Label2.Visible:=False;
        Application.MessageBox('条码错误!','提示',0);
        Exit;
      end;
      FStyleNo:=Trim(Self.Order_Main.fieldbyname('StyleNo').AsString);
      FOrderUnit:=Trim(ADOTmp.fieldbyname('OrdUnit').AsString);
      FHX:=Trim(Self.Order_Main.fieldbyname('PRTHX').AsString);
      FSE:=Trim(Self.Order_Main.fieldbyname('SOrddefstr4').AsString);
      MJXH.Text:=Trim(ADOTmp.fieldbyname('MJXH').AsString);
      MJstr4.Text:=Trim(ADOTmp.fieldbyname('MJstr4').AsString);
      MJMaoZ.Text:=Trim(ADOTmp.fieldbyname('MJMaoZ').AsString);
      MJLen.Text:=Trim(ADOTmp.fieldbyname('MJLen').AsString);
      MJQty1.Text:=Trim(ADOTmp.fieldbyname('MJQty1').AsString);
      MJQty2.Text:=Trim(ADOTmp.fieldbyname('MJQty2').AsString);
      MJQty4.Text:=Trim(ADOTmp.fieldbyname('MJQty4').AsString);
      MJFK.Text:=Trim(ADOTmp.fieldbyname('MJFK').AsString);
      MJSJKZ.Text:=Trim(ADOTmp.fieldbyname('MJSJKZ').AsString);
      M.Caption:=Trim(Trim(ADOTmp.fieldbyname('MJTypeOther').AsString));
      Ma.Caption:=Trim(Trim(ADOTmp.fieldbyname('MJStr1').AsString));
      if Trim(m.Caption)='' then
      begin
        m.Caption:=Trim(ADOTmp.fieldbyname('AOrddefstr3').AsString);
      end;
      if Trim(Ma.Caption)='' then
      begin
        Ma.Caption:=Trim(ADOTmp.fieldbyname('AOrddefstr2').AsString);
      end;
      if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString))='正品' then
      begin
        RadioGroup1.ItemIndex:=0
      end else
      if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString))='次品' then
      begin
        RadioGroup1.ItemIndex:=1;
      end else
      if Trim(Trim(ADOTmp.fieldbyname('MJType').AsString))='留样' then
      begin
        RadioGroup1.ItemIndex:=2;
      end;
      with ADOTmp do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from WFB_MJJY_CD A  where A.MJID='''+Trim(MJID.Text)+'''');
        Open;
      end;
      Label2.Caption:=Trim(MJID.Text);
      Label2.Visible:=True;
      MJID.Text:='';
      SCreateCDS20(ADOTmp,Order_MJ);
      SInitCDSData20(ADOTmp,Order_MJ);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmMJEdit.Free;
  end;
end;

procedure TfrmJYOrderCDOne.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main,True);
end;

procedure TfrmJYOrderCDOne.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,False);
end;

procedure TfrmJYOrderCDOne.TBZFClick(Sender: TObject);
begin
  if Order_Main.Locate('ssel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要作废数据吗？作废后数据不能恢复。','提示',32+4)<>IDYES then  Exit;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_CR where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
          sql.Add(' and CRFlag=''入库''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('已产生入库数据不能删除!','提示',0);
          Exit;
        end else
        begin
          //begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('delete WFB_MJJY where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
              sql.Add('delete WFB_MJJY_CD where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
              sql.Add('delete CK_BanCP_CR where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+''''); 
              sql.Add('delete CK_BanCP_KC where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
              ExecSQL;
            end;
         // end;
        end;
        Order_Main.Delete;
      end else
        Next;
    end;
  end;
  Order_Main.EnableControls;

end;

procedure TfrmJYOrderCDOne.ToolButton1Click(Sender: TObject);
var
  fPrintFile:string;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryPrint do
        begin
          Close;
          SQL.Clear;
          sql.Add('select QtyUnit=A.MJTypeOther,B.orderNo,G.ConNo,GangNo=A.MJStr4,isnull(C.styleNo,F.StyleNo) StyleNo,G.KHConNo,G.ConLBName,G.ConNLBName');
          sql.Add(',B.MaiTouNote,B.MPRTCode,B.LBName,B.NLBName ');
          sql.Add(',MPRTCodeNameEng=ISNULL((select Top 1 F.note from KH_Zdy F where F.zdyname=B.MPRTCodeName and F.Type=''PRTCodeName''),B.MPRTCodeName)');
          sql.Add(',B.MPRTCF,B.MPRTMF,B.MPRTKZ,isnull(C.PRTColorEng,F.SOrdDefStr4) OrdPRTColorEng,F.SOrddefstr4,B.MPRTCodeName,B.MPRTSpec,B.MPRTKuanNO,B.LengUnit,D.PiQty ');
          sql.Add(',Case when A.MJSJKZ*A.MJFK<>0 then Cast(A.MJQty4*1000/A.MJSJKZ/(A.MJFK/100) as int) else 0 end as MQty');
          sql.add(' ,substring(G.ConNo,LEN(G.ConNO)-3,LEN(G.ConNO)) HS,F.XHNo');
          SQL.ADD(' ,F.PRTMF,F.PRTKZ,F.PRTCodeName,F.PRTCode,G.CustomerNoName,C.*,A.*');
          sql.Add(' from WFB_MJJY 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(' left join KH_Zdy E on C.PRTColor=E.ZdyName and E.Type=''OrdColor'' ');
          sql.Add(' inner join CK_SXPB_CR D on D.SPId=A.APId ');
          sql.add(' left join JYOrderCon_Sub F on F.Subid=A.ConID');
          sql.add(' left join JYOrderCon_Main G on G.Mainid=F.Mainid');
          SQL.Add(' where A.MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
          Open;
        end;
        try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(ADOQueryPrint.fieldbyname('MJID').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;
        if Trim(ADOQueryPrint.fieldbyname('LBName').AsString)<>'' then
          fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('LBName').AsString)+'.rmf'
        else
          fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\通用英文标签.rmf' ;
        //  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\15029外唛.rmf' ;
        if FileExists(fPrintFile) then
        begin
            RMVariables['QRBARCODE']:=fImagePath;
            RMVariables['ID']:=trim(ADOQueryPrint.fieldbyname('HS').AsString)+Trim(ADOQueryPrint.fieldbyname('XHNo').AsString);
            RMVariables['FMPRTMF']:=StrToFloatDef(ADOQueryPrint.fieldbyname('MPRTMF').AsString,0)/100;
            RM2.LoadFromFile(fPrintFile);
            Rm2.DefaultCopies:=strtointdef(trim(ComboBox1.Text),1);
         //   RM2.ShowReport;
            RM2.PrintReport;
        {  IF Trim(ADOQueryPrint.fieldbyname('NLBName').AsString)<>'' then
          begin
             fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('NLBName').AsString)+'.rmf';
             if FileExists(fPrintFile) then
             begin
              RMVariables['QRBARCODE']:=fImagePath;
              RM3.LoadFromFile(fPrintFile);
              RM3.DefaultCopies:=1;
            //  RM3.ShowReport;
              RM3.PrintReport;
             end;
           end;  }
        end
        else
        begin
          Order_Main.EnableControls;
          Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\卷标签.rmf'),'提示',0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmJYOrderCDOne.RadioGroup1Click(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmJYOrderCDOne.TBAftChkHXClick(Sender: TObject);
var
  fsj,fsj10:String;
begin
  if order_Main.IsEmpty then exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        if Trim(fsj)='' then
        begin
          fsj:=Trim(Order_Main.fieldbyname('SubId').AsString);
        end
        else
        begin
          if Trim(fsj)<>Trim(Order_Main.fieldbyname('SubId').AsString) then
          begin
            Order_Main.EnableControls;
            Application.MessageBox('不同产品不能一起回修!','提示',0);
            Exit;
          end;
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        if Trim(fsj10)='' then
        begin
          fsj10:=Trim(Order_Main.fieldbyname('fromfactoryName').AsString);
        end
        else
        begin
          if Trim(fsj10)<>Trim(Order_Main.fieldbyname('fromfactoryName').AsString) then
          begin
            Order_Main.EnableControls;
            Application.MessageBox('不同染厂不能一起回修!','提示',0);
            Exit;
          end;
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQuery1 do
        begin
          close;
          sql.clear;
          sql.add('update WFB_MJJY set MJStr2=''已回修'',MJType=''次品'' ');
          sql.add('where MJID='''+trim(Order_Main.fieldbyname('MJID').AsString)+'''');
          execsql;
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;
  application.MessageBox('操作成功','提示');
  initgrid();
  FactoryName1.Text:=trim(Order_Main.fieldbyname('fromfactoryName').AsString);
  //Panel6.Visible:=true;
end;

function TfrmJYOrderCDOne.Savedata():Boolean;
var
  maxno,LLID:string;
  HXPS:Integer;
  HXQty,HXMQty:Double;
begin
  HXPS:=0;
  HXQty:=0;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 1 * from Contract_Cloth_LL where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    sql.Add(' and FirstName='''+Trim(Order_Main.fieldbyname('AOrddefstr4').AsString)+'''');
    Open;
  end;
  LLID:=Trim(ADOQueryTemp.fieldbyname('LLID').AsString);
  if Trim(LLID)='' then
  begin
    Result:=False;
    Application.MessageBox('没有相对应的加工厂!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxno,'HX','Contract_Cloth_BefChkHX',2,1)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取回修最大号失败!','提示',0);
      Exit;
    end;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean=True then
        begin
          HXPS:=HXPS+1;
          HXQty:=HXQty+Order_Main.FieldByName('MJLen').Value;
        end;
        Next;
      end;
    end;
  except
  end;
end;

procedure TfrmJYOrderCDOne.Aorddefstr1Change(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmJYOrderCDOne.OrderNoKeyPress(Sender: TObject; var Key: Char);
var fsj:string;
begin
  if Key=#13 then
  begin
    if Length(OrderNo.Text)<4 then Exit;
    {MovePanel2.Visible:=True;
    MovePanel2.Refresh;}
    fsj:=' where CC.OrderNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''';
    if canshu1='业务员' then
    begin
      fsj:=fsj+' and CC.YWY='''+trim(DName)+'''';
    end;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec P_JYOrder_HZFX20 :begdate,:enddate,:PState,:FFSql ');
        Parameters.ParamByName('begdate').Value:='2016-01-01';
        Parameters.ParamByName('enddate').Value:='2050-01-01';
        Parameters.ParamByName('PState').Value:=3;
        Parameters.ParamByName('FFSql').Value:=fsj;
        //ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
    //MovePanel2.Visible:=False;
  end;
end;

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

procedure TfrmJYOrderCDOne.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if Order_Main.IsEmpty then exit;
  with ADOTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from WFB_MJJY_CD A where A.MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOTmp,Order_MJ);
  SInitCDSData20(ADOTmp,Order_MJ);
end;

procedure TfrmJYOrderCDOne.GetCDSData();
begin
  CDS_CD.FieldByName('MJXH').Value:=CDS_LS.fieldbyname('MJXH').Value;
  CDS_CD.FieldByName('GangNo').Value:=CDS_LS.fieldbyname('Aorddefstr1').Value;
  CDS_CD.FieldByName('PRTColor').Value:=CDS_LS.fieldbyname('PRTColor').Value;
  CDS_CD.FieldByName('PRTEColor').Value:=CDS_LS.fieldbyname('SOrddefstr4').Value;
  CDS_CD.FieldByName('MJLen').Value:=CDS_LS.fieldbyname('MJLen').Value;
  CDS_CD.FieldByName('MJJZ').Value:=CDS_LS.fieldbyname('MJQty4').Value;
  CDS_CD.FieldByName('MJMZ').Value:=CDS_LS.fieldbyname('MJMaoZ').Value;
  CDS_CD.FieldByName('MJFK').Value:=CDS_LS.fieldbyname('MJFK').Value;
  CDS_CD.FieldByName('KFQtyHZ').Value:=CDS_LS.fieldbyname('KFQtyHZ').Value;
  CDS_CD.FieldByName('DengJi').Value:=CDS_LS.fieldbyname('DengJi').Value;
  CDS_CD.FieldByName('PanDing').Value:=CDS_LS.fieldbyname('PanDing').Value;
  CDS_CD.FieldByName('MJID').Value:=CDS_LS.fieldbyname('MJID').Value;
end;

procedure TfrmJYOrderCDOne.ToolButton2Click(Sender: TObject);
var
  fPrintFile,fZMFile:string;
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_JYMD1 ');
    sql.add('@mainID='+quotedstr(Trim(Order_Main.FieldByName('ConNO').AsString)));
    //sql.add(',@begtime='''+trim(formatdatetime('yyyy-MM-dd',begdate.DateTime))+''' ');
    //sql.add(',@Endtime='''+trim(formatdatetime('yyyy-MM-dd',Enddate.DateTime+1))+''' ');
    sql.Add(',@begtime=''2000-01-01''');
    sql.Add(',@Endtime=''2100-01-01''');
    sql.add(',@MJStr2='+quotedstr(Trim(Order_Main.FieldByName('MJStr2').AsString)));
    sql.Add(',@FHStatus='''+trim(Order_Main.FieldByName('FHStatus').AsString)+''' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_HZ);
  SInitCDSData20(ADOQueryTemp,CDS_HZ);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_JYMD1 ');
    sql.add('@mainID='+quotedstr(Trim(Order_Main.FieldByName('ConNO').AsString)));
    //sql.add(',@begtime='''+trim(formatdatetime('yyyy-MM-dd',begdate.DateTime))+'''');
    //sql.add(',@Endtime='''+trim(formatdatetime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
    sql.Add(',@begtime=''2000-01-01''');
    sql.Add(',@Endtime=''2100-01-01''');
    sql.add(',@MJStr2='+quotedstr(Trim(Order_Main.FieldByName('MJStr2').AsString)));
    sql.Add(',@FHStatus='''+trim(Order_Main.FieldByName('FHStatus').AsString)+''' ');
    sql.add(',@flag=''2'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_COL);
  SInitCDSData20(ADOQuery1,CDS_COL);
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(ComboBox6.Text)+'.rmf' ;
  if FileExists(fPrintFile) then
  begin
    if RMCD.CanExport=true then
    begin
      fZMFile:='C:\Users\Administrator\Desktop';
      if not DirectoryExists(fZMFile) then
      begin
        fZMFile:='C:\Documents and Settings\Administrator\桌面\'+trim(Order_Main.fieldbyname('ConNO').AsString)+' DETAILED PL.xls';
      end
      else
      begin
        fZMFile:='C:\Users\Administrator\Desktop\'+trim(Order_Main.fieldbyname('ConNO').AsString)+' DETAILED PL.XLS';
      end;
      RMCD.ExportTo(RMXLSExport1,fZMFile);
    end;
    RMCD.CanExport:=true;
    RMCD.LoadFromFile(fPrintFile);
    RMCD.ShowReport;
    RMCD.CanExport:=False;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检验报告.rmf'),'提示',0);
  end;
end;

procedure TfrmJYOrderCDOne.GetBGData();
var
  FMJId:string;
  i,j,z:Integer;
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select MJXH=Cast(0 as int),GangNo=Cast('''' as varchar(20)),PRTColor=Cast('''' as varchar(20)),MJID=Cast('''' as varchar(20))');
    sql.Add(',PRTEColor=Cast('''' as varchar(20)),MJLen=Cast(0 as decimal(18,2)),MJJZ=Cast(0 as decimal(18,2)),XH=Cast(0 as int)');
    sql.Add(',MJMZ=Cast(0 as decimal(18,2)),MJFK=Cast(0 as decimal(18,2)),KFQtyHZ=Cast(0 as int),DengJi=Cast('''' as varchar(20)),PanDing=Cast('''' as varchar(20))');
    sql.Add(',CDName0=Cast('''' as varchar(20)),CDBeg0=Cast('''' as varchar(20)),KFQty0=Cast(0 as int)');
    sql.Add(',CDName1=Cast('''' as varchar(20)),CDBeg1=Cast('''' as varchar(20)),KFQty1=Cast(0 as int)');
    sql.Add(',CDName2=Cast('''' as varchar(20)),CDBeg2=Cast('''' as varchar(20)),KFQty2=Cast(0 as int)');
    sql.Add(',CDName3=Cast('''' as varchar(20)),CDBeg3=Cast('''' as varchar(20)),KFQty3=Cast(0 as int)');
    sql.Add(',CDName4=Cast('''' as varchar(20)),CDBeg4=Cast('''' as varchar(20)),KFQty4=Cast(0 as int)');
    sql.Add(',CDName5=Cast('''' as varchar(20)),CDBeg5=Cast('''' as varchar(20)),KFQty5=Cast(0 as int)');
    sql.Add(',CDName6=Cast('''' as varchar(20)),CDBeg6=Cast('''' as varchar(20)),KFQty6=Cast(0 as int)');
    sql.Add(',CDName7=Cast('''' as varchar(20)),CDBeg7=Cast('''' as varchar(20)),KFQty7=Cast(0 as int)');
    sql.Add(',CDName8=Cast('''' as varchar(20)),CDBeg8=Cast('''' as varchar(20)),KFQty8=Cast(0 as int)');
    sql.Add(',CDName9=Cast('''' as varchar(20)),CDBeg9=Cast('''' as varchar(20)),KFQty9=Cast(0 as int)');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_CD);
  SInitCDSData20(ADOQueryTemp,CDS_CD);
  if CDS_CD.IsEmpty=False then CDS_CD.Delete;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*,Case when isnull(MJType,'''')=''正品'' and KFQtyHZ<26 then ''A''    ');
    sql.Add(' when isnull(MJType,'''')=''正品'' and KFQtyHZ>33 then ''C''');
    sql.Add(' when isnull(MJType,'''')=''正品'' and KFQtyHZ>25 and A.KFQtyHZ<34  then ''B''');
    sql.Add(' else MJType end as DengJi');
    sql.Add('from (select A.CDBeg,A.CDName,A.KouFenQty,B.MJXH,C.Aorddefstr1,JS.PRTColor,JS.SOrddefstr4,B.MJID ');
    sql.Add(',CDEName=(select Note from KH_Zdy KZ where KZ.ZdyName=A.CDName and KZ.Type=''WFBCD'' )');
    sql.Add(',B.MJLen,B.MJQty4,B.MJMaoZ,B.MJTypeOther,B.MJSJKZ,B.MJFK,B.MJType,B.PanDing');
    sql.Add(',KFQtyHZ=(select Sum(KouFenQty) from WFB_MJJY_CD MCD where MCD.MJID=B.MJID)');
    sql.Add(' from WFB_MJJY B inner join WFB_MJJY_CD A on B.MJID=A.MJID');
    sql.Add(' inner join JYOrder_Sub_AnPai C on B.APID=C.APID');
    sql.Add(' inner join JYOrder_Sub JS on C.SubId=JS.SubId');
    SQL.Add(' where B.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    if Trim(MJType.Text)<>'' then
    begin
      sql.Add(' and isnull(B.MJType,'''')='''+Trim(MJType.Text)+'''');
    end;

    sql.Add(')A order by PRTColor,Cast(Aorddefstr1 as int),MJID,MJXH');
    Open;
  end;
  //ShowMessage(ADOQueryTemp.SQL.Text);
  //ShowMessage(IntToStr(ADOQueryTemp.RecordCount));
  SCreateCDS20(ADOQueryTemp,CDS_LS);
  SInitCDSData20(ADOQueryTemp,CDS_LS);
  CDS_LS.DisableControls;
  with CDS_LS do
  begin
    First;
    while not Eof do
    begin
      if CDS_CD.IsEmpty then
      begin
        with CDS_CD do
        begin
          Append;
          GetCDSData();
          if CheckBox1.Checked=True then
            FieldByName('CDName0').Value:=CDS_LS.fieldbyname('CDEName').Value
          else
            FieldByName('CDName0').Value:=CDS_LS.fieldbyname('CDName').Value;
          FieldByName('CDBeg0').Value:=CDS_LS.fieldbyname('CDBeg').Value;
          FieldByName('KFQty0').Value:=CDS_LS.fieldbyname('KouFenQty').Value;
          FieldByName('XH').Value:=0;
          Post;
          FMJId:=Trim(CDS_LS.fieldbyname('MJID').AsString);
          i:=0;//列号
          j:=0;
          z:=0;//行号
        end;
      end else
      begin
        if Trim(CDS_LS.fieldbyname('MJID').AsString)=FMJId then
        begin
          i:=i+1;
          if i<10 then
          begin
            with CDS_CD do
            begin
              Edit;
              GetCDSData();
              if CheckBox1.Checked=True then
                FieldByName('CDName'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDEName').Value
              else
                FieldByName('CDName'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDName').Value;
              FieldByName('CDBeg'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDBeg').Value;
              FieldByName('KFQty'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('KouFenQty').Value;
              Post;
            end;
          end else
          begin
            i:=0;
            with CDS_CD do
            begin
              Append;
              GetCDSData();
              if CheckBox1.Checked=True then
                FieldByName('CDName'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDEName').Value
              else
                FieldByName('CDName'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDName').Value;
              FieldByName('CDBeg'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDBeg').Value;
              FieldByName('KFQty'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('KouFenQty').Value;
              Post;
            end;
          end;
        end else
        begin
          i:=0;
          with CDS_CD do
          begin
            Append;
            GetCDSData();
            if CheckBox1.Checked=True then
              FieldByName('CDName'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDEName').Value
            else
              FieldByName('CDName'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDName').Value;
            FieldByName('CDBeg'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('CDBeg').Value;
            FieldByName('KFQty'+Trim(IntToStr(i))).Value:=CDS_LS.fieldbyname('KouFenQty').Value;
            Post;
          end;
          FMJId:=Trim(CDS_LS.fieldbyname('MJID').AsString);
        end;
      end;
      Next;
    end;
  end;
  CDS_LS.EnableControls;
end;

procedure TfrmJYOrderCDOne.v1Column15PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with Order_Main do
  begin
    Edit;
    FieldByName('PanDing').Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate WFB_MJJY Set PanDing='''+Trim(mvalue)+'''');
    SQL.Add(' where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmJYOrderCDOne.Button2Click(Sender: TObject);
begin
  Panel2.Visible:=false;
end;

procedure TfrmJYOrderCDOne.Button1Click(Sender: TObject);
var
  i,J:string;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if trim(ComboBox4.Text)='' then
  begin
    Application.MessageBox('换算类型不能为空!','提示',0);
    Exit;
  end;
  i:=floattostr(strtofloatdef(trim(cxCurrencyEdit1.Text),0));
  J:= floattostr(strtofloatdef(trim(cxCurrencyEdit2.Text),0));
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          IF trim(ComboBox4.Text)='净重换算长度'then
          begin
            sql.Add('Update WFB_MJJY Set MKXs='''+trim(i)+''',MJQty3='''+trim(J)+''',MJlen=cast(1.00* MJQty4 *'''+trim(i)+''' as decimal(18,1))  ');
            sql.Add('where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
            sql.Add('Update WFB_MJJY Set MJMaoZ=MJQty4+MJQty3  ');
            sql.Add('where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
            sql.Add('Update CK_BanCP_CR Set MJlen=cast(1.00* MJQty4 *'''+trim(i)+''' as decimal(18,1)) ');
            sql.Add(',MJMaoZ=cast(1.00* MJQty4 +'''+trim(J)+''' as decimal(18,1)) ');
            sql.Add('where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
          end;
          ExecSQL;
        end;
      end;
      Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！','提示信息');
    Panel2.Visible:=false;
    TBRafresh.Click;
    exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！','提示信息',0);
  end;
end;

procedure TfrmJYOrderCDOne.ToolButton3Click(Sender: TObject);
begin
  Panel2.Left:=(self.Width-Panel2.Width) div 2;
  Panel2.Visible:=true;
end;

procedure TfrmJYOrderCDOne.CheckBox2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,CheckBox2.Checked);
end;

procedure TfrmJYOrderCDOne.TOrderClick(Sender: TObject);
begin
  frmProductOrderListSel:=tfrmProductOrderListSel.Create(SELF);
  with frmProductOrderListSel do
  begin
    if ShowModal=1 then
    begin
      with Self.Order_Main do
      begin
        First;
        while not Eof do
        begin
          if Self.Order_Main.FieldByName('SSel').AsBoolean=True then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update WFB_MJJY Set Subid='''+trim(frmProductOrderListSel.Order_Main.fieldbyname('Subid').AsString)+''' ');
              sql.Add(',Mainid='''+trim(frmProductOrderListSel.Order_Main.fieldbyname('Mainid').AsString)+''' ');
              sql.Add('where MJID='''+Trim(self.Order_Main.fieldbyname('MJID').AsString)+'''');
              ExecSQL;
            end;
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update CK_BanCP_CR Set Subid='''+trim(frmProductOrderListSel.Order_Main.fieldbyname('Subid').AsString)+''' ');
              sql.Add(',Mainid='''+trim(frmProductOrderListSel.Order_Main.fieldbyname('Mainid').AsString)+''' ');
              sql.Add('where MJID='''+Trim(self.Order_Main.fieldbyname('MJID').AsString)+'''');
              ExecSQL;
            end;
          end;
          Next;
        end;
      end;
    end;
    free;
  end;
end;

procedure TfrmJYOrderCDOne.ToolButton6Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean=True then
        begin
          IF trim(fieldbyname('mjstr2').AsString)<>'未入库' then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Order_Main.EnableControls;
            application.MessageBox('此条码已入库不能撤销打包!','提示信息',MB_ICONERROR);
            exit;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY Set baoID='''',baoNo=''''  ');
            sql.Add('where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据撤销成功！','提示信息');
    TBRafresh.Click;
    exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据撤销失败！','提示信息',0);
  end;
end;

procedure TfrmJYOrderCDOne.ToolButton7Click(Sender: TObject);
begin
  frmCPGangNo:=TfrmCPGangNo.Create(self);
  with frmCPGangNo do
  begin
    if showmodal=1 then
    begin
     // InitGrid();
    end;
  end;
  frmCPGangNo.Free;
end;

procedure TfrmJYOrderCDOne.ToolButton9Click(Sender: TObject);
begin
  frmSMLB:=TfrmSMLB.create(self);
  with frmSMLB do
  begin
    show;
  end;
end;

procedure TfrmJYOrderCDOne.Button4Click(Sender: TObject);
begin
  Panel3.Visible:=false;
end;

procedure TfrmJYOrderCDOne.Button3Click(Sender: TObject);
var fPrintFile: string;
    Txt,fImagePath:string;
    Moudle: THandle;
    Makebar:TMakebar;
    Mixtext:TMixtext;
    FBaoID:string;
begin
  if Order_Main.IsEmpty then Exit;
  if trim(Order_Main.FieldByName('BaoID').AsString)='' then
  begin
    application.MessageBox('未打包不能打印','提示');
    exit;
  end;

  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.Baoid,A.BaoNo,G.ConNO,G.KHConNO,F.StyleNo,A.MJTypeOther as QtyUnit,D.OrderNo,D.customerNoName,D.MprtCodeName,D.Mprtspec,D.OrdPerson1,D.MPRTKuanNO,D.LBName,D.NLBName,');
    SQL.ADD('C.SOrddefstr4,G.MPrtCode,G.MPRTSpec,G.ConPerson2,G.conDefstr2,G.conDefstr3,D.MaiTouNote,D.LengUnit,');
    sql.Add('MprtCodeNameEng=(select top 1 Note from KH_Zdy X where X.zdyName=D.MprtCodeName), ');
    SQL.ADD('count(A.MJID) as JSl,sum(A.MJMaoZ) MJMAOZ,sum(MJQty3) as MJQty3,sum(MJQty4) as MJQty4,SUM(A.MJLen)as MJLen');
    sql.add(' ,substring(G.ConNo,LEN(G.ConNO)-3,LEN(G.ConNO)) HS,F.XHNo');
    sql.Add('from WFB_MJJY A');
    sql.Add(' inner join JYOrder_Sub C on C.SubID=A.SubID');
    sql.Add(' inner join JYOrder_Main D on D.MainID=A.MainID');
    sql.add(' left join CK_SXPB_CR E on E.SPID=A.APID');
    sql.add(' inner join JYOrderCon_Sub F on F.Subid=E.ConCKID ');
    sql.add(' inner join JYOrderCon_Main G on G.Mainid=F.Mainid ');
    SQL.Add('where A.BaoID='''+Trim(Order_Main.fieldbyname('BaoID').AsString)+'''');
    SQL.ADD('group by A.Baoid,A.BaoNo,G.ConNO,G.KHConNO,F.StyleNo,A.MJTypeOther,D.OrderNo,D.customerNoName,D.MprtCodeName,D.Mprtspec,D.OrdPerson1,D.MPRTKuanNO,D.LBName,D.NLBName,');
    sql.add('C.SOrddefstr4,G.MPrtCode,G.MPRTSpec,G.ConPerson2,G.conDefstr2,G.conDefstr3,D.MaiTouNote,D.LengUnit,F.XHNo');
    Open;
  end;

  if ADOQueryPrint.RecordCount>1 then
  begin
    Application.MessageBox('打包错误，请重新打包!','提示',0);
    Exit;
  end;

  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=trim(ADOQueryPrint.fieldbyname('baoID').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;

  IF Check_bq.Checked then
  begin
    if Trim(ADOQueryPrint.fieldbyname('NLBName').AsString)<>'' then
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\'+Trim(ADOQueryPrint.fieldbyname('NLBName').AsString)+'.rmf' 
    else
    fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\包标签.rmf';

    if FileExists(fPrintFile) then
    begin
      RMVariables['QRBARCODE']:=fImagePath;
      RMVariables['ID']:=trim(ADOQueryPrint.fieldbyname('HS').AsString)+Trim(ADOQueryPrint.fieldbyname('XHNo').AsString);
      RM2.DefaultCopies:=strtointdef(trim(ComboBox2.Text),1);
      RM2.LoadFromFile(fPrintFile);
     //   RM2.ShowReport;
      RM2.printReport;
    end
    else
    begin
 //   Order_Main.EnableControls;
      Application.MessageBox(PChar('没有找'+fPrintFile),'提示',0);
      Exit;
    end;
  end;

  IF check_Fbz.Checked then
  begin
    fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(Fbq.Text)+'.rmf' ;

    if FileExists(fPrintFile) then
    begin
      RMVariables['QRBARCODE']:=fImagePath;
      RM2.DefaultCopies:=strtointdef(trim(ComboBox3.Text),1);
      RM2.LoadFromFile(fPrintFile);
   //   RM2.ShowReport;
      RM2.printReport;
    end
    else
    begin
 //   Order_Main.EnableControls;
      Application.MessageBox(PChar('没有找'+fPrintFile),'提示',0);
      Exit;
    end;
  end;

end;

procedure TfrmJYOrderCDOne.ToolButton5Click(Sender: TObject);
begin
  Panel3.Left:=(self.Width-Panel3.Width) div 2;
  Panel3.Visible:=true;
end;

procedure TfrmJYOrderCDOne.FbqBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='BAOFFLAG';
      flagname:='包副标签';
      if ShowModal=1 then
      begin
        Fbq.Text:=trim(ClientDataSet1.fieldbyname('zdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJYOrderCDOne.MJIDKeyPress(Sender: TObject; var Key: Char);
var fsj:string;
begin
  if Key=#13 then
  begin
    if Length(MJID.Text)<4 then Exit;
    {MovePanel2.Visible:=True;
    MovePanel2.Refresh;}
    fsj:=' where A.MJID like '''+'%'+Trim(MJID.Text)+'%'+'''';
    if canshu1='业务员' then
    begin
      fsj:=fsj+' and CC.YWY='''+trim(DName)+'''';
    end;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec P_JYOrder_HZFX20 :begdate,:enddate,:PState,:FFSql ');
        Parameters.ParamByName('begdate').Value:='2016-01-01';
        Parameters.ParamByName('enddate').Value:='2050-01-01';
        Parameters.ParamByName('PState').Value:=3;
        Parameters.ParamByName('FFSql').Value:=fsj;
        //ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
    //MovePanel2.Visible:=False;
  end;
end;

procedure TfrmJYOrderCDOne.APIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    {if Length(APID.Text)<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec P_JYOrder_HZFX20 :begdate,:enddate,:PState,:FFSql ');
        Parameters.ParamByName('begdate').Value:='2010-01-01';
        Parameters.ParamByName('enddate').Value:='2050-01-01';
        Parameters.ParamByName('PState').Value:=3;
        Parameters.ParamByName('FFSql').Value:=' where B.APID like '''+'%'+Trim(APID.Text)+'%'+'''';
        //ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
      if Order_Main.IsEmpty=False then
      begin
        //InitOrderColor(Trim(Order_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
       // InitBCGangNo(Trim(Order_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
      //  InitRCGangNo(Trim(Order_Main.fieldbyname('SubId').AsString),GangNo,ADOQueryTemp);
      end;
    finally
      ADOQueryMain.EnableControls;
    end;
    //MovePanel2.Visible:=False;}
  end;
end;

procedure TfrmJYOrderCDOne.ISbaoChange(Sender: TObject);
begin
  Tbfind.Click;
end;

procedure TfrmJYOrderCDOne.TBBNClick(Sender: TObject);
var
  fsubid:string;
begin
  IF  Order_Main.IsEmpty then exit;
  if Order_Main.Locate('ssel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  fsubid:='';
  frmProductOrderListSel_cx:=TfrmProductOrderListSel_cx.create(self);
  with frmProductOrderListSel_cx do
  begin
    if showmodal=1 then
    begin
      fsubid:= trim(Order_Main.fieldbyname('Subid').asstring);
    end;
    free;
  end;
  IF trim(fsubid)<>'' then
  begin
    ADOQueryCmd.Connection.BeginTrans;
    Try
      with Order_Main do
      begin
        DisableControls;
        First;
        while not eof do
        begin
          IF fieldbyname('ssel').AsBoolean then
          begin
            with ADOQueryCmd do
            begin
             Close;
             sql.Clear;
             sql.Add('UPdate WFB_MJJY Set ConID='''+Trim(FSubid)+''' ');
             sql.Add('where MJID='+quotedstr(trim(Order_Main.fieldbyName('MJID').AsString)));
             execsql;
            end;
            with ADOQueryCmd do
            begin
             Close;
             sql.Clear;
             sql.Add('UPdate CK_BanCP_CR Set ConID='''+Trim(FSubid)+''' ');
             sql.Add('where MJID='+quotedstr(trim(Order_Main.fieldbyName('MJID').AsString)));
             execsql;
            end;
          end;
          next;
        end;
        first;
        EnableControls;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      TBRafresh.Click;
    except
      Order_Main.EnableControls;
      ADOQueryCmd.Connection.RollbackTrans;
    end;
  end;
  Application.MessageBox('操作成功','提示');
end;
procedure TfrmJYOrderCDOne.Button6Click(Sender: TObject);
begin
  Panel5.Visible:=false;
end;

procedure TfrmJYOrderCDOne.Button5Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if trim(ComboBox5.Text)='' then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update WFB_MJJY Set MJTypeOther='''+trim(ComboBox5.Text)+''' ');
          sql.Add('where MJID='''+Trim(Order_Main.fieldbyname('MJID').AsString)+'''');
          ExecSQL;
        end;
      end;
      Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！','提示信息');
    Panel2.Visible:=false;
    TBRafresh.Click;
    exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！','提示信息',0);
  end;
end;

procedure TfrmJYOrderCDOne.ToolButton12Click(Sender: TObject);
begin
  Panel5.Visible:=true;
end;

procedure TfrmJYOrderCDOne.TADDClick(Sender: TObject);
begin
  frmJYOrderCDOne_LR:=TfrmJYOrderCDOne_LR.create(Self);
  with frmJYOrderCDOne_LR do
  begin
    FCRID:='';
    if showModal=1 then
    begin
      initGrid();
    end;
    free;
  end;
end;

procedure TfrmJYOrderCDOne.TUpdateClick(Sender: TObject);
begin
  if Order_Main.isempty then exit;
  frmJYOrderCDOne_LR:=TfrmJYOrderCDOne_LR.create(Self);
  with frmJYOrderCDOne_LR do
  begin
    FCRID:=trim(Order_Main.fieldbyname('CRID').AsString);
    if showModal=1 then
    begin
      initGrid();
    end;
    free;
  end;
end;

procedure TfrmJYOrderCDOne.Tv1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id,id1:Integer;
begin
  Id:=Tv1.GetColumnByFieldName('MJType').Index;
  Id1:=Tv1.GetColumnByFieldName('FHStatus').Index;
  if ARecord.Values[id]='次品' then
  begin
    AStyle:=DataLink_TradeManage.Red;
  end;
  if ARecord.Values[id1]='暂不发货' then
  begin
    AStyle:=DataLink_TradeManage.FontBlue;
  end;
end;

procedure TfrmJYOrderCDOne.Button8Click(Sender: TObject);
begin
  Panel6.Visible:=False;
end;

procedure TfrmJYOrderCDOne.Button7Click(Sender: TObject);
begin
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        if YSData()=False then
        begin
          Application.MessageBox('更新应收款失败!','提示',0);
          Exit;
        end;
      end;
      next;
    end;
  end;
end;

procedure TfrmJYOrderCDOne.MJStr4KeyPress(Sender: TObject;
  var Key: Char);
var fsj:string;
begin
  if Key=#13 then
  begin
    {MovePanel2.Visible:=True;
    MovePanel2.Refresh;}
    fsj:=' where A.MJStr4 like '''+'%'+Trim(MJStr4.Text)+'%'+'''';
    if canshu1='业务员' then
    begin
      fsj:=fsj+' and CC.YWY='''+trim(DName)+'''';
    end;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec P_JYOrder_HZFX20 :begdate,:enddate,:PState,:FFSql ');
        Parameters.ParamByName('begdate').Value:='2016-01-01';
        Parameters.ParamByName('enddate').Value:='2050-01-01';
        Parameters.ParamByName('PState').Value:=3;
        Parameters.ParamByName('FFSql').Value:=fsj;
        //ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
    //MovePanel2.Visible:=False;
  end;
end;

procedure TfrmJYOrderCDOne.MPRTCodeNameKeyPress(Sender: TObject;
  var Key: Char);
var fsj:string;
begin
  if Key=#13 then
  begin
    {MovePanel2.Visible:=True;
    MovePanel2.Refresh;}
    fsj:=' where CC.MPRTCodeName like '''+'%'+Trim(MPRTCodeName.Text)+'%'+'''';
    if canshu1='业务员' then
    begin
      fsj:=fsj+' and CC.YWY='''+trim(DName)+'''';
    end;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec P_JYOrder_HZFX20 :begdate,:enddate,:PState,:FFSql ');
        Parameters.ParamByName('begdate').Value:='2016-01-01';
        Parameters.ParamByName('enddate').Value:='2050-01-01';
        Parameters.ParamByName('PState').Value:=3;
        Parameters.ParamByName('FFSql').Value:=fsj;
        //ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
    //MovePanel2.Visible:=False;
  end;
end;

procedure TfrmJYOrderCDOne.conNOKeyPress(Sender: TObject; var Key: Char);
var fsj:string;
begin
  if Key=#13 then
  begin
    {MovePanel2.Visible:=True;
    MovePanel2.Refresh;}
    fsj:=' where EE.conNO like '''+'%'+Trim(conNO.Text)+'%'+'''';
    if canshu1='业务员' then
    begin
      fsj:=fsj+' and CC.YWY='''+trim(DName)+'''';
    end;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec P_JYOrder_HZFX20 :begdate,:enddate,:PState,:FFSql ');
        Parameters.ParamByName('begdate').Value:='2016-01-01';
        Parameters.ParamByName('enddate').Value:='2050-01-01';
        Parameters.ParamByName('PState').Value:=3;
        Parameters.ParamByName('FFSql').Value:=fsj;
        //ShowMessage(sql.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
    //MovePanel2.Visible:=False;
  end;
end;

procedure TfrmJYOrderCDOne.cxTabControl1Change(Sender: TObject);
begin
  TBManage.Visible:=false;
  TBBN.Visible:=false;
  TBAftchkHX.Visible:=false;
  Toolbutton3.Visible:=false;
  if cxtabcontrol1.TabIndex=0 then
  begin
    TBManage.Visible:=true;
    TBBN.Visible:=true;
    TBAftchkHX.Visible:=true;
    Toolbutton3.Visible:=true;
  end;
  initgrid();
end;

procedure TfrmJYOrderCDOne.TFHClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.Locate('Ssel',true,[])=False then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  Panel7.Visible:=true;
end;

procedure TfrmJYOrderCDOne.Button9Click(Sender: TObject);
begin
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQuery1 do
        begin
          close;
          sql.clear;
          sql.add('update WFB_MJJY set FHStatus='''+trim(FHStatus1.Text)+''' ');
          sql.add('where MJID='''+trim(Order_Main.fieldbyname('MJID').AsString)+'''');
          execsql;
        end;
      end;
      Next;
    end;
  end;
  Application.MessageBox('操作成功','提示');
end;

procedure TfrmJYOrderCDOne.Button10Click(Sender: TObject);
begin
  Panel7.Visible:=false;
end;

end.
