def finish_record(self, price, finished_by, used_coupon=None): prepayments = self.get_total_prepayments() denomination = (used_coupon.denomination if used_coupon else 0) revenue = price - denomination - prepayments assert revenue >= Money( 0, settings.DEFAULT_CURRENCY ), "Сумма должна быть больше либо равна внесенной предоплаты и номинала купона." for payment in EmployeeRecordPayment.objects.filter(record=self): payment.delete() self.transaction_class.create_transaction( transaction_purpose_pk=self.parlor_id, amount=revenue, created_by=finished_by, reference=f"Закрытие записи №{self.id}", entity_object=self) self.status = self.STATUS.finished self.price = price self.used_coupon = used_coupon self.save(update_fields=['status', 'price', 'used_coupon']) coupon = (used_coupon.denomination if used_coupon and used_coupon.type == used_coupon.TYPE_CHOICES.discount else 0) cash_for_motivation = price - denomination performer_motivation = (self.performer.session_motivations.filter( session_type_id=self.type.id).values('base_percent').first()) if performer_motivation is not None: performer_motivation = Decimal( performer_motivation.get('base_percent', 0) / 100.0) perfomer_salary = cash_for_motivation * performer_motivation if perfomer_salary > Money(0, settings.DEFAULT_CURRENCY): EmployeeRecordPayment.create( self, self.performer, EmployeeRecordPayment.TYPE_CHOICES.work, perfomer_salary, finished_by) invite_motivation = (self.created_by.session_motivations.filter( session_type_id=self.type.id).values('invite_percent').first()) if invite_motivation is not None: invite_motivation = Decimal( invite_motivation.get('invite_percent', 0) / 100.0) invite_salary = cash_for_motivation * invite_motivation if invite_salary > Money(0, settings.DEFAULT_CURRENCY): EmployeeRecordPayment.create( self, self.performer, EmployeeRecordPayment.TYPE_CHOICES.invite, invite_salary, finished_by) return self
def _update_markup_cost(self, start_date, end_date): """Populate markup costs for OpenShift. Args: start_date (str) The date to start populating the table. end_date (str) The date to end on. Returns None """ with CostModelDBAccessor(self._schema, self._provider_uuid) as cost_model_accessor: markup = cost_model_accessor.markup markup = Decimal(markup.get("value", 0)) / 100 with OCPReportDBAccessor(self._schema) as accessor: LOG.info( "Updating markup for" "\n\tSchema: %s \n\t%s Provider: %s (%s) \n\tDates: %s - %s", self._schema, self._provider.type, self._provider.name, self._provider_uuid, start_date, end_date, ) accessor.populate_markup_cost(markup, start_date, end_date, self._cluster_id) LOG.info("Finished updating markup.")
def add_deltas(self, query_data, query_sum): """Calculate and add cost deltas to a result set. Args: query_data (list) The existing query data from execute_query query_sum (list) The sum returned by calculate_totals Returns: (dict) query data with new with keys "value" and "percent" """ delta_group_by = ['date'] + self._get_group_by() delta_filter = self._get_filter(delta=True) q_table = self._mapper._provider_map.get('tables').get('previous_query') previous_query = q_table.objects.filter(delta_filter) previous_dict = self._create_previous_totals(previous_query, delta_group_by) for row in query_data: key = tuple((row[key] for key in delta_group_by)) previous_total = previous_dict.get(key, 0) current_total = row.get(self._delta, 0) row['delta_value'] = current_total - previous_total row['delta_percent'] = self._percent_delta(current_total, previous_total) # Calculate the delta on the total aggregate if self._delta in query_sum: current_total_sum = Decimal(query_sum.get(self._delta) or 0) else: current_total_sum = Decimal(query_sum.get('value') or 0) delta_field = self._mapper._report_type_map.get('delta_key').get(self._delta) prev_total_sum = previous_query.aggregate(value=delta_field) if self.resolution == 'daily': dates = [entry.get('date') for entry in query_data] prev_total_filters = self._get_previous_totals_filter(dates) if prev_total_filters: prev_total_sum = previous_query\ .filter(prev_total_filters)\ .aggregate(value=delta_field) prev_total_sum = Decimal(prev_total_sum.get('value') or 0) total_delta = current_total_sum - prev_total_sum total_delta_percent = self._percent_delta(current_total_sum, prev_total_sum) self.query_delta = { 'value': total_delta, 'percent': total_delta_percent } if self.order_field == 'delta': reverse = True if self.order_direction == 'desc' else False query_data = sorted(list(query_data), key=lambda x: x['delta_percent'], reverse=reverse) return query_data
def add_deltas(self, query_data, query_sum): """Calculate and add cost deltas to a result set. Args: query_data (list) The existing query data from execute_query query_sum (list) The sum returned by calculate_totals Returns: (dict) query data with new with keys "value" and "percent" """ delta_group_by = ["date"] + self._get_group_by() delta_filter = self._get_filter(delta=True) previous_query = self.query_table.objects.filter(delta_filter) previous_dict = self._create_previous_totals(previous_query, delta_group_by) for row in query_data: key = tuple(row[key] for key in delta_group_by) previous_total = previous_dict.get(key) or 0 current_total = row.get(self._delta) or 0 row["delta_value"] = current_total - previous_total row["delta_percent"] = self._percent_delta(current_total, previous_total) # Calculate the delta on the total aggregate if self._delta in query_sum: if isinstance(query_sum.get(self._delta), dict): current_total_sum = Decimal(query_sum.get(self._delta, {}).get("value") or 0) else: current_total_sum = Decimal(query_sum.get(self._delta) or 0) else: if isinstance(query_sum.get("cost"), dict): current_total_sum = Decimal(query_sum.get("cost", {}).get("total").get("value") or 0) else: current_total_sum = Decimal(query_sum.get("cost") or 0) delta_field = self._mapper._report_type_map.get("delta_key").get(self._delta) prev_total_sum = previous_query.aggregate(value=delta_field) if self.resolution == "daily": dates = [entry.get("date") for entry in query_data] prev_total_filters = self._get_previous_totals_filter(dates) if prev_total_filters: prev_total_sum = previous_query.filter(prev_total_filters).aggregate(value=delta_field) prev_total_sum = Decimal(prev_total_sum.get("value") or 0) total_delta = current_total_sum - prev_total_sum total_delta_percent = self._percent_delta(current_total_sum, prev_total_sum) self.query_delta = {"value": total_delta, "percent": total_delta_percent} if self.order_field == "delta": reverse = True if self.order_direction == "desc" else False query_data = sorted( list(query_data), key=lambda x: (x["delta_percent"] is None, x["delta_percent"]), reverse=reverse ) return query_data
def get_product_variant(self, arguman): product_id = self choices = getDetailListForUpdateCart(arguman) variant = Variant.objects.filter(product__pk=product_id) for item in range(len(choices)): variant = variant.filter(sub_details__pk=choices[item]) print("new item") if variant: variant = Decimal(Variant.objects.get(id=variant.get().id).price) else: variant = Decimal(0) return variant
def _iterate_and_validate_columns(self, selected_year, current_uf_values, response): only_current_year = getattr(self, 'only_current_year', False) day = None month = None current_dates = list( map(lambda uf_value: uf_value.get('date'), current_uf_values)) for td in response.css('.Grid tr td'): cell = td.css('::text').extract_first() if cell in MONTH_DAYS: day = int(cell) # Day of month month = 1 continue else: uf_value = cell # UF Value date = get_valid_date(int(selected_year), month, day) if uf_value is not None and date is not None: uf_value = Decimal( uf_value.replace('.', '').replace(',', '.')) if date not in current_dates: self.uf_values.append( UFValue(value=uf_value, date=date)) elif only_current_year: uf = list( filter( lambda uf_value: uf_value.get('date') == date, current_uf_values))[0] current_value = uf.get('value') if uf_value != current_value: UFValue.objects.filter(date=date).update( value=uf_value) logger.info( 'Value for the date {} updated: {}'.format( date, uf_value)) month += 1