def _get_mts_mto_rule(self): self.ensure_one() route_model = self.env['stock.location.route'] pull_model = self.env['procurement.rule'] try: mts_mto_route = self.env.ref('stock_mts_mto_rule.route_mto_mts') except: mts_mto_route = route_model.search([ ('name', 'like', 'Make To Order + Make To Stock') ]) if not mts_mto_route: raise exceptions.Warning( _('Can\'t find any generic MTS+MTO route.')) if not self.mto_pull_id: raise exceptions.Warning( _('Can\'t find MTO Rule on the warehouse')) mts_rules = pull_model.search([ ('location_src_id', '=', self.lot_stock_id.id), ('route_id', '=', self.delivery_route_id.id) ]) if not mts_rules: raise exceptions.Warning( _('Can\'t find MTS Rule on the warehouse')) return { 'name': self._format_routename(route_type='mts_mto'), 'route_id': mts_mto_route.id, 'action': 'split_procurement', 'mto_rule_id': self.mto_pull_id.id, 'mts_rule_id': mts_rules[0].id, 'warehouse_id': self.id, 'location_id': self.mto_pull_id.location_id.id, 'picking_type_id': self.mto_pull_id.picking_type_id.id, }
def _unique_date_range(self): # Based in l10n_es_aeat module domain = [('id', '!=', self.id)] if self.date_from and self.date_to: domain += [ '|', '&', ('date_from', '<=', self.date_to), ('date_from', '>=', self.date_from), '|', '&', ('date_to', '<=', self.date_to), ('date_to', '>=', self.date_from), '|', '&', ('date_from', '=', False), ('date_to', '>=', self.date_from), '|', '&', ('date_to', '=', False), ('date_from', '<=', self.date_to), ] elif self.date_from: domain += [('date_to', '>=', self.date_from)] elif self.date_to: domain += [('date_from', '<=', self.date_to)] date_lst = self.search(domain) if date_lst: raise exceptions.Warning( _("Error! The dates of the record overlap with an existing " "record."))
def create_xml(self): """Creates xml that is to be exported and sent to estate for partner vat intra. :return: Value for next action. :rtype: dict """ self.ensure_one() company = self.company_id if not (company.partner_id and company.partner_id.country_id and company.partner_id.country_id.id): self._company_warning(_('The country of your company is not set, ' 'please make sure to configure it first.')) if not company.company_registry: self._company_warning(_('The registry number of your company is not set, ' 'please make sure to configure it first.')) if len(self.year) != 4: raise exceptions.Warning(_('Year must be 4 digits number (YYYY)')) #Create root declaration decl = ET.Element('DeclarationReport') decl.set('xmlns', INTRASTAT_XMLNS) #Add Administration elements admin = ET.SubElement(decl, 'Administration') fromtag = ET.SubElement(admin, 'From') fromtag.text = company.company_registry fromtag.set('declarerType', 'KBO') ET.SubElement(admin, 'To').text = "NBB" ET.SubElement(admin, 'Domain').text = "SXX" if self.arrivals == 'be-standard': decl.append(self.sudo()._get_lines(dispatchmode=False, extendedmode=False)) elif self.arrivals == 'be-extended': decl.append(self.sudo()._get_lines(dispatchmode=False, extendedmode=True)) if self.dispatches == 'be-standard': decl.append(self.sudo()._get_lines(dispatchmode=True, extendedmode=False)) elif self.dispatches == 'be-extended': decl.append(self.sudo()._get_lines(dispatchmode=True, extendedmode=True)) #Get xml string with declaration data_file = ET.tostring(decl, encoding='UTF-8', method='xml') #change state of the wizard self.write({'name': 'intrastat_%s%s.xml' % (self.year, self.month), 'file_save': base64.encodestring(data_file), 'state': 'download'}) return { 'name': _('Save'), 'view_type': 'form', 'view_mode': 'form', 'res_model': 'l10n_be_intrastat_xml.xml_decl', 'type': 'ir.actions.act_window', 'target': 'new', 'res_id': self.id, }
def print_document(self, record_ids, data=None): """ Print a document, do not return the document file """ document, doc_format = self.with_context( must_skip_send_to_printer=True).render_qweb_pdf( record_ids, data=data) behaviour = self.behaviour() printer = behaviour.pop('printer', None) if not printer: raise exceptions.Warning( _('No printer configured to print this report.') ) # TODO should we use doc_format instead of report_type return printer.print_document(self, document, doc_format=self.report_type, **behaviour)
def write(self, vals): res = super(Task, self).write(vals) for task in self: if 'stage_id' in vals or 'material_ids' in vals: if task.stage_id.consume_material: todo_lines = task.material_ids.filtered( lambda m: not m.stock_move_id) if todo_lines: todo_lines.create_stock_move() todo_lines.create_analytic_line() else: if task.unlink_stock_move(): if task.material_ids.mapped('analytic_line_id'): raise exceptions.Warning( _("You can't move to a not consume stage if " "there are already analytic lines")) task.material_ids.mapped('analytic_line_id').unlink() return res
def _prepare_mrp_move_data_bom_explosion(self, product, bomline, qty, mrp_date_demand_2, bom, name): product_mrp_area = self._get_product_mrp_area_from_product_and_area( bomline.product_id, product.mrp_area_id) if not product_mrp_area: raise exceptions.Warning(_("No MRP product found")) return { 'mrp_area_id': product.mrp_area_id.id, 'product_id': bomline.product_id.id, 'product_mrp_area_id': product_mrp_area.id, 'production_id': None, 'purchase_order_id': None, 'purchase_line_id': None, 'stock_move_id': None, 'mrp_qty': -(qty * bomline.product_qty), # TODO: review with UoM 'current_qty': None, 'mrp_date': mrp_date_demand_2, 'current_date': None, 'mrp_type': 'd', 'mrp_origin': 'mrp', 'mrp_order_number': None, 'parent_product_id': bom.product_id.id, 'name': ('Demand Bom Explosion: ' + name).replace( 'Demand Bom Explosion: Demand Bom ' 'Explosion: ', 'Demand Bom Explosion: '), }
def _get_intrastat_linekey(self, declcode, inv_line, dispatchmode, extendedmode): IntrastatRegion = self.env['l10n_be_intrastat.region'] company = self.company_id #Check type of transaction if inv_line.intrastat_transaction_id: extta = inv_line.intrastat_transaction_id.code else: extta = "1" #Check country if inv_line.invoice_id.intrastat_country_id: excnt = inv_line.invoice_id.intrastat_country_id.code else: excnt = inv_line.invoice_id.partner_shipping_id.country_id.code or inv_line.invoice_id.partner_id.country_id.code #Check region #If purchase, comes from purchase order, linked to a location, #which is linked to the warehouse #if sales, the sales order is linked to the warehouse #if sales, from a delivery order, linked to a location, #which is linked to the warehouse #If none found, get the company one. exreg = None if inv_line.invoice_id.type in ('in_invoice', 'in_refund'): #comes from purchase po_lines = self.env['purchase.order.line'].search([('invoice_lines', 'in', inv_line.id)], limit=1) if po_lines: if self._is_situation_triangular(company, po_line=po_lines): return location = self.env['stock.location'].browse(po_lines.order_id._get_destination_location()) region_id = self.env['stock.warehouse'].get_regionid_from_locationid(location) if region_id: exreg = IntrastatRegion.browse(region_id).code elif inv_line.invoice_id.type in ('out_invoice', 'out_refund'): #comes from sales so_lines = self.env['sale.order.line'].search([('invoice_lines', 'in', inv_line.id)], limit=1) if so_lines: if self._is_situation_triangular(company, so_line=so_lines): return saleorder = so_lines.order_id if saleorder and saleorder.warehouse_id and saleorder.warehouse_id.region_id: exreg = IntrastatRegion.browse(saleorder.warehouse_id.region_id.id).code if not exreg: if company.region_id: exreg = company.region_id.code else: self._company_warning(_('The Intrastat Region of the selected company is not set, ' 'please make sure to configure it first.')) #Check commodity codes intrastat_id = inv_line.product_id.get_intrastat_recursively() if intrastat_id: exgo = self.env['report.intrastat.code'].browse(intrastat_id).name else: raise exceptions.Warning( _('Product "%s" has no intrastat code, please configure it') % inv_line.product_id.display_name) #In extended mode, 2 more fields required if extendedmode: #Check means of transport if inv_line.invoice_id.transport_mode_id: extpc = inv_line.invoice_id.transport_mode_id.code elif company.transport_mode_id: extpc = company.transport_mode_id.code else: self._company_warning(_('The default Intrastat transport mode of your company ' 'is not set, please make sure to configure it first.')) #Check incoterm if inv_line.invoice_id.incoterm_id: exdeltrm = inv_line.invoice_id.incoterm_id.code elif company.incoterm_id: exdeltrm = company.incoterm_id.code else: self._company_warning(_('The default Incoterm of your company is not set, ' 'please make sure to configure it first.')) else: extpc = "" exdeltrm = "" intrastatkey = namedtuple("intrastatkey", ['EXTRF', 'EXCNT', 'EXTTA', 'EXREG', 'EXGO', 'EXTPC', 'EXDELTRM']) return intrastatkey(EXTRF=declcode, EXCNT=excnt, EXTTA=extta, EXREG=exreg, EXGO=exgo, EXTPC=extpc, EXDELTRM=exdeltrm)
def _get_lines(self, dispatchmode=False, extendedmode=False): company = self.company_id IntrastatRegion = self.env['l10n_be_intrastat.region'] if dispatchmode: mode1 = 'out_invoice' mode2 = 'in_refund' declcode = "29" else: mode1 = 'in_invoice' mode2 = 'out_refund' declcode = "19" decl = ET.Element('Report') if not extendedmode: decl.set('code', 'EX%sS' % declcode) else: decl.set('code', 'EX%sE' % declcode) decl.set('date', '%s-%s' % (self.year, self.month)) datas = ET.SubElement(decl, 'Data') if not extendedmode: datas.set('form', 'EXF%sS' % declcode) else: datas.set('form', 'EXF%sE' % declcode) datas.set('close', 'true') intrastatkey = namedtuple("intrastatkey", ['EXTRF', 'EXCNT', 'EXTTA', 'EXREG', 'EXGO', 'EXTPC', 'EXDELTRM']) entries = {} query = """ SELECT inv_line.id FROM account_invoice_line inv_line JOIN account_invoice inv ON inv_line.invoice_id=inv.id LEFT JOIN res_country ON res_country.id = inv.intrastat_country_id LEFT JOIN res_partner ON res_partner.id = inv.partner_id LEFT JOIN res_country countrypartner ON countrypartner.id = res_partner.country_id JOIN product_product ON inv_line.product_id=product_product.id JOIN product_template ON product_product.product_tmpl_id=product_template.id WHERE inv.state IN ('open','paid') AND inv.company_id=%s AND not product_template.type='service' AND (res_country.intrastat=true OR (inv.intrastat_country_id is NULL AND countrypartner.intrastat=true)) AND ((res_country.code IS NOT NULL AND not res_country.code=%s) OR (res_country.code is NULL AND countrypartner.code IS NOT NULL AND not countrypartner.code=%s)) AND inv.type IN (%s, %s) AND to_char(inv.date_invoice, 'YYYY')=%s AND to_char(inv.date_invoice, 'MM')=%s """ self.env.cr.execute(query, (company.id, company.partner_id.country_id.code, company.partner_id.country_id.code, mode1, mode2, self.year, self.month)) lines = self.env.cr.fetchall() invoicelines_ids = [rec[0] for rec in lines] invoicelines = self.env['account.invoice.line'].browse(invoicelines_ids) for inv_line in invoicelines: #Check type of transaction if inv_line.intrastat_transaction_id: extta = inv_line.intrastat_transaction_id.code else: extta = "1" #Check country if inv_line.invoice_id.intrastat_country_id: excnt = inv_line.invoice_id.intrastat_country_id.code else: excnt = inv_line.invoice_id.partner_id.country_id.code #Check region #If purchase, comes from purchase order, linked to a location, #which is linked to the warehouse #if sales, the sales order is linked to the warehouse #if sales, from a delivery order, linked to a location, #which is linked to the warehouse #If none found, get the company one. exreg = None if inv_line.invoice_id.type in ('in_invoice', 'in_refund'): #comes from purchase po_lines = self.env['purchase.order.line'].search([('invoice_lines', 'in', inv_line.id)], limit=1) if po_lines: if self._is_situation_triangular(company, po_line=po_lines): continue location = self.env['stock.location'].browse(po_lines.order_id._get_destination_location()) region_id = self.env['stock.warehouse'].get_regionid_from_locationid(location) if region_id: exreg = IntrastatRegion.browse(region_id).code elif inv_line.invoice_id.type in ('out_invoice', 'out_refund'): #comes from sales so_lines = self.env['sale.order.line'].search([('invoice_lines', 'in', inv_line.id)], limit=1) if so_lines: if self._is_situation_triangular(company, so_line=so_lines): continue saleorder = so_lines.order_id if saleorder and saleorder.warehouse_id and saleorder.warehouse_id.region_id: exreg = IntrastatRegion.browse(saleorder.warehouse_id.region_id.id).code if not exreg: if company.region_id: exreg = company.region_id.code else: self._company_warning(_('The Intrastat Region of the selected company is not set, ' 'please make sure to configure it first.')) #Check commodity codes intrastat_id = inv_line.product_id.get_intrastat_recursively() if intrastat_id: exgo = self.env['report.intrastat.code'].browse(intrastat_id).name else: raise exceptions.Warning( _('Product "%s" has no intrastat code, please configure it') % inv_line.product_id.display_name) #In extended mode, 2 more fields required if extendedmode: #Check means of transport if inv_line.invoice_id.transport_mode_id: extpc = inv_line.invoice_id.transport_mode_id.code elif company.transport_mode_id: extpc = company.transport_mode_id.code else: self._company_warning(_('The default Intrastat transport mode of your company ' 'is not set, please make sure to configure it first.')) #Check incoterm if inv_line.invoice_id.incoterm_id: exdeltrm = inv_line.invoice_id.incoterm_id.code elif company.incoterm_id: exdeltrm = company.incoterm_id.code else: self._company_warning(_('The default Incoterm of your company is not set, ' 'please make sure to configure it first.')) else: extpc = "" exdeltrm = "" linekey = intrastatkey(EXTRF=declcode, EXCNT=excnt, EXTTA=extta, EXREG=exreg, EXGO=exgo, EXTPC=extpc, EXDELTRM=exdeltrm) #We have the key #calculate amounts if inv_line.price_unit and inv_line.quantity: amount = inv_line.price_unit * inv_line.quantity else: amount = 0 weight = (inv_line.product_id.weight or 0.0) * \ inv_line.uom_id._compute_quantity(inv_line.quantity, inv_line.product_id.uom_id) if not inv_line.product_id.uom_id.category_id: supply_units = inv_line.quantity else: supply_units = inv_line.quantity * inv_line.uom_id.factor amounts = entries.setdefault(linekey, (0, 0, 0)) amounts = (amounts[0] + amount, amounts[1] + weight, amounts[2] + supply_units) entries[linekey] = amounts numlgn = 0 for linekey in entries: amounts = entries[linekey] if round(amounts[0], 0) == 0: continue numlgn += 1 item = ET.SubElement(datas, 'Item') self._set_Dim(item, 'EXSEQCODE', text_type(numlgn)) self._set_Dim(item, 'EXTRF', text_type(linekey.EXTRF)) self._set_Dim(item, 'EXCNT', text_type(linekey.EXCNT)) self._set_Dim(item, 'EXTTA', text_type(linekey.EXTTA)) self._set_Dim(item, 'EXREG', text_type(linekey.EXREG)) self._set_Dim(item, 'EXTGO', text_type(linekey.EXGO)) if extendedmode: self._set_Dim(item, 'EXTPC', text_type(linekey.EXTPC)) self._set_Dim(item, 'EXDELTRM', text_type(linekey.EXDELTRM)) self._set_Dim(item, 'EXTXVAL', text_type(round(amounts[0], 0)).replace(".", ",")) self._set_Dim(item, 'EXWEIGHT', text_type(round(amounts[1], 0)).replace(".", ",")) self._set_Dim(item, 'EXUNITS', text_type(round(amounts[2], 0)).replace(".", ",")) if numlgn == 0: #no datas datas.set('action', 'nihil') return decl