def execute():
	for doctype in ('repost_item_valuation', 'stock_entry_detail', 'purchase_receipt_item',
			'purchase_invoice_item', 'delivery_note_item', 'sales_invoice_item', 'packed_item'):
		frappe.reload_doc('stock', 'doctype', doctype)
	frappe.reload_doc('buying', 'doctype', 'purchase_receipt_item_supplied')

	reposting_project_deployed_on = get_creation_time()
	posting_date = getdate(reposting_project_deployed_on)
	posting_time = get_time(reposting_project_deployed_on)

	if posting_date == today():
		return

	frappe.clear_cache()
	frappe.flags.warehouse_account_map = {}

	company_list = []

	data = frappe.db.sql('''
		SELECT
			name, item_code, warehouse, voucher_type, voucher_no, posting_date, posting_time, company
		FROM
			`tabStock Ledger Entry`
		WHERE
			creation > %s
			and is_cancelled = 0
		ORDER BY timestamp(posting_date, posting_time) asc, creation asc
	''', reposting_project_deployed_on, as_dict=1)

	frappe.db.auto_commit_on_many_writes = 1
	print("Reposting Stock Ledger Entries...")
	total_sle = len(data)
	i = 0
	for d in data:
		if d.company not in company_list:
			company_list.append(d.company)

		update_entries_after({
			"item_code": d.item_code,
			"warehouse": d.warehouse,
			"posting_date": d.posting_date,
			"posting_time": d.posting_time,
			"voucher_type": d.voucher_type,
			"voucher_no": d.voucher_no,
			"sle_id": d.name
		}, allow_negative_stock=True)

		i += 1
		if i%100 == 0:
			print(i, "/", total_sle)


	print("Reposting General Ledger Entries...")

	if data:
		for row in frappe.get_all('Company', filters= {'enable_perpetual_inventory': 1}):
			if row.name in company_list:
				update_gl_entries_after(posting_date, posting_time, company=row.name)

	frappe.db.auto_commit_on_many_writes = 0
Example #2
0
def repost_gl_entries(doc):
    if not cint(erpnext.is_perpetual_inventory_enabled(doc.company)):
        return

    if doc.based_on == 'Transaction':
        ref_doc = frappe.get_doc(doc.voucher_type, doc.voucher_no)
        items, warehouses = ref_doc.get_items_and_warehouses()
    else:
        items = [doc.item_code]
        warehouses = [doc.warehouse]

    update_gl_entries_after(doc.posting_date, doc.posting_time,
                            warehouses, items, company=doc.company)
Example #3
0
def execute():
	data = frappe.db.sql(''' SELECT name, item_code, warehouse, voucher_type, voucher_no, posting_date, posting_time
		from `tabStock Ledger Entry` where creation > '2020-12-26 12:58:55.903836' and is_cancelled = 0
		order by timestamp(posting_date, posting_time) asc, creation asc''', as_dict=1)

	for index, d in enumerate(data):
		update_entries_after({
			"item_code": d.item_code,
			"warehouse": d.warehouse,
			"posting_date": d.posting_date,
			"posting_time": d.posting_time,
			"voucher_type": d.voucher_type,
			"voucher_no": d.voucher_no,
			"sle_id": d.name
		}, allow_negative_stock=True)

	frappe.db.auto_commit_on_many_writes = 1

	for row in frappe.get_all('Company', filters= {'enable_perpetual_inventory': 1}):
		update_gl_entries_after('2020-12-25', '01:58:55', company=row.name)

	frappe.db.auto_commit_on_many_writes = 0
    def test_landed_cost_voucher(self):
        frappe.db.set_value("Buying Settings", None, "allow_multiple_items", 1)

        pr = make_purchase_receipt(
            company="_Test Company with perpetual inventory",
            warehouse="Stores - TCP1",
            supplier_warehouse="Work in Progress - TCP1",
            get_multiple_items=True,
            get_taxes_and_charges=True,
        )

        last_sle = frappe.db.get_value(
            "Stock Ledger Entry",
            {
                "voucher_type": pr.doctype,
                "voucher_no": pr.name,
                "item_code": "_Test Item",
                "warehouse": "Stores - TCP1",
                "is_cancelled": 0,
            },
            fieldname=["qty_after_transaction", "stock_value"],
            as_dict=1,
        )

        create_landed_cost_voucher("Purchase Receipt", pr.name, pr.company)

        pr_lc_value = frappe.db.get_value("Purchase Receipt Item",
                                          {"parent": pr.name},
                                          "landed_cost_voucher_amount")
        self.assertEqual(pr_lc_value, 25.0)

        last_sle_after_landed_cost = frappe.db.get_value(
            "Stock Ledger Entry",
            {
                "voucher_type": pr.doctype,
                "voucher_no": pr.name,
                "item_code": "_Test Item",
                "warehouse": "Stores - TCP1",
                "is_cancelled": 0,
            },
            fieldname=["qty_after_transaction", "stock_value"],
            as_dict=1,
        )

        self.assertEqual(last_sle.qty_after_transaction,
                         last_sle_after_landed_cost.qty_after_transaction)
        self.assertEqual(
            last_sle_after_landed_cost.stock_value - last_sle.stock_value,
            25.0)

        # assert after submit
        self.assertPurchaseReceiptLCVGLEntries(pr)

        # Mess up cancelled SLE modified timestamp to check
        # if they aren't effective in any business logic.
        frappe.db.set_value(
            "Stock Ledger Entry",
            {
                "is_cancelled": 1,
                "voucher_type": pr.doctype,
                "voucher_no": pr.name
            },
            "is_cancelled",
            1,
            modified=add_to_date(now(),
                                 hours=1,
                                 as_datetime=True,
                                 as_string=True),
        )

        items, warehouses = pr.get_items_and_warehouses()
        update_gl_entries_after(pr.posting_date,
                                pr.posting_time,
                                warehouses,
                                items,
                                company=pr.company)

        # reassert after reposting
        self.assertPurchaseReceiptLCVGLEntries(pr)