def make_data(rows): return { "no_of_sales": len(rows), "total_qty": sum_by("total_qty", rows), "total": sum_by("total", rows), "taxes_and_charges": sum_by("total_taxes_and_charges", rows), "discount_amount": sum_by("discount_amount", rows), "grand_total": sum_by("grand_total", rows), }
def fn(row_dict): row = frappe._dict(row_dict) net_amount = row.net_amount or 0 item_net = item_map.get(row.invoice, {}).get(row.item_code, 0) tax_weight = net_amount / item_net if item_net else 0 tax = sum_by(row.item_code, tax_map.get(row.invoice, [])) * tax_weight return merge(row_dict, {"tax": tax, "total": net_amount + tax})
def make_payment(payment): mop_conversion_rate = ( payment.amount / payment.mop_amount if payment.mop_amount else 1 ) expected_amount = ( payment.amount - sum_by("change_amount", sales) if payment.is_default else (payment.mop_amount or payment.amount) ) return merge( pick(["is_default", "mode_of_payment", "type"], payment), { "mop_conversion_rate": mop_conversion_rate, "collected_amount": expected_amount, "expected_amount": expected_amount, "difference_amount": 0, "mop_currency": payment.mop_currency or frappe.defaults.get_global_default("currency"), "base_collected_amount": expected_amount * flt(mop_conversion_rate), }, )
def set_report_details(self): args = merge( pick(["user", "pos_profile", "company"], self.as_dict()), { "period_from": self.period_from or now(), "period_to": self.period_to or now(), }, ) sales, returns = _get_invoices(args) actual_payments = _get_payments(args) taxes = _get_taxes(args) def make_invoice(invoice): return merge( pick(["grand_total", "paid_amount", "change_amount"], invoice), { "invoice": invoice.name, "total_qty": invoice.pos_total_qty }, ) def make_payment(payment): mop_conversion_rate = (payment.amount / payment.mop_amount if payment.mop_amount else 1) expected_amount = (payment.amount - sum_by("change_amount", sales) if payment.is_default else (payment.mop_amount or payment.amount)) return merge( pick(["is_default", "mode_of_payment", "type"], payment), { "mop_conversion_rate": mop_conversion_rate, "collected_amount": expected_amount, "expected_amount": expected_amount, "difference_amount": 0, "mop_currency": payment.mop_currency or frappe.defaults.get_global_default("currency"), "base_collected_amount": expected_amount * flt(mop_conversion_rate), }, ) make_tax = partial(pick, ["rate", "tax_amount"]) self.returns_total = sum_by("grand_total", returns) self.returns_net_total = sum_by("net_total", returns) self.grand_total = sum_by("grand_total", sales + returns) self.net_total = sum_by("net_total", sales + returns) self.outstanding_total = sum_by("outstanding_amount", sales) self.total_invoices = len(sales + returns) self.average_sales = sum_by("net_total", sales) / len(sales) if sales else 0 self.total_quantity = sum_by("pos_total_qty", sales) self.returns_quantity = -sum_by("pos_total_qty", returns) self.tax_total = sum_by("tax_amount", taxes) self.discount_total = sum_by("discount_amount", sales) self.change_total = sum_by("change_amount", sales) self.invoices = [] for invoice in sales: self.append("invoices", make_invoice(invoice)) self.returns = [] for invoice in returns: self.append("returns", make_invoice(invoice)) existing_payments = self.payments def get_form_collected(mop): existing = compose( excepts(StopIteration, first, lambda x: None), partial(filter, lambda x: x.mode_of_payment == mop), )(existing_payments) if not existing or existing.collected_amount == existing.expected_amount: return {} return {"collected_amount": existing.collected_amount} self.payments = [] for payment in actual_payments: self.append( "payments", merge(make_payment(payment), get_form_collected(payment.mode_of_payment)), ) self.taxes = [] for tax in taxes: self.append("taxes", make_tax(tax))
def set_missing_fields(self): for item in self.items: item.amount = flt(item.qty) * flt(item.basic_rate) item.valuation_rate = item.amount / flt(item.qty) self.total_value = sum_by("amount")(self.items) self.total_qty = sum_by("qty")(self.items)
def set_report_details(self): args = merge( pick(["user", "pos_profile", "company"], self.as_dict()), { "period_from": get_datetime(self.period_from), "period_to": get_datetime(self.period_to), }, ) sales, returns = _get_invoices(args) actual_payments, collection_payments = _get_payments(args) taxes = _get_taxes(args) def make_invoice(invoice): return merge( pick(["grand_total", "paid_amount", "change_amount"], invoice), { "invoice": invoice.name, "total_quantity": invoice.pos_total_qty, "sales_employee": invoice.pb_sales_employee, }, ) def make_payment(payment): mop_conversion_rate = (payment.amount / payment.mop_amount if payment.mop_amount else 1) expected_amount = (payment.amount - sum_by("change_amount", sales) if payment.is_default else (payment.mop_amount or payment.amount)) return merge( pick(["is_default", "mode_of_payment", "type"], payment), { "mop_conversion_rate": mop_conversion_rate, "collected_amount": expected_amount, "expected_amount": expected_amount, "difference_amount": 0, "mop_currency": payment.mop_currency or frappe.defaults.get_global_default("currency"), "base_collected_amount": expected_amount * flt(mop_conversion_rate), }, ) make_tax = partial(pick, ["rate", "tax_amount"]) get_employees = partial( pick, ["pb_sales_employee", "pb_sales_employee_name", "grand_total"]) self.returns_total = sum_by("grand_total", returns) self.returns_net_total = sum_by("net_total", returns) self.grand_total = sum_by("grand_total", sales + returns) self.net_total = sum_by("net_total", sales + returns) self.outstanding_total = sum_by("outstanding_amount", sales) self.total_invoices = len(sales + returns) self.average_sales = sum_by("net_total", sales) / len(sales) if sales else 0 self.total_quantity = sum_by("pos_total_qty", sales) self.returns_quantity = -sum_by("pos_total_qty", returns) self.tax_total = sum_by("tax_amount", taxes) self.discount_total = sum_by("discount_amount", sales) self.change_total = sum_by("change_amount", sales) self.total_collected = (sum_by("amount", actual_payments) + sum_by("amount", collection_payments) - self.change_total) self.invoices = [] for invoice in sales: self.append("invoices", make_invoice(invoice)) self.returns = [] for invoice in returns: self.append("returns", make_invoice(invoice)) existing_payments = self.payments def get_form_collected(mop): existing = compose( excepts(StopIteration, first, lambda x: None), partial(filter, lambda x: x.mode_of_payment == mop), )(existing_payments) if not existing or existing.collected_amount == existing.expected_amount: return {} return {"collected_amount": existing.collected_amount} self.payments = [] for payment in actual_payments: self.append( "payments", merge(make_payment(payment), get_form_collected(payment.mode_of_payment)), ) for payment in collection_payments: collected_payment = merge( make_payment(payment), get_form_collected(payment.mode_of_payment)) existing_payment = list( filter( lambda x: x.mode_of_payment == collected_payment[ "mode_of_payment"], self.payments, ))[0] if existing_payment: for field in [ "expected_amount", "collected_amount", "difference_amount", "base_collected_amount", ]: existing_payment.set( field, sum([ existing_payment.get(field), collected_payment.get(field, 0), ]), ) else: self.append("payments", collected_payment) self.taxes = [] for tax in taxes: self.append("taxes", make_tax(tax)) self.employees = [] employee_with_sales = compose(list, partial(map, get_employees))(sales) employees = compose( list, unique, partial(map, lambda x: x["pb_sales_employee"]))(employee_with_sales) for employee in employees: sales_employee_name = compose( first, partial(filter, lambda x: x["pb_sales_employee"] == employee))( employee_with_sales)["pb_sales_employee_name"] sales = compose( list, partial(map, lambda x: x["grand_total"]), partial(filter, lambda x: x["pb_sales_employee"] == employee), )(employee_with_sales) self.append( "employees", { "sales_employee": employee, "sales_employee_name": sales_employee_name, "invoices_count": len(sales), "sales_total": sum(sales), }, ) self.item_groups = [] for row in _get_item_groups(args): self.append("item_groups", row)
def _merge_sum(data): return {x: sum_by(x, data) for x in ["taxable_amount", "vat_amount"]}