def make_sales_invoice(reference_name, patient, company, therapy_plan_template): from erpbee.stock.get_item_details import get_item_details si = frappe.new_doc('Sales Invoice') si.company = company si.patient = patient si.customer = frappe.db.get_value('Patient', patient, 'customer') item = frappe.db.get_value('Therapy Plan Template', therapy_plan_template, 'linked_item') price_list, price_list_currency = frappe.db.get_values( 'Price List', {'selling': 1}, ['name', 'currency'])[0] args = { 'doctype': 'Sales Invoice', 'item_code': item, 'company': company, 'customer': si.customer, 'selling_price_list': price_list, 'price_list_currency': price_list_currency, 'plc_conversion_rate': 1.0, 'conversion_rate': 1.0 } item_line = si.append('items', {}) item_details = get_item_details(args) item_line.item_code = item item_line.qty = 1 item_line.rate = item_details.price_list_rate item_line.amount = flt(item_line.rate) * flt(item_line.qty) item_line.reference_dt = 'Therapy Plan' item_line.reference_dn = reference_name item_line.description = item_details.description si.set_missing_values(for_validate=True) return si
def test_mixed_conditions_for_item_group(self): for item in ["Mixed Cond Item 1", "Mixed Cond Item 2"]: make_item(item, {"item_group": "Products"}) make_item_price(item, "_Test Price List", 100) test_record = { "doctype": "Pricing Rule", "title": "_Test Pricing Rule for Item Group", "apply_on": "Item Group", "item_groups": [ { "item_group": "Products", }, { "item_group": "Seed", }, ], "selling": 1, "mixed_conditions": 1, "currency": "USD", "rate_or_discount": "Discount Percentage", "discount_percentage": 10, "applicable_for": "Customer Group", "customer_group": "All Customer Groups", "company": "_Test Company" } frappe.get_doc(test_record.copy()).insert() args = frappe._dict({ "item_code": "Mixed Cond Item 1", "item_group": "Products", "company": "_Test Company", "price_list": "_Test Price List", "currency": "_Test Currency", "doctype": "Sales Order", "conversion_rate": 1, "price_list_currency": "_Test Currency", "plc_conversion_rate": 1, "order_type": "Sales", "customer": "_Test Customer", "customer_group": "_Test Customer Group", "name": None }) details = get_item_details(args) self.assertEquals(details.get("discount_percentage"), 10)
def get_items_from_product_bundle(args): args = json.loads(args) items = [] bundled_items = get_product_bundle_items(args["item_code"]) for item in bundled_items: args.update({ "item_code": item.item_code, "qty": flt(args["quantity"]) * flt(item.qty) }) items.append(get_item_details(args)) return items
def complete_procedure(self): if self.consume_stock and self.items: stock_entry = make_stock_entry(self) if self.items: consumable_total_amount = 0 consumption_details = False customer = frappe.db.get_value('Patient', self.patient, 'customer') if customer: for item in self.items: if item.invoice_separately_as_consumables: price_list, price_list_currency = frappe.db.get_values( 'Price List', {'selling': 1}, ['name', 'currency'])[0] args = { 'doctype': 'Sales Invoice', 'item_code': item.item_code, 'company': self.company, 'warehouse': self.warehouse, 'customer': customer, 'selling_price_list': price_list, 'price_list_currency': price_list_currency, 'plc_conversion_rate': 1.0, 'conversion_rate': 1.0 } item_details = get_item_details(args) item_price = item_details.price_list_rate * item.qty item_consumption_details = item_details.item_name + ' ' + str( item.qty) + ' ' + item.uom + ' ' + str(item_price) consumable_total_amount += item_price if not consumption_details: consumption_details = _( 'Clinical Procedure ({0}):').format(self.name) consumption_details += '\n\t' + item_consumption_details if consumable_total_amount > 0: frappe.db.set_value('Clinical Procedure', self.name, 'consumable_total_amount', consumable_total_amount) frappe.db.set_value('Clinical Procedure', self.name, 'consumption_details', consumption_details) else: frappe.throw(_('Please set Customer in Patient {0}').format( frappe.bold(self.patient)), title=_('Customer Not Found')) self.db_set('status', 'Completed') if self.consume_stock and self.items: return stock_entry
def test_pricing_rule_for_margin(self): from erpbee.stock.get_item_details import get_item_details from frappe import MandatoryError test_record = { "doctype": "Pricing Rule", "title": "_Test Pricing Rule", "apply_on": "Item Code", "items": [{ "item_code": "_Test FG Item 2", }], "selling": 1, "currency": "USD", "rate_or_discount": "Discount Percentage", "rate": 0, "margin_type": "Percentage", "margin_rate_or_amount": 10, "company": "_Test Company" } frappe.get_doc(test_record.copy()).insert() item_price = frappe.get_doc({ "doctype": "Item Price", "price_list": "_Test Price List 2", "item_code": "_Test FG Item 2", "price_list_rate": 100 }) item_price.insert(ignore_permissions=True) args = frappe._dict({ "item_code": "_Test FG Item 2", "company": "_Test Company", "price_list": "_Test Price List", "currency": "_Test Currency", "doctype": "Sales Order", "conversion_rate": 1, "price_list_currency": "_Test Currency", "plc_conversion_rate": 1, "order_type": "Sales", "customer": "_Test Customer", "name": None }) details = get_item_details(args) self.assertEquals(details.get("margin_type"), "Percentage") self.assertEquals(details.get("margin_rate_or_amount"), 10)
def test_get_item_details(self): # delete modified item price record and make as per test_records frappe.db.sql("""delete from `tabItem Price`""") to_check = { "item_code": "_Test Item", "item_name": "_Test Item", "description": "_Test Item 1", "warehouse": "_Test Warehouse - _TC", "income_account": "Sales - _TC", "expense_account": "_Test Account Cost for Goods Sold - _TC", "cost_center": "_Test Cost Center - _TC", "qty": 1.0, "price_list_rate": 100.0, "base_price_list_rate": 0.0, "discount_percentage": 0.0, "rate": 0.0, "base_rate": 0.0, "amount": 0.0, "base_amount": 0.0, "batch_no": None, "uom": "_Test UOM", "conversion_factor": 1.0, } make_test_objects("Item Price") details = get_item_details({ "item_code": "_Test Item", "company": "_Test Company", "price_list": "_Test Price List", "currency": "_Test Currency", "doctype": "Sales Order", "conversion_rate": 1, "price_list_currency": "_Test Currency", "plc_conversion_rate": 1, "order_type": "Sales", "customer": "_Test Customer", "conversion_factor": 1, "price_list_uom_dependant": 1, "ignore_pricing_rule": 1 }) for key, value in iteritems(to_check): self.assertEqual(value, details.get(key))
def test_cumulative_pricing_rule(self): frappe.delete_doc_if_exists('Pricing Rule', '_Test Cumulative Pricing Rule') test_record = { "doctype": "Pricing Rule", "title": "_Test Cumulative Pricing Rule", "apply_on": "Item Code", "currency": "USD", "items": [{ "item_code": "_Test Item", }], "is_cumulative": 1, "selling": 1, "applicable_for": "Customer", "customer": "_Test Customer", "rate_or_discount": "Discount Percentage", "rate": 0, "min_amt": 0, "max_amt": 10000, "discount_percentage": 17.5, "price_or_product_discount": "Price", "company": "_Test Company", "valid_from": frappe.utils.nowdate(), "valid_upto": frappe.utils.nowdate() } frappe.get_doc(test_record.copy()).insert() args = frappe._dict({ "item_code": "_Test Item", "company": "_Test Company", "price_list": "_Test Price List", "currency": "_Test Currency", "doctype": "Sales Invoice", "conversion_rate": 1, "price_list_currency": "_Test Currency", "plc_conversion_rate": 1, "order_type": "Sales", "customer": "_Test Customer", "name": None, "transaction_date": frappe.utils.nowdate() }) details = get_item_details(args) self.assertTrue(details)
def test_pricing_rule_for_discount(self): from erpbee.stock.get_item_details import get_item_details from frappe import MandatoryError test_record = { "doctype": "Pricing Rule", "title": "_Test Pricing Rule", "apply_on": "Item Code", "items": [{ "item_code": "_Test Item" }], "currency": "USD", "selling": 1, "rate_or_discount": "Discount Percentage", "rate": 0, "discount_percentage": 10, "company": "_Test Company" } frappe.get_doc(test_record.copy()).insert() args = frappe._dict({ "item_code": "_Test Item", "company": "_Test Company", "price_list": "_Test Price List", "currency": "_Test Currency", "doctype": "Sales Order", "conversion_rate": 1, "price_list_currency": "_Test Currency", "plc_conversion_rate": 1, "order_type": "Sales", "customer": "_Test Customer", "name": None }) details = get_item_details(args) self.assertEqual(details.get("discount_percentage"), 10) prule = frappe.get_doc(test_record.copy()) prule.applicable_for = "Customer" prule.title = "_Test Pricing Rule for Customer" self.assertRaises(MandatoryError, prule.insert) prule.customer = "_Test Customer" prule.discount_percentage = 20 prule.insert() details = get_item_details(args) self.assertEqual(details.get("discount_percentage"), 20) prule = frappe.get_doc(test_record.copy()) prule.apply_on = "Item Group" prule.items = [] prule.append('item_groups', {'item_group': "All Item Groups"}) prule.title = "_Test Pricing Rule for Item Group" prule.discount_percentage = 15 prule.insert() args.customer = "_Test Customer 1" details = get_item_details(args) self.assertEqual(details.get("discount_percentage"), 10) prule = frappe.get_doc(test_record.copy()) prule.applicable_for = "Campaign" prule.campaign = "_Test Campaign" prule.title = "_Test Pricing Rule for Campaign" prule.discount_percentage = 5 prule.priority = 8 prule.insert() args.campaign = "_Test Campaign" details = get_item_details(args) self.assertEqual(details.get("discount_percentage"), 5) frappe.db.sql( "update `tabPricing Rule` set priority=NULL where campaign='_Test Campaign'" ) from erpbee.accounts.doctype.pricing_rule.utils import MultiplePricingRuleConflict self.assertRaises(MultiplePricingRuleConflict, get_item_details, args) args.item_code = "_Test Item 2" details = get_item_details(args) self.assertEquals(details.get("discount_percentage"), 15)
def test_pricing_rule_for_variants(self): from erpbee.stock.get_item_details import get_item_details from frappe import MandatoryError if not frappe.db.exists("Item", "Test Variant PRT"): frappe.get_doc({ "doctype": "Item", "item_code": "Test Variant PRT", "item_name": "Test Variant PRT", "description": "Test Variant PRT", "item_group": "_Test Item Group", "is_stock_item": 1, "variant_of": "_Test Variant Item", "default_warehouse": "_Test Warehouse - _TC", "stock_uom": "_Test UOM", "attributes": [{ "attribute": "Test Size", "attribute_value": "Medium" }], }).insert() frappe.get_doc({ "doctype": "Pricing Rule", "title": "_Test Pricing Rule 1", "apply_on": "Item Code", "currency": "USD", "items": [{ "item_code": "_Test Variant Item", }], "selling": 1, "rate_or_discount": "Discount Percentage", "rate": 0, "discount_percentage": 7.5, "company": "_Test Company" }).insert() args = frappe._dict({ "item_code": "Test Variant PRT", "company": "_Test Company", "price_list": "_Test Price List", "currency": "_Test Currency", "doctype": "Sales Order", "conversion_rate": 1, "price_list_currency": "_Test Currency", "plc_conversion_rate": 1, "order_type": "Sales", "customer": "_Test Customer", "name": None }) details = get_item_details(args) self.assertEqual(details.get("discount_percentage"), 7.5) # add a new pricing rule for that item code, it should take priority frappe.get_doc({ "doctype": "Pricing Rule", "title": "_Test Pricing Rule 2", "apply_on": "Item Code", "items": [{ "item_code": "Test Variant PRT", }], "currency": "USD", "selling": 1, "rate_or_discount": "Discount Percentage", "rate": 0, "discount_percentage": 17.5, "company": "_Test Company" }).insert() details = get_item_details(args) self.assertEqual(details.get("discount_percentage"), 17.5)
def test_item_defaults(self): frappe.delete_doc_if_exists("Item", "Test Item With Defaults", force=1) make_item( "Test Item With Defaults", { "item_group": "_Test Item Group", "brand": "_Test Brand With Item Defaults", "item_defaults": [{ "company": "_Test Company", "default_warehouse": "_Test Warehouse 2 - _TC", # no override "expense_account": "_Test Account Stock Expenses - _TC", # override brand default "buying_cost_center": "_Test Write Off Cost Center - _TC", # override item group default }] }) sales_item_check = { "item_code": "Test Item With Defaults", "warehouse": "_Test Warehouse 2 - _TC", # from item "income_account": "_Test Account Sales - _TC", # from brand "expense_account": "_Test Account Stock Expenses - _TC", # from item "cost_center": "_Test Cost Center 2 - _TC", # from item group } sales_item_details = get_item_details({ "item_code": "Test Item With Defaults", "company": "_Test Company", "price_list": "_Test Price List", "currency": "_Test Currency", "doctype": "Sales Invoice", "conversion_rate": 1, "price_list_currency": "_Test Currency", "plc_conversion_rate": 1, "customer": "_Test Customer", }) for key, value in iteritems(sales_item_check): self.assertEqual(value, sales_item_details.get(key)) purchase_item_check = { "item_code": "Test Item With Defaults", "warehouse": "_Test Warehouse 2 - _TC", # from item "expense_account": "_Test Account Stock Expenses - _TC", # from item "income_account": "_Test Account Sales - _TC", # from brand "cost_center": "_Test Write Off Cost Center - _TC" # from item } purchase_item_details = get_item_details({ "item_code": "Test Item With Defaults", "company": "_Test Company", "price_list": "_Test Price List", "currency": "_Test Currency", "doctype": "Purchase Invoice", "conversion_rate": 1, "price_list_currency": "_Test Currency", "plc_conversion_rate": 1, "supplier": "_Test Supplier", }) for key, value in iteritems(purchase_item_check): self.assertEqual(value, purchase_item_details.get(key))
def test_item_tax_template(self): expected_item_tax_template = [ { "item_code": "_Test Item With Item Tax Template", "tax_category": "", "item_tax_template": "_Test Account Excise Duty @ 10" }, { "item_code": "_Test Item With Item Tax Template", "tax_category": "_Test Tax Category 1", "item_tax_template": "_Test Account Excise Duty @ 12" }, { "item_code": "_Test Item With Item Tax Template", "tax_category": "_Test Tax Category 2", "item_tax_template": None }, { "item_code": "_Test Item Inherit Group Item Tax Template 1", "tax_category": "", "item_tax_template": "_Test Account Excise Duty @ 10" }, { "item_code": "_Test Item Inherit Group Item Tax Template 1", "tax_category": "_Test Tax Category 1", "item_tax_template": "_Test Account Excise Duty @ 12" }, { "item_code": "_Test Item Inherit Group Item Tax Template 1", "tax_category": "_Test Tax Category 2", "item_tax_template": None }, { "item_code": "_Test Item Inherit Group Item Tax Template 2", "tax_category": "", "item_tax_template": "_Test Account Excise Duty @ 15" }, { "item_code": "_Test Item Inherit Group Item Tax Template 2", "tax_category": "_Test Tax Category 1", "item_tax_template": "_Test Account Excise Duty @ 12" }, { "item_code": "_Test Item Inherit Group Item Tax Template 2", "tax_category": "_Test Tax Category 2", "item_tax_template": None }, { "item_code": "_Test Item Override Group Item Tax Template", "tax_category": "", "item_tax_template": "_Test Account Excise Duty @ 20" }, { "item_code": "_Test Item Override Group Item Tax Template", "tax_category": "_Test Tax Category 1", "item_tax_template": "_Test Item Tax Template 1" }, { "item_code": "_Test Item Override Group Item Tax Template", "tax_category": "_Test Tax Category 2", "item_tax_template": None }, ] expected_item_tax_map = { None: {}, "_Test Account Excise Duty @ 10": { "_Test Account Excise Duty - _TC": 10 }, "_Test Account Excise Duty @ 12": { "_Test Account Excise Duty - _TC": 12 }, "_Test Account Excise Duty @ 15": { "_Test Account Excise Duty - _TC": 15 }, "_Test Account Excise Duty @ 20": { "_Test Account Excise Duty - _TC": 20 }, "_Test Item Tax Template 1": { "_Test Account Excise Duty - _TC": 5, "_Test Account Education Cess - _TC": 10, "_Test Account S&H Education Cess - _TC": 15 } } for data in expected_item_tax_template: details = get_item_details({ "item_code": data['item_code'], "tax_category": data['tax_category'], "company": "_Test Company", "price_list": "_Test Price List", "currency": "_Test Currency", "doctype": "Sales Order", "conversion_rate": 1, "price_list_currency": "_Test Currency", "plc_conversion_rate": 1, "order_type": "Sales", "customer": "_Test Customer", "conversion_factor": 1, "price_list_uom_dependant": 1, "ignore_pricing_rule": 1 }) self.assertEqual(details.item_tax_template, data['item_tax_template']) self.assertEqual(json.loads(details.item_tax_rate), expected_item_tax_map[details.item_tax_template])