def update_cash_basis(cls, lines, ratio, period): if not lines: return to_save = [] lines = cls.browse( sorted(lines, key=sortable_values(cls.group_cash_basis_key))) for key, lines in groupby(lines, key=cls.group_cash_basis_key): key = dict(key) if not key['on_cash_basis']: continue lines = list(lines) company = lines[0].company line_no_periods = [l for l in lines if not l.period] if line_no_periods: line_no_period, = line_no_periods to_save.append(line_no_period) else: line_no_period = None total = sum(l.amount for l in lines) amount = company.currency.round(total * ratio) if line_no_period and line_no_period.amount == amount: line_no_period.period = period else: line = cls(**key) if line_no_period: line.amount = amount - total + line_no_period.amount line_no_period.amount -= line.amount else: line.amount = amount - total line.period = period if line.amount: to_save.append(line) cls.save(to_save)
def transition_create_quotations(self): pool = Pool() Quotation = pool.get('purchase.request.quotation') QuotationLine = pool.get('purchase.request.quotation.line') quotations = [] lines = [] requests = [ r for r in self.records if r.state in ['draft', 'quotation'] ] for supplier in self.ask_suppliers.suppliers: sub_requests = [ r for r in requests if self.filter_request(r, supplier) ] sub_requests = sorted(sub_requests, key=sortable_values(self._group_request_key)) for key, grouped_requests in groupby(sub_requests, key=self._group_request_key): quotation = self.get_quotation(supplier, key) for request in grouped_requests: line = self.get_quotation_line(request, quotation) line.quotation = quotation lines.append(line) quotations.append(quotation) QuotationLine.save(lines) Quotation.save(quotations) self.model.update_state(requests) self.succeed.number_quotations = len(quotations) return 'succeed'
def _get_carrier_context(self): context = super(ShipmentIn, self)._get_carrier_context() if not self.carrier: return context if self.carrier.carrier_cost_method != 'weight': return context weights = [] context['weights'] = weights lines = self.incoming_moves or [] keyfunc = partial(self._group_parcel_key, lines) lines = sorted(lines, key=sortable_values(keyfunc)) for key, parcel in groupby(lines, key=keyfunc): weights.append(self._parcel_weight(parcel)) return context
def _get_carrier_context(self): context = super(Sale, self)._get_carrier_context() if not self.carrier or self.carrier.carrier_cost_method != 'weight': return context context = context.copy() weights = [] context['weights'] = weights lines = [l for l in self.lines or [] if l.quantity and l.quantity > 0] keyfunc = partial(self._group_parcel_key, lines) lines = sorted(lines, key=sortable_values(keyfunc)) for key, parcel in groupby(lines, key=keyfunc): weights.append(self._parcel_weight(parcel)) return context
def test_sortable_values(self): def key(values): return values values = [ (('a', 1), ('b', None)), (('a', 1), ('b', 3)), (('a', 1), ('b', 2)), ] with self.assertRaises(TypeError): sorted(values, key=key) self.assertEqual(sorted(values, key=sortable_values(key)), [ (('a', 1), ('b', 2)), (('a', 1), ('b', 3)), (('a', 1), ('b', None)), ])
def get_invoice_lines(cls, consumptions, invoice): "Return a list of lines and a list of consumptions" pool = Pool() InvoiceLine = pool.get('account.invoice.line') lines, grouped_consumptions = [], [] consumptions = sorted(consumptions, key=sortable_values(cls._group_invoice_key)) for key, sub_consumptions in groupby(consumptions, key=cls._group_invoice_key): sub_consumptions = list(sub_consumptions) line = InvoiceLine(**dict(key)) line.invoice_type = 'out' line.type = 'line' line.quantity = sum(c.quantity for c in sub_consumptions) line.account = line.product.account_revenue_used if not line.account: raise InvoiceError( gettext( 'sale_subscription' '.msg_consumption_invoice_missing_account_revenue', product=line.product.rec_name)) taxes = [] pattern = line._get_tax_rule_pattern() party = invoice.party for tax in line.product.customer_taxes_used: if party.customer_tax_rule: tax_ids = party.customer_tax_rule.apply(tax, pattern) if tax_ids: taxes.extend(tax_ids) continue taxes.append(tax.id) if party.customer_tax_rule: tax_ids = party.customer_tax_rule.apply(None, pattern) if tax_ids: taxes.extend(tax_ids) line.taxes = taxes lines.append(line) grouped_consumptions.append(sub_consumptions) return lines, grouped_consumptions
def _get_carrier_context(self): context = super(ShipmentOut, self)._get_carrier_context() if not self.carrier: return context if self.carrier.carrier_cost_method != 'weight': return context weights = [] context['weights'] = weights if (self.state in {'draft', 'waiting', 'assigned'} and self.warehouse_storage != self.warehouse_output): lines = self.inventory_moves or [] else: lines = self.outgoing_moves or [] keyfunc = partial(self._group_parcel_key, lines) lines = sorted(lines, key=sortable_values(keyfunc)) for key, parcel in groupby(lines, key=keyfunc): weights.append(self._parcel_weight(parcel)) return context