unit U_PBRKInPut;

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, StdCtrls, ExtCtrls;

type
  TfrmPBRKInPut = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v2SPName: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Column12: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1SPCF: TcxGridDBColumn;
    v1SPSpec: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1BatchNo: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    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 v1Column10PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column15PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column7PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    FSPType,FDelStr:String;
    function SaveCKData():Boolean;
  public
    { Public declarations }
    FBCId,canshu3,FHZCRType,ZHFlag:String;

  end;

var
  frmPBRKInPut: TfrmPBRKInPut;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp,
  U_ZdyAttachCP,U_ZdyAttachGYS;

{$R *.dfm}

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

procedure TfrmPBRKInPut.TBAddClick(Sender: TObject);
begin
  if Trim(ZHFlag)='1' then
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('FHDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',Now));
      FieldByName('Unit').Value:='Kg';

      Post;
    end;
  end ;
end;
function TfrmPBRKInPut.SaveCKData():Boolean;
var
  FCRID,Maxno,FFSPID,FQMID,maxMXId:string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from SupplierDeliver where FHId='''+Trim(CDS_Sub.fieldbyname('FHId').AsString)+'''');
          open;
        end;
        FFSPID:=Trim(ADOQueryTemp.fieldbyname('FHId').AsString);
        if Trim(FFSPID)='' then
        begin
          if GetLSNo(ADOQueryCmd,Maxno,'FH','SupplierDeliver',3,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取坯布编号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          Maxno:=Trim(FFSPID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from SupplierDeliver where FHId='''+Trim(Maxno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FFSPID)='' then
          begin
            Append;
          end else
          begin
            Edit;
          end;
          FieldByName('FHId').Value:=Trim(Maxno);
          FieldByName('FhType').Value:='坯布';


          FieldByName('SupplierNo').Value:=Trim(CDS_Sub.fieldbyname('SupplierNo').AsString);


           FieldByName('SHWCFlag').Value:=1;
          FieldByName('Status').Value:=0;

          RTSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'SupplierDeliver',2);
          
          if Trim(FFSPID)='' then
            FieldByName('Filler').Value:=Trim(DName)
          else
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDate(ADOQueryTemp);
          end;
          Post;
        end;


        Edit;
        FieldByName('FHId').Value:=Trim(Maxno);
        //FieldByName('CRID').Value:=Trim(FCRID);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
  
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;

end;
procedure TfrmPBRKInPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption),Tv1,'纱线仓库');
  Close;
end;

procedure TfrmPBRKInPut.FormShow(Sender: TObject);
var
  fsj:String;
begin
  ReadCxGrid(trim(Self.Caption),Tv1,'纱线仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from SupplierDeliver A');
    sql.Add(' where FHId='''+Trim(FBCId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Sub);
  SInitCDSData20(ADOQueryTemp,CDS_Sub);

end;

procedure TfrmPBRKInPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('SPID').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 SupplierDeliver where FHId='''+Trim(CDS_Sub.fieldbyname('FHId').AsString)+'''');
        ExecSQL;
      end;
 
     
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
    end;

  end else
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmPBRKInPut.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  //if CDS_MX.IsEmpty then Exit;
  if CDS_Sub.Locate('FHDate',null,[])=True then
  begin
    Application.MessageBox('入库时间不能为空!','提示',0);
    Exit;
  end;

  if CDS_Sub.Locate('CPName',null,[])=True then
  begin
    Application.MessageBox('品名不能为空!','提示',0);
    Exit;
  end;
                 
  if CDS_Sub.Locate('SupplierName',null,[])=True then
  begin
    Application.MessageBox('供应商不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('Qty',null,[])=True then
  begin
    Application.MessageBox('匹数数不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('Qty',null,[])=True then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('Unit',null,[])=True then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;
  if SaveCKData() then
  begin
    if Trim(FDelStr)<>'有提示' then
    Application.MessageBox('保存成功!','提示',0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmPBRKInPut.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 TfrmPBRKInPut.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('SupplierNo').Value:=Trim(CDS_HZ.fieldbyname('CoCode').AsString);
          FieldByName('SupplierName').Value:=Trim(CDS_HZ.fieldbyname('CoName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

procedure TfrmPBRKInPut.v2Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  if Trim(canshu3)='' then
  begin
    try
      frmZDYHelp:=TfrmZDYHelp.Create(Application);
      with frmZDYHelp do
      begin
        flag:='SXYCLName';
        flagname:='纱线名称';
//        ViewFlag:=True;
        if ShowModal=1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('CPName').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
           
          end;
        end;
      end;
    finally
      frmZDYHelp.Free;
    end;
  end else
  begin
    try
      frmZdyAttachCP:=TfrmZdyAttachCP.Create(Application);
      with frmZdyAttachCP do
      begin
        if ShowModal=1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('SPName').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('ZdyName').AsString);
            FieldByName('SPSpec').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr1').AsString);
            FieldByName('SPCF').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr2').AsString);
            FieldByName('SPMF').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr7').AsString);
            FieldByName('SPKZ').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr8').AsString);
            FieldByName('SPKZ').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr8').AsString);
            FieldByName('SPCode').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('ZdyCode').AsString);
            FieldByName('CPID').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('ATID').AsString);
          end;
        end;
      end;
    finally
      frmZdyAttachCP.Free;
    end;
  end;

end;

procedure TfrmPBRKInPut.v1Column5PropertiesButtonClick(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 TfrmPBRKInPut.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(CDS_HZ.fieldbyname('CoCode').AsString);
          FieldByName('ToFactoryName').Value:=Trim(CDS_HZ.fieldbyname('CoName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

procedure TfrmPBRKInPut.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(CDS_HZ.fieldbyname('CoCode').AsString);
          FieldByName('FromFactoryName').Value:=Trim(CDS_HZ.fieldbyname('CoName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
  
  {try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YFFactory';
      flagname:='加工厂';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FromFactoryNo').Value:=Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString);
          FieldByName('FromFactoryName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end; }
end;

procedure TfrmPBRKInPut.v1Column10PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GSTT';
      flagname:='公司抬头';
      frmZDYHelp.TBAdd.Visible:=False;
      frmZDYHelp.TBDel.Visible:=False;
      frmZDYHelp.TBEdit.Visible:=False;
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ComTaiTou').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPBRKInPut.v1Column7PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CPCF';
      flagname:='成分';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('CPCF').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPBRKInPut.v1Column15PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='SXPinPai';
      flagname:='纱线品牌';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SXPinPai').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPBRKInPut.v1Column7PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FFieldName:String;
  FQty,FPrice,FCostPrice,FMoney,FBBMoney:double;
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:=mvalue;
//    Post;
//  end;
//  FQty:= CDS_Sub.fieldbyname('Qty').AsFloat;
//  FPrice:= CDS_Sub.fieldbyname('Price').AsFloat;
//  FCostPrice:= CDS_Sub.fieldbyname('CostPrice').AsFloat;
//  FMoney:=CDS_Sub.fieldbyname('Money').AsFloat;
//  if  FQty>0 then
//    FPrice:=RoundFloat(FMoney/FQty,2)
//  else
//    FPrice:=0;
//
//  with CDS_Sub do
//  begin
//    Edit;
//    FieldByName('Price').Value:=FPrice;
//    Post;
//  end;
//
//  tv1.Controller.EditingController.ShowEdit();
end;

end.
