⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ap_invoicecancel.pas

📁 一个MRPII系统源代码版本
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit Ap_InvoiceCancel;

Interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Base_Panel, ExtEdit, StdCtrls, Mask, ActnList, Db, AdODB, ExtCtrls,
  ComCtrls, ToolWin, Grids, DBGridEh, jpeg;

Type
  TFrm_Ap_InvoiceCancel = Class(TFrm_Base_Panel)
    TlBtn_Cancel: TToolButton;
    ToolButton2: TToolButton;
    TlBtn_locate: TToolButton;
    ToolButton5: TToolButton;
    TlBtn_Quit: TToolButton;
    ToolButton7: TToolButton;
    Label2: TLabel;
    Edt_ApInvoiceNo: TEdit;
    Label3: TLabel;
    Label1: TLabel;
    Label6: TLabel;
    Lbl_CurrencyCode: TLabel;
    Label9: TLabel;
    Lbl_VendorName: TLabel;
    Label8: TLabel;
    Label7: TLabel;
    Lbl_Notax: TLabel;
    Label12: TLabel;
    Label16: TLabel;
    Lbl_TaxRate_Percent: TLabel;
    Label11: TLabel;
    Label13: TLabel;
    Lbl_EmployeeName: TLabel;
    Lbl_EmployeeCode: TLabel;
    Label4: TLabel;
    Panel1: TPanel;
    Label5: TLabel;
    Edit1: TEdit;
    Label10: TLabel;
    Edit2: TEdit;
    DBGridEh1: TDBGridEh;
    Panel2: TPanel;
    ListBox1: TListBox;
    Panel4: TPanel;
    Lbl_Diff: TLabel;
    Label18: TLabel;
    Label15: TLabel;
    Label17: TLabel;
    Lbl_CurrencyName: TLabel;
    AdoQry_Main: TAdoQuery;
    DataSource1: TDataSource;
    AdoQuery1: TAdoQuery;
    LBl_ApInvoicedate: TLabel;
    Lbl_ApInvoiceInputDate: TLabel;
    Lbl_VendorCode: TLabel;
    Lbl_ApInvoiceNoTax: TLabel;
    Lbl_ApInvoiceTax: TLabel;
    Lbl_ApInvoiceAmount: TLabel;
    Action4: TAction;
    Lbl_ApInvoiceRemArk: TLabel;
    Lbl_ApInvoiceType: TLabel;
    procedure TlBtn_locateClick(Sender: TObject);
    procedure TlBtn_CancelClick(Sender: TObject);
    procedure Edt_ApInvoiceNoExit(Sender: TObject);
    procedure Act_LocateExecute(Sender: TObject);
    procedure Act_CancelExecute(Sender: TObject);
    procedure Edt_ApInvoiceNoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Action4Execute(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormActivate(Sender: TObject);
    procedure AdoQry_MainBeforeInsert(DataSet: TDataSet);
  private
    { Private declarations }
    i:integer;
    flag: integer;                                      //控制运行inputListbox次数;
    Lc_CurrApInvoiceId:Integer;                         //当前应付发票的ID;
    Lc_CurrApInvoicelogId:Integer;                      //当前应付发票日志的ID;
    TaxRate_Percent:double;                             //当前供应商税率;
    curApMonth:string;                                  //获取已结帐月份;
    procedure GetFieldValue(InvoiceNo:string);          //返回所查询的记录;
    procedure GetPerSaveInvInBill(PerSaveApInvoiceNo:string);//得到暂存票据中入库单相关资料 ;
    function isperSaveBillNo(Billno:string):boolean;      //判断此发票是否为暂存票据;
    procedure inputListbox;                             //把匹配的入库单据号写入ListBOX中;
    procedure SaveApInvoiceLog;                         //写入应付发票日志ApInvoiceLog;
    procedure saveApInvoiceLineLog;                     //写入应付发票行日志ApInvoiceLineLog;
    procedure UnDeleteInvInBill;                        //修改(删除)InvInBill中财务发票匹配的资料;
    Function ApCheckout(Month:String):Boolean ;         //根据月份判断该月份是否已经结账;
    procedure UpdateCurrentAp;                          //修改CurrentAp中相关资料;
    procedure UnUpdateCurrentAp;                        //删除CurrentAp中相关资料;
    procedure DeleteApInvoice(ApInvoiceNo:string);      //删除发票;
    procedure clearedit;                                //清除所有具有输入焦点的控件上的内容;
    function SlaveCodeUsable(R_ItemCode:String):Boolean; //
    function PickApInvoiceNo(InitCode:String):String;
  public
    { Public declarations }
    procedure InitForm(AdoConnection:TAdoConnection);
  end;

var
  Frm_Ap_InvoiceCancel: TFrm_Ap_InvoiceCancel;

implementation
                                                                   
uses Ap_Invoice_Select, Sys_Global;

{$R *.DFM}

procedure TFrm_Ap_InvoiceCancel.GetFieldValue(InvoiceNo: string);
begin
  if Trim(InvoiceNo)<>''then
  with  AdoQry_Tmp do
  begin
    Close;
    sql.clear;
    sql.Add('select ApInvoice.*,'+
            '  Vendor.VendorName, '+
            '  Vendor.VendorTaxRate_Percent , '+
            '  Currency.CurrencyName ,'+
            '  Employee.EmployeeName '+
            'from ApInvoice left join Vendor '+
            '  on ApInvoice.VendorCode= Vendor.VendorCode '+
            '  left join Currency '+
            '  on ApInvoice.CurrencyCode=Currency.CurrencyCode '+
            '  left join Employee '+
            '  on ApInvoice.EmployeeCode=Employee.EmployeeCode '+
            'where ApInvoice.ApInvoiceNo='''+InvoiceNo+''' ');
    open;
    //把得到的数据显示出来
    case fieldbyname('ApInvoiceType').asinteger of
      0: Lbl_ApInvoiceType.Caption:='发票';
      1: Lbl_ApInvoiceType.Caption:='收据';
      //2: CmBx_ApInvoiceType.text:='暂存';
    end;
    Edt_ApInvoiceNo.text:=fieldbyname('ApInvoiceNo').asstring;
    Lbl_ApInvoiceDate.Caption:=formatdatetime('yyyy.mm.dd',fieldbyname('ApInvoiceDate').asdatetime);
    Lbl_EmployeeCode.Caption:=fieldbyname('EmployeeCode').asstring;
    Lbl_EmployeeName.Caption:=fieldbyname('EmployeeName').asstring;
    Lbl_ApInvoiceInputDate.Caption:=formatdatetime('yyyy.mm.dd',fieldbyname('ApInvoiceInputDate').asdatetime);
    lbl_VendorCode.Caption:=fieldbyname('VendorCode').asstring;
    Lbl_VendorName.Caption:=fieldbyname('VendorName').asstring;
    Lbl_CurrencyCode.Caption:=fieldbyname('CurrencyCode').asstring;
    Lbl_CurrencyName.Caption:=fieldbyname('CurrencyName').asstring;
    lbl_ApInvoiceAmount.Caption:=fieldbyname('ApInvoiceAmount').asstring;
    lbl_ApInvoiceTax.Caption:=fieldbyname('ApInvoiceTax').asstring;
    Lbl_ApInvoiceNoTax.Caption:=floattostr(fieldbyname('ApInvoiceAmount').asfloat-fieldbyname('ApInvoiceTax').asfloat);
    Lbl_TaxRate_Percent.Caption:=inttostr(fieldbyname('VendorTaxRate_Percent').asinteger);
    Lbl_ApInvoiceRemArk.Caption:=fieldbyname('ApInvoiceRemArk').asstring;
    AdoQry_TMP.Close;
  end;
    //根据发票号得到相应的入库单号
  if not isperSaveBillNo(InvoiceNo) then
  begin
    with AdoQry_Main do
    begin
      Close;
      sql.clear;
      sql.Add('select DISTINCT InvInBill.WhCode+'' ''+ Warehouse.WHName  as whCodeName ,'+
              '   InvInBill.InvBillid as InvBillid ,'+
              '  InvInBill.InvBillDate,'+
              '  InvInBill.InvBillNo, '+
              '  sum(isnull(InvInBillLine.InvBillNoTaxAmountc,0)) as InvBillNoTaxAmount ,'+
              '  InvInBill.InvBillFinChck '+
              ' from ApInvoice left join ApInvoiceLine '+
              '  on ApInvoice.ApInvoiceId=ApInvoiceLine.ApInvoiceId '+
              '  left join InvInBill '+
              '  on ApInvoiceLine.InvBillId=InvInBill.InvBillId '+
              '  left join Warehouse '+
              '  on InvInBill.WhCode=Warehouse.WhCode '+
              '  left join InvInBillLine '+
              '  on InvInBill.InvBillid=InvInBillLine.InvBillid '+
              'where ApInvoice.ApInvoiceNo='''+InvoiceNo+''''+
              '  and InvInBill.InvBillFinChck=1 '+
//              '   and InvInBill.realBillflag=1 ' +
              ' group by InvInBill.WhCode+'' ''+ Warehouse.WHName ,InvInBill.InvBillid,InvInBill.InvBillDate,InvInBill.InvBillNo,InvInBill.VendorCode,InvInBill.InvBillFinChck ');
              open;
       Label18.Caption:='0';
       Listbox1.clear;
    end;
      //tlbtn_look.enabled:=(AdoQry_Main.recordCount>0);
  end
  else
  begin
    GetPerSaveInvInBill(Trim(InvoiceNo));
  end;
  if AdoQry_Main.recordCount>0 then
  begin
    Pnl_Hint.Caption:='提示:入库单的行数:'+Inttostr(AdoQry_Main.recordCount);
    AdoQry_Main.First;
    while not AdoQry_Main.eof do
    begin
      inputListbox;
      AdoQry_Main.next;
    end;
  end
  else
    Pnl_Hint.Caption:='提示:没有入库单';
end;

procedure TFrm_Ap_InvoiceCancel.GetPerSaveInvInBill(
  PerSaveApInvoiceNo: string);
begin
  with AdoQry_Main do
  begin
    AdoQry_Main.Close;
    AdoQry_Main.sql.clear;
    AdoQry_Main.sql.Add('select InvInBill.WhCode+'' ''+ Warehouse.WHName  as whCodeName ,'+
                    '   InvInBill.InvBillid as InvBillid ,'+
                    '   InvInBill.InvBillDate as InvBilldate,'+
                    '   InvInBill.InvBillNo  as InvBillno , '+
                    '   sum(InvInBillLine.InvBillNoTaxAmountC) as InvBillNoTaxAmount ,'+
                    '   InvInBill.InvBillFinChck as  InvBillfinchck '+
                    ' from InvInBill left join  Warehouse '+
                    '   on InvInBill.WhCode=Warehouse.WhCode '+
                    '   left join InvInBillLine '+
                    '   on InvInBill.InvBillid=InvInBillLine.InvBillid '+
                    ' where InvInBill.VendorCode='''+Trim(Lbl_VendorCode.Caption)+''''+
                    '   and ((InvInBill.BillTypeCode =''0101'' )' +
                    '   or (InvInBill.BillTypeCode=''0103'' )' +
                    '   or (InvInBill.BillTypeCode=''0102'' )' +
                    '   or ( (BillTypeCode=''0199'' )  and InvInBill.BillType2Code in ( select BillType2Code from BillType2 where ChangeAp=1 )))'+
                    '   and InvInBill.InvBillStkChck=1 ' +
                    '   and InvInBill.InvBillValuation<>1' +
                    '   and InvInBill.realBillflag=1 ' +
                    //'   and InvInBill.InvBillFinChck<>1'+
                    //'   and InvInBill.InvBillid='''+inttostr(AdoQry_Tmp.fieldbyname('InvBillid').asinteger) +''''+
                    '   and InvInBill.InvBillid in ( select  ApInvoiceLine.InvBillid as InvBillid '+
                    '                              from ApInvoice left join  ApInvoiceLine '+
                    '                              on ApInvoice.ApInvoiceId=ApInvoiceLine.ApInvoiceId '+
                    '                              where ApInvoice.ApInvoiceNo='''+Trim(PerSaveApInvoiceNo)+''' )'+
                    //'                              where ApInvoice.ApInvoiceNo='''+Trim(Edt_ApInvoiceNo.text)+''' )'=
                    ' group by InvInBill.WhCode+'' ''+ Warehouse.WHName ,InvInBill.InvBillId,Warehouse.WHName,InvInBill.InvBillDate,InvInBill.InvBillNo,InvInBill.VendorCode,InvInBill.InvBillFinChck ');
                    //' having sum(InvInBillLine.InvBillNoTaxAmount)>0');
     open;
  end;
  AdoQry_Main.First;
  while not AdoQry_Main.eof do
  begin
    AdoQry_Main.edit;
    AdoQry_Main.fieldbyname('InvBillFinChck').asinteger:=1;
    AdoQry_Main.post;
    AdoQry_Main.next;
  end;
end;


procedure TFrm_Ap_InvoiceCancel.inputListbox;
var
  str:string;
begin
  if i=0 then
  begin
    str:='仓库代号:'+getCode(AdoQry_Main.fieldbyname('WHCodeName').asstring);
    str:=str+' '+'入库单据号:'+AdoQry_Main.fieldbyname('InvBillNo').asstring;
    str:=str+' '+' 单据未税金额:'+floattostr(AdoQry_Main.fieldbyname('InvBillNoTaxAmount').asfloat);
    Listbox1.Items.Add(str);
    //Label18.Caption:=floattostr(strtofloat(Trim(Label18.Caption))+ AdoQry_Main.fieldbyname('InvBillNoTaxAmount').asfloat);
    //Lbl_Diff.Caption:= floattostr(strtofloat(lbl_ApInvoiceNoTax.Caption)-strtofloat(Trim(Label18.Caption)));
    Label18.Caption:=formatfloat('0.##',strtofloat(Trim(Label18.Caption))+AdoQry_Main.fieldbyname('InvBillNoTaxAmount').asfloat);
    Lbl_Diff.Caption:=formatfloat('0.##',strtofloat(Lbl_ApInvoiceNoTax.Caption)-strtofloat(Trim(Label18.Caption)));
  end;
end;

function TFrm_Ap_InvoiceCancel.isperSaveBillNo(Billno: string): boolean;
begin
   Result:=False;
   with AdoQry_tmp do
   begin
     Close;
     sql.clear;
     sql.Add('select  ApInvoiceNo '+
                      ' from ApInvoice '+
                      ' where ApInvoiceType=2 and ApInvoiceNo='''+Billno+'''');
     open;
     Result:=(recordCount>0);
   end;
end;

procedure TFrm_Ap_InvoiceCancel.TlBtn_locateClick(Sender: TObject);
var
  Tmp_ApInvoiceNo:String;
begin
  inherited;
  Tmp_ApInvoiceNo:='';
  //取得发票号码
  Frm_Ap_Invoice_Select:=TFrm_Ap_Invoice_Select.Create(self);
  Frm_Ap_Invoice_Select.ShowModal;
  if Frm_Ap_Invoice_Select.ModalResult=mroK then
  begin
    Tmp_ApInvoiceNo:=Frm_Ap_Invoice_Select.InvoiceNo;
  end;
  Frm_Ap_Invoice_Select.Release;

  //根据取得的发票号码更新数据
  if  Tmp_ApInvoiceNo<>'' then
  begin
    Label18.Caption:='0';
    Listbox1.clear;
    GetFieldValue(Tmp_ApInvoiceNo);
    TlBtn_locate.enabled:=False;
    TlBtn_Cancel.enabled:=True;    
  end;
end;

procedure TFrm_Ap_InvoiceCancel.TlBtn_CancelClick(Sender: TObject);
var
  Yearstr,Monthstr:string;
begin

  {if flag=1 then
    abort;}
  if activecontrol.Name='Edt_ApInvoiceTax' then
    abort;
  inherited;
  flag:=4;
  if ApCheckout(copy(Trim(Lbl_ApInvoiceInputDate.Caption),1,7)) then
  begin
    Yearstr:=copy(Trim(lbl_ApInvoiceInputDate.Caption),1,4);
    if copy(Trim(lbl_ApInvoiceInputDate.Caption),6,1)='0' then
      Monthstr:=copy(Trim(lbl_ApInvoiceInputDate.Caption),7,1)
    else
      Monthstr:=copy(Trim(lbl_ApInvoiceInputDate.Caption),6,2);
    DispInfo(Yearstr+'年'+Monthstr+'月已经结账,不能删除此张发票!',3) ;
    abort;
  end;

  if  DispInfo('删除此发票吗!',2)='n' then
    abort
  else
  begin
    with AdoQry_Tmp do
    begin
      Close;
      sql.clear;
      sql.Add('select ApInvoiceNo'
        +' from  ApInvoice '
        +' where ApInvoiceNo='''+Edt_ApInvoiceNo.text+''''
        +' and ApPayFlag in (1,0)');
      Prepared;
      open;
    end;
    if not  AdoQry_Tmp.Isempty then
    begin
      DispInfo('此发票已匹配,不能删除!',1);
      Abort;
    end
    else
    begin
      if  not DbConnect.InTransaction then
      DbConnect.beginTrans ;
      try


        //判断此发票是否为暂存票据;
        if  not isperSaveBillNo(Trim(Edt_ApInvoiceNo.text)) then
        begin
           //UnDeleteInvInBill;
          {if IsPrePay <0.00 then
          //如果有预付款;
            UnUpdateCurrentApP
          else}
            UnUpdateCurrentAp;
        end;
        UnDeleteInvInBill;
        DeleteApInvoice(Trim(Edt_ApInvoiceNo.text));
        SaveApInvoiceLog;
        SaveApInvoiceLineLog;
        DbConnect.CommitTrans;
        DispInfo('删除成功!',3);
        //flag:=0;
        Listbox1.clear;
        Label18.Caption:='0';
        Lbl_Diff.Caption:='0';
        Pnl_Hint.Caption:='';
        TlBtn_locate.enabled:=True;
        TlBtn_Cancel.enabled:=False;

      except
        DbConnect.RollBackTrans ;
        DispInfo('删除失败!',1);
        abort;
      end;
      TlBtn_Cancel.Enabled:=False;
      clearedit;
      //ReadOnly(2);
    end;
  end;
end;


procedure TFrm_Ap_InvoiceCancel.saveApInvoiceLineLog;
var
  ApInvoiceId1,logaction:string;
  i:integer;
begin
  i:=0;
  if  Lbl_ApInvoiceType.Caption='发票' then
    i:=0;
  if  Lbl_ApInvoiceType.Caption='收据' then
    i:=1;
  ApInvoiceId1:=IntToStr(Lc_CurrApInvoiceId);
  //设置日志的状态
  logaction:='D';
  with AdoQry_tmp do
  begin
    Close;
    sql.clear;
    sql.Add('set noCount on '+
            'insert into ApInvoiceLog '+
            '  (LogDate,'+
            '   LogOperatorCode , '+
            '   LogAction ,'+
            '   ApInvoiceid ,'+
            '   ApInvoiceNo, '+
            '   ApInvoicedate, '+
            '   EmployeeCode, '+
            '   ApInvoiceInputDate, '+
            '   VendorCode, '+
            '   CurrencyCode, '+
            '   ApInvoiceAmount, '+
            '   ApInvoiceTax, '+
            '   ApInvoiceType ,'+
            '   ApInvoiceRemArk) '+
            'Values '+
            '  (getdate(),'+
            '   '''+Trim(Lbl_EmployeeCode.Caption)+''','+
            '   '''+logaction+''','+
            '   '''+ApInvoiceId1+''','+
            '   '''+Trim(Edt_ApInvoiceNo.text)+''', '+
            '   '''+Trim(Lbl_ApInvoicedate.Caption)+''','+
            '   '''+Trim(Lbl_EmployeeCode.Caption)+''','+
            '   '''+Trim(lbl_ApInvoiceInputDate.Caption)+''', '+
            '   '''+Trim(lbl_VendorCode.Caption)+''','+
            '   '''+Trim(Lbl_CurrencyCode.Caption)+''','+
            '   '+Trim(lbl_ApInvoiceAmount.Caption)+','+
            '   '+Trim(Lbl_ApInvoiceTax.Caption)+','+
            ' '''+inttostr(i)+''', '+

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -