def make_gl_entry(si): amt = cint(si.net_total_export) - cint(si.redeem_points) create_jv(si, amt, frappe.db.get_value('Company', si.company, 'default_income_account'), si.debit_to, adj_outstanding=True)
def update_cheque_lot(ref_doc): if ref_doc: current = ref_doc.next_no if cint(current) < cint(ref_doc.end_no): ref_doc.db_set("next_no", str((cint(current) + 1)).zfill(len(ref_doc.next_no))) ref_doc.db_set("status", "In Use") else: ref_doc.db_set("status", "Used")
def update_cheque_lot(ref_doc): if ref_doc: current = ref_doc.next_no if cint(current) < cint(ref_doc.end_no): ref_doc.db_set( "next_no", str((cint(current) + 1)).zfill(len(ref_doc.next_no))) ref_doc.db_set("status", "In Use") else: ref_doc.db_set("status", "Used")
def create_point_transaction(ref_link, ref_name, inv, tran_type, points, rule_details=None): if points != 0: tran = frappe.new_doc("Point Transaction") tran.ref_link = ref_link tran.ref_name = ref_name tran.date = today() tran.type = tran_type tran.points = cint(points) * 1 if tran_type == 'Earned' else -1 * cint(points) if rule_details: tran.valied_upto = add_months(nowdate(), cint(rule_details.get('valid_upto'))) tran.invoice_number = inv tran.rule_details = cstr(rule_details) tran.docstatus = 1 tran.insert()
def sync_plant(data): barcode = data.get("id") name = frappe.db.sql_list("select name from tabPlant where name=%(barcode)s or bio_barcode=%(barcode)s", {"barcode": barcode}) if name: name = name.pop() doc = frappe.get_doc("Plant", name) if doc.get("bio_transaction_id") == data.get("transactionid"): frappe.db.set_value("Plant", name, "bio_last_sync", now(), update_modified=False) return False else: sessiontime = datetime.datetime.fromtimestamp(cint(data.get("sessiontime"))) doc = frappe.get_doc({ "__islocal": 1, "doctype": "Plant", "bio_barcode": barcode, "posting_date": sessiontime.strftime(DATE_FORMAT), "posting_time": sessiontime.strftime(TIME_FORMAT) }) plant_room = frappe.get_doc("Plant Room", {"bio_id": data.get("room")}) doc.update({ "strain": find_strain(data.get("strain")), "plant_room": plant_room.get("name") if plant_room else "", "is_mother_plant": cint(data.get("mother")), "destroy_scheduled": cint(data.get("removescheduled")), "harvest_collect": cint(data.get("harvestcollect")), "cure_collect": cint(data.get("curecollect")), "bio_transaction_id": cint(data.get("transactionid")), "bio_last_sync": now(), "disabled": 0, }) item_values = get_item_values(data.get("parentid"), ["name", "item_group"]) if item_values: item, item_group = item_values doc.item = item doc.item_group = item_group if doc.get("destroy_scheduled") and data.get("removescheduletime"): doc.remove_time = datetime.datetime.fromtimestamp(cint(data.get("removescheduletime"))).strftime(DATETIME_FORMAT) if data.get("removereason"): doc.remove_reason = data.get("removereason") state = cint(data.get("state")) doc.state = "Drying" if state == 1 else ("Cured" if state == 2 else "Growing") doc.flags.in_import = True doc.flags.ignore_validate_update_after_submit = True doc.flags.ignore_mandatory = True if doc.is_new(): doc.submit() else: doc.save() frappe.db.commit()
def validate_einvoice_fields(doc): einvoicing_enabled = cint( frappe.db.get_value('E Invoice Settings', 'E Invoice Settings', 'enable')) invalid_doctype = doc.doctype != 'Sales Invoice' invalid_supply_type = doc.get('gst_category') not in [ 'Registered Regular', 'SEZ', 'Overseas', 'Deemed Export' ] company_transaction = doc.get('billing_address_gstin') == doc.get( 'company_gstin') no_taxes_applied = not doc.get('taxes') if not einvoicing_enabled or invalid_doctype or invalid_supply_type or company_transaction or no_taxes_applied: return if doc.docstatus == 0 and doc._action == 'save': if doc.irn: frappe.throw(_('You cannot edit the invoice after generating IRN'), title=_('Edit Not Allowed')) if len(doc.name) > 16: raise_document_name_too_long_error() elif doc.docstatus == 1 and doc._action == 'submit' and not doc.irn: frappe.throw( _('You must generate IRN before submitting the document.'), title=_('Missing IRN')) elif doc.irn and doc.docstatus == 2 and doc._action == 'cancel' and not doc.irn_cancelled: frappe.throw(_('You must cancel IRN before cancelling the document.'), title=_('Cancel Not Allowed'))
def before_submit(plant_entry): """BioTrack sync up: inventory_new or inventory_adjust""" barcodeid = [] for ple_detail in plant_entry.get("plants"): plant = frappe.get_doc("Plant", ple_detail.plant_code) if is_bio_plant(plant): barcodeid.append(plant.get("bio_barcode")) if len(barcodeid) == 0: return if plant_entry.purpose == "Convert": convert_on_submit(plant_entry, barcodeid) return action = "plant_harvest" if plant_entry.purpose == "Harvest" else "plant_cure" res = None try: res = call(action, { "barcodeid": barcodeid, "location": get_location(), "weights": make_weights_data(plant_entry.flower, plant_entry.other_material, plant_entry.waste), "collectadditional": cint(plant_entry.additional_collections), }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") if res: plant_entry.bio_transaction = res.get("transactionid") items = plant_entry.items or {} map_item_derivatives(items, res.get("derivatives", []))
def set_current_invoice_end(self): """ This sets the date of the end of the current billing period. If the subscription is in trial period, it will be set as the end of the trial period. If is not in a trial period, it will be `x` days from the beginning of the current billing period where `x` is the billing interval from the `Subscription Plan` in the `Subscription`. """ if self.is_trialling() and getdate( self.current_invoice_start) < getdate(self.trial_period_end): self.current_invoice_end = self.trial_period_end else: billing_cycle_info = self.get_billing_cycle_data() if billing_cycle_info: if self.is_new_subscription() and getdate( self.start_date) < getdate(self.current_invoice_start): self.current_invoice_end = add_to_date( self.start_date, **billing_cycle_info) # For cases where trial period is for an entire billing interval if getdate(self.current_invoice_end) < getdate( self.current_invoice_start): self.current_invoice_end = add_to_date( self.current_invoice_start, **billing_cycle_info) else: self.current_invoice_end = add_to_date( self.current_invoice_start, **billing_cycle_info) else: self.current_invoice_end = get_last_day( self.current_invoice_start) if self.follow_calendar_months: billing_info = self.get_billing_cycle_and_interval() billing_interval_count = billing_info[0][ 'billing_interval_count'] calendar_months = get_calendar_months(billing_interval_count) calendar_month = 0 current_invoice_end_month = getdate( self.current_invoice_end).month current_invoice_end_year = getdate( self.current_invoice_end).year for month in calendar_months: if month <= current_invoice_end_month: calendar_month = month if cint(calendar_month - billing_interval_count) <= 0 and \ getdate(self.current_invoice_start).month != 1: calendar_month = 12 current_invoice_end_year -= 1 self.current_invoice_end = get_last_day(cstr(current_invoice_end_year) + '-' \ + cstr(calendar_month) + '-01') if self.end_date and getdate(self.current_invoice_end) > getdate( self.end_date): self.current_invoice_end = self.end_date
def santize_einvoice_fields(einvoice): int_fields = ["Pin","Distance","CrDay"] float_fields = ["Qty","FreeQty","UnitPrice","TotAmt","Discount","PreTaxVal","AssAmt","GstRt","IgstAmt","CgstAmt","SgstAmt","CesRt","CesAmt","CesNonAdvlAmt","StateCesRt","StateCesAmt","StateCesNonAdvlAmt","OthChrg","TotItemVal","AssVal","CgstVal","SgstVal","IgstVal","CesVal","StCesVal","Discount","OthChrg","RndOffAmt","TotInvVal","TotInvValFc","PaidAmt","PaymtDue","ExpDuty",] copy = einvoice.copy() for key, value in copy.items(): if isinstance(value, list): for idx, d in enumerate(value): santized_dict = santize_einvoice_fields(d) if santized_dict: einvoice[key][idx] = santized_dict else: einvoice[key].pop(idx) if not einvoice[key]: einvoice.pop(key, None) elif isinstance(value, dict): santized_dict = santize_einvoice_fields(value) if santized_dict: einvoice[key] = santized_dict else: einvoice.pop(key, None) elif not value or value == "None": einvoice.pop(key, None) elif key in float_fields: einvoice[key] = flt(value, 2) elif key in int_fields: einvoice[key] = cint(value) return einvoice
def validate_eligibility(doc): if isinstance(doc, six.string_types): doc = json.loads(doc) invalid_doctype = doc.get('doctype') != 'Sales Invoice' if invalid_doctype: return False einvoicing_enabled = cint(frappe.db.get_single_value('E Invoice Settings', 'enable')) if not einvoicing_enabled: return False einvoicing_eligible_from = frappe.db.get_single_value('E Invoice Settings', 'applicable_from') or '2021-04-01' if getdate(doc.get('posting_date')) < getdate(einvoicing_eligible_from): return False invalid_company = not frappe.db.get_value('E Invoice User', { 'company': doc.get('company') }) invalid_supply_type = doc.get('gst_category') not in ['Registered Regular', 'SEZ', 'Overseas', 'Deemed Export'] company_transaction = doc.get('billing_address_gstin') == doc.get('company_gstin') # if export invoice, then taxes can be empty # invoice can only be ineligible if no taxes applied and is not an export invoice no_taxes_applied = not doc.get('taxes') and not doc.get('gst_category') == 'Overseas' has_non_gst_item = any(d for d in doc.get('items', []) if d.get('is_non_gst')) if invalid_company or invalid_supply_type or company_transaction or no_taxes_applied or has_non_gst_item: return False return True
def bulk_clone(name): source_plant = frappe.get_doc("Plant", name) if source_plant.qty > 1: warehouse = frappe.get_doc("Warehouse", source_plant.get("warehouse")) location = frappe.get_value("BioTrack Settings", None, "location") remaining_qty = source_plant.qty - 1 result = biotrackthc_call("plant_new", { "room": warehouse.external_id, "quantity": remaining_qty, "strain": source_plant.strain, "source": source_plant.item_code, "mother": cint(source_plant.get("is_mother")), "location": location }) for barcode in result.get("barcode_id"): plant = frappe.new_doc("Plant") plant.update({ "barcode": barcode, "item_group": source_plant.item_group, "source": source_plant.item_code, "strain": source_plant.strain, "warehouse": source_plant.warehouse, "state": source_plant.state, "birthdate": now(), }) plant.save() # save directly with sql to avoid mistimestamp check frappe.db.set_value("Plant", source_plant.name, "qty", 1, update_modified=False) frappe.publish_realtime("list_update", {"doctype": "Plant"})
def validate_eligibility(doc): if isinstance(doc, six.string_types): doc = json.loads(doc) invalid_doctype = doc.get('doctype') != 'Sales Invoice' if invalid_doctype: return False einvoicing_enabled = cint(frappe.db.get_single_value('E Invoice Settings', 'enable')) if not einvoicing_enabled: return False einvoicing_eligible_from = frappe.db.get_single_value('E Invoice Settings', 'applicable_from') or '2021-04-01' if getdate(doc.get('posting_date')) < getdate(einvoicing_eligible_from): return False invalid_company = not frappe.db.get_value('E Invoice User', { 'company': doc.get('company') }) invalid_supply_type = doc.get('gst_category') not in ['Registered Regular', 'SEZ', 'Overseas', 'Deemed Export'] company_transaction = doc.get('billing_address_gstin') == doc.get('company_gstin') no_taxes_applied = not doc.get('taxes') if invalid_company or invalid_supply_type or company_transaction or no_taxes_applied: return False return True
def create_invoice(self, prorate): """ Creates a `Sales Invoice`, submits it and returns it """ invoice = frappe.new_doc('Sales Invoice') invoice.set_posting_time = 1 invoice.posting_date = self.current_invoice_start invoice.customer = self.customer ## Add dimesnions 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: invoice.append('items', item) # Taxes if self.tax_template: invoice.taxes_and_charges = self.tax_template invoice.set_taxes() # Due date if self.days_until_due: invoice.append( 'payment_schedule', { 'due_date': add_days(self.current_invoice_end, 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.save() invoice.submit() return invoice
def get_marketing_account(company): abbr = frappe.db.get_value("Company", company, "abbr") if cint(frappe.db.sql("""select if( exists(select * from tabAccount where name = '%s'), 1, 0) """%"Marketing Expenses - %s"%abbr, as_list=1)[0][0]): return "Marketing Expenses - %s"%abbr else: frappe.throw(_("Marketing Account not exist. Please create Marketing Account under Indirect Expenses Account"))
def add_primacasa_items(): from frappe.utils.csvutils import read_csv_content from frappe.core.doctype.data_import.importer import upload with open( "/home/frappe/frappe-bench/apps/erpnext/erpnext/primacasa_items.csv", "r") as infile: rows = read_csv_content(infile.read()) i = 0 for index, row in enumerate(rows): if index: print(index) item = frappe.new_doc('Item') item.company = 'Primacasa' item.item_name = row[1] item.description = row[2] item.item_group = row[3] item.sub_item_group = row[4] item.item_company = row[5] item.standard_rate = row[6] item.valuation_rate = row[6] item.stock_uom = row[7] item.is_stock_item = 1 item.item_code = row[11] if flt(row[8]) > 0 and flt(row[6]) > 0: item.opening_stock = row[8] if cint(row[12]) == 1: item.append('taxes', { 'tax_type': 'VAT 5% - P', 'tax_rate': 5 }) if row[10]: item.append( 'item_defaults', { 'company': 'Primacasa', 'default_warehouse': 'Stores - P', 'default_price_list': 'Standard Selling', 'default_supplier': row[10] }) item.append('supplier_items', { 'supplier': row[10], 'supplier_part_no': str(row[10][0:5]) }) item.save() i += 1 print('*************') print(i) print('*************')
def validate_einvoice(validations, einvoice, errors=[]): for fieldname, field_validation in validations.items(): value = einvoice.get(fieldname, None) if not value or value == "None": # remove keys with empty values einvoice.pop(fieldname, None) continue value_type = field_validation.get("type").lower() if value_type in ['object', 'array']: child_validations = field_validation.get('properties') if isinstance(value, list): for d in value: validate_einvoice(child_validations, d, errors) if not d: # remove empty dicts einvoice.pop(fieldname, None) else: validate_einvoice(child_validations, value, errors) if not value: # remove empty dicts einvoice.pop(fieldname, None) continue # convert to int or str if value_type == 'string': einvoice[fieldname] = str(value) elif value_type == 'number': is_integer = '.' not in str(field_validation.get('maximum')) precision = 3 if '.999' in str( field_validation.get('maximum')) else 2 einvoice[fieldname] = flt( value, precision) if not is_integer else cint(value) value = einvoice[fieldname] max_length = field_validation.get('maxLength') minimum = flt(field_validation.get('minimum')) maximum = flt(field_validation.get('maximum')) pattern_str = field_validation.get('pattern') pattern = re.compile(pattern_str or '') label = field_validation.get('description') or fieldname if value_type == 'string' and len(value) > max_length: errors.append( _('{} should not exceed {} characters').format( label, max_length)) if value_type == 'number' and (value > maximum or value < minimum): errors.append( _('{} {} should be between {} and {}').format( label, value, minimum, maximum)) if pattern_str and not pattern.match(value): errors.append(field_validation.get('validationMsg')) return errors
def is_past_grace_period(self): """ Returns `True` if the grace period for the `Subscription` has passed """ current_invoice = self.get_current_invoice() if self.current_invoice_is_past_due(current_invoice): subscription_settings = frappe.get_single('Subscription Settings') grace_period = cint(subscription_settings.grace_period) return getdate(nowdate()) > add_days(current_invoice.due_date, grace_period)
def set_status_grace_period(self): """ Sets the `Subscription` `status` based on the preference set in `Subscription Settings`. Used when the `Subscription` needs to decide what to do after the current generated invoice is past it's due date and grace period. """ subscription_settings = frappe.get_single('Subscription Settings') if self.status == 'Past Due Date' and self.is_past_grace_period(): self.status = 'Cancelled' if cint(subscription_settings.cancel_after_grace) else 'Unpaid'
def update_other_charges(tax_row, invoice_value_details, gst_accounts_list, invoice, considered_rows): prev_row_id = cint(tax_row.row_id) - 1 if tax_row.account_head in gst_accounts_list and prev_row_id not in considered_rows: if tax_row.charge_type == 'On Previous Row Amount': amount = invoice.get('taxes')[prev_row_id].tax_amount_after_discount_amount invoice_value_details.total_other_charges -= abs(amount) considered_rows.append(prev_row_id) if tax_row.charge_type == 'On Previous Row Total': amount = invoice.get('taxes')[prev_row_id].base_total - invoice.base_net_total invoice_value_details.total_other_charges -= abs(amount) considered_rows.append(prev_row_id)
def get_marketing_account(company): abbr = frappe.db.get_value("Company", company, "abbr") if cint( frappe.db.sql("""select if( exists(select * from tabAccount where name = '%s'), 1, 0) """ % "Marketing Expenses - %s" % abbr, as_list=1)[0][0]): return "Marketing Expenses - %s" % abbr else: frappe.throw( _("Marketing Account not exist. Please create Marketing Account under Indirect Expenses Account" ))
def money_in_words(number, main_currency=None, fraction_currency=None): """ Returns string in words with currency and fraction currency. """ from frappe.utils import get_defaults _ = frappe._ try: # note: `flt` returns 0 for invalid input and we don't want that number = float(number) except ValueError: return "" number = flt(number) if number < 0: return "" d = get_defaults() if not main_currency: main_currency = d.get('currency', 'INR') if not fraction_currency: fraction_currency = frappe.db.get_value( "Currency", main_currency, "fraction", cache=True) or _("Cent") number_format = frappe.db.get_value("Currency", main_currency, "number_format", cache=True) or \ frappe.db.get_default("number_format") or "#,###.##" fraction_length = get_number_format_info(number_format)[2] n = "%.{0}f".format(fraction_length) % number numbers = n.split('.') main, fraction = numbers if len(numbers) > 1 else [n, '00'] if len(fraction) < fraction_length: zeros = '0' * (fraction_length - len(fraction)) fraction += zeros in_million = True if number_format == "#,##,###.##": in_million = False # 0.00 if main == '0' and fraction in ['00', '000']: out = "{0} {1}".format(main_currency, _('Zero')) # 0.XX elif main == '0': out = fraction + '/100' else: out = _(in_words(main, in_million).title()) if cint(fraction): out = out + ' ' + _('and') + ' ' + fraction + '/100' return out
def create_point_transaction(ref_link, ref_name, inv, tran_type, points, rule_details=None): if points != 0: tran = frappe.new_doc("Point Transaction") tran.ref_link = ref_link tran.ref_name = ref_name tran.date = today() tran.type = tran_type tran.points = cint(points) * 1 if tran_type == 'Earned' else -1 * cint( points) if rule_details: tran.valied_upto = add_months(nowdate(), cint(rule_details.get('valid_upto'))) tran.invoice_number = inv tran.rule_details = cstr(rule_details) tran.docstatus = 1 tran.insert()
def tst_add_locc(): frappe.get_doc({ "doctype": "Leave Allocation", "employee": 'EMPtst0001', "employee_name": 'عمر', "leave_type": 'Annual Leave - اجازة اعتيادية', "from_date": '2020-01-01', "to_date": '2020-12-31', "carry_forward": cint(1), "new_leaves_allocated": 0, "docstatus": 1 }).insert(ignore_permissions=True)
def create_reddem_points_entry(rule_details, sales_invoice_details, debit_to, credit_to, journal_voucher): debit_to, credit_to = credit_to, debit_to for entry in journal_voucher.entries: if entry.mode == "Redeem": conversion_factor = frappe.db.get_value('LPE Configuration', None, 'conversion_factor') create_point_transaction( 'Customer', sales_invoice_details.customer, entry.against_invoice, 'Redeem', cint(flt(entry.credit) / flt(conversion_factor))) create_jv(sales_invoice_details, sales_invoice_details.redeem_points, debit_to, credit_to)
def _create_product(stock_entry, data): derivative_type = frappe.get_value("Item Group", stock_entry.product_group, "external_id") request_data = {} if not derivative_type: frappe.throw("Invalid Inventory type on '{0}'".format( stock_entry.product_group)) request_data["data"] = data request_data["derivative_type"] = cint(derivative_type) request_data["derivative_quantity"] = flt(stock_entry.product_qty) request_data["derivative_quantity_uom"] = "g" request_data["waste"] = flt(stock_entry.product_waste) request_data["waste_uom"] = "g" product_usable = flt(stock_entry.product_usable) if product_usable: request_data["derivative_usable"] = product_usable if stock_entry.product_name: request_data["derivative_product"] = stock_entry.product_name response = {} try: response = call("inventory_convert", request_data) except BioTrackClientError as ex: frappe.throw(cstr(ex.message), title="BioTrack sync-up failed") derivatives = response.get("derivatives", []) for derivative in derivatives: item_type = derivative.get("barcode_type") barcode = derivative.get("barcode_id") if item_type == 27 and stock_entry.waste_item: frappe.db.set_value("Item", stock_entry.waste_item, { "bio_barcode": barcode, "disabled": 0 }, update_modified=False) elif item_type == derivative_type and stock_entry.product_item: frappe.db.set_value("Item", stock_entry.product_item, { "bio_barcode": barcode, "disabled": 0 }, update_modified=False)
def create_invoice(self, prorate): """ Creates a `Sales Invoice`, submits it and returns it """ invoice = frappe.new_doc('Sales Invoice') invoice.set_posting_time = 1 invoice.posting_date = self.current_invoice_start invoice.customer = self.customer if self.company: invoice.company = self.company # 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: invoice.append('items', item) # Taxes if self.tax_template: invoice.taxes_and_charges = self.tax_template invoice.set_taxes() # Due date invoice.append( 'payment_schedule', { 'due_date': add_days(self.current_invoice_end, 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_additional_discount = 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.save() invoice.submit() return invoice
def tst_allocation(): length = frappe.db.sql( "select count(name) from `tabEmployee` where status!='left'") emp = frappe.db.sql( "select name,date_of_joining,employee_name,department,emp_nationality,work_days from `tabEmployee` where status!='left'" ) c = 0 for i in range(length[0][0]): leave_allocation = frappe.db.sql( "select from_date,to_date from `tabLeave Allocation` where employee='{0}' order by creation desc " .format(emp[i][0])) if leave_allocation: if str(datetime.date.today()) > str(leave_allocation[0][1]): next_allocation = datetime.datetime.strptime( str(leave_allocation[0][1]), '%Y-%m-%d') frappe.get_doc({ "doctype": "Leave Allocation", "employee": emp[i][0], "employee_name": emp[i][2], "department": emp[i][3], "leave_type": 'Annual Leave - اجازة اعتيادية', "from_date": date(next_allocation.year, next_allocation.month, next_allocation.day) + relativedelta(days=+1), "to_date": date(next_allocation.year, next_allocation.month, next_allocation.day) + relativedelta(days=+1, years=+1), "carry_forward": cint(1), "new_leaves_allocated": emp[i][5], "docstatus": 1 }).insert(ignore_permissions=True) print leave_allocation[0][0], leave_allocation[0][1] c += 1 print c
def set_subscription_status(self): """ Sets the status of the `Subscription` """ if self.is_trialling(): self.status = 'Trialling' elif self.status == 'Past Due Date' and self.is_past_grace_period(): subscription_settings = frappe.get_single('Subscription Settings') self.status = 'Cancelled' if cint(subscription_settings.cancel_after_grace) else 'Unpaid' elif self.status == 'Past Due Date' and not self.has_outstanding_invoice(): self.status = 'Active' elif self.current_invoice_is_past_due(): self.status = 'Past Due Date' elif self.is_new_subscription(): self.status = 'Active' # todo: then generate new invoice self.save()
def before_submit(plant): # only root plant get handled if plant.brother_plant: return plants = plant.flags.bulk_plants or [] plants.append(plant) if len(plants) != plant.get("qty"): frappe.throw("Bulk adding qty mismatch") plant_room = frappe.get_doc("Plant Room", plant.get("plant_room")) if not is_bio_plant_room(plant_room): return source = None if plant.get("item_code"): source = frappe.get_value("Item", plant.item_code, "bio_barcode") elif plant.get("source_plant"): source = frappe.get_value("Plant", plant.source_plant, "bio_barcode") if not source: return try: result = call( "plant_new", { "room": plant_room.bio_id, "quantity": plant.get("qty"), "strain": plant.get("strain"), "source": source, "mother": cint(plant.get("is_mother")), "location": get_location() }) except BioTrackClientError as e: plant.revert_on_failure() frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") else: for idx, barcode in enumerate(result.get("barcode_id")): doc = plants[idx] frappe.db.set_value("Plant", doc.name, "bio_barcode", barcode, update_modified=False)
def generate_eway_bill(self, **kwargs): args = frappe._dict(kwargs) headers = self.get_headers() eway_bill_details = get_eway_bill_details(args) data = json.dumps( { 'Irn': args.irn, 'Distance': cint(eway_bill_details.distance), 'TransMode': eway_bill_details.mode_of_transport, 'TransId': eway_bill_details.gstin, 'TransName': eway_bill_details.transporter, 'TrnDocDt': eway_bill_details.document_date, 'TrnDocNo': eway_bill_details.document_name, 'VehNo': eway_bill_details.vehicle_no, 'VehType': eway_bill_details.vehicle_type }, indent=4) try: res = self.make_request('post', self.generate_ewaybill_url, headers, data) if res.get('success'): self.invoice.ewaybill = res.get('result').get('EwbNo') self.invoice.eway_bill_cancelled = 0 self.invoice.update(args) self.invoice.flags.updater_reference = { 'doctype': self.invoice.doctype, 'docname': self.invoice.name, 'label': _('E-Way Bill Generated') } self.update_invoice() else: raise RequestFailed except RequestFailed: errors = self.sanitize_error_message(res.get('message')) self.raise_error(errors=errors) except Exception: self.log_error(data) self.raise_error(True)
def before_submit(plant): # only root plant get handled if plant.brother_plant: return plants = plant.flags.bulk_plants or [] plants.append(plant) if len(plants) != plant.get("qty"): frappe.throw("Bulk adding qty mismatch") plant_room = frappe.get_doc("Plant Room", plant.get("plant_room")) if not is_bio_plant_room(plant_room): return source = None if plant.get("item_code"): source = frappe.get_value("Item", plant.item_code, "bio_barcode") elif plant.get("source_plant"): source = frappe.get_value("Plant", plant.source_plant, "bio_barcode") if not source: return try: result = call("plant_new", { "room": plant_room.bio_id, "quantity": plant.get("qty"), "strain": plant.get("strain"), "source": source, "mother": cint(plant.get("is_mother")), "location": get_location() }) except BioTrackClientError as e: plant.revert_on_failure() frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") else: for idx, barcode in enumerate(result.get("barcode_id")): doc = plants[idx] frappe.db.set_value("Plant", doc.name, "bio_barcode", barcode, update_modified=False)
def set_subscription_status(self): """ Sets the status of the `Subscription` """ if self.is_trialling(): self.status = "Trialling" elif self.status == "Active" and self.end_date and getdate() > getdate( self.end_date): self.status = "Completed" elif self.is_past_grace_period(): subscription_settings = frappe.get_single("Subscription Settings") self.status = "Cancelled" if cint( subscription_settings.cancel_after_grace) else "Unpaid" elif self.current_invoice_is_past_due( ) and not self.is_past_grace_period(): self.status = "Past Due Date" elif not self.has_outstanding_invoice(): self.status = "Active" elif self.is_new_subscription(): self.status = "Active" self.save()
def before_submit(plant_entry): """BioTrack sync up: inventory_new or inventory_adjust""" barcodeid = [] for ple_detail in plant_entry.get("plants"): plant = frappe.get_doc("Plant", ple_detail.plant_code) if is_bio_plant(plant): barcodeid.append(plant.get("bio_barcode")) if len(barcodeid) == 0: return if plant_entry.purpose == "Convert": convert_on_submit(plant_entry, barcodeid) return action = "plant_harvest" if plant_entry.purpose == "Harvest" else "plant_cure" res = None try: res = call( action, { "barcodeid": barcodeid, "location": get_location(), "weights": make_weights_data(plant_entry.flower, plant_entry.other_material, plant_entry.waste), "collectadditional": cint(plant_entry.additional_collections), }) except BioTrackClientError as e: frappe.throw(cstr(e.message), title="BioTrackTHC sync up failed") if res: plant_entry.bio_transaction = res.get("transactionid") items = plant_entry.items or {} map_item_derivatives(items, res.get("derivatives", []))
def _create_product(stock_entry, data): derivative_type = frappe.get_value("Item Group", stock_entry.product_group, "external_id") request_data = {} if not derivative_type: frappe.throw("Invalid Inventory type on '{0}'".format(stock_entry.product_group)) request_data["data"] = data request_data["derivative_type"] = cint(derivative_type) request_data["derivative_quantity"] = flt(stock_entry.product_qty) request_data["derivative_quantity_uom"] = "g" request_data["waste"] = flt(stock_entry.product_waste) request_data["waste_uom"] = "g" product_usable = flt(stock_entry.product_usable) if product_usable: request_data["derivative_usable"] = product_usable if stock_entry.product_name: request_data["derivative_product"] = stock_entry.product_name response = {} try: response = call("inventory_convert", request_data) except BioTrackClientError as ex: frappe.throw(cstr(ex.message), title="BioTrack sync-up failed") derivatives = response.get("derivatives", []) for derivative in derivatives: item_type = derivative.get("barcode_type") barcode = derivative.get("barcode_id") if item_type == 27 and stock_entry.waste_item: frappe.db.set_value("Item", stock_entry.waste_item, {"bio_barcode": barcode, "disabled": 0}, update_modified=False) elif item_type == derivative_type and stock_entry.product_item: frappe.db.set_value("Item", stock_entry.product_item, {"bio_barcode": barcode, "disabled": 0}, update_modified=False)
def is_trial(self): if self.trial_period_start: return getdate(self.trial_period_end) > getdate(nowdate()) or cint( self.trial_validated) == 0 else: return False
def calc_basic_points(rule_details, inv_amount): return rule_details.get('points_earned')*cint(inv_amount/rule_details.get('amount'))
def calc_referral_points(rule_details): return cint(rule_details.get('referred_points'))
def get_print_process_required(item_name): return cint(frappe.get_value('Item', item_name, 'print_process_required'))
def get_due_date(self): return add_days(self.current_invoice_start if \ self.generate_invoice_at_period_start else self.current_invoice_end, cint(self.days_until_due))
def something(valid_modes, journal_voucher): total = 0 for entry in journal_voucher.entries: if entry.mode in valid_modes: total += cint(entry.credit) return total
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 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) 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 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: 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.save() invoice.submit() return invoice
def create_reddem_points_entry(rule_details, sales_invoice_details, debit_to, credit_to, journal_voucher): debit_to, credit_to = credit_to, debit_to for entry in journal_voucher.entries: if entry.mode == "Redeem": conversion_factor = frappe.db.get_value('LPE Configuration', None, 'conversion_factor') create_point_transaction('Customer', sales_invoice_details.customer, entry.against_invoice, 'Redeem', cint(flt(entry.credit) / flt(conversion_factor))) create_jv(sales_invoice_details, sales_invoice_details.redeem_points, debit_to, credit_to)
def multiplier_points(rule_details, points_earned): return points_earned * cint(rule_details.get('multiplier'))