from decimal import Decimal from trytond.pool import PoolMeta class Invoice(metaclass=PoolMeta): __name__ = 'account.invoice' def _get_report_sale(self): # Bridge invoice templates to the originating sale so FODT files can # reuse stable sale.report_* properties instead of complex expressions. sales = list(self.sales or []) return sales[0] if sales else None def _get_report_sale_line(self): sale = self._get_report_sale() if sale and sale.lines: return sale.lines[0] def _get_report_lot(self): line = self._get_report_sale_line() if line and line.lots: for lot in line.lots: if lot.lot_type == 'physic': return lot return line.lots[0] def _get_report_shipment(self): lot = self._get_report_lot() if not lot: return None return ( getattr(lot, 'lot_shipment_in', None) or getattr(lot, 'lot_shipment_out', None) or getattr(lot, 'lot_shipment_internal', None) ) @property def report_address(self): sale = self._get_report_sale() if sale and sale.report_address: return sale.report_address if self.invoice_address and self.invoice_address.full_address: return self.invoice_address.full_address return '' @property def report_contract_number(self): sale = self._get_report_sale() if sale and sale.full_number: return sale.full_number return self.origins or '' @property def report_shipment(self): sale = self._get_report_sale() if sale and sale.report_shipment: return sale.report_shipment return self.description or '' @property def report_trader_initial(self): sale = self._get_report_sale() if sale and sale.trader: return self.trader.initial return '' @property def report_operator_initial(self): sale = self._get_report_sale() if sale and sale.operator: return self.operator.initial return '' @property def report_product_description(self): line = self._get_report_sale_line() if line and line.product: return line.product.description or '' return '' @property def report_crop_name(self): sale = self._get_report_sale() if sale and sale.crop: return sale.crop.name or '' return '' @property def report_attributes_name(self): line = self._get_report_sale_line() if line: return line.attributes_name or '' return '' @property def report_price(self): sale = self._get_report_sale() if sale and sale.report_price: return sale.report_price return '' @property def report_payment_date(self): sale = self._get_report_sale() if sale and sale.report_payment_date: return sale.report_payment_date return '' @property def report_payment_description(self): sale = self._get_report_sale() if sale and sale.payment_term: return sale.payment_term.description or '' if self.payment_term: return self.payment_term.description or '' return '' @property def report_nb_bale(self): sale = self._get_report_sale() if sale and sale.report_nb_bale: return sale.report_nb_bale return '' @property def report_gross(self): sale = self._get_report_sale() if sale and sale.report_gross != '': return sale.report_gross return '' @property def report_net(self): sale = self._get_report_sale() if sale and sale.report_net != '': return sale.report_net if self.lines: return self.lines[0].quantity return '' @property def report_lbs(self): net = self.report_net if net == '': return '' return Decimal(net) * Decimal('2.20462') @property def report_bl_date(self): shipment = self._get_report_shipment() if shipment: return shipment.bl_date @property def report_loading_port(self): shipment = self._get_report_shipment() if shipment and shipment.from_location: return shipment.from_location.rec_name return '' @property def report_discharge_port(self): shipment = self._get_report_shipment() if shipment and shipment.to_location: return shipment.to_location.rec_name return '' @property def report_incoterm(self): sale = self._get_report_sale() if not sale: return '' incoterm = sale.incoterm.code if sale.incoterm else '' location = sale.incoterm_location.party_name if sale.incoterm_location else '' if incoterm and location: return f"{incoterm} {location}" return incoterm or location @property def report_proforma_invoice_number(self): lot = self._get_report_lot() if lot and lot.sale_invoice_line_prov and lot.sale_invoice_line_prov.invoice: return lot.sale_invoice_line_prov.invoice.number or '' return '' @property def report_proforma_invoice_date(self): lot = self._get_report_lot() if lot and lot.sale_invoice_line_prov and lot.sale_invoice_line_prov.invoice: return lot.sale_invoice_line_prov.invoice.invoice_date @property def report_controller_name(self): shipment = self._get_report_shipment() if shipment and shipment.controller: return shipment.controller.rec_name return '' @property def report_si_number(self): shipment = self._get_report_shipment() if shipment: return shipment.number or '' return ''