def redeem(self, redeemed_value=0): self.check_permission(permtype='write') if self.allow_partial_redemption and 0 < redeemed_value < self.current_value: self.db_set('current_value', self.current_value - redeemed_value) self.add_comment( "Info", _("redeemed {0}, new value is {1}").format( frappe.bold( frappe.format( redeemed_value, dict(fieldtype="Currency", options="currency"))), frappe.bold( frappe.format( self.current_value, dict(fieldtype="Currency", options="currency"))))) elif self.allow_partial_redemption == 0 or redeemed_value == self.current_value: self.db_set('current_value', 0) if not self.redemption_date: self.db_set('redemption_date', today()) else: frappe.throw( _("The redemption value must be between {0} and {1}.").format( frappe.bold( frappe.format( 0, dict(fieldtype="Currency", options="currency"))), frappe.bold( frappe.format( self.current_value, dict(fieldtype="Currency", options="currency"))))) self.set_status(update=True)
def append_vat_on_expenses(data, filters): """Appends Expenses and All Other Inputs.""" append_data(data, '', _('VAT on Expenses and All Other Inputs'), '', '') append_data( data, '9', _('Standard Rated Expenses'), frappe.format(get_standard_rated_expenses_total(filters), 'Currency'), frappe.format(get_standard_rated_expenses_tax(filters), 'Currency')) append_data( data, '10', _('Supplies subject to the reverse charge provision'), frappe.format(get_reverse_charge_recoverable_total(filters), 'Currency'), frappe.format(get_reverse_charge_recoverable_tax(filters), 'Currency'))
def test_currency_formatting(self): df = frappe._dict({"fieldname": "amount", "fieldtype": "Currency", "options": "currency"}) doc = frappe._dict({"amount": 5}) frappe.db.set_default("currency", "INR") # if currency field is not passed then default currency should be used. self.assertEqual(format(100000, df, doc, format="#,###.##"), "₹ 100,000.00") doc.currency = "USD" self.assertEqual(format(100000, df, doc, format="#,###.##"), "$ 100,000.00") frappe.db.set_default("currency", None)
def _add_balance(accounts, company): filters = dict(company=company) # This is hack job, Al doesn't approve. _, p_l = get_report("p&l", filters) _, b_s = get_report("balance_sheet", filters) reports = [*b_s, *p_l] reports = {k["account"]: k["amount"] for k in reports} reports = unformat_reports(reports) for account in accounts: acc = account["value"].split(" - ")[0] if acc in reports: account["balance"] = frappe.format(float(reports[acc]), "Currency") else: account["balance"] = frappe.format(0, "Currency")
def append_emiratewise_expenses(data, emirates, amounts_by_emirate): """Append emiratewise standard rated expenses and vat.""" for no, emirate in enumerate(emirates, 97): if emirate in amounts_by_emirate: amounts_by_emirate[emirate]["no"] = _('1{0}').format(chr(no)) amounts_by_emirate[emirate]["legend"] = _( 'Standard rated supplies in {0}').format(emirate) data.append(amounts_by_emirate[emirate]) else: append_data(data, _('1{0}').format(chr(no)), _('Standard rated supplies in {0}').format(emirate), frappe.format(0, 'Currency'), frappe.format(0, 'Currency')) return amounts_by_emirate
def get_xls(columns, data): '''Convert data to xls''' stream = StringIO.StringIO() workbook = xlwt.Workbook() sheet = workbook.add_sheet(_("Sheet 1")) # formats dateformat = xlwt.easyxf(num_format_str= (frappe.defaults.get_global_default("date_format") or "yyyy-mm-dd")) bold = xlwt.easyxf('font: bold 1') # header for i, col in enumerate(columns): sheet.write(0, i, col.label, bold) for i, row in enumerate(data): for j, df in enumerate(columns): f = None val = row[columns[j].fieldname] if isinstance(val, (datetime.datetime, datetime.date)): f = dateformat if f: sheet.write(i+1, j, val, f) else: sheet.write(i+1, j, frappe.format(val, df, row)) workbook.save(stream) stream.seek(0) return stream.read()
def standard_rated_expenses_emiratewise(data, filters): """Append emiratewise standard rated expenses and vat.""" total_emiratewise = get_total_emiratewise(filters) emirates = get_emirates() amounts_by_emirate = {} for emirate, amount, vat in total_emiratewise: amounts_by_emirate[emirate] = { "legend": emirate, "raw_amount": amount, "raw_vat_amount": vat, "amount": frappe.format(amount, 'Currency'), "vat_amount": frappe.format(vat, 'Currency'), } amounts_by_emirate = append_emiratewise_expenses(data, emirates, amounts_by_emirate) return emirates, amounts_by_emirate
def set_rates(self): self.net_total = 0 for d in self.items: net_rate = 0.0 for i in range(date_diff(self.to_date, self.from_date)): day = add_days(self.from_date, i) if not d.item: continue day_rate = frappe.db.sql( """ select item.rate,item.name from `tabHotel Room Pricing Item` item, `tabHotel Room Pricing` pricing where item.parent = pricing.name and item.item = %s and %s between pricing.from_date and pricing.to_date""", (d.item, day)) if day_rate: net_rate += day_rate[0][0] else: frappe.throw(_( "Item/Package " + d.item + " should have a Pricing rule From date {}, Please set here <a href='desk#List/Hotel Room Pricing/List' target='_blank'>Hotel Room Pricing</a>" ).format(frappe.format(day, dict(fieldtype="Date"))), exc=HotelRoomPricingNotSetError) d.rate = net_rate d.amount = net_rate * flt(d.qty) self.net_total += d.amount
def set_rates(self): self.net_total = 0 for d in self.items: net_rate = 0.0 for i in range(date_diff(self.to_date, self.from_date)): day = add_days(self.from_date, i) if not d.item: continue day_rate = frappe.db.sql(""" select item.rate from `tabHotel Room Pricing Item` item, `tabHotel Room Pricing` pricing where item.parent = pricing.name and item.item = %s and %s between pricing.from_date and pricing.to_date""", (d.item, day)) if day_rate: net_rate += day_rate[0][0] else: frappe.throw( _("Please set Hotel Room Rate on {}".format( frappe.format(day, dict(fieldtype="Date")))), exc=HotelRoomPricingNotSetError) d.rate = net_rate d.amount = net_rate * flt(d.qty) self.net_total += d.amount
def set_rates(self): self.net_total = 0 for d in self.items: net_rate = 0.0 for i in range(date_diff(self.to_date, self.from_date)): day = add_days(self.from_date, i) if not d.item: continue day_rate = frappe.db.sql( """ select item.rate from `tabHotel Room Pricing Item` item, `tabHotel Room Pricing` pricing where item.parent = pricing.name and item.item = %s and %s between pricing.from_date and pricing.to_date""", (d.item, day)) if day_rate: net_rate += day_rate[0][0] else: frappe.throw(_("Please set Hotel Room Rate on {}".format( frappe.format(day, dict(fieldtype="Date")))), exc=HotelRoomPricingNotSetError) d.rate = net_rate d.amount = net_rate * flt(d.qty) self.net_total += d.amount
def test_currency_formatting(self): df = frappe._dict({ 'fieldname': 'amount', 'fieldtype': 'Currency', 'options': 'currency' }) doc = frappe._dict({'amount': 5}) frappe.db.set_default("currency", 'INR') # if currency field is not passed then default currency should be used. self.assertEqual(format(100, df, doc), '100.00 ₹') doc.currency = 'USD' self.assertEqual(format(100, df, doc), "100.00 $") frappe.db.set_default("currency", None)
def get_spreadsheet_data(columns, data): out = [[_(df.label) for df in columns], ] for row in data: new_row = [] out.append(new_row) for df in columns: new_row.append(frappe.format(row[df.fieldname], df, row)) return out
def get_csv(self, columns, data): out = [[df.label for df in columns], ] for row in data: new_row = [] out.append(new_row) for df in columns: new_row.append(frappe.format(row[df.fieldname], df, row)) return to_csv(out)
def append_vat_on_expenses(data, filters): """Appends Expenses and All Other Inputs.""" append_data(data, "", _("VAT on Expenses and All Other Inputs"), "", "") append_data( data, "9", _("Standard Rated Expenses"), frappe.format(get_standard_rated_expenses_total(filters), "Currency"), frappe.format(get_standard_rated_expenses_tax(filters), "Currency"), ) append_data( data, "10", _("Supplies subject to the reverse charge provision"), frappe.format(get_reverse_charge_recoverable_total(filters), "Currency"), frappe.format(get_reverse_charge_recoverable_tax(filters), "Currency"), )
def add_data_row(an, tree, data, i): balance, _, children, _ = tree[an] is_group = 0 if not children else 1 account = an if i == 0: account = frappe.bold(account) amount = frappe.format(balance, "Currency") data.append(get_data_row(account, amount, i, is_group)) if is_group: for child in children: add_data_row(child, tree, data, i + 1)
def validate(self): max_benefits = get_max_benefits(self.employee, self.claim_date) if not max_benefits or max_benefits <= 0: frappe.throw(_("Employee {0} has no maximum benefit amount").format(self.employee)) payroll_period = get_payroll_period(self.claim_date, self.claim_date, frappe.db.get_value("Employee", self.employee, "company")) if not payroll_period: frappe.throw(_("{0} is not in a valid Payroll Period").format(frappe.format(self.claim_date, dict(fieldtype='Date')))) self.validate_max_benefit_for_component(payroll_period) self.validate_max_benefit_for_sal_struct(max_benefits) self.validate_benefit_claim_amount(max_benefits, payroll_period) if self.pay_against_benefit_claim: self.validate_non_pro_rata_benefit_claim(max_benefits, payroll_period)
def append_vat_on_sales(data, filters): """Appends Sales and All Other Outputs.""" append_data(data, "", _("VAT on Sales and All Other Outputs"), "", "") emirates, amounts_by_emirate = standard_rated_expenses_emiratewise( data, filters) append_data( data, "2", _("Tax Refunds provided to Tourists under the Tax Refunds for Tourists Scheme" ), frappe.format((-1) * get_tourist_tax_return_total(filters), "Currency"), frappe.format((-1) * get_tourist_tax_return_tax(filters), "Currency"), ) append_data( data, "3", _("Supplies subject to the reverse charge provision"), frappe.format(get_reverse_charge_total(filters), "Currency"), frappe.format(get_reverse_charge_tax(filters), "Currency"), ) append_data(data, "4", _("Zero Rated"), frappe.format(get_zero_rated_total(filters), "Currency"), "-") append_data(data, "5", _("Exempt Supplies"), frappe.format(get_exempt_total(filters), "Currency"), "-") append_data(data, "", "", "", "") return emirates, amounts_by_emirate
def append_vat_on_sales(data, filters): """Appends Sales and All Other Outputs.""" append_data(data, '', _('VAT on Sales and All Other Outputs'), '', '') emirates, amounts_by_emirate = standard_rated_expenses_emiratewise( data, filters) append_data( data, '2', _('Tax Refunds provided to Tourists under the Tax Refunds for Tourists Scheme' ), frappe.format((-1) * get_tourist_tax_return_total(filters), 'Currency'), frappe.format((-1) * get_tourist_tax_return_tax(filters), 'Currency')) append_data(data, '3', _('Supplies subject to the reverse charge provision'), frappe.format(get_reverse_charge_total(filters), 'Currency'), frappe.format(get_reverse_charge_tax(filters), 'Currency')) append_data(data, '4', _('Zero Rated'), frappe.format(get_zero_rated_total(filters), 'Currency'), "-") append_data(data, '5', _('Exempt Supplies'), frappe.format(get_exempt_total(filters), 'Currency'), "-") append_data(data, '', '', '', '') return emirates, amounts_by_emirate
def get_preview_data(doctype, docname): preview_fields = [] meta = frappe.get_meta(doctype) if not meta.show_preview_popup: return preview_fields = [ field.fieldname for field in meta.fields if field.in_preview and field.fieldtype not in no_value_fields and field.fieldtype not in table_fields ] # no preview fields defined, build list from mandatory fields if not preview_fields: preview_fields = [ field.fieldname for field in meta.fields if field.reqd and field.fieldtype not in table_fields ] title_field = meta.get_title_field() image_field = meta.image_field preview_fields.append(title_field) preview_fields.append(image_field) preview_fields.append("name") preview_data = frappe.get_list(doctype, filters={"name": docname}, fields=preview_fields, limit=1) if not preview_data: return preview_data = preview_data[0] formatted_preview_data = { "preview_image": preview_data.get(image_field), "preview_title": preview_data.get(title_field), "name": preview_data.get("name"), } for key, val in preview_data.items(): if val and meta.has_field(key) and key not in [image_field, title_field, "name"]: formatted_preview_data[meta.get_field(key).label] = frappe.format( val, meta.get_field(key).fieldtype, translated=True, ) return formatted_preview_data
def validate_availability(self): for i in range(date_diff(self.to_date, self.from_date)): day = add_days(self.from_date, i) self.rooms_booked = {} for d in self.items: if not d.item in self.rooms_booked: self.rooms_booked[d.item] = 0 room_type = frappe.db.get_value("Hotel Room Package", d.item, 'hotel_room_type') rooms_booked = get_rooms_booked(room_type, day, exclude_reservation=self.name) \ + d.qty + self.rooms_booked.get(d.item) total_rooms = self.get_total_rooms(d.item) if total_rooms < rooms_booked: frappe.throw(_("Hotel Rooms of type {0} are unavailable on {1}").format(d.item, frappe.format(day, dict(fieldtype="Date"))), exc=HotelRoomUnavailableError) self.rooms_booked[d.item] += rooms_booked
def validate_availability(self): for i in range(date_diff(self.to_date, self.from_date)): day = add_days(self.from_date, i) self.rooms_booked = {} for d in self.items: if not d.item in self.rooms_booked: self.rooms_booked[d.item] = 0 room_type = frappe.db.get_value("Hotel Room Package", d.item, 'hotel_room_type') rooms_booked = get_rooms_booked(room_type, day, exclude_reservation=self.name) \ + d.qty + self.rooms_booked.get(d.item) total_rooms = self.get_total_rooms(d.item) if total_rooms < rooms_booked: frappe.throw(_("Hotel Rooms of type {0} are unavailable on {1}".format(d.item, frappe.format(day, dict(fieldtype="Date")))), exc=HotelRoomUnavailableError) self.rooms_booked[d.item] += rooms_booked
def get_preview_data(doctype, docname, fields): fields = json.loads(fields) preview_fields = [field['name'] for field in fields if field['type'] not in no_value_fields] preview_fields.append(frappe.get_meta(doctype).get_title_field()) if 'name' not in fields: preview_fields.append('name') preview_fields.append(frappe.get_meta(doctype).image_field) preview_data = frappe.get_list(doctype, filters={ 'name': docname }, fields=preview_fields, limit=1) if preview_data: preview_data = preview_data[0] preview_data = {k: v for k, v in preview_data.items() if v is not None} for k,v in preview_data.items(): if frappe.get_meta(doctype).has_field(k): preview_data[k] = frappe.format(v,frappe.get_meta(doctype).get_field(k).fieldtype) if not preview_data: return None return preview_data
def get_tax_fieldname(description, rate): return "tax_{0}_{1}".format(scrub(description), scrub(frappe.format(flt(rate))))
def format_currency(cur, amt): bal = cur + " " bal += frappe.format(amt, "Currency") return bal
def append_total_row(i_c, totals, data): label = frappe.bold(f'Total ({"Credit" if i_c else "Debit"})') amount = frappe.format(totals[i_c], "Currency") row = get_data_row(label, amount, 0, 0) data.append(row)
def get_tax_label(description, rate): if rate: return "{0} ({1}%)".format(description, frappe.format(flt(rate))) else: return description
'edit_report_settings': frappe.utils.get_link_to_form('Auto Email Report', self.name) }) @staticmethod def get_spreadsheet_data(columns, data): <<<<<<< HEAD out = [[df.label for df in columns], ] ======= out = [[_(df.label) for df in columns], ] >>>>>>> 176d241496ede1357a309fa44a037b757a252581 for row in data: new_row = [] out.append(new_row) for df in columns: new_row.append(frappe.format(row[df.fieldname], df, row)) return out def get_file_name(self): return "{0}.{1}".format(self.report.replace(" ", "-").replace("/", "-"), self.format.lower()) def send(self): if self.filter_meta and not self.filters: frappe.throw(_("Please set filters value in Report Filter table.")) data = self.get_report_content() if not data: return attachments = None