unit U_OrderInPutZPNew;

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, cxGridBandedTableView,
  cxGridDBBandedTableView, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_GridReport, RM_System, RM_Dataset, cxDropDownEdit;

type
  TfrmOrderInPutZPNew = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    OrderNo: TEdit;
    OrderDate: TDateTimePicker;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSourceZ: TDataSource;
    Order_SubZ: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    CDS_ZDY: TClientDataSet;
    WFBCodeName: TcxButtonEdit;
    ADOQuery1: TADOQuery;
    Label12: TLabel;
    CustomNoName: TcxButtonEdit;
    Label13: TLabel;
    ArrivalPortName: TcxButtonEdit;
    Label16: TLabel;
    OrdQty: TEdit;
    OrdUnit: TComboBox;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    ETADate: TDateTimePicker;
    Label22: TLabel;
    ETDDate: TDateTimePicker;
    Label23: TLabel;
    ZGDate: TDateTimePicker;
    Label24: TLabel;
    BegRKDate: TDateTimePicker;
    Label25: TLabel;
    BegSCDate: TDateTimePicker;
    Day1: TEdit;
    Label26: TLabel;
    Label27: TLabel;
    Label28: TLabel;
    Day2: TEdit;
    Label29: TLabel;
    Label30: TLabel;
    Label31: TLabel;
    Day3: TEdit;
    Label18: TLabel;
    Label32: TLabel;
    Label33: TLabel;
    Label4: TLabel;
    Order_SubF: TClientDataSet;
    DataSourceF: TDataSource;
    Order_SubQ: TClientDataSet;
    DataSourceQ: TDataSource;
    ScrollBox2: TScrollBox;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBBandedTableView;
    v3Column1: TcxGridDBBandedColumn;
    v3Column2: TcxGridDBBandedColumn;
    v3Column3: TcxGridDBBandedColumn;
    v3Column4: TcxGridDBBandedColumn;
    v3Column5: TcxGridDBBandedColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBBandedTableView;
    v2Column1: TcxGridDBBandedColumn;
    v2Column2: TcxGridDBBandedColumn;
    v2Column3: TcxGridDBBandedColumn;
    v2Column4: TcxGridDBBandedColumn;
    v2Column5: TcxGridDBBandedColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBBandedTableView;
    v1Column1: TcxGridDBBandedColumn;
    v1Column2: TcxGridDBBandedColumn;
    v1Column3: TcxGridDBBandedColumn;
    v1Column4: TcxGridDBBandedColumn;
    v1Column5: TcxGridDBBandedColumn;
    v1Column6: TcxGridDBBandedColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    TBPrint: TToolButton;
    v1Column7: TcxGridDBBandedColumn;
    v2Column6: TcxGridDBBandedColumn;
    v3Column6: TcxGridDBBandedColumn;
    v1Column8: TcxGridDBBandedColumn;
    v2Column7: TcxGridDBBandedColumn;
    v3Column7: TcxGridDBBandedColumn;
    v1Column9: TcxGridDBBandedColumn;
    v2Column8: TcxGridDBBandedColumn;
    v3Column8: TcxGridDBBandedColumn;
    ADOQueryQG: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    BZ: TcxButtonEdit;
    Label5: TLabel;
    cxGrid5: TcxGrid;
    TvSub: TcxGridDBTableView;
    vSubColumn3: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    vSubColumn2: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    TvSubColumn1: TcxGridDBColumn;
    vSubColumn1: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    Label7: TLabel;
    cxGrid4: TcxGrid;
    TVDB: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    VDBColumn1: TcxGridDBColumn;
    VDBColumn2: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Note: TRichEdit;
    Label35: TLabel;
    DataSource1: TDataSource;
    CDS_Sub: TClientDataSet;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    CDS_DB: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    DataSource3: TDataSource;
    vSubColumn4: TcxGridDBColumn;
    vSubColumn5: TcxGridDBColumn;
    vSubColumn6: TcxGridDBColumn;
    vSubColumn7: TcxGridDBColumn;
    vSubColumn8: TcxGridDBColumn;
    vSubColumn9: TcxGridDBColumn;
    vSubColumn10: TcxGridDBColumn;
    Label9: TLabel;
    Label6: TLabel;
    SCXName: TcxButtonEdit;
    Label8: TLabel;
    WJGName: TcxButtonEdit;
    Label34: TLabel;
    MJKZX: TEdit;
    MJKZD: TEdit;
    Label36: TLabel;
    XJKZD: TEdit;
    Label39: TLabel;
    XJKZX: TEdit;
    Label37: TLabel;
    PanZDY: TPanel;
    CXGridZDY: TcxGrid;
    TVZDY: TcxGridDBTableView;
    VHelpZDYName: TcxGridDBColumn;
    CXGridZDYLevel1: TcxGridLevel;
    Button1: TButton;
    Label10: TLabel;
    Label11: TLabel;
    vSubColumn11: TcxGridDBColumn;
    N3: TMenuItem;
    procedure TBCloseClick(Sender: TObject);
    procedure TVZDYCellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure WFBCodeNameKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure TVZDYKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CustomNoNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure CustomNoNamePropertiesChange(Sender: TObject);
    procedure ArrivalPortNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure SCXNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure WJGNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure WFBCodeNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure WFBCodeNamePropertiesChange(Sender: TObject);
    procedure ArrivalPortNamePropertiesChange(Sender: TObject);
    procedure SCXNamePropertiesChange(Sender: TObject);
    procedure WJGNamePropertiesChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Day1Change(Sender: TObject);
    procedure OrdQtyKeyPress(Sender: TObject; var Key: Char);
    procedure Day2Change(Sender: TObject);
    procedure Day3Change(Sender: TObject);
    procedure ETADateChange(Sender: TObject);
    procedure BegRKDateChange(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure v1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure OrdQtyChange(Sender: TObject);
    procedure v3Column4PropertiesEditValueChanged(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v3Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure WFBBZUnit1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure WFBBZUnit2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure WFBBZUnit3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure BZPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure BZPropertiesChange(Sender: TObject);
    procedure WFBBZQty1Change(Sender: TObject);
    procedure vSubColumn6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure vSubColumn8PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure vSubColumn10PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure MenuItem1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure vSubColumn2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    MInt:Integer;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    procedure DelOrderSub(FOrder_Sub:TClientDataSet);
    procedure SELYCL(SCDS_Sub:TClientDataSet);
    procedure EditYCL(SCDS_Sub:TClientDataSet);
    { Private declarations }
  public
    PState,BState:Integer;
    FMainId:String;
    FXS:Integer;
    OrderType:String;
    { Public declarations }
  end;

var
  frmOrderInPutZPNew: TfrmOrderInPutZPNew;

implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_GetPGJBInList;

{$R *.dfm}

procedure TfrmOrderInPutZPNew.TBCloseClick(Sender: TObject);
begin
  if PState<>3 then
  begin
    if Application.MessageBox('是否要保存数据？','提示',32+4)=IDYES then
    begin
     TBSave.Click;
     Exit;
    end;
  end;
  Close;
  WriteCxBandedGrid('配料主',Tv1,'无纺布订单');
  WriteCxBandedGrid('配料辅',Tv2,'无纺布订单');
  WriteCxBandedGrid('配料其它',Tv3,'无纺布订单');

end;

procedure TfrmOrderInPutZPNew.InitData();
var
  i:Integer;
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' exec P_WFBOrder_List :begdate,:endate,:MainId');
    if PState>0 then
    begin
      ADOQuery1.Parameters.ParamByName('MainId').Value:=Trim(FMainId);
      ADOQuery1.Parameters.ParamByName('begdate').Value:='';
      ADOQuery1.Parameters.ParamByName('endate').Value:='';
    end;
    if PState=0 then
    begin
      ADOQuery1.Parameters.ParamByName('MainId').Value:='999999';
      ADOQuery1.Parameters.ParamByName('begdate').Value:='';
      ADOQuery1.Parameters.ParamByName('endate').Value:='';
    end;
    Open;
  end;
 // SCreateCDS20(ADOQuery1,Order_Sub);
  //SInitCDSData20(ADOQuery1,Order_Sub);
  SCSHData(ADOQuery1,ScrollBox1,0);
  if PState=0 then
  begin
    OrderDate.DateTime:=StrToDate(FormatDateTime('yyyy-MM-dd',SGetServerDateTime(ADOTemp)));
    ETADate.DateTime:=OrderDate.DateTime;
    ETDDate.DateTime:=OrderDate.DateTime;
    ZGDate.DateTime:=OrderDate.DateTime;
    BegRKDate.DateTime:=OrderDate.DateTime;
    BegSCDate.DateTime:=OrderDate.DateTime;
    ETADate.Checked:=False;
    ETDDate.Checked:=False;
    ZGDate.Checked:=False;
    BegRKDate.Checked:=False;
    BegSCDate.Checked:=False;
  end;
  if Trim(DParameters1)<>'审核'  then
  begin
    BegRKDate.Enabled:=False;
    Day3.Enabled:=False;
    BegSCDate.Enabled:=False;
    //Note.Enabled:=False;
    cxGrid4.Enabled:=False;
    MJKZX.Enabled:=False;
    MJKZD.Enabled:=False;
    XJKZX.Enabled:=False;
    XJKZD.Enabled:=False;
    SCXName.Enabled:=False;
    WJGName.Enabled:=False;
    ScrollBox2.Enabled:=False;
  end;
  if PState=4  then
  begin
    BegRKDate.Enabled:=True;
    Day3.Enabled:=True;
    BegSCDate.Enabled:=True;
    //Note.Enabled:=False;
    cxGrid4.Enabled:=True;
    MJKZX.Enabled:=True;
    MJKZD.Enabled:=True;
    XJKZX.Enabled:=True;
    XJKZD.Enabled:=True;
    SCXName.Enabled:=True;
    WJGName.Enabled:=True;
    ScrollBox2.Enabled:=True;
  end;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from WFB_DB where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_DB);
  SInitCDSData20(ADOQuery1,CDS_DB);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from WFBOrder_Sub where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_Sub);
  SInitCDSData20(ADOQuery1,CDS_Sub);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,Case when A.YLQty-A.YCLYJkc>=0 then A.YLQty-A.YCLYJkc else null end as BZ from WFBYCL_PB A where MainId='''+Trim(FMainId)+'''');
    sql.Add(' and PBType=''主要'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_SubZ);
  SInitCDSData20(ADOQuery1,Order_SubZ);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,Case when A.YLQty-A.YCLYJkc>=0 then A.YLQty-A.YCLYJkc else null end as BZ from WFBYCL_PB A where MainId='''+Trim(FMainId)+'''');
    sql.Add(' and PBType=''辅助'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_SubF);
  SInitCDSData20(ADOQuery1,Order_SubF);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,Case when A.YLQty-A.YCLYJkc>=0 then A.YLQty-A.YCLYJkc else null end as BZ from WFBYCL_PB A where MainId='''+Trim(FMainId)+'''');
    sql.Add(' and PBType=''其它'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_SubQ);
  SInitCDSData20(ADOQuery1,Order_SubQ);
  PanZDY.Visible:=False;
  if PState=4 then
  begin
    PState:=0;
    FMainId:='999999';
    //OrderNo.Text:='';
    WFBCodeName.Text:='';
    WFBCodeName.Hint:='';
    PanZDY.Visible:=False;
    with CDS_DB do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('DBID').Value:='';
        Post;
        Next;
      end;
    end;
    with Order_SubZ do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('PBID').Value:='';
        FieldByName('YLKC').Value:=0;
        FieldByName('YCLYJKC').Value:=0;
        FieldByName('BZ').Value:=0;
        FieldByName('SGQty').Value:=0;
        Post;
        Next;
      end;
    end;
    v1Column1.Options.Focusing:=True;
    with Order_SubF do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('PBID').Value:='';
        FieldByName('YLKC').Value:=0;
        FieldByName('YCLYJKC').Value:=0;
        FieldByName('BZ').Value:=0;
        FieldByName('SGQty').Value:=0;
        Post;
        Next;
      end;
    end;
    v2Column1.Options.Focusing:=True;
    with Order_SubQ do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('PBID').Value:='';
        FieldByName('YLKC').Value:=0;
        FieldByName('YCLYJKC').Value:=0;
        FieldByName('BZ').Value:=0;
        FieldByName('SGQty').Value:=0;
        Post;
        Next;
      end;
    end;
    v3Column1.Options.Focusing:=True;
  end;
end;

procedure TfrmOrderInPutZPNew.ZDYHelp(FButn:TcxButtonEdit;LType:string);
var
  FType,ZDYName,FText:String;
begin
  PanZDY.Visible:=True;
  PanZDY.Left:=FButn.Left;
  PanZDY.Top:=FButn.Top+FButn.Height;
  with ADOZDY do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    SQL.Add('select RTrim(ZDYNo) ZDYNo,RTrim(ZDYName) ZDYName from KH_ZDY where Type='''+Trim(LType)+'''');
    Open;
  end;
  FText:=Trim(FButn.Text);
  if FText<>'' then
    SDofilter(ADOZDY,' ZDYName like '+QuotedStr('%'+Trim(FText)+'%'))
  else
    SDofilter(ADOZDY,'');
  VHelpZDYName.Summary.GroupFormat:=Trim(FButn.Name);
end;

procedure TfrmOrderInPutZPNew.TVZDYCellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  FName:string;
begin
  if ADOZDY.IsEmpty then Exit;
  FName:=Trim(VHelpZDYName.Summary.GroupFormat);
  TcxButtonEdit(FindComponent(FName)).Text:=Trim(ADOZDY.fieldbyname('ZDYName').AsString);
  TcxButtonEdit(FindComponent(FName)).Hint:=Trim(ADOZDY.fieldbyname('ZDYNO').AsString);
  PanZDY.Visible:=False;
end;

procedure TfrmOrderInPutZPNew.Button1Click(Sender: TObject);
begin
  PanZDY.Visible:=False;
end;

procedure TfrmOrderInPutZPNew.WFBCodeNameKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key=vk_return) or (Key=vk_Down) then
  begin
    if ADOZDY.Active then
      CXGridZDY.SetFocus;
  end;
end;

procedure TfrmOrderInPutZPNew.TVZDYKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    if ADOZDY.IsEmpty then Exit;
    TcxButtonEdit(FindComponent(VHelpZDYName.Summary.GroupFormat)).Text:=Trim(ADOZDY.fieldbyname('ZDYName').AsString);
    TcxButtonEdit(FindComponent(VHelpZDYName.Summary.GroupFormat)).Hint:=Trim(ADOZDY.fieldbyname('ZDYNO').AsString);
    PanZDY.Visible:=False;
    ADOZDY.Active:=False;
  end;
end;

procedure TfrmOrderInPutZPNew.FormShow(Sender: TObject);
begin
  InitData();
  ReadCxBandedGrid('配料主',Tv1,'无纺布订单');
  ReadCxBandedGrid('配料辅',Tv2,'无纺布订单');
  ReadCxBandedGrid('配料其它',Tv3,'无纺布订单');
end;

function TfrmOrderInPutZPNew.SaveData():Boolean;
var
  maxno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from WFBOrder_Main where MainId='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='999999' then
      begin
        Append;
        if GetLSNo(ADOTemp,maxno,'','WFBOrder_Main',2,1)=False then
        begin
          ADOCmd.Connection.RollbackTrans;
          Application.MessageBox('生成流水号异常！','提示',0);
          exit;
        end;
      end
      else begin
        maxno:=Trim(FMainId);
        Edit;
      end;
      FieldByName('MainId').Value:=Trim(maxno);
      FieldByName('OrderType').Value:=Trim(OrderType);
      SSetsaveSql(ADOCmd,'WFBOrder_Main',ScrollBox1,0);
      if PState=2 then
      begin
        FieldByName('Chker').Value:=Trim(DName);
        FieldByName('ChkTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      if Trim(FMainId)='999999' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        if PState=1 then
        begin
          FieldByName('Editer').Value:=Trim(DName);
          FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
        end;
      end;
      Post;
    end;
    FMainId:=Trim(maxno);
    ///保存打包详细
    with CDS_DB do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_DB.fieldbyname('DBId').AsString)='' then
        begin
          if GetLSNo(ADOTemp,maxno,'DB','WFB_DB',3,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(CDS_DB.fieldbyname('DBId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from WFB_DB  ');
          sql.Add(' where DBId='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_DB.fieldbyname('DBId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(FMainId);
          FieldByName('DBId').Value:=Trim(maxno);
          SSetSaveDataCDSNew(ADOCmd,TVDB,CDS_DB,'WFB_DB',0);
          Post;
        end;
        CDS_DB.Edit;
        CDS_DB.FieldByName('DBId').Value:=Trim(maxno);
        //Order_Sub.Post;
        Next;
      end;
    end;
    //保存主要原料
    if not Order_SubZ.IsEmpty then
    begin
      with Order_SubZ do
      begin
        First;
        while not Eof do
        begin
          if Trim(Order_SubZ.fieldbyname('PBId').AsString)='' then
          begin
            if GetLSNo(ADOTemp,maxno,'PB','WFBYCL_PB',3,1)=False then
            begin
              ADOCmd.Connection.RollbackTrans;
              Application.MessageBox('取子流水号失败！','提示',0);
              Exit;
            end;
          end else
          begin
            maxno:=Trim(Order_SubZ.fieldbyname('PBId').AsString);
          end;
          with ADOCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select * from WFBYCL_PB  ');
            sql.Add(' where PBId='''+Trim(maxno)+'''');
            Open;
          end;
          with ADOCmd do
          begin
            if Trim(Order_SubZ.fieldbyname('PBId').AsString)='' then
              Append
            else
              Edit;
            FieldByName('MainId').Value:=Trim(FMainId);
            FieldByName('PBId').Value:=Trim(maxno);
            FieldByName('CRId').Value:=Trim(Order_SubZ.fieldbyname('CRID').AsString);
            FieldByName('PBType').Value:='主要';
           // SSetSaveDataCDSNew();
            SSetSaveDataCDSBandNew(ADOCmd,TV1,Order_SubZ,'WFBYCL_PB',0);
            Post;
          end;
          Order_SubZ.Edit;
          Order_SubZ.FieldByName('PBId').Value:=Trim(maxno);
          //Order_Sub.Post;
          Next;
        end;
      end;
    end;
    //保存辅助原料
    if not Order_SubF.IsEmpty then
    begin
      with Order_SubF do
      begin
        First;
        while not Eof do
        begin
          if Trim(Order_SubF.fieldbyname('PBId').AsString)='' then
          begin
            if GetLSNo(ADOTemp,maxno,'PB','WFBYCL_PB',3,1)=False then
            begin
              ADOCmd.Connection.RollbackTrans;
              Application.MessageBox('取子流水号失败！','提示',0);
              Exit;
            end;
          end else
          begin
            maxno:=Trim(Order_SubF.fieldbyname('PBId').AsString);
          end;
          with ADOCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select * from WFBYCL_PB  ');
            sql.Add(' where PBId='''+Trim(maxno)+'''');
            Open;
          end;
          with ADOCmd do
          begin
            if Trim(Order_SubF.fieldbyname('PBId').AsString)='' then
              Append
            else
              Edit;
            FieldByName('MainId').Value:=Trim(FMainId);
            FieldByName('PBId').Value:=Trim(maxno);
            FieldByName('PBType').Value:='辅助';
            FieldByName('CRId').Value:=Trim(Order_SubF.fieldbyname('CRID').AsString);
           // SSetSaveDataCDSNew();
            SSetSaveDataCDSBandNew(ADOCmd,TV2,Order_SubF,'WFBYCL_PB',0);
            Post;
          end;
          Order_SubF.Edit;
          Order_SubF.FieldByName('PBId').Value:=Trim(maxno);
          //Order_Sub.Post;
          Next;
        end;
      end;
    end;
    //保存其它原料
    if not Order_SubQ.IsEmpty then
    begin
      with Order_SubQ do
      begin
        First;
        while not Eof do
        begin
          if Trim(Order_SubQ.fieldbyname('PBId').AsString)='' then
          begin
            if GetLSNo(ADOTemp,maxno,'PB','WFBYCL_PB',3,1)=False then
            begin
              ADOCmd.Connection.RollbackTrans;
              Application.MessageBox('取子流水号失败！','提示',0);
              Exit;
            end;
          end else
          begin
            maxno:=Trim(Order_SubQ.fieldbyname('PBId').AsString);
          end;
          with ADOCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('select * from WFBYCL_PB  ');
            sql.Add(' where PBId='''+Trim(maxno)+'''');
            Open;
          end;
          with ADOCmd do
          begin
            if Trim(Order_SubQ.fieldbyname('PBId').AsString)='' then
              Append
            else
              Edit;
            FieldByName('MainId').Value:=Trim(FMainId);
            FieldByName('PBId').Value:=Trim(maxno);
            FieldByName('PBType').Value:='其它';
            FieldByName('CRId').Value:=Trim(Order_SubQ.fieldbyname('CRID').AsString);
           // SSetSaveDataCDSNew();
            SSetSaveDataCDSBandNew(ADOCmd,TV3,Order_SubQ,'WFBYCL_PB',0);
            Post;
          end;
          Order_SubQ.Edit;
          Order_SubQ.FieldByName('PBId').Value:=Trim(maxno);
          //Order_Sub.Post;
          Next;
        end;
      end;
    end;

    ///保存子表
    {with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
        begin
          if GetLSNo(ADOTemp,maxno,'OS','Order_Sub',3,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 Order_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);
          SSetSaveDataCDSNew(ADOCmd,Tv1,Order_Sub,'Order_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 TfrmOrderInPutZPNew.TBSaveClick(Sender: TObject);
begin
  OrderDate.SetFocus;
  if Trim(OrderNo.Text)='' then
  begin
    Application.MessageBox('订单编号不能为空！','提示',0);
    Exit;
  end;
  if Trim(WFBCodeName.Text)='' then
  begin
    Application.MessageBox('代号不能为空！','提示',0);
    Exit;
  end;
  if Trim(WFBCodeName.Hint)='' then
  begin
    Application.MessageBox('代号未定义！','提示',0);
    Exit;
  end;
  {if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    Exit;
  end; }
  if CDS_DB.Locate('BSL',null,[]) then
  begin
    Application.MessageBox('包数量不能为空！','提示',0);
    Exit;
  end;
  if CDS_DB.Locate('JSL',null,[]) then
  begin
    Application.MessageBox('卷数量不能为空！','提示',0);
    Exit;
  end;
  if Order_SubZ.Locate('YLKC;YCLYJKC;BZ',VarArrayOf([0,0,0]),[]) then
  begin
    Application.MessageBox('请指定物料！','提示',0);
    Exit;
  end;
  if Order_SubF.Locate('YLKC;YCLYJKC;BZ',VarArrayOf([0,0,0]),[]) then
  begin
    Application.MessageBox('请指定物料！','提示',0);
    Exit;
  end;
  if Order_SubQ.Locate('YLKC;YCLYJKC;BZ',VarArrayOf([0,0,0]),[]) then
  begin
    Application.MessageBox('请指定物料！','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
  end;
end;
procedure TfrmOrderInPutZPNew.SELYCL(SCDS_Sub:TClientDataSet);
begin
 try
    frmGetPGJBInList:=TfrmGetPGJBInList.Create(Application);
    with frmGetPGJBInList do
    begin
      if ShowModal=1 then
      begin
        with ClientDataSet2 do
        begin
          First;
          while not Eof do
          begin
            with SCDS_Sub do
            begin
              Append;
              SCDS_Sub.FieldByName('YCLCode').Value:=Trim(ClientDataSet2.fieldbyname('YCLCode').AsString);
              SCDS_Sub.FieldByName('YCLName').Value:=Trim(ClientDataSet2.fieldbyname('YCLName').AsString);
              SCDS_Sub.FieldByName('YCLSpec').Value:=Trim(ClientDataSet2.fieldbyname('YCLSpec').AsString);
              //SCDS_Sub.FieldByName('GYSName').Value:=Trim(ClientDataSet2.fieldbyname('GYSName').AsString);
              //SCDS_Sub.FieldByName('GYS').Value:=Trim(ClientDataSet2.fieldbyname('GYS').AsString);
              SCDS_Sub.FieldByName('YLKC').Value:=Trim(ClientDataSet2.fieldbyname('QuantityKC').AsString);
              //CDS_Sub.FieldByName('Qty').Value:=Trim(ClientDataSet2.fieldbyname('QtyKC').AsString);
              SCDS_Sub.FieldByName('YLUnit').Value:=Trim(ClientDataSet2.fieldbyname('CRUnit').AsString);
              SCDS_Sub.FieldByName('CRID').Value:=Trim(ClientDataSet2.fieldbyname('CRID').AsString);
              SCDS_Sub.FieldByName('YCLYJKC').Value:=Trim(ClientDataSet2.fieldbyname('YJKC').AsString);
              //SCDS_Sub.FieldByName('YCLType').Value:=Trim(ClientDataSet2.fieldbyname('YCLType').AsString);
              //SCDS_Sub.FieldByName('YCLPrice').Value:=Trim(ClientDataSet2.fieldbyname('YCLPrice').AsString);
              //SCDS_Sub.FieldByName('KCPlace').Value:=Trim(ClientDataSet2.fieldbyname('KCPlace').AsString);
              //CDS_Sub.FieldByName('DepotShow').Value:=Trim(ClientDataSet2.fieldbyname('DepotShow').AsString);
              Post;
            end;
            Next;
          end;
        end;
      end;
    end;
 finally
    frmGetPGJBInList.Free;
 end;
end;

procedure TfrmOrderInPutZPNew.EditYCL(SCDS_Sub:TClientDataSet);
begin
 try
    frmGetPGJBInList:=TfrmGetPGJBInList.Create(Application);
    with frmGetPGJBInList do
    begin
      if ShowModal=1 then
      begin
        with ClientDataSet2 do
        begin
          //First;
         // while not Eof do
          //begin
            with SCDS_Sub do
            begin
              Edit;
              SCDS_Sub.FieldByName('YCLCode').Value:=Trim(ClientDataSet2.fieldbyname('YCLCode').AsString);
              SCDS_Sub.FieldByName('YCLName').Value:=Trim(ClientDataSet2.fieldbyname('YCLName').AsString);
              SCDS_Sub.FieldByName('YCLSpec').Value:=Trim(ClientDataSet2.fieldbyname('YCLSpec').AsString);
              //SCDS_Sub.FieldByName('GYSName').Value:=Trim(ClientDataSet2.fieldbyname('GYSName').AsString);
              //SCDS_Sub.FieldByName('GYS').Value:=Trim(ClientDataSet2.fieldbyname('GYS').AsString);
              SCDS_Sub.FieldByName('YLKC').Value:=Trim(ClientDataSet2.fieldbyname('QuantityKC').AsString);
              //CDS_Sub.FieldByName('Qty').Value:=Trim(ClientDataSet2.fieldbyname('QtyKC').AsString);
              SCDS_Sub.FieldByName('YLUnit').Value:=Trim(ClientDataSet2.fieldbyname('CRUnit').AsString);
              SCDS_Sub.FieldByName('CRID').Value:=Trim(ClientDataSet2.fieldbyname('CRID').AsString);
              SCDS_Sub.FieldByName('YCLYJKC').Value:=Trim(ClientDataSet2.fieldbyname('YJKC').AsString);
              if SCDS_Sub.FieldByName('YLQty').Value-ClientDataSet2.fieldbyname('YJKC').Value>=0 then
              SCDS_Sub.FieldByName('BZ').Value:=SCDS_Sub.FieldByName('YLQty').Value-
                                              ClientDataSet2.fieldbyname('YJKC').Value
              else
                SCDS_Sub.FieldByName('BZ').Value:=null;
              //SCDS_Sub.FieldByName('YCLType').Value:=Trim(ClientDataSet2.fieldbyname('YCLType').AsString);
              //SCDS_Sub.FieldByName('YCLPrice').Value:=Trim(ClientDataSet2.fieldbyname('YCLPrice').AsString);
              //SCDS_Sub.FieldByName('KCPlace').Value:=Trim(ClientDataSet2.fieldbyname('KCPlace').AsString);
              //CDS_Sub.FieldByName('DepotShow').Value:=Trim(ClientDataSet2.fieldbyname('DepotShow').AsString);
              //Post;
            end;
           // Next;
          //end;
        end;
      end;
    end;
 finally
    frmGetPGJBInList.Free;
 end;
end;
procedure TfrmOrderInPutZPNew.ToolButton1Click(Sender: TObject);
begin
  if MInt=1 then
  begin
    SELYCL(Order_SubZ);
  end else
  if MInt=2 then
  begin
    SELYCL(Order_SubF);
  end else
  if MInt=3 then
  begin
    SELYCL(Order_SubQ);
  end;
end;

procedure TfrmOrderInPutZPNew.ToolButton2Click(Sender: TObject);
begin
  if MInt=1 then
  begin
    DelOrderSub(Order_SubZ);
  end else
  if MInt=2 then
  begin
    DelOrderSub(Order_SubF);
  end else
  if MInt=3 then
  begin
    DelOrderSub(Order_SubQ);
  end;
end;
procedure TfrmOrderInPutZPNew.DelOrderSub(FOrder_Sub:TClientDataSet);
begin
  if FOrder_Sub.IsEmpty then Exit;
  if Trim(FOrder_Sub.fieldbyname('PBID').AsString)<>'' then
  begin
    if Application.MessageBox('不能在此删除已保存的数据！','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete WFBYCL_PB where PBID='''+Trim(FOrder_Sub.fieldbyname('PBID').AsString)+'''');
      ExecSQL;
    end;
  end;
  FOrder_Sub.Delete;
end;

procedure TfrmOrderInPutZPNew.CustomNoNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim('CustomNoName');
      flagname:='客户名称';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        CustomNoName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        CustomNoName.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutZPNew.CustomNoNamePropertiesChange(Sender: TObject);
begin
  if FXS=99 then
  begin
    PanZDY.Visible:=False;
    FXS:=0;
    Exit;
  end;
  ZDYHelp(CustomNoName,'CustomNoName');
end;

procedure TfrmOrderInPutZPNew.ArrivalPortNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim('ArrivalPort');
      flagname:='到达港';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        ArrivalPortName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        ArrivalPortName.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutZPNew.SCXNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim('SCXName');
      flagname:='生产线';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        SCXName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        SCXName.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutZPNew.WJGNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim('WJGName');
      flagname:='网结构';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        WJGName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        WJGName.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutZPNew.WFBCodeNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim(WFBCodeName.Name);
      flagname:='代号';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        WFBCodeName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        WFBCodeName.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutZPNew.WFBCodeNamePropertiesChange(Sender: TObject);
begin
  if FXS=99 then
  begin
    PanZDY.Visible:=False;
    FXS:=0;
    Exit;
  end;
  ZDYHelp(WFBCodeName,Trim(WFBCodeName.Name));
end;

procedure TfrmOrderInPutZPNew.ArrivalPortNamePropertiesChange(Sender: TObject);
begin
  if FXS=99 then
  begin
    PanZDY.Visible:=False;
    FXS:=0;
    Exit;
  end;
  ZDYHelp(ArrivalPortName,'ArrivalPort');
end;

procedure TfrmOrderInPutZPNew.SCXNamePropertiesChange(Sender: TObject);
begin
  if FXS=99 then
  begin
    PanZDY.Visible:=False;
    FXS:=0;
    Exit;
  end;
  ZDYHelp(SCXName,Trim(SCXName.Name));
end;

procedure TfrmOrderInPutZPNew.WJGNamePropertiesChange(Sender: TObject);
begin
  if FXS=99 then
  begin
    PanZDY.Visible:=False;
    FXS:=0;
    Exit;
  end;
  ZDYHelp(WJGName,Trim(WJGName.Name));
end;

procedure TfrmOrderInPutZPNew.N1Click(Sender: TObject);
begin  
  with CDS_DB do
  begin
    Append;
    FieldByName('DBUnit').Value:='卷/包';
    Post;
  end;
end;

procedure TfrmOrderInPutZPNew.N2Click(Sender: TObject);
begin
  if CDS_DB.IsEmpty then Exit;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete WFB_DB where DBID='''+Trim(CDS_DB.fieldbyname('DBID').AsString)+'''');
    ExecSQL;
  end;
  CDS_DB.Delete;
end;

procedure TfrmOrderInPutZPNew.Day1Change(Sender: TObject);
var
  DayL:string;
begin
  if trim(Day1.Text)='' then DayL:='0'
  else DayL:=Trim(Day1.Text);
  ETDDate.DateTime:=ETADate.Date-strtoint(DayL);
end;

procedure TfrmOrderInPutZPNew.OrdQtyKeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in['0'..'9','.',#8,#13]) then
  begin
    key:=#0;
  end;
end;

procedure TfrmOrderInPutZPNew.Day2Change(Sender: TObject);
var
  DayL:string;
begin
  if trim(Day2.Text)='' then DayL:='0'
  else DayL:=Trim(Day2.Text);
  ZGDate.DateTime:=ETDDate.Date-strtoint(DayL);
end;
procedure TfrmOrderInPutZPNew.Day3Change(Sender: TObject);
var
  DayL:string;
begin
  if trim(Day3.Text)='' then DayL:='0'
  else DayL:=Trim(Day3.Text);
  BegSCDate.DateTime:=BegRKDate.Date-strtoint(DayL);
end;
procedure TfrmOrderInPutZPNew.ETADateChange(Sender: TObject);
begin
  if Trim(Day1.Text)<>'' then
  begin
    ETDDate.DateTime:=ETADate.Date-strtoint(Day1.Text);
  end;
  if Trim(Day2.Text)<>'' then
  begin
    ZGDate.DateTime:=ETDDate.Date-strtoint(Day2.Text);
  end;
end;

procedure TfrmOrderInPutZPNew.BegRKDateChange(Sender: TObject);
begin
  if Trim(Day3.Text)<>'' then
  begin
    BegSCDate.DateTime:=BegRKDate.Date-strtoint(Day3.Text);
  end;
end;

procedure TfrmOrderInPutZPNew.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  MInt:=1;
  Tv1.Bands[0].Caption:='主要原料√';
  Tv2.Bands[0].Caption:='辅助原料';
  Tv3.Bands[0].Caption:='其它';
  Tv1.Bands[0].Styles.Header.TextColor:=clBlue;
  Tv2.Bands[0].Styles.Header.TextColor:=clBlack;
  Tv3.Bands[0].Styles.Header.TextColor:=clBlack;
end;

procedure TfrmOrderInPutZPNew.Tv2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  MInt:=2;
  Tv1.Bands[0].Caption:='主要原料';
  Tv2.Bands[0].Caption:='辅助原料√';
  Tv3.Bands[0].Caption:='其它';
  Tv1.Bands[0].Styles.Header.TextColor:=clBlack;
  Tv2.Bands[0].Styles.Header.TextColor:=clBlue;
  Tv3.Bands[0].Styles.Header.TextColor:=clBlack;
end;

procedure TfrmOrderInPutZPNew.Tv3MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  MInt:=3;
  Tv1.Bands[0].Caption:='主要原料';
  Tv2.Bands[0].Caption:='辅助原料';
  Tv3.Bands[0].Caption:='其它√';
  Tv1.Bands[0].Styles.Header.TextColor:=clBlack;
  Tv2.Bands[0].Styles.Header.TextColor:=clBlack;
  Tv3.Bands[0].Styles.Header.TextColor:=clBlue;
end;

procedure TfrmOrderInPutZPNew.v1Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,YLSH,DHSL:Double;
begin
  {if Trim(OrdQty.Text)='' then
  begin
    Application.MessageBox('订货数量不能为空！','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingValue;  //㎏㎡
  if Trim(OrdUnit.Text)='㎏' then
  begin
    if Trim(Order_SubZ.fieldbyname('YLSH').AsString)='' then
    begin
      YLSH:=0;
    end else
      YLSH:=StrToFloat(Order_SubZ.fieldbyname('YLSH').AsString);
    with Order_SubZ do
    begin
      Edit;
      FieldByName('YLQty').Value:=StrToFloat(OrdQty.Text)*mvalue/100*(1+YLSH/100);
      if StrToFloat(OrdQty.Text)*mvalue/100*(1+YLSH/100)-FieldByName('YCLYJKC').Value>=0 then
        FieldByName('BZ').Value:=StrToFloat(OrdQty.Text)*mvalue/100*(1+YLSH/100)-FieldByName('YCLYJKC').Value
      else
        FieldByName('BZ').Value:=null;
      FieldByName('YLPB').Value:=mvalue;
      Post;
    end;
  end else
  if Trim(OrdUnit.Text)='㎡' then
  begin
    if Trim(WFBKZ.Text)='' then
    begin
      Application.MessageBox('克重不能为空！','提示',0);
      Exit;
    end;
    {if Trim(WFBfk.Text)='' then
    begin
      Application.MessageBox('幅宽不能为空！','提示',0);
      Exit;
    end;}
    {DHSL:=StrToFloat(OrdQty.Text)*strtofloat(WFBKZ.Text)/1000;
    if Trim(Order_SubZ.fieldbyname('YLSH').AsString)='' then
    begin
      YLSH:=0;
    end else
      YLSH:=StrToFloat(Order_SubZ.fieldbyname('YLSH').AsString);
    with Order_SubZ do
    begin
      Edit;
      FieldByName('YLQty').Value:=DHSL*mvalue/100*(1+YLSH/100);
      if DHSL*mvalue/100*(1+YLSH/100)-FieldByName('YCLYJKC').Value>=0 then
        FieldByName('BZ').Value:=DHSL*mvalue/100*(1+YLSH/100)-FieldByName('YCLYJKC').Value
      else
        FieldByName('BZ').Value:=null;
      FieldByName('YLPB').Value:=mvalue;
      Post;
    end;

  end; }
end;


procedure TfrmOrderInPutZPNew.v1Column3PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,YLPB,DHSL:Double;
begin
  {if Trim(OrdQty.Text)='' then
  begin
    Application.MessageBox('订货数量不能为空！','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingValue;  //㎏㎡
  if Trim(OrdUnit.Text)='㎏' then
  begin
    if Trim(Order_SubZ.fieldbyname('YLPB').AsString)='' then
    begin
      YLPB:=0;
    end else
      YLPB:=StrToFloat(Order_SubZ.fieldbyname('YLPB').AsString);
    with Order_SubZ do
    begin
      Edit;
      FieldByName('YLQty').Value:=StrToFloat(OrdQty.Text)*YLPB/100*(1+mvalue/100);
      if StrToFloat(OrdQty.Text)*YLPB/100*(1+mvalue/100)-FieldByName('YCLYJKC').Value>=0 then
        FieldByName('BZ').Value:=StrToFloat(OrdQty.Text)*YLPB/100*(1+mvalue/100)-FieldByName('YCLYJKC').Value
      else
        FieldByName('BZ').Value:=null;
      FieldByName('YLSH').Value:=mvalue;
      Post;
    end;
  end else
  begin
    if Trim(WFBKZ.Text)='' then
    begin
      Application.MessageBox('克重不能为空！','提示',0);
      Exit;
    end;
    {if Trim(WFBfk.Text)='' then
    begin
      Application.MessageBox('幅宽不能为空！','提示',0);
      Exit;
    end;}
    {DHSL:=StrToFloat(OrdQty.Text)*strtofloat(WFBKZ.Text)/1000;
    if Trim(Order_SubZ.fieldbyname('YLPB').AsString)='' then
    begin
      YLPB:=0;
    end else
      YLPB:=StrToFloat(Order_SubZ.fieldbyname('YLPB').AsString);
    with Order_SubZ do
    begin
      Edit;
      FieldByName('YLQty').Value:=DHSL*YLPB/100*(1+mvalue/100);
      if DHSL*YLPB/100*(1+mvalue/100)-FieldByName('YCLYJKC').Value>=0 then
        FieldByName('BZ').Value:=DHSL*YLPB/100*(1+mvalue/100)-FieldByName('YCLYJKC').Value
      else
        FieldByName('BZ').Value:=null;
      FieldByName('YLSH').Value:=mvalue;
      Post;
    end;
  end; }
end;

procedure TfrmOrderInPutZPNew.v2Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,DHSL:Double;
begin
  {if Trim(OrdQty.Text)='' then
  begin
    Application.MessageBox('订货数量不能为空！','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingValue;  //㎏㎡
  if Trim(OrdUnit.Text)='㎏' then
  begin
    with Order_SubF do
    begin
      Edit;
      FieldByName('YLQty').Value:=StrToFloat(OrdQty.Text)*(mvalue/1000);
      if StrToFloat(OrdQty.Text)*(mvalue/1000)-FieldByName('YCLYJKC').Value>=0 then
        FieldByName('BZ').Value:=StrToFloat(OrdQty.Text)*(mvalue/1000)-FieldByName('YCLYJKC').Value
      else
        FieldByName('BZ').Value:=null;
      FieldByName('YLSHQ').Value:=mvalue;
      Post;
    end;
  end else
  begin
    if Trim(WFBKZ.Text)='' then
    begin
      Application.MessageBox('克重不能为空！','提示',0);
      Exit;
    end;
    DHSL:=StrToFloat(OrdQty.Text)*strtofloat(WFBKZ.Text)/1000;
    with Order_SubF do
    begin
      Edit;
      FieldByName('YLQty').Value:=DHSL*(mvalue/1000);
      if DHSL*(mvalue/1000)-FieldByName('YCLYJKC').Value>=0 then
        FieldByName('BZ').Value:=DHSL*(mvalue/1000)-FieldByName('YCLYJKC').Value
      else
        FieldByName('BZ').Value:=null;
      FieldByName('YLSHQ').Value:=mvalue;
      Post;
    end;
  end; }
end;

procedure TfrmOrderInPutZPNew.OrdQtyChange(Sender: TObject);
var
  YLPB,YLSH,DHSL:Double;
begin
  {if Trim(OrdQty.Text)='' then Exit;
  if Trim(OrdUnit.Text)='㎏' then
    DHSL:=StrToFloat(OrdQty.Text)
  else
  if Trim(OrdUnit.Text)='㎡' then
  begin
    if Trim(WFBKZ.Text)='' then Exit;
    DHSL:=StrToFloat(OrdQty.Text)*strtofloat(WFBKZ.Text)/1000;
  end;
  if not Order_SubZ.IsEmpty then
  begin
    with Order_SubZ do
    begin
      Order_SubZ.DisableControls;
      First;
      while not Eof do
      begin
        Edit;
        if Trim(fieldbyname('YLPB').AsString)='' then
          YLPB:=0
        else
          YLPB:=StrToFloat(fieldbyname('YLPB').AsString);
        if Trim(fieldbyname('YLSH').AsString)='' then
          YLSH:=0
        else
          YLSH:=StrToFloat(fieldbyname('YLSH').AsString);
        FieldByName('YLQty').Value:=DHSL*YLPB/100*(1+YLSH/100);
        if DHSL*YLPB/100*(1+YLSH/100)-FieldByName('YCLYJKC').Value>=0 then
          FieldByName('BZ').Value:=DHSL*YLPB/100*(1+YLSH/100)-FieldByName('YCLYJKC').Value
        else
          FieldByName('BZ').Value:=null;
        Post;
        Next;
      end;
      Order_SubZ.EnableControls;
      First;
    end;
  end;
  if not Order_SubF.IsEmpty then
  begin
    with Order_SubF do
    begin
      Order_SubF.DisableControls;
      First;
      while not Eof do
      begin
        Edit;
        if Trim(fieldbyname('YLSHQ').AsString)='' then
          YLSH:=0
        else
          YLSH:=StrToFloat(fieldbyname('YLSHQ').AsString);
        FieldByName('YLQty').Value:=DHSL*(YLSH/1000);
        if DHSL*(YLSH/1000)-FieldByName('YCLYJKC').Value>=0 then
          FieldByName('BZ').Value:=DHSL*(YLSH/1000)-FieldByName('YCLYJKC').Value
        else
          FieldByName('BZ').Value:=null;
        Post;
        Next;
      end;
      Order_SubF.EnableControls;
      First;
    end;
  end; }
end;
procedure TfrmOrderInPutZPNew.v3Column4PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:Double;
begin
  mvalue:=TcxTextEdit(Sender).EditingValue;
  with Order_SubQ do
  begin
    Edit;
    FieldByName('YLQty').Value:=mvalue;
    if mvalue-fieldbyname('YCLYJKC').Value>=0 then
      FieldByName('BZ').Value:=mvalue-fieldbyname('YCLYJKC').Value
    else
      FieldByName('BZ').Value:=null;
    Post;
  end;
end;

procedure TfrmOrderInPutZPNew.TBPrintClick(Sender: TObject);
var
  fPrintFile:String;
begin
  with ADOQueryQG do
  begin
    Close;
    sql.Clear;
    sql.Add(' exec P_Print_SGD :OrderNo');
    Parameters.ParamByName('OrderNo').Value:=Trim(OrderNo.Text);
    Open;
  end;
  if ADOQueryQG.IsEmpty then  Exit;
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\请购单.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RMVariables['dyr']:=Trim(DName);
    RM2.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\请购单.rmf'),'提示',0);
  end;
end;

procedure TfrmOrderInPutZPNew.v1Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  EditYCL(Order_SubZ);
end;

procedure TfrmOrderInPutZPNew.v2Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  EditYCL(Order_SubF);
end;

procedure TfrmOrderInPutZPNew.v3Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  EditYCL(Order_SubQ);
end;

procedure TfrmOrderInPutZPNew.WFBBZUnit1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
 { try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim('WFBBZUnitD');
      flagname:='大包装';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        WFBBZUnit1.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        WFBBZUnit1.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;  }
end;

procedure TfrmOrderInPutZPNew.WFBBZUnit2PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
 { try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim('WFBBZUnitZ');
      flagname:='中包装';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        WFBBZUnit2.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        WFBBZUnit2.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end; }
end;

procedure TfrmOrderInPutZPNew.WFBBZUnit3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  {try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim('WFBBZUnitX');
      flagname:='小包装';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        WFBBZUnit3.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        WFBBZUnit3.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;}
end;

procedure TfrmOrderInPutZPNew.BZPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim('BZ');
      flagname:='单价单位';
      if ShowModal=1 then
      begin
        Self.FXS:=99;
        BZ.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        BZ.Hint:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutZPNew.BZPropertiesChange(Sender: TObject);
begin
  if FXS=99 then
  begin
    PanZDY.Visible:=False;
    FXS:=0;
    Exit;
  end;
  ZDYHelp(BZ,'BZ');
end;

procedure TfrmOrderInPutZPNew.WFBBZQty1Change(Sender: TObject);
var
  FQty1,FQty2,FQty3,FFK1,FFK2,FKZ1,YLPB,YLSH,DHSL:Double;
begin
  {if Trim(WFBBZQty1.Text)<>'' then
  begin
    FQty1:=StrToFloat(WFBBZQty1.Text);
  end else
  begin
    FQty1:=1;
  end;
  if Trim(WFBBZQty2.Text)<>'' then
  begin
    FQty2:=StrToFloat(WFBBZQty2.Text);
  end else
  begin
    FQty2:=1;
  end;
  if Trim(WFBBZQty3.Text)<>'' then
  begin
    FQty3:=StrToFloat(WFBBZQty3.Text);
  end else
  begin
    FQty3:=1;
  end;
  if Trim(WFBFK1.Text)<>'' then
  begin
    FFK1:=StrToFloat(WFBFK1.Text);
  end else
  begin
    FFK1:=0;
  end;
  if Trim(WFBFK2.Text)<>'' then
  begin
    FFK2:=StrToFloat(WFBFK2.Text);
  end else
  begin
    FFK2:=0;
  end;
  if Trim(WFBKZ.Text)<>'' then
  begin
    FKZ1:=StrToFloat(WFBKZ.Text);
  end else
  begin
    FKZ1:=0;
  end;
  if Trim(WFBKZ.Text)='' then
  begin
    OrdQty.Text:=FloatToStr(FQty1*FQty2*FQty3*FFK1*FFK2/10000);
    OrdUnit.ItemIndex:=OrdUnit.Items.IndexOf('㎡');
  end else
  begin
    OrdQty.Text:=FloatToStr(FQty1*FQty2*FQty3*FFK1*FFK2/10000*FKZ1/1000);
    OrdUnit.ItemIndex:=OrdUnit.Items.IndexOf('㎏');
  end;
  if Trim(OrdQty.Text)='' then Exit;
  if Trim(OrdUnit.Text)='㎏' then
    DHSL:=StrToFloat(OrdQty.Text)
  else
  if Trim(OrdUnit.Text)='㎡' then
  begin
    if Trim(WFBKZ.Text)='' then Exit;
    DHSL:=StrToFloat(OrdQty.Text)*strtofloat(WFBKZ.Text)/1000;
  end;
  if not Order_SubZ.IsEmpty then
  begin
    with Order_SubZ do
    begin
      Order_SubZ.DisableControls;
      First;
      while not Eof do
      begin
        Edit;
        if Trim(fieldbyname('YLPB').AsString)='' then
          YLPB:=0
        else
          YLPB:=StrToFloat(fieldbyname('YLPB').AsString);
        if Trim(fieldbyname('YLSH').AsString)='' then
          YLSH:=0
        else
          YLSH:=StrToFloat(fieldbyname('YLSH').AsString);
        FieldByName('YLQty').Value:=DHSL*YLPB/100*(1+YLSH/100);
        if DHSL*YLPB/100*(1+YLSH/100)-FieldByName('YCLYJKC').Value>=0 then
          FieldByName('BZ').Value:=DHSL*YLPB/100*(1+YLSH/100)-FieldByName('YCLYJKC').Value
        else
          FieldByName('BZ').Value:=null;
        Post;
        Next;
      end;
      Order_SubZ.EnableControls;
      First;
    end;
  end;
  if not Order_SubF.IsEmpty then
  begin
    with Order_SubF do
    begin
      Order_SubF.DisableControls;
      First;
      while not Eof do
      begin
        Edit;
        if Trim(fieldbyname('YLSHQ').AsString)='' then
          YLSH:=0
        else
          YLSH:=StrToFloat(fieldbyname('YLSHQ').AsString);
        FieldByName('YLQty').Value:=DHSL*(YLSH/1000);
        if DHSL*(YLSH/1000)-FieldByName('YCLYJKC').Value>=0 then
          FieldByName('BZ').Value:=DHSL*(YLSH/1000)-FieldByName('YCLYJKC').Value
        else
          FieldByName('BZ').Value:=null;
        Post;
        Next;
      end;
      Order_SubF.EnableControls;
      First;
    end;
  end;}
end;

procedure TfrmOrderInPutZPNew.vSubColumn6PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim('WFBBZUnitD');
      flagname:='大包装';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        CDS_Sub.FieldByName('SWFBBZUnit1').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutZPNew.vSubColumn8PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim('WFBBZUnitZ');
      flagname:='中包装';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        CDS_Sub.FieldByName('SWFBBZUnit2').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutZPNew.vSubColumn10PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Trim('WFBBZUnitD');
      flagname:='小包装';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        CDS_Sub.FieldByName('SWFBBZUnit3').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutZPNew.MenuItem1Click(Sender: TObject);
var
  i:Integer;
begin
  CopyAddRow(TvSub,CDS_Sub);
  i:=CDS_Sub.RecordCount;
  with CDS_Sub do
  begin
    Edit;
    FieldByName('MainId').Value:='';
    FieldByName('SubId').Value:='';
    FieldByName('SWFBColor').Value:='';
    FieldByName('XHNO').Value:=IntToStr(i);
    Post;
  end;
  OrdQty.Text:=floattostr(TvSub.DataController.Summary.FooterSummaryValues[0]);
end;

procedure TfrmOrderInPutZPNew.N3Click(Sender: TObject);
var
  mvalue,YLSH,DHSL,YLQty,YLPB,YLSHQ:Double;
  FOrdQty,FKZ:String;
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('SubID').AsString)<>'' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from WFBYCL_PBSub where SubId='''+Trim(CDS_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 WFBOrder_Sub where SubID='''+Trim(CDS_Sub.fieldbyname('SubID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_Sub.Delete;
  //YLUpdate();
  OrdQty.Text:=floattostr(TvSub.DataController.Summary.FooterSummaryValues[0]);
end;

procedure TfrmOrderInPutZPNew.cxGridDBColumn3PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WFBColor';
      flagname:='颜色';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SWFBColor').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderInPutZPNew.vSubColumn2PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WFBHX';
      flagname:='花型';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SWFBHW').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
