unit U_ProductOrderAnPai;

interface

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

type
  TfrmProductOrderAnPai = class(TForm)
    ToolBar1: TToolBar;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    DataSource2: TDataSource;
    CDS_Sub: TClientDataSet;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    V2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ADOQueryPrint: TADOQuery;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    Panel1: TPanel;
    Label3: TLabel;
    ConNo: TEdit;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    v4Column1: TcxGridDBColumn;
    v4Column2: TcxGridDBColumn;
    cxGrid4Level1: TcxGridLevel;
    CDS_OrderNo: TClientDataSet;
    DS_OrderNo: TDataSource;
    V2Column5: TcxGridDBColumn;
    V2Column6: TcxGridDBColumn;
    V2Column7: TcxGridDBColumn;
    V2Column8: TcxGridDBColumn;
    V2Column9: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ADOQueryHC: TADOQuery;
    Panel3: TPanel;
    DateTimePicker1: TDateTimePicker;
    BtnEditA1: TBtnEditA;
    ComboBox1: TComboBox;
    BtnEditA2: TBtnEditA;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    BtnEditA3: TBtnEditA;
    Label6: TLabel;
    V2Column10: TcxGridDBColumn;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    V2Column11: TcxGridDBColumn;
    BtnEditA4: TBtnEditA;
    Label7: TLabel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolButton9: TToolButton;
    ToolButton10: TToolButton;
    V2Column12: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    Panel4: TPanel;
    ToolBar3: TToolBar;
    ToolButton12: TToolButton;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    V3Column3: TcxGridDBColumn;
    cxGrid3Level1: TcxGridLevel;
    ADORoll: TADOQuery;
    CDS_Roll: TClientDataSet;
    DSRoll: TDataSource;
    V3Column4: TcxGridDBColumn;
    Panel5: TPanel;
    CheckBox1: TCheckBox;
    Label10: TLabel;
    Edit2: TEdit;
    V2Column13: TcxGridDBColumn;
    V3note: TcxGridDBColumn;
    V3Aorddefstr1: TcxGridDBColumn;
    V2C_CodeName: TcxGridDBColumn;
    cxGrid5: TcxGrid;
    TV5: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1Qty1: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    v1PRTUnit: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    v1gf: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGridDBTableC_Unit: TcxGridDBColumn;
    V5Column1: TcxGridDBColumn;
    V5Column2: TcxGridDBColumn;
    V2Column14: TcxGridDBColumn;
    Label11: TLabel;
    C_CodeName: TEdit;
    Label12: TLabel;
    C_Color: TEdit;
    Label13: TLabel;
    FactoryNo: TBtnEditA;
    ADOQuery_Tp: TADOQuery;
    V5Column3: TcxGridDBColumn;
    DateTimePicker2: TDateTimePicker;
    V2Column15: TcxGridDBColumn;
    V2Column16: TcxGridDBColumn;
    Label15: TLabel;
    Label16: TLabel;
    Edit3: TEdit;
    Edit4: TEdit;
    Panel6: TPanel;
    Edit1: TcxCurrencyEdit;
    Label8: TLabel;
    Label9: TLabel;
    cxCurrencyEdit1: TcxCurrencyEdit;
    cxCurrencyEdit2: TcxCurrencyEdit;
    Label14: TLabel;
    Button1: TButton;
    Button2: TButton;
    cxCurrencyEdit3: TcxCurrencyEdit;
    Label17: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure AOrdDefNote12DblClick(Sender: TObject);
    procedure AOrdDefNote7BtnDnClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure Tv4DblClick(Sender: TObject);
    procedure V2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
    procedure V2Column4PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V2Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V2Column6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V2Column8PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure BtnEditA1BtnClick(Sender: TObject);
    procedure BtnEditA2BtnClick(Sender: TObject);
    procedure BtnEditA3BtnClick(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure BtnEditA4BtnClick(Sender: TObject);
    procedure V2Column11PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure TV3CustomDrawIndicatorCell(Sender: TcxGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo;
      var ADone: Boolean);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure cxCurrencyEdit1KeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
    procedure V2Column13PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Button2Click(Sender: TObject);
    procedure V2Column13PropertiesEditValueChanged(Sender: TObject);
    procedure V2C_CodeNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V2C_CodeNamePropertiesEditValueChanged(Sender: TObject);
    procedure TV3MouseEnter(Sender: TObject);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure TV5FocusedRecordChanged(
      Sender: TcxCustomGridTableView; APrevFocusedRecord,
      AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure FactoryNoBtnClick(Sender: TObject);
    procedure cxCurrencyEdit2KeyPress(Sender: TObject; var Key: Char);
    procedure V2Column14PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V2Column14PropertiesEditValueChanged(Sender: TObject);
    procedure TV2CustomDrawIndicatorCell(Sender: TcxGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo;
      var ADone: Boolean);
    procedure V3Column3PropertiesEditValueChanged(Sender: TObject);
    procedure V3Column4PropertiesEditValueChanged(Sender: TObject);
    procedure V3Aorddefstr1PropertiesEditValueChanged(Sender: TObject);
    procedure V3notePropertiesEditValueChanged(Sender: TObject);
    procedure TV5CustomDrawIndicatorCell(Sender: TcxGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo;
      var ADone: Boolean);
  private
    DQdate:TDateTime;
    fRoll:Boolean;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridFH();
    function  SaveData():Boolean;
    procedure UpdateHC(FFirstNo:String);
    function  YFData():Boolean;
    function  DELYFData():Boolean;
    procedure InitRoll();
    function  SaveData10():Boolean;
    function  SaveMJID():boolean;
    { Private declarations }
  public
    FFInt:Integer;
    fckName:string;
    { Public declarations }
  end;

var
  frmProductOrderAnPai: TfrmProductOrderAnPai;

implementation
uses
  U_DataLink,U_OrderInPut,U_Fun, U_ZDYHelp,U_iniParam, U_ZDYHelpSel,U_BefChkHX,
  U_ZdyAttachGYS, U_ZdyAttachCP,U_ClothContractList_Sel;

{$R *.dfm}
function  TfrmProductOrderAnPai.SaveMJID():boolean;
begin
  ADOQueryCmd.Connection.BeginTrans;
  try
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('delete  from JYOrder_Sub_AnPai_MX where APTM='''+Trim(CDS_Roll.fieldbyname('APTM').AsString)+'''');
        execsql;
      end;

      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from JYOrder_Sub_AnPai_MX where APTM='''+Trim(CDS_Roll.fieldbyname('APTM').AsString)+'''');
        Open;
      end;

      with ADOQueryCmd do
      begin
        append;
        FieldByName('ApId').Value:=Trim(CDS_Sub.fieldbyname('apid').AsString);
        FieldByName('ApsubId').Value:= CDS_Roll.recNo;
        FieldByName('APTM').Value:=Trim(CDS_Roll.fieldbyname('APTM').AsString);
        FieldByName('AOrdQty2').Value:=CDS_Roll.fieldbyname('AOrdQty2').AsInteger;
        FieldByName('AOrdQty1').Value:=CDS_Roll.fieldbyname('AOrdQty1').AsFloat;
        FieldByName('AOrdQty3').Value:=CDS_Roll.fieldbyname('AOrdQty3').AsFloat;
        FieldByName('AorddefNote1').Value:=Trim(CDS_Roll.fieldbyname('AorddefNote1').AsString);
        FieldByName('Aorddefstr1').Value:=Trim(CDS_Roll.fieldbyname('Aorddefstr1').AsString);
        post;
      end;


      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('update YF_Money_CR SET Qty=(select sum(AOrdQty1) from JYOrder_Sub_AnPai X where X.apid=YF_Money_CR.YFTypeId )');
        sql.Add('where YFTypeId='+quotedstr(trim(CDS_Roll.fieldbyname('APID').AsString)));
        sql.Add('update YF_Money_CR SET Money=Price*Qty,BBMoney=Price*Qty ');
        sql.Add('where YFTypeId='+quotedstr(trim(CDS_Roll.fieldbyname('APID').AsString)));
        execsql;
      end;



    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete  from WFB_MJJY ');
      sql.Add('where MJID='+quotedstr(trim(CDS_Roll.fieldbyname('APTM').AsString)));
      execsql;
    end;
      IF CDS_Roll.FieldByName('AOrdQty3').AsFloat<>0 then
      begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select *  from WFB_MJJY ');
            sql.Add('where 1=2 ');
            open;
            append;
            FieldByName('MainId').value:=Trim(cds_sub.fieldbyname('mainiD').AsString);
            FieldByName('SubId').value:=Trim(cds_sub.fieldbyname('subid').AsString);
            FieldByName('conNo').Value:=Trim(cds_sub.fieldbyname('conNo').AsString);
            FieldByName('APId').value:=Trim(cds_sub.fieldbyname('apid').AsString);
            FieldByName('MJID').Value:=Trim(CDS_Roll.fieldbyname('APTM').AsString);
            FieldByName('MJSTr1').value:=Trim(cds_sub.fieldbyname('AOrddefstr2').AsString);
            FieldByName('MJTypeOther').value:=Trim(cds_sub.fieldbyname('AOrddefstr3').AsString);
            FieldByName('MJStr2').Value:='未入库';
            FieldByName('MJType').Value:=Trim(CDS_Roll.fieldbyname('Aorddefstr1').AsString);
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('MJLen').Value:=CDS_Roll.fieldbyname('AOrdQty3').AsFloat;
            FieldByName('MJQty1').Value:=CDS_Roll.fieldbyname('AOrdQty1').AsFloat;
            FieldByName('MJxh').Value:=CDS_Roll.fieldbyname('apsubid').AsFloat;
            FieldByName('note').Value:=Trim(CDS_Roll.fieldbyname('AorddefNote1').AsString);
            FieldByName('C_Code').value:=Trim(cds_sub.fieldbyname('C_Code').AsString);
            FieldByName('C_CodeName').value:=Trim(cds_sub.fieldbyname('C_CodeName').AsString);
            FieldByName('C_spec').value:=Trim(cds_sub.fieldbyname('C_spec').AsString);
            FieldByName('C_color').value:=Trim(cds_sub.fieldbyname('C_color').AsString);
            FieldByName('RCGangNo').value:=Trim(cds_sub.fieldbyname('GangNo').AsString);
            FieldByName('BCGangNo').value:=Trim(cds_sub.fieldbyname('AOrddefstr1').AsString);
            FieldByName('ckName').value:=Trim(fckName);
            post;
          end;
      end;

    ADOQueryCmd.Connection.CommitTrans;
    result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('检验保存失败!','提示信息',0);
  end;
end;
function TfrmProductOrderAnPai.SaveData10():Boolean;
var
  maxno:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    ///保存子表
    with CDS_Sub do
    begin
   //   First;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub_AnPai where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_Sub.fieldbyname('Sflag').AsString)='0' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
          FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
          FieldByName('ApId').Value:=Trim(CDS_Sub.fieldbyname('apid').AsString);
          FieldByName('ckName').Value:=Trim(Order_Main.fieldbyname('SYRName').AsString);
          FieldByName('ADefDate1').Value:=CDS_Sub.fieldbyname('ADefDate1').AsDateTime;
          FieldByName('AOrddefstr4').Value:=Trim(Order_Main.fieldbyname('factoryNoName').AsString);
          FieldByName('AOrddefstr5').Value:=Trim(Order_Main.fieldbyname('factoryNo').AsString);
          FieldByName('GangNo').Value:=CDS_Sub.fieldbyname('GangNo').Value;
          FieldByName('conNo').Value:=CDS_Sub.fieldbyname('conNo').Value;
          FieldByName('C_Code').Value:=CDS_Sub.fieldbyname('C_Code').Value;
          FieldByName('C_CodeName').Value:=CDS_Sub.fieldbyname('C_CodeName').Value;
          FieldByName('C_Spec').Value:=CDS_Sub.fieldbyname('C_Spec').Value;
          FieldByName('C_Color').Value:=CDS_Sub.fieldbyname('C_Color').Value;
          FieldByName('AOrddefstr1').Value:=CDS_Sub.fieldbyname('AOrddefstr1').Value;
          FieldByName('AOrddefstr2').Value:=CDS_Sub.fieldbyname('AOrddefstr2').Value;
          if Trim(CDS_Sub.fieldbyname('ZSXS').AsString)<>'' then
            FieldByName('ZSXS').Value:=CDS_Sub.fieldbyname('ZSXS').Value
          else
            FieldByName('ZSXS').Value:=1;
          FieldByName('AOrdQty2').Value:=CDS_Sub.fieldbyname('AOrdQty2').AsInteger;
          FieldByName('AOrdQty1').Value:=CDS_Sub.fieldbyname('AOrdQty1').AsFloat;

          FieldByName('AOrdQty3').Value:=CDS_Sub.fieldbyname('AOrdQty3').AsInteger;
          FieldByName('AOrdQty4').Value:=CDS_Sub.fieldbyname('AOrdQty4').AsFloat;

          FieldByName('AOrddefstr3').Value:=CDS_Sub.fieldbyname('AOrddefstr3').Value;
          FieldByName('AOrdDefNote1').Value:=CDS_Sub.fieldbyname('AOrdDefNote1').Value;
          FieldByName('LLID').Value:=Order_Main.fieldbyname('subID').Value;
          FieldByName('AOrddefstr6').Value:=CDS_Sub.fieldbyname('AOrddefstr6').Value;
          if CDS_Sub.fieldbyname('AOrdFlag1').AsBoolean=True then
            FieldByName('AOrdFlag1').Value:=1
          else
            FieldByName('AOrdFlag1').Value:=0;

          if Trim(CDS_Sub.fieldbyname('Sflag').AsString)='0' then
            FieldByName('Filler').Value:=Trim(DName)
          else
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;

          if Trim(Order_Main.fieldbyname('C_Unit').AsString)=Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString) then
          begin
            FieldByName('HCYZQty').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value;
          end else
          begin
            if Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString)='M' then
            begin
              FieldByName('HCYZQty').Value:=(CDS_Sub.fieldbyname('AOrdQty1').Value*1.00/ADOQueryTemp.fieldbyname('TPMQty').Value)
                                            *ADOQueryTemp.fieldbyname('TPQty').Value ;
            end;
          end;
          
          if Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString)='M' then
          begin
            FieldByName('HCMQty').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value;
          end else
          begin
            FieldByName('HCMQty').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value*CDS_Sub.fieldbyname('ZSXS').Value;
          end;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('delete  from JYOrder_Sub_AnPai_MX where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          execsql;
        end;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub_AnPai_MX where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          Open;
        end;
        CDS_Roll.DisableControls;
        CDS_Roll.First;
        while not CDS_Roll.Eof do
        begin
          with ADOQueryCmd do
          begin
            append;
            FieldByName('ApId').Value:=Trim(CDS_Roll.fieldbyname('apid').AsString);
            FieldByName('ApsubId').Value:= CDS_Roll.recNo;
            FieldByName('APTM').Value:=Trim(CDS_Roll.fieldbyname('APTM').AsString);
            FieldByName('AOrdQty2').Value:=CDS_Roll.fieldbyname('AOrdQty2').AsInteger;
            FieldByName('AOrdQty1').Value:=CDS_Roll.fieldbyname('AOrdQty1').AsFloat;
            FieldByName('AorddefNote1').Value:=Trim(CDS_Roll.fieldbyname('AorddefNote1').AsString);
            FieldByName('Aorddefstr1').Value:=Trim(CDS_Roll.fieldbyname('Aorddefstr1').AsString);
            post;
          end;
          CDS_Roll.Next;
        end;
        CDS_Roll.EnableControls;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_Sub Set SOrdFlag20=1 where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
          ExecSQL;
        end;
        CDS_Sub.Edit;
      //  CDS_Sub.FieldByName('APId').Value:=Trim(maxno);
        CDS_Sub.FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);

        //Order_Sub.Post;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' UPdate WFB_MJJY Set MJTypeOther='''+Trim(CDS_Sub.fieldbyname('AOrddefstr3').AsString)+'''');
          SQL.Add(' where APId='''+Trim(maxno)+'''');
          sql.Add(' UPdate CK_BanCP_KC Set KCQtyUnit='''+Trim(CDS_Sub.fieldbyname('AOrddefstr3').AsString)+'''');
          SQL.Add(' where MJId in (select MJID from WFB_MJJY where APID='''+Trim(maxno)+''')');
          sql.Add(' UPdate CK_BanCP_CR Set QtyUnit='''+Trim(CDS_Sub.fieldbyname('AOrddefstr3').AsString)+'''');
          SQL.Add(' where MJId in (select MJID from WFB_MJJY where APID='''+Trim(maxno)+''')');
          ExecSQL;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' UPdate Contract_sub Set H_PS=(select sum(AOrdQty2) from JYOrder_Sub_AnPai X where X.mainID='''+trim(Order_Main.fieldbyname('mainID').AsString)+''' and X.subID='''+trim(Order_Main.fieldbyname('subID').AsString)+''' )');
          sql.Add(' ,H_Qty=(select sum(AOrdQty1) from JYOrder_Sub_AnPai X where X.mainID='''+trim(Order_Main.fieldbyname('mainID').AsString)+''' and X.subID='''+trim(Order_Main.fieldbyname('subID').AsString)+''' )');
          SQL.Add(' where subID ='+quotedstr(trim(Order_Main.fieldbyname('fromsubID').AsString)));
          ExecSQL;
        end;

       with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' UPdate Contract_sub Set H_PS=(select sum(AOrdQty2) from JYOrder_Sub_AnPai X where X.mainID='''+trim(Order_Main.fieldbyname('mainID').AsString)+''' and X.subID='''+trim(Order_Main.fieldbyname('subID').AsString)+''' )');
          sql.Add(' ,H_Qty=(select sum(AOrdQty1) from JYOrder_Sub_AnPai X where X.mainID='''+trim(Order_Main.fieldbyname('mainID').AsString)+''' and X.subID='''+trim(Order_Main.fieldbyname('subID').AsString)+''' )');
          SQL.Add(' where subID ='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
          ExecSQL;
        end;
        if YFData()=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('更新财务应付款失败!','提示',0);
          Exit;
        end;
      {  IF trim(Order_Main.FieldByName('contk1').AsString)<>'回修加工' then
        begin

        end;  }
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    MovePanel2.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;

procedure TfrmProductOrderAnPai.InitRoll();
begin
//  if CDS_Sub.Active then exit;
  try
  //  ADORoll.DisableControls;
    with ADORoll do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Sub_AnPai_MX where ApID='''+Trim(CDS_Sub.fieldbyname('ApID').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADORoll,CDS_Roll);
    SInitCDSData20(ADORoll,CDS_Roll);
  finally
 //   cxgrid3.SetFocus;
  //  ADORoll.EnableControls;
  end;
end;

procedure TfrmProductOrderAnPai.FormDestroy(Sender: TObject);
begin
  frmProductOrderAnPai:=nil;
end;

procedure TfrmProductOrderAnPai.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  WriteCxGrid('指示单信息1XXX',TV5,'回仓管理');
  WriteCxGrid('回仓信息',Tv2,'回仓管理');
  Action:=caFree;
end;

procedure TfrmProductOrderAnPai.FormCreate(Sender: TObject);
begin
  
  //BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-7;
  //EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  DQdate:=SGetServerDate(ADOQueryTemp);
  fRoll:=true;
end;

procedure TfrmProductOrderAnPai.TBCloseClick(Sender: TObject);
begin
  Close;

end;

procedure TfrmProductOrderAnPai.InitGrid();
begin
  fRoll:=true;
  try
//    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Sub_AnPai where LLId='''+Trim(Order_Main.fieldbyname('subID').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Sub);
    SInitCDSData20(ADOQueryMain,CDS_Sub);
  finally
 //   ADOQueryMain.EnableControls;
    if fRoll then
    InitRoll();
  end;
end;
procedure TfrmProductOrderAnPai.InitGridFH();
begin
end;

procedure TfrmProductOrderAnPai.InitForm();
var
  i:Integer;
begin
  DateTimePicker1.DateTime:=SGetServerDateTime(ADOQueryTemp);
  ReadCxGrid('指示单信息1XXX',TV5,'回仓管理');
  ReadCxGrid('回仓信息',Tv2,'回仓管理');
end;

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

procedure TfrmProductOrderAnPai.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(TV5,ADOQueryMain,'采购单列表');
end;

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

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

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

procedure TfrmProductOrderAnPai.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

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

    end;
  except
  end; }
end;

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

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

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

procedure TfrmProductOrderAnPai.ToolButton1Click(Sender: TObject);
begin
  with ADOQuery_Tp do
  begin
    Close;
    sql.Clear;
    sql.Add('select *,PBFactory=(select factoryNoName from Contract_Main X where X.mainID=B.frommainID), ');
    sql.Add('fdhNO=(select conNo from Contract_Main X where X.mainID=B.frommainID) ');
    sql.Add('from Contract_Main A');
    sql.Add('inner join  Contract_sub B on B.MainID=A.mainID ');
    //  sql.Add('where A.MainId='''+Trim(CDS_OrderNo.fieldbyname('MainId').AsString)+'''');
    sql.Add('where A.conNO like '+quotedstr('%'+trim(CONnO.Text)+'%'));
    sql.Add('and A.FactoryNoName like '+quotedstr('%'+trim(FactoryNo.Text)+'%'));
    sql.Add('and B.C_CodeName like '+quotedstr('%'+trim(C_CodeName.Text)+'%'));
    sql.Add('and B.C_Color = '+quotedstr(trim(C_Color.Text)));
    sql.Add(' and A.conType IN(''坯布预投坯'',''成品后加工'') ');
    sql.Add(' and isnull(B.C_status,''0'')=''0'' ');
    open;
  end;
  SCreateCDS20(ADOQuery_Tp,Order_Main);
  SInitCDSData20(ADOQuery_Tp,Order_Main);

  InitGrid();
  BtnEditA1.TxtCode:=Trim(Order_Main.fieldbyname('FactoryNo').AsString);
  BtnEditA1.Text:=Trim(Order_Main.fieldbyname('FactoryNoName').AsString);
  BtnEditA4.Text:=Trim(Order_Main.fieldbyname('PBFactory').AsString);
end;

procedure TfrmProductOrderAnPai.ToolButton2Click(Sender: TObject);
var
  maxno:string;
begin
  if  Order_Main.IsEmpty then exit;
 { if CDS_Sub.FieldByName('Sflag').AsString='0' then
  begin
    if application.MessageBox('当前汇总记录未保存，是否保存?','提示信息',1)=1 then
    begin
      abort;
    end;
  end; }
  if GetLSNo(ADOQueryCmd,maxno,'W','JYOrder_Sub_AnPai',3,1)=False then
  begin
    Application.MessageBox('取子流水号失败！','提示',0);
    Exit;
  end;
  with CDS_Sub do
  begin
    last;
    Append;
    FieldByName('apid').Value:=trim(maxno);
    FieldByName('Sflag').Value:='0';
    FieldByName('mainID').Value:=Trim(Order_Main.fieldbyname('mainID').AsString);
    FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
    FieldByName('C_Code').Value:=Trim(Order_Main.fieldbyname('C_Code').AsString);
    FieldByName('C_CodeName').Value:=Trim(Order_Main.fieldbyname('C_CodeName').AsString);
    FieldByName('C_COLor').Value:=Trim(Order_Main.fieldbyname('C_COLor').AsString);
   // FieldByName('ADefDate1').Value:=DateTimePicker1.Date;
    FieldByName('ADefDate1').Value:=formatdateTime('yyyy-MM-dd',DateTimePicker1.Date)+' '+formatdateTime('HH:nn:ss',DateTimePicker2.Time); 
    FieldByName('AOrddefstr4').Value:=Trim(BtnEditA1.Text);
    FieldByName('AOrddefstr5').Value:=Trim(BtnEditA1.TxtCode);
    FieldByName('AOrddefstr2').Value:=Trim(Order_Main.fieldbyname('C_UNit').AsString);
    FieldByName('AOrddefstr3').Value:=Trim(Order_Main.fieldbyname('C_UNit').AsString);
  //  FieldByName('gangNO').Value:=Trim(Order_Main.fieldbyname('gangNO').AsString);
    FieldByName('AOrddefstr1').Value:=Trim(Order_Main.fieldbyname('BCGangNO').AsString);
  //  FieldByName('P_Color').Value:=Trim(Order_Main.fieldbyname('P_Color').AsString);
    FieldByName('zsxs').Value:='1';
  //  FieldByName('AOrddefstr2').Value:=Trim(ComboBox1.Text);
  //  FieldByName('AOrddefstr3').Value:=Trim(BtnEditA2.Text);
    FieldByName('AOrdDefNote1').Value:=Trim(BtnEditA3.Text);
    FieldByName('AOrddefstr6').Value:=Trim(Order_Main.fieldbyname('PBfactory').AsString);
    FieldByName('AOrdQty2').Value:=Order_Main.fieldbyname('Qty1').AsFloat;
    FieldByName('AOrdQty1').Value:=Order_Main.fieldbyname('C_Qty').AsFloat;
    Post;
  end;
  fRoll:=false;
end;

procedure TfrmProductOrderAnPai.ToolButton3Click(Sender: TObject);
var
  YFID,CRID:string;
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('APID').AsString)<>'' then
  begin
     {  with ADOQueryTemp do
       begin
         Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR ');
          sql.Add(' where MainID='''+Trim(Order_Main.fieldbyname('MainID').AsString)+'''');
          sql.Add(' and subID='''+Trim(Order_Main.fieldbyname('subID').AsString)+'''');
          sql.Add(' and YFName=''加工费'' ');
          Open;
          if not IsEmpty then
          begin
           YFID:=Fieldbyname('YFID').AsString;
           CRID:=Fieldbyname('CRID').AsString;
          end;
        end;
       }
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 1* from WFB_MJJY where APID='''+Trim(CDS_Sub.fieldbyname('APID').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('delete JYOrder_Sub_AnPai where APID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
        sql.Add('delete JYOrder_Sub_AnPai_MX where APID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
        sql.Add('delete YF_Money_CR where YFTypeID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');

      {  sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(AOrdQty1),0) from JYOrder_Sub_AnPai A  ');
        sql.Add(' where A.mainID=YF_Money_CR.mainID and  A.subiD=YF_Money_CR.subiD   )');
        sql.Add(',PS=(select isnull(sum(AOrdQty2),0) from JYOrder_Sub_AnPai A  ');
        sql.Add(' where A.mainID=YF_Money_CR.mainID and  A.subiD=YF_Money_CR.subiD   )');
        sql.Add(' where YFID='''+Trim(YFID)+'''');

        sql.Add('delete from YF_Money_CR ');
        sql.Add('where YFID='''+Trim(YFID)+'''  ');
        sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(' where  CRID='''+Trim(CRID)+'''');    }
        ExecSQL;
      end;

      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
    end;
  end;
  with CDS_Sub do
  begin
    Delete;
  end;

end;

function TfrmProductOrderAnPai.SaveData():Boolean;
var
  maxno:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    ///保存子表
    with CDS_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_Sub.fieldbyname('APId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'','JYOrder_Sub_AnPai',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(CDS_Sub.fieldbyname('APId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub_AnPai where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_Sub.fieldbyname('APId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
          FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
          FieldByName('ApId').Value:=Trim(maxno);
          FieldByName('ADefDate1').Value:=CDS_Sub.fieldbyname('ADefDate1').Value;
          FieldByName('AOrddefstr4').Value:=Trim(Order_Main.fieldbyname('FirstName').AsString);
          FieldByName('AOrddefstr5').Value:=Trim(Order_Main.fieldbyname('FirstNo').AsString);
          FieldByName('GangNo').Value:=CDS_Sub.fieldbyname('GangNo').Value;
          FieldByName('AOrddefstr1').Value:=CDS_Sub.fieldbyname('AOrddefstr1').Value;
          FieldByName('AOrddefstr2').Value:=CDS_Sub.fieldbyname('AOrddefstr2').Value;
          if Trim(CDS_Sub.fieldbyname('ZSXS').AsString)<>'' then
            FieldByName('ZSXS').Value:=CDS_Sub.fieldbyname('ZSXS').Value
          else
            FieldByName('ZSXS').Value:=1;
          FieldByName('AOrdQty2').Value:=CDS_Sub.fieldbyname('AOrdQty2').Value;
          FieldByName('AOrdQty1').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value;
          FieldByName('AOrddefstr3').Value:=CDS_Sub.fieldbyname('AOrddefstr3').Value;
          FieldByName('AOrdDefNote1').Value:=CDS_Sub.fieldbyname('AOrdDefNote1').Value;
          FieldByName('LLID').Value:=Order_Main.fieldbyname('LLID').Value;
          FieldByName('AOrddefstr6').Value:=CDS_Sub.fieldbyname('AOrddefstr6').Value;
          if CDS_Sub.fieldbyname('AOrdFlag1').AsBoolean=True then
            FieldByName('AOrdFlag1').Value:=1
          else
            FieldByName('AOrdFlag1').Value:=0;

          //RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_Sub,'JYOrder_Sub_AnPai',0);
          {if Trim(CDS_Sub.fieldbyname('APID').AsString)<>'' then
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end else
          begin

          end; }
          if Trim(CDS_Sub.fieldbyname('APId').AsString)='' then
            FieldByName('Filler').Value:=Trim(DName)
          else
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select Top 1* from Contract_Cloth_LL where LLId='''+Trim(Order_Main.fieldbyname('LLId').AsString)+'''');
            Open;
          end;
          if Trim(ADOQueryTemp.fieldbyname('TPUnit').AsString)=Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString) then
          begin
            FieldByName('HCYZQty').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value;
          end else
          begin
            if Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString)='M' then
            begin
              FieldByName('HCYZQty').Value:=(CDS_Sub.fieldbyname('AOrdQty1').Value*1.00/ADOQueryTemp.fieldbyname('TPMQty').Value)
                                            *ADOQueryTemp.fieldbyname('TPQty').Value ;
            end;
          end;
          if Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString)='M' then
          begin
            FieldByName('HCMQty').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value;
          end else
          begin
            FieldByName('HCMQty').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value*CDS_Sub.fieldbyname('ZSXS').Value;
          end;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_Sub Set SOrdFlag20=1 where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
          ExecSQL;
        end;
        CDS_Sub.Edit;
        CDS_Sub.FieldByName('APId').Value:=Trim(maxno);
        CDS_Sub.FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);

        //Order_Sub.Post;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' UPdate WFB_MJJY Set MJTypeOther='''+Trim(CDS_Sub.fieldbyname('AOrddefstr3').AsString)+'''');
          SQL.Add(' where APId='''+Trim(maxno)+'''');
          sql.Add(' UPdate CK_BanCP_KC Set KCQtyUnit='''+Trim(CDS_Sub.fieldbyname('AOrddefstr3').AsString)+'''');
          SQL.Add(' where MJId in (select MJID from WFB_MJJY where APID='''+Trim(maxno)+''')');
          sql.Add(' UPdate CK_BanCP_CR Set QtyUnit='''+Trim(CDS_Sub.fieldbyname('AOrddefstr3').AsString)+'''');
          SQL.Add(' where MJId in (select MJID from WFB_MJJY where APID='''+Trim(maxno)+''')');
          ExecSQL;
        end;

     {   if YFData()=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('更新财务应付款失败!','提示',0);
          Exit;
        end;   }
        Next;
      end;
    end;
   { with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('UPdate Contract_Cloth_LL Set HCPS=0,HCQty=0,HCMQty=0 where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryHC do
    begin
      Close;
      sql.Clear;
      sql.Add('select distinct(AOrddefstr5) AOrddefstr5 from JYOrder_Sub_AnPai where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      Open;
    end;
    with ADOQueryHC do
    begin
      First;
      while not Eof do
      begin
        UpdateHC(Trim(ADOQueryHC.fieldbyname('AOrddefstr5').AsString));
        Next;
      end;
    end;  }
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate Contract_Cloth_LL Set HCPS=(select isnull(sum(AOrdQty2),0) from JYOrder_Sub_AnPai A where A.LLID=Contract_Cloth_LL.LLID )');
      sql.Add(',HCMQty=(select isnull(sum(HCMQty),0) from JYOrder_Sub_AnPai A where A.LLID=Contract_Cloth_LL.LLID )');
      sql.Add(' where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      if (Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString)=Trim(Order_Main.fieldbyname('TPUnit').AsString)) then
        sql.Add('Update Contract_Cloth_LL Set HCQty=(select isnull(Sum(AOrdQty1),0) from JYOrder_Sub_AnPai A where A.LLID=Contract_Cloth_LL.LLID) ')
      else
        sql.Add('Update Contract_Cloth_LL Set HCQty=HCMQty*1.00/TPMQty*TPQty ');
      sql.Add(' where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
      ExecSQL;
    end;
    {if YFData()=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('更新财务应付款失败!','提示',0);
      Exit;
    end;  }
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    MovePanel2.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;

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

procedure TfrmProductOrderAnPai.AOrdDefNote7BtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text:='';
  TBtnEditC(Sender).TxtCode:='';
end;

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

procedure TfrmProductOrderAnPai.ToolButton5Click(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WFBCD';
      flagname:='疵点';
      fnote:=True;
      V1Note.Caption:='英文名称';
      if ShowModal=1 then
      begin
        
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderAnPai.ToolButton6Click(Sender: TObject);
var
  fPrintFile:string;
  Txt,fImagePath:string;
  Moudle: THandle;
  Makebar:TMakebar;
  Mixtext:TMixtext;
begin
  if CDS_Sub.IsEmpty then Exit;
  if CDS_Sub.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;

  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\检验指示单标签.rmf' ;
  if not FileExists(fPrintFile) then
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检验指示单标签.rmf'),'提示',0);
    Exit;
  end;
  with CDS_Sub do
  begin
    DisableControls;
    First;
    while not eof do
    begin
      IF Fieldbyname('ssel').AsBoolean then
      begin

      with ADOQueryPrint do
      begin
        close;
        sql.Clear;
        sql.Add('select A.*               ');
        sql.Add(',CPKW=(select Top 1 zdyCode from KH_Zdy_Attachment X where X.DEFstr1=A.C_CodeName and X.DEFstr2=A.C_Color)');
        sql.Add(',CPNUM=(select count(CRID) from CK_BanCP_KC X where X.C_CodeName=A.C_CodeName and X.C_Color=A.C_Color)');
        sql.Add(',C_CodeNameZW=(select Top 1 defnote3 from KH_Zdy_Attachment X where X.ZdyName=A.C_CodeName and Type=''CP'') ');
        sql.Add(',C_colorName=(select Top 1 DEFstr6 from KH_Zdy_Attachment X where X.ZdyName=A.C_CodeName and X.DEFstr3=A.C_Color and Type=''CP'') ');
        sql.Add('from JYOrder_Sub_AnPai A ');
        sql.Add('where Apid='+quotedstr(trim(CDS_Sub.fieldbyname('apid').AsString)));
        open;
        if  isempty then
        begin
          CDS_Sub.EnableControls;
          application.MessageBox('此回仓单未保存，不能保存','提示信息',0);
          exit;
        end;
        end;
        try
          Moudle:=LoadLibrary('MakeQRBarcode.dll');
          @Makebar:=GetProcAddress(Moudle,'Make');
          @Mixtext:=GetProcAddress(Moudle,'MixText');
          Txt:=Trim(ADOQueryPrint.fieldbyname('APID').AsString);
          fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp';
          if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
          CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
          if FileExists(fImagePath) then DeleteFile(fImagePath);
          Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
        except
          application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
          Order_Main.EnableControls;
          exit;
        end;

        RMVariables['QRBARCODE']:=fImagePath;
        RM1.LoadFromFile(fPrintFile);
        if CheckBox1.Checked then   RM1.ShowReport
        else
        begin
          RM1.DefaultCopies:=strtointdef(trim(edit2.Text),1);
          RM1.PrintReport;
        end;
      end;
      next;
    end;
    EnableControls;
    First;
  end;


end;

procedure TfrmProductOrderAnPai.ConNoChange(Sender: TObject);
var
  mvalue:String;
begin
  if ADOQuery_Tp.Active=False then Exit;
  mvalue:=SGetFilters(Panel1,1,2);

    if (trim(mvalue)='') and (trim(C_Color.Text)<>'') then
    begin
      mvalue:=' C_Color='+quotedstr(trim(C_Color.Text));
    end
    else
    if (trim(mvalue)<>'') and (trim(C_Color.Text)<>'') then
    begin
      mvalue:=mvalue+' and C_Color='+quotedstr(trim(C_Color.Text));
    end;

  SDofilter(ADOQuery_Tp,mvalue);
  SCreateCDS20(ADOQuery_Tp,Order_Main);
  SInitCDSData20(ADOQuery_Tp,Order_Main);
end;

procedure TfrmProductOrderAnPai.Tv4DblClick(Sender: TObject);
begin
  cxGrid4.Visible:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
      sql.Add('select *,PBFactory=(select factoryNoName from Contract_Main X where X.mainID=B.frommainID), ');
      sql.Add('fdhNO=(select conNo from Contract_Main X where X.mainID=B.frommainID) ');
      sql.Add('from Contract_Main A');
      sql.Add('inner join  Contract_sub B on B.MainID=A.mainID ');
      sql.Add('where A.MainId='''+Trim(CDS_OrderNo.fieldbyname('MainId').AsString)+'''');
      sql.Add(' and A.conType IN(''坯布预投坯'',''成品后加工'') ');
      sql.Add(' and isnull(B.C_status,''0'')=''0'' ');
    open;
  end;
 { with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select D.TpNote,D.gangNo,D.BCGangNO,D.C_Color,C.FirstName,C.FirstNo,C.TPPS,C.TPMQty,C.LLID,C.HXPS,C.HXQty,C.bcpQty,C.HXUnit,C.DHIDHelp,C.DHID,C.TPUnit,A.*,B.* ');
    sql.Add(',PBFactory=(select Top 1 AA.factoryNoName from Contract_Main AA inner join Contract_Cloth_DH BB on AA.MainId=BB.MainId');
    sql.Add(' where BB.DHID=C.DHID)');
    sql.Add(',HCUnit=(select Top 1 AOrddefstr2  from JYOrder_Sub_AnPai AA where AA.SubId=B.SubId)');
    sql.Add('from Contract_Main A inner join Contract_sub B on A.MainId=B.MainId ');
    sql.Add(' inner join Contract_Cloth_LL C on C.OrdSubId=B.SubId');
    sql.Add(' inner join Contract_Cloth_LLMX D on D.MXID=C.LLID');
    sql.Add('where A.MainId='''+Trim(CDS_OrderNo.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and C.JXJGFlag=0 and D.JGType<>''退货出库'' ');
    sql.Add(' and exists(select * from Contract_Cloth_LLMX LM where LM.OrdSubId=B.SubId and LM.OrdSubId=C.OrdSubId )');
//    ShowMessage(SQL.Text);
    Open;
  end;    }
  SCreateCDS20(ADOQueryTemp,Order_Main);
  SInitCDSData20(ADOQueryTemp,Order_Main);

  InitGrid();
  BtnEditA1.TxtCode:=Trim(Order_Main.fieldbyname('FactoryNo').AsString);
  BtnEditA1.Text:=Trim(Order_Main.fieldbyname('FactoryNoName').AsString);
  BtnEditA4.Text:=Trim(Order_Main.fieldbyname('PBFactory').AsString);
end;

procedure TfrmProductOrderAnPai.V2Column1PropertiesEditValueChanged(
  Sender: TObject);
//var
  //mvalue,FFieldName,mvalue10,mvalue20:String;
  //FHCPS,FHCQty,FHCMQty:Double;
begin
  {if Trim(CDS_Sub.fieldbyname('AOrddefstr4').AsString)='' then
  begin
    Application.MessageBox('染厂不能为空!','提示',0);
    Exit;
  end;
  with Self.ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Cloth_LL A ');
    sql.Add(' where A.FirstNo='''+Trim(CDS_Sub.fieldbyname('AOrddefstr5').AsString)+'''');
    sql.Add(' and A.OrdSubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  if Self.ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('此染厂没有相对应的数据!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if  FFieldName='AOrdQty1' then
  begin
    if Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString)='' then
    begin
      Application.MessageBox('数量单位不能为空!','提示',0);
      Exit;
    end;
  end;
  if Trim(mvalue)='' then
  begin
    if FFieldName='ZSXS' then
      mvalue:='1'
    else
    if FFieldName='AOrddefstr2' then
      mvalue:=''
    else
      mvalue:='0';
  end;

  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate JYOrder_Sub_AnPai ');
    if Trim(FFieldName)='AOrddefstr2' then
    begin
      sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
    end else
    begin
      sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    end;
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
    ExecSQL;
  end;
  if Trim(FFieldName)='AOrdQty2' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select isnull(Sum(AOrdQty2),0) HCPS,isnull(Sum(AOrdQty1),0) HCQty from JYOrder_Sub_AnPai  ');
      sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      sql.Add(' and AOrddefstr5='''+Trim(CDS_Sub.fieldbyname('AOrddefstr5').AsString)+'''');
      Open;
    end;
    FHCPS:=ADOQueryTemp.FieldByName('HCPS').Value;
    if FHCPS>0 then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Contract_Cloth_LL where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
        sql.Add(' and FirstNo='''+Trim(CDS_Sub.fieldbyname('AOrddefstr5').AsString)+'''');
        SQL.Add(' order by LLIdx');
        Open;
      end;
      with ADOQueryTemp do
      begin
        First;
        while (not eof) and (FHCPS>0) do
        begin
          if FHCPS>=FieldByName('TPPS').Value then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCPS=TPPS ');
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCPS:=FHCPS-FieldByName('TPPS').Value;
          end else
          begin

            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCPS='+FloatToStr(FHCPS));
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCPS:=0;
          end;
          Next;
        end;
      end;
    end;
  end;
  if Trim(FFieldName)='AOrdQty1' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select isnull(Sum(AOrdQty2),0) HCPS,isnull(Sum(AOrdQty1),0) HCQty from JYOrder_Sub_AnPai  ');
      sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      sql.Add(' and AOrddefstr5='''+Trim(CDS_Sub.fieldbyname('AOrddefstr5').AsString)+'''');
      Open;
    end;
    FHCQty:=ADOQueryTemp.FieldByName('HCQty').Value;
    if FHCQty>0 then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Contract_Cloth_LL where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
        sql.Add(' and FirstNo='''+Trim(CDS_Sub.fieldbyname('AOrddefstr5').AsString)+'''');
        SQL.Add(' order by LLIdx');
        Open;
      end;
      with ADOQueryTemp do
      begin
        First;
        while (not eof) and (FHCQty>0) do
        begin
          if FHCQty>=FieldByName('BCPQty').Value then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCQty=BCPQty ');
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCQty:=FHCQty-FieldByName('BCPQty').Value;
          end else
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCQty='+FloatToStr(FHCQty));
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCQty:=0;
          end;
          Next;
        end;
      end;
    end;
  end;}
end;
procedure TfrmProductOrderAnPai.UpdateHC(FFirstNo:String);
var
  FHCPS,FHCYZQty,FHCMQty:Double;
begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select isnull(Sum(AOrdQty2),0) HCPS from JYOrder_Sub_AnPai  ');
      sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      sql.Add(' and AOrddefstr5='''+Trim(FFirstNo)+'''');
      Open;
    end;
    FHCPS:=ADOQueryTemp.FieldByName('HCPS').Value;
    if FHCPS=0 then
    begin
    
    end;
    if FHCPS>0 then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Contract_Cloth_LL where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
        sql.Add(' and FirstNo='''+Trim(FFirstNo)+'''');
        SQL.Add(' order by LLIdx');
        Open;
      end;
      with ADOQueryTemp do
      begin
        First;
        while (not eof) and (FHCPS>0) do
        begin
          if FHCPS>=FieldByName('TPPS').Value then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCPS=TPPS ');
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCPS:=FHCPS-FieldByName('TPPS').Value;
          end else
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCPS='+FloatToStr(FHCPS));
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCPS:=0;
          end;
          Next;
        end;
      end;
    end;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select isnull(Sum(HCYZQty),0) HCYZQty,isnull(Sum(HCMQty),0) HCMQty from JYOrder_Sub_AnPai  ');
      sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      sql.Add(' and AOrddefstr5='''+Trim(FFirstNo)+'''');
      Open;
    end;
    FHCYZQty:=ADOQueryTemp.FieldByName('HCYZQty').Value;
    FHCMQty:=ADOQueryTemp.FieldByName('HCMQty').Value;
    if FHCYZQty>=0 then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Contract_Cloth_LL where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
        sql.Add(' and FirstNo='''+Trim(FFirstNo)+'''');
        SQL.Add(' order by LLIdx');
        Open;
      end;
      with ADOQueryTemp do
      begin
        First;
        while (not eof) and (FHCYZQty>0) do
        begin
          if FHCYZQty>=FieldByName('BCPQty').Value then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCQty=BCPQty ');
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCYZQty:=FHCYZQty-FieldByName('BCPQty').Value;
          end else
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCQty='+FloatToStr(FHCYZQty));
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCYZQty:=0;
          end;
          Next;
        end;
      end;
      with ADOQueryTemp do
      begin
        First;
        while (not eof) and (FHCMQty>0) do
        begin
          if FHCMQty>=FieldByName('BCPMQty').Value then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCMQty=BCPMQty ');
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCMQty:=FHCMQty-FieldByName('BCPMQty').Value;
          end else
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCMQty='+FloatToStr(FHCMQty));
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCMQty:=0;
          end;
          Next;
        end;
      end;
    end;
end;

procedure TfrmProductOrderAnPai.cxGridDBColumn1PropertiesEditValueChanged(
  Sender: TObject);
//var
  //mvalue,FFieldName:String;
begin
  {mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate JYOrder_Sub_AnPai ');
    if Trim(mvalue)<>'' then
    begin
      sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
    end else
    begin
      sql.Add(' Set '+FFieldName+'=NULL');
    end;
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
    ExecSQL;
  end;  }
end;


procedure TfrmProductOrderAnPai.V2Column4PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('AOrddefstr2').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_Sub_AnPai ');
          sql.Add(' Set AOrddefstr2='''+Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'''');
          SQL.Add(',Editer='''+Trim(DName)+'''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          sql.Add(' UPdate WFB_MJJY Set MJStr1='''+Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'''');
          SQL.Add(' where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

procedure TfrmProductOrderAnPai.V2Column6PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
var
  mvalue:string;
begin
  mvalue:=Trim(CDS_Sub.FieldByName('AOrdDefNote1').AsString);
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='JYYQ';
      flagname:='检验要求';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('AOrdDefNote1').Value:=mvalue+frmZDYHelpSel.ReturnStr;
        {with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_Sub_AnPai ');
          sql.Add(' Set AOrdDefNote1='''+Trim(mvalue+frmZDYHelpSel.ReturnStr)+'''');
          SQL.Add(',Editer='''+Trim(DName)+'''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          ExecSQL;
        end; }
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmProductOrderAnPai.V2Column8PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      //flag:='RKPlace';
      flag:='FactoryNo1Name';
      flagname:='染厂';
      MainType:='染厂';
      if ShowModal=1 then
      begin

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Cloth_LL A ');
          sql.Add(' where A.FirstNo='''+Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString)+'''');
          sql.Add(' and A.OrdSubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
          Application.MessageBox('此染厂没有相对应的数据!','提示',0);

        end else
        begin
          with CDS_Sub do
          begin
            Edit;
            FieldByName('AOrddefstr4').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
            FieldByName('AOrddefstr5').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString);
          end;
          {with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPdate JYOrder_Sub_AnPai ');
            sql.Add(' Set AOrddefstr5='''+Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString)+'''');
            sql.Add(' ,AOrddefstr4='''+Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'''');
            SQL.Add(',Editer='''+Trim(DName)+'''');
            SQL.Add(',Edittime=getdate() ');
            SQL.Add(' where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
            ExecSQL;
          end;}
        end;

      end;
    end;
  finally
    //frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderAnPai.ToolButton4Click(Sender: TObject);
var
  FDW:String;
begin
  ConNo.SetFocus;
  if CDS_Sub.Locate('C_CodeName',null,[])=True then
  begin
    Application.MessageBox('产品名称不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('C_CodeName','',[])=True then
  begin
    Application.MessageBox('产品名称不能为空!','提示',0);
    Exit;
  end;
 { if CDS_Sub.Locate('conNo','',[])=True then
  begin
    Application.MessageBox('任务单号不能为空!','提示',0);
    Exit;
  end;   }
  if CDS_Sub.Locate('C_Color',null,[])=True then
  begin
    Application.MessageBox('颜色不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('C_Color','',[])=True then
  begin
    Application.MessageBox('颜色不能为空!','提示',0);
    Exit;
  end;
{  if CDS_Sub.Locate('AOrddefstr4',null,[])=True then
  begin
    Application.MessageBox('染厂不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr4','',[])=True then
  begin
    Application.MessageBox('染厂不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('GangNo',null,[])=True then
  begin
    Application.MessageBox('本厂缸号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr2',null,[])=True then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr2','',[])=True then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr2','Kg',[])=True then
  begin
    if Trim(CDS_Sub.FieldByName('ZSXS').AsString)='' then
    begin
      Application.MessageBox('折算系数不能为空!','提示',0);
      Exit;
    end;
  end;
  if CDS_Sub.Locate('AOrdQty2',null,[])=True then
  begin
    Application.MessageBox('匹数量不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrdQty1',null,[])=True then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
 { if CDS_Sub.Locate('AOrdQty1','',[])=True then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
 { FDW:=Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString);
  if CDS_Sub.Locate('AOrddefstr2',FDW,[])=False then
  begin
    Application.MessageBox('数量单位不一致!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr3',null,[]) then
  begin
    Application.MessageBox('检验数量单位不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr3','',[]) then
  begin
    Application.MessageBox('检验数量单位不能为空!','提示',0);
    Exit;
  end;
  FDW:=Trim(CDS_Sub.fieldbyname('AOrddefstr3').AsString);
  if CDS_Sub.Locate('AOrddefstr3',FDW,[])=False then
  begin
    Application.MessageBox('检验数量单位不一致!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 1* from Contract_Cloth_LL where OrdSubId='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
    Open;
  end;
  if Trim(ADOQueryTemp.fieldbyname('TPUnit').AsString)='M' then
  begin
    if CDS_Sub.Locate('AOrddefstr2','Kg',[]) then
    begin
      Application.MessageBox('领料数量单位为M,回仓数量单位不能为Kg!','提示',0);
      Exit;
    end;
  end;
  if CDS_Roll.IsEmpty then
  begin
    Application.MessageBox('细码不能为空!','提示',0);
    Exit;
  end;




  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY where APID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
    Open;
  end;

    if not ADOQueryTemp.IsEmpty then
    begin
      Application.MessageBox('已保存检验数量，回仓信息不能修改！',0);
      Exit;
    end;
    }
  if  trim(CDS_Sub.FieldByName('AOrddefstr4').AsString)='' then
  begin
    Application.MessageBox('染厂不能为空!','提示',0);
    Exit;
  end;
  if  trim(CDS_Sub.FieldByName('AOrddefstr1').AsString)='' then
  begin
    Application.MessageBox('缸号不能为空!','提示',0);
    Exit;
  end;     

  if  trim(CDS_Sub.FieldByName('AOrddefstr2').AsString)='' then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;

  if  trim(CDS_Sub.FieldByName('ZSXS').AsString)='' then
  begin
    Application.MessageBox('折算系数不能为空!','提示',0);
    Exit;
  end;
  if  trim(CDS_Sub.FieldByName('AOrddefstr3').AsString)='' then
  begin
    application.MessageBox('检验数量单位不能为空!','提示',0);
    Exit;
  end;

  {
  if CDS_Sub.FieldByName('AOrdQty2').AsInteger<> CDS_Roll.RecordCount then
  begin
    Application.MessageBox('细码匹数和总匹数不一致!','提示',0);
    Exit;
  end;


  if CDS_Sub.FieldByName('AOrdQty1').AsFloat<>  tv3.DataController.Summary.FooterSummaryValues[1] then
  begin
    Application.MessageBox('细码数量和总数量不一致!','提示',0);
    Exit;
  end;
   }

  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  if  SaveData10() then
  begin
    MovePanel2.Visible:=False;
    Application.MessageBox('保存成功!','提示',0);
    initRoll();
    fRoll:=true;
    Exit;
  end;
  MovePanel2.Visible:=False;
end;

procedure TfrmProductOrderAnPai.BtnEditA1BtnClick(Sender: TObject);
begin
{ try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FactoryNo1Name';
      flagname:='染厂';
      MainType:='染厂';
      if ShowModal=1 then
      begin

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Cloth_LL A ');
          sql.Add(' where A.FirstNo='''+Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString)+'''');
          sql.Add(' and A.OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
          Application.MessageBox('此染厂没有相对应的数据!','提示',0);

        end else
        begin
          BtnEditA1.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
          BtnEditA1.TxtCode:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString);
        end;

      end;
    end;
  finally
    //frmZDYHelp.Free;
  end;}

try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        BtnEditA1.Text:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        BtnEditA1.TxtCode:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;
function TfrmProductOrderAnPai.YFData():Boolean;
var
  CRID,OrdMainId,YFID,Price,LLID:String;
begin
  Result:=False;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete YF_Money_CR where YFTypeID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
    ExecSQL;
  end;
  OrdMainId:=Trim(Order_Main.fieldbyname('MainId').AsString);
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(CDS_Sub.fieldbyname('AOrddefstr4').AsString)+'''');
    Open;
  end;
  if ADOQueryCmd.IsEmpty=False then
  begin
    CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('FactoryName').Value:=Trim(Order_Main.fieldbyname('FactoryNoName').AsString);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;


  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFTypeID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
    sql.Add('and YFName IN(''加工费'',''回修费'') ');
    Open;
  end;
    if ADOQueryTemp.IsEmpty then
    begin
      if GetLSNo(ADOQueryCmd,YFID,'RJ','YF_Money_CR',3,1)=False then
      begin
        Application.MessageBox('取染费应付最大号失败!','提示',0);
        Exit;
      end;
    end
    else
    begin
      YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where YFID='''+Trim(YFID)+'''');
      Open;
    end;

      with ADOQueryCmd do
      begin
      if ADOQueryTemp.IsEmpty then
        Append
      else
        Edit;
        FieldByName('YFID').Value:=Trim(YFID);
        FieldByName('YFTypeId').Value:=Trim(CDS_Sub.fieldbyname('APId').AsString);
        FieldByName('CRID').Value:=StrToInt(CRID);
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('CRType').Value:='应付款登记';
        FieldByName('CRFlag').Value:='应付付';
        FieldByName('QtyFlag').Value:=1;
        FieldByName('FactoryName').Value:=Trim(CDS_Sub.fieldbyname('AOrddefstr4').AsString);
        FieldByName('CRTime').Value:=CDS_Sub.fieldbyname('ADefDate1').Value;
     //   FieldByName('Qty').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value;
        FieldByName('YFType').Value:='自动生成';
        FieldByName('Price').Value:=0;
        FieldByName('HuiLv').Value:=1;
        FieldByName('BZType').Value:='人民币';
        FieldByName('ComTaiTou').Value:=Trim(CDS_Sub.fieldbyname('AOrddefstr4').AsString);
        FieldByName('QtyUnit').Value:=Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString);
        IF trim(Order_Main.FieldByName('conTk1').AsString)='回修加工' then
         FieldByName('YFName').Value:='回修费'
        else
        FieldByName('YFName').Value:='加工费';
        FieldByName('ps').Value:=CDS_Sub.fieldbyname('AOrdQty2').AsFloat;
        FieldByName('Qty').Value:=CDS_Sub.fieldbyname('AOrdQty1').AsFloat;

        FieldByName('MainId').Value:=Trim(Order_Main.fieldbyname('mainID').AsString);
        FieldByName('subID').Value:=Trim(Order_Main.fieldbyname('subID').AsString);
        FieldByName('P_CodeName').Value:=Trim(CDS_Sub.fieldbyname('C_CodeName').AsString);
        FieldByName('P_Color').Value:=Trim(CDS_Sub.fieldbyname('C_color').AsString);
        IF trim(Order_Main.FieldByName('conTk1').AsString)='正常加工' then
        FieldByName('fstr1').Value:='后加工'
        else IF trim(Order_Main.FieldByName('conTk1').AsString)='回修加工' then
        FieldByName('fstr1').Value:='回修加工'
        else FieldByName('fstr1').Value:='正常加工';
        Post;
      end;

{    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(AOrdQty1),0) from JYOrder_Sub_AnPai A  ');
      sql.Add(' where A.mainID=YF_Money_CR.mainID and  A.subiD=YF_Money_CR.subiD   )');
      sql.Add(',PS=(select isnull(sum(AOrdQty2),0) from JYOrder_Sub_AnPai A  ');
      sql.Add(' where A.mainID=YF_Money_CR.mainID and  A.subiD=YF_Money_CR.subiD   )');
      sql.Add(' where YFID='''+Trim(YFID)+'''');
      ExecSQL;
    END; }
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from YF_Money_CR ');
      sql.Add('where YFID='''+Trim(YFID)+''' and Qty=0 ');
      ExecSQL;
    END;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  Result:=True;
end;
function TfrmProductOrderAnPai.DELYFData():Boolean;
var
  CRID,OrdMainId,YFID,Price,LLID:String;
begin
  Result:=False;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete YF_Money_CR where YFTypeID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
    ExecSQL;
  end;
  OrdMainId:=Trim(Order_Main.fieldbyname('MainId').AsString);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main.fieldbyname('FirstName').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('FactoryName').Value:=Trim(Order_Main.fieldbyname('FirstName').AsString);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select Case when HXQty>0 then HCQty-HXQty else HCQty end as HCQty');
      SQL.Add(',Case when HXPS>0 then HCPS-HXPS else HCPS end as HCPS');
      SQL.Add(' from Contract_Cloth_LL where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
      Open;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  Result:=True;
end;

procedure TfrmProductOrderAnPai.BtnEditA2BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        BtnEditA2.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderAnPai.BtnEditA3BtnClick(Sender: TObject);
begin
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='JYYQ';
      flagname:='检验要求';
      if ShowModal=1 then
      begin
        BtnEditA3.Text:=Trim(BtnEditA3.Text)+frmZDYHelpSel.ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmProductOrderAnPai.ToolButton8Click(Sender: TObject);
begin
  CDS_Sub.DisableControls;
  with CDS_Sub do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=True;
      Post;
      Next;
    end;
  end;
  CDS_Sub.EnableControls;
end;

procedure TfrmProductOrderAnPai.ToolButton7Click(Sender: TObject);
begin
  CDS_Sub.DisableControls;
  with CDS_Sub do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
      Next;
    end;
  end;
  CDS_Sub.EnableControls;
end;

procedure TfrmProductOrderAnPai.BtnEditA4BtnClick(Sender: TObject);
begin
 { try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FactoryNo1Name';
      flagname:='坯布厂';
      MainType:='坯布厂';
      if ShowModal=1 then
      begin
        BtnEditA4.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;   }

try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        BtnEditA1.Text:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        BtnEditA1.TxtCode:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmProductOrderAnPai.V2Column11PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FactoryNo1Name';
      flagname:='坯布厂';
      MainType:='坯布厂';
      if ShowModal=1 then
      begin
        begin
          with CDS_Sub do
          begin
            Edit;
            FieldByName('AOrddefstr6').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
          end;
        end;

      end;
    end;
  finally
    //frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderAnPai.ToolButton9Click(Sender: TObject);
begin
  try
    frmBefChkHX:=TfrmBefChkHX.Create(Application);
    with frmBefChkHX do
    begin
      orderno.Caption:=Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
      PRTColor.Caption:=Trim(Self.Order_Main.fieldbyname('PRTColor').AsString);
      FirstName.Caption:=Trim(Self.Order_Main.fieldbyname('FirstName').AsString);
      PBFactory.Caption:=Trim(Self.Order_Main.fieldbyname('PBFactory').AsString);
      FLLID:=Trim(Self.Order_Main.fieldbyname('LLID').AsString);
      with ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Contract_Cloth_BefChkHX where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
        Open;
      end;
      SCreateCDS20(ADOQuery1,ClientDataSet1);
      SInitCDSData20(ADOQuery1,ClientDataSet1);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmBefChkHX.Free;
  end;
end;

procedure TfrmProductOrderAnPai.ToolButton10Click(Sender: TObject);
begin
   with ADOQuery_Tp do
  begin
    Close;
    sql.Clear;
      sql.Add('select *,PBFactory=(select factoryNoName from Contract_Main X where X.mainID=B.frommainID), ');
      sql.Add('fdhNO=(select conNo from Contract_Main X where X.mainID=B.frommainID) ');
      sql.Add('from Contract_Main A');
      sql.Add('inner join  Contract_sub B on B.MainID=A.mainID ');
      sql.Add('where A.conNO like '+quotedstr('%'+trim(CONnO.Text)+'%'));
      sql.Add('and A.FactoryNoName like '+quotedstr('%'+trim(FactoryNo.Text)+'%'));
      sql.Add('and B.C_CodeName like '+quotedstr('%'+trim(C_CodeName.Text)+'%'));
      sql.Add('and B.C_Color like '+quotedstr('%'+trim(C_Color.Text)+'%'));
      sql.Add(' and A.conType IN(''坯布预投坯'',''成品后加工'') ');
      sql.Add(' and isnull(B.C_status,''0'')=''0'' ');
      IF trim(fCKName)<>'' then
      sql.Add(' and A.SYRname='''+trim(fckName)+''' ');
    open;
  end;
  SCreateCDS20(ADOQuery_Tp,Order_Main);
  SInitCDSData20(ADOQuery_Tp,Order_Main);

  InitGrid();
  BtnEditA1.TxtCode:=Trim(Order_Main.fieldbyname('FactoryNo').AsString);
  BtnEditA1.Text:=Trim(Order_Main.fieldbyname('FactoryNoName').AsString);
  BtnEditA4.Text:=Trim(Order_Main.fieldbyname('PBFactory').AsString);

  if ADOQuery_Tp.Active=False then Exit;
  SDofilter(ADOQuery_Tp,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQuery_Tp,Order_Main);
  SInitCDSData20(ADOQuery_Tp,Order_Main);
end;

procedure TfrmProductOrderAnPai.ToolButton12Click(Sender: TObject);
begin
  if cds_Roll.IsEmpty then exit;

  {    with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete JYOrder_Sub_AnPai_MX where APID='''+Trim(CDS_Roll.fieldbyname('APID').AsString)+'''');
        sql.Add('and APsubID='''+Trim(CDS_Roll.fieldbyname('APsubID').AsString)+'''');
        ExecSQL;
      end;    }
    cds_Roll.Delete;
end;

procedure TfrmProductOrderAnPai.TV3CustomDrawIndicatorCell(
  Sender: TcxGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
   if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    FValue :=IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index+1);
    FBounds := AViewInfo.Bounds;
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bBottom, bLeft, bRight], 1);
    InflateRect(FBounds, -1, -1);
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

procedure TfrmProductOrderAnPai.Edit1KeyPress(Sender: TObject;
  var Key: Char);
var
  fAPTM:string;
  i:integer;
begin
  if cds_sub.IsEmpty then exit;
  if key=#13 then
  begin
    for i:=1 to strtointdef(cxCurrencyEdit3.Text,1) do
    begin
      if GetLSNo(ADOQueryCmd,fAPTM,'','APTM',4,1)=False then
      begin
        Application.MessageBox('取子流水号失败！','提示',0);
        Exit;
      end;
      with CDS_Roll do
      begin
        DisableControls;
        last;
        append;
        FieldByName('AOrdQty1').Value:=strtofloatdef(trim(edit1.Text),0);
        FieldByName('AOrdQty2').Value:=1;
        FieldByName('apid').Value:=CDS_Sub.fieldbyname('apid').AsString;
        FieldByName('Aorddefstr1').Value:='正品';
        FieldByName('APTM').Value:=trim(fAPTM);
        post;
        EnableControls;
      end;
      with CDS_Sub do
      begin
        edit;
        FieldByName('AOrdQty1').Value:=tv3.DataController.Summary.FooterSummaryValues[1];
        FieldByName('AOrdQty2').Value:=CDS_Roll.RecordCount;
        post;
      end;
      self.tv2.Controller.EditingController.ShowEdit();
      self.tv3.Controller.EditingController.ShowEdit();
    end;
    edit1.text:='';
  end;

end;

procedure TfrmProductOrderAnPai.cxCurrencyEdit1KeyPress(Sender: TObject;
  var Key: Char);
var
  fAPTM:string;
begin
{  if cds_sub.IsEmpty then exit;
  if key=#13 then
  begin
    with CDS_Roll do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        edit;
        FieldByName('AOrdQty3').Value:=FieldByName('AOrdQty1').AsFloat* strtofloatdef(trim(cxCurrencyEdit1.Text),0);
        Post;
        next;
      end;
      first;
      EnableControls;
    end;
      self.tv3.Controller.EditingController.ShowEdit();
  end;   }


  if key=#13 then
  begin
    if cds_sub.IsEmpty then exit;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from JYOrder_Sub_AnPai ');
      sql.Add('where Apid='+quotedstr(trim(CDS_Roll.fieldbyname('apid').AsString)));
      open;
      if  isempty then
      begin
        application.MessageBox('此回仓单未保存，不能保存','提示信息',0);
        exit;
      end;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY ');
      sql.Add('where MJID='+quotedstr(trim(CDS_Roll.fieldbyname('apTm').AsString)));
      sql.Add('and mjstr2<>'+quotedstr(trim('未入库')));
      open;
      if not isempty then
      begin
        application.MessageBox('此回仓单已入库','提示信息',0);
        exit;
      end;
    end;

    
    with CDS_Roll do
    begin
      IF (Eof) and (fieldbyname('AOrdQty3').AsFloat<>0)  then
      begin
        if GetLSNo(ADOQueryCmd,fAPTM,'','APTM',4,1)=False then
        begin
          Application.MessageBox('取子流水号失败！','提示',0);
          Exit;
        end;
        append;
        FieldByName('AOrdQty1').Value:=0;
        FieldByName('AOrdQty2').Value:=1;
        FieldByName('apid').Value:=CDS_Sub.fieldbyname('apid').AsString;
        FieldByName('Aorddefstr1').Value:='正品';
        FieldByName('APTM').Value:=trim(fAPTM);
        post;
      end;
      edit;
      FieldByName('AOrdQty3').Value:=strtofloatdef(trim(cxCurrencyEdit1.Text),0);
      Post;
      IF not SaveMJID() then
      begin
        edit;
        FieldByName('AOrdQty3').Value:=0;
        Post;
        application.MessageBox('数据保存失败！','提示信息',0);
        self.tv3.Controller.EditingController.ShowEdit();
        exit;
      end;
      next;
    end;
    self.tv3.Controller.EditingController.ShowEdit();
    cxCurrencyEdit1.text:='';
  end;
end;

procedure TfrmProductOrderAnPai.Button1Click(Sender: TObject);
begin
  Edit1.SetFocus;


  if strtofloatdef(trim(edit3.Text),0)<>  CDS_Roll.RecordCount then
  begin
    Application.MessageBox('检验匹数不正确!','提示',0);
    Exit;
  end;

  if  strtofloatdef(trim(edit4.Text),0)<>  tv3.DataController.Summary.FooterSummaryValues[2] then
  begin
    Application.MessageBox('检验数量不正确!','提示',0);
    Exit;
  end;
  
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from JYOrder_Sub_AnPai ');
    sql.Add('where apid='+quotedstr(trim(CDS_Sub.fieldbyname('apid').AsString)));
    open;
    if  isempty then
    begin
      application.MessageBox('此回仓单未保存，不能检验保存','提示信息',0);
      exit;
    end;
  end;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from WFB_MJJY ');
    sql.Add('where apid='+quotedstr(trim(CDS_Sub.fieldbyname('apid').AsString)));
    sql.Add('and mjstr2<>'+quotedstr(trim('未入库')));
    open;
    if not isempty then
    begin
      application.MessageBox('此回仓单已入库','提示信息',0);
      exit;
    end;
  end;
  ADOQueryCmd.Connection.BeginTrans;
  try

      with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('delete  from JYOrder_Sub_AnPai_MX where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          execsql;
        end;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub_AnPai_MX where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          Open;
        end;
        CDS_Roll.DisableControls;
        CDS_Roll.First;
        while not CDS_Roll.Eof do
        begin
          IF trim(CDS_Roll.fieldbyname('apid').AsString)='' then CDS_Roll.delete;
          with ADOQueryCmd do
          begin
            append;
            FieldByName('ApId').Value:=Trim(CDS_Sub.fieldbyname('apid').AsString);
            FieldByName('ApsubId').Value:= CDS_Roll.recNo;
            FieldByName('APTM').Value:=Trim(CDS_Sub.fieldbyname('APTM').AsString);
            FieldByName('AOrdQty2').Value:=CDS_Roll.fieldbyname('AOrdQty2').AsInteger;
            FieldByName('AOrdQty1').Value:=CDS_Roll.fieldbyname('AOrdQty1').AsFloat;
            FieldByName('AOrdQty3').Value:=CDS_Roll.fieldbyname('AOrdQty3').AsFloat;
            FieldByName('AorddefNote1').Value:=Trim(CDS_Roll.fieldbyname('AorddefNote1').AsString);
            FieldByName('Aorddefstr1').Value:=Trim(CDS_Roll.fieldbyname('Aorddefstr1').AsString);
            post;
          end;
          CDS_Roll.Next;
        end;
        CDS_Roll.EnableControls;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete  from WFB_MJJY ');
      sql.Add('where apid='+quotedstr(trim(CDS_Sub.fieldbyname('apid').AsString)));
      execsql;
    end;

        CDS_Roll.DisableControls;
        CDS_Roll.First;
        while not CDS_Roll.Eof do
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select *  from WFB_MJJY ');
            sql.Add('where 1=2 ');
            open;
            append;
            FieldByName('MainId').value:=Trim(cds_sub.fieldbyname('mainiD').AsString);
            FieldByName('SubId').value:=Trim(cds_sub.fieldbyname('subid').AsString);
            FieldByName('conNo').Value:=Trim(cds_sub.fieldbyname('conNo').AsString);
            FieldByName('APId').value:=Trim(cds_sub.fieldbyname('apid').AsString);
            FieldByName('MJID').Value:=Trim(CDS_Sub.fieldbyname('APTM').AsString);
            FieldByName('MJSTr1').value:=Trim(cds_sub.fieldbyname('AOrddefstr2').AsString);
            FieldByName('MJTypeOther').value:=Trim(cds_sub.fieldbyname('AOrddefstr3').AsString);
            FieldByName('MJStr2').Value:='未入库';
            FieldByName('MJType').Value:=Trim(CDS_Roll.fieldbyname('Aorddefstr1').AsString);
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('MJLen').Value:=CDS_Roll.fieldbyname('AOrdQty3').AsFloat;
            FieldByName('MJQty1').Value:=CDS_Roll.fieldbyname('AOrdQty1').AsFloat;
            FieldByName('MJxh').Value:=CDS_Roll.fieldbyname('apsubid').AsFloat;
            FieldByName('note').Value:=Trim(CDS_Roll.fieldbyname('AorddefNote1').AsString);
            post;
          end;
          CDS_Roll.Next;
        end;
        CDS_Roll.EnableControls;
        CDS_Roll.First;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('检验保存成功!','提示信息');
  except

    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('检验保存失败!','提示信息',0);
  end;
end;

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

procedure TfrmProductOrderAnPai.Button2Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('APID').AsString)<>'' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 1* from CK_BanCP_CR where APID='''+Trim(CDS_Sub.fieldbyname('APID').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_Sub_AnPai_MX  SET AOrdQty3=0 where APID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
        sql.Add('delete WFB_MJJY where APID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      InitRoll();
    except
      ADOQueryCmd.Connection.RollbackTrans;
    end;
  end;


end;

procedure TfrmProductOrderAnPai.V2Column13PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;


  with CDS_Sub do
  begin
    Edit;
    FieldByName('C_Color').Value:=Trim(mvalue);
    Post;
  end;
  self.tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmProductOrderAnPai.V2C_CodeNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
{  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='MPRTCodeName';
      flagname:='产品名称';
      V1Name.Caption:='中文名称';
      V1Note.Caption:='英文名称';
//      MainType:=Trim(DName);
      fnote:=FALSE;
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('C_Code').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Self.CDS_Sub.FieldByName('C_CodeName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'#';
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.CDS_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;  }
 try
    frmZdyAttachCP:=TfrmZdyAttachCP.Create(Application);
    with frmZdyAttachCP do
    begin
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('C_Code').Value:=Trim(CDS_HZ.fieldbyname('ZdyCode').AsString);
        Self.CDS_Sub.FieldByName('C_CodeName').Value:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
   //     Self.Order_Sub.FieldByName('Color').Value:=Trim(CDS_HZ.fieldbyname('DEFstr3').AsString);
    //    Self.Order_Sub.FieldByName('SOrddefstr4').Value:=Trim(ClientDataSet1.fieldbyname('Note').AsString);
        Self.CDS_Sub.Post;
      end;
    end;
  finally
    frmZdyAttachCP.Free;
  end;
   self.tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmProductOrderAnPai.V2C_CodeNamePropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;

  IF trim(mvalue)='' then exit;

  IF rightBstr(trim(mvalue),1)<>'#'THEN
  BEGIn
    mvalue:=trim(mvalue)+'#';
  END;


  with CDS_Sub do
  begin
    Edit;
    FieldByName('C_CodeName').Value:=Trim(mvalue);
    Post;
  end;
  self.tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmProductOrderAnPai.TV3MouseEnter(Sender: TObject);
begin
  cxGrid3.SetFocus;
  tv3.Controller.EditingController.ShowEdit();
end;

procedure TfrmProductOrderAnPai.TV2FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  if fRoll then
  InitRoll();
end;

procedure TfrmProductOrderAnPai.TV5FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  InitGrid();
  BtnEditA1.TxtCode:=Trim(Order_Main.fieldbyname('factoryNo').AsString);
  BtnEditA1.Text:=Trim(Order_Main.fieldbyname('factoryNoName').AsString);
  BtnEditA4.Text:=Trim(Order_Main.fieldbyname('PBFactory').AsString); 
  end;

procedure TfrmProductOrderAnPai.FactoryNoBtnClick(Sender: TObject);
begin
 try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        FactoryNo.Text:=Trim(CDS_HZ.fieldbyname('zdyName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

procedure TfrmProductOrderAnPai.cxCurrencyEdit2KeyPress(Sender: TObject;
  var Key: Char);
begin



  if key=#13 then
  begin
    if cds_sub.IsEmpty then exit;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from JYOrder_Sub_AnPai ');
      sql.Add('where Apid='+quotedstr(trim(cds_sub.fieldbyname('apid').AsString)));
      open;
      if  isempty then
      begin
        application.MessageBox('此回仓单未保存，不能保存','提示信息',0);
        exit;
      end;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY ');
      sql.Add('where apid='+quotedstr(trim(CDS_Sub.fieldbyname('apid').AsString)));
      sql.Add('and mjstr2<>'+quotedstr(trim('未入库')));
      open;
      if not isempty then
      begin
        application.MessageBox('此回仓单已入库','提示信息',0);
        exit;
      end;
    end;
    with CDS_Roll do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        edit;
        FieldByName('AOrdQty3').Value:=FieldByName('AOrdQty1').AsFloat* strtofloatdef(trim(cxCurrencyEdit2.Text),0);
        Post;
        IF not SaveMJID() then
        begin
          edit;
          FieldByName('AOrdQty3').Value:=0;
          Post;
          application.MessageBox('数据保存失败！','提示信息',0);
          EnableControls;
          self.tv3.Controller.EditingController.ShowEdit();
          exit;
        end;
        next;
      end;
      first;
      EnableControls;
    end;

    self.tv3.Controller.EditingController.ShowEdit();
    cxCurrencyEdit2.Text:='';
  end;   

end;

procedure TfrmProductOrderAnPai.V2Column14PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmClothContractList_sel:=TfrmClothContractList_sel.create(self);
  with frmClothContractList_sel do
  begin
    ffactoryName:=self.Order_Main.fieldbyname('FactoryNoName').AsString;
    C_CodeName.Text:=self.Order_Main.fieldbyname('C_CodeName').AsString;
    if showmodal=1 then
    begin
      CDS_Sub.Edit;
      CDS_Sub.FieldByName('conNo').Value:= Order_Main.fieldbyname('conNo').AsString;
      CDS_Sub.post;
    end;
    free;
  end;
  self.tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmProductOrderAnPai.V2Column14PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;

 // IF trim(mvalue)='' then exit;
  with CDS_Sub do
  begin
    Edit;
    FieldByName('conNo').Value:=Trim(mvalue);
    Post;
  end;


 { with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select *  from Contract_Main A ');
    sql.Add('where conNo='+quotedstr(trim(CDS_Sub.fieldbyname('conNo').AsString)));
    sql.Add('and  conType='+quotedstr(trim('坯布到货')));
    open;
  end;
  IF ADOQueryCmd.IsEmpty then
  begin
    application.MessageBox('任务单号不存在！','提示信息',0);
    with CDS_Sub do
    begin
      Edit;
      FieldByName('conNo').Value:='';
      Post;
    end;
  end;                        }
  self.tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmProductOrderAnPai.TV2CustomDrawIndicatorCell(
  Sender: TcxGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
   if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    FValue :=IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index+1);
    FBounds := AViewInfo.Bounds;
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bBottom, bLeft, bRight], 1);
    InflateRect(FBounds, -1, -1);
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

procedure TfrmProductOrderAnPai.V3Column3PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:double;
begin
  mvalue:=strtofloatdef(trim(TcxTextEdit(Sender).EditingText),0);
  with CDS_Roll do
  begin
    Edit;
    FieldByName('AOrdQty1').Value:=mvalue;
    Post;
  end;
  IF CDS_Roll.fieldbyname('APSubID').AsInteger<1 then exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add(' update JYOrder_Sub_AnPai_MX SET AOrdQty1='+quotedstr(trim(CDS_Roll.fieldbyname('AOrdQty1').AsString)));
    sql.Add(' where apID='+quotedstr(trim(CDS_Roll.fieldbyname('APID').AsString)));
    sql.Add(' and apSubID='+quotedstr(trim(CDS_Roll.fieldbyname('apSubID').AsString)));
    sql.Add(' update WFB_MJJY SET MJQty1='+quotedstr(trim(CDS_Roll.fieldbyname('AOrdQty1').AsString)));
    sql.Add(' where apID='+quotedstr(trim(CDS_Roll.fieldbyname('APID').AsString)));
    sql.Add(' and MJXh='+quotedstr(trim(CDS_Roll.fieldbyname('apSubID').AsString)));
    execsql;
  end;
end;

procedure TfrmProductOrderAnPai.V3Column4PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:double;
begin

  mvalue:=CDS_Roll.fieldbyname('AOrdQty3').AsFloat;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add(' select *from WFB_MJJY ');
    sql.Add(' where MJID='+quotedstr(trim(CDS_Roll.fieldbyname('APTM').AsString)));
    sql.Add(' and MJStr2='+quotedstr(trim('已入库')));
    open;
    if ADOQueryCmd.RecordCount>0 then
    begin
      application.MessageBox('此回仓单已入库','提示信息',0);
      with CDS_Roll do
      begin
        Edit;
        FieldByName('AOrdQty3').Value:=mvalue;
        Post;
      end;
      exit;
    end;
  end;
  mvalue:=strtofloatdef(trim(TcxTextEdit(Sender).EditingText),0);

  with CDS_Roll do
  begin
    Edit;
    FieldByName('AOrdQty3').Value:=mvalue;
    Post;
  end;

  IF not SaveMJID() then
  begin
    with CDS_Roll do
    begin
      Edit;
      FieldByName('AOrdQty3').Value:=0;
      Post;
    end;
    application.MessageBox('数据保存失败！','提示信息',0);
    self.tv3.Controller.EditingController.ShowEdit();
    exit;
  end;


end;

procedure TfrmProductOrderAnPai.V3Aorddefstr1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=trim(CDS_Roll.fieldbyname('Aorddefstr1').AsString);
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add(' select *from WFB_MJJY ');
    sql.Add(' where MJID='+quotedstr(trim(CDS_Roll.fieldbyname('APTM').AsString)));
    sql.Add(' and MJStr2='+quotedstr(trim('已入库')));
    open;
    if ADOQueryCmd.RecordCount>0 then
    begin
      application.MessageBox('此回仓单已入库','提示信息',0);
      with CDS_Roll do
      begin
        Edit;
        FieldByName('Aorddefstr1').Value:=mvalue;
        Post;
      end;
      exit;
    end;
  end;
  mvalue:=trim(TcxTextEdit(Sender).EditingText);

  with CDS_Roll do
  begin
    Edit;
    FieldByName('Aorddefstr1').Value:=mvalue;
    Post;
  end;

  IF not SaveMJID() then
  begin
    with CDS_Roll do
    begin
      Edit;
      FieldByName('Aorddefstr1').Value:='';
      Post;
    end;
    application.MessageBox('数据保存失败！','提示信息',0);
    self.tv3.Controller.EditingController.ShowEdit();
    exit;
  end;


end;

procedure TfrmProductOrderAnPai.V3notePropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=trim(CDS_Roll.fieldbyname('AorddefNote1').AsString);
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add(' select *from WFB_MJJY ');
    sql.Add(' where MJID='+quotedstr(trim(CDS_Roll.fieldbyname('APTM').AsString)));
    sql.Add(' and MJStr2='+quotedstr(trim('已入库')));
    open;
    if ADOQueryCmd.RecordCount>0 then
    begin
      application.MessageBox('此回仓单已入库','提示信息',0);
      with CDS_Roll do
      begin
        Edit;
        FieldByName('AorddefNote1').Value:=mvalue;
        Post;
      end;
      exit;
    end;
  end;
  mvalue:=trim(TcxTextEdit(Sender).EditingText);

  with CDS_Roll do
  begin
    Edit;
    FieldByName('AorddefNote1').Value:=mvalue;
    Post;
  end;

  IF not SaveMJID() then
  begin
    with CDS_Roll do
    begin
      Edit;
      FieldByName('AorddefNote1').Value:='';
      Post;
    end;
    application.MessageBox('数据保存失败！','提示信息',0);
    self.tv3.Controller.EditingController.ShowEdit();
    exit;
  end;


end;

procedure TfrmProductOrderAnPai.TV5CustomDrawIndicatorCell(
  Sender: TcxGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
   if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    FValue :=IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index+1);
    FBounds := AViewInfo.Bounds;
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bBottom, bLeft, bRight], 1);
    InflateRect(FBounds, -1, -1);
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;
end.
