def get_regional_address_details(party_details, doctype, company): if isinstance(party_details, str): party_details = json.loads(party_details) party_details = frappe._dict(party_details) update_party_details(party_details, doctype) party_details.place_of_supply = get_place_of_supply(party_details, doctype) if is_internal_transfer(party_details, doctype): party_details.taxes_and_charges = "" party_details.taxes = [] return party_details if doctype in ("Sales Invoice", "Delivery Note", "Sales Order", "Quotation"): master_doctype = "Sales Taxes and Charges Template" tax_template_by_category = get_tax_template_based_on_category( master_doctype, company, party_details) elif doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt"): master_doctype = "Purchase Taxes and Charges Template" tax_template_by_category = get_tax_template_based_on_category( master_doctype, company, party_details) if tax_template_by_category: party_details["taxes_and_charges"] = tax_template_by_category party_details["taxes"] = get_taxes_and_charges( master_doctype, tax_template_by_category) return party_details if not party_details.place_of_supply: return party_details if not party_details.company_gstin: return party_details if (doctype in ("Sales Invoice", "Delivery Note", "Sales Order") and party_details.company_gstin and party_details.company_gstin[:2] != party_details.place_of_supply[:2] ) or (doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt") and party_details.supplier_gstin and party_details.supplier_gstin[:2] != party_details.place_of_supply[:2]): default_tax = get_tax_template(master_doctype, company, 1, party_details.company_gstin[:2]) else: default_tax = get_tax_template(master_doctype, company, 0, party_details.company_gstin[:2]) if not default_tax: return party_details party_details["taxes_and_charges"] = default_tax party_details["taxes"] = get_taxes_and_charges(master_doctype, default_tax) return party_details
def get_tax_template_for_sez(party_details, master_doctype, company, party_type): gst_details = frappe.db.get_value( party_type, {'name': party_details.get(frappe.scrub(party_type))}, ['gst_category', 'export_type'], as_dict=1) if gst_details: if gst_details.gst_category == 'SEZ' and gst_details.export_type == 'With Payment of Tax': default_tax = frappe.db.get_value( master_doctype, { "company": company, "is_inter_state": 1, "disabled": 0, "gst_state": number_state_mapping[party_details.company_gstin[:2]] }) party_details["taxes_and_charges"] = default_tax party_details.taxes = get_taxes_and_charges( master_doctype, default_tax)
def get_regional_address_details(out, doctype, company): out.place_of_supply = get_place_of_supply(out, doctype) if not out.place_of_supply: return if doctype in ("Sales Invoice", "Delivery Note"): master_doctype = "Sales Taxes and Charges Template" if not out.company_gstin: return elif doctype == "Purchase Invoice": master_doctype = "Purchase Taxes and Charges Template" if not out.supplier_gstin: return if ((doctype in ("Sales Invoice", "Delivery Note") and out.company_gstin and out.company_gstin[:2] != out.place_of_supply[:2]) or (doctype == "Purchase Invoice" and out.supplier_gstin and out.supplier_gstin[:2] != out.place_of_supply[:2])): default_tax = frappe.db.get_value(master_doctype, { "company": company, "is_inter_state": 1, "disabled": 0 }) else: default_tax = frappe.db.get_value(master_doctype, { "company": company, "disabled": 0, "is_default": 1 }) if not default_tax: return out["taxes_and_charges"] = default_tax out.taxes = get_taxes_and_charges(master_doctype, default_tax)
def get_regional_address_details(out, doctype, company): out.place_of_supply = get_place_of_supply(out, doctype) if not out.place_of_supply: return if doctype in ("Sales Invoice", "Delivery Note"): master_doctype = "Sales Taxes and Charges Template" if not out.company_gstin: return elif doctype == "Purchase Invoice": master_doctype = "Purchase Taxes and Charges Template" if not out.supplier_gstin: return if ((doctype in ("Sales Invoice", "Delivery Note") and out.company_gstin and out.company_gstin[:2] != out.place_of_supply[:2]) or (doctype == "Purchase Invoice" and out.supplier_gstin and out.supplier_gstin[:2] != out.place_of_supply[:2])): default_tax = frappe.db.get_value(master_doctype, {"company": company, "is_inter_state":1, "disabled":0}) else: default_tax = frappe.db.get_value(master_doctype, {"company": company, "disabled":0, "is_default": 1}) if not default_tax: return out["taxes_and_charges"] = default_tax out.taxes = get_taxes_and_charges(master_doctype, default_tax)
def make_sales_order(customer,currency, price_list, discount, users, billing_cycle=None, coupon=None): try: sales_order = frappe.new_doc("Sales Order") sales_order.customer = customer sales_order.coupon = coupon sales_order.delivery_date = nowdate() sales_order.selling_price_list = price_list sales_order.currency = currency sales_order.discount_amount = discount if billing_cycle: sales_order.append("items",{ "item_code": billing_cycle, "item_name": billing_cycle, "qty": cint(users), "delivery_date": nowdate() }) tax_template = frappe.get_value("Sales Taxes and Charges Template", {"is_default": 1}) taxes = get_taxes_and_charges('Sales Taxes and Charges Template', tax_template) for tax in taxes: sales_order.append('taxes', tax) sales_order.insert(ignore_permissions=True) sales_order.submit() return sales_order except Exception as e: print(frappe.get_traceback())
def get_tax_template_based_on_category(master_doctype, company, party_details): if not party_details.get('tax_category'): return default_tax = frappe.db.get_value(master_doctype, {'company': company, 'tax_category': party_details.get('tax_category')}, 'name') if default_tax: party_details["taxes_and_charges"] = default_tax party_details.taxes = get_taxes_and_charges(master_doctype, default_tax)
def get_regional_address_details(party_details, doctype, company): if isinstance(party_details, string_types): party_details = json.loads(party_details) party_details = frappe._dict(party_details) update_party_details(party_details, doctype) party_details.place_of_supply = get_place_of_supply(party_details, doctype) if is_internal_transfer(party_details, doctype): party_details.taxes_and_charges = '' party_details.taxes = '' return party_details if doctype in ("Sales Invoice", "Delivery Note", "Sales Order"): master_doctype = "Sales Taxes and Charges Template" get_tax_template_for_sez(party_details, master_doctype, company, 'Customer') get_tax_template_based_on_category(master_doctype, company, party_details) if party_details.get('taxes_and_charges'): return party_details if not party_details.company_gstin: return party_details elif doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt"): master_doctype = "Purchase Taxes and Charges Template" get_tax_template_for_sez(party_details, master_doctype, company, 'Supplier') get_tax_template_based_on_category(master_doctype, company, party_details) if party_details.get('taxes_and_charges'): return party_details if not party_details.supplier_gstin: return party_details if not party_details.place_of_supply: return party_details if not party_details.company_gstin: return party_details if ((doctype in ("Sales Invoice", "Delivery Note", "Sales Order") and party_details.company_gstin and party_details.company_gstin[:2] != party_details.place_of_supply[:2]) or (doctype in ("Purchase Invoice", "Purchase Order", "Purchase Receipt") and party_details.supplier_gstin and party_details.supplier_gstin[:2] != party_details.place_of_supply[:2])): default_tax = get_tax_template(master_doctype, company, 1, party_details.company_gstin[:2]) else: default_tax = get_tax_template(master_doctype, company, 0, party_details.company_gstin[:2]) if not default_tax: return party_details party_details["taxes_and_charges"] = default_tax party_details.taxes = get_taxes_and_charges(master_doctype, default_tax) return party_details
def set_missing_lead_customer_details(self, for_validate=False): customer, lead = None, None if getattr(self, "customer", None): customer = self.customer elif self.doctype == "Opportunity" and self.party_name: if self.opportunity_from == "Customer": customer = self.party_name else: lead = self.party_name elif self.doctype == "Quotation" and self.party_name: if self.quotation_to == "Customer": customer = self.party_name else: lead = self.party_name if customer: from erpnext.accounts.party import _get_party_details fetch_payment_terms_template = False if self.get("__islocal") or self.company != frappe.db.get_value( self.doctype, self.name, "company"): fetch_payment_terms_template = True party_details = _get_party_details( customer, ignore_permissions=self.flags.ignore_permissions, doctype=self.doctype, company=self.company, posting_date=self.get("posting_date"), fetch_payment_terms_template=fetch_payment_terms_template, party_address=self.customer_address, shipping_address=self.shipping_address_name, company_address=self.get("company_address"), ) if not self.meta.get_field("sales_team"): party_details.pop("sales_team") self.update_if_missing(party_details) elif lead: from erpnext.crm.doctype.lead.lead import get_lead_details self.update_if_missing( get_lead_details( lead, posting_date=self.get("transaction_date") or self.get("posting_date"), company=self.company, )) if self.get("taxes_and_charges" ) and not self.get("taxes") and not for_validate: taxes = get_taxes_and_charges("Sales Taxes and Charges Template", self.taxes_and_charges) for tax in taxes: self.append("taxes", tax)
def set_missing_values(source, target): target.run_method("set_missing_values") if target.doctype == 'Purchase Receipt': master_doctype = 'Purchase Taxes and Charges Template' else: master_doctype = 'Sales Taxes and Charges Template' if not target.get('taxes') and target.get('taxes_and_charges'): for tax in get_taxes_and_charges(master_doctype, target.get('taxes_and_charges')): target.append('taxes', tax)
def set_missing_values(source, target): target.update_stock = 1 target.set_warehouse = source.customer_warehouse target.taxes_and_charges = source.sales_taxes_and_charges_template taxes = get_taxes_and_charges( 'Sales Taxes and Charges Template', source.sales_taxes_and_charges_template) for tax in taxes: target.append('taxes', tax) target.flags.ignore_permissions = ignore_permissions target.cost_center = source.cost_center target.run_method("set_missing_values") target.run_method("calculate_taxes_and_totals")
def set_missing_values(source, target): target.run_method("set_missing_values") set_purchase_references(target) if target.doctype == "Purchase Receipt": master_doctype = "Purchase Taxes and Charges Template" else: master_doctype = "Sales Taxes and Charges Template" if not target.get("taxes") and target.get("taxes_and_charges"): for tax in get_taxes_and_charges(master_doctype, target.get("taxes_and_charges")): target.append("taxes", tax)
def getTax(sales_invoice): taxes = get_taxes_and_charges('Sales Taxes and Charges Template', sales_invoice.taxes_and_charges) for tax in taxes: sales_invoice.append('taxes', tax)
def update_tax_table(doc): taxes = get_taxes_and_charges('Sales Taxes and Charges Template', doc.taxes_and_charges) for tax in taxes: doc.append('taxes', tax)
def getTax(sales_invoice): taxes = get_taxes_and_charges( "Sales Taxes and Charges Template", sales_invoice.taxes_and_charges ) for tax in taxes: sales_invoice.append("taxes", tax)
def get_invoice_dict(self): def get_item_dict(): default_uom = db.get_single_value("Stock Settings", "stock_uom") or _("Nos") cost_center = db.get_value("Company", self.get("company"), "cost_center") if not cost_center: frappe.throw( _("Please set the Default Cost Center in {0} company") \ .format(frappe.bold(self.get("company"))) ) item_name = _("Opening Invoice Item") return frappe._dict({ "uom": default_uom, "rate": flt(self.due_capital_balance), "qty": 1.000, "conversion_factor": 1.000, "item_name": item_name, "description": item_name, income_expense_account_field: self.get("temporary_opening_account"), "cost_center": cost_center, }) from erpnext.controllers.accounts_controller import get_taxes_and_charges master_doctype = "Sales Taxes and Charges Template" master_name = frappe.db.get_value(master_doctype, { "disabled": False, "is_default": True, }, "name") if not master_name: frappe.throw(_("Please specify the default {}" \ .format(master_doctype))) income_expense_account_field = "income_account" item = get_item_dict() taxes = get_taxes_and_charges(master_doctype, master_name) if taxes: # taxes[0].included_in_print_rate = True taxes[0].charge_type = "Actual" taxes[0].tax_amount = self.get("tax_amount") args = frappe._dict({ "is_pos": False, "items": [item], "taxes_and_charges": master_name, "taxes": taxes, "is_opening": "No", "set_posting_time": 1.000, "company": self.get("company"), "due_date": self.get("posting_date") \ if self.get("due_date") < self.get("posting_date") \ else self.get("due_date"), "posting_date": self.get("posting_date"), "customer": self.get("customer"), "doctype": "Sales Invoice", "currency": frappe.db.get_value("Company", self.get("company"), "default_currency") }) return args
def add_to_basket(_items): customer = get_customer(frappe.session.user).name quotation = frappe.db.sql( """SELECT `name` FROM `tabQuotation` WHERE `party_name` = '{customer}' AND `docstatus` = 0 LIMIT 1""" .format(customer=customer), as_dict=True) _items = json.loads(_items) if len(quotation) == 1: quotation = frappe.get_doc("Quotation", quotation[0].name) new_item_found = [] for item in _items: # check if item already exist in QTN old_item = frappe.db.sql( """SELECT `qty`, `name`, `item_code`, `rate` FROM `tabQuotation Item` WHERE `parent` = '{quotation}' AND `item_code` = '{item_code}'""" .format(quotation=quotation.name, item_code=item[0]), as_dict=True) if len(old_item) == 1: # update qty new_qty = old_item[0].qty + float(item[1]) new_amount = new_qty * old_item[0].rate update = frappe.db.sql( """UPDATE `tabQuotation Item` SET `qty` = '{new_qty}', `amount` = '{new_amount}' WHERE `name` = '{name}'""" .format(new_qty=new_qty, new_amount=new_amount, name=old_item[0].name), as_list=True) else: # add item to quotation new_item_found.append({'item_code': item[0], 'qty': item[1]}) frappe.db.commit() if len(new_item_found) > 0: for item in new_item_found: quotation = frappe.get_doc("Quotation", quotation.name) row = quotation.append('items', {}) row.item_code = item["item_code"] row.qty = item["qty"] quotation.save(ignore_permissions=True) frappe.db.commit() else: items = [] for item in _items: item_dict = {'item_code': item[0], 'qty': item[1]} items.append(item_dict) quotation = frappe.get_doc({ "doctype": "Quotation", "party_name": customer, "items": items, "order_type": "Shopping Cart", "selling_price_list": "WooCommerce Listpreise", "taxes_and_charges": "Gemischte MWST Warenverkauf (302)", "taxes": get_taxes_and_charges( master_doctype='Sales Taxes and Charges Template', master_name='Gemischte MWST Warenverkauf (302)') }) quotation.insert(ignore_permissions=True) return quotation.name
def create_purchase_invoice(row): import json if type(row) == str: _row = frappe._dict(json.loads(row)) row = frappe.get_doc("Landed Cost Taxes and Charges", _row.name) if row.invoice: return "This LCV already has an invoice" total = abs(row.total) amount = abs(row.amount) p_inv = frappe.new_doc("Purchase Invoice") stock_uom = frappe.db.get_single_value("Stock Settings", "stock_uom") credit_to_dop, credit_to_usd = frappe.get_value( "Company", row.company, ["default_payable_account", "default_payable_account_usd"]) credit_obj = { "DOP": credit_to_dop, "USD": credit_to_usd, } if not row.expense_account: frappe.throw("Please set and expense account") inv_date = row.date or nowdate() p_inv.update({ "invoice_type": "Services", "supplier": row.supplier, "posting_date": inv_date, "date": inv_date, "bill_no": inv_date, "bill_date": inv_date, "supplier_invoice_no": row.supplier_invoice_no, "tipo_bienes_y_servicios_comprados": row.tipo_bienes_y_servicios_comprados, "company": frappe.db.get_value("Landed Cost Voucher", row.parent, "company"), "due_date": add_days(inv_date, 30), "update_stock": 0, "cost_center": row.cost_center, "credit_to": credit_obj[row.currency], "is_petty_cash": row.is_petty_cash, "set_posting_time": 1, "bill_no": row.supplier_invoice or "", "vencimiento_ncf": row.expiry_date or "", "taxes_and_charges": row.purchase_taxes_and_charges_template or "", "price_list": "Compra estándar {}".format(row.currency or "DOP"), "currency": row.currency or "DOP", "price_list_currency": row.currency or "DOP", "party_account_currency": row.currency or "DOP", "exchange_rate": row.exchange_rate or "1", "account": row.expense_account, }) # Let's create the item if doesn't exists item_name = "Voucher Expenses" p_inv.append( "items", { "item_name": item_name, "qty": 1, "uom": stock_uom or "Unit", "conversion_factor": 1, "stock_uom": stock_uom or "Unit", "cost_center": row.cost_center, "stock_qty": 1, "expense_account": row.expense_account, "remarks": row.description, "rate": total if total else amount, }) p_inv.set_missing_values() if row.purchase_taxes_and_charges_template and not row.tax_amount: frappe.throw("Favor especificar el monto del impuesto") if row.purchase_taxes_and_charges_template: for r in get_taxes_and_charges( master_doctype="Purchase Taxes and Charges Template", master_name=row.purchase_taxes_and_charges_template, ): r.update({"charge_type": "Actual", "tax_amount": row.tax_amount}) p_inv.append("taxes", r) p_inv.calculate_taxes_and_totals() p_inv.save() p_inv.submit() row.update({ "invoice": p_inv.name, "date": p_inv.posting_date, "create_invoice": 1 #if created using the button this needs to be set to 1 }) row.db_update() if row.amount < 0: pay_and_return(p_inv) return "Purchase Invoice {} Created".format(p_inv.name)
def item_validate(doc, method): if doc.taxes_template and not doc.taxes: doc.extend( "taxes", get_taxes_and_charges('Item Taxes Template', doc.taxes_template))
def get_taxes_template(item_code): item_tax_template = get_taxes_and_charges("Item", item_code) if len(item_tax_template) > 0: return item_tax_template[0]["item_tax_template"] else: return ""
def update_tax_table(doc): taxes = get_taxes_and_charges('Sales Taxes and Charges Template', doc.taxes_and_charges) for tax in taxes: doc.append('taxes', tax)
def getTax(purchase_invoice): taxes = get_taxes_and_charges('Purchase Taxes and Charges Template', purchase_invoice.taxes_and_charges) for tax in taxes: purchase_invoice.append('taxes', tax)