unit U_DJBCKInPut_JTZS;

interface

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

type
  TfrmDJBCKInPut_JTZS = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1ZSDate: TcxGridDBColumn;
    v2BTMoney: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    v1SCPerson: TcxGridDBColumn;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1GJPrice: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    v1YGMoney: TcxGridDBColumn;
    v1CarNo: TcxGridDBColumn;
    v1JBQty: TcxGridDBColumn;
    v1EndQty: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1BTNote: TcxGridDBColumn;
    v1BegQty: TcxGridDBColumn;
    v1C_CodeName: TcxGridDBColumn;
    v1MPZQty: TcxGridDBColumn;
    v1PS: TcxGridDBColumn;
    TSDZH: TToolButton;
    v1JYDate: TcxGridDBColumn;
    v1WCDate: TcxGridDBColumn;
    v1KKMoney: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column14PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column11PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column16PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column18PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1SCPersonPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1BTNotePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TBAddClick(Sender: TObject);
    procedure v1BegQtyPropertiesEditValueChanged(Sender: TObject);
    procedure TSDZHClick(Sender: TObject);
    procedure v1C_CodeNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveCKData():Boolean;
    function YFData():Boolean;
  public
    { Public declarations }
    FZSID:String;
  end;

var
  frmDJBCKInPut_JTZS: TfrmDJBCKInPut_JTZS;

implementation
uses
  U_DataLink,U_RTFun,U_ClothContractListHZCX,U_ZDYHelp,U_ZDYHelpSel,U_ZdyAttachGYS,U_ProductOrderListSel,U_JYOrderCDOne;

{$R *.dfm}

function TfrmDJBCKInPut_JTZS.YFData():Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName,FKHName:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select TOP 1 * from JYOrder_Main where Mainid='+quotedstr(Trim(CDS_Sub.fieldbyname('ORDMainIdRK').AsString)));
    Open;
  end;
  FComTaiTou:=trim(ADOQueryTemp.fieldbyname('ComTaiTou').AsString);
  FKHName:=trim(ADOQueryTemp.fieldbyname('CustomerNoName').AsString);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='+quotedstr(Trim(FKHName)));
    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(Trim(FKHName));
      FieldByName('ZdyStr1').Value:='应收收';
      Post;
    end;
  end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where FactoryName='+quotedstr(Trim(FKHName)));
        sql.Add(' and YFTypeId='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
        sql.add(' and CRTime='''+trim(formatdatetime('yyyy-MM-dd',CDS_Sub.fieldbyname('CRTime').AsDateTime))+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
      if GetLSNo(ADOQueryCmd,YFID,'YF','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('SPID').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(FKHName);
        FieldByName('CRTime').Value:=trim(formatdatetime('yyyy-MM-dd',CDS_Sub.fieldbyname('CRTime').AsDateTime));
        FieldByName('PBNote').Value:=trim(CDS_Sub.fieldbyname('Note').AsString);
        FieldByName('YFType').Value:='自动生成';
        FieldByName('Price').Value:=0;
        FieldByName('money').Value:=0;
        FieldByName('BBmoney').Value:=0;
        FieldByName('HuiLv').Value:=1;
        fieldbyname('SLV').Value:=1;
        FieldByName('BZType').Value:='RMB';
        FieldByName('ComTaiTou').Value:=FComTaiTou;
        FieldByName('QtyUnit').Value:=Trim(CDS_Sub.fieldbyname('QtyUnit').AsString);
        FieldByName('P_CodeName').Value:=Trim(CDS_Sub.fieldbyname('SPName').AsString);
        fieldbyname('OrderNo').Value:=trim(CDS_Sub.fieldbyname('OrderNo').AsString);
        fieldbyname('P_Spec').Value:=trim(CDS_Sub.fieldbyname('SPSpec').AsString);
        //fieldbyname('PBChang').Value:=trim(CDS_Sub.fieldbyname('PBChang').AsString);
        //fieldbyname('GYSName').Value:=trim(CDS_Sub.fieldbyname('GYSName').AsString);

        fieldbyname('YFName').Value:='成品退回费';
        fieldbyname('FeeType').Value:='其他费用';
        FieldByName('MainId').Value:=Trim(CDS_Sub.fieldbyname('ORDMainIdRK').AsString);       //汇总
        FieldByName('status').Value:='0';
        Post;
      end;
        with ADOQueryCmd do
        begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_CR Set ');
        if trim(CDS_Sub.fieldbyname('QtyUnit').AsString)<>'KG' then
        begin
          sql.add('MQty=(select -isnull(Sum(Qty),0) from CK_SXPB_CR A where A.SPID=YF_Money_CR.YFTypeId and YF_Money_CR.CRTime=convert(varchar(10),A.CRTime,120) and A.CRType=''成品退回'' and A.CRFlag=''入库'')');
        end
        else
        sql.add('MaoQty=(select -isnull(Sum(Qty),0) from CK_SXPB_CR A where A.SPID=YF_Money_CR.YFTypeId and YF_Money_CR.CRTime=convert(varchar(10),A.CRTime,120) and A.CRType=''成品退回'' and A.CRFlag=''入库'')');
        sql.Add(',Qty=(select -isnull(Sum(Qty),0) from CK_SXPB_CR A where A.SPID=YF_Money_CR.YFTypeId and YF_Money_CR.CRTime=convert(varchar(10),A.CRTime,120) and A.CRType=''成品退回'' and A.CRFlag=''入库'')');
        sql.Add(',PS=(select -isnull(Sum(PiQty),0) from CK_SXPB_CR A where A.SPID=YF_Money_CR.YFTypeId and YF_Money_CR.CRTime=convert(varchar(10),A.CRTime,120) and A.CRType=''成品退回'' and A.CRFlag=''入库'')');
        sql.Add(' where YFTypeId='''+trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
        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;

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

function TfrmDJBCKInPut_JTZS.SaveCKData():Boolean;
var
  FCRID,Maxno,FFSPID:string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        if Trim(FZSID)='' then
        begin
          if GetLSNo(ADOQueryCmd,Maxno,'ZS','Cloth_JiTaiZShu',3,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取自动编号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          Maxno:=Trim(FZSID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Cloth_JiTaiZShu where ZSID='''+Trim(Maxno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if isempty then
          begin
            Append;
          end else
          begin
            Edit;
          end;
          FieldByName('ZSID').Value:=Trim(Maxno);
          FieldByName('Mainid').Value:=Trim(CDS_Sub.fieldbyname('Mainid').AsString);
          FieldByName('Subid').Value:=Trim(CDS_Sub.fieldbyname('Subid').AsString);
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'Cloth_JiTaiZShu',2);
          FieldByName('MPZQty').Value:=CDS_Sub.fieldbyname('MPZQty').AsFloat;
          FieldByName('PS').Value:=CDS_Sub.fieldbyname('PS').AsFloat;
          FieldByName('KKMoney').Value:=CDS_Sub.fieldbyname('KKMoney').AsFloat;
          FieldByName('Filler').Value:=Trim(DName);
          Post;
        end;
        Edit;
        FieldByName('ZSID').Value:=Trim(Maxno);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;

procedure TfrmDJBCKInPut_JTZS.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('机台转数登记',Tv1,'待检布仓库');
  Close;
end;

procedure TfrmDJBCKInPut_JTZS.FormShow(Sender: TObject);
var
  fsj:String;
begin
  ReadCxGrid('机台转数登记',Tv1,'待检布仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,cast('''' as varchar(50)) C_CodeName ');
    sql.Add(' from Cloth_JiTaiZShu A');
    sql.Add(' where ZSID='''+Trim(FZSID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Sub);
  SInitCDSData20(ADOQueryTemp,CDS_Sub);
end;

procedure TfrmDJBCKInPut_JTZS.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('ZSID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete from Cloth_JiTaiZShu where ZSID='''+Trim(CDS_Sub.fieldbyname('ZSID').AsString)+'''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
    end;
  end;
  CDS_Sub.Delete;
end;

procedure TfrmDJBCKInPut_JTZS.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if CDS_Sub.Locate('ZSDate',null,[])=True then
  begin
    Application.MessageBox('登记时间不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('SCPerson',null,[])=True then
  begin
    Application.MessageBox('挡车工不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('CarNo',null,[])=True then
  begin
    Application.MessageBox('机台号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('WCDate',null,[])=True then
  begin
    Application.MessageBox('完成日期不能为空','提示',0);
    Exit;
  end;
  with cds_Sub do
  begin
    first;
    while not eof do
    begin
      if Fieldbyname('BTMoney').AsFloat<>0 then
      begin
        if fieldbyname('BTNote').AsString='' then
        begin
          application.MessageBox('补贴原因不能为空','提示');
          exit;
        end;
      end;
      next;
    end;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  end;
end;

procedure TfrmDJBCKInPut_JTZS.v1Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SXKuWei';
      flagname:='库位';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('KuWei').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDJBCKInPut_JTZS.v1Column14PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          edit;
          FieldByName('FactoryNo').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyCode').AsString) ;
          FieldByName('FactoryName').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyName').AsString);
          FieldByName('FromFactoryNo').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyCode').AsString) ;
          FieldByName('FromFactoryName').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

procedure TfrmDJBCKInPut_JTZS.v2Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WJGName';
      flagname:='外加工名称';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SPName').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDJBCKInPut_JTZS.v1Column5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KuWei';
      flagname:='库位';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('KuWei').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDJBCKInPut_JTZS.v1Column11PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          edit;
          FieldByName('ToFactoryNo').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyCode').AsString) ;
          FieldByName('ToFactoryName').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmDJBCKInPut_JTZS.v1Column16PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          edit;
          FieldByName('FromFactoryNo').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyCode').AsString) ;
          FieldByName('FromFactoryName').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmDJBCKInPut_JTZS.v1Column18PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KuWei';
      flagname:='库位';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('KuWei').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDJBCKInPut_JTZS.v1SCPersonPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SCPerson';
      flagname:='挡车工';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SCPerson').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDJBCKInPut_JTZS.v1BTNotePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
var FBTNote:string;
begin
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='BTNote';
      flagname:='补贴原因';
      if ShowModal=1 then
      begin
        with ClientDataSet1 do
        begin
          first;
          while not eof do
          begin
            if fieldbyname('Ssel').AsBoolean=true then
            begin
              if FBTNote='' then
              FBTNote:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)
              else
              FBTNote:=FBTNote+','+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              with Self.CDS_Sub do
              begin
                Edit;
                FieldByName('BTNote').Value:=trim(FBTNote);
              end;
            end;
            next;
          end
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmDJBCKInPut_JTZS.TBAddClick(Sender: TObject);
begin
  v1ConNo.Options.Editing:=false;
  v1C_CodeName.Options.Editing:=false;
  //v1SCPerson.Options.Editing:=false;
  v1CarNo.Options.Editing:=false;
  v1ZSDate.Options.Editing:=false;
  frmJYOrderCDOnePB:=TfrmJYOrderCDOnePB.Create(self);
  with frmJYOrderCDOnePB do
  begin
    if showmodal=1 then
    begin
      with Order_Main do
      begin
        first;
        while not eof do
        begin
          if fieldbyname('Ssel').AsBoolean=true then
          begin
            with CDS_Sub do
            begin
              Append;
              fieldbyname('ConNo').Value:=trim(Order_Main.fieldbyname('ConNo').AsString);
              fieldbyname('Mainid').Value:=trim(Order_Main.fieldbyname('Mainid').AsString);
              fieldbyname('Subid').Value:=trim(Order_Main.fieldbyname('Subid').AsString);
              fieldbyname('C_CodeName').Value:=trim(Order_Main.fieldbyname('C_CodeName').AsString);
              fieldbyname('ZSDate').Value:=Order_Main.fieldbyname('APDate').AsDateTime;
              fieldbyname('JYDate').Value:=Order_Main.fieldbyname('JYDate').AsDateTime;
              fieldbyname('SCPerson').Value:=trim(Order_Main.fieldbyname('SCPerson').AsString);
              fieldbyname('CarNo').Value:=trim(Order_Main.fieldbyname('CarNo').AsString);
              fieldbyname('WCDate').Value:=Order_Main.fieldbyname('JYDate').AsDateTime;
              with ADOQueryTemp do
              begin
                close;
                sql.Clear;
                sql.Add('select Top 1 AA.EndQty from Cloth_JiTaiZShu AA ');
                sql.add('where AA.CarNo='''+trim(Order_Main.fieldbyname('CarNo').AsString)+''' ');
                sql.add('Order By AA.filltime desc');
                open;
              end;
              fieldbyname('JBQty').Value:=ADOQueryTemp.fieldbyname('EndQty').AsFloat;
              post;
            end;
          end;
          next;
        end;
      end;
    end;
    free;
  end;
end;

procedure TfrmDJBCKInPut_JTZS.v1BegQtyPropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FPrice,FMoney,FHuiLv,FBBMoney,FKCBB,FFieldName:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  if (FFieldName<>'Qty') and (FFieldName<>'GJPrice') and (FFieldName<>'BTMoney') and (FFieldName<>'MPZQty') and (FFieldName<>'KKMoney') then
  begin
    with CDS_Sub do
    begin
      Edit;
      if fieldbyname('BegQty').AsFloat=0 then
      fieldbyname('Qty').Value:=fieldbyname('EndQty').AsFloat-fieldbyname('BegQty').AsFloat
      else
      fieldbyname('Qty').Value:=fieldbyname('EndQty').AsFloat-fieldbyname('JBQty').AsFloat;
      Post;
    end;
  end;
  with CDS_Sub do
  begin
    Edit;
    fieldbyname('YGMoney').Value:=fieldbyname('Qty').AsFloat*fieldbyname('GJPrice').AsFloat/10000+fieldbyname('BTMoney').AsFloat-fieldbyname('KKMoney').AsFloat;
    if fieldbyname('MPZQty').AsFloat<>0 then
    fieldbyname('PS').Value:=fieldbyname('Qty').AsFloat/fieldbyname('MPZQty').AsFloat;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmDJBCKInPut_JTZS.TSDZHClick(Sender: TObject);
begin
  v1ConNo.Options.Editing:=true;
  v1C_CodeName.Options.Editing:=true;
  v1SCPerson.Options.Editing:=true;
  v1CarNo.Options.Editing:=true;
  v1ZSDate.Options.Editing:=true;
  with CDS_Sub do
  begin
    append;
    post;
  end;
end;

procedure TfrmDJBCKInPut_JTZS.v1C_CodeNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='C_CodeName';
      flagname:='产品名称';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('C_CodeName').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
