unit U_BPJGDZInPut;

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
  TfrmBPJGDZInPut = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v2Column1: 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;
    v1Column14: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: 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 v1Column14PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column6PropertiesEditValueChanged(Sender: TObject);
    procedure v1(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure v1QtyToFZQtyXSPropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData():Boolean;
  public
    { Public declarations }
    FBCId:String;
  end;

var
  frmBPJGDZInPut: TfrmBPJGDZInPut;

implementation
uses
  U_DataLink,U_RTFun,U_ClothContractListHZCX,U_ZDYHelp, U_GYSSelList,U_ContractCGJGSXSelList;

{$R *.dfm}

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

procedure TfrmBPJGDZInPut.TBAddClick(Sender: TObject);
begin
  with CDS_Sub do
  begin
    Append;
    FieldByName('DZDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',Now));
    Post;
  end;
end;
function TfrmBPJGDZInPut.SaveCKData():Boolean;
var
  FCRID,Maxno,FDZID:string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        if Trim(CDS_Sub.fieldbyname('DZID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,Maxno,'BJ','JYOrder_Main_MD_DuiZhang',3,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取纱线对账编号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          Maxno:=Trim(CDS_Sub.fieldbyname('DZID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(Maxno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from JYOrder_Main_MD_DuiZhang');
            sql.Add(' where DZID='''+Trim(CDS_Sub.fieldbyname('DZID').AsString)+'''');
            Open;
          end;
          FDZID:=Trim(ADOQueryTemp.fieldbyname('DZID').AsString);
          if Trim(FDZID)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDate(ADOQueryTemp);
          end;
          FieldByName('DZID').Value:=Trim(Maxno);
          FieldByName('DZType').Value:='布匹加工';
          FieldByName('Status').Value:='待对账';
          FieldByName('FactoryNo').Value:=Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'JYOrder_Main_MD_DuiZhang',2);
          Post;
        end;
        Edit;
        FieldByName('DZID').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 TfrmBPJGDZInPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('布匹加工对账录入无折算',Tv1,'财务对账');
  Close;
end;

procedure TfrmBPJGDZInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('布匹加工对账录入无折算',Tv1,'财务对账');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from JYOrder_Main_MD_DuiZhang A');
    sql.Add(' where DZID='''+Trim(FBCId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Sub);
  SInitCDSData20(ADOQueryTemp,CDS_Sub);
end;

procedure TfrmBPJGDZInPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('DZID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD_DuiZhang where DZID='''+Trim(CDS_Sub.fieldbyname('DZID').AsString)+'''');
      sql.Add(' and isnull(Status,'''')=''已入账'' ');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      Application.MessageBox('已入账，不能删除!','提示',0);
      Exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete JYOrder_Main_MD_DuiZhang where DZID='''+Trim(CDS_Sub.fieldbyname('DZID').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 TfrmBPJGDZInPut.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if CDS_Sub.Locate('DZDate',null,[])=True then
  begin
    Application.MessageBox('日期不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('MPRTCodeName',null,[])=True then
  begin
    Application.MessageBox('品名不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('FactoryName',null,[])=True then
  begin
    Application.MessageBox('供应商不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('QtyUnit',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('Price',null,[])=True then
  begin
    Application.MessageBox('单价不能为空!','提示',0);
    Exit;
  end;
  ToolBar1.SetFocus;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmBPJGDZInPut.v1Column14PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSSelList:=TfrmGYSSelList.Create(Application);
    with frmGYSSelList do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          edit;
          FieldByName('FactoryNo').Value:=Trim(frmGYSSelList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('FactoryName').Value:=Trim(frmGYSSelList.Order_Main.fieldbyname('KHName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSSelList.Free;
  end;
end;

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

procedure TfrmBPJGDZInPut.v2Column6PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FName,FQty,FPrice:String;
begin
  FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then mvalue:='0';
  with CDS_Sub do
  begin
    edit;
    FieldByName(FName).Value:=mvalue;
    //Post;
  end;
  FQty:=CDS_Sub.fieldbyname('Qty').AsString;
  FPrice:=CDS_Sub.fieldbyname('Price').AsString;
  if Trim(FQty)='' then FQty:='0';
  if Trim(FPrice)='' then FPrice:='0';
  with CDS_Sub do
  begin
    Edit;
    FieldByName('Money').Value:=StrToFloat(FQty)*StrToFloat(FPrice);
    //Post;
  end;
  if Trim(FName)='Qty' then
  begin
   { if Trim(CDS_Sub.fieldbyname('QtyToFZQtyXS').AsString)<>'' then
    begin
      with CDS_Sub do
      begin
        Edit;
        FieldByName('FZQty').Value:=StrToFloat(FQty)*CDS_Sub.fieldbyname('QtyToFZQtyXS').Value;
      end;
    end; }
  end;
end;

procedure TfrmBPJGDZInPut.v1(
  Sender: TObject);
var
  mvalue:String;
begin
 { mvalue:=TcxComboBox(Sender).EditingText;
  with CDS_Sub do
  begin
    edit;
    FieldByName('FZQtyUnit').Value:=Trim(mvalue);
  end;
  if Trim(mvalue)='' then
  begin
    with CDS_Sub do
    begin
      Edit;
      FieldByName('QtyToFZQtyXS').Value:=Null;
      FieldByName('FZQty').Value:=Null;
    end;
  end; }
end;

procedure TfrmBPJGDZInPut.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  FName:string;
begin
  {FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if Trim(FName)='QtyToFZQtyXS' then
  begin
    if Trim(CDS_Sub.fieldbyname('QtyUnit').AsString)='' then
    begin
      v1QtyToFZQtyXS.Options.Editing:=False;
      Application.MessageBox('数量单位不能为空!','提示',0);
      exit;
    end;
    if Trim(CDS_Sub.fieldbyname('FZQtyUnit').AsString)='' then
    begin
      v1QtyToFZQtyXS.Options.Editing:=False;
      Application.MessageBox('辅助单位不能为空!','提示',0);
      exit;
    end;
    v1QtyToFZQtyXS.Options.Editing:=True;
  end;
  if Trim(FName)='FZQty' then
  begin
    if Trim(CDS_Sub.fieldbyname('FZQtyUnit').AsString)='' then
    begin
      v1FZQty.Options.Editing:=False;
      Application.MessageBox('辅助单位不能为空!','提示',0);
      exit;
    end;
    v1FZQty.Options.Editing:=True;
  end; }
end;

procedure TfrmBPJGDZInPut.v1QtyToFZQtyXSPropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FName,FQty:String;
begin
  {mvalue:=TcxTextEdit(Sender).EditingText;
  //if Trim(mvalue)='' then mvalue:='0';
  with CDS_Sub do
  begin
    edit;
    FieldByName('QtyToFZQtyXS').Value:=mvalue;
    //Post;
  end;
  FQty:=CDS_Sub.fieldbyname('Qty').AsString;
  if Trim(FQty)='' then FQty:='0';
  if Trim(CDS_Sub.fieldbyname('QtyToFZQtyXS').AsString)<>'' then
  begin
    with CDS_Sub do
    begin
      Edit;
      FieldByName('FZQty').Value:=StrToFloat(FQty)*CDS_Sub.fieldbyname('QtyToFZQtyXS').Value;
    end;
  end else
  begin
    with CDS_Sub do
    begin
      Edit;
      FieldByName('FZQty').Value:=Null;
    end;
  end; }
end;

end.
