def set_missing_values(source, target): from erpnext.controllers.accounts_controller import get_default_taxes_and_charges quotation = frappe.get_doc(target) company_currency = frappe.db.get_value("Company", quotation.company, "default_currency") party_account_currency = get_party_account_currency("Customer", quotation.customer, quotation.company) if quotation.customer else company_currency quotation.currency = party_account_currency or company_currency if company_currency == quotation.currency: exchange_rate = 1 else: exchange_rate = get_exchange_rate(quotation.currency, company_currency, quotation.transaction_date) quotation.conversion_rate = exchange_rate # get default taxes taxes = get_default_taxes_and_charges("Sales Taxes and Charges Template") if taxes: quotation.extend("taxes", taxes) quotation.run_method("set_missing_values") quotation.run_method("calculate_taxes_and_totals") if not source.with_items: quotation.opportunity = source.name
def set_missing_values(source, target): from erpnext.controllers.accounts_controller import get_default_taxes_and_charges quotation = frappe.get_doc(target) company_currency = frappe.get_cached_value('Company', quotation.company, "default_currency") if quotation.quotation_to == 'Customer' and quotation.party_name: party_account_currency = get_party_account_currency("Customer", quotation.party_name, quotation.company) else: party_account_currency = company_currency quotation.currency = party_account_currency or company_currency if company_currency == quotation.currency: exchange_rate = 1 else: exchange_rate = get_exchange_rate(quotation.currency, company_currency, quotation.transaction_date, args="for_selling") quotation.conversion_rate = exchange_rate # get default taxes taxes = get_default_taxes_and_charges("Sales Taxes and Charges Template", company=quotation.company) if taxes.get('taxes'): quotation.update(taxes) quotation.run_method("set_missing_values") quotation.run_method("calculate_taxes_and_totals") if not source.with_items: quotation.opportunity = source.name
def create_supplier_quotation(doc): if isinstance(doc, basestring): doc = json.loads(doc) validate_duplicate_supplier_quotation(doc) try: sq_doc = frappe.get_doc( { "doctype": "Supplier Quotation", "supplier": doc.get("supplier"), "terms": doc.get("terms"), "company": doc.get("company"), "currency": doc.get("currency") or get_party_account_currency("Supplier", doc.get("supplier"), doc.get("company")), "buying_price_list": doc.get("buying_price_list") or frappe.db.get_value("Buying Settings", None, "buying_price_list"), } ) add_items(sq_doc, doc.get("supplier"), doc.get("items")) sq_doc.flags.ignore_permissions = True sq_doc.run_method("set_missing_values") sq_doc.save() frappe.msgprint(_("Supplier Quotation {0} created").format(sq_doc.name)) return sq_doc.name except Exception: return
def postprocess(source, target_doc): target_doc.supplier = for_supplier args = get_party_details(for_supplier, party_type="Supplier", ignore_permissions=True) target_doc.currency = args.currency or get_party_account_currency("Supplier", for_supplier, source.company) target_doc.buying_price_list = args.buying_price_list or frappe.db.get_value( "Buying Settings", None, "buying_price_list" ) set_missing_values(source, target_doc)
def get_customers(pos_profile, doc): filters = {'disabled': 0} customer_list = [] customers = frappe.get_all("Customer", fields=["*"], filters = filters) for customer in customers: customer_currency = get_party_account_currency('Customer', customer.name, doc.company) or doc.currency if customer_currency == doc.currency: customer_list.append(customer) return customer_list
def validate_currency(self): if self.get("currency"): party_type, party = self.get_party() if party_type and party: party_account_currency = get_party_account_currency(party_type, party, self.company) if (party_account_currency and party_account_currency != self.company_currency and self.currency != party_account_currency): frappe.throw(_("Accounting Entry for {0}: {1} can only be made in currency: {2}") .format(party_type, party, party_account_currency), InvalidCurrency)
def run_purchase(current_date): # make material requests for purchase items that have negative projected qtys if can_make("Material Request"): report = "Items To Be Requested" for row in query_report.run(report)["result"][:how_many("Material Request")]: make_material_request(current_date, row[0], -row[-1]) # get supplier details supplier = get_random("Supplier") company_currency = frappe.db.get_value("Company", "Wind Power LLC", "default_currency") party_account_currency = get_party_account_currency("Supplier", supplier, "Wind Power LLC") if company_currency == party_account_currency: exchange_rate = 1 else: exchange_rate = get_exchange_rate(party_account_currency, company_currency) # make supplier quotations if can_make("Supplier Quotation"): from erpnext.stock.doctype.material_request.material_request import make_supplier_quotation report = "Material Requests for which Supplier Quotations are not created" for row in query_report.run(report)["result"][:how_many("Supplier Quotation")]: if row[0] != "'Total'": sq = frappe.get_doc(make_supplier_quotation(row[0])) sq.transaction_date = current_date sq.fiscal_year = cstr(current_date.year) sq.supplier = supplier sq.currency = party_account_currency or company_currency sq.conversion_rate = exchange_rate sq.insert() sq.submit() frappe.db.commit() # make purchase orders if can_make("Purchase Order"): from erpnext.stock.doctype.material_request.material_request import make_purchase_order report = "Requested Items To Be Ordered" for row in query_report.run(report)["result"][:how_many("Purchase Order")]: if row[0] != "'Total'": po = frappe.get_doc(make_purchase_order(row[0])) po.supplier = supplier po.currency = party_account_currency or company_currency po.conversion_rate = exchange_rate po.transaction_date = current_date po.fiscal_year = cstr(current_date.year) po.insert() po.submit() frappe.db.commit() if can_make("Subcontract"): make_subcontract(current_date)
def make_quotation(): # get open opportunites opportunity = get_random("Opportunity", {"status": "Open", "with_items": 1}) if opportunity: from erpnext.crm.doctype.opportunity.opportunity import make_quotation qtn = frappe.get_doc(make_quotation(opportunity)) qtn.insert() frappe.db.commit() qtn.submit() frappe.db.commit() else: # make new directly # get customer, currency and exchange_rate customer = get_random("Customer") company_currency = frappe.db.get_value("Company", "Wind Power LLC", "default_currency") party_account_currency = get_party_account_currency("Customer", customer, "Wind Power LLC") if company_currency == party_account_currency: exchange_rate = 1 else: exchange_rate = get_exchange_rate(party_account_currency, company_currency) qtn = frappe.get_doc( { "creation": frappe.flags.current_date, "doctype": "Quotation", "quotation_to": "Customer", "customer": customer, "currency": party_account_currency or company_currency, "conversion_rate": exchange_rate, "order_type": "Sales", "transaction_date": frappe.flags.current_date, } ) add_random_children( qtn, "items", rows=3, randomize={"qty": (1, 5), "item_code": ("Item", {"has_variants": "0", "is_fixed_asset": 0})}, unique="item_code", ) qtn.insert() frappe.db.commit() qtn.submit() frappe.db.commit()
def validate_supplier(self): prevent_po = frappe.db.get_value("Supplier", self.supplier, 'prevent_pos') if prevent_po: standing = frappe.db.get_value("Supplier Scorecard", self.supplier, 'status') if standing: frappe.throw(_("Purchase Orders are not allowed for {0} due to a scorecard standing of {1}.") .format(self.supplier, standing)) warn_po = frappe.db.get_value("Supplier", self.supplier, 'warn_pos') if warn_po: standing = frappe.db.get_value("Supplier Scorecard",self.supplier, 'status') frappe.msgprint(_("{0} currently has a {1} Supplier Scorecard standing, and Purchase Orders to this supplier should be issued with caution.").format(self.supplier, standing), title=_("Caution"), indicator='orange') self.party_account_currency = get_party_account_currency("Supplier", self.supplier, self.company)
def set_missing_values(source, target): quotation = frappe.get_doc(target) company_currency = frappe.db.get_value("Company", quotation.company, "default_currency") party_account_currency = get_party_account_currency("Customer", quotation.customer, quotation.company) if company_currency == party_account_currency: exchange_rate = 1 else: exchange_rate = get_exchange_rate(party_account_currency, company_currency) quotation.currency = party_account_currency or company_currency quotation.conversion_rate = exchange_rate quotation.run_method("set_missing_values") quotation.run_method("calculate_taxes_and_totals")
def set_missing_values(source, target): quotation = frappe.get_doc(target) company_currency = frappe.db.get_value("Company", quotation.company, "default_currency") party_account_currency = get_party_account_currency( "Customer", quotation.customer, quotation.company) if company_currency == party_account_currency: exchange_rate = 1 else: exchange_rate = get_exchange_rate(party_account_currency, company_currency) quotation.currency = party_account_currency or company_currency quotation.conversion_rate = exchange_rate quotation.run_method("set_missing_values") quotation.run_method("calculate_taxes_and_totals")
def set_missing_values(source, target): from erpnext.controllers.accounts_controller import get_default_taxes_and_charges quotation = frappe.get_doc(target) quotation.order_type = "Maintenance" company_currency = frappe.get_cached_value('Company', quotation.company, "default_currency") if quotation.quotation_to == 'Customer' and quotation.party_name: party_account_currency = get_party_account_currency( "Customer", quotation.party_name, quotation.company) else: party_account_currency = company_currency quotation.currency = party_account_currency or company_currency if company_currency == quotation.currency: exchange_rate = 1 else: exchange_rate = get_exchange_rate(quotation.currency, company_currency, quotation.transaction_date, args="for_selling") quotation.conversion_rate = exchange_rate # add item quotation.append( 'items', { 'item_code': source.item, 'qty': source.billing_qty, 'uom': source.sales_uom, 'item_booking': source.name }) # get default taxes taxes = get_default_taxes_and_charges( "Sales Taxes and Charges Template", company=quotation.company) if taxes.get('taxes'): quotation.update(taxes) quotation.run_method("set_missing_values") quotation.run_method("calculate_taxes_and_totals")
def validate_currency(self): company_currency = get_company_currency(self.company) account_currency = get_account_currency(self.account) if not self.account_currency: self.account_currency = company_currency if account_currency != self.account_currency: frappe.throw(_("Accounting Entry for {0} can only be made in currency: {1}") .format(self.account, (account_currency or company_currency)), InvalidAccountCurrency) if self.party_type and self.party: party_account_currency = get_party_account_currency(self.party_type, self.party, self.company) if party_account_currency != self.account_currency: frappe.throw(_("Accounting Entry for {0}: {1} can only be made in currency: {2}") .format(self.party_type, self.party, party_account_currency), InvalidAccountCurrency) validate_party_gle_currency(self.party_type, self.party, self.company)
def create_supplier_quotation(doc): if isinstance(doc, string_types): doc = json.loads(doc) try: sq_doc = frappe.get_doc({ "doctype": "Supplier Quotation", "supplier": doc.get('supplier'), "terms": doc.get("terms"), "company": doc.get("company"), "currency": doc.get('currency') or get_party_account_currency('Supplier', doc.get('supplier'), doc.get('company')), "buying_price_list": doc.get('buying_price_list') or frappe.db.get_value('Buying Settings', None, 'buying_price_list') }) add_items(sq_doc, doc.get('supplier'), doc.get('items')) sq_doc.flags.ignore_permissions = True sq_doc.run_method("set_missing_values") sq_doc.save() frappe.msgprint(_("Supplier Quotation {0} created").format(sq_doc.name)) return sq_doc.name except Exception: return None
def load_dashboard_info(self): #info = get_dashboard_info("Customer", self.name) party=self.customer party_type="Customer" current_fiscal_year = get_fiscal_year(nowdate(), as_dict=True) company = frappe.db.get_default("company") or frappe.get_all("Company")[0].name party_account_currency = get_party_account_currency(party_type, party, company) company_default_currency = get_default_currency() \ or frappe.db.get_value('Company', company, 'default_currency') if party_account_currency==company_default_currency: total_field = "base_grand_total" else: total_field = "grand_total" doctype = "Sales Invoice" billing_this_year = frappe.db.sql(""" select sum({0}) from `tab{1}` where {2}=%s and docstatus=1 and posting_date between %s and %s """.format(total_field, doctype, party_type.lower()), (party, current_fiscal_year.year_start_date, current_fiscal_year.year_end_date)) total_unpaid = frappe.db.sql(""" select sum(debit_in_account_currency) - sum(credit_in_account_currency) from `tabGL Entry` where party_type = %s and party=%s""", (party_type, party)) info = {} info["billing_this_year"] = flt(billing_this_year[0][0]) if billing_this_year else 0 info["currency"] = party_account_currency info["total_unpaid"] = flt(total_unpaid[0][0]) if total_unpaid else 0 if party_type == "Supplier": info["total_unpaid"] = -1 * info["total_unpaid"] return info self.set_onload('dashboard_info', info)
def validate_supplier(self): prevent_po = frappe.db.get_value("Supplier", self.supplier, 'prevent_pos') if prevent_po: standing = frappe.db.get_value("Supplier Scorecard", self.supplier, 'status') if standing: frappe.throw( _("Purchase Orders are not allowed for {0} due to a scorecard standing of {1}." ).format(self.supplier, standing)) warn_po = frappe.db.get_value("Supplier", self.supplier, 'warn_pos') if warn_po: standing = frappe.db.get_value("Supplier Scorecard", self.supplier, 'status') frappe.msgprint(_( "{0} currently has a {1} Supplier Scorecard standing, and Purchase Orders to this supplier should be issued with caution." ).format(self.supplier, standing), title=_("Caution"), indicator='orange') self.party_account_currency = get_party_account_currency( "Supplier", self.supplier, self.company)
def set_missing_values(source, target): from erpnext.controllers.accounts_controller import get_default_taxes_and_charges quotation = frappe.get_doc(target) company_currency = frappe.get_cached_value("Company", quotation.company, "default_currency") if quotation.quotation_to == "Customer" and quotation.party_name: party_account_currency = get_party_account_currency( "Customer", quotation.party_name, quotation.company) else: party_account_currency = company_currency quotation.currency = party_account_currency or company_currency if company_currency == quotation.currency: exchange_rate = 1 else: exchange_rate = get_exchange_rate(quotation.currency, company_currency, quotation.transaction_date, args="for_selling") quotation.conversion_rate = exchange_rate # get default taxes taxes = get_default_taxes_and_charges( "Sales Taxes and Charges Template", company=quotation.company) if taxes.get("taxes"): quotation.update(taxes) quotation.run_method("set_missing_values") quotation.run_method("calculate_taxes_and_totals") if not source.with_items: quotation.opportunity = source.name
def get_data(filters, show_party_name): if filters.get('party_type') in ('Customer', 'Supplier', 'Employee', 'Member'): party_name_field = "{0}_name".format( frappe.scrub(filters.get('party_type'))) elif filters.get('party_type') == 'Student': party_name_field = 'first_name' elif filters.get('party_type') == 'Shareholder': party_name_field = 'title' else: party_name_field = 'name' party_filters = { "name": filters.get("party") } if filters.get("party") else {} parties = frappe.get_all(filters.get("party_type"), fields=["name", party_name_field], filters=party_filters, order_by="name") company_currency = frappe.get_cached_value('Company', filters.company, "default_currency") opening_balances = get_opening_balances(filters) balances_within_period = get_balances_within_period(filters) data = [] # total_debit, total_credit = 0, 0 total_row = frappe._dict({ "opening_debit": 0, "opening_credit": 0, "debit": 0, "credit": 0, "closing_debit": 0, "closing_credit": 0 }) for party in parties: row = {"party": party.name} if show_party_name: row["party_name"] = party.get(party_name_field) row["party_currency"] = get_party_account_currency( filters.get('party_type'), party.name, filters.get('company')) # opening opening_debit, opening_credit = opening_balances.get( party.name, [0, 0]) row.update({ "opening_debit": opening_debit, "opening_credit": opening_credit }) # within period debit, credit = balances_within_period.get(party.name, [0, 0]) row.update({"debit": debit, "credit": credit}) # closing closing_debit, closing_credit = toggle_debit_credit( opening_debit + debit, opening_credit + credit) row.update({ "closing_debit": closing_debit, "closing_credit": closing_credit }) # totals for col in total_row: total_row[col] += row.get(col) row.update({ "currency": get_party_account_currency(filters.get('party_type'), party.name, filters.get('company')) }) has_value = False if (opening_debit or opening_credit or debit or credit or closing_debit or closing_credit): has_value = True if cint(filters.show_zero_values) or has_value: data.append(row) # Add total row ##total_row.update({ ## "party": "'" + _("Totals") + "'", ## "currency": company_currency ##}) ##data.append(total_row) return data
def run(): frappe.set_user("*****@*****.**") frappe.set_user_lang("fr") if random.random() < 0.6: report = "Items To Be Requested" for row in query_report.run(report)["result"][:random.randint(1, 5)]: item_code, qty = row[0], abs(row[-1]) mr = make_material_request(item_code, qty) if random.random() < 0.6: for mr in frappe.get_all('Material Request', filters={'material_request_type': 'Purchase', 'status': 'Open'}, limit=random.randint(1,6)): if not frappe.get_all('Request for Quotation', filters={'material_request': mr.name}, limit=1): rfq = make_request_for_quotation(mr.name) rfq.transaction_date = frappe.flags.current_date add_suppliers(rfq) rfq.save() rfq.submit() # Make suppier quotation from RFQ against each supplier. if random.random() < 0.6: for rfq in frappe.get_all('Request for Quotation', filters={'status': 'Open'}, limit=random.randint(1, 6)): if not frappe.get_all('Supplier Quotation', filters={'request_for_quotation': rfq.name}, limit=1): rfq = frappe.get_doc('Request for Quotation', rfq.name) for supplier in rfq.suppliers: supplier_quotation = make_quotation_from_rfq(rfq.name, supplier.supplier) supplier_quotation.save() supplier_quotation.submit() # get supplier details supplier = get_random("Supplier") company_currency = frappe.get_cached_value('Company', erpnext.get_default_company(), "default_currency") party_account_currency = get_party_account_currency("Supplier", supplier, erpnext.get_default_company()) if company_currency == party_account_currency: exchange_rate = 1 else: exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_buying") # make supplier quotations if random.random() < 0.5: from erpnext.stock.doctype.material_request.material_request import make_supplier_quotation report = "Material Requests for which Supplier Quotations are not created" for row in query_report.run(report)["result"][:random.randint(1, 3)]: if row[0] != "Total": sq = frappe.get_doc(make_supplier_quotation(row[0])) sq.transaction_date = frappe.flags.current_date sq.supplier = supplier sq.currency = party_account_currency or company_currency sq.conversion_rate = exchange_rate sq.insert() sq.submit() frappe.db.commit() # make purchase orders if random.random() < 0.5: from erpnext.stock.doctype.material_request.material_request import make_purchase_order report = "Requested Items To Be Ordered" for row in query_report.run(report)["result"][:how_many("Purchase Order")]: if row[0] != "Total": try: po = frappe.get_doc(make_purchase_order(row[0])) po.supplier = supplier po.currency = party_account_currency or company_currency po.conversion_rate = exchange_rate po.transaction_date = frappe.flags.current_date po.insert() po.submit() except Exception: pass else: frappe.db.commit() if random.random() < 0.5: make_subcontract()
def get_report_pdf(doc, consolidated=True): statement_dict = {} ageing = '' base_template_path = "frappe/www/printview.html" template_path = "erpnext/accounts/doctype/process_statement_of_accounts/process_statement_of_accounts.html" for entry in doc.customers: if doc.include_ageing: ageing_filters = frappe._dict({ 'company': doc.company, 'report_date': doc.to_date, 'ageing_based_on': doc.ageing_based_on, 'range1': 30, 'range2': 60, 'range3': 90, 'range4': 120, 'customer': entry.customer }) col1, ageing = get_ageing(ageing_filters) if ageing: ageing[0]['ageing_based_on'] = doc.ageing_based_on tax_id = frappe.get_doc('Customer', entry.customer).tax_id presentation_currency = get_party_account_currency('Customer', entry.customer, doc.company) \ or doc.currency or get_company_currency(doc.company) filters = frappe._dict({ 'from_date': doc.from_date, 'to_date': doc.to_date, 'company': doc.company, 'finance_book': doc.finance_book if doc.finance_book else None, 'account': doc.account if doc.account else None, 'party_type': 'Customer', 'party': [entry.customer], 'presentation_currency': presentation_currency, 'group_by': doc.group_by, 'currency': doc.currency, 'cost_center': [cc.cost_center_name for cc in doc.cost_center], 'project': [p.project_name for p in doc.project], 'show_opening_entries': 0, 'include_default_book_entries': 0, 'tax_id': tax_id if tax_id else None }) col, res = get_soa(filters) for x in [0, -2, -1]: res[x]['account'] = res[x]['account'].replace("'", "") if len(res) == 3: continue html = frappe.render_template(template_path, \ {"filters": filters, "data": res, "ageing": ageing[0] if (doc.include_ageing and ageing) else None}) html = frappe.render_template(base_template_path, {"body": html, \ "css": get_print_style(), "title": "Statement For " + entry.customer}) statement_dict[entry.customer] = html if not bool(statement_dict): return False elif consolidated: result = ''.join(list(statement_dict.values())) return get_pdf(result, {'orientation': doc.orientation}) else: for customer, statement_html in statement_dict.items(): statement_dict[customer] = get_pdf( statement_html, {'orientation': doc.orientation}) return statement_dict
def create_invoice(self, prorate): """ Creates a `Invoice`, submits it and returns it """ doctype = "Sales Invoice" if self.party_type == "Customer" else "Purchase Invoice" invoice = frappe.new_doc(doctype) # For backward compatibility # Earlier subscription didn't had any company field company = self.get("company") or get_default_company() if not company: frappe.throw( _("Company is mandatory was generating invoice. Please set default company in Global Defaults" )) invoice.company = company invoice.set_posting_time = 1 invoice.posting_date = (self.current_invoice_start if self.generate_invoice_at_period_start else self.current_invoice_end) invoice.cost_center = self.cost_center if doctype == "Sales Invoice": invoice.customer = self.party else: invoice.supplier = self.party if frappe.db.get_value("Supplier", self.party, "tax_withholding_category"): invoice.apply_tds = 1 # Add party currency to invoice invoice.currency = get_party_account_currency(self.party_type, self.party, self.company) # Add dimensions in invoice for subscription: accounting_dimensions = get_accounting_dimensions() for dimension in accounting_dimensions: if self.get(dimension): invoice.update({dimension: self.get(dimension)}) # Subscription is better suited for service items. I won't update `update_stock` # for that reason items_list = self.get_items_from_plans(self.plans, prorate) for item in items_list: item["cost_center"] = self.cost_center invoice.append("items", item) # Taxes tax_template = "" if doctype == "Sales Invoice" and self.sales_tax_template: tax_template = self.sales_tax_template if doctype == "Purchase Invoice" and self.purchase_tax_template: tax_template = self.purchase_tax_template if tax_template: invoice.taxes_and_charges = tax_template invoice.set_taxes() # Due date if self.days_until_due: invoice.append( "payment_schedule", { "due_date": add_days(invoice.posting_date, cint(self.days_until_due)), "invoice_portion": 100, }, ) # Discounts if self.additional_discount_percentage: invoice.additional_discount_percentage = self.additional_discount_percentage if self.additional_discount_amount: invoice.discount_amount = self.additional_discount_amount if self.additional_discount_percentage or self.additional_discount_amount: discount_on = self.apply_additional_discount invoice.apply_discount_on = discount_on if discount_on else "Grand Total" # Subscription period invoice.from_date = self.current_invoice_start invoice.to_date = self.current_invoice_end invoice.flags.ignore_mandatory = True invoice.set_missing_values() invoice.save() if self.submit_invoice: invoice.submit() return invoice
def get_report_pdf(doc, consolidated=True): statement_dict = {} ageing = "" base_template_path = "frappe/www/printview.html" template_path = ( "erpnext/accounts/doctype/process_statement_of_accounts/process_statement_of_accounts.html" ) for entry in doc.customers: if doc.include_ageing: ageing_filters = frappe._dict({ "company": doc.company, "report_date": doc.to_date, "ageing_based_on": doc.ageing_based_on, "range1": 30, "range2": 60, "range3": 90, "range4": 120, "customer": entry.customer, }) col1, ageing = get_ageing(ageing_filters) if ageing: ageing[0]["ageing_based_on"] = doc.ageing_based_on tax_id = frappe.get_doc("Customer", entry.customer).tax_id presentation_currency = (get_party_account_currency( "Customer", entry.customer, doc.company) or doc.currency or get_company_currency(doc.company)) if doc.letter_head: from frappe.www.printview import get_letter_head letter_head = get_letter_head(doc, 0) filters = frappe._dict({ "from_date": doc.from_date, "to_date": doc.to_date, "company": doc.company, "finance_book": doc.finance_book if doc.finance_book else None, "account": [doc.account] if doc.account else None, "party_type": "Customer", "party": [entry.customer], "presentation_currency": presentation_currency, "group_by": doc.group_by, "currency": doc.currency, "cost_center": [cc.cost_center_name for cc in doc.cost_center], "project": [p.project_name for p in doc.project], "show_opening_entries": 0, "include_default_book_entries": 0, "tax_id": tax_id if tax_id else None, }) col, res = get_soa(filters) for x in [0, -2, -1]: res[x]["account"] = res[x]["account"].replace("'", "") if len(res) == 3: continue html = frappe.render_template( template_path, { "filters": filters, "data": res, "ageing": ageing[0] if (doc.include_ageing and ageing) else None, "letter_head": letter_head if doc.letter_head else None, "terms_and_conditions": frappe.db.get_value("Terms and Conditions", doc.terms_and_conditions, "terms") if doc.terms_and_conditions else None, }, ) html = frappe.render_template( base_template_path, { "body": html, "css": get_print_style(), "title": "Statement For " + entry.customer }, ) statement_dict[entry.customer] = html if not bool(statement_dict): return False elif consolidated: result = "".join(list(statement_dict.values())) return get_pdf(result, {"orientation": doc.orientation}) else: for customer, statement_html in statement_dict.items(): statement_dict[customer] = get_pdf( statement_html, {"orientation": doc.orientation}) return statement_dict
def get_party_account_currency(party_type, party, company): from erpnext.accounts.party import get_party_account_currency return get_party_account_currency(party_type, party, company)
def work(): frappe.set_user(frappe.db.get_global('demo_purchase_user')) if random.random() < 0.3: report = "Items To Be Requested" for row in query_report.run(report)["result"][:random.randint(1, 5)]: item_code, qty = row[0], abs(row[-1]) mr = make_material_request(item_code, qty) if random.random() < 0.3: for mr in frappe.get_all('Material Request', filters={'material_request_type': 'Purchase', 'status': 'Open'}, limit=random.randint(1,6)): if not frappe.get_all('Request for Quotation', filters={'material_request': mr.name}, limit=1): rfq = make_request_for_quotation(mr.name) rfq.transaction_date = frappe.flags.current_date add_suppliers(rfq) rfq.save() rfq.submit() # Make suppier quotation from RFQ against each supplier. if random.random() < 0.3: for rfq in frappe.get_all('Request for Quotation', filters={'status': 'Open'}, limit=random.randint(1, 6)): if not frappe.get_all('Supplier Quotation', filters={'request_for_quotation': rfq.name}, limit=1): rfq = frappe.get_doc('Request for Quotation', rfq.name) for supplier in rfq.suppliers: supplier_quotation = make_quotation_from_rfq(rfq.name, supplier.supplier) supplier_quotation.save() supplier_quotation.submit() # get supplier details supplier = get_random("Supplier") company_currency = frappe.db.get_value("Company", "Wind Power LLC", "default_currency") party_account_currency = get_party_account_currency("Supplier", supplier, "Wind Power LLC") if company_currency == party_account_currency: exchange_rate = 1 else: exchange_rate = get_exchange_rate(party_account_currency, company_currency) # make supplier quotations if random.random() < 0.2: from erpnext.stock.doctype.material_request.material_request import make_supplier_quotation report = "Material Requests for which Supplier Quotations are not created" for row in query_report.run(report)["result"][:random.randint(1, 3)]: if row[0] != "'Total'": sq = frappe.get_doc(make_supplier_quotation(row[0])) sq.transaction_date = frappe.flags.current_date sq.supplier = supplier sq.currency = party_account_currency or company_currency sq.conversion_rate = exchange_rate sq.insert() sq.submit() frappe.db.commit() # make purchase orders if random.random() < 0.5: from erpnext.stock.doctype.material_request.material_request import make_purchase_order report = "Requested Items To Be Ordered" for row in query_report.run(report)["result"][:how_many("Purchase Order")]: if row[0] != "'Total'": po = frappe.get_doc(make_purchase_order(row[0])) po.supplier = supplier po.currency = party_account_currency or company_currency po.conversion_rate = exchange_rate po.transaction_date = frappe.flags.current_date po.insert() po.submit() frappe.db.commit() if random.random() < 0.2: make_subcontract()
def run_purchase(current_date): if can_make("Material Request"): report = "Items To Be Requested" for row in query_report.run(report)["result"][:how_many("Material Request")]: mr = make_material_request(current_date, row[0], -row[-1]) if mr and can_make("Request for Quotation"): rfq = make_request_for_quotation(mr.name) rfq.transaction_date = current_date rfq.status = "Draft" rfq.company = 'Wind Power LLC' add_suppliers(rfq) rfq.message_for_supplier = 'Please supply the specified items at the best possible rates.' rfq.save() rfq.submit() # Make suppier quotation from RFQ against each supplier. for supplier in rfq.suppliers: supplier_quotation = make_quotation_from_rfq(rfq.name, supplier.supplier) supplier_quotation.save() supplier_quotation.submit() # get supplier details supplier = get_random("Supplier") company_currency = frappe.db.get_value("Company", "Wind Power LLC", "default_currency") party_account_currency = get_party_account_currency("Supplier", supplier, "Wind Power LLC") if company_currency == party_account_currency: exchange_rate = 1 else: exchange_rate = get_exchange_rate(party_account_currency, company_currency) # make supplier quotations if can_make("Supplier Quotation"): from erpnext.stock.doctype.material_request.material_request import make_supplier_quotation report = "Material Requests for which Supplier Quotations are not created" for row in query_report.run(report)["result"][:how_many("Supplier Quotation")]: if row[0] != "'Total'": sq = frappe.get_doc(make_supplier_quotation(row[0])) sq.transaction_date = current_date sq.supplier = supplier sq.currency = party_account_currency or company_currency sq.conversion_rate = exchange_rate sq.insert() sq.submit() frappe.db.commit() # make purchase orders if can_make("Purchase Order"): from erpnext.stock.doctype.material_request.material_request import make_purchase_order report = "Requested Items To Be Ordered" for row in query_report.run(report)["result"][:how_many("Purchase Order")]: if row[0] != "'Total'": po = frappe.get_doc(make_purchase_order(row[0])) po.supplier = supplier po.currency = party_account_currency or company_currency po.conversion_rate = exchange_rate po.transaction_date = current_date po.insert() po.submit() frappe.db.commit() if can_make("Subcontract"): make_subcontract(current_date)
def get_data(filters, show_party_name, party_name_field='name', parties=None): company_currency = frappe.get_cached_value('Company', filters.company, "default_currency") opening_balances = get_opening_balances(filters) balances_within_period = get_balances_within_period(filters) data = [] # total_debit, total_credit = 0, 0 total_row = frappe._dict({ "opening_debit": 0, "opening_credit": 0, "debit": 0, "credit": 0, "closing_debit": 0, "closing_credit": 0 }) for party in parties: row = {"party_type": filters.get('party_type'), "party": party.name} # if show_party_name: row["party_name"] = party.get(party_name_field) row["party_currency"] = get_party_account_currency( filters.get('party_type'), party.name, filters.get('company')) # opening if filters.group_by_account: opening_debit, opening_credit, account = opening_balances.get( party.name, [0, 0, '']) else: opening_debit, opening_credit = opening_balances.get( party.name, [0, 0]) row.update({ "opening_debit": opening_debit, "opening_credit": opening_credit }) # within period debit, credit = balances_within_period.get(party.name, [0, 0]) row.update({"debit": debit, "credit": credit}) # closing closing_debit, closing_credit = toggle_debit_credit( opening_debit + debit, opening_credit + credit) row.update({ "closing_debit": closing_debit, "closing_credit": closing_credit }) # totals for col in total_row: total_row[col] += row.get(col) row.update({ "currency": get_party_account_currency(filters.get('party_type'), party.name, filters.get('company')), "party_type_link": filters.get('party_type') }) if filters.group_by_account: row.update({"account": account}) has_value = False if (opening_debit or opening_credit or debit or credit or closing_debit or closing_credit): has_value = True if cint(filters.show_zero_values) or has_value: data.append(row) # Add total row ##total_row.update({ ## "party": "'" + _("Totals") + "'", ## "currency": company_currency ##}) ##data.append(total_row) return data
def postprocess(source, target_doc): target_doc.supplier = for_supplier args = get_party_details(for_supplier, party_type="Supplier", ignore_permissions=True) target_doc.currency = args.currency or get_party_account_currency('Supplier', for_supplier, source.company) target_doc.buying_price_list = args.buying_price_list or frappe.db.get_value('Buying Settings', None, 'buying_price_list') set_missing_values(source, target_doc)
def make_quotation(domain): # get open opportunites opportunity = get_random("Opportunity", { "status": "Open", "with_items": 1 }) if opportunity: from erpnext.crm.doctype.opportunity.opportunity import make_quotation qtn = frappe.get_doc(make_quotation(opportunity)) qtn.insert() frappe.db.commit() qtn.submit() frappe.db.commit() else: # make new directly # get customer, currency and exchange_rate customer = get_random("Customer") company_currency = frappe.get_cached_value( "Company", erpnext.get_default_company(), "default_currency") party_account_currency = get_party_account_currency( "Customer", customer, erpnext.get_default_company()) if company_currency == party_account_currency: exchange_rate = 1 else: exchange_rate = get_exchange_rate(party_account_currency, company_currency, args="for_selling") qtn = frappe.get_doc({ "creation": frappe.flags.current_date, "doctype": "Quotation", "quotation_to": "Customer", "party_name": customer, "currency": party_account_currency or company_currency, "conversion_rate": exchange_rate, "order_type": "Sales", "transaction_date": frappe.flags.current_date, }) add_random_children( qtn, "items", rows=3, randomize={ "qty": (1, 5), "item_code": ("Item", { "has_variants": "0", "is_fixed_asset": 0, "domain": domain }), }, unique="item_code", ) qtn.insert() frappe.db.commit() qtn.submit() frappe.db.commit()
def get_dashboard_info(party_type, party, loyalty_program=None): current_fiscal_year = get_fiscal_year(nowdate(), as_dict=True) doctype = "Sales Invoice" if party_type == "Customer" else "Purchase Invoice" companies = frappe.get_all(doctype, filters={ 'docstatus': 1, 'is_opening': 'No', party_type.lower(): party }, distinct=1, fields=['company']) company_wise_info = [] company_wise_grand_total = frappe.get_all( doctype, filters={ 'docstatus': 1, party_type.lower(): party, 'is_opening': 'No', 'posting_date': ('between', [ current_fiscal_year.year_start_date, current_fiscal_year.year_end_date ]) }, group_by="company", fields=[ "company", "sum(grand_total) as grand_total", "sum(base_grand_total) as base_grand_total" ]) loyalty_point_details = [] if party_type == "Customer": loyalty_point_details = frappe._dict( frappe.get_all( "Loyalty Point Entry", filters={ 'customer': party, 'expiry_date': ('>=', getdate()), }, group_by="company", fields=["company", "sum(loyalty_points) as loyalty_points"], as_list=1)) company_wise_billing_this_year = frappe._dict() for d in company_wise_grand_total: company_wise_billing_this_year.setdefault( d.company, { "grand_total": d.grand_total, "base_grand_total": d.base_grand_total }) company_wise_total_unpaid = frappe._dict( frappe.db.sql( """ select company, sum(debit_in_account_currency) - sum(credit_in_account_currency) from `tabGL Entry` where party_type = %s and party=%s group by company""", (party_type, party))) for d in companies: print(d.name) company_default_currency = frappe.db.get_value("Company", d.company, 'default_currency') party_account_currency = get_party_account_currency( party_type, party, d.company) if party_account_currency == company_default_currency: billing_this_year = flt( company_wise_billing_this_year.get(d.company, {}).get("base_grand_total")) else: billing_this_year = flt( company_wise_billing_this_year.get(d.company, {}).get("grand_total")) total_unpaid = flt(company_wise_total_unpaid.get(d.company)) if loyalty_point_details: loyalty_points = loyalty_point_details.get(d.company) info = {} info["billing_this_year"] = flt( billing_this_year) if billing_this_year else 0 info["currency"] = party_account_currency info["total_unpaid"] = flt(total_unpaid) if total_unpaid else 0 info["company"] = d.company if party_type == "Customer" and loyalty_point_details: info["loyalty_points"] = loyalty_points if party_type == "Supplier": info["total_unpaid"] = -1 * info["total_unpaid"] company_wise_info.append(info) return company_wise_info