unit U_OrderInPutPrice;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxPC, cxGridCustomPopupMenu,
  cxGridPopupMenu, cxDropDownEdit,jpeg,IdGlobal, RM_Common, RM_Class,
  RM_e_Xls, RM_System, RM_GridReport, RM_Dataset;

type
  TfrmOrderInPutPrice = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1SOrddefstr1: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxTabSheet3: TcxTabSheet;
    v1Column1: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label10: TLabel;
    Label3: TLabel;
    Label14: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label2: TLabel;
    Label6: TLabel;
    Label21: TLabel;
    Label25: TLabel;
    Label43: TLabel;
    OrderNo: TEdit;
    ConNo: TEdit;
    OrdDate: TDateTimePicker;
    ChuanY: TBtnEditC;
    DLYDate: TDateTimePicker;
    YWY: TBtnEditC;
    Label4: TLabel;
    Note: TMemo;
    Label5: TLabel;
    CDS_JGLC: TClientDataSet;
    DataSource2: TDataSource;
    CDS_JGC: TClientDataSet;
    DataSource3: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    MPRTCodeName: TEdit;
    MPRTSpec: TEdit;
    Label7: TLabel;
    MPRTCF: TEdit;
    MPRTMF: TEdit;
    MPRTKZ: TEdit;
    CustomerNoName: TBtnEditA;
    MPRTCode: TBtnEditA;
    Label8: TLabel;
    Label11: TLabel;
    Label15: TLabel;
    Label9: TLabel;
    MPRTGYHZ: TEdit;
    ClientDataSet5: TClientDataSet;
    cxGridPopupMenu5: TcxGridPopupMenu;
    ClientDataSet6: TClientDataSet;
    DataSource6: TDataSource;
    cxGridPopupMenu6: TcxGridPopupMenu;
    v1Column2: TcxGridDBColumn;
    ComTaiTou: TBtnEditA;
    Label18: TLabel;
    LiDanPerson: TBtnEditA;
    ToolBar4: TToolBar;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ScrollBox2: TScrollBox;
    cxGrid5: TcxGrid;
    Tv5: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    ToolBar5: TToolBar;
    ToolButton9: TToolButton;
    ToolButton12: TToolButton;
    ScrollBox3: TScrollBox;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    v3Column9: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolButton13: TToolButton;
    v1Column18: TcxGridDBColumn;
    ToolButton18: TToolButton;
    ToolButton19: TToolButton;
    v5Column1: TcxGridDBColumn;
    v5Column2: TcxGridDBColumn;
    cxTabSheet7: TcxTabSheet;
    ToolBar8: TToolBar;
    ToolButton20: TToolButton;
    ToolButton21: TToolButton;
    cxGrid8: TcxGrid;
    Tv8: TcxGridDBTableView;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridLevel6: TcxGridLevel;
    DataSource8: TDataSource;
    cxGridPopupMenu8: TcxGridPopupMenu;
    ClientDataSet8: TClientDataSet;
    v5Column3: TcxGridDBColumn;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    RM1: TRMGridReport;
    CDS_Print: TClientDataSet;
    RMDBMain: TRMDBDataSet;
    ToolButton23: TToolButton;
    v1Column14: TcxGridDBColumn;
    Panel6: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Label44: TLabel;
    SCYaoSu: TMemo;
    Panel7: TPanel;
    ToolBar3: TToolBar;
    ToolButton8: TToolButton;
    ToolButton10: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton11: TToolButton;
    Panel8: TPanel;
    Label33: TLabel;
    Label37: TLabel;
    Label38: TLabel;
    MPRTMFNote: TEdit;
    MPRTKZNote: TEdit;
    Label16: TLabel;
    DuanYiZhuang: TEdit;
    Label17: TLabel;
    Label39: TLabel;
    YaoFeng: TBtnEditC;
    Label45: TLabel;
    LiuYang: TBtnEditC;
    Label42: TLabel;
    PiChang: TBtnEditC;
    KHNO: TEdit;
    Label46: TLabel;
    Label47: TLabel;
    YanHuoNote: TMemo;
    Label48: TLabel;
    DanZheng: TBtnEditC;
    Label50: TLabel;
    Label51: TLabel;
    WanBaoZhuang: TBtnEditC;
    Label52: TLabel;
    ZhiGuan: TBtnEditC;
    Label53: TLabel;
    SuLiaoDai: TBtnEditC;
    Label54: TLabel;
    XiaoBiaoQian: TBtnEditC;
    Label55: TLabel;
    TangJin: TBtnEditC;
    Label34: TLabel;
    Label35: TLabel;
    Label32: TLabel;
    KgZMiXs: TEdit;
    KgZMaXs: TEdit;
    PiZhong: TEdit;
    Label28: TLabel;
    ADOQueryPrint: TADOQuery;
    DataSource5: TDataSource;
    ClientDataSet9: TClientDataSet;
    SSDept: TBtnEditC;
    Label20: TLabel;
    MaiTou: TMemo;
    Label22: TLabel;
    DaiKuanTH: TComboBox;
    Label23: TLabel;
    QueRenCYCH: TComboBox;
    Label24: TLabel;
    YanHuoCH: TComboBox;
    Label26: TLabel;
    CHFSNote: TEdit;
    v5Column6: TcxGridDBColumn;
    v5Column7: TcxGridDBColumn;
    v5Column8: TcxGridDBColumn;
    v5Column9: TcxGridDBColumn;
    v5Column10: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure RanFactoryBtnUpClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure NoteDblClick(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure OrdDefStr2BtnUpClick(Sender: TObject);
    procedure YCLFactoryBtnUpClick(Sender: TObject);
    procedure MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure cxGridDBColumn6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure CustomerNoNameBtnClick(Sender: TObject);
    procedure MPRTCodeBtnClick(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure Tv5CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure cxGridDBColumn1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton9Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Tv6CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv5CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure v1Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ComTaiTouBtnClick(Sender: TObject);
    procedure LiDanPersonBtnClick(Sender: TObject);
    procedure v3Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton13Click(Sender: TObject);
    procedure v3Column2PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn32PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton18Click(Sender: TObject);
    procedure ToolButton19Click(Sender: TObject);
    procedure v5Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton20Click(Sender: TObject);
    procedure ToolButton21Click(Sender: TObject);
    procedure v5Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton23Click(Sender: TObject);
    procedure ConNoDblClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
  private
    CanShu1,CanShu2,ConPrice:String;
    FJSFlag:Boolean;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    function  SaveDataSubOne():Boolean;
    function  SaveDataMain():Boolean;
    function SaveDataSubMore():Boolean;


    function  SaveMDData(FOrdMainId,FOrdSubId,FPSID,FPFID:string):Boolean;
    function  UpdateTJCKQty(FFPFID:string):Boolean;
    function  UpdateTJCKQtyOne(FFPFID:string):Boolean;
    { Private declarations }
  public
    PState,CopyInt,PriceFlag,DRInt,JYInt:Integer;
    FMainId,FFMainId,FOrderNo,ConSubId,DataType,FST,FXianShi:String;
    FXS:Integer;
    { Public declarations }
  end;

var
  frmOrderInPutPrice: TfrmOrderInPutPrice;
  newh:hwnd;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_CPManageTPCX,U_ZHKHListNewCX
  ,U_GYSSelList,U_OrderSubSel,U_TJCKKC,U_LabelList, U_ModuleNote,U_OrderSubSelMX
  ,U_ContractSelList,U_CPSel, U_GYSList;

{$R *.dfm}

procedure TfrmOrderInPutPrice.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('指示单录入YD',Tv1,'生产指示单管理');
  WriteCxGrid('流程录入YD',Tv2,'生产指示单管理');
  WriteCxGrid('加工商录入YD',Tv3,'生产指示单管理');
  WriteCxGrid('待处理主信息',Tv5,'生产指示单管理');
  WriteCxGrid('疵点信息',Tv8,'生产指示单管理');

end;

procedure TfrmOrderInPutPrice.InitData();
var
  fsj:string;
begin
  fsj:='select ZdyName Name,ZdyNo Code from KH_Zdy where Type=''PFGenDanPerson'' ';
  SInitCxGridComboBoxBySql(ADOTemp,cxGridDBColumn2,fsj,0,False,'');
  if Trim(CanShu1)<>'高权限' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD where ORDMainId='''+Trim(FMainId)+'''');
      Open;
    end;
    if ADOTemp.IsEmpty=false then
    begin
      OrderNo.ReadOnly:=True;
    end else
    begin
      OrderNo.ReadOnly:=False;
    end;
  end;
  ReadCxGrid('指示单录入YD',Tv1,'生产指示单管理');
  ReadCxGrid('流程录入YD',Tv2,'生产指示单管理');
  ReadCxGrid('加工商录入YD',Tv3,'生产指示单管理');
  ReadCxGrid('待处理主信息',Tv5,'生产指示单管理');
  ReadCxGrid('疵点信息',Tv8,'生产指示单管理');
  

  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrder_Sub where 1=1 ');
    if PState=1 then
    begin
      sql.Add(' and MainId='''+Trim(FMainId)+'''');
    end;
    if PState=0 then
    begin
      sql.Add(' and 1<>1');
    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from JYOrder_CD where 1=1 ');
    if PState=1 then
    begin
      sql.Add(' and MainId='''+Trim(FMainId)+'''');
    end;
    if PState=0 then
    begin
      sql.Add(' and 1<>1');
    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet8);
  SInitCDSData20(ADOQuery1,ClientDataSet8);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  FJSFlag:=ADOQuery1.FieldByName('JSFlag').AsBoolean;
  SCSHDataNew(ADOQuery1,Panel1,2);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    if CopyInt<>99 then
      sql.Add('select * from JYOrder_Process where MainId='''+Trim(FMainId)+'''')
    else
      sql.Add('select * from JYOrder_Process where 1<>1');
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_JGLC);
  SInitCDSData20(ADOQuery1,CDS_JGLC);

  if PState=0 then
  begin
    OrdDate.DateTime:=SGetServerDateTime(ADOTemp);
    DlyDate.DateTime:=OrdDate.DateTime;
    {LiDanPerson.Text:=Trim(DName);
    YWY.Text:='';   }
  end else
  begin
  end;
  if CopyInt=99 then
  begin
    PState:=0;
    FMainId:='';
    //OrdPerson1.Text:=Trim(DName);
    OrderNo.Text:='';
    OrderNo.ReadOnly:=False;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value:='';
        FieldByName('SubId').Value:='';
        Post;
        Next;
      end;
    end;
  end;
  Label33.Caption:=Trim(OrderNo.Text)+'  '+Trim(MPRTCodeName.Text);
end;

procedure TfrmOrderInPutPrice.ZDYHelp(FButn:TcxButtonEdit;LType:string);
begin
end;

procedure TfrmOrderInPutPrice.FormShow(Sender: TObject);
begin
  if Trim(FXianShi)='安排' then
  begin
    Panel1.Enabled:=False;
    ToolButton1.Visible:=False;
    ToolButton2.Visible:=False;
    Tv1.OptionsSelection.CellSelect:=False;
    CustomerNoName.Visible:=False;
  end else
  begin
    if Trim(FST)='仓库' then
    begin
      //cxPageControl1.ActivePageIndex:=1;
      Panel1.Enabled:=False;
      ToolButton1.Visible:=False;
      ToolButton2.Visible:=False;
      Tv1.OptionsSelection.CellSelect:=False;
      CustomerNoName.Visible:=False;
      TBSave.Visible:=False;
      ToolBar4.Visible:=False;
      ToolBar3.Visible:=False;
      ToolBar8.Visible:=False;
      Tv2.OptionsSelection.CellSelect:=False;
      Tv3.OptionsSelection.CellSelect:=False;
    end else
    begin
      ToolBar3.Visible:=False;
      ToolBar4.Visible:=False;
      ToolBar5.Visible:=False;
      ToolBar8.Visible:=False;
      Tv2.OptionsSelection.CellSelect:=False;
      Tv3.OptionsSelection.CellSelect:=False;
      Tv5.OptionsSelection.CellSelect:=False;
      Tv8.OptionsSelection.CellSelect:=False;
      Tv1.OptionsSelection.CellSelect:=True;
    end;

  end;
  InitData();
end;

function TfrmOrderInPutPrice.SaveData():Boolean;
var
  maxno,maxsubno,maxJDNo,JDMainId,maxjfno,maxjcno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'JM','JYOrder_Main',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FMainId);
    end;
    if Trim(FXianShi)='' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from JYOrder_Main where MainId='''+Trim(FMainId)+'''');
        Open;
      end;
      with ADOCmd do
      begin
        if Trim(FMainId)='' then
        begin
          Append;
        end
        else begin
          Edit;
        end;
        FieldByName('MainId').Value:=Trim(maxno);
        if Trim(ConSubId)<>'' then
        begin
          FieldByName('ConSubId').Value:=Trim(ConSubId);
        end;
        RTSetsavedata(ADOCmd,'JYOrder_Main',Panel1,2);
        FieldByName('CustomerNo').Value:=Trim(CustomerNoName.TxtCode);
        if Trim(FMainId)='' then
        begin
          FieldByName('Filler').Value:=Trim(DName);
        end else
        begin
          FieldByName('Editer').Value:=Trim(DName);
          FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
        end;
        Post;
      end;

      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrder_Main where orderno='''+Trim(OrderNo.Text)+'''');
        Open;
      end;
      if ADOTemp.RecordCount>1 then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('订单号重复!','提示',0);
        Exit;
      end;
      ///保存子表
      with Order_Sub do
      begin
        First;
        while not Eof do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
          begin
            if GetLSNo(ADOCmd,maxsubno,'JS','JYOrder_Sub',4,1)=False then
            begin
              ADOCmd.Connection.RollbackTrans;
              Application.MessageBox('取子流水号失败！','提示',0);
              Exit;
            end;
          end else
          begin
            maxsubno:=Trim(Order_Sub.fieldbyname('SubId').AsString);
          end;
          with ADOCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select * from JYOrder_Sub where SubId='''+Trim(maxsubno)+'''');
            Open;
          end;
          with ADOCmd do
          begin
            if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
              Append
            else
              Edit;
            FieldByName('MainId').Value:=Trim(maxno);
            FieldByName('SubId').Value:=Trim(maxsubno);
            RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'JYOrder_Sub',0);
            Post;
          end;
          Order_Sub.Edit;
          Order_Sub.FieldByName('SubId').Value:=Trim(maxsubno);
          //Order_Sub.Post;
          Next;
        end;
      end;
    end else
    if Trim(FXianShi)='安排' then
    begin
      ///疵点表
      with ClientDataSet8 do
      begin
        First;
        while not Eof do
        begin
          if Trim(ClientDataSet8.fieldbyname('JCId').AsString)='' then
          begin
            if GetLSNo(ADOCmd,maxjcno,'JC','JYOrder_CD',4,1)=False then
            begin
              ADOCmd.Connection.RollbackTrans;
              Application.MessageBox('取疵点流水号失败！','提示',0);
              Exit;
            end;
          end else
          begin
            maxjcno:=Trim(ClientDataSet8.fieldbyname('JCId').AsString);
          end;
          with ADOCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select * from JYOrder_CD where JCId='''+Trim(maxjcno)+'''');
            Open;
          end;
          with ADOCmd do
          begin
            if Trim(ClientDataSet8.fieldbyname('JCId').AsString)='' then
            begin
              Append;
              //FieldByName('Filler').Value:=Trim(DName);
            end
            else
            begin
              Edit;
              //FieldByName('Editer').Value:=Trim(DName);
              //FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
            end;
            FieldByName('MainId').Value:=Trim(maxno);
            FieldByName('JCId').Value:=Trim(maxjcno);
            //FieldByName('SubId').Value:=Trim(maxsubno);
            RTSetSaveDataCDS(ADOCmd,Tv8,ClientDataSet8,'JYOrder_CD',0);
            Post;
          end;
          ClientDataSet8.Edit;
          ClientDataSet8.FieldByName('JCId').Value:=Trim(maxjcno);
          //Order_Sub.Post;
          Next;
        end;
      end;
      ///保存流程表
      with CDS_JGLC do
      begin
        First;
        while not Eof do
        begin
          if Trim(CDS_JGLC.fieldbyname('PSId').AsString)='' then
          begin
            if GetLSNo(ADOCmd,maxsubno,'PS','JYOrder_Process',4,1)=False then
            begin
              ADOCmd.Connection.RollbackTrans;
              Application.MessageBox('取流程流水号失败！','提示',0);
              Exit;
            end;
          end else
          begin
            maxsubno:=Trim(CDS_JGLC.fieldbyname('PSId').AsString);
          end;
          with ADOCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select * from JYOrder_Process where PSId='''+Trim(maxsubno)+'''');
            Open;
          end;
          with ADOCmd do
          begin
            if Trim(CDS_JGLC.fieldbyname('PSId').AsString)='' then
              Append
            else
              Edit;
            FieldByName('MainId').Value:=Trim(maxno);
            FieldByName('PSId').Value:=Trim(maxsubno);
            RTSetSaveDataCDS(ADOCmd,Tv2,CDS_JGLC,'JYOrder_Process',0);
            Post;
          end;
          CDS_JGLC.Edit;
          CDS_JGLC.FieldByName('PSId').Value:=Trim(maxsubno);
          CDS_JGLC.FieldByName('MainId').Value:=Trim(maxno);
          //Order_Sub.Post;
          Next;
        end;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
    FMainId:=Trim(maxno);
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;
function TfrmOrderInPutPrice.SaveDataMain():Boolean;
var
  maxno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JYOrder_Main where MainId='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
        maxno:=Trim(FFMainId);
      end
      else begin
        maxno:=Trim(FMainId);
        Edit;
      end;
      FieldByName('MainId').Value:=Trim(maxno);
      //SSetsaveSqlNew(ADOCmd,'JYOrder_Main',ScrollBox1,2);
      if PState=1 then
      begin
        FieldByName('OrdUpDate').Value:=SGetServerDateTime(ADOTemp);
      end;
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;
    FMainId:=Trim(maxno);
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;
function TfrmOrderInPutPrice.SaveDataSubOne():Boolean;
var
  maxno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存子表
    with Order_Sub do
    begin
      //First;
      //while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
        begin
          if GetLSNo(ADOTemp,maxno,'JS','JYOrder_Sub',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub where MainId='''+Trim(FMainId)+'''');
          sql.Add(' and SubId='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(FMainId);
          FieldByName('SubId').Value:=Trim(maxno);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'JYOrder_Sub',0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value:=Trim(maxno);
        //Order_Sub.Post;
        //Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;
function TfrmOrderInPutPrice.SaveDataSubMore():Boolean;
var
  maxno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
        begin
          if GetLSNo(ADOTemp,maxno,'JS','JYOrder_Sub',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub where MainId='''+Trim(FMainId)+'''');
          sql.Add(' and SubId='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(FMainId);
          FieldByName('SubId').Value:=Trim(maxno);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'JYOrder_Sub',0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value:=Trim(maxno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;

procedure TfrmOrderInPutPrice.TBSaveClick(Sender: TObject);
var
  FReal:Double;
begin
  ToolBar1.SetFocus;
  if Trim(OrderNo.Text)='' then
  begin
    Application.MessageBox('订单号不能为空！','提示',0);
    Exit;
  end;
  if Trim(ConNo.Text)='' then
  begin
    Application.MessageBox('合同号不能为空！','提示',0);
    Exit;
  end;
  if Trim(MPRTMF.Text)<>'' then
  begin
    if TryStrToFloat(MPRTMF.Text,FReal)=False then
    begin
      Application.MessageBox('门幅非法数字!','提示',0);
      Exit;
    end;
  end else
  begin
    Application.MessageBox('门幅不能为空!','提示',0);
    Exit;
  end;
  if Trim(MPRTKZ.Text)<>'' then
  begin
    if TryStrToFloat(MPRTKZ.Text,FReal)=False then
    begin
      Application.MessageBox('克重非法数字!','提示',0);
      Exit;
    end;
  end else
  begin
    Application.MessageBox('克重不能为空!','提示',0);
    Exit;
  end;
  if Trim(FXianShi)='安排' then
  begin
    {if CDS_JGLC.IsEmpty=False then
    begin
      if CDS_JGLC.Locate('MFQty',Null,[])=True then
      begin
        Application.MessageBox('生产流程中的门幅不能为空!','提示',0);
        exit;
      end;
      if CDS_JGLC.Locate('KZQty',Null,[])=True then
      begin
        Application.MessageBox('生产流程中的克重不能为空!','提示',0);
        exit;
      end;
    end;}
    if CDS_JGLC.IsEmpty=False then
    begin
      if CDS_JGLC.Locate('KgZM',null,[])=True then
      begin
        Application.MessageBox('折算系数不能为空!','提示',0);
        Exit;
      end;
    end;
    {if CDS_JGLC.IsEmpty=False then
    begin
      if CDS_JGLC.Locate('PSName','出货',[])=False then
      begin
        Application.MessageBox('生产流程没有选择出货！','提示',0);
        Exit;
      end;
    end; }
    if cxPageControl1.ActivePageIndex=2 then
    begin
      if CDS_JGC.IsEmpty=False then
      begin
        if CDS_JGC.Locate('PFID',null,[])=True then
        begin
          Application.MessageBox('安排信息未确定!','提示',0);
          Exit;
        end;
      end;

    end;
  end;

  if Trim(KgZMiXs.Text)<>'' then
  begin
    if TryStrToFloat(KgZMiXs.Text,FReal)=False then
    begin
      Application.MessageBox('折算系数非法数字!','提示',0);
      Exit;
    end;
  end;
  {if Trim(ClothOneQty.Text)<>'' then
  begin
    if TryStrToFloat(ClothOneQty.Text,FReal)=False then
    begin
      Application.MessageBox('每匹白坯数量非法数字!','提示',0);
      Exit;
    end;
  end;
  if Trim(MPRTOneQty.Text)<>'' then
  begin
    if TryStrToFloat(MPRTOneQty.Text,FReal)=False then
    begin
      Application.MessageBox('每匹成品数量非法数字!','提示',0);
      Exit;
    end;
  end; }
 
  if Order_Sub.Locate('PRTOrdQty',Null,[])=True then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('PRTColor',Null,[])=True then
  begin
    Application.MessageBox('颜色中文不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
  end;
 // if cxPageControl1.ActivePageIndex=2 then
  begin
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.PRTHX,B.PRTColorNo,B.PRTOrdQty,B.OrderUnit from JYOrder_PCS_Sub A ');
      sql.Add(' left join JYOrder_Sub B on A.SubId=B.SubId');
      sql.Add('where A.PSID='''+Trim(CDS_JGLC.fieldbyname('PSID').AsString)+'''');
      SQL.Add(' and isnull(A.CKName,'''')<>''调节仓库'' ');
      Open;
    end;
    SCreateCDS20(ADOQuery1,CDS_JGC);
    SInitCDSData20(ADOQuery1,CDS_JGC);
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.PRTHX,B.PRTColorNo,B.PRTOrdQty,B.OrderUnit from JYOrder_PCS_Sub A ');
      sql.Add(' left join JYOrder_Sub B on A.SubId=B.SubId');
      sql.Add('where A.PSID='''+Trim(CDS_JGLC.fieldbyname('PSID').AsString)+'''');
      SQL.Add(' and isnull(A.CKName,'''')=''调节仓库'' ');
      Open;
    end;
    SCreateCDS20(ADOQuery1,ClientDataSet5);
    SInitCDSData20(ADOQuery1,ClientDataSet5);
    
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,C.OrderNo,D.C_Code,D.C_CodeName,B.ToFactoryName,B.RKQtyUnit from JYOrder_Main_MD_Out A ');
      sql.Add(' inner join JYOrder_Main_MD B on A.MDId=B.MDId');
      sql.Add(' inner join JYOrder_Main C on B.OrdMainId=C.MainId');
      sql.Add(' inner join Contract_Sub D on B.ConSubId=D.SubId');
      sql.Add('where A.PFID='''+Trim(ClientDataSet5.fieldbyname('PFID').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQuery1,ClientDataSet6);
    SInitCDSData20(ADOQuery1,ClientDataSet6);
  end;

end;

procedure TfrmOrderInPutPrice.v1PRTColorPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdColor';
      flagname:='颜色';
      V1Name.Caption:='中文名称';
      V1Note.Caption:='英文名称';
      MainType:=Trim(DName);
      fnote:=True;
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTColor').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.Order_Sub.FieldByName('PRTColorEng').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutPrice.ToolButton1Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Trim(OrderNo.Text)='' then
  begin
    Application.MessageBox('订单号不能为空!','提示',0);
    Exit;
  end;
  CopyAddRow(Tv1,Order_Sub);
  with Order_Sub do
  begin
    Edit;
    FieldByName('PRTColor').Value:='';
    FieldByName('PRTOrdQty').Value:=null;
    FieldByName('PRTColorNo').Value:=null;
    FieldByName('PRTColorEng').Value:=null;
    FieldByName('PRTHX').Value:=null;
    FieldByName('SubId').Value:='';
    FieldByName('XHInt').Value:=Order_Sub.RecordCount;
    Post;
  end;
end;

procedure TfrmOrderInPutPrice.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
 { with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Cloth_LLMX where OrdSubId='''+Trim(Order_Sub.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty=False then
  begin
    Application.MessageBox('已领料不能删除数据!','提示',0);
    Exit;
  end;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Sub_AnPai where SubId='''+Trim(Order_Sub.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty=False then
  begin
    Application.MessageBox('已回仓不能删除数据!','提示',0);
    Exit;
  end; }

  if Trim(Order_Sub.fieldbyname('SubId').AsString)<>'' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD where ORDSubId='''+Trim(Order_Sub.fieldbyname('SubId').AsString)+'''');
      Open;
    end;
    if ADOTemp.IsEmpty=false then
    begin
      Application.MessageBox('已经录入码单不能删除数据!','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Sub where SubId='''+Trim(Order_Sub.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
 { if Order_Sub.IsEmpty then
  begin
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Main where MainId='''+Trim(FMainId)+'''');
      ExecSQL;
    end;
  end;  }
end;

procedure TfrmOrderInPutPrice.RanFactoryBtnUpClick(Sender: TObject);
begin
  {try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='RKPlace';
      flagname:='染厂名称';    
      if ShowModal=1 then
      begin
        JGFactoryName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        JGFactoryName.TxtCode:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end; }
end;

procedure TfrmOrderInPutPrice.CustomerNoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text:='';
  TBtnEditC(Sender).TxtCode:='';
end;

procedure TfrmOrderInPutPrice.NoteDblClick(Sender: TObject);
begin
  {try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='OrdNote';
      flagname:='备注及要求';
      if ShowModal=1 then
      begin
        Note.Text:=Note.Text+frmZDYHelpSel.ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;}
end;

procedure TfrmOrderInPutPrice.v1OrderUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('OrderUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutPrice.v1PriceUnitPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PriceUnit';
      flagname:='价格单位';
      if Trim(DParameters1)<>'高权限' then
      begin
        TBAdd.Visible:=False;
        TBEdit.Visible:=False;
        TBDel.Visible:=False;
      end;

      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PriceUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutPrice.OrdDefStr2BtnUpClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if Trim(flag)='OrdDefStr2' then
      begin
        //flag:='OrdDefStr2';
        V1Name.Caption:='中文名称';
        V1Note.Caption:='英文名称';
        fnote:=True;
      end;
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        if Trim(flag)='MPRTCode' then
        begin
          with ADOTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select Top 1 * from JYOrder_Main where MPRTCode='''+Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'''');
            sql.Add(' order by FillTime desc');
            Open;
          end;
          MPRTCodeName.Text:=ADOTemp.fieldbyname('MPRTCodeName').Value;
          MPRTMF.Text:=ADOTemp.fieldbyname('MPRTMF').Value;
          MPRTKZ.Text:=ADOTemp.fieldbyname('MPRTKZ').Value;
          MPRTSpec.Text:=ADOTemp.fieldbyname('MPRTSpec').Value;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutPrice.YCLFactoryBtnUpClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      MainType:=TEdit(Sender).Name;
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutPrice.MPRTSCTeBieNoteBtnUpClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;


procedure TfrmOrderInPutPrice.ToolButton6Click(Sender: TObject);
var
  Fint:Integer;
  FReal,FMF,FKZ:Double;
begin
  {if Trim(FMainId)='' then
  begin
    Application.MessageBox('基本信息未保存!','提示',0);
    Exit;
  end;  }
  Fint:=CDS_JGLC.RecordCount;
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='JGGX';
      flagname:='加工工序';
      if ShowModal=1 then
      begin
        with ClientDataSet1 do
        begin
          First;
          while not eof do
          begin
            if ClientDataSet1.FieldByName('SSel').AsBoolean=True then
            begin
              with CDS_JGLC do
              begin
                Append;
                FieldByName('PSXH').Value:=Fint+1;
                FieldByName('PSName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
                if Trim(MPRTMF.Text)<>'' then
                begin
                  if TryStrToFloat(MPRTMF.Text,FReal)=True then
                  begin
                    FieldByName('MFQty').Value:=MPRTMF.Text;
                    FMF:=StrToFloat(MPRTMF.Text);
                  end else
                  begin
                    FMF:=0;
                  end;
                end else
                begin
                  FMF:=0;
                end;
                if Trim(MPRTKZ.Text)<>'' then
                begin
                  if TryStrToFloat(MPRTKZ.Text,FReal)=True then
                  begin
                    FieldByName('KZQty').Value:=MPRTKZ.Text;
                    FKZ:=StrToFloat(MPRTKZ.Text);
                  end else
                  begin
                    FKZ:=0;
                  end;
                end else
                begin
                  FKZ:=0;
                end;
                if FMF*FKZ<>0 then
                begin
                  FieldByName('KgZM').Value:=100000*1.0000/(FMF*FKZ);
                end else
                begin
                  FieldByName('KgZM').Value:=0;
                end;
                Fint:=Fint+1;
                Post;
              end;
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPutPrice.ToolButton4Click(Sender: TObject);
begin
  if CDS_JGLC.IsEmpty then
  begin
    Application.MessageBox('加工流程不能为空!','提示',0);
    Exit;
  end;
  if Trim(CDS_JGLC.fieldbyname('PSID').AsString)='' then
  begin
    Application.MessageBox('信息未保存!','提示',0);
    Exit;
  end;
  try
    frmGYSSelList:=TfrmGYSSelList.Create(Application);
    with frmGYSSelList do
    begin
      if ShowModal=1 then
      begin
        with CDS_JGC do
        begin
          Append;
          FieldByName('FactoryNo').Value:=Trim(frmGYSSelList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('FactoryName').Value:=Trim(frmGYSSelList.Order_Main.fieldbyname('KHNameJC').AsString);
          Post;
        end;
      end;
    end;
  finally
    frmGYSSelList.Free;
  end;

end;

procedure TfrmOrderInPutPrice.ToolButton8Click(Sender: TObject);
var
  maxno:string;
begin
  {if Trim(CDS_JGLC.fieldbyname('PSID').AsString)='' then
  begin
    Application.MessageBox('信息未保存!','提示',0);
    Exit;
  end;}
  if CDS_JGC.IsEmpty then Exit;
  ToolBar1.SetFocus;
  try
    ADOCmd.Connection.BeginTrans;
    CDS_JGC.DisableControls;
    with CDS_JGC do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_JGC.fieldbyname('PFID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'PF','JYOrder_PCS_Sub',3,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            CDS_JGC.EnableControls;
            Application.MessageBox('取流程加工厂流水号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(CDS_JGC.fieldbyname('PFID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_PCS_Sub where PFID='''+Trim(CDS_JGC.fieldbyname('PFID').AsString)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_JGC.fieldbyname('PFID').AsString)='' then
          begin
            Append;
          end else
          begin
            Edit;
          end;
          FieldByName('MainID').Value:=Trim(FMainId);
          FieldByName('PSID').Value:=Trim(CDS_JGLC.fieldbyname('PSID').AsString);
          FieldByName('SubID').Value:=Trim(CDS_JGC.fieldbyname('SubID').AsString);
          FieldByName('PFID').Value:=Trim(maxno);
          FieldByName('FactoryNo').Value:=Trim(CDS_JGC.fieldbyname('FactoryNo').AsString);
          FieldByName('FactoryName').Value:=Trim(CDS_JGC.fieldbyname('FactoryName').AsString);
          FieldByName('PFDlyDate').Value:=CDS_JGC.fieldbyname('PFDlyDate').Value;
          RTSetSaveDataCDS(ADOCmd,Tv3,CDS_JGC,'JYOrder_PCS_Sub',0);
          Post;
        end;
        if Trim(CDS_JGC.fieldbyname('PFTPUnit').AsString)<>'M' then
        begin
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update JYOrder_PCS_Sub Set PFTPMQty=PFTPQty*');
            sql.Add('(select KgZM from JYOrder_Process A where A.PSID=JYOrder_PCS_Sub.PSID)');
            sql.Add(' where PFID='''+Trim(maxno)+'''');
            ExecSQL;
          end;
        end else
        begin
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update JYOrder_PCS_Sub Set PFTPMQty=PFTPQty');
            sql.Add(' where PFID='''+Trim(maxno)+'''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;

    ADOCmd.Connection.CommitTrans;
    CDS_JGC.EnableControls;
    Application.MessageBox('操作成功!','提示',0);
    Exit;
  except
    ADOCmd.Connection.RollbackTrans;
    CDS_JGC.EnableControls;
    Application.MessageBox('操作异常!','提示',0);
    Exit;
  end;
end;

procedure TfrmOrderInPutPrice.cxGridDBColumn6PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
var
  fsj:string;
begin
  {try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PSNote';
      flagname:='生产流程说明';

      if ShowModal=1 then
      begin
        with CDS_JGLC do
        begin
          Edit;
          FieldByName('PSNote').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;  }
  fsj:=Trim(CDS_JGLC.fieldbyname('PSNote').AsString);
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='PSNote';
      flagname:='生产流程说明';
      MainType:=Trim(CDS_JGLC.fieldbyname('PSName').AsString);
      if ShowModal=1 then
      begin
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if ClientDataSet1.FieldByName('SSel').AsBoolean=True then
            begin
              fsj:=fsj+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
            end;
            Next;
          end;
        end;
        with CDS_JGLC do
        begin
          Edit;
          FieldByName('PSNote').Value:=Trim(fsj);
          //Post;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPutPrice.CustomerNoNameBtnClick(Sender: TObject);
begin
  try
    frmZHKHListNewCX:=TfrmZHKHListNewCX.Create(Application);
    with frmZHKHListNewCX do
    begin
      if ShowModal=1 then
      begin
        CustomerNoName.Text:=Trim(frmZHKHListNewCX.Order_Main.fieldbyname('KHNameJC').AsString);
        CustomerNoName.TxtCode:=Trim(frmZHKHListNewCX.Order_Main.fieldbyname('ZKID').AsString);
       // KHNO.Text:=Trim(frmZHKHListNewCX.Order_Main.fieldbyname('KHCode').AsString);
      end;
    end;
  finally
    frmZHKHListNewCX.Free;
  end;
end;

procedure TfrmOrderInPutPrice.MPRTCodeBtnClick(Sender: TObject);
var
  fsj:string;
begin
  try
    frmCPSel:=TfrmCPSel.Create(Application);
    with frmCPSel do
    begin
      frmCPSel.Align:=alClient;
      if ShowModal=1 then
      begin
        MPRTCode.Text:=Trim(frmCPSel.CDS_Main.fieldbyname('CYNO').AsString);
        MPRTCodeName.Text:=Trim(frmCPSel.CDS_Main.fieldbyname('CYName').AsString);
        MPRTSpec.Text:=Trim(frmCPSel.CDS_Main.fieldbyname('CYSpec').AsString);
        MPRTCF.Text:=Trim(frmCPSel.CDS_Main.fieldbyname('CYCF').AsString);
        MPRTMF.Text:=Trim(frmCPSel.CDS_Main.fieldbyname('CYMF').AsString);
        MPRTKZ.Text:=Trim(frmCPSel.CDS_Main.fieldbyname('CYKZ').AsString);
      end;
    end;
  finally
    frmCPSel.Free;
  end;
  if Trim(MPRTMF.Text)<>'' then
  begin
    MPRTMF.Text:=Copy(Trim(MPRTMF.Text),1,Pos('C',Trim(MPRTMF.Text))-1);
  end;
  if Trim(MPRTKZ.Text)<>'' then
  begin
    MPRTKZ.Text:=Copy(Trim(MPRTKZ.Text),1,Pos('g',Trim(MPRTKZ.Text))-1);
  end;
end;

procedure TfrmOrderInPutPrice.ToolButton7Click(Sender: TObject);
begin
  if CDS_JGC.IsEmpty=False then
  begin
    Application.MessageBox('已安排不能删除数据!','提示',0);
    Exit;
  end;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD where PSID='''+Trim(CDS_JGLC.fieldbyname('PSID').AsString)+'''');
    Open;
  end;
  if ADOTemp.IsEmpty=False then
  begin
    Application.MessageBox('已录入码单，不能删除数据!','提示',0);
    Exit;
  end;
  if Trim(CDS_JGLC.fieldbyname('PSId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Process where PSId='''+Trim(CDS_JGLC.fieldbyname('PSId').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_JGLC.Delete;
end;

procedure TfrmOrderInPutPrice.Tv2CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.PRTHX,PRTColor=isnull(B.PRTColorNo,'''')+'' ''+isnull(B.PRTColorEng,'''')+'' ''+isnull(B.PRTColor,''''), ');
    sql.Add(' B.PRTOrdQty,B.OrderUnit from JYOrder_PCS_Sub A');
    sql.Add(' left join JYOrder_Sub B on A.SubId=B.SubId');
    sql.Add('where A.PSID='''+Trim(CDS_JGLC.fieldbyname('PSID').AsString)+'''');
    SQL.Add(' and isnull(A.CKName,'''')<>''调节仓库'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_JGC);
  SInitCDSData20(ADOQuery1,CDS_JGC);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,B.PRTHX,PRTColor=isnull(B.PRTColor,'''')+'' ''+isnull(B.PRTColorNo,'''') ');
    sql.Add(' ,orderNo=(select OrderNo from JYOrder_Main JM where JM.MainId=(select ORDMainid from JYOrder_Main_MD MD where MD.MDID=A.DiaoYongMDID))');
    sql.Add(' ,B.PRTOrdQty,B.OrderUnit from JYOrder_PCS_Sub A');
    sql.Add(' left join JYOrder_Sub B on A.SubId=B.SubId');
    sql.Add('where A.PSID='''+Trim(CDS_JGLC.fieldbyname('PSID').AsString)+'''');
    SQL.Add(' and isnull(A.CKName,'''')=''调节仓库'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet5);
  SInitCDSData20(ADOQuery1,ClientDataSet5);
end;

procedure TfrmOrderInPutPrice.ToolButton10Click(Sender: TObject);
var
  i:Integer;
  FName:String;
begin

  if CDS_JGLC.IsEmpty then
  begin
    Application.MessageBox('加工流程不能为空!','提示',0);
    Exit;
  end;
  if Trim(CDS_JGLC.fieldbyname('PSID').AsString)='' then
  begin
    Application.MessageBox('信息未保存!','提示',0);
    Exit;
  end;
  if CDS_JGC.IsEmpty then
  begin
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.PRTHX,PRTColor=isnull(B.PRTColorNo,'''')+'' ''+isnull(B.PRTColorEng,'''')+'' ''+isnull(B.PRTColor,''''), ');
      sql.Add(' B.PRTOrdQty,B.OrderUnit from JYOrder_PCS_Sub A');
      sql.Add(' left join JYOrder_Sub B on A.SubId=B.SubId');
      sql.Add('where A.PSID='''+Trim(CDS_JGLC.fieldbyname('PSID').AsString)+'''');
      SQL.Add(' and isnull(A.CKName,'''')<>''调节仓库'' ');
      Open;
    end;
    SCreateCDS20(ADOQuery1,CDS_JGC);
    SInitCDSData20(ADOQuery1,CDS_JGC);
  end;
  try
    frmOrderSubSel:=TfrmOrderSubSel.Create(Application);
    with frmOrderSubSel do
    begin
      FFFMainId:=Trim(Self.FMainId);
      if ShowModal=1 then
      begin
        ClientDataSet1.DisableControls;
        with frmOrderSubSel.ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            Self.CDS_JGC.Append;
            Self.CDS_JGC.FieldByName('SubId').Value:=frmOrderSubSel.ClientDataSet1.FieldByName('SubId').Value;
            for i:=0 to frmOrderSubSel.Tv3.ColumnCount-1 do
            begin
              FName:=frmOrderSubSel.Tv3.Columns[i].DataBinding.FilterFieldName;
              Self.CDS_JGC.FieldByName(FName).Value:=frmOrderSubSel.ClientDataSet1.FieldByName(FName).Value;

            end;
            Self.CDS_JGC.FieldByName('FactoryNo').Value:=frmOrderSubSel.ClientDataSet1.FieldByName('FactoryNo').Value;
            Self.CDS_JGC.Post;
            Next;
          end;
        end;
        ClientDataSet1.EnableControls;
      end;
    end;
  finally
  end;
end;

procedure TfrmOrderInPutPrice.ToolButton11Click(Sender: TObject);
begin
  if CDS_JGC.IsEmpty then Exit;
  ToolBar1.SetFocus;
  OneKeyPost(Tv3,CDS_JGC);
end;

procedure TfrmOrderInPutPrice.cxGridDBColumn3PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmGYSSelList:=TfrmGYSSelList.Create(Application);
    with frmGYSSelList do
    begin
      if ShowModal=1 then
      begin
        with CDS_JGC do
        begin
          edit;
          FieldByName('FactoryNo').Value:=Trim(frmGYSSelList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('FactoryName').Value:=Trim(frmGYSSelList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSSelList.Free;
  end;
end;

procedure TfrmOrderInPutPrice.ToolButton5Click(Sender: TObject);
begin
  if CDS_JGC.IsEmpty then Exit;
  if Trim(CDS_JGC.fieldbyname('PFId').AsString)<>'' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD where PFId='''+Trim(CDS_JGC.fieldbyname('PFId').AsString)+'''');
      Open;
    end;
    if ADOTemp.IsEmpty=false then
    begin
      Application.MessageBox('已经录入码单不能删除数据!','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_PCS_Sub where PFId='''+Trim(CDS_JGC.fieldbyname('PFId').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_JGC.Delete;
end;

procedure TfrmOrderInPutPrice.v2Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FName,FMF,FKZ:string;
begin
  {mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_JGLC do
  begin
    Edit;
    FieldByName(FName).Value:=Trim(mvalue);
    Post;
  end;
  FMF:=Trim(CDS_JGLC.fieldbyname('MFQty').AsString);
  FKZ:=Trim(CDS_JGLC.fieldbyname('KZQty').AsString);
  if Trim(FMF)='' then Exit;
  if Trim(FKZ)='' then Exit;
  if StrToFloat(FMF)=0 then Exit;
  if StrToFloat(FKZ)=0 then Exit;
  with CDS_JGLC do
  begin
    Edit;
    FieldByName('KgZM').Value:=100000*1.0000/(StrToFloat(FMF)*StrToFloat(FKZ));
    Post;
  end;}
end;

procedure TfrmOrderInPutPrice.ToolButton12Click(Sender: TObject);
var
  fsj:string;
begin
  if CDS_JGLC.IsEmpty then Exit;
  if FJSFlag=True then
  begin
    Application.MessageBox('结算完结不能操作数据!','提示',0);
    Exit;
  end;

  try
    frmTJCKKC:=TfrmTJCKKC.Create(Application);
    with frmTJCKKC do
    begin
      FFMainId:=Trim(Self.FMainId);
      if ShowModal=1 then
      begin
        fsj:='99';
        DataType:=Trim(frmTJCKKC.ComboBox1.Text);
        FDataType:=Trim(FST);
        with frmTJCKKC.Order_Sub do
        begin
          with Self.ClientDataSet5 do
          begin
            Append;
            FieldByName('FactoryName').Value:=frmTJCKKC.Order_Sub.fieldbyname('ToFactoryName').Value;
            FieldByName('PFTPUnit').Value:=frmTJCKKC.Order_Sub.fieldbyname('QtyUnit').Value;
            FieldByName('OrderNo').Value:=frmTJCKKC.Order_Sub.fieldbyname('OrderNo').Value;
            FieldByName('BatchNo').Value:=frmTJCKKC.Order_Sub.fieldbyname('BatchNo').Value;
            FieldByName('CPName').Value:=frmTJCKKC.Order_Sub.fieldbyname('ConName').Value;
            FieldByName('DiaoYongMDID').Value:=frmTJCKKC.Order_Sub.fieldbyname('MDID').Value;
            FieldByName('PFTPPS').Value:=frmTJCKKC.DRPS.Text;
            FieldByName('PFTPQty').Value:=frmTJCKKC.DRQty.Text;
            FieldByName('DataType').Value:=frmTJCKKC.ComboBox1.Text;
            if DataType='外围' then
            begin
              FieldByName('DiaoYongCKPS').Value:=frmTJCKKC.DRPS.Text;
              FieldByName('DiaoYongCKQty').Value:=frmTJCKKC.DRQty.Text;
            end;
            Post;
          end;
        end;
      end;
    end;
  finally
    frmTJCKKC.Free;
  end;
end;

procedure TfrmOrderInPutPrice.Tv5CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  PFID,FPFID,Fsj:String;
begin
  {if Trim(CanShu1)<>'高权限' then
  begin
    if Trim(ClientDataSet5.fieldbyname('PFID').AsString)<>'' then
    begin
      Tv5.OptionsSelection.CellSelect:=False;
    end else
    begin
      Tv5.OptionsSelection.CellSelect:=True;
    end;
  end; }

  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,C.OrderNo,D.C_Code,D.C_CodeName,B.FactoryName,B.RKQtyUnit from JYOrder_Main_MD_Out A ');
    sql.Add(' inner join JYOrder_Main_MD B on A.MDId=B.MDId');
    sql.Add(' inner join JYOrder_Main C on B.OrdMainId=C.MainId');
    sql.Add(' left join Contract_Sub D on B.ConSubId=D.SubId');
    sql.Add('where A.PFID='''+Trim(ClientDataSet5.fieldbyname('PFID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet6);
  SInitCDSData20(ADOQuery1,ClientDataSet6);

end;

procedure TfrmOrderInPutPrice.cxGridDBColumn1PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmGYSSelList:=TfrmGYSSelList.Create(Application);
    with frmGYSSelList do
    begin
      if ShowModal=1 then
      begin
        with ClientDataSet5 do
        begin
          edit;
          FieldByName('FactoryNo').Value:=Trim(frmGYSSelList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('FactoryName').Value:=Trim(frmGYSSelList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSSelList.Free;
  end;
 { try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with ClientDataSet5 do
        begin
          edit;
          FieldByName('FactoryNo').Value:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('FactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;  }
end;

procedure TfrmOrderInPutPrice.ToolButton9Click(Sender: TObject);
var
  maxno,maxsubno,Fstr:string;
begin
  if ClientDataSet5.IsEmpty then Exit;
  ToolBar1.SetFocus;
  if FJSFlag=True then
  begin
    Application.MessageBox('结算完结不能操作数据!','提示',0);
    Exit;
  end;
  if ClientDataSet5.Locate('FactoryName',Null,[]) then
  begin
    Application.MessageBox('供应商不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet5.Locate('XDFactoryName',Null,[]) then
  begin
    Application.MessageBox('下道供应商不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet5.Locate('XDFactoryName',Null,[]) then
  begin
    Application.MessageBox('下道供应商不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet5.Locate('PFTPPS',Null,[]) then
  begin
    Application.MessageBox('计划调入匹数不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet5.Locate('PFTPQty',Null,[]) then
  begin
    Application.MessageBox('计划调入数量不能为空!','提示',0);
    Exit;
  end;
  ClientDataSet5.DisableControls;
  with ClientDataSet5 do
  begin
    First;
    while not Eof do
    begin
      Fstr:=Copy(Trim(CDS_JGLC.fieldbyname('PSName').AsString),1,2);
      if (Fstr<>'坯') and (Fstr<>'纱') then
      begin
        if Trim(ClientDataSet5.fieldbyname('SubId').AsString)='' then
        begin
          Application.MessageBox('颜色或者花型号不能为空!','提示',0);
          Exit;
        end;

      end;
      Next;
    end;
  end;
  ClientDataSet5.EnableControls;
  if Application.MessageBox('确定要操作此数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOCmd.Connection.BeginTrans;
    if Trim(ClientDataSet5.fieldbyname('PFID').AsString)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'PF','JYOrder_PCS_Sub',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取流程加工厂流水号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(ClientDataSet5.fieldbyname('PFID').AsString);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_PCS_Sub where PFID='''+Trim(ClientDataSet5.fieldbyname('PFID').AsString)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(ClientDataSet5.fieldbyname('PFID').AsString)='' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      FieldByName('MainID').Value:=Trim(FMainId);
      FieldByName('PSID').Value:=Trim(CDS_JGLC.fieldbyname('PSID').AsString);
      FieldByName('PFID').Value:=Trim(maxno);
      FieldByName('FactoryName').Value:=Trim(ClientDataSet5.fieldbyname('FactoryName').AsString);
      FieldByName('XDFactoryName').Value:=Trim(ClientDataSet5.fieldbyname('XDFactoryName').AsString);
      FieldByName('PFDlyDate').Value:=ClientDataSet5.fieldbyname('PFDlyDate').Value;
      FieldByName('SubId').Value:=ClientDataSet5.fieldbyname('SubId').Value;
      FieldByName('DiaoYongMDID').Value:=ClientDataSet5.fieldbyname('DiaoYongMDID').Value;
      FieldByName('DataType').Value:=ClientDataSet5.fieldbyname('DataType').Value;
      RTSetSaveDataCDS(ADOCmd,Tv5,ClientDataSet5,'JYOrder_PCS_Sub',0);
      FieldByName('CKName').Value:='调节仓库';
      Post;
    end;
    if Trim(ClientDataSet5.fieldbyname('PFTPUnit').AsString)<>'M' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_PCS_Sub Set PFTPMQty=PFTPQty*');
        sql.Add('(select KgZM from JYOrder_Process A where A.PSID=JYOrder_PCS_Sub.PSID)');
        sql.Add(' where PFID='''+Trim(maxno)+'''');
        ExecSQL;
      end;
    end else
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_PCS_Sub Set PFTPMQty=PFTPQty');
        sql.Add(' where PFID='''+Trim(maxno)+'''');
        ExecSQL;
      end;
    end;
    with ClientDataSet5 do
    begin
      Edit;
      FieldByName('PFID').Value:=Trim(maxno);
      Post;
    end;
    if Trim(DataType)='外围' then
    begin
      SaveMDData(FMainId,Trim(ClientDataSet5.fieldbyname('SubId').AsString),
               Trim(CDS_JGLC.fieldbyname('PSID').AsString),Trim(maxno));
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JYOrder_Main_MD Set CKPS=(select isnull(Sum(isnull(DiaoYongCKPS,0)),0) from JYOrder_PCS_Sub A where isnull(A.DiaoYongMDID,'''')=JYOrder_Main_MD.MDID)');
      sql.Add(',CKQty=(select isnull(Sum(isnull(DiaoYongCKQty,0)),0) from JYOrder_PCS_Sub A where isnull(A.DiaoYongMDID,'''')=JYOrder_Main_MD.MDID)');
      sql.Add(' where MDID='''+Trim(ClientDataSet5.fieldbyname('DiaoYongMDID').AsString)+'''');
      ExecSQL;
    end;


    ADOCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功!','提示',0);
    Exit;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
    Exit;
  end;
end;
function TfrmOrderInPutPrice.SaveMDData(FOrdMainId,FOrdSubId,FPSID,FPFID:string):Boolean;
var
  maxno,MDID:string;
  BDInt,CPInt,JYInt:Integer;
begin
  Result:=False;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD where PFID='''+Trim(FPFID)+'''');
    sql.Add(' and MDType=''外围'' ');
    open;
  end;
  if ADOTemp.IsEmpty=False then
  begin
    MDID:=Trim(ADOTemp.fieldbyname('MDID').AsString);
  end else
  begin
    MDID:='';
  end;
  if Trim(MDID)='' then
  begin
    if GetLSNo(ADOCmd,maxno,'','JYOrder_Main_MD',3,1)=False then
    begin
      ADOCmd.Connection.RollbackTrans;
      Result:=False;
      Application.MessageBox('取码单流水号失败!!','提示',0);
      Exit;
    end;
  end else
  begin
    maxno:=Trim(MDID);
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(MDID)+'''');
    Open;
  end;
  with ADOCmd do
  begin
    if Trim(MDID)='' then
    begin
      Append;
      FieldByName('Filler').Value:=Trim(DName);
    end else
    begin
      Edit;
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditTIme').Value:=SGetServerDateTime(ADOQuery1);
    end;
    FieldByName('MDID').Value:=Trim(maxno);
    FieldByName('OrdMainID').Value:=Trim(FOrdMainId);
    FieldByName('OrdSubId').Value:=Trim(ClientDataSet5.fieldbyname('SubId').AsString);
    FieldByName('PSID').Value:=Trim(FPSID);
    FieldByName('PFID').Value:=Trim(FPFID);
    FieldByName('MDType').Value:=Trim(DataType);
    FieldByName('MDNO').Value:='DC'+Trim(maxno);
    FieldByName('FactoryName').Value:=Trim(ClientDataSet5.fieldbyname('FactoryName').AsString);
    FieldByName('FromFactoryName').Value:=Trim(ClientDataSet5.fieldbyname('XDFactoryName').AsString);
    FieldByName('PS').Value:=ClientDataSet5.fieldbyname('PFTPPS').Value;
    FieldByName('Qty').Value:=ClientDataSet5.fieldbyname('PFTPQty').Value;
    FieldByName('QtyUnit').Value:=ClientDataSet5.fieldbyname('PFTPUnit').Value;
    FieldByName('PSName').Value:=CDS_JGLC.fieldbyname('PSName').Value;
    if Trim(ClientDataSet5.fieldbyname('PFDlyDate').AsString)<>'' then
      FieldByName('MDDate').Value:=ClientDataSet5.fieldbyname('PFDlyDate').Value
    else
      FieldByName('MDDate').Value:=SGetServerDateTime(ADOQuery1);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_PCS_Sub Set DiaoYongCKPS=(select isnull(Sum(PS),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID )');
    sql.Add(',DiaoYongCKQty=(select isnull(Sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID )');
    sql.Add(' where PFID='''+Trim(FPFID)+'''');
    ExecSQL;
  end;
  if Trim(ClientDataSet5.fieldbyname('BatchNo').AsString)='' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main_MD Set ');
      sql.Add('ConName=(select ConName from JYOrder_Main_MD A where A.MDID='''+Trim(ClientDataSet5.fieldbyname('DiaoYongMDID').AsString)+''')');
      sql.Add(',ConSpec=(select ConSpec from JYOrder_Main_MD A where A.MDID='''+Trim(ClientDataSet5.fieldbyname('DiaoYongMDID').AsString)+''')');
      sql.Add(',ConCF=(select ConCF from JYOrder_Main_MD A where A.MDID='''+Trim(ClientDataSet5.fieldbyname('DiaoYongMDID').AsString)+''')');
      sql.Add(',ConMF=(select ConMF from JYOrder_Main_MD A where A.MDID='''+Trim(ClientDataSet5.fieldbyname('DiaoYongMDID').AsString)+''')');
      sql.Add(',ConKZ=(select ConKZ from JYOrder_Main_MD A where A.MDID='''+Trim(ClientDataSet5.fieldbyname('DiaoYongMDID').AsString)+''')');
      //sql.Add('ConPrice=(select ConName from JYOrder_Main_MD A where A.MDID='''+Trim(ClientDataSet5.fieldbyname('DiaoYongMDID').AsString)+''')');
      SQL.Add(' where MDID='''+Trim(maxno)+'''');
      ExecSQL;
    end;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main_MD Set ');
      sql.Add('ConName=(select Top 1 SPName from CK_SXPB_CR A where A.BatchNO='''+Trim(ClientDataSet5.fieldbyname('BatchNO').AsString)+''')');
      sql.Add(',ConSpec=(select Top 1 SPSpec from CK_SXPB_CR A where A.BatchNO='''+Trim(ClientDataSet5.fieldbyname('BatchNO').AsString)+''')');
      sql.Add(',ConCF=(select  Top 1 SPCF from CK_SXPB_CR A where A.BatchNO='''+Trim(ClientDataSet5.fieldbyname('BatchNO').AsString)+''')');
      sql.Add(',ConMF=(select  Top 1 SPMF from CK_SXPB_CR A where A.BatchNO='''+Trim(ClientDataSet5.fieldbyname('BatchNO').AsString)+''')');
      sql.Add(',ConKZ=(select  Top 1 SPKZ from CK_SXPB_CR A where A.BatchNO='''+Trim(ClientDataSet5.fieldbyname('BatchNO').AsString)+''')');
      sql.Add(',ConPrice=(select Top 1 CostPrice from CK_SXPB_CR A ');
      sql.Add('         where A.BatchNO='''+Trim(ClientDataSet5.fieldbyname('BatchNO').AsString)+''' order by CRTime desc)');
      SQL.Add(' where MDID='''+Trim(maxno)+'''');
      ExecSQL;
    end;
  end;
  Result:=True;

end;
function TfrmOrderInPutPrice.UpdateTJCKQty(FFPFID:string):Boolean;
var
  FAPQty,FMDQty,FPS,FQty:Double;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from JYOrder_Main_MD_Out where PFID='''+Trim(FFPFID)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    Result:=True;
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select distinct(MDID) MDID from JYOrder_Main_MD_Out where PFID='''+Trim(FFPFID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet9);
  SInitCDSData20(ADOQueryTemp,ClientDataSet9);
  with ClientDataSet9 do
  begin
    First;
    while not Eof do
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select isnull(sum(SJCKQty),0) Qty from JYOrder_Main_MD_Out where MDID='''+Trim(ClientDataSet9.fieldbyname('MDID').AsString)+'''');
        Open;
      end;
      FAPQty:=ADOTemp.fieldbyname('Qty').Value;
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select RKQty from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet9.fieldbyname('MDID').AsString)+'''');
        Open;
      end;
      FMDQty:=ADOTemp.fieldbyname('RKQty').Value;
      if FMDQty<FAPQty then
      begin
        Application.MessageBox('调用申请数量大于库存数量不能操作!','提示',0);
        Exit;
      end;
      Next;
    end;
  end;

  with ClientDataSet9 do
  begin
    First;
    while not Eof do
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select RKPS PS, RKQty Qty from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet9.fieldbyname('MDID').AsString)+'''');
        Open;
      end;
      FPS:=ADOQueryTemp.fieldbyname('PS').Value;
      FQty:=ADOQueryTemp.fieldbyname('Qty').Value;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrder_Main_MD_Out where MDID='''+Trim(ClientDataSet9.fieldbyname('MDID').AsString)+'''');
        Open;
      end;
      SCreateCDS20(ADOQueryTemp,ClientDataSet8);
      SInitCDSData20(ADOQueryTemp,ClientDataSet8);
      with ClientDataSet8 do
      begin
        First;
        while not Eof do
        begin
          if ClientDataSet8.FieldByName('CKQty').Value<=FQty then
          begin
            if ClientDataSet8.FieldByName('CKQty').Value=ClientDataSet8.FieldByName('SJCKQty').Value then
            begin
              with ADOCmd do
              begin
                Close;
                sql.Clear;
                sql.Add(' Update JYOrder_Main_MD_Out Set SJCKPS=CKPS,SJCKQty=CKQty ');
                sql.Add(' where CKID='''+Trim(ClientDataSet8.fieldbyname('CKID').AsString)+'''');
                ExecSQL;
              end;
              FPS:=FPS-ClientDataSet8.FieldByName('CKPS').Value;
              FQty:=FQty-ClientDataSet8.FieldByName('CKQty').Value;
            end else
            begin
              FPS:=FPS-ClientDataSet8.FieldByName('SJCKPS').Value;
              FQty:=FQty-ClientDataSet8.FieldByName('SJCKQty').Value;
            end;

            ClientDataSet8.Delete;
          end else
          begin
            with ADOCmd do
            begin
              Close;
              sql.Clear;
              sql.Add(' Update JYOrder_Main_MD_Out Set SJCKPS='+floattostr(FPS) );
              sql.Add(' ,SJCKQty='+floattostr(FQty) );
              sql.Add(' where CKID='''+Trim(ClientDataSet8.fieldbyname('CKID').AsString)+'''');
              ExecSQL;
            end;
            ClientDataSet8.Last;
          end;
        end;
      end;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_Main_MD Set CKPS=');
        sql.Add('(select isnull(sum(SJCKPS),0) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID) ');
        sql.Add(',CKQty=(select isnull(sum(SJCKQty),0) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID) ');
        sql.Add(' where MDID='''+Trim(ClientDataSet9.fieldbyname('MDID').AsString)+'''');
        ExecSQL;
      end;
      Next;
    end;
  end;
  Result:=True;
end;
function TfrmOrderInPutPrice.UpdateTJCKQtyOne(FFPFID:string):Boolean;
var
  FAPQty,FMDQty,FPS,FQty:Double;
  FMDID:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from JYOrder_Main_MD_Out where PFID='''+Trim(FFPFID)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    Result:=True;
    Exit;
  end;
  FMDID:=Trim(ADOQueryTemp.fieldbyname('MDID').AsString);
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(SJCKQty),0) Qty from JYOrder_Main_MD_Out where MDID='''+Trim(FMDID)+'''');
    Open;
  end;
  FAPQty:=ADOTemp.fieldbyname('Qty').Value;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select RKQty from JYOrder_Main_MD where MDID='''+Trim(FMDID)+'''');
    Open;
  end;
  FMDQty:=ADOTemp.fieldbyname('RKQty').Value;
  if FMDQty<FAPQty then
  begin
    Application.MessageBox('调用申请数量大于库存数量不能操作!','提示',0);
    Exit;
  end;
  //更新实际出库匹数和数量
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD_Out Set ');
    sql.Add('SJCKPS=(select isnull(sum(PS),0) from JYOrder_Main_MD A where A.PFID=JYOrder_Main_MD_Out.PFID and isnull(A.CKName,'''')<>''调节仓库'' ),');
    sql.Add('SJCKQty=(select isnull(sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_Main_MD_Out.PFID and isnull(A.CKName,'''')<>''调节仓库'' )');
    sql.Add(' where PFID='''+Trim(FFPFID)+'''');
    ExecSQL;
  end;
  //更新实际出库匹数和数量
  //更新原始TJ码单的出库匹数和数量
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD Set CKPS=');
    sql.Add('(select isnull(sum(SJCKPS),0) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID) ');
    sql.Add(',CKQty=(select isnull(sum(SJCKQty),0) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID) ');
    sql.Add(' where MDID='''+Trim(FMDID)+'''');
    ExecSQL;
  end;
  //更新原始TJ码单的出库匹数和数量
  Result:=True;
end;

procedure TfrmOrderInPutPrice.FormCreate(Sender: TObject);
begin
  CanShu1:=Trim(DParameters1);
  
end;

procedure TfrmOrderInPutPrice.Tv6CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  {if Trim(CanShu1)<>'高权限' then
  begin
    if Trim(ClientDataSet6.fieldbyname('CKID').AsString)<>'' then
    begin
      Tv6.OptionsSelection.CellSelect:=False;
    end;
  end; }
end;

procedure TfrmOrderInPutPrice.Tv5CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if ClientDataSet5.IsEmpty then Exit;
  if Trim(ClientDataSet5.fieldbyname('PFID').AsString)='' then
  begin
    ClientDataSet5.Delete;
  end;
end;

procedure TfrmOrderInPutPrice.v1Column2PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmLabelList:=TfrmLabelList.Create(Application);
    with frmLabelList do
    begin
      SLBName:=Trim(Order_Sub.fieldbyname('SLBName').AsString);
      if Self.JYInt=99 then
      begin
        frmLabelList.Tadd.Visible:=False;
        frmLabelList.Tupd.Visible:=False;
        frmLabelList.TOK.Visible:=False;
        frmLabelList.Tdel.Visible:=False;
      end;
      if ShowModal=1 then
      begin

        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('SLBName').Value:=Trim(LBName);
        end;
      end;
    end;
  finally
    frmLabelList.Free;
  end;
end;

procedure TfrmOrderInPutPrice.ComTaiTouBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GSTT';
      flagname:='公司抬头';
      V1ZdyStr1.Visible:=True;
      V1ZdyStr2.Visible:=True;
      V1ZdyStr3.Visible:=True;
      V1ZdyStr4.Visible:=True;
      V1ZdyStr1.Caption:='简称';
      V1ZdyStr2.Caption:='电话';
      V1ZdyStr3.Caption:='传真';
      V1ZdyStr4.Caption:='地址';
      frmZDYHelp.Align:=alClient;
      if ShowModal=1 then
      begin
        ComTaiTou.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutPrice.LiDanPersonBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='LiDanPerson';
      flagname:='业务助理';
      fnote:=True;
      V1Note.Caption:='电话';
      if ShowModal=1 then
      begin
        LiDanPerson.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutPrice.v3Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PFGenDanPerson';
      flagname:='跟单员';
      fnote:=True;
      V1Note.Caption:='电话';
      if ShowModal=1 then
      begin
        with CDS_JGC do
        begin
          Edit;
          FieldByName('PFGenDanPerson').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutPrice.ToolButton13Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='生产指示单';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmOrderInPutPrice.v3Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  FReal:Double;
  mvalue:string;
begin
  {if Trim(ClothOneQty.Text)<>'' then
  begin
    if TryStrToFloat(Trim(ClothOneQty.Text),FReal)=False then
    begin
      Application.MessageBox('白坯每匹数量非法数字!','提示',0);
      Exit;
    end;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  with CDS_JGC do
  begin
    Edit;
    FieldByName('PFTPPS').Value:=mvalue;
    FieldByName('PFTPQty').Value:=StrToFloat(mvalue)*StrToFloat(ClothOneQty.Text);
    Post;
  end;}
end;

procedure TfrmOrderInPutPrice.cxGridDBColumn32PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,JFPrice,JFQty:string;
begin
 
end;





procedure TfrmOrderInPutPrice.ToolButton18Click(Sender: TObject);
begin
  if Application.MessageBox('确定要重新连接吗？','提示',32+4)<>IDYES then Exit;
  if not Assigned(DataLink_DDMD) then
     DataLink_DDMD:=TDataLink_DDMD.Create(Application);
  Try
    with DataLink_DDMD.ADOLink do
    begin
      //if not Connected then
      begin
        Connected:=false;
        ConnectionString:=DConString;
        LoginPrompt:=false;
        Connected:=true;
      end;
    end;
  Except
    application.MessageBox('数据库连接失败！','错误',mb_Ok+ MB_ICONERROR);
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate  SY_MaxBH Set BHNo=BHNo+1 where BHType=''JYOrder_Sub'' ');
    sql.Add('UPdate  SY_MaxBH Set BHNo=BHNo+1 where BHType=''JYOrder_Main'' ');
    ExecSQL;
  end;
end;

procedure TfrmOrderInPutPrice.ToolButton19Click(Sender: TObject);
begin
  if ClientDataSet5.IsEmpty then Exit;
  if FJSFlag=True then
  begin
    Application.MessageBox('结算完结不能操作数据!','提示',0);
    Exit;
  end;
  if Trim(ClientDataSet5.fieldbyname('PFID').AsString)<>'' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD where PFID='''+Trim(ClientDataSet5.fieldbyname('PFID').AsString)+'''');
      sql.Add(' and (isnull(ConSubId,'''')<>'''' ');
      sql.Add(' or MDNO like ''MD%'') ');
      Open;
    end;
    if ADOTemp.IsEmpty=False then
    begin
      Application.MessageBox('数据已处理，不能删除!','提示',0);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD ');
      sql.Add(' where PFID='''+Trim(ClientDataSet5.fieldbyname('PFID').AsString)+'''');
      sql.Add(' and isnull(MDType,'''')=''实体'' ');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      Application.MessageBox('已录入出库码单，不能删除数据!','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete JYOrder_Main_MD where PFID='''+Trim(ClientDataSet5.fieldbyname('PFID').AsString)+'''');
        sql.Add('delete JYOrder_PCS_Sub where PFID='''+Trim(ClientDataSet5.fieldbyname('PFID').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate JYOrder_Main_MD Set CKPS=(select isnull(Sum(isnull(DiaoYongCKPS,0)),0) from JYOrder_PCS_Sub A where isnull(A.DiaoYongMDID,'''')=JYOrder_Main_MD.MDID)');
        sql.Add(',CKQty=(select isnull(Sum(isnull(DiaoYongCKQty,0)),0) from JYOrder_PCS_Sub A where isnull(A.DiaoYongMDID,'''')=JYOrder_Main_MD.MDID)');
        sql.Add(' where MDID='''+Trim(ClientDataSet5.fieldbyname('DiaoYongMDID').AsString)+'''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      ClientDataSet5.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
      Exit;
    end;

  end else
  begin
    ClientDataSet5.Delete;
  end;

end;

procedure TfrmOrderInPutPrice.v5Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
var
  FSubId,FFSubId:String;
begin
  FSubId:=Trim(ClientDataSet5.fieldbyname('SubId').AsString);
  if Trim(FSubId)<>'' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY A');
      sql.Add(' inner join JYOrder_Main_MD B on A.APID=B.MDID');
      sql.Add(' inner join JYOrder_PCS_Sub C on B.PFID=C.PFID');
      sql.Add(' where C.PFID='''+Trim(ClientDataSet5.fieldbyname('PFID').AsString)+'''');
      Open;
    end;
    if ADOTemp.IsEmpty=False then
    begin
       if Trim(CanShu1)<>'高权限' then
       begin
         Application.MessageBox('已有打卷数据不能更改色号!','提示',0);
         Exit;
       end;
       with ADOTemp do
       begin
         Close;
         sql.Clear;
         sql.Add('select * from JYOrder_FH where SubId='''+Trim(FSubId)+'''');
         Open;
       end;
       if ADOTemp.IsEmpty=False then
       begin
         Application.MessageBox('已有发货记录不能更改色号!','提示',0);
         Exit;
       end;
    end;

  end;
  try
    frmOrderSubSelMX:=TfrmOrderSubSelMX.Create(Application);
    with frmOrderSubSelMX do
    begin
      FFFMainId:=Trim(Self.FMainId);
      if ShowModal=1 then
      begin
        with Self.ClientDataSet5 do
        begin
          Edit;
          FieldByName('SubId').Value:=frmOrderSubSelMX.Order_Sub.fieldbyname('SubId').Value;
          FieldByName('PRTColor').Value:=frmOrderSubSelMX.Order_Sub.fieldbyname('PRTColor').AsString+' '+
                                         frmOrderSubSelMX.Order_Sub.fieldbyname('PRTColorNo').AsString;
          FieldByName('PRTHX').Value:=frmOrderSubSelMX.Order_Sub.fieldbyname('PRTHX').Value;
          //Post;
        end;
      end;
    end;
  finally
    frmOrderSubSelMX.Free;
  end;
  if Trim(ClientDataSet5.fieldbyname('PFID').AsString)<>'' then
  begin
    FFSubId:=Trim(ClientDataSet5.fieldbyname('SubId').AsString);
    if Trim(FSubId)<>Trim(FFSubId) then
    begin
      if Trim(CanShu1)='高权限' then
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_PCS_Sub Set SubId='''+Trim(FFSubId)+'''');
          sql.Add(' where PFID='''+Trim(ClientDataSet5.fieldbyname('PFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main_MD Set ORDSubId='''+Trim(FFSubId)+'''');
          sql.Add(' where PFID='''+Trim(ClientDataSet5.fieldbyname('PFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update WFB_MJJY Set SubId='''+Trim(FFSubId)+'''');
          sql.Add(' where APID in(select MDID from JYOrder_Main_MD A');
          sql.Add(' where A.PFID='''+Trim(ClientDataSet5.fieldbyname('PFID').AsString)+''')');
          ExecSQL;
        end;
      end;

    end;
  end;

end;

procedure TfrmOrderInPutPrice.ToolButton20Click(Sender: TObject);
begin
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='WFBCD';
      flagname:='疵点名称';
      fnote:=True;
      V1Note.Caption:='英文名称';
      if ShowModal=1 then
      begin
        with frmZDYHelpSel.ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if frmZDYHelpSel.ClientDataSet1.FieldByName('SSel').AsBoolean=True then
            begin
              with ClientDataSet8 do
              begin
                Append;
                FieldByName('ZDYCODE').Value:=Trim(frmZDYHelpSel.ClientDataSet1.fieldbyname('ZDYNo').AsString);
                FieldByName('ZdyName').Value:=Trim(frmZDYHelpSel.ClientDataSet1.fieldbyname('ZdyName').AsString);
                FieldByName('ZdyNameEng').Value:=Trim(frmZDYHelpSel.ClientDataSet1.fieldbyname('Note').AsString);
                Post;
              end;
            end;
            Next;
          end;
        end;

      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmOrderInPutPrice.ToolButton21Click(Sender: TObject);
begin
  if ClientDataSet8.IsEmpty then Exit;
  if Trim(ClientDataSet8.fieldbyname('JCID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('delete JYOrder_CD where JCID='''+Trim(ClientDataSet8.fieldbyname('JCID').AsString)+'''');
      ExecSQL;
    end;
  end;
  ClientDataSet8.Delete;
end;

procedure TfrmOrderInPutPrice.v5Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSSelList:=TfrmGYSSelList.Create(Application);
    with frmGYSSelList do
    begin
      if ShowModal=1 then
      begin
        with ClientDataSet5 do
        begin
          edit;
          FieldByName('XDFactoryNo').Value:=Trim(frmGYSSelList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('XDFactoryName').Value:=Trim(frmGYSSelList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSSelList.Free;
  end;
end;

procedure TfrmOrderInPutPrice.ToolButton23Click(Sender: TObject);
var
   fPrintFile:String;
begin
  if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
  begin
    Application.MessageBox('数据未保存不能预览标签!','提示',0);
    Exit;
  end;
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_Print_Lable_View :SubId,:ZSUnit,:DuoPinNote');
    Parameters.ParamByName('SubId').Value:=Trim(Order_Sub.fieldbyname('SubId').AsString);
    Parameters.ParamByName('ZSUnit').Value:='';
    Parameters.ParamByName('DuoPinNote').Value:='';
    Open;
  end;
  SCreateCDS20(ADOQueryPrint,CDS_Print);
  SInitCDSData20(ADOQueryPrint,CDS_Print);
  if Trim(Order_Sub.fieldbyname('SLBName').AsString)<>'' then
    fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\'+Trim(Order_Sub.fieldbyname('SLBName').AsString)
  else
    Exit;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
    //RM1.PrintReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\'+Trim(ADOQueryPrint.fieldbyname('SLBName').AsString)),'提示',0);
  end;
end;


procedure TfrmOrderInPutPrice.ConNoDblClick(Sender: TObject);
begin
  {try
    frmContractSelList:=TfrmContractSelList.Create(Application);
    with frmContractSelList do
    begin
      if ShowModal=1 then
      begin
        Self.ConNo.Text:=Trim(frmContractSelList.Order_Main.fieldbyname('ConNO').AsString);
        Self.ConSubId:=Trim(frmContractSelList.Order_Main.fieldbyname('SubId').AsString);
        Self.ConPrice:=Trim(frmContractSelList.Order_Main.fieldbyname('PRTPrice').AsString);
        if frmContractSelList.CheckBox1.Checked=True then
        begin
          Self.CustomerNoName.Text:=Trim(frmContractSelList.Order_Main.fieldbyname('CustomerNoName').AsString);
          Self.CustomerNoName.TxtCode:=Trim(frmContractSelList.Order_Main.fieldbyname('CustomerNo').AsString);
          Self.ComTaiTou.Text:=Trim(frmContractSelList.Order_Main.fieldbyname('SYRName').AsString);
          Self.MPRTCode.Text:=Trim(frmContractSelList.Order_Main.fieldbyname('PRTCode').AsString);
          Self.MPRTGY.Text:=Trim(frmContractSelList.Order_Main.fieldbyname('PRTGY').AsString);
          Self.MPRTCodeName.Text:=Trim(frmContractSelList.Order_Main.fieldbyname('PRTCodeName').AsString);
          Self.MPRTCF.Text:=Trim(frmContractSelList.Order_Main.fieldbyname('PRTCF').AsString);
          Self.MPRTMF.Text:=Trim(frmContractSelList.Order_Main.fieldbyname('PRTMF').AsString);
          Self.MPRTKZ.Text:=Trim(frmContractSelList.Order_Main.fieldbyname('PRTKZ').AsString);
          Self.MPRTGYHZ.Text:=Trim(frmContractSelList.Order_Main.fieldbyname('PRTGYHZ').AsString);
          Self.MPRTSpec.Text:=Trim(frmContractSelList.Order_Main.fieldbyname('PRTSpec').AsString);
        end;
      end;
    end;
  finally
    frmContractSelList.Free;
  end;  }
end;

procedure TfrmOrderInPutPrice.OrderNoChange(Sender: TObject);
begin
  Label33.Caption:=Trim(OrderNo.Text)+'  '+Trim(MPRTCodeName.Text);
end;

procedure TfrmOrderInPutPrice.MPRTCodeNameChange(Sender: TObject);
begin
  Label33.Caption:=Trim(OrderNo.Text)+'  '+Trim(MPRTCodeName.Text);
end;

procedure TfrmOrderInPutPrice.cxPageControl1Change(Sender: TObject);
begin
  Label33.Caption:=Trim(OrderNo.Text)+'  '+Trim(MPRTCodeName.Text);
end;

end.
