unit U_ProductOrder_Ph_other;

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, cxPC, cxContainer, cxTextEdit, cxCurrencyEdit;

type
  TfrmProductOrder_Ph_other = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: 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;
    v1OrderNo: TcxGridDBColumn;
    v1JGFactoryName: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    v1MPRTCF: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    Label4: TLabel;
    CustomerNoName: TEdit;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    Label2: TLabel;
    JGFactoryName: TEdit;
    Label5: TLabel;
    OrdPerson1: TEdit;
    Label6: TLabel;
    PRTCF: TEdit;
    Label7: TLabel;
    PRTColor: TEdit;
    Label8: TLabel;
    PRTCodeName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    v1Column8: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    v1OrdDefStr1: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label10: TLabel;
    PRTSpec: TEdit;
    Label11: TLabel;
    OrdDefStr1: TEdit;
    Label12: TLabel;
    PRTKZ: TEdit;
    Label13: TLabel;
    PRTMF: TEdit;
    ToolButton2: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton3: TToolButton;
    v1Column4: TcxGridDBColumn;
    ComboBox1: TComboBox;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1PRTRollNum: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    ToolBar2: TToolBar;
    TV2: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    del: TToolButton;
    V2Column1: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    V2Column5: TcxGridDBColumn;
    phwc: TToolButton;
    phok: TToolButton;
    fh: TToolButton;
    V2Column7: TcxGridDBColumn;
    V2Column8: TcxGridDBColumn;
    V2Column9: TcxGridDBColumn;
    ADO_PH: TADOQuery;
    DS_PH: TDataSource;
    CDS_PH: TClientDataSet;
    V2Column10: TcxGridDBColumn;
    Panel3: TPanel;
    Panel2: TPanel;
    Edit1: TEdit;
    Label14: TLabel;
    ToolButton4: TToolButton;
    v1PRTColor: TcxGridDBColumn;
    V2C_Color: TcxGridDBColumn;
    ToolButton5: TToolButton;
    TBPrint1: TToolButton;
    ADOPrint: TADOQuery;
    RMDB_MD: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    RMXLSExport2: TRMXLSExport;
    Label15: TLabel;
    CRdate: TDateTimePicker;
    V2Column6: TcxGridDBColumn;
    V2Column11: TcxGridDBColumn;
    ToolButton6: TToolButton;
    v1Ssel: TcxGridDBColumn;
    V2Column12: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    V2Filler: TcxGridDBColumn;
    V2PHYG: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    TBPrint2: TToolButton;
    Label16: TLabel;
    Label17: TLabel;
    fRoll1: TcxCurrencyEdit;
    fnum1: TcxCurrencyEdit;
    ToolButton7: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure phwcClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure phokClick(Sender: TObject);
    procedure delClick(Sender: TObject);
    procedure fhClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure TBPrint1Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure TBPrint2Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
  private
    DQdate:TDateTime;
    TblCprk:string;
    procedure InitGrid();
    procedure InitPH();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridFH();
    procedure Query_MJ(mjid:string);
    function SaveData():Boolean;
    function YSData(Order_Main10:TClientDataSet):Boolean;
    function SaveEjK(FNO,FID,fBCID,fCkNO:string):boolean;
    procedure CreateTable();
    procedure DropTable();
    { Private declarations }
  public
    fDParameters1:string;
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmProductOrder_Ph_other: TfrmProductOrder_Ph_other;

implementation
uses
  U_DataLink,U_OrderInPut,U_Fun,U_CKProductBCPKCListSel_other, U_CKSMEdit_other;

{$R *.dfm}
procedure TfrmProductOrder_Ph_other.DropTable();
var
  strSQL:string;
begin
 // TblCprk := '[##CP_INOut123' + trim(DCode) + IntToStr(Handle)+formatdatetime('hhnnsszzz',time) + ']';
	//创建临时表
  strSQL := 'IF OBJECT_ID(''tempdb..' + TblCprk + ''') IS NOT NULL DROP TABLE ' + TblCprk;
	with ADOQueryCmd do
	begin
		Close;
		SQL.Clear;
		SQL.Add(strSQL);
		ExecSQL;
	end;
end;
procedure TfrmProductOrder_Ph_other.CreateTable();
var
  strSQL:string;
begin
  TblCprk := '[##CP_PH' + trim(DCode) + IntToStr(Handle)+formatdatetime('hhnnsszzz',time) + ']';
	//创建临时表
  strSQL := 'IF OBJECT_ID(''tempdb..' + TblCprk + ''') IS NOT NULL DROP TABLE ' + TblCprk + #13
			    + 'CREATE TABLE ' + TblCprk + ' ('
			    + 'MainID VARCHAR(20),'
          + 'subID  VARCHAR(20),'
          + 'BCID   Varchar(20))'#13;
	with ADOQueryCmd do
	begin
		Close;
		SQL.Clear;
		SQL.Add(strSQL);
		ExecSQL;
	end;
end;
function TfrmProductOrder_Ph_other.SaveEjK(FNO,FID,fBCID,fCkNO:string):boolean;
var
  fkcID:integer;
  Filler:string;
  FillTime:TdateTime;
begin
  result:=false;
  try

    with ADOQueryCmd  do
    begin
      Close;
      sql.Clear;
      SQL.add('Update BP_Kcid Set kcid=kcid+1');
      sql.Add('select kcid  from BP_Kcid');
      Open;
    end;
    fkcID:=ADOQueryCmd.fieldbyname('kcid').AsInteger;

    with CDS_PH do
    begin
       with ADOQueryCmd do
       begin
          close;
          sql.Clear;
          sql.Add('select * from BP_InOut');
          sql.Add('where 1=2 ');
          open;
        end;
      //  fkcID:=strtoint(RightBStr(trim(FNO),length(FNO)-3)+trim(FID));
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('CRNO').Value:=trim(FNO);
        ADOQueryCmd.FieldByName('CRID').Value:=FID;
        ADOQueryCmd.FieldByName('CRTime').Value:=formatdateTime('yyyy-MM-dd',CRdate.Date);
        ADOQueryCmd.FieldByName('CKName').Value:='广东仓库';
        ADOQueryCmd.FieldByName('CRType').Value:='广东调拨入库';
        ADOQueryCmd.FieldByName('CRFlag').Value:='入库';
        ADOQueryCmd.FieldByName('mainID').Value:=trim(fieldbyname('mainID').AsString);
        ADOQueryCmd.FieldByName('subID').Value:=trim(fieldbyname('subID').AsString);
        ADOQueryCmd.FieldByName('MJID').Value:=trim(fieldbyname('MJID').AsString);
        ADOQueryCmd.FieldByName('MJXH').Value:=trim(fieldbyname('MJXH').AsString);
        ADOQueryCmd.FieldByName('CPTYPE').Value:=trim(fieldbyname('MJType').AsString);
        ADOQueryCmd.FieldByName('P_Code').Value:=trim(fieldbyname('C_Code').AsString);
        ADOQueryCmd.FieldByName('P_CodeName').Value:=trim(fieldbyname('C_CodeName').AsString);
        ADOQueryCmd.FieldByName('P_spec').Value:=trim(fieldbyname('C_spec').AsString);
        ADOQueryCmd.FieldByName('P_Color').Value:=trim(fieldbyname('C_Color').AsString);
     //   ADOQueryCmd.FieldByName('P_MF').Value:=fieldbyname('P_MF').AsFloat;
     //   ADOQueryCmd.FieldByName('P_KZ').Value:=fieldbyname('P_KZ').AsFloat;
        ADOQueryCmd.FieldByName('RollNum').Value:=fieldbyname('RollNum').AsFloat;
        ADOQueryCmd.FieldByName('Qty').Value:=fieldbyname('Qty').AsFloat;
        ADOQueryCmd.FieldByName('QtyUnit').Value:=trim(fieldbyname('QtyUnit').AsString);
        ADOQueryCmd.FieldByName('packNo').Value:=trim(fieldbyname('packNo').AsString);
        ADOQueryCmd.FieldByName('note').Value:='';
        ADOQueryCmd.FieldByName('Filler').Value:=trim(Filler);
        ADOQueryCmd.FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
        ADOQueryCmd.FieldByName('fromMainID').Value:=trim(fCkNO);
        ADOQueryCmd.FieldByName('FromSubID').Value:=trim(FbcID);
        ADOQueryCmd.FieldByName('kcID').Value:=fkcID;
        ADOQueryCmd.Post;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from BP_KC');
          sql.Add('where 1=2 ');
          open;
        end;
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('kcID').Value:=fkcID;
        ADOQueryCmd.FieldByName('CKName').Value:='广东仓库';
        ADOQueryCmd.FieldByName('CRType').Value:='广东调拨入库';
        ADOQueryCmd.FieldByName('CRNO').Value:=trim(fNo);
        ADOQueryCmd.FieldByName('CRID').Value:=fID;
        ADOQueryCmd.FieldByName('CRTime').Value:=formatdateTime('yyyy-MM-dd',CRdate.Date);
        ADOQueryCmd.FieldByName('CRFlag').Value:='入库';
        ADOQueryCmd.FieldByName('mainID').Value:=trim(fieldbyname('mainID').AsString);
        ADOQueryCmd.FieldByName('subID').Value:=trim(fieldbyname('subID').AsString);
        ADOQueryCmd.FieldByName('MJXH').Value:=trim(fieldbyname('MJXH').AsString);
        ADOQueryCmd.FieldByName('MJID').Value:=trim(fieldbyname('MJID').AsString);
        ADOQueryCmd.FieldByName('CPType').Value:=trim(fieldbyname('MJType').AsString);
        ADOQueryCmd.FieldByName('P_Code').Value:=trim(fieldbyname('C_Code').AsString);
        ADOQueryCmd.FieldByName('P_CodeName').Value:=trim(fieldbyname('C_CodeName').AsString);
        ADOQueryCmd.FieldByName('P_spec').Value:=trim(fieldbyname('C_spec').AsString);
        ADOQueryCmd.FieldByName('P_Color').Value:=trim(fieldbyname('C_Color').AsString);
    //    ADOQueryCmd.FieldByName('P_MF').Value:=fieldbyname('P_MF').AsFloat;
    //    ADOQueryCmd.FieldByName('P_KZ').Value:=fieldbyname('P_KZ').AsFloat;
        ADOQueryCmd.FieldByName('RollNum').Value:=fieldbyname('RollNum').AsFloat;
        ADOQueryCmd.FieldByName('Qty').Value:=fieldbyname('Qty').AsFloat;
        ADOQueryCmd.FieldByName('QtyUnit').Value:=trim(fieldbyname('QtyUnit').AsString);
        ADOQueryCmd.FieldByName('kcID').Value:=fkcID;
        ADOQueryCmd.Post;
      end;
    result:=true;
  except
  end;
end;
function TfrmProductOrder_Ph_other.YSData(Order_Main10:TClientDataSet):Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit,FComTaiTou,customName:String;
begin
  Result:=False;
  with Order_Main10 do
  begin

    with ADOQueryTemp do
    begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main10.fieldbyname('CustomerNoName').AsString)+'''');
        sql.Add(' and YFDefFlag1=2 ');
        Open;
    end;
    if not ADOQueryTemp.IsEmpty 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('CustomerNoName').AsString);
          FieldByName('ZdyStr1').Value:='应收收';
          FieldByName('YFDefFlag1').Value:=2;
          Post;
        end;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('select * from YF_Money_CR ');
      sql.Add(' where MainID='''+Trim(Order_Main10.fieldbyname('MainID').AsString)+'''');
      sql.Add(' and subID='''+Trim(Order_Main10.fieldbyname('subID').AsString)+'''');
      sql.Add(' and YFName=''销售金额'' ');
      Open;
    end;
      if ADOQueryTemp.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd,YFID,'CS','YF_Money_CR',4,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('CustomerNoName').AsString);
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',CRdate.Date));
          FieldByName('YFType').Value:='自动生成';
          FieldByName('Price').Value:=Order_Main10.fieldbyname('PRTPrice').Value;
          FieldByName('HuiLv').Value:=1;
          FieldByName('BZType').Value:='人民币';
          FieldByName('QtyUnit').Value:='M';
          FieldByName('ComTaiTou').Value:=Trim(Order_Main10.fieldbyname('CustomerNoName').AsString);
          FieldByName('YFName').Value:='销售金额';
          FieldByName('P_CodeName').Value:=Trim(Order_Main10.fieldbyname('PRTCodeName').AsString);
          FieldByName('P_Color').Value:=Trim(Order_Main10.fieldbyname('PRTColor').AsString);
          FieldByName('MainId').Value:=Trim(Order_Main10.fieldbyname('Mainid').AsString);
          FieldByName('subID').Value:=Trim(Order_Main10.fieldbyname('subID').AsString);
          FieldByName('SYRName').Value:=Trim(Order_Main10.fieldbyname('SYRName').AsString);
          FieldByName('YFDefFlag1').Value:=2;
          FieldByName('status').Value:='1';
        //  FieldByName('syrName').Value:='湖州门市部';
          Post;
        end;
      end
      else
      begin
        YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where YFID='''+trim(YFID)+'''');
          Open;
          edit;
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',CRdate.Date));
          post;
        end;
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(Qty),0) from CK_BanCP_CR1 A  ');
        sql.Add(' where A.mainID=YF_Money_CR.mainID and  A.subiD=YF_Money_CR.subiD and A.CRType=''配货出库''  )');
        sql.Add(',PS=(select isnull(count(*),0) from CK_BanCP_CR1 A  ');
        sql.Add(' where A.mainID=YF_Money_CR.mainID and  A.subiD=YF_Money_CR.subiD and A.CRType=''配货出库''  )');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        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 ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(' where CRID='+CRID);
        ExecSQL;
      end;
  end;
  Result:=True;
end;
function TfrmProductOrder_Ph_other.SaveData():Boolean;
var
  phID:string;
begin
  Result:=false;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from CK_BanCP_PH1 ');
      sql.Add('where mainID='+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('and subID='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
      execsql;
    end;

    with CDS_PH do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        if fieldbyname('qty').AsFloat=0 then continue;
        if GetLSNo(ADOQueryTemp,phID,'PH','CK_BanCP_PH1',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取配货单号失败！','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_PH1 ');
          sql.Add('where mainID='+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
          sql.Add('and subID='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
          open;
        end;
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('phID').Value:=trim(phID);
        ADOQueryCmd.FieldByName('conNo').Value:=trim(fieldbyname('conNo').AsString);
        ADOQueryCmd.FieldByName('MainID').Value:=trim(Order_Main.fieldbyname('MainID').AsString);
        ADOQueryCmd.FieldByName('SubID').Value:=trim(Order_Main.fieldbyname('SubID').AsString);
        ADOQueryCmd.FieldByName('PMainID').Value:=trim(fieldbyname('PMainID').AsString);
        ADOQueryCmd.FieldByName('PSubID').Value:=trim(fieldbyname('PSubID').AsString);
        ADOQueryCmd.FieldByName('mjID').Value:=trim(fieldbyname('mjID').AsString);
        ADOQueryCmd.FieldByName('CRID').Value:=trim(fieldbyname('CRID').AsString);
        ADOQueryCmd.FieldByName('qtyunit').Value:=trim(fieldbyname('kcqtyunit').AsString);
        ADOQueryCmd.FieldByName('qty').Value:=fieldbyname('qty').AsFloat;
        ADOQueryCmd.FieldByName('Rollnum').Value:=fieldbyname('Rollnum').AsFloat;
        ADOQueryCmd.FieldByName('Filler').Value:=trim(DName);
        ADOQueryCmd.FieldByName('FillTime').Value:=date();
        ADOQueryCmd.Post;
        with ADOQueryCmd do
        begin
        close;
        sql.Clear;
        sql.Add('select * from CK_BanCP_KC1 ');
        sql.Add('where mjid='+quotedstr(trim(CDS_PH.fieldbyname('mjid').AsString)));
        sql.Add('and CRID='+quotedstr(trim(CDS_PH.fieldbyname('CRID').AsString)));
        open;
        edit;
        fieldbyname('kcstatus').Value:='1';
        post;
        end;
        next;
      end;
      first;
      EnableControls;

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from JYOrder_sub ');
        sql.Add('where mainID='+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
        sql.Add('and subID='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
        open;
        edit;
        fieldbyname('substatus').Value:='2';
        post;
      end;
    end;
     ADOQueryCmd.Connection.CommitTrans;
     Result:=true;
  except
     ADOQueryCmd.Connection.RollbackTrans;
  end;
end;
procedure TfrmProductOrder_Ph_other.Query_MJ(mjid:string);
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,Rollnum=A.kcRollNum,Qty=A.kcQty,B.mjxh,D.mainID,D.subID,D.C_Code,D.C_CodeName,D.C_Spec,F.conNo     ');
    sql.Add('from CK_BanCP_KC1 A                                                              ');
    sql.Add('inner join WFB_MJJY B on B.mjid=A.mjid                              ');
    sql.Add('inner join Contract_Sub D on D.MainId=B.mainID and D.SubId=b.subID                   ');
    sql.Add('inner join Contract_main F on F.MainId=F.mainID              ');
    sql.Add('where A.mjID='+quotedstr(trim(mjid)));
    open;
    if IsEmpty then
    begin
      application.MessageBox('此卷号不存在!','提示信息',MB_ICONERROR);
      exit;
    end
    else if CDS_PH.Locate('mjid',fieldbyname('MjID').AsString,[]) then
    begin
      application.MessageBox('此卷号已加载!','提示信息',MB_ICONERROR);
      exit;
    end
    else
    begin
      CDS_PH.Append;
      CDS_PH.FieldByName('conNo').Value:=trim(fieldbyname('conNo').AsString);
      CDS_PH.FieldByName('mjid').Value:=trim(fieldbyname('mjid').AsString);
   //   CDS_PH.FieldByName('mjxh').Value:=trim(fieldbyname('mjxh').AsString);
      CDS_PH.FieldByName('C_CodeName').Value:=trim(fieldbyname('C_CodeName').AsString);
      CDS_PH.FieldByName('C_Spec').Value:=trim(fieldbyname('C_Spec').AsString);
      CDS_PH.FieldByName('PMainID').Value:=trim(fieldbyname('MainID').AsString);
      CDS_PH.FieldByName('PSubID').Value:=trim(fieldbyname('SubID').AsString);
      CDS_PH.FieldByName('kcqtyunit').Value:=trim(fieldbyname('kcqtyunit').AsString);
      CDS_PH.FieldByName('kcqty').Value:=fieldbyname('kcqty').AsFloat;
      CDS_PH.FieldByName('kcRollnum').Value:=fieldbyname('kcRollnum').AsFloat;
      CDS_PH.FieldByName('Qty').Value:=fieldbyname('kcqty').AsFloat;
      CDS_PH.FieldByName('RollNum').Value:=fieldbyname('kcRollnum').AsFloat;
      CDS_PH.FieldByName('CRID').Value:=fieldbyname('CRID').AsFloat;
      CDS_PH.Post;
    end;
  end;
end;
procedure TfrmProductOrder_Ph_other.InitPH();
begin
  if ADOQueryMain.Active=False then Exit;
  if Order_Main.IsEmpty THEN EXIT;
  try
    ADO_PH.DisableControls;
    with ADO_PH do
    begin
      Close;
      sql.Clear;
      Filtered:=False;
      sql.Add('select A.*,B.KCKGQty,B.KCQty,B.KCQtyUnit,B.kcRollNum,B.CPType,B.C_Code,C.kckw,B.C_CodeName,B.C_Spec,B.C_Color,B.RCGangNo,E.MJType,E.apID,B.mjxh     ');
      sql.Add('from CK_BanCP_PH1 A                                                              ');
      sql.Add('INNER join CK_BanCP_KC1 B on B.CRID=A.CRID                                               ');
      sql.Add('LEFT join CK_BanCP_CR1 C on C.CRID=B.CRID  and  CRFlag=''入库''               ');
      sql.Add('LEFT join WFB_MJJY E on A.MJID=E.MJID                   ');
      sql.Add('where A.mainID='+quotedstr(trim(Order_Main.fieldbyname('mainID').asstring)));
      sql.Add('and A.subID='+quotedstr(trim(Order_Main.fieldbyname('subID').asstring)));
      Open;
    end;
    SCreateCDS20(ADO_PH,CDS_PH);
    SInitCDSData20(ADO_PH,CDS_PH);
  finally
    ADO_PH.EnableControls;
  end;
end;


procedure TfrmProductOrder_Ph_other.FormDestroy(Sender: TObject);
begin
  frmProductOrder_Ph_other:=nil;
end;

procedure TfrmProductOrder_Ph_other.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin

  Action:=caFree;
end;

procedure TfrmProductOrder_Ph_other.FormCreate(Sender: TObject);
begin
  cxgrid2.Align:=alClient;
//  CreateTable();
  //BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-7;
  //EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  DQdate:=SGetServerDate(ADOQueryTemp);

  CRdate.DateTime:= DQdate;
  cxTabControl1.TabIndex:=0;
  del.Visible:=false;
  phwc.Visible:=false;
  phok.Visible:=false;
  fh.Visible:=false;
  Panel2.Visible:=false;
  ToolButton4.Visible:=false;
  TV2.OptionsData.Editing:=false;
  ToolButton5.Visible:=false;
  case  cxTabControl1.TabIndex of
    0:begin
        del.Visible:=true;
        phwc.Visible:=true;
        Panel2.Visible:=true;
        ToolButton4.Visible:=true;
        TV2.OptionsData.Editing:=true;
      end;
    1:begin
        phok.Visible:=true;
        ToolButton5.Visible:=true;
      end;
    2:begin
        fh.Visible:=true;
        ToolButton5.Visible:=true;
      end;
    3:begin
      end;
  end;
  application.ProcessMessages;
end;

procedure TfrmProductOrder_Ph_other.TBCloseClick(Sender: TObject);
begin
  Close;
  if FCloth<>1 then
    WriteCxGrid(self.Caption,Tv1,'生产指示单管理')
  else
    WriteCxGrid(self.Caption,Tv1,'生产指示单管理');
end;

procedure TfrmProductOrder_Ph_other.InitGrid();
var
  strwhere:string;
begin
  strwhere:='';

  if cxTabControl1.TabIndex=0 then
  begin
    strwhere:=strwhere+' and isnull(substatus,''0'')='''+inttostr(cxTabControl1.TabIndex)+'''';

    strwhere:=strwhere+' and isnull(OrderType,''0'')<>''剪样单'' ';
  end;
  if cxTabControl1.TabIndex>=1 then
  strwhere:=strwhere+' and isnull(substatus,''0'')='''+inttostr(cxTabControl1.TabIndex+1)+'''';


  strwhere:=strwhere+' and syrName=''龙运纺织'' ';

//  strwhere:=strwhere+' and CustomerNoName<>''湖州门市部'' ';

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.add('exec P_View_Order :begdate,:enddate,:WSql') ;
      Parameters.ParamByName('WSql').Value:=strwhere;
      Parameters.ParamByName('begdate').Value:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime);
      Parameters.ParamByName('enddate').Value:=FormatDateTime('yyyy-MM-dd',enddate.DateTime+1);
      ExecSQL;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    TBFind.Click;
  finally
    ADOQueryMain.EnableControls;

  end;
end;
procedure TfrmProductOrder_Ph_other.InitGridFH();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec Order_QryList :MainId,:WSql');
      Parameters.ParamByName('WSql').Value:=' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''''
                                           +' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrder_Ph_other.InitForm();
begin

  if FCloth<>1 then
    ReadCxGrid(self.Caption,Tv1,'生产指示单管理')
  else
    ReadCxGrid(self.Caption,Tv1,'生产指示单管理');

  if FCloth=1 then
  begin
    v1Column4.Visible:=True;

  end else
  begin
    v1Column4.Visible:=False;
  end;
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 10* from JYOrder_Main Order by FillTime desc');
    Open;
  end;
  ComboBox1.Clear;
  with ADOQueryTemp do
  begin
    First;
    while not Eof do
    begin
      ComboBox1.Items.Add(Trim(ADOQueryTemp.fieldbyname('OrderNO').AsString));
      Next;
    end;
  end;
  //InitGrid();
end;

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

procedure TfrmProductOrder_Ph_other.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;
  try
    frmOrderInPut:=TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FOrderNo:=Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
      if ShowModal=1 then
      begin

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

procedure TfrmProductOrder_Ph_other.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from Contract_Cloth_LL A where exists (select * from JYOrder_Sub B where B.SubId=A.OrdSubId  ');
    SQL.Add(' and B.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''')');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已产生数据不能删除!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已产生数据不能删除!','提示',0);
    Exit;
  end;
  {with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Cloth_LLMX where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已领料不能删除数据!','提示',0);
    Exit;
  end; }
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Sub_AnPai where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已回仓不能删除数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmProductOrder_Ph_other.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add('delete JYOrder_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      ExecSQL;
    end;
    {with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
      if IsEmpty then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;   }
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmProductOrder_Ph_other.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'生产指示单列表');
end;

procedure TfrmProductOrder_Ph_other.TBPrintClick(Sender: TObject);
var
   fPrintFile:string;
   Porderno,PRTCodeName,funit:string;
   i,j:Integer;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(DParameters1)<>'高权限' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.FieldByName('OrdFlag1').AsBoolean=True then
    begin
      Application.MessageBox('已打印过数据，不能再次打印!','提示',0);
      Exit;
    end;
  end;

  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf' ;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select PRTRollNum=Cast('''' as varchar(200)),PRTOrderQty=Cast('''' as varchar(200)),PRTCodeName,PRTOrderQty2=Cast('''' as varchar(200)),PRTCodeName,PRTOrderQty3=Cast('''' as varchar(200)),PRTCodeName, ');
    sql.add('PRTColor=Cast('''' as varchar(200))');
    sql.Add(' from  JYOrder_Sub ');
    sql.Add(' where 1<>1 ');
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  if CDS_Print.IsEmpty=False then CDS_Print.Delete;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select *,case when PRTRollNum<>0 then ''匹'' else OrderUnit end Unit ');
    sql.Add(' from  JYOrder_Sub ');
    sql.Add(' where  MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  ADOQueryPrint.DisableControls;
  CDS_Print.DisableControls;
  with ADOQueryPrint do
  begin
    First;
    while not Eof do
    begin
      if CDS_Print.IsEmpty then
      begin
        with CDS_Print do
        begin
          Append;
          FieldByName('PRTCodeName').Value:=ADOQueryPrint.fieldbyname('PRTCodeName').Value;
          FieldByName('PRTColor').Value:=ADOQueryPrint.fieldbyname('PRTColor').Value;
          FieldByName('PRTRollNum').Value:=Trim(IntToStr(ADOQueryPrint.fieldbyname('PRTRollNum').AsInteger));
       //   FieldByName('PRTOrderQty').Value:=Trim(FloatToStr(ADOQueryPrint.fieldbyname('PRTOrderQty').Value));
          if trim(ADOQueryPrint.FieldByName('unit').AsString) ='匹' then
          FieldByName('PRTOrderQty').Value:=trim(ADOQueryPrint.fieldbyname('PRTColor').AsString)+'/'+Trim(IntToStr(ADOQueryPrint.fieldbyname('PRTRollNum').AsInteger))
                                                 +trim(ADOQueryPrint.FieldByName('unit').AsString)
          else
          FieldByName('PRTOrderQty').Value:=trim(ADOQueryPrint.fieldbyname('PRTColor').AsString)+'/'+Trim(floattoStr(ADOQueryPrint.fieldbyname('PRTOrderQty').AsFloat))
                                                  +trim(ADOQueryPrint.FieldByName('unit').AsString);
          Post;
          i:=1;
          PRTCodeName:=Trim(ADOQueryPrint.fieldbyname('PRTCodeName').AsString);
          funit:=Trim(ADOQueryPrint.fieldbyname('Unit').AsString);
        end;
      end else
      begin
        if (Trim(ADOQueryPrint.fieldbyname('PRTCodeName').AsString)=PRTCodeName) and (Trim(ADOQueryPrint.fieldbyname('Unit').AsString)=funit) then
        begin
          if i<3 then
          begin
            with CDS_Print do
            begin
              Edit;
              FieldByName('PRTColor').Value:=Trim(FieldByName('PRTColor').AsString)+','
                                             +Trim(ADOQueryPrint.fieldbyname('PRTColor').AsString);
              FieldByName('PRTRollNum').Value:=Trim(FieldByName('PRTRollNum').AsString)+','+
                                               Trim(IntToStr(ADOQueryPrint.fieldbyname('PRTRollNum').AsInteger));
           //   FieldByName('PRTOrderQty').Value:=Trim(FieldByName('PRTOrderQty').AsString)+','+Trim(FloatToStr(ADOQueryPrint.fieldbyname('PRTOrderQty').Value));

              if i=1 then
              begin
              if trim(ADOQueryPrint.FieldByName('unit').AsString) ='匹' then
              FieldByName('PRTOrderQty2').Value:=trim(ADOQueryPrint.fieldbyname('PRTColor').AsString)+'/'+Trim(IntToStr(ADOQueryPrint.fieldbyname('PRTRollNum').AsInteger))
                                                 +trim(ADOQueryPrint.FieldByName('unit').AsString)
              else
              FieldByName('PRTOrderQty2').Value:=trim(ADOQueryPrint.fieldbyname('PRTColor').AsString)+'/'+Trim(floattoStr(ADOQueryPrint.fieldbyname('PRTOrderQty').AsFloat))
                                                  +trim(ADOQueryPrint.FieldByName('unit').AsString);
              end;
              if i=2 then
              begin
              if trim(ADOQueryPrint.FieldByName('unit').AsString) ='匹' then
              FieldByName('PRTOrderQty3').Value:=trim(ADOQueryPrint.fieldbyname('PRTColor').AsString)+'/'+Trim(IntToStr(ADOQueryPrint.fieldbyname('PRTRollNum').AsInteger))
                                                 +trim(ADOQueryPrint.FieldByName('unit').AsString)
              else
              FieldByName('PRTOrderQty3').Value:=trim(ADOQueryPrint.fieldbyname('PRTColor').AsString)+'/'+Trim(floattoStr(ADOQueryPrint.fieldbyname('PRTOrderQty').AsFloat))
                                               +trim(ADOQueryPrint.FieldByName('unit').AsString);
              end;
              Post;
            end;
            i:=i+1;
          end else
          begin
            with CDS_Print do
            begin
              Append;
              FieldByName('PRTCodeName').Value:=ADOQueryPrint.fieldbyname('PRTCodeName').Value;
              FieldByName('PRTColor').Value:=ADOQueryPrint.fieldbyname('PRTColor').Value;
              FieldByName('PRTRollNum').Value:=Trim(IntToStr(ADOQueryPrint.fieldbyname('PRTRollNum').AsInteger));
           //   FieldByName('PRTOrderQty').Value:=Trim(FloatToStr(ADOQueryPrint.fieldbyname('PRTOrderQty').Value));

              if trim(ADOQueryPrint.FieldByName('unit').AsString) ='匹' then
              FieldByName('PRTOrderQty').Value:=trim(ADOQueryPrint.fieldbyname('PRTColor').AsString)+'/'+Trim(IntToStr(ADOQueryPrint.fieldbyname('PRTRollNum').AsInteger))
                                                 +trim(ADOQueryPrint.FieldByName('unit').AsString)
              else
              FieldByName('PRTOrderQty').Value:=trim(ADOQueryPrint.fieldbyname('PRTColor').AsString)+'/'+Trim(floattoStr(ADOQueryPrint.fieldbyname('PRTOrderQty').AsFloat))
                                               +trim(ADOQueryPrint.FieldByName('unit').AsString);

              Post;
              i:=1;
              PRTCodeName:=Trim(ADOQueryPrint.fieldbyname('PRTCodeName').AsString);
              funit:=Trim(ADOQueryPrint.fieldbyname('Unit').AsString);
            end;
          end;
        end else
        begin
          with CDS_Print do
          begin
            Append;
            FieldByName('PRTCodeName').Value:=ADOQueryPrint.fieldbyname('PRTCodeName').Value;
            FieldByName('PRTColor').Value:=ADOQueryPrint.fieldbyname('PRTColor').Value;
            FieldByName('PRTRollNum').Value:=Trim(IntToStr(ADOQueryPrint.fieldbyname('PRTRollNum').AsInteger));
              if trim(ADOQueryPrint.FieldByName('unit').AsString) ='匹' then
              FieldByName('PRTOrderQty').Value:=trim(ADOQueryPrint.fieldbyname('PRTColor').AsString)+'/'+Trim(IntToStr(ADOQueryPrint.fieldbyname('PRTRollNum').AsInteger))
                                                 +trim(ADOQueryPrint.FieldByName('unit').AsString)
              else
              FieldByName('PRTOrderQty').Value:=trim(ADOQueryPrint.fieldbyname('PRTColor').AsString)+'/'+Trim(floattoStr(ADOQueryPrint.fieldbyname('PRTOrderQty').AsFloat))
                                                +trim(ADOQueryPrint.FieldByName('unit').AsString);

            Post;
            i:=1;
            PRTCodeName:=Trim(ADOQueryPrint.fieldbyname('PRTCodeName').AsString);
            funit:=Trim(ADOQueryPrint.fieldbyname('Unit').AsString);
          end;
        end;
      end;
      Next;
    end;
  end;
  ADOQueryPrint.EnableControls;
  CDS_Print.EnableControls;
  if FileExists(fPrintFile) then
  begin
    RMVariables['customerNoName']:=Trim(Order_Main.fieldbyname('customerNoName').AsString);
    RMVariables['OrderNo']:=Trim(Order_Main.fieldbyname('OrderNo').AsString);
    RMVariables['Note']:=Trim(Order_Main.fieldbyname('Note').AsString);
    RMVariables['FHAddress']:=Trim(Order_Main.fieldbyname('FHAddress').AsString);
    RMVariables['LXFS']:=Trim(Order_Main.fieldbyname('LXFS').AsString);
    RMVariables['OrdDate']:=Order_Main.fieldbyname('OrdDate').Value;
    RMVariables['OrdPerson1']:=Order_Main.fieldbyname('OrdPerson1').Value;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单.rmf'),'提示',0);
  end;
  if Trim(DParameters1)<>'高权限' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main Set OrdDefDate1=getdate(),OrdPerson1='''+Trim(DName)+''',OrdFlag1=1');
      sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      ExecSQL;
    end;

  end;

end;


procedure TfrmProductOrder_Ph_other.TBRafreshClick(Sender: TObject);
begin
  if FFInt=1 then
  begin
    InitGridFH();
  end else
  begin
    InitGrid();
 {   with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 10* from JYOrder_Main Order by FillTime desc');
      Open;
    end;
    ComboBox1.Clear;
    with ADOQueryTemp do
    begin
      First;
      while not Eof do
      begin
        ComboBox1.Items.Add(Trim(ADOQueryTemp.fieldbyname('OrderNO').AsString));
        Next;
      end;
    end; }
  end;
end;

procedure TfrmProductOrder_Ph_other.TBAddClick(Sender: TObject);
var
  maxno:string;
begin
  try
    frmOrderInPut:=TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState:=0;
      FMainId:='';
      if ShowModal=1 then
      begin

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

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

procedure TfrmProductOrder_Ph_other.FormShow(Sender: TObject);
begin
 InitForm();
end;

procedure TfrmProductOrder_Ph_other.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
 // if ToolButton1.Visible=False then Exit;
// ToolButton1.Click;

end;

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

procedure TfrmProductOrder_Ph_other.CheckBox1Click(Sender: TObject);
begin
  IF  Order_Main.IsEmpty then exit;
  with Order_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=checkbox1.Checked;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

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

procedure TfrmProductOrder_Ph_other.Tv1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id,id10:Integer;
begin
  {try
    if Tv1.GroupedItemCount=0 then
    begin
      Id:=Tv1.GetColumnByFieldName('DeliveryDate').Index-tv1.GroupedItemCount;
      Id10:=Tv1.GetColumnByFieldName('SubStatus').Index-tv1.GroupedItemCount;
      if Trim(VarToStr(ARecord.Values[id]))='' then Exit;
      if Id<0 then Exit;
      if ARecord.Values[id10]='完成' then exit;
      if (ARecord.Values[id]-DQdate)>=4 then Exit;
      if ((ARecord.Values[id]-DQdate)>=0) and ((ARecord.Values[id]-DQdate)<4) then
        AStyle:=DataLink_.QHuangSe
      else
      if ARecord.Values[id]-DQdate<0 then
      begin
        AStyle:=DataLink_OrderManage.FenHongS;
      end;
    end else
    begin

    end;
  except
  end; }
end;

procedure TfrmProductOrder_Ph_other.v1DeliveryDateCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { Id:=TV1.GetColumnByFieldName('DeliveryDate').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('SubStatus').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id10]='完成' then Exit;
  if AViewInfo.GridRecord.Values[Id]-SGetServerDate(ADOQueryTemp)>=4 then Exit;
  if ((AViewInfo.GridRecord.Values[id]-SGetServerDate10(ADOQueryTemp))>=0) and ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))<4) then
    ACanvas.Brush.Color:=clYellow
  else
  if (AViewInfo.GridRecord.Values[id])-(SGetServerDate10(ADOQueryTemp)<0) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='Purple' then
  begin
    ACanvas.Brush.Color:=clPurple;
  end else
  if AViewInfo.GridRecord.Values[Id]='Olive' then
  begin
    ACanvas.Brush.Color:=clOlive;
  end else
  if AViewInfo.GridRecord.Values[Id]='Teal' then
  begin
    ACanvas.Brush.Color:=clTeal;
  end else
  if AViewInfo.GridRecord.Values[Id]='Background' then
  begin
    ACanvas.Brush.Color:=clBackground;
  end;   }
end;

procedure TfrmProductOrder_Ph_other.N1Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单10.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单10.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmProductOrder_Ph_other.N2Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmProductOrder_Ph_other.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmOrderInPut:=TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Visible:=False;
      TBSave.Visible:=False;
      ScrollBox1.Enabled:=False;
      Tv1.OptionsSelection.CellSelect:=False;
      if ShowModal=1 then
      begin

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

procedure TfrmProductOrder_Ph_other.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmOrderInPut:=TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState:=1;
      CopyInt:=99;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      if ShowModal=1 then
      begin

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

procedure TfrmProductOrder_Ph_other.ToolButton3Click(Sender: TObject);
begin
  ModalResult:=1;
end;

procedure TfrmProductOrder_Ph_other.OrderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
{  if Key=#13 then
  begin
    if Length(OrderNoM.Text)<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.add('exec P_View_Order :begdate,:enddate,:WSql') ;
        if Trim(DParameters1)<>'高权限'  then
        begin
          Parameters.ParamByName('WSql').Value:=' and A.Filler='''+Trim(DName)+'''';
        end;
        begin
          Parameters.ParamByName('WSql').Value:=' and orderno like '''+'%'+Trim(OrderNoM.Text)+'%'+'''';
        end;
        Parameters.ParamByName('begdate').Value:='1899-01-01';
        Parameters.ParamByName('enddate').Value:='2050-01-01';
        ExecSQL;
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;   }
end;

procedure TfrmProductOrder_Ph_other.ConNoKeyPress(Sender: TObject;
  var Key: Char);
begin
{  if Key=#13 then
  begin
    if Length(conno.Text)<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.add('exec P_View_Order :begdate,:enddate,:WSql') ;
        if Trim(DParameters1)<>'高权限'  then
        begin
          Parameters.ParamByName('WSql').Value:=' and A.Filler='''+Trim(DName)+'''';
        end;
        begin
          Parameters.ParamByName('WSql').Value:=' and conno like '''+'%'+Trim(conno.Text)+'%'+'''';
        end;
        Parameters.ParamByName('begdate').Value:='1899-01-01';
        Parameters.ParamByName('enddate').Value:='2050-01-01';
        ExecSQL;
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;   }
end;

procedure TfrmProductOrder_Ph_other.Edit1KeyPress(Sender: TObject;
  var Key: Char);
begin
  if key=#13 then
  begin
    Query_MJ(Edit1.Text);
  end;
end;

procedure TfrmProductOrder_Ph_other.phwcClick(Sender: TObject);
var
  phRollNum,phQty:double;
begin
  if cds_ph.IsEmpty then exit;

  with CDS_PH do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      if (trim(fieldbyname('C_CodeName').AsString)<>trim(Order_Main.FieldByName('PRTCodeName').AsString))
        or (trim(fieldbyname('C_Color').AsString)<>trim(Order_Main.FieldByName('PRTColor').AsString)) then
      begin
        application.MessageBox('产品名称或者颜色不一样，不能配货！','提示信息',0);
        EnableControls;
        exit;
      end;
      next;
    end;
    first;
    EnableControls;
  end;
  phRollNum:=0;
  phQty:=0;
  with CDS_PH do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      phRollNum:=phRollNum+ CDS_PH.fieldbyname('RollNum').AsFloat;
      phQty:=phQty+ CDS_PH.fieldbyname('Qty').AsFloat;
      next;
    end;
    first;
    EnableControls;
  end;

  if (phRollNum<>Order_Main.FieldByName('PRTRollNum').AsFloat) or
  (phQty<>Order_Main.FieldByName('PRTOrderQty').AsFloat) then
  begin
    if Application.MessageBox('你的配货未达到配货要求，你确认这样配货吗？','提示信息',32+4)<>IDYES then Exit;
  end;

  if SaveData() then
  begin
    application.MessageBox('配货成功!','提示信息');
        TBRafresh.Click;
  end
  else
  begin
    application.MessageBox('配货失败!','提示信息',0);
  end;
end;

procedure TfrmProductOrder_Ph_other.cxTabControl1Change(Sender: TObject);
begin
  del.Visible:=false;
  phwc.Visible:=false;
  phok.Visible:=false;
  fh.Visible:=false;
  Panel2.Visible:=false;
  ToolButton4.Visible:=false;
  TV2.OptionsData.Editing:=false;
  ToolButton5.Visible:=false;
  TBPrint1.Visible:=false;
  TBPrint2.Visible:=false;
  ToolButton6.Visible:=false;
  case  cxTabControl1.TabIndex of
    0:begin
        del.Visible:=true;
        phwc.Visible:=true;
        Panel2.Visible:=true;
        ToolButton4.Visible:=true;
        TV2.OptionsData.Editing:=true;
      end;
    1:begin
        fh.Visible:=true;
        ToolButton5.Visible:=true;
        if fDParameters1='高权限' then
          ToolButton6.Visible:=true;
      end;
    2:begin
        TBPrint1.Visible:=true;
    //    TBPrint2.Visible:=true;
      end;
  end;
  application.ProcessMessages;
  initGrid();
end;

procedure TfrmProductOrder_Ph_other.phokClick(Sender: TObject);
begin
  if cds_ph.IsEmpty then exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from JYOrder_sub ');
      sql.Add('where mainID='+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('and subID='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
      open;
      edit;
      fieldbyname('substatus').Value:='2';
      post;
    end;
    application.MessageBox('配货确认成功！','提示信息');
    TBRafresh.Click;
  except
     application.MessageBox('配货确认失败！','提示信息',0);
  end;
end;

procedure TfrmProductOrder_Ph_other.delClick(Sender: TObject);
begin
  if cds_ph.IsEmpty then exit;
  with ADOQueryCmd do
  begin
      close;
      sql.Clear;
      sql.Add('delete from CK_BanCP_PH1 ');
      sql.Add('where PHID='+quotedstr(trim(cds_ph.fieldbyname('PHID').AsString)));
      execsql;
  end;
  cds_ph.Delete;
end;

procedure TfrmProductOrder_Ph_other.fhClick(Sender: TObject);
var
  CKOrdNo,maxno,rkNo:string;
  i,fRoll:integer;
  fNum:double;
begin
  if cds_ph.IsEmpty then exit;

  if Order_Main.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
{  fRoll:=0;
  fNum:=0;
  with Order_Main do
  begin
    First;
    DisableControls;
    while not eof do
    begin
      if FieldByName('ssel').AsBoolean then
      begin
        fRoll:=fRoll+ Fieldbyname('PH_RollNum').AsInteger;
        fNum:=fNum+ Fieldbyname('PH_Qty').AsFloat;
      end;
      next;
    end;
    First;
    EnableControls
  end;

  IF fRoll<>strtointdef(trim(froll1.Text),0) then
  begin
    application.MessageBox('配货匹数不正确，不能发货！','提示信息',0);
    exit;
  end;
  IF fnum<>strtofloatdef(trim(fnum1.Text),0) then
  begin
    application.MessageBox('配货数量不正确，不能发货！','提示信息',0);
    exit;
  end;  }

  ADOQueryCmd.Connection.BeginTrans;
  try
    Order_Main.First;
    Order_Main.DisableControls;
    CDS_ph.DisableControls;
    while not Order_Main.eof do
    begin
      IF Order_Main.FieldByName('ssel').AsBoolean then
      begin
        initPH();
          if GetLSNo(ADOQueryTemp,CKOrdNo,'CT','CK_BanCP_CR1',3,1)=False then
          begin
            Order_Main.EnableControls;
            CDS_ph.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取出库单号失败!','提示',0);
            Exit;
          end;
          IF trim(CKOrdNo)='' then
          begin
            Order_Main.EnableControls;
            CDS_ph.EnableControls;
            Application.MessageBox('获取出库单号发生错误!','提示',0);
            Exit;
          end;
        end;
        i:=1;
        with CDS_ph do
        begin
        First;
        while not eof  do
        begin
          if GetLSNo(ADOQueryTemp,maxno,'CH','CK_BanCP_CR1',4,1)=False then
          begin
            Order_Main.EnableControls;
            CDS_ph.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取出库最大号失败！','提示',0);
            Exit;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCp_CR1 where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('BCID').Value:=Trim(maxno);
            FieldByName('CRID').Value:=CDS_ph.fieldbyname('CRID').Value;
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',CRdate.DateTime);
            FieldByName('KGQty').Value:=CDS_ph.fieldbyname('KGQty').AsFloat;
            FieldByName('Qty').Value:=CDS_ph.fieldbyname('Qty').AsFloat;
            FieldByName('RollNum').Value:=CDS_ph.fieldbyname('RollNum').AsFloat;
            FieldByName('QtyUnit').Value:=CDS_ph.fieldbyname('QtyUnit').Value;
            FieldByName('MJID').Value:=CDS_ph.fieldbyname('MJID').Value;
            FieldByName('MainID').Value:=CDS_ph.fieldbyname('MainID').Value;
            FieldByName('SubID').Value:=CDS_ph.fieldbyname('SubID').Value;
            FieldByName('APID').Value:=CDS_ph.fieldbyname('apid').Value;
            FieldByName('MJID').Value:=CDS_ph.fieldbyname('MJID').Value;
            FieldByName('MJxh').Value:=CDS_ph.fieldbyname('MJxh').Value;
            FieldByName('CPType').Value:=CDS_ph.fieldbyname('MJType').Value;
            FieldByName('kckw').Value:=CDS_ph.fieldbyname('kckw').Value;

            FieldByName('C_Code').Value:=trim(CDS_ph.fieldbyname('C_Code').AsString);
            FieldByName('C_CodeName').Value:=trim(CDS_ph.fieldbyname('C_CodeName').AsString);
            FieldByName('C_spec').Value:=trim(CDS_ph.fieldbyname('C_spec').AsString);
            FieldByName('C_Color').Value:=trim(CDS_ph.fieldbyname('C_Color').AsString);
            FieldByName('BCGangNO').Value:=trim(CDS_ph.fieldbyname('BCGangNO').AsString);
            FieldByName('RCGangNO').Value:=trim(CDS_ph.fieldbyname('RCGangNO').AsString);
            FieldByName('packNo').Value:=trim(CDS_ph.fieldbyname('packNo').AsString);
            FieldByName('PHID').Value:=trim(CDS_ph.fieldbyname('PHID').AsString);
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('CRFlag').Value:='出库';
            FieldByName('CRType').Value:='配货出库';
            FieldByName('CPType').Value:=trim(CDS_ph.fieldbyname('CPType').AsString);
            FieldByName('CKOrdNo').Value:=Trim(CKOrdNo);
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCp_PH1 where PHID='+quotedstr(trim(CDS_ph.fieldbyname('PHID').AsString)));
            open;
            edit;
            fieldbyname('P_Status').Value:='1';
            post;
          end;
        i:=i+1;
        next;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPDATE CK_BanCp_KC1 SET kcstatus=null,KCKgQty =A.KCKgQty -B.KgQty,KCQty =A.KCQty -B.Qty,KCRollNum =A.KCRollNum -B.RollNum ');
        sql.Add('FROM  CK_BanCp_KC1 A ');
        sql.Add('INNER JOIN CK_BanCP_CR1 B on B.CRID=A.CRID ');
        sql.Add('WHERE B.CKOrdNo ='+Quotedstr(trim(CKOrdNo)));
        ExecSQL;
      end;


    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from JYOrder_sub ');
      sql.Add('where mainID='+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('and subID='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
      open;
      edit;
      fieldbyname('substatus').Value:='3';
      post;
     // end;
    end;
        if not YSData(Order_Main) then
        begin
         Order_Main.EnableControls;
         CDS_ph.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          application.MessageBox('发货失败！','提示信息');
          exit;
        end;
    end;
    Order_Main.Next;
  end;

   CDS_ph.EnableControls;
   Order_Main.EnableControls;
   ADOQueryCmd.Connection.CommitTrans;

   with ADOQueryCmd do
   begin
     Close;
     sql.Clear;
     sql.Add('update CK_BanCP_Kc1 SET KCQty=0,kcRollNum=0 where KCQty<0 ');
     sql.Add('UPDATE CK_BanCp_KC1 SET KCRollNum =1 WHERE (KCQty<>0 or KCKgQty<>0)and KcRollNum=0 ');
     ExecSQL;
   end;
  if Application.MessageBox('确定要打印出库单吗？','提示',32+4)=IDYES then
  begin
    TBPrint1.Click;
  end;
    TBRafresh.Click;
    CRdate.DateTime:= DQdate;
  except
    CDS_ph.EnableControls;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('发货失败！','提示信息');
  end;
end;

procedure TfrmProductOrder_Ph_other.ToolButton4Click(Sender: TObject);
begin
  if  Order_Main.IsEmpty then exit;
  frmCKProductBCPKCListsel_other:=TfrmCKProductBCPKCListsel_other.create(self);
  with frmCKProductBCPKCListsel_other do
  begin
    if Trim(DParameters1)<>'高权限' then
    begin
      v2RollNum.Visible:=false;
      v2RollNum.Options.Editing:=false;
      v2RollNum.Hidden:=true;
      v2Qty.Visible:=false;
      v2Qty.Options.Editing:=false;
      v2Qty.Hidden:=true;
    end;

    fmainID:=  trim(self.Order_Main.FieldByName('mainID').AsString);
 //   C_CodeName.Text:= trim(self.Order_Main.FieldByName('PRTCodeName').AsString);
  //  C_Color.Text:=  trim(self.Order_Main.FieldByName('PRTColor').AsString);
  //  FCodeName:= trim(self.Order_Main.FieldByName('PRTCodeName').AsString);
  //  fColor:=  trim(self.Order_Main.FieldByName('PRTColor').AsString);
    if showmodal=1 then
    begin
      {CDS_PH.DisableControls;
      CDS_Main.DisableControls;
      CDS_Main.first;
      while not CDS_Main.eof do
      begin
        if  (CDS_Main.FieldByName('ssel').AsBoolean) and
            (not CDS_PH.Locate('mjid',trim(CDS_Main.fieldbyname('mjid').AsString),[])) then
        begin
          CDS_PH.Append;
          CDS_PH.FieldByName('conNo').Value:=trim(CDS_Main.fieldbyname('conNo').AsString);
          CDS_PH.FieldByName('mjid').Value:=trim(CDS_Main.fieldbyname('mjid').AsString);
     //   CDS_PH.FieldByName('mjxh').Value:=trim(fieldbyname('mjxh').AsString);
          CDS_PH.FieldByName('C_CodeName').Value:=trim(CDS_Main.fieldbyname('C_CodeName').AsString);
          CDS_PH.FieldByName('C_Spec').Value:=trim(CDS_Main.fieldbyname('C_Spec').AsString);
          CDS_PH.FieldByName('C_Color').Value:=trim(CDS_Main.fieldbyname('C_Color').AsString);
          CDS_PH.FieldByName('PMainID').Value:=trim(CDS_Main.fieldbyname('MainID').AsString);
          CDS_PH.FieldByName('PSubID').Value:=trim(CDS_Main.fieldbyname('SubID').AsString);
          CDS_PH.FieldByName('kcqtyunit').Value:=trim(CDS_Main.fieldbyname('kcqtyunit').AsString);
          CDS_PH.FieldByName('kcqty').Value:=CDS_Main.fieldbyname('kcqty').AsFloat;
          CDS_PH.FieldByName('kcRollnum').Value:=CDS_Main.fieldbyname('kcRollnum').AsFloat;
          CDS_PH.FieldByName('Qty').Value:=CDS_Main.fieldbyname('kcqty').AsFloat;
          CDS_PH.FieldByName('RollNum').Value:=CDS_Main.fieldbyname('kcRollnum').AsFloat;
          CDS_PH.FieldByName('CRID').Value:=CDS_Main.fieldbyname('CRID').AsFloat;
          CDS_PH.Post;
        end;
        CDS_Main.next;
      end;
      CDS_PH.EnableControls;
      CDS_Main.EnableControls }
      CDS_PH.DisableControls;
      CDS_SubSel.DisableControls;
      CDS_SubSel.first;
      while not CDS_SubSel.eof do
      begin
        if (not CDS_PH.Locate('CRID',trim(CDS_SubSel.fieldbyname('CRID').AsString),[])) then
        begin
          CDS_PH.Append;
          CDS_PH.FieldByName('conNo').Value:=trim(CDS_SubSel.fieldbyname('conNo').AsString);
          CDS_PH.FieldByName('mjid').Value:=trim(CDS_SubSel.fieldbyname('mjid').AsString);
     //   CDS_PH.FieldByName('mjxh').Value:=trim(fieldbyname('mjxh').AsString);
          CDS_PH.FieldByName('C_CodeName').Value:=trim(CDS_SubSel.fieldbyname('C_CodeName').AsString);
          CDS_PH.FieldByName('C_Spec').Value:=trim(CDS_SubSel.fieldbyname('C_Spec').AsString);
          CDS_PH.FieldByName('C_Color').Value:=trim(CDS_SubSel.fieldbyname('C_Color').AsString);
          CDS_PH.FieldByName('PMainID').Value:=trim(CDS_SubSel.fieldbyname('MainID').AsString);
          CDS_PH.FieldByName('PSubID').Value:=trim(CDS_SubSel.fieldbyname('SubID').AsString);
          CDS_PH.FieldByName('kcqtyunit').Value:=trim(CDS_SubSel.fieldbyname('kcqtyunit').AsString);
          CDS_PH.FieldByName('kcqty').Value:=CDS_SubSel.fieldbyname('kcqty').AsFloat;
          CDS_PH.FieldByName('kcRollnum').Value:=CDS_SubSel.fieldbyname('kcRollnum').AsFloat;
          CDS_PH.FieldByName('Qty').Value:=CDS_SubSel.fieldbyname('kcqty').AsFloat;
          CDS_PH.FieldByName('RollNum').Value:=CDS_SubSel.fieldbyname('kcRollnum').AsFloat;
          CDS_PH.FieldByName('CRID').Value:=CDS_SubSel.fieldbyname('CRID').AsFloat;
          CDS_PH.Post;
        end;
        CDS_SubSel.next;
      end;
      CDS_PH.EnableControls;
      CDS_SubSel.EnableControls;
    end;
    free;
  end;
  TBRafresh.Click;
end;

procedure TfrmProductOrder_Ph_other.ToolButton5Click(Sender: TObject);
begin
  if cds_ph.IsEmpty then exit;
  if cxTabControl1.TabIndex=1 then
  begin
   try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from JYOrder_sub ');
      sql.Add('where mainID='+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('and subID='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
      open;
      edit;
      fieldbyname('substatus').Value:='0';
      post;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update CK_BanCP_KC1 SET kcstatus=null  ');
      sql.Add('from CK_BanCP_KC1 A                    ');
      sql.Add('inner join CK_BanCP_PH1 B on B.CRID=A.CRID ');
      sql.Add('where B.mainID='+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('and B.subID='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
      execsql;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete  from CK_BanCP_PH1 ');
      sql.Add('where mainID='+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('and subID='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
      execsql;
    end;
    application.MessageBox('撤销配货成功！','提示信息');
    TBRafresh.Click;
   except
     application.MessageBox('撤销配货失败！','提示信息',0);
   end;
  end;

{ if cxTabControl1.TabIndex=1 then
  begin
   try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from JYOrder_sub ');
      sql.Add('where mainID='+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
      sql.Add('and subID='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
      open;
      edit;
      fieldbyname('substatus').Value:='0';
      post;
    end;
    application.MessageBox('撤销配货确认成功！','提示信息');
    TBRafresh.Click;
   except
     application.MessageBox('撤销配货确认失败！','提示信息',0);
   end;
  end;   }
end;

procedure TfrmProductOrder_Ph_other.TBPrint1Click(Sender: TObject);
var
  filepath,fOrderNo:string;
  i:integer;
  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;


  createTable();
  i:=0;
  with Order_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      IF Fieldbyname('ssel').AsBoolean then
      begin
        IF i=0 then fOrderNo:=trim(fieldbyname('OrderNo').AsString);
        IF (i>0) and (trim(fieldbyname('OrderNo').AsString)<> fOrderNo) then
        begin
           application.MessageBox('选择了不同的订单，不能打印！','提示信息',0);
           EnableControls;
           exit;
        end;
        with adoqueryCmd do
        begin
          Close;
	   	    SQL.Clear;
	   	    SQL.Add(' insert into '+TblCprk+'(MainID,subID) values( ');
          SQL.Add(' '+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
          SQL.Add(','+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
          sql.add(')');
	   	    execsql;
        end;
        i:=i+1;
      end;
      next;
    end;
    First;
    EnableControls;
  end;



  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Do_PrintMd10 ');
    sql.Add('@mainID='+quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
    sql.Add(',@TblCprk='+quotedstr(trim(TblCprk)));
    sql.Add(',@flag='+quotedstr(trim('配货出库龙运')));
    open;
  end;




  try
    filepath:=ExtractFilePath(Application.ExeName) + 'report\出库码单龙运.rmf';
    if not FileExists(Pchar(filepath))  then
    begin
      application.MessageBox(pchar('文件['+filepath+']不存在！'),'提示信息',MB_IConError);
      exit;
    end;

        try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(AdoPrint.fieldbyname('orderNo').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;
    RMVariables['QRBARCODE']:=fImagePath;
    RMVariables['dtxz'] :=FormatDateTime('yyyy-mm-dd',date());
    RMVariables['zdr'] :=trim(DName);
    RMVariables['Filler'] :=trim(Order_Main.fieldbyname('Filler').AsString);
    RMGridReport1.LoadFromFile(filepath);
    RMGridReport1.ShowReport;
  finally
     DropTable();
  end;

end;


procedure TfrmProductOrder_Ph_other.ToolButton6Click(Sender: TObject);
begin
  if  Order_Main.IsEmpty then exit;
  frmCKProductBCPKCListsel_other:=TfrmCKProductBCPKCListsel_other.create(self);
  with frmCKProductBCPKCListsel_other do
  begin

    fmainID:=  trim(self.Order_Main.FieldByName('mainID').AsString);
 //   C_CodeName.Text:= trim(self.Order_Main.FieldByName('PRTCodeName').AsString);
  //  C_Color.Text:=  trim(self.Order_Main.FieldByName('PRTColor').AsString);
  //  FCodeName:= trim(self.Order_Main.FieldByName('PRTCodeName').AsString);
  //  fColor:=  trim(self.Order_Main.FieldByName('PRTColor').AsString);
    if showmodal=1 then
    begin
      {CDS_PH.DisableControls;
      CDS_Main.DisableControls;
      CDS_Main.first;
      while not CDS_Main.eof do
      begin
        if  (CDS_Main.FieldByName('ssel').AsBoolean) and
            (not CDS_PH.Locate('mjid',trim(CDS_Main.fieldbyname('mjid').AsString),[])) then
        begin
          CDS_PH.Append;
          CDS_PH.FieldByName('conNo').Value:=trim(CDS_Main.fieldbyname('conNo').AsString);
          CDS_PH.FieldByName('mjid').Value:=trim(CDS_Main.fieldbyname('mjid').AsString);
     //   CDS_PH.FieldByName('mjxh').Value:=trim(fieldbyname('mjxh').AsString);
          CDS_PH.FieldByName('C_CodeName').Value:=trim(CDS_Main.fieldbyname('C_CodeName').AsString);
          CDS_PH.FieldByName('C_Spec').Value:=trim(CDS_Main.fieldbyname('C_Spec').AsString);
          CDS_PH.FieldByName('C_Color').Value:=trim(CDS_Main.fieldbyname('C_Color').AsString);
          CDS_PH.FieldByName('PMainID').Value:=trim(CDS_Main.fieldbyname('MainID').AsString);
          CDS_PH.FieldByName('PSubID').Value:=trim(CDS_Main.fieldbyname('SubID').AsString);
          CDS_PH.FieldByName('kcqtyunit').Value:=trim(CDS_Main.fieldbyname('kcqtyunit').AsString);
          CDS_PH.FieldByName('kcqty').Value:=CDS_Main.fieldbyname('kcqty').AsFloat;
          CDS_PH.FieldByName('kcRollnum').Value:=CDS_Main.fieldbyname('kcRollnum').AsFloat;
          CDS_PH.FieldByName('Qty').Value:=CDS_Main.fieldbyname('kcqty').AsFloat;
          CDS_PH.FieldByName('RollNum').Value:=CDS_Main.fieldbyname('kcRollnum').AsFloat;
          CDS_PH.FieldByName('CRID').Value:=CDS_Main.fieldbyname('CRID').AsFloat;
          CDS_PH.Post;
        end;
        CDS_Main.next;
      end;
      CDS_PH.EnableControls;
      CDS_Main.EnableControls }
      CDS_PH.DisableControls;
      CDS_SubSel.DisableControls;
      CDS_SubSel.first;
      while not CDS_SubSel.eof do
      begin
        if (not CDS_PH.Locate('CRID',trim(CDS_SubSel.fieldbyname('CRID').AsString),[])) then
        begin
          CDS_PH.Append;
          CDS_PH.FieldByName('conNo').Value:=trim(CDS_SubSel.fieldbyname('conNo').AsString);
          CDS_PH.FieldByName('mjid').Value:=trim(CDS_SubSel.fieldbyname('mjid').AsString);
     //   CDS_PH.FieldByName('mjxh').Value:=trim(fieldbyname('mjxh').AsString);
          CDS_PH.FieldByName('C_CodeName').Value:=trim(CDS_SubSel.fieldbyname('C_CodeName').AsString);
          CDS_PH.FieldByName('C_Spec').Value:=trim(CDS_SubSel.fieldbyname('C_Spec').AsString);
          CDS_PH.FieldByName('C_Color').Value:=trim(CDS_SubSel.fieldbyname('C_Color').AsString);
          CDS_PH.FieldByName('PMainID').Value:=trim(CDS_SubSel.fieldbyname('MainID').AsString);
          CDS_PH.FieldByName('PSubID').Value:=trim(CDS_SubSel.fieldbyname('SubID').AsString);
          CDS_PH.FieldByName('kcqtyunit').Value:=trim(CDS_SubSel.fieldbyname('kcqtyunit').AsString);
          CDS_PH.FieldByName('kcqty').Value:=CDS_SubSel.fieldbyname('kcqty').AsFloat;
          CDS_PH.FieldByName('kcRollnum').Value:=CDS_SubSel.fieldbyname('kcRollnum').AsFloat;
          CDS_PH.FieldByName('Qty').Value:=CDS_SubSel.fieldbyname('kcqty').AsFloat;
          CDS_PH.FieldByName('RollNum').Value:=CDS_SubSel.fieldbyname('kcRollnum').AsFloat;
          CDS_PH.FieldByName('CRID').Value:=CDS_SubSel.fieldbyname('CRID').AsFloat;
          CDS_PH.Post;
        end;
        CDS_SubSel.next;
      end;
      CDS_PH.EnableControls;
      CDS_SubSel.EnableControls;
    end;
    free;
  end;
  TBRafresh.Click;
end;

procedure TfrmProductOrder_Ph_other.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
if Order_Main.IsEmpty then exit;
  initPH();
end;

procedure TfrmProductOrder_Ph_other.TBPrint2Click(Sender: TObject);
var
  filepath,fOrderNo:string;
  i:integer;
begin
  if Order_Main.IsEmpty then exit;

  if Order_Main.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;


  createTable();
  i:=0;
  with Order_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      IF Fieldbyname('ssel').AsBoolean then
      begin
        IF i=0 then fOrderNo:=trim(fieldbyname('OrderNo').AsString);
        IF (i>0) and (trim(fieldbyname('OrderNo').AsString)<> fOrderNo) then
        begin
           application.MessageBox('选择了不同的订单，不能打印！','提示信息',0);
           EnableControls;
           exit;
        end;
        with adoqueryCmd do
        begin
          Close;
	   	    SQL.Clear;
	   	    SQL.Add(' insert into '+TblCprk+'(MainID,subID) values( ');
          SQL.Add(' '+quotedstr(trim(Order_Main.fieldbyname('mainID').AsString)));
          SQL.Add(','+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
          sql.add(')');
	   	    execsql;
        end;
        i:=i+1;
      end;
      next;
    end;
    First;
    EnableControls;
  end;

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Do_PrintMd10 ');
    sql.Add('@mainID='+quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
    sql.Add(',@TblCprk='+quotedstr(trim(TblCprk)));
    sql.Add(',@flag='+quotedstr(trim('配货出库湖州')));
    open;
  end;

  try
    filepath:=ExtractFilePath(Application.ExeName) + 'report\出库码单湖州大纸张.rmf';
    if not FileExists(Pchar(filepath))  then
    begin
      application.MessageBox(pchar('文件['+filepath+']不存在！'),'提示信息',MB_IConError);
      exit;
    end;
    RMVariables['dtxz'] :=FormatDateTime('yyyy-mm-dd',date());
    RMVariables['zdr'] :=trim(DName);
    RMVariables['Filler'] :=trim(Order_Main.fieldbyname('Filler').AsString);
    RMGridReport1.LoadFromFile(filepath);
    RMGridReport1.ShowReport;
  finally
     DropTable();
  end;

end;


procedure TfrmProductOrder_Ph_other.ToolButton7Click(Sender: TObject);
begin
  try
    frmCKSMEdit_other:=TfrmCKSMEdit_other.Create(Application);
    with frmCKSMEdit_other do
    begin

      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmCKSMEdit_other.Free;
  end;
end;

end.
