unit U_TJCKOutOK;

interface

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

type
  TfrmTJCKOutOK = class(TForm)
    Panel1: TPanel;
    Label3: TLabel;
    OrderNo: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBBandedTableView;
    cxGridDBBandedColumn1: TcxGridDBBandedColumn;
    cxGridDBBandedColumn2: TcxGridDBBandedColumn;
    Tv2Column1: TcxGridDBBandedColumn;
    v6DutyPerson: TcxGridDBBandedColumn;
    v6DutyFactoryName: TcxGridDBBandedColumn;
    v6DutyReason: TcxGridDBBandedColumn;
    v6Column4: TcxGridDBBandedColumn;
    v6Column5: TcxGridDBBandedColumn;
    v6Column6: TcxGridDBBandedColumn;
    v6ToFactoryName: TcxGridDBBandedColumn;
    v6Column7: TcxGridDBBandedColumn;
    cxGridLevel3: TcxGridLevel;
    v6Column15: TcxGridDBBandedColumn;
    v6Column16: TcxGridDBBandedColumn;
    v6Column17: TcxGridDBBandedColumn;
    v6Column18: TcxGridDBBandedColumn;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    ADOQuery1: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v6Column20: TcxGridDBBandedColumn;
    v6Column21: TcxGridDBBandedColumn;
    cxTabControl1: TcxTabControl;
    ScrollBox3: TScrollBox;
    Button8: TButton;
    Button9: TButton;
    Button10: TButton;
    v6Column1: TcxGridDBBandedColumn;
    v6Column2: TcxGridDBBandedColumn;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn9: TcxGridDBColumn;
    v5Column2: TcxGridDBColumn;
    v5Column7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    v5Column3: TcxGridDBColumn;
    v5Column4: TcxGridDBColumn;
    v5Column5: TcxGridDBColumn;
    v5Column1: TcxGridDBColumn;
    v5Column9: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel3: TPanel;
    Label16: TLabel;
    v5Column6: TcxGridDBColumn;
    Label18: TLabel;
    C_Code: TEdit;
    Label19: TLabel;
    C_CodeName: TEdit;
    v5Column8: TcxGridDBColumn;
    v6Column3: TcxGridDBBandedColumn;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    v6Column14: TcxGridDBBandedColumn;
    v6Column19: TcxGridDBBandedColumn;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Panel5: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Label5: TLabel;
    FromPrice: TEdit;
    Label4: TLabel;
    FromConTypeOther: TBtnEditA;
    Label6: TLabel;
    ToFactoryName: TBtnEditA;
    Label7: TLabel;
    Label8: TLabel;
    ToPrice: TEdit;
    Label9: TLabel;
    ToConTypeOther: TBtnEditA;
    Label10: TLabel;
    Button1: TButton;
    v1Column4: TcxGridDBColumn;
    FromFactoryName: TBtnEditA;
    Label2: TLabel;
    Label11: TLabel;
    FromPSName: TComboBox;
    Label12: TLabel;
    ToPSName: TComboBox;
    MDType: TComboBox;
    Label13: TLabel;
    v3Column1: TcxGridDBColumn;
    Button2: TButton;
    Button3: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Button10Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure FromFactoryNameBtnClick(Sender: TObject);
    procedure ToFactoryNameBtnClick(Sender: TObject);
    procedure FromConTypeOtherBtnClick(Sender: TObject);
    procedure ToConTypeOtherBtnClick(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function  SaveData():Boolean;
    function  SaveConData(FONID:string):Boolean;
    function  SaveMDData(FONID,FOrdMainId,FOrdSubId,FPSID,FPFID,FConMainid,FConSubId:string):Boolean;
  public
    { Public declarations }
  end;

var
  frmTJCKOutOK: TfrmTJCKOutOK;

implementation
uses
  U_DataLink,U_RTFun, U_GYSList,U_ZDYHelp, U_ModuleNote;
{$R *.dfm}

procedure TfrmTJCKOutOK.FormDestroy(Sender: TObject);
begin
  frmTJCKOutOK:=nil;
end;

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

procedure TfrmTJCKOutOK.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;
procedure TfrmTJCKOutOK.InitGrid();
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    sql.Clear;
    sql.Add('select B.OrderNo,B.MPRTCode,B.MPRTCodeName,C.PRTHX,C.PRTColorNo,D.PSName,A.*  ');
    sql.Add(' from JYOrder_PCS_Sub A');
    sql.Add(' inner join JYOrder_Main B on A.Mainid=B.MainId');
    sql.Add(' left join JYOrder_Sub C on A.SubId=C.SubId');
    SQL.Add(' inner join JYOrder_Process D on A.PSID=D.PSID');
    sql.Add(' where isnull(A.CKName,'''')=''调节仓库'' ');
    if cxTabControl1.TabIndex=0 then
    begin
      sql.Add(' and A.TJOutFlag=0');
    end else
    if cxTabControl1.TabIndex=1 then
    begin
      sql.Add(' and A.TJOutFlag=1');
    end;
    Open;
  end;
  SCreateCDS20(ADOQueryMain,ClientDataSet1);
  SInitCDSData20(ADOQueryMain,ClientDataSet1);
end;

procedure TfrmTJCKOutOK.FormShow(Sender: TObject);
begin
  {ReadCxBandedGrid('Tv6',Tv6,'调节仓库');
  ReadCxBandedGrid('Tv2',Tv2,'调节仓库');
  ReadCxBandedGrid('Tv3',Tv3,'调节仓库');
  ReadCxGrid('Tv5',Tv5,'调节仓库'); }
  EndDate.Date:=SGetServerDate(ADOQuery1);
  BegDate.Date:=EndDate.Date-30;
  cxTabControl1.TabIndex:=0;
end;

procedure TfrmTJCKOutOK.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmTJCKOutOK.ToolButton1Click(Sender: TObject);
begin
  {if ClientDataSet6.IsEmpty then Exit;
  if cxTabControl1.TabIndex=2 then Exit;
  if Application.MessageBox('确定要执行入库操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      RTSetSaveDataCDSBand(ADOQueryCmd,tv6,ClientDataSet6,'JYOrder_Main_MD',3);
      if ClientDataSet6.FieldByName('CostFlag').AsBoolean=True then
      begin
        FieldByName('CostFlag').Value:=1
      end else
      begin
        FieldByName('CostFlag').Value:=0;
      end;
      FieldByName('CRDate').Value:=SGetServerDateTime(ADOQueryTemp);
      FieldByName('CRFlag').Value:='入库';
      FieldByName('CRType').Value:='正常入库';
      FieldByName('CRPerson').Value:=Trim(DName);
      FieldByName('QtyFlag').Value:=1;
      FieldByName('RKMDID').Value:=ClientDataSet6.fieldbyname('MDID').Value;
      FieldByName('DutyFactoryNo').Value:=ClientDataSet6.fieldbyname('DutyFactoryNo').Value;
      FieldByName('ToFactoryNo').Value:=ClientDataSet6.fieldbyname('ToFactoryNo').Value;
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main_MD Set KCPS=(select Sum(PS*QtyFlag) from JYOrder_Main_MD A where A.RKMDID=JYOrder_Main_MD.MDID)');
      sql.Add(',KCQty=(select Sum(Qty*QtyFlag) from JYOrder_Main_MD A where A.RKMDID=JYOrder_Main_MD.MDID)');
      sql.Add(' where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    if cxTabControl1.TabIndex=0 then
      ClientDataSet6.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('入库异常!','提示',0);
  end;  }
end;

procedure TfrmTJCKOutOK.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTJCKOutOK.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    
  end;

end;

procedure TfrmTJCKOutOK.Button10Click(Sender: TObject);
begin
  {WriteCxBandedGrid('Tv6',Tv6,'调节仓库');
  WriteCxBandedGrid('Tv2',Tv2,'调节仓库');
  WriteCxBandedGrid('Tv3',Tv3,'调节仓库');
  WriteCxGrid('Tv5',Tv5,'调节仓库');  }
  close;

end;

procedure TfrmTJCKOutOK.Button8Click(Sender: TObject);
begin
  InitGrid();
end;

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

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

procedure TfrmTJCKOutOK.FromFactoryNameBtnClick(Sender: TObject);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        FromFactoryName.TxtCode:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString);
        FromFactoryName.Text:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmTJCKOutOK.ToFactoryNameBtnClick(Sender: TObject);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        ToFactoryName.TxtCode:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString);
        ToFactoryName.Text:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmTJCKOutOK.FromConTypeOtherBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ConTypeOther';
      flagname:='采购加工合同类型';
      if ShowModal=1 then
      begin
        FromConTypeOther.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTJCKOutOK.ToConTypeOtherBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ConTypeOther';
      flagname:='采购加工合同类型';
      if ShowModal=1 then
      begin
        ToConTypeOther.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTJCKOutOK.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select AM.CKID,AM.CKPS,AM.CKQty,AM.SJCKPS,AM.SJCKQty,JM.OrderNo, A.*,B.PSName OrdPSName, ');
    sql.Add(' JS.PRTHX,JS.PRTColor,JS.PRTColorNo,D.C_Code,D.C_CodeName,ClotheType10=A.ClotheType,');
    sql.Add(' JSKCPS=A.RKPS-A.CKPS,');
    sql.Add(' JSKCQty=A.RKQty-A.CKQty');
    sql.Add(' from JYOrder_Main_MD_Out AM');
    sql.Add(' inner join  JYOrder_Main_MD  A on AM.MDID=A.MDID');
    sql.Add(' inner join JYOrder_Main JM on A.OrdMainId=JM.MainId');
    sql.Add(' left join JYOrder_Sub JS on A.OrdSubId=JS.SubId');
    sql.Add(' inner join JYOrder_Process B on A.PSID=B.PSID');
    sql.Add(' inner join Contract_Main C on A.ConMainid=C.MainID');
    sql.Add(' inner join Contract_Sub D on A.ConSubid=D.SubID');
    if ClientDataSet1.IsEmpty=False then
      SQL.Add(' where AM.PFID='''+Trim(ClientDataSet1.fieldbyname('PFID').AsString)+'''')
    else
      sql.Add(' where 1<>1');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet2);
  SInitCDSData20(ADOQuery1,ClientDataSet2);
  if cxTabControl1.TabIndex=0 then
  begin
    FromFactoryName.Text:=Trim(ClientDataSet1.fieldbyname('FactoryName').AsString);
    FromFactoryName.TxtCode:=Trim(ClientDataSet1.fieldbyname('FactoryNo').AsString);
  end else
  begin
    with ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD_OutNote where PFID='''+Trim(ClientDataSet1.fieldbyname('PFID').AsString)+'''');
      Open;
    end;
    SCSHDataNew(ADOQuery1,Panel5,99);
  end;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
    if ClientDataSet2.IsEmpty=False then
      sql.Add(' where A.ConNo like '''+'%'+Trim(ClientDataSet2.fieldbyname('OrderNo').AsString)+'%'+'''')
    else
      sql.Add(' where 1<>1');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet3);
  SInitCDSData20(ADOQuery1,ClientDataSet3);
end;

procedure TfrmTJCKOutOK.Tv2CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId ');
    if ClientDataSet2.IsEmpty=False then
      sql.Add(' where A.ConNo like '''+'%'+Trim(ClientDataSet2.fieldbyname('OrderNo').AsString)+'%'+'''')
    else
      sql.Add(' where 1<>1');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet3);
  SInitCDSData20(ADOQuery1,ClientDataSet3);
end;

procedure TfrmTJCKOutOK.Button1Click(Sender: TObject);
Var
  FReal:Double;
  FInt:Integer;
begin
  if Trim(FromFactoryName.Text)='' then
  begin
    Application.MessageBox('本道供应商不能为空!','提示',0);
    Exit;
  end;
  {if Trim(ToFactoryName.Text)='' then
  begin
    Application.MessageBox('下道供应商不能为空!','提示',0);
    Exit;
  end; }
  if Trim(FromPrice.Text)='' then
  begin
    Application.MessageBox('本道价格不能为空!','提示',0);
    Exit;
  end;
  if Trim(MDType.Text)='' then
  begin
    Application.MessageBox('码单类型不能为空!','提示',0);
    Exit;
  end;
  if TryStrToFloat(FromPrice.Text,FReal)=False then
  begin
    Application.MessageBox('本道价格非法数字!','提示',0);
    Exit;
  end;
  if Trim(FromConTypeOther.Text)='' then
  begin
    Application.MessageBox('本道合同类型不能为空!','提示',0);
    Exit;
  end;
  if (Trim(ToFactoryName.Text)+Trim(ToConTypeOther.Text))<>'' then
  begin
    if Trim(ToFactoryName.Text)='' then
    begin
      Application.MessageBox('下道供应商不能为空!','提示',0);
      Exit;
    end;
    if Trim(ToConTypeOther.Text)='' then
    begin
      Application.MessageBox('下道合同类型不能为空!','提示',0);
      Exit;
    end;
  end;
  if Trim(ToPrice.Text)<>'' then
  begin
    if TryStrToFloat(ToPrice.Text,FReal)=False then
    begin
      Application.MessageBox('下道价格非法数字!','提示',0);
      Exit;
    end;
  end;
  if Tv2.DataController.Summary.FooterSummaryValues[0]>Tv2.DataController.Summary.FooterSummaryValues[2] then
  begin
    Application.MessageBox('出库数量大于库存数量!','提示',0);
    Exit;
  end;
  if Tv2.DataController.Summary.FooterSummaryValues[0]<>Tv2.DataController.Summary.FooterSummaryValues[1] then
  begin
    if Application.MessageBox('出库数量与申请数量不等，确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
    FInt:=99;
  end;
  if FInt<>99 then
    if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    Exit;
  end;

end;
function TfrmTJCKOutOK.SaveData():Boolean;
var
  maxno,ONID:string;
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_OutNote where PFID='''+Trim(ClientDataSet1.fieldbyname('PFID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    ONID:=Trim(ADOQueryTemp.fieldbyname('ONID').AsString);
  end else
  begin
    ONID:='';
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(ONID)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'ON','JYOrder_Main_MD_OutNote',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result:=False;
        Application.MessageBox('取调节仓库出库流水号失败!!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(ONID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD_OutNote where ONID='''+Trim(ONID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(ONID)='' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTIme').Value:=SGetServerDateTime(ADOQuery1);
      end;
      FieldByName('PFID').Value:=Trim(ClientDataSet1.fieldbyname('PFID').AsString);
      FieldByName('ONID').Value:=Trim(maxno);
      FieldByName('FromFactoryName').Value:=Trim(FromFactoryName.Text);
      FieldByName('FromFactoryNo').Value:=Trim(FromFactoryName.TxtCode);
      FieldByName('ToFactoryName').Value:=Trim(ToFactoryName.Text);
      FieldByName('ToFactoryNo').Value:=Trim(FromFactoryName.TxtCode);
      FieldByName('FromConTypeOther').Value:=Trim(FromConTypeOther.Text);
      FieldByName('ToConTypeOther').Value:=Trim(ToConTypeOther.Text);
      if Trim(FromPrice.Text)<>'' then
      begin
        FieldByName('FromPrice').Value:=Trim(FromPrice.Text);
      end else
      begin
        FieldByName('FromPrice').Value:='0';
      end;
      if Trim(ToPrice.Text)<>'' then
      begin
        FieldByName('ToPrice').Value:=Trim(ToPrice.Text);
      end else
      begin
        FieldByName('ToPrice').Value:='0';
      end;
      FieldByName('MDTYpe').Value:=Trim(MDType.Text);
      Post;
    end;
    SaveConData(maxno);
    with ClientDataSet2 do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main_MD_Out Set ');
          if Trim(ClientDataSet2.fieldbyname('SJCKPS').AsString)<>'' then
          begin
            sql.Add(' SJCKPS='+ClientDataSet2.fieldbyname('SJCKPS').AsString);
          end else
          begin
            sql.Add(' SJCKPS=0');
          end;
          if Trim(ClientDataSet2.fieldbyname('SJCKQty').AsString)<>'' then
          begin
            sql.Add(' ,SJCKQty='+ClientDataSet2.fieldbyname('SJCKQty').AsString);
          end else
          begin
            sql.Add(' ,SJCKQty=0');
          end;
          SQL.Add(' where CKID='''+Trim(ClientDataSet2.fieldbyname('CKID').AsString)+'''');
          ExecSQL;
        end;
        Next;
      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='''+Trim(ClientDataSet2.fieldbyname('MDID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_PCS_Sub Set TJOutFlag=1');
      sql.Add(' where PFID='''+Trim(ClientDataSet1.fieldbyname('PFID').AsString)+'''');
      ExecSQL;
    end;
    if Trim(ClientDataSet1.fieldbyname('PFTPUnit').AsString)<>'M' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        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,'''')='''')*');
        sql.Add('(select KgZM from JYOrder_Process A where A.PSID=JYOrder_PCS_Sub.PSID)');
        sql.Add(' where PFID='''+Trim(ClientDataSet1.fieldbyname('PFID').AsString)+'''');
        ExecSQL;
      end;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        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,'''')='''')');
        sql.Add(' where PFID='''+Trim(ClientDataSet1.fieldbyname('PFID').AsString)+'''');
        ExecSQL;
      end;
    end;
    if cxTabControl1.TabIndex=0 then
      ClientDataSet1.Delete;
    ADOQueryCmd.Connection.CommitTrans;
    //Application.MessageBox('保存成功!','提示',0);
    Result:=True;
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;
function TfrmTJCKOutOK.SaveConData(FONID:string):Boolean;
var
  maxno,maxsubno,MainID,SubId,PSName:string;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Main where ONID='''+Trim(FONID)+'''');
    sql.Add(' and ConType='''+Trim(ClientDataSet1.fieldbyname('PSName').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    MainID:=Trim(ADOQueryTemp.fieldbyname('MainID').AsString);
  end else
  begin
    MainID:='';
  end;
  if Trim(MainID)='' then
  begin
    if GetLSNo(ADOQueryCmd,maxno,'NM','Contract_Main',3,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Result:=False;
      Application.MessageBox('取加工采购合同流水号失败!!','提示',0);
      Exit;
    end;
  end else
  begin
    maxno:=Trim(MainID);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Main where MainId='''+Trim(MainID)+'''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if Trim(MainID)='' then
    begin
      Append;
      FieldByName('Filler').Value:=Trim(DName);
    end else
    begin
      Edit;
      FieldByName('Editer').Value:=Trim(DName);
      FieldByName('EditTIme').Value:=SGetServerDateTime(ADOQuery1);
    end;
    FieldByName('ONID').Value:=Trim(FONID);
    FieldByName('MainID').Value:=Trim(maxno);
    FieldByName('ConNo').Value:=Trim(ClientDataSet1.fieldbyname('OrderNo').AsString);
    FieldByName('FactoryNoName').Value:=Trim(FromFactoryName.Text);
    FieldByName('FactoryNo').Value:=Trim(FromFactoryName.TxtCode);
    FieldByName('ConTypeOther').Value:=Trim(FromConTypeOther.Text);
    FieldByName('ConType').Value:=Trim(ClientDataSet1.fieldbyname('PSName').AsString);
    Post;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Sub where MainId='''+Trim(maxno)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    SubId:='';
  end else
  begin
    SubId:=Trim(ADOQueryTemp.fieldbyname('SubId').AsString);
  end;
  if Trim(SubId)='' then
  begin
    if GetLSNo(ADOQueryCmd,maxsubno,'NS','Contract_Sub',3,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Result:=False;
      Application.MessageBox('取加工采购合同子流水号失败!!','提示',0);
      Exit;
    end;
  end else
  begin
    maxsubno:=Trim(SubId);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Sub where SubId='''+Trim(SubId)+'''');
    Open;
  end;
  with ADOQueryCmd do
  begin
    if Trim(SubId)='' then
    begin
      Append;
    end else
    begin
      Edit;
    end;
    FieldByName('MainId').Value:=Trim(maxno);
    FieldByName('SubId').Value:=Trim(maxsubno);
    FieldByName('C_Code').Value:=ClientDataSet1.fieldbyname('MPRTCode').Value;
    FieldByName('C_CodeName').Value:=ClientDataSet1.fieldbyname('MPRTCodeName').Value;
    FieldByName('C_Qty').Value:=Tv2.DataController.Summary.FooterSummaryValues[0];
    FieldByName('C_Unit').Value:=ClientDataSet2.fieldbyname('RKQtyUnit').Value;
    if Trim(FromPrice.Text)='' then
      FieldByName('Price').Value:=0
    else
      FieldByName('Price').Value:=Trim(FromPrice.Text);
    Post;
  end;
  SaveMDData(FONID,Trim(ClientDataSet1.fieldbyname('MainId').AsString),
                     Trim(ClientDataSet1.fieldbyname('SubId').AsString),
                     Trim(ClientDataSet1.fieldbyname('PSID').AsString),
                     Trim(ClientDataSet1.fieldbyname('PFID').AsString),
                     Trim(maxno),Trim(maxsubno));
  if Trim(ToFactoryName.Text)<>'' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Process where ');
      sql.Add(' Mainid='''+Trim(ClientDataSet1.fieldbyname('MainId').AsString)+'''');
      sql.Add(' and PSXH=(select PSXH+1 from JYOrder_Process where PSID='''+Trim(ClientDataSet1.fieldbyname('PSID').AsString)+''')');
     // sql.Add(' and PSName='''+Trim(ClientDataSet1.fieldbyname('PSName').AsString)+''')');

      Open;
    end;
    PSName:=Trim(ADOQueryTemp.fieldbyname('PSName').AsString);
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Main where ONID='''+Trim(FONID)+'''');
      sql.Add(' and ConType='''+Trim(PSName)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      MainID:=Trim(ADOQueryTemp.fieldbyname('MainID').AsString);
    end else
    begin
      MainID:='';
    end;
    if Trim(MainID)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'JM','Contract_Main',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result:=False;
        Application.MessageBox('取加工采购合同流水号失败!!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(MainID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Main where MainId='''+Trim(MainID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(MainID)='' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTIme').Value:=SGetServerDateTime(ADOQuery1);
      end;
      FieldByName('ONID').Value:=Trim(FONID);
      FieldByName('ConNo').Value:=Trim(ClientDataSet1.fieldbyname('OrderNo').AsString);
      FieldByName('MainID').Value:=Trim(maxno);
      FieldByName('FactoryNoName').Value:=Trim(ToFactoryName.Text);
      FieldByName('FactoryNo').Value:=Trim(ToFactoryName.TxtCode);
      FieldByName('ConTypeOther').Value:=Trim(ToConTypeOther.Text);
      FieldByName('ConType').Value:=Trim(PSName);
      Post;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Sub where MainId='''+Trim(maxno)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      SubId:='';
    end else
    begin
      SubId:=Trim(ADOQueryTemp.fieldbyname('SubId').AsString);
    end;
    if Trim(SubId)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxsubno,'JS','Contract_Sub',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result:=False;
        Application.MessageBox('取加工采购合同子流水号失败!!','提示',0);
        Exit;
      end;
    end else
    begin
      maxsubno:=Trim(SubId);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Sub where SubId='''+Trim(SubId)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(SubId)='' then
      begin
        Append;
      end else
      begin
        Edit;
      end;
      FieldByName('MainId').Value:=Trim(maxno);
      FieldByName('SubId').Value:=Trim(maxsubno);
      FieldByName('C_Code').Value:=ClientDataSet1.fieldbyname('MPRTCode').Value;
      FieldByName('C_CodeName').Value:=ClientDataSet1.fieldbyname('MPRTCodeName').Value;
      FieldByName('C_Qty').Value:=Tv2.DataController.Summary.FooterSummaryValues[0];
      FieldByName('C_Unit').Value:=ClientDataSet2.fieldbyname('RKQtyUnit').Value;
      if Trim(ToPrice.Text)='' then
        FieldByName('Price').Value:=0
      else
        FieldByName('Price').Value:=Trim(ToPrice.Text);
      Post;
    end;
  end;
  Result:=True;

end;
function TfrmTJCKOutOK.SaveMDData(FONID,FOrdMainId,FOrdSubId,FPSID,FPFID,FConMainid,FConSubId:string):Boolean;
var
  maxno,MDID:string;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD where ONID='''+Trim(FONID)+'''');
    sql.Add(' and FactoryName='''+Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    MDID:=Trim(ADOQueryTemp.fieldbyname('MDID').AsString);
  end else
  begin
    MDID:='';
  end;
  if Trim(MDID)='' then
  begin
    if GetLSNo(ADOQueryCmd,maxno,'NM','JYOrder_Main_MD',3,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Result:=False;
      Application.MessageBox('取码单流水号失败!!','提示',0);
      Exit;
    end;
  end else
  begin
    maxno:=Trim(MDID);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(MDID)+'''');
    Open;
  end;
  with ADOQueryCmd 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('ONID').Value:=Trim(FONID);
    FieldByName('MDID').Value:=Trim(maxno);
    FieldByName('OrdMainID').Value:=Trim(FOrdMainId);
    FieldByName('ConMainID').Value:=Trim(FConMainId);
    FieldByName('ConSubID').Value:=Trim(FConSubId);
    FieldByName('OrdSubId').Value:=Trim(FOrdSubId);
    FieldByName('PSID').Value:=Trim(FPSID);
    FieldByName('PFID').Value:=Trim(FPFID);
    FieldByName('MDType').Value:=Trim(MDType.Text);
    FieldByName('MDNO').Value:=Trim(maxno);
    FieldByName('FactoryName').Value:=Trim(FromFactoryName.Text);
    FieldByName('FactoryNo').Value:=Trim(FromFactoryName.TxtCode);
    FieldByName('PS').Value:=Tv2.DataController.Summary.FooterSummaryValues[3];
    FieldByName('Qty').Value:=Tv2.DataController.Summary.FooterSummaryValues[0];
    FieldByName('QtyUnit').Value:=ClientDataSet2.fieldbyname('RKQtyUnit').Value;
    FieldByName('PSName').Value:=ClientDataSet1.fieldbyname('PSName').Value;
    FieldByName('FromFactoryName').Value:=Trim(ToFactoryName.Text);
    FieldByName('FromFactoryNo').Value:=Trim(ToFactoryName.TxtCode);
    FieldByName('MDDate').Value:=SGetServerDate(ADOQueryTemp);
    Post;
  end;
  Result:=True;

end;

procedure TfrmTJCKOutOK.Button2Click(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 TfrmTJCKOutOK.Button3Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if ClientDataSet1.IsEmpty then Exit;
  with ADOQueryTemp 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_Main_MD_OutNote C on B.ONID=C.ONID');
    SQL.Add(' where C.PFID='''+Trim(ClientDataSet1.fieldbyName('PFID').AsString)+'''');
    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 ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      //撤销码单实际出库数量
      sql.Add('Update JYOrder_Main_MD_Out Set SJCKPS=0,SJCKQty=0 where PFID='''+Trim(ClientDataSet1.fieldbyname('PFID').AsString)+'''');
      //更新出库数量
      sql.Add('Update JYOrder_Main_MD Set CKPS=(select Sum(SJCKPS) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID)');
      sql.Add(',CKQty=(select Sum(SJCKQty) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID)');
      sql.Add(' where MDID in(select MDID from JYOrder_Main_MD_Out B where B.PFID='''+Trim(ClientDataSet1.fieldbyname('PFID').AsString)+''')');
      //删除自动生成的合同
      sql.Add('delete Contract_Main where ONID in(');
      sql.Add(' select ONID from JYOrder_Main_MD_OutNote where PFID='''+Trim(ClientDataSet1.fieldbyname('PFID').AsString)+''')');
      sql.Add(' and isnull(ONID,'''')<>'''' ');
      SQL.Add(' delete Contract_Sub where not exists(select * from Contract_Main A where A.Mainid=Contract_Sub.MainId)');
      //删除自动生成的码单
      sql.Add(' delete JYOrder_Main_MD where ONID in(');
      sql.Add(' select ONID from JYOrder_Main_MD_OutNote where PFID='''+Trim(ClientDataSet1.fieldbyname('PFID').AsString)+''')');
      sql.Add(' and isnull(ONID,'''')<>'''' ');
      //更新安排完成数量
      sql.Add(' Update JYOrder_PCS_Sub Set WCMQty=0,TJOutFlag=0 where PFID='''+Trim(ClientDataSet1.fieldbyname('PFID').AsString)+'''');
      //删除调节仓库调出备注
       sql.Add(' Delete JYOrder_Main_MD_OutNote  where PFID='''+Trim(ClientDataSet1.fieldbyname('PFID').AsString)+'''');
      ExecSQL;
    end;


    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet1.Delete;
    Application.MessageBox('操作成功!','提示',0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常!','提示',0);
  end;
end;

procedure TfrmTJCKOutOK.OrderNoChange(Sender: TObject);
begin
  Button9.Click;
end;

end.
