From 5cd22894b090f29b1c435ce5207bf34e5fa744a3 Mon Sep 17 00:00:00 2001 From: WebNotes Date: Mon, 11 Mar 2013 10:58:42 +0530 Subject: [PATCH 1/6] it's done --- .../doctype/backup_manager/backup_dropbox.py | 51 +++++++++---------- .../doctype/backup_manager/backup_manager.py | 5 +- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/setup/doctype/backup_manager/backup_dropbox.py b/setup/doctype/backup_manager/backup_dropbox.py index 3b0857fdd9c..e8eed3be199 100644 --- a/setup/doctype/backup_manager/backup_dropbox.py +++ b/setup/doctype/backup_manager/backup_dropbox.py @@ -1,6 +1,7 @@ import os import webnotes -from webnotes.utils import get_request_site_address +from webnotes.utils import get_request_site_address, get_base_path +from webnotes import _ @webnotes.whitelist() def get_dropbox_authorize_url(): @@ -67,22 +68,23 @@ def backup_to_dropbox(): backup = new_backup() filename = backup.backup_path_db upload_file_to_dropbox(filename, "database", dropbox_client) - - # upload files - response = dropbox_client.metadata("files") - + path1 = os.path.join(get_base_path(), "public", "backups") + response = dropbox_client.metadata('/database') - # add missing files - for filename in os.listdir(os.path.join("public", "files")): + #add missing files + found = False + for filename in os.listdir(path1): found = False + pth=path1+'/'+filename + size=os.stat(pth).st_size for file_metadata in response["contents"]: if filename==os.path.basename(file_metadata["path"]): - if os.stat(os.path.join("public", "files", filename)).st_size==file_metadata["bytes"]: + if size==file_metadata["bytes"]: found=True - if not found: - upload_file_to_dropbox(os.path.join("public", "files", filename), "files", dropbox_client) - + upload_file_to_dropbox(pth, "database", dropbox_client) + if found: + webnotes.msgprint("no backup required everything is upto date") def get_dropbox_session(): from dropbox import session @@ -96,20 +98,17 @@ def get_dropbox_session(): return sess def upload_file_to_dropbox(filename, folder, dropbox_client): - if __name__=="__main__": - print "Uploading " + filename - size = os.stat(filename).st_size - f = open(filename,'r') - - if size > 4194304: - uploader = dropbox_client.get_chunked_uploader(f, size) - while uploader.offset < size: - try: - uploader.upload_chunked() - except rest.ErrorResponse, e: - pass - else: - response = dropbox_client.put_file(folder + "/" + os.path.basename(filename), f, overwrite=True) + size = os.stat(filename).st_size + f = open(filename,'r') + if size > 4194304: + uploader = dropbox_client.get_chunked_uploader(f, size) + while uploader.offset < size: + try: + uploader.upload_chunked() + except rest.ErrorResponse, e: + pass + else: + response = dropbox_client.put_file(folder + "/" + os.path.basename(filename), f, overwrite=True) if __name__=="__main__": - backup_to_dropbox() \ No newline at end of file + backup_to_dropbox() \ No newline at end of file diff --git a/setup/doctype/backup_manager/backup_manager.py b/setup/doctype/backup_manager/backup_manager.py index 48d48e817c7..2b9cfca85ac 100644 --- a/setup/doctype/backup_manager/backup_manager.py +++ b/setup/doctype/backup_manager/backup_manager.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals import webnotes from webnotes import _ +from webnotes.utils import getTraceback class DocType: def __init__(self, d, dl): @@ -25,6 +26,7 @@ def take_backups(): backup_to_dropbox() send_email(True, "Dropbox") except Exception, e: + webnotes.errprint(e) send_email(False, "Dropbox", e) def send_email(success, service_name, error_status=None): @@ -40,7 +42,8 @@ def send_email(success, service_name, error_status=None): failed.

Error message: %s

Please contact your system manager for more information.

- """ % (service_name, error_status) +

Detailed Error Trace: %s

""" % \ + (service_name, error_status, getTraceback().replace("\n", "
")) # email system managers from webnotes.utils.email_lib import sendmail From 6f7531813411af3dfda736f06585dff2ef00d20c Mon Sep 17 00:00:00 2001 From: Saurabh Date: Wed, 20 Mar 2013 12:55:28 +0530 Subject: [PATCH 2/6] no -ve balance accepted in sales and purchase invoice --- controllers/accounts_controller.py | 4 ++++ controllers/buying_controller.py | 5 +++-- controllers/selling_controller.py | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/controllers/accounts_controller.py b/controllers/accounts_controller.py index 576921a5303..cd40d619568 100644 --- a/controllers/accounts_controller.py +++ b/controllers/accounts_controller.py @@ -21,6 +21,10 @@ from webnotes.utils import flt from utilities.transaction_base import TransactionBase class AccountsController(TransactionBase): + def validate(self): + if self.meta.get_field("grand_total"): + self.validate_value("grand_total", ">=", 0) + def get_gl_dict(self, args, cancel=None): """this method populates the common properties of a gl entry record""" if cancel is None: diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py index 2f3128c98c9..0509de0074a 100644 --- a/controllers/buying_controller.py +++ b/controllers/buying_controller.py @@ -27,7 +27,8 @@ from webnotes.model.utils import round_floats_in_doc from controllers.accounts_controller import AccountsController class BuyingController(AccountsController): - def validate(self): + def validate(self): + super(BuyingController, self).validate() if self.meta.get_field("currency"): self.company_currency = get_company_currency(self.doc.company) self.validate_conversion_rate("currency", "conversion_rate") @@ -37,7 +38,7 @@ class BuyingController(AccountsController): # IMPORTANT: enable this only when client side code is similar to this one # self.calculate_taxes_and_totals() - + # set total in words self.set_total_in_words() diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py index 40606c31989..9db8f4acad4 100644 --- a/controllers/selling_controller.py +++ b/controllers/selling_controller.py @@ -23,6 +23,7 @@ from controllers.accounts_controller import AccountsController class SellingController(AccountsController): def validate(self): + super(SellingController, self).validate() self.set_total_in_words() def set_total_in_words(self): From 9c02b81cdd02bb32b47b45f7d6821b9ef1dc930f Mon Sep 17 00:00:00 2001 From: Saurabh Date: Wed, 20 Mar 2013 13:51:43 +0530 Subject: [PATCH 3/6] merged backup dropbox file --- setup/doctype/backup_manager/backup_dropbox.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/setup/doctype/backup_manager/backup_dropbox.py b/setup/doctype/backup_manager/backup_dropbox.py index b66880305ea..2c7fda6ca94 100644 --- a/setup/doctype/backup_manager/backup_dropbox.py +++ b/setup/doctype/backup_manager/backup_dropbox.py @@ -86,8 +86,6 @@ def backup_to_dropbox(): filename = os.path.join(get_base_path(), "public", "files") for filename in os.listdir(filename): found = False - pth=path1+'/'+filename - size=os.stat(pth).st_size for file_metadata in response["contents"]: if filename==os.path.basename(file_metadata["path"]): if os.stat(filename).st_size==file_metadata["bytes"]: @@ -121,4 +119,4 @@ def upload_file_to_dropbox(filename, folder, dropbox_client): response = dropbox_client.put_file(folder + "/" + os.path.basename(filename), f, overwrite=True) if __name__=="__main__": - backup_to_dropbox() + backup_to_dropbox() \ No newline at end of file From f848b8bafc07be0d5123014fff3bc8251f50010c Mon Sep 17 00:00:00 2001 From: Saurabh Date: Wed, 20 Mar 2013 13:55:08 +0530 Subject: [PATCH 4/6] merged backup manager python file --- setup/doctype/backup_manager/backup_manager.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/setup/doctype/backup_manager/backup_manager.py b/setup/doctype/backup_manager/backup_manager.py index 69de823df1c..213aa855005 100644 --- a/setup/doctype/backup_manager/backup_manager.py +++ b/setup/doctype/backup_manager/backup_manager.py @@ -30,7 +30,6 @@ def take_backups_dropbox(): backup_to_dropbox() send_email(True, "Dropbox") except Exception, e: - webnotes.errprint(e) send_email(False, "Dropbox", e) #backup to gdrive @@ -56,10 +55,9 @@ def send_email(success, service_name, error_status=None): failed.

Error message: %s

Please contact your system manager for more information.

-

Detailed Error Trace: %s

""" % \ - (service_name, error_status, getTraceback().replace("\n", "
")) + """ % (service_name, error_status) # email system managers from webnotes.utils.email_lib import sendmail sendmail(webnotes.conn.get_value("Backup Manager", None, "send_notifications_to").split(","), - subject=subject, msg=message) + subject=subject, msg=message) \ No newline at end of file From 7da72dd449670590892388d43173b65c64734dc7 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Wed, 20 Mar 2013 17:00:41 +0530 Subject: [PATCH 5/6] unlink against voucher in jv when Purchase Invoice, Sales Invoice or Journal Voucher are cancelled --- .../journal_voucher/journal_voucher.py | 3 + .../journal_voucher/test_journal_voucher.py | 85 ++++++++++++++++++- .../purchase_invoice/purchase_invoice.py | 11 +-- .../purchase_invoice/test_purchase_invoice.py | 36 ++++++++ .../purchase_invoice_advance.txt | 6 +- .../doctype/sales_invoice/sales_invoice.py | 16 ++-- .../sales_invoice/test_sales_invoice.py | 37 ++++++++ accounts/utils.py | 18 +++- 8 files changed, 185 insertions(+), 27 deletions(-) diff --git a/accounts/doctype/journal_voucher/journal_voucher.py b/accounts/doctype/journal_voucher/journal_voucher.py index 2acf08efd65..f4bd55cdbf7 100644 --- a/accounts/doctype/journal_voucher/journal_voucher.py +++ b/accounts/doctype/journal_voucher/journal_voucher.py @@ -63,6 +63,9 @@ class DocType(AccountsController): self.make_gl_entries() def on_cancel(self): + from accounts.utils import remove_against_link_from_jv + remove_against_link_from_jv(self.doc.doctype, self.doc.name, "against_jv") + self.make_gl_entries(cancel=1) def validate_debit_credit(self): diff --git a/accounts/doctype/journal_voucher/test_journal_voucher.py b/accounts/doctype/journal_voucher/test_journal_voucher.py index bb846d16349..7cfeb595d81 100644 --- a/accounts/doctype/journal_voucher/test_journal_voucher.py +++ b/accounts/doctype/journal_voucher/test_journal_voucher.py @@ -19,8 +19,34 @@ from __future__ import unicode_literals import unittest import webnotes -test_records = [[ - { +class TestJournalVoucher(unittest.TestCase): + def test_journal_voucher_with_against_jv(self): + jv_invoice = webnotes.bean(copy=test_records[2]) + jv_invoice.insert() + jv_invoice.submit() + + self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail` + where against_jv=%s""", jv_invoice.doc.name)) + + jv_payment = webnotes.bean(copy=test_records[0]) + jv_payment.doclist[1].against_jv = jv_invoice.doc.name + jv_payment.insert() + jv_payment.submit() + + self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail` + where against_jv=%s""", jv_invoice.doc.name)) + + self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail` + where against_jv=%s and credit=400""", jv_invoice.doc.name)) + + # cancel jv_invoice + jv_invoice.cancel() + + self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail` + where against_jv=%s""", jv_invoice.doc.name)) + +test_records = [ + [{ "company": "_Test Company", "doctype": "Journal Voucher", "fiscal_year": "_Test Fiscal Year 2013", @@ -44,8 +70,59 @@ test_records = [[ "debit": 400.0, "credit": 0.0, "parentfield": "entries" - } -]] + }], + [{ + "company": "_Test Company", + "doctype": "Journal Voucher", + "fiscal_year": "_Test Fiscal Year 2013", + "naming_series": "_T-Journal Voucher-", + "posting_date": "2013-02-14", + "user_remark": "test", + "voucher_type": "Bank Voucher", + "cheque_no": "33", + "cheque_date": "2013-02-14" + }, + { + "account": "_Test Supplier - _TC", + "doctype": "Journal Voucher Detail", + "credit": 0.0, + "debit": 400.0, + "parentfield": "entries" + }, + { + "account": "_Test Account Bank Account - _TC", + "doctype": "Journal Voucher Detail", + "debit": 0.0, + "credit": 400.0, + "parentfield": "entries" + }], + [{ + "company": "_Test Company", + "doctype": "Journal Voucher", + "fiscal_year": "_Test Fiscal Year 2013", + "naming_series": "_T-Journal Voucher-", + "posting_date": "2013-02-14", + "user_remark": "test", + "voucher_type": "Bank Voucher", + "cheque_no": "33", + "cheque_date": "2013-02-14" + }, + { + "account": "_Test Customer - _TC", + "doctype": "Journal Voucher Detail", + "credit": 0.0, + "debit": 400.0, + "parentfield": "entries" + }, + { + "account": "Sales - _TC", + "doctype": "Journal Voucher Detail", + "credit": 400.0, + "debit": 0.0, + "parentfield": "entries", + "cost_center": "_Test Cost Center - _TC" + }], +] diff --git a/accounts/doctype/purchase_invoice/purchase_invoice.py b/accounts/doctype/purchase_invoice/purchase_invoice.py index b56e2ace106..f535d56e602 100644 --- a/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -507,18 +507,13 @@ class DocType(BuyingController): if gl_entries: make_gl_entries(gl_entries, cancel=is_cancel) - def check_next_docstatus(self): - submit_jv = sql("select t1.name from `tabJournal Voucher` t1,`tabJournal Voucher Detail` t2 where t1.name = t2.parent and t2.against_voucher = '%s' and t1.docstatus = 1" % (self.doc.name)) - if submit_jv: - msgprint("Journal Voucher : " + cstr(submit_jv[0][0]) + " has been created against " + cstr(self.doc.doctype) + ". So " + cstr(self.doc.doctype) + " cannot be Cancelled.") - raise Exception, "Validation Error." - def on_cancel(self): - self.check_next_docstatus() + from accounts.utils import remove_against_link_from_jv + remove_against_link_from_jv(self.doc.doctype, self.doc.name, "against_voucher") self.make_gl_entries(is_cancel=1) get_obj(dt = 'Purchase Common').update_prevdoc_detail(self, is_submit = 0) - + def on_update(self): pass diff --git a/accounts/doctype/purchase_invoice/test_purchase_invoice.py b/accounts/doctype/purchase_invoice/test_purchase_invoice.py index b9f7ec928b0..6d9cfca0487 100644 --- a/accounts/doctype/purchase_invoice/test_purchase_invoice.py +++ b/accounts/doctype/purchase_invoice/test_purchase_invoice.py @@ -115,6 +115,42 @@ class TestPurchaseInvoice(unittest.TestCase): for i, item in enumerate(wrapper.doclist.get({"parentfield": "entries"})): self.assertEqual(item.item_code, expected_values[i][0]) self.assertEqual(item.item_tax_amount, expected_values[i][1]) + + def test_purchase_invoice_with_advance(self): + from accounts.doctype.journal_voucher.test_journal_voucher \ + import test_records as jv_test_records + + jv = webnotes.bean(copy=jv_test_records[1]) + jv.insert() + jv.submit() + + pi = webnotes.bean(copy=test_records[0]) + pi.doclist.append({ + "doctype": "Purchase Invoice Advance", + "parentfield": "advance_allocation_details", + "journal_voucher": jv.doc.name, + "jv_detail_no": jv.doclist[1].name, + "advance_amount": 400, + "allocated_amount": 300, + "remarks": jv.doc.remark + }) + pi.run_method("calculate_taxes_and_totals") + pi.insert() + pi.submit() + pi.load_from_db() + + self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail` + where against_voucher=%s""", pi.doc.name)) + + self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail` + where against_voucher=%s and debit=300""", pi.doc.name)) + + self.assertEqual(pi.doc.outstanding_amount, 1212.30) + + pi.cancel() + + self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail` + where against_voucher=%s""", pi.doc.name)) test_records = [ [ diff --git a/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.txt b/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.txt index 201bb53f03a..6b316840291 100644 --- a/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.txt +++ b/accounts/doctype/purchase_invoice_advance/purchase_invoice_advance.txt @@ -1,8 +1,8 @@ [ { - "creation": "2013-02-22 01:27:40", + "creation": "2013-03-08 15:36:46", "docstatus": 0, - "modified": "2013-03-07 07:03:26", + "modified": "2013-03-20 16:52:12", "modified_by": "Administrator", "owner": "Administrator" }, @@ -40,7 +40,7 @@ { "doctype": "DocField", "fieldname": "jv_detail_no", - "fieldtype": "Date", + "fieldtype": "Data", "hidden": 1, "label": "Journal Voucher Detail No", "oldfieldname": "jv_detail_no", diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py index 3f32a47db0b..f29c2e9b6c8 100644 --- a/accounts/doctype/sales_invoice/sales_invoice.py +++ b/accounts/doctype/sales_invoice/sales_invoice.py @@ -127,11 +127,14 @@ class DocType(SellingController): sales_com_obj = get_obj(dt = 'Sales Common') sales_com_obj.check_stop_sales_order(self) - self.check_next_docstatus() + + from accounts.utils import remove_against_link_from_jv + remove_against_link_from_jv(self.doc.doctype, self.doc.name, "against_invoice") + sales_com_obj.update_prevdoc_detail(0, self) self.make_gl_entries() - + def on_update_after_submit(self): self.validate_recurring_invoice() self.convert_to_recurring() @@ -399,8 +402,7 @@ class DocType(SellingController): if lst: from accounts.utils import reconcile_against_document reconcile_against_document(lst) - - + def validate_customer(self): """ Validate customer name with SO and DN""" for d in getlist(self.doclist,'entries'): @@ -830,12 +832,6 @@ class DocType(SellingController): grand_total = %s where invoice_no = %s and parent = %s""", (self.doc.name, self.doc.amended_from, self.doc.c_form_no)) - def check_next_docstatus(self): - submit_jv = webnotes.conn.sql("select t1.name from `tabJournal Voucher` t1,`tabJournal Voucher Detail` t2 where t1.name = t2.parent and t2.against_invoice = '%s' and t1.docstatus = 1" % (self.doc.name)) - if submit_jv: - msgprint("Journal Voucher : " + cstr(submit_jv[0][0]) + " has been created against " + cstr(self.doc.doctype) + ". So " + cstr(self.doc.doctype) + " cannot be Cancelled.") - raise Exception, "Validation Error." - @property def meta(self): if not hasattr(self, "_meta"): diff --git a/accounts/doctype/sales_invoice/test_sales_invoice.py b/accounts/doctype/sales_invoice/test_sales_invoice.py index 91c0622ceea..b63642c6435 100644 --- a/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -281,6 +281,7 @@ class TestSalesInvoice(unittest.TestCase): return dn def _insert_pos_settings(self): + webnotes.conn.sql("""delete from `tabPOS Setting`""") ps = webnotes.bean([ { "cash_bank_account": "_Test Account Bank Account - _TC", @@ -297,6 +298,42 @@ class TestSalesInvoice(unittest.TestCase): ]) ps.insert() + def test_sales_invoice_with_advance(self): + from accounts.doctype.journal_voucher.test_journal_voucher \ + import test_records as jv_test_records + + jv = webnotes.bean(copy=jv_test_records[0]) + jv.insert() + jv.submit() + + si = webnotes.bean(copy=test_records[0]) + si.doclist.append({ + "doctype": "Sales Invoice Advance", + "parentfield": "advance_adjustment_details", + "journal_voucher": jv.doc.name, + "jv_detail_no": jv.doclist[1].name, + "advance_amount": 400, + "allocated_amount": 300, + "remarks": jv.doc.remark + }) + si.insert() + si.submit() + si.load_from_db() + + self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail` + where against_invoice=%s""", si.doc.name)) + + self.assertTrue(webnotes.conn.sql("""select name from `tabJournal Voucher Detail` + where against_invoice=%s and credit=300""", si.doc.name)) + + self.assertEqual(si.doc.outstanding_amount, 261.8) + + si.cancel() + + self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail` + where against_invoice=%s""", si.doc.name)) + + test_dependencies = ["Journal Voucher", "POS Setting"] test_records = [ diff --git a/accounts/utils.py b/accounts/utils.py index 14ceb4e99e9..051cdd1b24f 100644 --- a/accounts/utils.py +++ b/accounts/utils.py @@ -17,7 +17,7 @@ from __future__ import unicode_literals import webnotes -from webnotes.utils import nowdate, cstr, flt +from webnotes.utils import nowdate, cstr, flt, now from webnotes.model.doc import addchild from webnotes import msgprint, _ from webnotes.utils import formatdate @@ -233,4 +233,18 @@ def get_cost_center_list(doctype, txt, searchfield, start, page_len, filters): return webnotes.conn.sql("""select name, parent_cost_center from `tabCost Center` where docstatus < 2 %s and %s like %s order by name limit %s, %s""" % (conditions, searchfield, "%s", "%s", "%s"), - tuple(filter_values + ["%%%s%%" % txt, start, page_len])) \ No newline at end of file + tuple(filter_values + ["%%%s%%" % txt, start, page_len])) + +def remove_against_link_from_jv(ref_type, ref_no, against_field): + webnotes.conn.sql("""update `tabJournal Voucher Detail` set `%s`=null, + modified=%s, modified_by=%s + where `%s`=%s and docstatus < 2""" % (against_field, "%s", "%s", against_field, "%s"), + (now(), webnotes.session.user, ref_no)) + + webnotes.conn.sql("""update `tabGL Entry` + set against_voucher_type=null, against_voucher=null, + modified=%s, modified_by=%s + where against_voucher_type=%s and against_voucher=%s + and voucher_no != ifnull(against_voucher, "") + and ifnull(is_cancelled, "No")="No" """, + (now(), webnotes.session.user, ref_type, ref_no)) From 407331675f486186d2e26d23748c6af1ab667af1 Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Wed, 20 Mar 2013 21:14:03 +0530 Subject: [PATCH 6/6] recurring invoice test cases, set period start date and period end date as first and last day of the month if previously so, else just add months --- .../doctype/fiscal_year/test_fiscal_year.py | 12 +- .../doctype/sales_invoice/sales_invoice.py | 72 +++++----- .../doctype/sales_invoice/sales_invoice.txt | 6 +- .../sales_invoice/test_sales_invoice.py | 135 ++++++++++++++++++ 4 files changed, 188 insertions(+), 37 deletions(-) diff --git a/accounts/doctype/fiscal_year/test_fiscal_year.py b/accounts/doctype/fiscal_year/test_fiscal_year.py index e031a194fe8..b209b39b965 100644 --- a/accounts/doctype/fiscal_year/test_fiscal_year.py +++ b/accounts/doctype/fiscal_year/test_fiscal_year.py @@ -10,5 +10,15 @@ test_records = [ "doctype": "Fiscal Year", "year": "_Test Fiscal Year 2014", "year_start_date": "2014-01-01" - }] + }], + [{ + "doctype": "Fiscal Year", + "year": "_Test Fiscal Year 2015", + "year_start_date": "2015-01-01" + }], + [{ + "doctype": "Fiscal Year", + "year": "_Test Fiscal Year 2016", + "year_start_date": "2016-01-01" + }], ] \ No newline at end of file diff --git a/accounts/doctype/sales_invoice/sales_invoice.py b/accounts/doctype/sales_invoice/sales_invoice.py index f29c2e9b6c8..22ac8458a87 100644 --- a/accounts/doctype/sales_invoice/sales_invoice.py +++ b/accounts/doctype/sales_invoice/sales_invoice.py @@ -17,7 +17,9 @@ from __future__ import unicode_literals import webnotes -from webnotes.utils import add_days, cint, cstr, date_diff, flt, getdate, nowdate +from webnotes.utils import add_days, cint, cstr, date_diff, flt, getdate, nowdate, \ + get_first_day, get_last_day + from webnotes.utils.email_lib import sendmail from webnotes.utils import comma_and from webnotes.model.doc import make_autoname @@ -891,25 +893,18 @@ class DocType(SellingController): next_date = get_next_date(self.doc.posting_date, month_map[self.doc.recurring_type], cint(self.doc.repeat_on_day_of_month)) + webnotes.conn.set(self.doc, 'next_date', next_date) def get_next_date(dt, mcount, day=None): - import datetime - month = getdate(dt).month + mcount - year = getdate(dt).year - if not day: - day = getdate(dt).day - if month > 12: - month, year = month-12, year+1 - try: - next_month_date = datetime.date(year, month, day) - except: - import calendar - last_day = calendar.monthrange(year, month)[1] - next_month_date = datetime.date(year, month, last_day) - return next_month_date.strftime("%Y-%m-%d") - -def manage_recurring_invoices(next_date=None): + dt = getdate(dt) + + from dateutil.relativedelta import relativedelta + dt += relativedelta(months=mcount, day=day) + + return dt + +def manage_recurring_invoices(next_date=None, commit=True): """ Create recurring invoices on specific date by copying the original one and notify the concerned people @@ -929,19 +924,22 @@ def manage_recurring_invoices(next_date=None): ref_wrapper = webnotes.bean('Sales Invoice', ref_invoice) new_invoice_wrapper = make_new_invoice(ref_wrapper, next_date) send_notification(new_invoice_wrapper) - webnotes.conn.commit() + if commit: + webnotes.conn.commit() except: - webnotes.conn.rollback() + if commit: + webnotes.conn.rollback() - webnotes.conn.begin() - webnotes.conn.sql("update `tabSales Invoice` set \ - convert_into_recurring_invoice = 0 where name = %s", ref_invoice) - notify_errors(ref_invoice, ref_wrapper.doc.owner) - webnotes.conn.commit() + webnotes.conn.begin() + webnotes.conn.sql("update `tabSales Invoice` set \ + convert_into_recurring_invoice = 0 where name = %s", ref_invoice) + notify_errors(ref_invoice, ref_wrapper.doc.owner) + webnotes.conn.commit() exception_list.append(webnotes.getTraceback()) finally: - webnotes.conn.begin() + if commit: + webnotes.conn.begin() if exception_list: exception_message = "\n\n".join([cstr(d) for d in exception_list]) @@ -953,19 +951,27 @@ def make_new_invoice(ref_wrapper, posting_date): new_invoice = clone(ref_wrapper) mcount = month_map[ref_wrapper.doc.recurring_type] - + + invoice_period_from_date = get_next_date(ref_wrapper.doc.invoice_period_from_date, mcount) + + # get last day of the month to maintain period if the from date is first day of its own month + # and to date is the last day of its own month + if (cstr(get_first_day(ref_wrapper.doc.invoice_period_from_date)) == \ + cstr(ref_wrapper.doc.invoice_period_from_date)) and \ + (cstr(get_last_day(ref_wrapper.doc.invoice_period_to_date)) == \ + cstr(ref_wrapper.doc.invoice_period_to_date)): + invoice_period_to_date = get_last_day(get_next_date(ref_wrapper.doc.invoice_period_to_date, + mcount)) + else: + invoice_period_to_date = get_next_date(ref_wrapper.doc.invoice_period_to_date, mcount) + new_invoice.doc.fields.update({ "posting_date": posting_date, "aging_date": posting_date, - "due_date": add_days(posting_date, cint(date_diff(ref_wrapper.doc.due_date, ref_wrapper.doc.posting_date))), - - "invoice_period_from_date": \ - get_next_date(ref_wrapper.doc.invoice_period_from_date, mcount), - - "invoice_period_to_date": \ - get_next_date(ref_wrapper.doc.invoice_period_to_date, mcount), + "invoice_period_from_date": invoice_period_from_date, + "invoice_period_to_date": invoice_period_to_date, "fiscal_year": get_fiscal_year(posting_date)[0], "owner": ref_wrapper.doc.owner, }) diff --git a/accounts/doctype/sales_invoice/sales_invoice.txt b/accounts/doctype/sales_invoice/sales_invoice.txt index 35710b4d49a..462565c518b 100644 --- a/accounts/doctype/sales_invoice/sales_invoice.txt +++ b/accounts/doctype/sales_invoice/sales_invoice.txt @@ -1,8 +1,8 @@ [ { - "creation": "2013-03-12 11:56:25", + "creation": "2013-03-20 17:01:58", "docstatus": 0, - "modified": "2013-03-12 14:31:24", + "modified": "2013-03-20 19:17:38", "modified_by": "Administrator", "owner": "Administrator" }, @@ -1093,7 +1093,7 @@ "description": "The day of the month on which auto invoice will be generated e.g. 05, 28 etc ", "doctype": "DocField", "fieldname": "repeat_on_day_of_month", - "fieldtype": "Data", + "fieldtype": "Int", "label": "Repeat on Day of Month", "no_copy": 1, "print_hide": 1 diff --git a/accounts/doctype/sales_invoice/test_sales_invoice.py b/accounts/doctype/sales_invoice/test_sales_invoice.py index b63642c6435..92feae81533 100644 --- a/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -332,7 +332,142 @@ class TestSalesInvoice(unittest.TestCase): self.assertTrue(not webnotes.conn.sql("""select name from `tabJournal Voucher Detail` where against_invoice=%s""", si.doc.name)) + + def test_recurring_invoice(self): + from webnotes.utils import now_datetime, get_first_day, get_last_day, add_to_date + today = now_datetime().date() + base_si = webnotes.bean(copy=test_records[0]) + base_si.doc.fields.update({ + "convert_into_recurring_invoice": 1, + "recurring_type": "Monthly", + "notification_email_address": "test@example.com, test1@example.com, test2@example.com", + "repeat_on_day_of_month": today.day, + "posting_date": today, + "invoice_period_from_date": get_first_day(today), + "invoice_period_to_date": get_last_day(today) + }) + + # monthly + si1 = webnotes.bean(copy=base_si.doclist) + si1.insert() + si1.submit() + self._test_recurring_invoice(si1, True) + + # monthly without a first and last day period + si2 = webnotes.bean(copy=base_si.doclist) + si2.doc.fields.update({ + "invoice_period_from_date": today, + "invoice_period_to_date": add_to_date(today, days=30) + }) + si2.insert() + si2.submit() + self._test_recurring_invoice(si2, False) + + # quarterly + si3 = webnotes.bean(copy=base_si.doclist) + si3.doc.fields.update({ + "recurring_type": "Quarterly", + "invoice_period_from_date": get_first_day(today), + "invoice_period_to_date": get_last_day(add_to_date(today, months=3)) + }) + si3.insert() + si3.submit() + self._test_recurring_invoice(si3, True) + + # quarterly without a first and last day period + si4 = webnotes.bean(copy=base_si.doclist) + si4.doc.fields.update({ + "recurring_type": "Quarterly", + "invoice_period_from_date": today, + "invoice_period_to_date": add_to_date(today, months=3) + }) + si4.insert() + si4.submit() + self._test_recurring_invoice(si4, False) + + # yearly + si5 = webnotes.bean(copy=base_si.doclist) + si5.doc.fields.update({ + "recurring_type": "Yearly", + "invoice_period_from_date": get_first_day(today), + "invoice_period_to_date": get_last_day(add_to_date(today, years=1)) + }) + si5.insert() + si5.submit() + self._test_recurring_invoice(si5, True) + + # yearly without a first and last day period + si6 = webnotes.bean(copy=base_si.doclist) + si6.doc.fields.update({ + "recurring_type": "Yearly", + "invoice_period_from_date": today, + "invoice_period_to_date": add_to_date(today, years=1) + }) + si6.insert() + si6.submit() + self._test_recurring_invoice(si6, False) + + # change posting date but keep recuring day to be today + si7 = webnotes.bean(copy=base_si.doclist) + si7.doc.fields.update({ + "posting_date": add_to_date(today, days=-3) + }) + si7.insert() + si7.submit() + + # setting so that _test function works + si7.doc.posting_date = today + self._test_recurring_invoice(si7, True) + + def _test_recurring_invoice(self, base_si, first_and_last_day): + from webnotes.utils import add_months, get_last_day, getdate + from accounts.doctype.sales_invoice.sales_invoice import manage_recurring_invoices + + no_of_months = ({"Monthly": 1, "Quarterly": 3, "Yearly": 12})[base_si.doc.recurring_type] + + def _test(i): + self.assertEquals(i+1, webnotes.conn.sql("""select count(*) from `tabSales Invoice` + where recurring_id=%s and docstatus=1""", base_si.doc.recurring_id)[0][0]) + + next_date = add_months(base_si.doc.posting_date, no_of_months) + + manage_recurring_invoices(next_date=next_date, commit=False) + + recurred_invoices = webnotes.conn.sql("""select name from `tabSales Invoice` + where recurring_id=%s and docstatus=1 order by name desc""", base_si.doc.recurring_id) + + self.assertEquals(i+2, len(recurred_invoices)) + + new_si = webnotes.bean("Sales Invoice", recurred_invoices[0][0]) + + for fieldname in ["convert_into_recurring_invoice", "recurring_type", + "repeat_on_day_of_month", "notification_email_address"]: + self.assertEquals(base_si.doc.fields.get(fieldname), + new_si.doc.fields.get(fieldname)) + + self.assertEquals(new_si.doc.posting_date, unicode(next_date)) + + self.assertEquals(new_si.doc.invoice_period_from_date, + unicode(add_months(base_si.doc.invoice_period_from_date, no_of_months))) + + if first_and_last_day: + self.assertEquals(new_si.doc.invoice_period_to_date, + unicode(get_last_day(add_months(base_si.doc.invoice_period_to_date, + no_of_months)))) + else: + self.assertEquals(new_si.doc.invoice_period_to_date, + unicode(add_months(base_si.doc.invoice_period_to_date, no_of_months))) + + self.assertEquals(getdate(new_si.doc.posting_date).day, + base_si.doc.repeat_on_day_of_month) + + return new_si + + # if yearly, test 3 repetitions, else test 13 repetitions + count = no_of_months == 12 and 3 or 13 + for i in xrange(count): + base_si = _test(i) test_dependencies = ["Journal Voucher", "POS Setting"]