unit U_MDManage;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls, ToolWin, cxPC, cxControls,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxGridCustomView, cxGrid, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB,
  DBClient, cxCheckBox, cxButtonEdit;

type
  TfrmMDManage = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    Label10: TLabel;
    ConName: TEdit;
    PRTColorHZ: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    orderNo: TEdit;
    gangNo: TEdit;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3Column1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v3Column3: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    v3Column9: TcxGridDBColumn;
    v3Column10: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Label6: TLabel;
    MDNO: TEdit;
    Label7: TLabel;
    ConCode: TEdit;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu3: TcxGridPopupMenu;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQuery3: TADOQuery;
    v3Column12: TcxGridDBColumn;
    MDType: TComboBox;
    Label8: TLabel;
    PSName: TComboBox;
    Label9: TLabel;
    v3Column13: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    v3Column14: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    Panel3: TPanel;
    Label13: TLabel;
    cxGrid71: TcxGrid;
    Tv71: TcxGridDBTableView;
    v2Column2: TcxGridDBColumn;
    V2XH: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Panel7: TPanel;
    Label23: TLabel;
    cxGrid72: TcxGrid;
    Tv72: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1SOrddefstr1: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel2: TPanel;
    Label21: TLabel;
    cxGrid77: TcxGrid;
    Tv77: TcxGridDBTableView;
    v5Column8: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    v5Column6: TcxGridDBColumn;
    v5Column2: TcxGridDBColumn;
    v5Column7: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    v5Column3: TcxGridDBColumn;
    v5Column4: TcxGridDBColumn;
    v5Column5: TcxGridDBColumn;
    v5Column10: TcxGridDBColumn;
    v5Column1: TcxGridDBColumn;
    v5Column9: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    Panel4: TPanel;
    Button1: TButton;
    Button2: TButton;
    DataSource71: TDataSource;
    ClientDataSet71: TClientDataSet;
    cxGridPopupMenu71: TcxGridPopupMenu;
    DataSource72: TDataSource;
    ClientDataSet72: TClientDataSet;
    cxGridPopupMenu72: TcxGridPopupMenu;
    DataSource77: TDataSource;
    ClientDataSet77: TClientDataSet;
    cxGridPopupMenu77: TcxGridPopupMenu;
    Label11: TLabel;
    Edit1: TEdit;
    cxGrid6: TcxGrid;
    TvOrdeNo: TcxGridDBTableView;
    VOrderNo: TcxGridDBColumn;
    cxGrid4Level1: TcxGridLevel;
    CDS_OrderNo: TClientDataSet;
    DS_OrderNo: TDataSource;
    v77Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v3Column15: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v3Column16: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v3Column17: TcxGridDBColumn;
    v3Column18: TcxGridDBColumn;
    ClientDataSet8: TClientDataSet;
    v1Column13: TcxGridDBColumn;
    ClientDataSet9: TClientDataSet;
    ADOTemp: TADOQuery;
    v3Column19: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    v3Column20: TcxGridDBColumn;
    v3Column21: TcxGridDBColumn;
    v3Column22: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure MDNOKeyPress(Sender: TObject; var Key: Char);
    procedure TBExportClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv3Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure v5Column8PropertiesEditValueChanged(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure TvOrdeNoDblClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column13PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v3Column14PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column1PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    canshu1,canshu2,FMainId,FOrderNo:string;
    procedure UPMDWCdata(FQtyUnit,FPFID:String);
    function UpdateTJCKQty10(FFPFID:string):Boolean;
    function UpdateTJCKQty(FFPFID:string):Boolean;
  public
    { Public declarations }
  end;

var
  frmMDManage: TfrmMDManage;

implementation
uses
  U_DataLink,U_RTFun, U_GYSList,U_ZDYHelp, U_ModuleNote;

{$R *.dfm}

procedure TfrmMDManage.FormDestroy(Sender: TObject);
begin
  frmMDManage:=nil;
end;

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

procedure TfrmMDManage.TBRafreshClick(Sender: TObject);
var
  fsj:string;
begin
  try
    ADOQuery3.DisableControls;
    with ADOQuery3 do
    begin
      Close;
      Filtered:=False;
      sql.Clear;
      if CheckBox1.Checked=False then
        sql.Add('exec P_View_MD :begdate,:enddate,:orderno,:MDType,:WSql')
      else
        sql.Add('exec P_View_MD_NoCon :begdate,:enddate,:orderno,:MDType,:WSql');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date));
      Parameters.ParamByName('enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',enddate.Date+1));
      Parameters.ParamByName('orderno').Value:='';
      Parameters.ParamByName('MDType').Value:='';
      if cxPageControl1.ActivePageIndex=0 then
      begin
        fsj:=' and isnull(JMM.CKName,'''')='''' ';
      end else
      begin
        fsj:=' and isnull(JMM.CKName,'''')<>'''' ';
      end;
      Parameters.ParamByName('WSql').Value:=fsj;
      Open;
    end;
    SCreateCDS20(ADOQuery3,ClientDataSet3);
    SInitCDSData20(ADOQuery3,ClientDataSet3);
  finally
    ADOQuery3.EnableControls;
  end;
  TBFind.Click;
end;

procedure TfrmMDManage.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-7;
  cxPageControl1.ActivePageIndex:=0;
end;

procedure TfrmMDManage.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('正常码单管理',Tv3,'码单管理');
  WriteCxGrid('调节仓库码单管理',Tv1,'码单管理');
  Close;
end;

procedure TfrmMDManage.cxPageControl1Change(Sender: TObject);
begin
  TBRafresh.Click;
end;

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

procedure TfrmMDManage.orderNoKeyPress(Sender: TObject; var Key: Char);
var
  fsj:string;
begin
  if Key=#13 then
  begin
    if Trim(orderNo.Text)='' then Exit;
    if Length(Trim(orderNo.Text))<4 then Exit;
    try
      ADOQuery3.DisableControls;
      with ADOQuery3 do
      begin
        Close;
        Filtered:=False;
        sql.Clear;
        if CheckBox1.Checked=False then
          sql.Add('exec P_View_MD :begdate,:enddate,:orderno,:MDType,:WSql')
        else
          sql.Add('exec P_View_MD_NoCon :begdate,:enddate,:orderno,:MDType,:WSql');
        Parameters.ParamByName('begdate').Value:='';
        Parameters.ParamByName('enddate').Value:='';
        Parameters.ParamByName('orderno').Value:=Trim(orderNo.Text);
        Parameters.ParamByName('MDType').Value:='';
        if cxPageControl1.ActivePageIndex=0 then
        begin
          fsj:=' and isnull(JMM.CKName,'''')='''' ';
        end else
        begin
          fsj:=' and isnull(JMM.CKName,'''')<>'''' ';
        end;
        {if Trim(canshu2)<>'高权限' then
        begin
          fsj:=fsj+' and JMM.Filler='''+Trim(DName)+'''';
        end; }
        Parameters.ParamByName('WSql').Value:=fsj;
        Open;
      end;
      SCreateCDS20(ADOQuery3,ClientDataSet3);
      SInitCDSData20(ADOQuery3,ClientDataSet3);
    finally
      ADOQuery3.EnableControls;
    end;
    
  end;

end;

procedure TfrmMDManage.MDNOKeyPress(Sender: TObject; var Key: Char);
var
  fsj:string;
begin
  if Key=#13 then
  begin
    if Trim(MDNO.Text)='' then Exit;
    if Length(Trim(MDNO.Text))<4 then Exit;
    try
      ADOQuery3.DisableControls;
      with ADOQuery3 do
      begin
        Close;
        Filtered:=False;
        sql.Clear;
        if CheckBox1.Checked=False then
          sql.Add('exec P_View_MD :begdate,:enddate,:orderno,:MDType,:WSql')
        else
          sql.Add('exec P_View_MD_NoCon :begdate,:enddate,:orderno,:MDType,:WSql');
        Parameters.ParamByName('begdate').Value:='';
        Parameters.ParamByName('enddate').Value:='';
        Parameters.ParamByName('orderno').Value:='';
        Parameters.ParamByName('MDType').Value:=Trim(canshu1);
        fsj:=' and JMM.MDNO like '''+'%'+Trim(MDNO.Text)+'%'+'''';
        if cxPageControl1.ActivePageIndex=0 then
        begin
          fsj:=fsj+' and isnull(JMM.CKName,'''')='''' ';
        end else
        begin
          fsj:=fsj+' and isnull(JMM.CKName,'''')<>'''' ';
        end;
        {if Trim(canshu2)<>'高权限' then
        begin
          fsj:=fsj+' and JMM.Filler='''+Trim(DName)+'''';
        end; }
        Parameters.ParamByName('WSql').Value:=fsj;
        Open;
      end;
      SCreateCDS20(ADOQuery3,ClientDataSet3);
      SInitCDSData20(ADOQuery3,ClientDataSet3);
    finally
      ADOQuery3.EnableControls;
    end;
  end;

end;

procedure TfrmMDManage.TBExportClick(Sender: TObject);
begin
  if ClientDataSet3.IsEmpty then Exit;
  if cxPageControl1.ActivePageIndex=0 then
    TcxGridToExcel('正常码单',cxGrid3)
  else
    TcxGridToExcel('调节仓库码单',cxGrid1);
end;

procedure TfrmMDManage.FormShow(Sender: TObject);
begin
  READCxGrid('正常码单管理',Tv3,'码单管理');
  READCxGrid('调节仓库码单管理',Tv1,'码单管理');
end;

procedure TfrmMDManage.ToolButton4Click(Sender: TObject);
begin
  if ClientDataSet3.IsEmpty then Exit;
  if ClientDataSet3.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  ToolBar1.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet3.DisableControls;
    with ClientDataSet3 do
    begin
      while ClientDataSet3.Locate('SSel',True,[])=True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Edit;
          FieldByName('Editer').Value:=Trim(DName);
          FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          if cxPageControl1.ActivePageIndex=0 then
            RTSetSaveDataCDS(ADOQueryCmd,Tv3,ClientDataSet3,'JYOrder_Main_MD',99)
          else
            RTSetSaveDataCDS(ADOQueryCmd,Tv1,ClientDataSet3,'JYOrder_Main_MD',99);
          FieldByName('FromFactoryNo').Value:=Trim(ClientDataSet3.fieldbyname('FromFactoryNo').AsString);
          if ClientDataSet3.FieldByName('OtherOrdFlag').AsBoolean=True then
          begin
            FieldByName('OtherOrdFlag').Value:=1;
          end else
          begin
            FieldByName('OtherOrdFlag').Value:=0;
          end;
          if ClientDataSet3.FieldByName('HXFlag').AsBoolean=True then
          begin
            FieldByName('HXFlag').Value:=1;
          end else
          begin
            FieldByName('HXFlag').Value:=0;
          end;
          if ClientDataSet3.FieldByName('CiPinFlag').AsBoolean=True then
          begin
            FieldByName('CiPinFlag').Value:=1;
          end else
          begin
            FieldByName('CiPinFlag').Value:=0;
          end;
          FieldByName('RKDate').Value:=ClientDataSet3.fieldbyname('MDDate').Value;
          FieldByName('RKPS').Value:=ClientDataSet3.fieldbyname('PS').Value;
          FieldByName('RKQty').Value:=ClientDataSet3.fieldbyname('Qty').Value;
          FieldByName('RKQtyUnit').Value:=ClientDataSet3.fieldbyname('QtyUnit').Value;
          Post;
        end;
       
        if cxPageControl1.ActivePageIndex=0 then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from JYOrder_PCS_Sub where PFID='''+Trim(ClientDataSet3.fieldbyname('PFID').AsString)+'''');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            if Trim(ADOQueryTemp.fieldbyname('CKName').AsString)<>'' then
            begin
              with ADOQueryCmd do
              begin
                Close;
                sql.Clear;
                sql.Add('Update JYOrder_PCS_Sub Set DiaoYongCKPS=(select Sum(PS) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID )');
                sql.Add(',DiaoYongCKQty=(select Sum(Qty) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID )');
                sql.Add(' where PFID='''+Trim(ClientDataSet3.fieldbyname('PFID').AsString)+'''');
                ExecSQL;
              end;
              if Trim(ADOQueryTemp.fieldbyname('DiaoYongMDID').AsString)<>'' then
              begin
                with ADOQueryCmd do
                begin
                  Close;
                  sql.Clear;
                  sql.Add('UPdate JYOrder_Main_MD Set CKPS=(select Sum(DiaoYongCKPS) from JYOrder_PCS_Sub A where isnull(A.DiaoYongMDID,'''')=JYOrder_Main_MD.MDID)');
                  sql.Add(',CKQty=(select Sum(DiaoYongCKQty) from JYOrder_PCS_Sub A where isnull(A.DiaoYongMDID,'''')=JYOrder_Main_MD.MDID)');
                  sql.Add(' where MDID='''+Trim(ADOQueryTemp.fieldbyname('DiaoYongMDID').AsString)+'''');
                  ExecSQL;
                end;
              end;

            end;
          end;
          {if UpdateTJCKQty(Trim(ClientDataSet3.fieldbyname('PFID').AsString))=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet3.EnableControls;
            Exit;
          end; }
        end;
        {if  UpdateTJCKQty(Trim(ClientDataSet3.fieldbyname('PFID').AsString)) then
        begin

        end else
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          //Application.MessageBox('操作失败!','提示',0);
          exit;
        end;}
        Edit;
        FieldByName('SSel').Value:=False;
        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet3.EnableControls;
    Application.MessageBox('保存成功!','提示',0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    ClientDataSet3.EnableControls;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;
function TfrmMDManage.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 * from JYOrder_PCS_Sub where PFID='''+Trim(FFPFID)+'''');
    Open;
  end;
  FAPQty:=ADOQueryTemp.fieldbyname('PFTPQty').Value;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(Qty),0) MDQty from JYOrder_Main_MD where PFID='''+Trim(FFPFID)+'''');
    sql.Add(' and isnull(CKName,'''')<>''调节仓库'' ');
    Open;
  end;
  FMDQty:=ADOQueryTemp.fieldbyname('MDQty').Value;
  if FMDQty>FAPQty then
  begin
    if Application.MessageBox('出库数量大于调用申请数量,确定要录入码单吗？','提示',32+4)<>IDYES then
    begin
      Result:=False;
      Exit;
    end;
  end;  
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(PS),0) PS,isnull(Sum(Qty),0) Qty from JYOrder_Main_MD where PFID='''+Trim(FFPFID)+'''');
    sql.Add(' and isnull(CKName,'''')<>''调节仓库''');
    Open;
  end;
  FPS:=ADOQueryTemp.fieldbyname('PS').Value;
  FQty:=ADOQueryTemp.fieldbyname('Qty').Value;
  if FQty=0 then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main_MD_Out Set SJCKPS=0,SJCKQty=0');
      sql.Add(' where PFID='''+Trim(FFPFID)+'''');
      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 in');
      sql.Add('(select MDID from JYOrder_Main_MD_Out JMO where JMO.PFID='''+Trim(FFPFID)+''')');
      ExecSQL;
    end;
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD_Out Set SJCKPS=0,SJCKQty=0');
    sql.Add(' where PFID='''+Trim(FFPFID)+'''');
    ExecSQL;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_Out where PFID='''+Trim(FFPFID)+'''');
    sql.Add(' order by CKQty');
    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.RecordCount>1 then
        begin
          with ADOQueryCmd 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;
          ClientDataSet8.Delete;
        end else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' Update JYOrder_Main_MD_Out Set SJCKPS='+floattostr(FPS)+',SJCKQty='+floattostr(FQty));
            sql.Add(' where CKID='''+Trim(ClientDataSet8.fieldbyname('CKID').AsString)+'''');
            ExecSQL;
          end;
          FPS:=0;
          FQty:=0;
          ClientDataSet8.Delete;
        end;

      end else
      begin
        with ADOQueryCmd 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 ADOQueryCmd 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 in');
    sql.Add('(select MDID from JYOrder_Main_MD_Out JMO where JMO.PFID='''+Trim(FFPFID)+''')');
    ExecSQL;
  end;
  Result:=True;
end;
function TfrmMDManage.UpdateTJCKQty10(FFPFID:string):Boolean;
var
  FAPQty,FMDQty,FPS,FQty:Double;
begin
  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
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_PCS_Sub where PFID='''+Trim(FFPFID)+'''');
    Open;
  end;
  FAPQty:=ADOQueryTemp.fieldbyname('PFTPQty').Value;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(Qty),0) MDQty from JYOrder_Main_MD where PFID='''+Trim(FFPFID)+'''');
    sql.Add(' and isnull(CKName,'''')<>''调节仓库'' ');
    Open;
  end;
  FMDQty:=ADOQueryTemp.fieldbyname('MDQty').Value;
  if FMDQty>FAPQty then
  begin
    Application.MessageBox('出库数量大于调用申请数量不能录入码单!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(PS),0) PS,isnull(Sum(Qty),0) Qty from JYOrder_Main_MD where PFID='''+Trim(FFPFID)+'''');
    sql.Add(' and isnull(CKName,'''')<>''调节仓库''');
    Open;
  end;
  FPS:=ADOQueryTemp.fieldbyname('PS').Value;
  FQty:=ADOQueryTemp.fieldbyname('Qty').Value;
  if FQty=0 then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main_MD_Out Set SJCKPS=0,SJCKQty=0');
      sql.Add(' where PFID='''+Trim(FFPFID)+'''');
      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 in');
      sql.Add('(select MDID from JYOrder_Main_MD_Out JMO where JMO.PFID='''+Trim(FFPFID)+''')');
      ExecSQL;
    end;
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD_Out Set SJCKPS=0,SJCKQty=0');
    sql.Add(' where PFID='''+Trim(FFPFID)+'''');
    ExecSQL;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_Out where PFID='''+Trim(FFPFID)+'''');
    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
        with ADOQueryCmd 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;
        ClientDataSet8.Delete;
      end else
      begin
        with ADOQueryCmd 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 ADOQueryCmd 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 in');
    sql.Add('(select MDID from JYOrder_Main_MD_Out JMO where JMO.PFID='''+Trim(FFPFID)+''')');
    ExecSQL;
  end;
end;
procedure TfrmMDManage.Tv3MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  fsj:string;
begin
  if ClientDataSet3.IsEmpty then Exit;
  fsj:='select Code=FactoryNo,Name=FactoryName from JYOrder_PCS_Sub where MainId='''+Trim(ClientDataSet3.fieldbyname('ORDMainId').AsString)+''''+
       ' and isnull(FactoryNo,'''')<>'''' group by FactoryNo,FactoryName';
  SInitCxGridComboBoxBySql(ADOQuery1,Tv3Column1,fsj,1,True,'');
  fsj:='select PSName Name, Cast('''' as varchar(20)) code from JYOrder_Process '+
       ' where Mainid='''+Trim(ClientDataSet3.fieldbyname('ORDMainId').AsString)+''' order by PSXH';
  SInitComBoxBySql(ADOQueryTemp,PSName,False,fsj);
end;

procedure TfrmMDManage.Tv3Column1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,fsj:string;
begin
  mvalue:=TcxComboBox(Sender).EditingText;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName('FromFactoryName').Value:=Trim(mvalue);
    fsj:=TA((Tv3Column1.Properties as TcxComboBoxProperties).Items.Objects[(Tv3Column1.Properties as TcxComboBoxProperties).Items.IndexOf(Trim(mvalue))]).S;
    FieldByName('FromFactoryNo').Value:=Trim(fsj);
    Post;
  end;

end;

procedure TfrmMDManage.ToolButton1Click(Sender: TObject);
begin
  if ClientDataSet3.IsEmpty then Exit;
  if ClientDataSet3.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet3.DisableControls;
    with ClientDataSet3 do
    begin
      while Locate('SSel',True,[])=true do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from WFB_MJJY where APID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          ClientDataSet3.EnableControls;
          Application.MessageBox('已检验不能删除数据!','提示',0);
          Exit;
        end;
        if cxPageControl1.ActivePageIndex=1 then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select isnull(CKQty,0) from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
            Open;
          end;
          if ADOQueryTemp.FieldByName('CKQty').Value<>0 then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet3.EnableControls;
            Application.MessageBox('已有调用数据不能删除数据!','提示',0);
            Exit;
          end;
        end ;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete JYOrder_Main_MD where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
          ExecSQL;
        end;

        if cxPageControl1.ActivePageIndex=0 then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from JYOrder_PCS_Sub where PFID='''+Trim(ClientDataSet3.fieldbyname('PFID').AsString)+'''');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            if Trim(ADOQueryTemp.fieldbyname('CKName').AsString)<>'' then
            begin
              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(ClientDataSet3.fieldbyname('PFID').AsString)+'''');
                ExecSQL;
              end;
              if Trim(ADOQueryTemp.fieldbyname('DiaoYongMDID').AsString)<>'' then
              begin
                with ADOQueryCmd do
                begin
                  Close;
                  sql.Clear;
                  sql.Add('UPdate JYOrder_Main_MD Set CKPS=(select isnull(Sum(DiaoYongCKPS),0) from JYOrder_PCS_Sub A where isnull(A.DiaoYongMDID,'''')=JYOrder_Main_MD.MDID)');
                  sql.Add(',CKQty=(select isnull(Sum(DiaoYongCKQty),0) from JYOrder_PCS_Sub A where isnull(A.DiaoYongMDID,'''')=JYOrder_Main_MD.MDID)');
                  sql.Add(' where MDID='''+Trim(ADOQueryTemp.fieldbyname('DiaoYongMDID').AsString)+'''');
                  ExecSQL;
                end;
              end;

            end;
          end;
        end;
        ClientDataSet3.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet3.EnableControls;
    Application.MessageBox('删除成功!','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    ClientDataSet3.EnableControls;
    Application.MessageBox('删除异常!','提示',0);
  end;

end;

procedure TfrmMDManage.Tv1Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='DutyReason';
      flagname:='入库原因';
      if ShowModal=1 then
      begin
        with Self.ClientDataSet3 do
        begin
          Edit;
          FieldByName('DutyReason').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMDManage.Tv1Column4PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.ClientDataSet3 do
        begin
          Edit;
          FieldByName('ToFactoryNo').Value:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('ToFactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmMDManage.Tv1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  fsj:string;
begin
  if ClientDataSet3.IsEmpty then Exit;
  fsj:='select Code=FactoryNo,Name=FactoryName from JYOrder_PCS_Sub where MainId='''+Trim(ClientDataSet3.fieldbyname('ORDMainId').AsString)+''''+
       ' and isnull(FactoryNo,'''')<>'''' group by FactoryNo,FactoryName';
  SInitCxGridComboBoxBySql(ADOQuery1,Tv1Column2,fsj,1,True,'');
  fsj:='select Code=PFGenDanPerson,Name=PFGenDanPerson from JYOrder_PCS_Sub where MainId='''+Trim(ClientDataSet3.fieldbyname('ORDMainId').AsString)+''''+
       ' and isnull(PFGenDanPerson,'''')<>'''' group by PFGenDanPerson';
  SInitCxGridComboBoxBySql(ADOQuery1,Tv1Column1,fsj,1,True,'');
  fsj:='select PSName Name, Cast('''' as varchar(20)) code from JYOrder_Process '+
       'where Mainid='''+Trim(ClientDataSet3.fieldbyname('ORDMainId').AsString)+''' order by PSXH';
  SInitComBoxBySql(ADOQueryTemp,PSName,False,fsj);
end;

procedure TfrmMDManage.Tv1Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,fsj:string;
begin
  mvalue:=TcxComboBox(Sender).EditingText;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName('DutyFactoryName').Value:=Trim(mvalue);
    fsj:=TA((Tv1Column2.Properties as TcxComboBoxProperties).Items.Objects[(Tv1Column2.Properties as TcxComboBoxProperties).Items.IndexOf(Trim(mvalue))]).S;
    FieldByName('DutyFactoryNo').Value:=Trim(fsj);
    Post;
  end;

end;

procedure TfrmMDManage.ToolButton3Click(Sender: TObject);
begin
  ScrollBox1.Visible:=True;
  ScrollBox1.Refresh;
  
  
end;

procedure TfrmMDManage.Button2Click(Sender: TObject);
begin
  ScrollBox1.Visible:=False;
end;

procedure TfrmMDManage.v2Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  PSID,ConType:String;
  Mvalue:Boolean;
begin
  Mvalue:=TcxCheckBox(Sender).EditingValue;
  PSID:=Trim(ClientDataSet71.fieldbyname('PSID').AsString);
  with ClientDataSet71 do
  begin
    while  ClientDataSet71.Locate('SSel',True,[])=True do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
    end;
    ClientDataSet71.Locate('PSID',PSID,[]);
    Edit;
    FieldByName('SSel').Value:=Mvalue;
    Post;
  end;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.PRTHX,B.PRTColorNo,B.PRTColor,B.PRTOrdQty,B.OrderUnit,B.ColorNote,JP.PSName  ');
    sql.Add(',Case when A.PFTPMQty<>0 then Cast((A.WCMQty-A.TJMQty)*100*1.00/A.PFTPMQty as decimal(18,2)) else 0 end as WCL');
    sql.Add(' from JYOrder_PCS_Sub A');
    sql.Add(' inner join JYOrder_Process JP on A.PSID=JP.PSID');
    sql.Add(' left join JYOrder_Sub B on A.SubId=B.SubId');
    if Mvalue=True then
      sql.Add('where A.PSID='''+Trim(ClientDataSet71.fieldbyname('PSID').AsString)+'''')
    else
      sql.Add(' where A.MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet72);
  SInitCDSData20(ADOQuery1,ClientDataSet72);
  if Mvalue=True then
  begin
    ClientDataSet71.Locate('SSel',True,[]);
    ConType:=Trim(ClientDataSet71.fieldbyname('PSName').AsString);
  end else
  begin
    ConType:='';
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
    sql.Add(' where A.ConNo like '''+'%'+Trim(FOrderNo)+'%'+'''');
    if Trim(ConType)<>'' then
    begin
      sql.Add(' and A.ConType='''+Trim(ConType)+'''');
    end;
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet77);
  SInitCDSData20(ADOQueryTemp,ClientDataSet77);
end;

procedure TfrmMDManage.v1Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  PFID:String;
  Mvalue:Boolean;
begin
  Mvalue:=TcxCheckBox(Sender).EditingValue;
  PFID:=Trim(ClientDataSet72.fieldbyname('PFID').AsString);
  with ClientDataSet72 do
  begin
    while  ClientDataSet72.Locate('SSel',True,[])=True do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
    end;
    ClientDataSet72.Locate('PFID',PFID,[]);
    Edit;
    FieldByName('SSel').Value:=Mvalue;
    Post;
  end;
  if Mvalue=True then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
      sql.Add(' where A.ConNo like '''+'%'+Trim(FOrderNo)+'%'+'''');
      sql.Add(' and A.ConType='''+Trim(ClientDataSet72.fieldbyname('PSName').AsString)+'''');
      sql.Add(' and A.FactoryNoName='''+Trim(ClientDataSet72.fieldbyname('FactoryName').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet77);
    SInitCDSData20(ADOQueryTemp,ClientDataSet77);
  end else
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
      sql.Add(' where A.ConNo like '''+'%'+Trim(FOrderNo)+'%'+'''');
      sql.Add(' and A.ConType='''+Trim(ClientDataSet72.fieldbyname('PSName').AsString)+'''');
      //sql.Add(' and A.FactoryNoName='''+Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet77);
    SInitCDSData20(ADOQueryTemp,ClientDataSet77);
  end;
end;

procedure TfrmMDManage.v5Column8PropertiesEditValueChanged(
  Sender: TObject);
var
  PSID:String;
  Mvalue:Boolean;
begin
  Mvalue:=TcxCheckBox(Sender).EditingValue;
  PSID:=Trim(ClientDataSet77.fieldbyname('SubID').AsString);
  with ClientDataSet77 do
  begin
    while  ClientDataSet77.Locate('SSel',True,[])=True do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
    end;
    ClientDataSet77.Locate('SubID',PSID,[]);
    Edit;
    FieldByName('SSel').Value:=Mvalue;
    Post;
  end;
end;

procedure TfrmMDManage.Button1Click(Sender: TObject);
var
  FQtyUnit,FPFID:String;
begin
  if ClientDataSet3.IsEmpty then Exit;
  if ClientDataSet3.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if ClientDataSet71.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择生产流程!','提示',0);
    Exit;
  end;
  if ClientDataSet72.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择安排信息!','提示',0);
    Exit;
  end;
  if ClientDataSet77.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择加工合同!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  ClientDataSet71.Locate('SSel',True,[]);
  ClientDataSet72.Locate('SSel',True,[]);
  ClientDataSet77.Locate('SSel',True,[]);
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet3.DisableControls;
    with ClientDataSet3 do
    begin
      while  Locate('SSel',True,[])=True do
      begin
        FQtyUnit:=Trim(ClientDataSet3.fieldbyname('QtyUnit').AsString);
        FPFID:=Trim(ClientDataSet3.fieldbyname('PFID').AsString);
        {if ClientDataSet3.FieldByName('RKQty').AsFloat>0 then
        begin
          ClientDataSet3.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已入库不能重新指定!','',0);
          Exit;
        end;}
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('UPdate JYOrder_Main_MD Set Editer='''+Trim(DName)+''',EditTime=getdate(),');
          sql.Add(' ORDMainId='''+Trim(ClientDataSet72.fieldbyname('MainId').AsString)+'''');
          sql.Add(' ,ORDSubId='''+Trim(ClientDataSet72.fieldbyname('SubId').AsString)+'''');
          sql.Add(' ,PSId='''+Trim(ClientDataSet72.fieldbyname('PSId').AsString)+'''');
          sql.Add(' ,PFId='''+Trim(ClientDataSet72.fieldbyname('PFId').AsString)+'''');
          sql.Add(' ,ConMainId='''+Trim(ClientDataSet77.fieldbyname('MainId').AsString)+'''');
          sql.Add(' ,ConSubId='''+Trim(ClientDataSet77.fieldbyname('SubId').AsString)+'''');
          SQL.Add(' ,PSName='''+Trim(ClientDataSet71.fieldbyname('PSName').AsString)+'''');
          SQL.Add(' ,FactoryNo='''+Trim(ClientDataSet77.fieldbyname('FactoryNo').AsString)+'''');
          SQL.Add(' ,FactoryName='''+Trim(ClientDataSet77.fieldbyname('FactoryNoName').AsString)+'''');
          sql.Add(' where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
          ExecSQL;
        end;
        UPMDWCdata(FQtyUnit,FPFID);
        UPMDWCdata(FQtyUnit,Trim(ClientDataSet72.fieldbyname('PFId').AsString));
        if cxPageControl1.ActivePageIndex=0 then
          UpdateTJCKQty(FPFID);
        Edit;
        FieldByName('SSel').Value:=false;
        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet3.EnableControls;
    Application.MessageBox('操作成功!','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    ClientDataSet3.EnableControls;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;
procedure TfrmMDManage.UPMDWCdata(FQtyUnit,FPFID:String);
begin
  if Trim(FQtyUnit)<>'M' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      if cxPageControl1.ActivePageIndex=0 then
      begin
        sql.Add('Update JYOrder_PCS_Sub Set WCMQty=');
        sql.Add('isnull((select isnull(sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID and isnull(CKName,'''')='''')*');
      end
      else
      if cxPageControl1.ActivePageIndex=1 then
      begin
        sql.Add('Update JYOrder_PCS_Sub Set TJMQty=');
        sql.Add('isnull((select isnull(sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID and isnull(CKName,'''')=''调节仓库'')*');
      end;
      sql.Add('(select KgZM from JYOrder_Process A where A.PSID=JYOrder_PCS_Sub.PSID),0)');
      sql.Add(' where PFID='''+Trim(FPFID)+'''');
      ExecSQL;
    end;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      if cxPageControl1.ActivePageIndex=0 then
      begin
        sql.Add('Update JYOrder_PCS_Sub Set WCMQty=');
        sql.Add('(select isnull(sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID and isnull(CKName,'''')='''')');
      end else
      if cxPageControl1.ActivePageIndex=1 then
      begin
        sql.Add('Update JYOrder_PCS_Sub Set TJMQty=');
        sql.Add('(select isnull(sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID and isnull(CKName,'''')=''调节仓库'')');
      end;
      sql.Add(' where PFID='''+Trim(FPFID)+'''');
      ExecSQL;
    end;
  end;
end;

procedure TfrmMDManage.Edit1Change(Sender: TObject);
var
  mvalue:String;
begin
  mvalue:=Trim(Edit1.Text);
  if Length(Trim(mvalue))<2 then
  begin
    cxGrid6.Visible:=False;
    Exit;
  end;
  mvalue:='%'+Trim(mvalue)+'%';
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select OrderNo,MPRTCodeName,A.MainId from JYOrder_Main A');
    sql.Add(' where A.orderno like :orderno ');
    //sql.Add(' and A.MainId in(select MainId from JYOrder_Sub B where B.Mainid=A.MainId and B.subId in(select OrdSubId from Contract_Cloth_LL))');
    Parameters.ParamByName('orderno').Value:=mvalue;
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_OrderNo);
  SInitCDSData20(ADOQuery1,CDS_OrderNo);
  if CDS_OrderNo.IsEmpty then cxGrid6.Visible:=False else cxGrid6.Visible:=True;
end;

procedure TfrmMDManage.TvOrdeNoDblClick(Sender: TObject);
var
  fsj:string;
begin
  Edit1.Text:=CDS_OrderNo.fieldbyname('OrderNo').AsString;
  cxGrid6.Visible:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    //sql.Add(',PFGenDanPerson=dbo.F_Get_Order_SubStr(A.MainId,''PFGenDanPerson'')');
    sql.Add(' from JYOrder_Main A');
    sql.Add(' where Orderno='''+Trim(Edit1.Text)+'''');
    Open;
  end;
  FMainId:=Trim(ADOQueryTemp.fieldbyname('MainId').AsString);
  FOrderNo:=Trim(Edit1.Text);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.PRTHX,B.PRTColorNo,B.PRTColor,B.PRTOrdQty,B.OrderUnit,B.ColorNote,JP.PSName  ');
    sql.Add(',Case when A.PFTPMQty<>0 then Cast((A.WCMQty-A.TJMQty)*100*1.00/A.PFTPMQty as decimal(18,2)) else 0 end as WCL');
    sql.Add(' from JYOrder_PCS_Sub A');
    sql.Add(' inner join JYOrder_Process JP on A.PSID=JP.PSID');
    sql.Add(' left join JYOrder_Sub B on A.SubId=B.SubId');
    sql.Add(' where A.MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet72);
  SInitCDSData20(ADOQueryTemp,ClientDataSet72);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
    sql.Add(' where A.ConNo like '''+'%'+Trim(Edit1.Text)+'%'+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet77);
  SInitCDSData20(ADOQueryTemp,ClientDataSet77);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Process where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet71);
  SInitCDSData20(ADOQueryTemp,ClientDataSet71);
end;

procedure TfrmMDManage.ToolButton2Click(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 TfrmMDManage.v1Column13PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='RKYongTu';
      flagname:='用途';
      if ShowModal=1 then
      begin
        with Self.ClientDataSet3 do
        begin
          Edit;
          FieldByName('RKYongTu').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMDManage.v3Column14PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:Boolean;
begin
  mvalue:=TcxCheckBox(Sender).EditingValue;
  if ClientDataSet3.FieldByName('HCFlag').AsBoolean=True then
  begin
    with ClientDataSet3 do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
    end;
    Application.MessageBox('数据已核查不能更改数据!','提示',0);
    Exit;
  end else
  begin
    with ClientDataSet3 do
    begin
      Edit;
      FieldByName('SSel').Value:=mvalue;
      Post;
    end;
  end;
  if ClientDataSet3.FieldByName('JSFlag').AsBoolean=True then
  begin
    with ClientDataSet3 do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
    end;
    Application.MessageBox('结算完结不能更改数据!','提示',0);
    Exit;
  end else
  begin
    with ClientDataSet3 do
    begin
      Edit;
      FieldByName('SSel').Value:=mvalue;
      Post;
    end;
  end;
end;

procedure TfrmMDManage.v1Column1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:Boolean;
begin
  mvalue:=TcxCheckBox(Sender).EditingValue;
  if ClientDataSet3.FieldByName('HCFlag').AsBoolean=True then
  begin
    with ClientDataSet3 do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
    end;
    Application.MessageBox('数据已核查不能更改数据!','提示',0);
    Exit;
  end else
  begin
    with ClientDataSet3 do
    begin
      Edit;
      FieldByName('SSel').Value:=mvalue;
      Post;
    end;
  end;
  if ClientDataSet3.FieldByName('JSFlag').AsBoolean=True then
  begin
    with ClientDataSet3 do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
    end;
    Application.MessageBox('结算完结不能更改数据!','提示',0);
    Exit;
  end else
  begin
    with ClientDataSet3 do
    begin
      Edit;
      FieldByName('SSel').Value:=mvalue;
      Post;
    end;
  end;
end;

end.
