unit U_HandPayableInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB,
  DBClient, ExtCtrls, StdCtrls, BtnEdit, cxButtonEdit, cxTextEdit,
  cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxCalendar,
  dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu, dxSkinWXI,
  dxScrollbarAnnotations;

type
  TfrmHandPayableInPut = class(TForm)
    DataSource1: TDataSource;
    CDS_Invoice: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ToolBar1: TToolBar;
    TSave: TToolButton;
    TBClose: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    ADOQueryTmp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TSaveClick(Sender: TObject);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1P_SpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv1Column8PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    procedure initGrid();
    procedure SaveDate();
    { Private declarations }
  public
    FFID: string;
    fInvoiceFlag, PState: string;
    { Public declarations }
  end;

var
  frmHandPayableInPut: TfrmHandPayableInPut;

implementation

uses
  U_RTFun, U_ZDYHelp, U_CompanySel, U_dataLink, U_ClothInfoSel,
  U_TradePlanSel,U_UserSel,U_ZDYHelpFY;
{$R *.dfm}

procedure TfrmHandPayableInPut.SaveDate();
var
  MaxNo: string;
begin
  ADOQueryCmd.Connection.BeginTrans;
  try
    with CDS_Invoice do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('FFID').AsString = '' then
        begin
          if GetLSNo(ADOQueryTmp, MaxNo, 'SG', 'Finance_Flow', 4, 1) = False then
            raise Exception.Create('取最大号失败！');
        end
        else
          MaxNo := CDS_Invoice.fieldbyname('FFID').AsString;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.add('select * from Finance_Flow where FFID=' + quotedstr(Trim(MaxNo)));
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.FieldByName('FillId').Value := Trim(DCode);
          ADOQueryCmd.FieldByName('Filler').Value := Trim(DName);
          ADOQueryCmd.FieldByName('status').Value := '0';
        end
        else
        begin
          ADOQueryCmd.edit;
          ADOQueryCmd.FieldByName('EditId').Value := Trim(DCode);
          ADOQueryCmd.FieldByName('Editer').Value := Trim(DName);
          ADOQueryCmd.FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTmp);
        end;
        RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Invoice, 'Finance_Flow', 0);
        ADOQueryCmd.fieldbyname('FFID').Value := MaxNo;
//        ADOQueryCmd.fieldbyname('IVFlag').Value := '进项';
//        ADOQueryCmd.fieldbyname('OppCoNo').Value := trim(CDS_Invoice.FieldByName('OppCoNo').AsString);
//        ADOQueryCmd.fieldbyname('OppParentCoNo').Value := trim(CDS_Invoice.FieldByName('OppParentCoNo').AsString);
        ADOQueryCmd.fieldbyname('OurCoNo').Value := trim(CDS_Invoice.FieldByName('OurCoNo').AsString);
        ADOQueryCmd.fieldbyname('FFType').Value := '手工费用登记';
        ADOQueryCmd.Post;
        next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('保存成功！', '提示');
    Modalresult := 1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmHandPayableInPut.initGrid();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    if PState = '复制' then
    begin
      sql.Add('select '''' FFID,0.00 Amount,* from Finance_Flow where FFID=''' + trim(FFID) + '''');
    end
    else
    begin
      sql.Add('select * from Finance_Flow where FFID=''' + trim(FFID) + '''');
    end;
//    sql.add(' and isnull(status,''0'')=''0''');
//    showmessage(sql.text);
    open;
  end;

  SCreateCDS(ADOQueryTmp, CDS_Invoice);
  SInitCDSData(ADOQueryTmp, CDS_Invoice);
end;

procedure TfrmHandPayableInPut.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmHandPayableInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid(Self.Caption, Tv1, '财务管理');

  initGrid();
end;

procedure TfrmHandPayableInPut.TSaveClick(Sender: TObject);
begin

//  if CDS_Invoice.Locate('IVNo', null, []) then
//  begin
//    application.MessageBox('发票号不能为空', '提示');
//    exit;
//  end;
//  if CDS_Invoice.Locate('IVType', null, []) then
//  begin
//    application.MessageBox('发票类型不能为空', '提示');
//    exit;
//  end;
//  if CDS_Invoice.Locate('OurCoName', null, []) then
//  begin
//    application.MessageBox('我方单位不能为空', '提示');
//    exit;
//  end;
//  if CDS_Invoice.Locate('OppCoName', null, []) then
//  begin
//    application.MessageBox('对方单位不能为空', '提示');
//    exit;
//  end;
  SaveDate();
end;

procedure TfrmHandPayableInPut.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelpFY := TfrmZDYHelpFY.Create(Application);
    with frmZDYHelpFY do
    begin
      MainType := '手工费用';
      flag := 'FFAbstractDL';
      flagname := '费用类别';
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('FFAbstract').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Invoice.FieldByName('FFAbstractDL').Value := Trim(ClientDataSet1.fieldbyname('Note2').AsString);
      end;
    end;
  finally
    frmZDYHelpFY.Free;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmHandPayableInPut.v1Column5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
   try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin

      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('Certifier').Value := Trim(CDS_1.fieldbyname('UserName').AsString);
      end;
    end;

  finally
    frmUserSel.Free;
  end;
end;

procedure TfrmHandPayableInPut.v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('F_Name').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Invoice.FieldByName('F_Code').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmHandPayableInPut.v1P_SpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'P_Spec';
      flagname := '成品规格';
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('P_Spec').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

procedure TfrmHandPayableInPut.FormDestroy(Sender: TObject);
begin
  frmHandPayableInPut := nil;
end;

procedure TfrmHandPayableInPut.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmTradePlanSel := TfrmTradePlanSel.Create(Application);
    with frmTradePlanSel do
    begin
//      FMainID := MainID.Text;
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('OrderNo').Value := CDS_1.fieldbyname('OrderNo').value;
        Self.CDS_Invoice.FieldByName('OurCoName').Value := Trim(CDS_1.fieldbyname('OurCoName').AsString);
        Self.CDS_Invoice.FieldByName('OurCoNo').Value := Trim(CDS_1.fieldbyname('OurCoNo').AsString);
//
      end;
    end;
  finally
    frmTradePlanSel.Free;
  end;
end;

procedure TfrmHandPayableInPut.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';
      if ShowModal = 1 then
      begin

        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('OurCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Invoice.FieldByName('OurCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);

      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmHandPayableInPut.Tv1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := '手工费用';
      flag := 'FFAbstract';
      flagname := '摘要';
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('FFAbstract').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

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

procedure TfrmHandPayableInPut.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('OppCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Invoice.FieldByName('OppCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        Self.CDS_Invoice.FieldByName('OppParentCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Invoice.FieldByName('OppParentCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmHandPayableInPut.Tv1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('OppParentCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        Self.CDS_Invoice.FieldByName('OppParentCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmHandPayableInPut.Tv1Column8PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
   try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin

      if ShowModal = 1 then
      begin
        Self.CDS_Invoice.Edit;
        Self.CDS_Invoice.FieldByName('handler').Value := Trim(CDS_1.fieldbyname('UserName').AsString);
      end;
    end;

  finally
    frmUserSel.Free;
  end;
end;

procedure TfrmHandPayableInPut.ToolButton1Click(Sender: TObject);
begin
  if CDS_Invoice.IsEmpty = False then
  begin
    CopyAddRowCDS(CDS_Invoice);
    with CDS_Invoice do
    begin
      Edit;
      FieldByName('FFID').Value := null;
      post;
    end;
  end
  else
  begin
    with CDS_Invoice do
    begin
      Append;
      FieldByName('FFTime').Value := now();
//      FieldByName('QtyUnit').Value := 'Kg';
      FieldByName('Amount').Value := 0;
      Post;
    end;
  end;

//  CDS_Invoice.append;
//  CDS_Invoice.FieldByName('IVDate').Value := now();
//  CDS_Invoice.FieldByName('QtyUnit').Value := 'Kg';
//  CDS_Invoice.FieldByName('Amount').Value := 0;
//  CDS_Invoice.FieldByName('Qty').Value := 0;
//  CDS_Invoice.FieldByName('tax').Value := 0;
//  CDS_Invoice.FieldByName('TaxFee').Value := 0;
//  CDS_Invoice.FieldByName('taxAmount').Value := 0;
//  CDS_Invoice.FieldByName('Price').Value := 0;
//  CDS_Invoice.FieldByName('tax').Value := 13;
//  CDS_Invoice.FieldByName('Currency').Value := 'CNY';
//  CDS_Invoice.Post;

end;

procedure TfrmHandPayableInPut.ToolButton2Click(Sender: TObject);
begin
  if CDS_Invoice.IsEmpty then
    exit;
  if application.MessageBox('确定要删除吗', '提示', 1) = 2 then
    exit;
  with ADOQueryTmp do
  begin
    close;
    sql.clear;
    sql.add('delete from Finance_Flow where FFID=''' + trim(CDS_Invoice.fieldbyname('FFID').asstring) + ''' ');
    execsql;
  end;
  CDS_Invoice.delete;
end;

procedure TfrmHandPayableInPut.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(Self.Caption, Tv1, '财务管理');
end;

end.

