def _compute_taxes(self): Tax = Pool().get('account.tax') context = self.get_tax_context() res = {} for line in self.lines: # Don't round on each line to handle rounding error if line.type != 'line': continue with Transaction().set_context(**context): if(line.product and line.product.dont_multiply): taxes = Tax.compute(line.taxes, line.unit_price, 1.0) else: taxes = Tax.compute(line.taxes, line.unit_price, line.quantity) for tax in taxes: key, val = self._compute_tax(tax, self.type) val['invoice'] = self.id if not key in res: res[key] = val else: res[key]['base'] += val['base'] res[key]['amount'] += val['amount'] for key in res: for field in ('base', 'amount'): res[key][field] = self.currency.round(res[key][field]) return res
def apply_shipping_rate(self, rate): """ This method applies shipping rate. Rate is a dictionary with following minimum keys: { 'display_name': Name to display, 'carrier_service': carrier.service active record, 'cost': cost, 'cost_currency': currency.currency active repord, 'carrier': carrier active record, } """ Currency = Pool().get('currency.currency') shipment_cost = rate['cost_currency'].round(rate['cost']) if self.cost_currency != rate['cost_currency']: shipment_cost = Currency.compute( rate['cost_currency'], shipment_cost, self.cost_currency ) self.cost = shipment_cost self.cost_currency = self.cost_currency self.carrier = rate['carrier'] self.carrier_service = rate['carrier_service'] self.save()
def get_move_line(self): ''' Return the move line for the statement line ''' pool = Pool() MoveLine = pool.get('account.move.line') Currency = Pool().get('currency.currency') zero = Decimal("0.0") if not self.amount: return with Transaction().set_context(date=self.date): amount = Currency.compute(self.statement.journal.currency, self.amount, self.statement.company.currency) if self.statement.journal.currency != self.statement.company.currency: second_currency = self.statement.journal.currency.id amount_second_currency = -self.amount else: amount_second_currency = None second_currency = None return MoveLine( description=self.description, debit=amount < zero and -amount or zero, credit=amount >= zero and amount or zero, account=self.account, party=self.party if self.account.party_required else None, second_currency=second_currency, amount_second_currency=amount_second_currency, )
def apply_shipping_rate(self, rate): """ This method applies shipping rate. Rate is a dictionary with following minimum keys: { 'display_name': Name to display, 'carrier_service': carrier.service active record, 'cost': cost, 'cost_currency': currency.currency active repord, 'carrier': carrier active record, } """ Currency = Pool().get('currency.currency') shipment_cost = rate['cost_currency'].round(rate['cost']) if self.cost_currency != rate['cost_currency']: shipment_cost = Currency.compute(rate['cost_currency'], shipment_cost, self.cost_currency) self.cost = shipment_cost self.cost_currency = self.cost_currency self.carrier = rate['carrier'] self.carrier_service = rate['carrier_service'] self.save()
def update_pricelist_shipment_cost(self): "Add a shipping line to sale for pricelist costmethod" Sale = Pool().get('sale.sale') Currency = Pool().get('currency.currency') if not self.carrier or self.carrier.carrier_cost_method != 'pricelist': return with Transaction().set_context(self._get_carrier_context()): shipment_cost = self.carrier.get_sale_price() if not shipment_cost[0]: return shipment_cost = Currency.compute( Currency(shipment_cost[1]), shipment_cost[0], self.currency ) Sale.write([self], { 'lines': [ ('create', [{ 'type': 'line', 'product': self.carrier.carrier_product.id, 'description': self.carrier.carrier_product.name, 'quantity': 1, # XXX 'unit': self.carrier.carrier_product.sale_uom.id, 'unit_price': Decimal(shipment_cost), 'shipment_cost': Decimal(shipment_cost), 'amount': Decimal(shipment_cost), 'taxes': [], 'sequence': 9999, # XXX }]), ('delete', [ line for line in self.lines if line.shipment_cost ]), ] })
def on_change_amount(self): Currency = Pool().get('currency.currency') res = {} if self.party: if self.account and self.account not in ( self.party.account_receivable, self.party.account_payable): # The user has entered a non-default value, we keep it. pass elif self.amount: if self.amount > Decimal("0.0"): account = self.party.account_receivable else: account = self.party.account_payable res['account'] = account.id res['account.rec_name'] = account.rec_name if self.invoice: if self.amount and self.statement and self.statement.journal: invoice = self.invoice journal = self.statement.journal with Transaction().set_context(date=invoice.currency_date): amount_to_pay = Currency.compute(invoice.currency, invoice.amount_to_pay, journal.currency) if abs(self.amount) > amount_to_pay: res['invoice'] = None else: res['invoice'] = None return res
def on_change_amount(self): Currency = Pool().get('currency.currency') if self.party: with Transaction().set_context(date=self.date): if self.account and self.account not in ( self.party.account_receivable_used, self.party.account_payable_used): # The user has entered a non-default value, we keep it. pass elif self.amount: if self.amount > Decimal("0.0"): self.account = self.party.account_receivable_used else: self.account = self.party.account_payable_used if self.invoice: if self.amount and self.statement and self.statement.journal: invoice = self.invoice journal = self.statement.journal with Transaction().set_context(date=invoice.currency_date): amount_to_pay = Currency.compute(invoice.currency, invoice.amount_to_pay, journal.currency) if abs(self.amount) > amount_to_pay: self.invoice = None else: self.invoice = None
def apply_ups_shipping(self): "Add a shipping line to sale for ups" Sale = Pool().get('sale.sale') Currency = Pool().get('currency.currency') if self.is_ups_shipping: with Transaction().set_context(self._get_carrier_context()): shipment_cost, currency_id = self.carrier.get_sale_price() if not shipment_cost: return # Convert the shipping cost to sale currency from USD shipment_cost = Currency.compute( Currency(currency_id), shipment_cost, self.currency ) Sale.write([self], { 'lines': [ ('create', [{ 'type': 'line', 'product': self.carrier.carrier_product.id, 'description': self.ups_service_type.name, 'quantity': 1, # XXX 'unit': self.carrier.carrier_product.sale_uom.id, 'unit_price': shipment_cost, 'shipment_cost': shipment_cost, 'amount': shipment_cost, 'taxes': [], 'sequence': 9999, # XXX }]), ('delete', [ line for line in self.lines if line.shipment_cost ]), ] })
def _get_move_line(self, date, amount, account_id, party_required=False): ''' Return move line ''' Currency = Pool().get('currency.currency') res = {} if self.currency.id != self.company.currency.id: with Transaction().set_context(date=self.date): res['amount_second_currency'] = Currency.compute( self.company.currency, amount, self.currency) res['amount_second_currency'] = abs(res['amount_second_currency']) res['second_currency'] = self.currency.id else: res['amount_second_currency'] = Decimal('0.0') res['second_currency'] = None if amount >= Decimal('0.0'): res['debit'] = Decimal('0.0') res['credit'] = amount else: res['debit'] = - amount res['credit'] = Decimal('0.0') res['account'] = account_id res['maturity_date'] = date res['description'] = self.description if party_required: res['party'] = self.party.id return res
def _get_move_line(self, date, amount, account_id): ''' Return move line ''' Currency = Pool().get('currency.currency') res = {} if self.currency.id != self.company.currency.id: with Transaction().set_context(date=self.date): res['amount_second_currency'] = Currency.compute( self.company.currency, amount, self.currency) res['amount_second_currency'] = abs(res['amount_second_currency']) res['second_currency'] = self.currency.id else: res['amount_second_currency'] = Decimal('0.0') res['second_currency'] = None if amount >= Decimal('0.0'): res['debit'] = Decimal('0.0') res['credit'] = amount else: res['debit'] = -amount res['credit'] = Decimal('0.0') res['account'] = account_id res['maturity_date'] = date res['description'] = self.description res['party'] = self.party.id return res
def create_move(self, date=None): """ Create the account move for the payment :param date: Optional date for the account move :return: Active record of the created move """ Currency = Pool().get('currency.currency') Period = Pool().get('account.period') Move = Pool().get('account.move') journal = self.gateway.journal date = date or self.date if not journal.debit_account: self.raise_user_error('missing_debit_account', (journal.rec_name,)) period_id = Period.find(self.company.id, date=date) amount_second_currency = second_currency = None amount = self.amount if self.currency != self.company.currency: amount = Currency.compute( self.currency, self.amount, self.company.currency ) amount_second_currency = self.amount second_currency = self.currency lines = [{ 'description': self.rec_name, 'account': self.credit_account.id, 'party': self.party.id, 'debit': Decimal('0.0'), 'credit': amount, 'amount_second_currency': amount_second_currency, 'second_currency': second_currency, }, { 'description': self.rec_name, 'account': journal.debit_account.id, 'debit': amount, 'credit': Decimal('0.0'), 'amount_second_currency': amount_second_currency, 'second_currency': second_currency, }] move, = Move.create([{ 'journal': journal.id, 'period': period_id, 'date': date, 'lines': [('create', lines)], 'origin': '%s,%d' % (self.__name__, self.id), }]) Move.post([move]) # Set the move as the move of this transaction self.move = move self.save() return move
def get_move_line(self): ''' Return the move line for the statement line ''' pool = Pool() MoveLine = pool.get('account.move.line') Currency = Pool().get('currency.currency') zero = Decimal("0.0") with Transaction().set_context(date=self.date): amount = Currency.compute(self.statement.journal.currency, self.amount, self.statement.company.currency) if self.statement.journal.currency != self.statement.company.currency: second_currency = self.statement.journal.currency.id amount_second_currency = -self.amount else: amount_second_currency = None second_currency = None return MoveLine( description=self.description, debit=amount < zero and -amount or zero, credit=amount >= zero and amount or zero, account=self.account, party=self.party if self.account.party_required else None, second_currency=second_currency, amount_second_currency=amount_second_currency, )
def on_change_with_amount(self): Tax = Pool().get('account.tax') if self.tax and self.manual: tax = self.tax base = self.base or Decimal(0) for values in Tax.compute([tax], base, 1): if (values['tax'] == tax and values['base'] == base): return values['amount'] return self.amount
def get_cost_price_with_tax(self): Tax = Pool().get('account.tax') if self.cost_price: if self.taxes_category and not self.category: return None taxes = [Tax(t) for t in self.get_taxes('supplier_taxes_used')] taxes = Tax.compute(taxes, self.cost_price, 1.0) tax_amount = sum([t['amount'] for t in taxes], Decimal('0.0')) return self.cost_price + tax_amount
def get_move_line(self): ''' Return a list of move lines values for withholding tax ''' Currency = Pool().get('currency.currency') res = {} if not self.amount: return [] res['description'] = self.description if self.withholding.currency != self.withholding.company.currency: with Transaction().set_context( date=self.withholding.currency_date): amount = Currency.compute(self.withholding.currency, self.amount, self.withholding.company.currency) res['amount_second_currency'] = self.amount res['second_currency'] = self.withholding.currency.id else: amount = self.amount res['amount_second_currency'] = None res['second_currency'] = None if self.withholding.type in ('in_withholding', 'out_credit_note'): if amount >= Decimal('0.0'): res['debit'] = amount res['credit'] = Decimal('0.0') else: res['debit'] = Decimal('0.0') res['credit'] = -amount if res['amount_second_currency']: res['amount_second_currency'] = \ - res['amount_second_currency'] else: if amount >= Decimal('0.0'): res['debit'] = Decimal('0.0') res['credit'] = amount if res['amount_second_currency']: res['amount_second_currency'] = \ - res['amount_second_currency'] else: res['debit'] = -amount res['credit'] = Decimal('0.0') res['account'] = self.account.id if self.account.party_required: res['party'] = self.withholding.party.id if self.tax_code: res['tax_lines'] = [('create', [{ 'code': self.tax_code.id, 'amount': amount * self.tax_sign, 'tax': self.tax and self.tax.id or None }])] return [res]
def get_value(self, remainder, amount, currency): Currency = Pool().get('currency.currency') if self.type == 'fixed': return Currency.compute(self.currency, self.amount, currency) elif self.type == 'percent': return currency.round(remainder * self.ratio) elif self.type == 'percent_on_total': return currency.round(amount * self.ratio) elif self.type == 'remainder': return currency.round(remainder) return None
def get_value(self, remainder, amount, currency): Currency = Pool().get('currency.currency') if self.type == 'fixed': return Currency.compute(self.currency, self.amount, currency) elif self.type == 'percent': return currency.round(remainder * self.ratio) elif self.type == 'percent_on_total': return currency.round(amount * self.ratio) elif self.type == 'remainder': return currency.round(remainder) return None
def get_value(self, remainder, amount, currency): Currency = Pool().get("currency.currency") if self.type == "fixed": return Currency.compute(self.currency, self.amount, currency) elif self.type == "percent": return currency.round(remainder * self.percentage / Decimal("100")) elif self.type == "percent_on_total": return currency.round(amount * self.percentage / Decimal("100")) elif self.type == "remainder": return currency.round(remainder) return None
def _get_move_lines(self): ''' Return the move lines for the statement line ''' pool = Pool() MoveLine = pool.get('account.move.line') Currency = Pool().get('currency.currency') zero = Decimal("0.0") with Transaction().set_context(date=self.date): amount = Currency.compute(self.statement.journal.currency, self.amount, self.statement.company.currency) if self.statement.journal.currency != self.statement.company.currency: second_currency = self.statement.journal.currency.id amount_second_currency = abs(self.amount) else: amount_second_currency = None second_currency = None move_lines = [] move_lines.append(MoveLine( description=self.description, debit=amount < zero and -amount or zero, credit=amount >= zero and amount or zero, account=self.account, party=self.party if self.account.party_required else None, second_currency=second_currency, amount_second_currency=amount_second_currency, )) journal = self.statement.journal.journal if self.amount >= zero: account = journal.debit_account else: account = journal.credit_account if not account: self.raise_user_error('debit_credit_account_statement_journal', (journal.rec_name,)) if self.account == account: self.raise_user_error('same_debit_credit_account', { 'account': self.account.rec_name, 'line': self.rec_name, 'journal': journal, }) move_lines.append(MoveLine( description=self.description, debit=amount >= zero and amount or zero, credit=amount < zero and -amount or zero, account=account, party=self.party if account.party_required else None, second_currency=second_currency, amount_second_currency=amount_second_currency, )) return move_lines
def apply_ups_shipping(self): "Add a shipping line to sale for ups" Currency = Pool().get("currency.currency") if self.is_ups_shipping: with Transaction().set_context(self._get_carrier_context()): shipment_cost, currency_id = self.carrier.get_sale_price() if not shipment_cost: return # Convert the shipping cost to sale currency from USD shipment_cost = Currency.compute(Currency(currency_id), shipment_cost, self.currency) self.add_shipping_line(shipment_cost, "%s - %s" % (self.carrier.party.name, self.ups_service_type.name))
def create_using_amazon_data(cls, product_data): """ Create a new product with the `product_data` from amazon. :param product_data: Product Data from Amazon :returns: Active record of product created """ Template = Pool().get('product.template') Currency = Pool().get('currency.currency') SaleChannel = Pool().get('sale.channel') # TODO: Handle attribute sets in multiple languages product_attribute_set = product_data['Products']['Product'][ 'AttributeSets' ] if isinstance(product_attribute_set, dict): product_attributes = product_attribute_set['ItemAttributes'] else: product_attributes = product_attribute_set[0]['ItemAttributes'] product_values = cls.extract_product_values_from_amazon_data( product_attributes ) amazon_channel = SaleChannel( Transaction().context['current_channel'] ) assert amazon_channel.source == 'amazon_mws' list_price = Decimal('0.01') if product_attributes.get('ListPrice'): list_price = product_attributes['ListPrice']['Amount']['value'] currency_code = product_attributes['ListPrice']['CurrencyCode']['value'] # noqa currency, = Currency.search([ ('code', '=', currency_code), ], limit=1) list_price = Currency.compute( currency, Decimal(list_price), amazon_channel.company.currency ) product_values.update({ 'products': [('create', [{ 'code': product_data['Id']['value'], 'list_price': list_price, 'cost_price': list_price, 'description': product_attributes['Title']['value'], }])], }) product_template, = Template.create([product_values]) return product_template.products[0]
def get_other_taxes(cls, invoice): Currency = Pool().get('currency.currency') amount = Decimal('0') for invoice_tax in invoice.taxes: if (invoice_tax.tax.group and invoice_tax.tax.group.code.lower() not in ['iibb', 'iva']): tax_amount = invoice_tax.amount if invoice.currency.id != invoice.company.currency.id: amount += Currency.compute( invoice.currency, tax_amount, invoice.company.currency) else: amount += invoice.currency.round(tax_amount) return amount
def apply_product_shipping(self): """ This method apply product(carrier) shipping. """ Currency = Pool().get('currency.currency') with Transaction().set_context(self._get_carrier_context()): shipment_cost, currency_id = self.carrier.get_sale_price() shipment_cost = Currency.compute( Currency(currency_id), shipment_cost, self.currency ) self.add_shipping_line(shipment_cost, self.carrier.rec_name)
def get_sum_neto_by_iva_condition(cls, iva_condition, invoices): Currency = Pool().get('currency.currency') amount = Decimal('0') for invoice in invoices: if invoice.party.iva_condition == iva_condition: untaxed_amount = invoice.untaxed_amount if invoice.currency.id != invoice.company.currency.id: amount += Currency.compute( invoice.currency, untaxed_amount, invoice.company.currency) else: amount += invoice.currency.round(untaxed_amount) return amount
def get_sum_neto_by_tax(cls, tax, invoices): Currency = Pool().get('currency.currency') amount = Decimal('0') for invoice in invoices: for invoice_tax in invoice.taxes: if invoice_tax.tax == tax: untaxed_amount = invoice_tax.base if invoice.currency.id != invoice.company.currency.id: amount += Currency.compute( invoice.currency, untaxed_amount, invoice.company.currency) else: amount += invoice.currency.round(untaxed_amount) return amount
def get_iva(cls, invoice, rate, group_tax='IVA'): Currency = Pool().get('currency.currency') amount = Decimal('0') for invoice_tax in invoice.taxes: if (invoice_tax.tax.rate and invoice_tax.tax.rate == Decimal(rate) and invoice_tax.tax.group and group_tax.lower() in invoice_tax.tax.group.code.lower()): tax_amount = invoice_tax.amount if invoice.currency.id != invoice.company.currency.id: amount += Currency.compute( invoice.currency, tax_amount, invoice.company.currency) else: amount += invoice.currency.round(tax_amount) return amount
def get_sum_percibido_by_tax(cls, tax, invoices): Currency = Pool().get('currency.currency') amount = Decimal('0') for invoice in invoices: for invoice_tax in invoice.taxes: if invoice_tax.tax == tax: tax_amount = invoice_tax.amount if invoice.currency.id != invoice.company.currency.id: amount += Currency.compute( invoice.currency, tax_amount, invoice.company.currency) else: amount += invoice.currency.round(tax_amount) return amount
def get_beanstream_transaction_dict(self): """ Returns a dictionary of variables as required for the beanstream request """ Currency = Pool().get('currency.currency') res = { 'requestType': 'BACKEND', 'trnOrderNumber': self.uuid, 'trnAmount': str(Currency.compute( self.currency, self.amount, self.gateway.beanstream_currency) ), } return res
def apply_ups_shipping(self): "Add a shipping line to sale for ups" Currency = Pool().get('currency.currency') if self.is_ups_shipping: with Transaction().set_context(self._get_carrier_context()): shipment_cost, currency_id = self.carrier.get_sale_price() if not shipment_cost: return # Convert the shipping cost to sale currency from USD shipment_cost = Currency.compute(Currency(currency_id), shipment_cost, self.currency) self.add_shipping_line( shipment_cost, "%s - %s" % (self.carrier.party.name, self.ups_service_type.name))
def get_sum_neto_by_tax_and_subdivision(cls, tax, subdivision, invoices): Currency = Pool().get('currency.currency') amount = Decimal('0') for invoice in invoices: if invoice.invoice_address.subdivision == subdivision: for invoice_tax in invoice.taxes: if invoice_tax.tax == tax: untaxed_amount = invoice.untaxed_amount if invoice.currency.id != invoice.company.currency.id: amount += Currency.compute( invoice.currency, untaxed_amount, invoice.company.currency) else: amount += invoice.currency.round(untaxed_amount) return amount
def compute_cost(self): Currency = Pool().get('currency.currency') cost_price = self.employee.compute_cost_price(date=self.date) line_company = self.employee.company work_company = self.work.company if (line_company != work_company and line_company.currency != work_company.currency): with Transaction().set_context(date=self.date): cost_price = Currency.compute(line_company.currency, cost_price, work_company.currency) return Decimal(str(self.hours)) * cost_price
def get_sum_percibido_by_iva_condition(cls, iva_condition, invoices): Currency = Pool().get('currency.currency') amount = Decimal('0') for invoice in invoices: if invoice.party.iva_condition == iva_condition: for invoice_tax in invoice.taxes: if (invoice_tax.tax.group and 'iva' in invoice_tax.tax.group.code.lower()): tax_amount = invoice_tax.amount if invoice.currency.id != invoice.company.currency.id: amount += Currency.compute( invoice.currency, tax_amount, invoice.company.currency) else: amount += invoice.currency.round(tax_amount) return amount
def on_change_with_amount(self): Tax = Pool().get('account.tax') transaction = Transaction() company = transaction.context['company'] Company = Pool().get('company.company') companies = Company.search([('id', '=', company)]) for c in companies: company = c if self.tax and self.manual: tax = self.tax base = self.base or Decimal(0) for values in Tax.compute([tax], base, 1): if (values['tax'] == tax and values['base'] == base): amount = company.currency.round(values['amount']) return amount return company.currency.round(self.amount)
def apply_endicia_shipping(self): "Add a shipping line to sale for endicia" Currency = Pool().get('currency.currency') if self.carrier and self.carrier.carrier_cost_method == 'endicia': if not self.endicia_mailclass: self.raise_user_error('mailclass_missing') with Transaction().set_context(self._get_carrier_context()): shipment_cost_usd = self.carrier.get_sale_price() if not shipment_cost_usd[0]: return # Convert the shipping cost to sale currency from USD usd, = Currency.search([('code', '=', 'USD')]) shipment_cost = Currency.compute(usd, shipment_cost_usd[0], self.currency) self.add_shipping_line( shipment_cost, '%s - %s' % (self.carrier.party.name, self.endicia_mailclass.name))
def get_beanstream_transaction_dict(self): """ Returns a dictionary of variables as required for the beanstream request """ Currency = Pool().get('currency.currency') res = { 'requestType': 'BACKEND', 'trnOrderNumber': self.uuid, 'trnAmount': str( Currency.compute(self.currency, self.amount, self.gateway.beanstream_currency)), } return res
def get_secondary_amount(cls, invoice, value): Currency = Pool().get('currency.currency') if invoice.pos and invoice.pos.pos_type == 'electronic': afip_tr, = [ tr for tr in invoice.transactions if tr.pyafipws_result == 'A' ] request = SimpleXMLElement(unidecode(afip_tr.pyafipws_xml_request)) if invoice.pos.pyafipws_electronic_invoice_service == 'wsfex': ctz = Decimal(str(request('Moneda_ctz'))) elif invoice.pos.pyafipws_electronic_invoice_service == 'wsfe': ctz = Decimal(str(request('MonCotiz'))) currency_rate = invoice.currency_rate or ctz context = dict(date=invoice.currency_date) if currency_rate: context['currency_rate'] = currency_rate with Transaction().set_context(context): amount = Currency.compute(invoice.currency, value, invoice.company.currency) return amount
def get_move_line(self, period): ''' Return the move line for the receipt line ''' pool = Pool() MoveLine = pool.get('account.move.line') Currency = Pool().get('currency.currency') zero = Decimal('0.0') debit = Decimal('0.0') credit = Decimal('0.0') with Transaction().set_context(date=self.receipt.date): amount = Currency.compute(self.receipt.currency, self.amount, self.receipt.company.currency) if self.receipt.currency != self.receipt.company.currency: second_currency = self.receipt.currency amount_second_currency = self.amount else: amount_second_currency = None second_currency = None if self.receipt.type.type == 'in': if amount > zero: credit = amount else: debit = abs(amount) else: if amount > zero: debit = amount else: credit = abs(amount) return MoveLine( period=period, description=self.description, debit=debit, credit=credit, account=self.account, party=self.party if self.account.party_required else None, second_currency=second_currency, amount_second_currency=amount_second_currency, )
def apply_shipping_rate(self, rate): """ This method applies shipping rate. Rate is a dictionary with following minimum keys: { 'display_name': Name to display, 'carrier_service': carrier.service active record, 'cost': cost, 'cost_currency': currency.currency active record, 'carrier': carrier active record, } It also creates a shipment line by deleting all existing ones. The rate could optionally have integer ids of service, carrier and currency. """ Currency = Pool().get('currency.currency') Carrier = Pool().get('carrier') CarrierService = Pool().get('carrier.service') self.carrier = Carrier(int(rate['carrier'])) self.carrier_service = CarrierService(int(rate['carrier_service'])) \ if rate['carrier_service'] else None self.save() cost_currency = Currency(int(rate['cost_currency'])) shipment_cost = cost_currency.round(rate['cost']) if self.currency != cost_currency: shipment_cost = Currency.compute(cost_currency, shipment_cost, self.currency) self.add_shipping_line( shipment_cost, rate['display_name'], self.carrier, self.carrier_service, )
def apply_endicia_shipping(self): "Add a shipping line to sale for endicia" Sale = Pool().get('sale.sale') Currency = Pool().get('currency.currency') if self.carrier and self.carrier.carrier_cost_method == 'endicia': if not self.endicia_mailclass: self.raise_user_error('mailclass_missing') with Transaction().set_context(self._get_carrier_context()): shipment_cost_usd = self.carrier.get_sale_price() if not shipment_cost_usd[0]: return # Convert the shipping cost to sale currency from USD usd, = Currency.search([('code', '=', 'USD')]) shipment_cost = Currency.compute(usd, shipment_cost_usd[0], self.currency) Sale.write( [self], { 'lines': [ ( 'create', [{ 'type': 'line', 'product': self.carrier.carrier_product.id, 'description': self.endicia_mailclass.name, 'quantity': 1, # XXX 'unit': self.carrier.carrier_product.sale_uom.id, 'unit_price': Decimal(shipment_cost), 'shipment_cost': Decimal(shipment_cost), 'amount': Decimal(shipment_cost), 'taxes': [], 'sequence': 9999, # XXX }]), ('delete', [line for line in self.lines if line.shipment_cost]), ] })
def apply_endicia_shipping(self): "Add a shipping line to sale for endicia" Currency = Pool().get('currency.currency') if self.carrier and self.carrier.carrier_cost_method == 'endicia': if not self.endicia_mailclass: self.raise_user_error('mailclass_missing') with Transaction().set_context(self._get_carrier_context()): shipment_cost_usd = self.carrier.get_sale_price() if not shipment_cost_usd[0]: return # Convert the shipping cost to sale currency from USD usd, = Currency.search([('code', '=', 'USD')]) shipment_cost = Currency.compute( usd, shipment_cost_usd[0], self.currency ) self.add_shipping_line( shipment_cost, '%s - %s' % ( self.carrier.party.name, self.endicia_mailclass.name ) )
def get_sale_price_w_tax(self, price=Decimal('0.0'), party=None): ''' Calculate price with taxes from carrier product ''' Tax = Pool().get('account.tax') taxes = self.carrier_product.customer_taxes_used if taxes and party and party.customer_tax_rule: new_taxes = [] for tax in taxes: tax_ids = party.customer_tax_rule.apply(tax, pattern={}) new_taxes = new_taxes + tax_ids if new_taxes: taxes = Tax.browse(new_taxes) taxes = Tax.compute(taxes, price, 1) tax_amount = 0 for tax in taxes: tax_amount += tax['amount'] price = price + tax_amount price.quantize(Decimal(str(10.0 ** - DIGITS))) return price
def update_pricelist_shipment_cost(self): "Add a shipping line to sale for pricelist costmethod" Sale = Pool().get('sale.sale') Currency = Pool().get('currency.currency') if not self.carrier or self.carrier.carrier_cost_method != 'pricelist': return with Transaction().set_context(self._get_carrier_context()): shipment_cost = self.carrier.get_sale_price() if not shipment_cost[0]: return shipment_cost = Currency.compute(Currency(shipment_cost[1]), shipment_cost[0], self.currency) Sale.write( [self], { 'lines': [ ( 'create', [{ 'type': 'line', 'product': self.carrier.carrier_product.id, 'description': self.carrier.carrier_product.name, 'quantity': 1, # XXX 'unit': self.carrier.carrier_product.sale_uom.id, 'unit_price': Decimal(shipment_cost), 'shipment_cost': Decimal(shipment_cost), 'amount': Decimal(shipment_cost), 'taxes': [], 'sequence': 9999, # XXX }]), ('delete', [line for line in self.lines if line.shipment_cost]), ] })
def apply_endicia_shipping(self): "Add a shipping line to sale for endicia" Sale = Pool().get('sale.sale') Currency = Pool().get('currency.currency') if self.carrier and self.carrier.carrier_cost_method == 'endicia': if not self.endicia_mailclass: self.raise_user_error('mailclass_missing') with Transaction().set_context(self._get_carrier_context()): shipment_cost_usd = self.carrier.get_sale_price() if not shipment_cost_usd[0]: return # Convert the shipping cost to sale currency from USD usd, = Currency.search([('code', '=', 'USD')]) shipment_cost = Currency.compute( usd, shipment_cost_usd[0], self.currency ) Sale.write([self], { 'lines': [ ('create', [{ 'type': 'line', 'product': self.carrier.carrier_product.id, 'description': self.endicia_mailclass.name, 'quantity': 1, # XXX 'unit': self.carrier.carrier_product.sale_uom.id, 'unit_price': Decimal(shipment_cost), 'shipment_cost': Decimal(shipment_cost), 'amount': Decimal(shipment_cost), 'taxes': [], 'sequence': 9999, # XXX }]), ('delete', [ line for line in self.lines if line.shipment_cost ]), ] })
def apply_ups_shipping(self): "Add a shipping line to sale for ups" Sale = Pool().get('sale.sale') Currency = Pool().get('currency.currency') if self.is_ups_shipping: with Transaction().set_context(self._get_carrier_context()): shipment_cost, currency_id = self.carrier.get_sale_price() if not shipment_cost: return # Convert the shipping cost to sale currency from USD shipment_cost = Currency.compute(Currency(currency_id), shipment_cost, self.currency) Sale.write( [self], { 'lines': [ ( 'create', [{ 'type': 'line', 'product': self.carrier.carrier_product.id, 'description': self.ups_service_type.name, 'quantity': 1, # XXX 'unit': self.carrier.carrier_product.sale_uom.id, 'unit_price': shipment_cost, 'shipment_cost': shipment_cost, 'amount': shipment_cost, 'taxes': [], 'sequence': 9999, # XXX }]), ('delete', [line for line in self.lines if line.shipment_cost]), ] })
def create_move(self, date=None): """ Create the account move for the payment :param date: Optional date for the account move :return: Active record of the created move """ Currency = Pool().get('currency.currency') Period = Pool().get('account.period') Move = Pool().get('account.move') journal = self.gateway.journal date = date or self.date if not journal.debit_account: self.raise_user_error('missing_debit_account', (journal.rec_name, )) period_id = Period.find(self.company.id, date=date) amount_second_currency = second_currency = None amount = self.amount if self.currency != self.company.currency: amount = Currency.compute(self.currency, self.amount, self.company.currency) amount_second_currency = self.amount second_currency = self.currency refund = self.type == 'refund' lines = [{ 'description': self.rec_name, 'account': self.credit_account.id, 'party': self.party.id, 'debit': Decimal('0.0') if not refund else amount, 'credit': Decimal('0.0') if refund else amount, 'amount_second_currency': amount_second_currency, 'second_currency': second_currency, }, { 'description': self.rec_name, 'account': journal.debit_account.id, 'debit': Decimal('0.0') if refund else amount, 'credit': Decimal('0.0') if not refund else amount, 'amount_second_currency': amount_second_currency, 'second_currency': second_currency, }] move, = Move.create([{ 'journal': journal.id, 'period': period_id, 'date': date, 'lines': [('create', lines)], 'origin': '%s,%d' % (self.__name__, self.id), }]) Move.post([move]) # Set the move as the move of this transaction self.move = move self.save() return move
def _get_counterpart_move_lines(self, line): pool = Pool() MoveLine = pool.get('account.move.line') Currency = Pool().get('currency.currency') # Generate counterpart line move_lines = [] counterpart = MoveLine() counterpart.journal = self.journal.journal counterpart.description = line.description counterpart.debit = line.credit counterpart.credit = line.debit counterpart.account = line.account if line.account.party_required: counterpart.party = line.party counterpart.origin = str(self) amount = line.debit - line.credit amount_second_currency = None second_currency = None if self.statement_currency != self.company_currency: with Transaction().set_context(date=self.date.date()): amount_second_currency = abs( Currency.compute(self.company_currency, amount, self.statement_currency)) second_currency = self.statement_currency counterpart.amount_second_currency = ( amount_second_currency * (-1 if line.debit - line.credit > 0 else 1)) counterpart.second_currency = second_currency # Generate Bank Line. journal = self.journal account = journal.account if not account: raise UserError( gettext( 'account_bank_statement_counterpart.account_statement_journal', journal=journal.rec_name)) if not account.bank_reconcile: raise UserError( gettext( 'account_bank_statement_counterpart.account_not_bank_reconcile', journal=journal.rec_name)) if line.account == account: raise UserError( gettext('account_bank_statement_counterpart.same_account', account=line.account.rec_name, line=line.rec_name, journal=journal.rec_name)) bank_move = MoveLine( journal=journal.journal, description=self.description, debit=amount >= _ZERO and amount or _ZERO, credit=amount < _ZERO and -amount or _ZERO, account=account, origin=self, move_origin=self.statement, second_currency=second_currency, amount_second_currency=amount_second_currency, ) if account.party_required: bank_move.party = line.party or self.company.party move_lines.append(bank_move) move_lines.append(counterpart) return move_lines
def get_balance(cls, accounts, name): res = {} Line = Pool().get('analytic_account.line') Currency = Pool().get('currency.currency') cursor = Transaction().cursor ids = [a.id for a in accounts] childs = cls.search([('parent', 'child_of', ids)]) all_ids = {}.fromkeys(ids + [c.id for c in childs]).keys() id2account = {} all_accounts = cls.browse(all_ids) for account in all_accounts: id2account[account.id] = account line_query = Line.query_get() cursor.execute('SELECT a.id, ' 'SUM((COALESCE(l.debit, 0) - COALESCE(l.credit, 0))), ' 'c.currency ' 'FROM analytic_account_account a ' 'LEFT JOIN analytic_account_line l ' 'ON (a.id = l.account) ' 'LEFT JOIN account_move_line ml ' 'ON (ml.id = l.move_line) ' 'LEFT JOIN account_account aa ' 'ON (aa.id = ml.account) ' 'LEFT JOIN company_company c ' 'ON (c.id = aa.company) ' 'WHERE a.type != \'view\' ' 'AND a.id IN (' + ','.join(('%s',) * len(all_ids)) + ') ' 'AND ' + line_query + ' ' 'AND a.active ' 'GROUP BY a.id, c.currency', all_ids) account_sum = {} id2currency = {} for account_id, sum, currency_id in cursor.fetchall(): account_sum.setdefault(account_id, Decimal('0.0')) # SQLite uses float for SUM if not isinstance(sum, Decimal): sum = Decimal(str(sum)) if currency_id != id2account[account_id].currency.id: currency = None if currency_id in id2currency: currency = id2currency[currency_id] else: currency = Currency(currency_id) id2currency[currency.id] = currency account_sum[account_id] += Currency.compute(currency, sum, id2account[account_id].currency, round=True) else: account_sum[account_id] += \ id2account[account_id].currency.round(sum) for account_id in ids: res.setdefault(account_id, Decimal('0.0')) childs = cls.search([ ('parent', 'child_of', [account_id]), ]) to_currency = id2account[account_id].currency for child in childs: from_currency = id2account[child.id].currency res[account_id] += Currency.compute(from_currency, account_sum.get(child.id, Decimal('0.0')), to_currency, round=True) res[account_id] = to_currency.round(res[account_id]) if id2account[account_id].display_balance == 'credit-debit': res[account_id] = - res[account_id] return res