Nabin Hait | 3edefb1 | 2016-07-20 16:13:18 +0530 | [diff] [blame] | 1 | |
| 2 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 3 | # License: GNU General Public License v3. See license.txt |
| 4 | |
| 5 | from __future__ import unicode_literals |
| 6 | |
| 7 | import frappe |
| 8 | from frappe.utils.make_random import get_random |
| 9 | from erpnext.accounts.doctype.asset.asset import make_purchase_invoice, make_sales_invoice |
| 10 | from erpnext.accounts.doctype.asset.depreciation import post_depreciation_entries, scrap_asset |
| 11 | |
| 12 | def work(): |
| 13 | frappe.set_user(frappe.db.get_global('demo_accounts_user')) |
| 14 | |
| 15 | asset_list = make_asset_purchase_entry() |
| 16 | |
| 17 | if not asset_list: |
| 18 | # fixed_asset.work() already run |
| 19 | return |
| 20 | |
| 21 | # post depreciation entries as on today |
| 22 | post_depreciation_entries() |
| 23 | |
| 24 | # scrap a random asset |
| 25 | frappe.db.set_value("Company", "Wind Power LLC", "disposal_account", "Gain/Loss on Asset Disposal - WPL") |
| 26 | |
| 27 | asset = get_random_asset() |
| 28 | scrap_asset(asset.name) |
| 29 | |
| 30 | # Sell a random asset |
| 31 | sell_an_asset() |
| 32 | |
| 33 | def make_asset_purchase_entry(): |
| 34 | asset_list = frappe.get_all("Asset", filters={"purchase_invoice": ["in", ("", None)]}, |
| 35 | fields=["name", "item_code", "gross_purchase_amount", "company", "purchase_date"]) |
| 36 | |
| 37 | # make purchase invoice |
| 38 | for asset in asset_list: |
| 39 | pi = make_purchase_invoice(asset.name, asset.item_code, asset.gross_purchase_amount, |
| 40 | asset.company, asset.purchase_date) |
| 41 | pi.supplier = get_random("Supplier") |
| 42 | pi.save() |
| 43 | pi.submit() |
| 44 | |
| 45 | return asset_list |
| 46 | |
| 47 | def sell_an_asset(): |
| 48 | asset = get_random_asset() |
| 49 | si = make_sales_invoice(asset.name, asset.item_code, "Wind Power LLC") |
| 50 | si.customer = get_random("Customer") |
| 51 | si.get("items")[0].rate = asset.value_after_depreciation * 0.8 \ |
| 52 | if asset.value_after_depreciation else asset.gross_purchase_amount * 0.9 |
| 53 | si.save() |
| 54 | si.submit() |
| 55 | |
| 56 | def get_random_asset(): |
| 57 | return frappe.db.sql(""" select name, item_code, value_after_depreciation, gross_purchase_amount |
| 58 | from `tabAsset` |
| 59 | where docstatus=1 and status not in ("Scrapped", "Sold") order by rand() limit 1""", as_dict=1)[0] |