def _get_invoiced(self): precision = self.env['decimal.precision'].precision_get( 'Product Unit of Measure') for order in self: if order.state not in ('purchase', 'done'): order.invoice_status = 'no' continue if any( float_compare( line.qty_invoiced, line.product_qty if line.product_id.purchase_method == 'purchase' else line.qty_received, precision_digits=precision) == -1 for line in order.order_line): order.invoice_status = 'to invoice' elif all( float_compare( line.qty_invoiced, line.product_qty if line.product_id.purchase_method == 'purchase' else line.qty_received, precision_digits=precision) >= 0 for line in order.order_line) and order.invoice_ids: order.invoice_status = 'invoiced' else: order.invoice_status = 'no'
def _create_or_update_picking(self): for line in self: if line.product_id.type in ('product', 'consu'): # Prevent decreasing below received quantity if float_compare(line.product_qty, line.qty_received, line.product_uom.rounding) < 0: raise UserError(_('You cannot decrease the ordered quantity below the received quantity.\n' 'Create a return first.')) if float_compare(line.product_qty, line.qty_invoiced, line.product_uom.rounding) == -1: # If the quantity is now below the invoiced quantity, create an activity on the vendor bill # inviting the user to create a refund. activity = self.env['mail.activity'].sudo().create({ 'activity_type_id': self.env.ref('mail.mail_activity_data_todo').id, 'note': _('The quantities on your purchase order indicate less than billed. You should ask for a refund. '), 'res_id': line.invoice_lines[0].invoice_id.id, 'res_model_id': self.env.ref('account.model_account_invoice').id, }) activity._onchange_activity_type_id() # If the user increased quantity of existing line or created a new line pickings = line.order_id.picking_ids.filtered(lambda x: x.state not in ('done', 'cancel') and x.location_dest_id.usage in ('internal', 'transit')) picking = pickings and pickings[0] or False if not picking: res = line.order_id._prepare_picking() picking = self.env['stock.picking'].create(res) move_vals = line._prepare_stock_moves(picking) for move_val in move_vals: self.env['stock.move']\ .create(move_val)\ ._action_confirm()\ ._action_assign()
def put_in_pack(self): picking_move_lines = self.picking_id.move_line_ids if not self.picking_id.picking_type_id.show_reserved: picking_move_lines = self.picking_id.move_line_nosuggest_ids move_line_ids = picking_move_lines.filtered(lambda ml: float_compare( ml.qty_done, 0.0, precision_rounding=ml.product_uom_id.rounding ) > 0 and not ml.result_package_id) if not move_line_ids: move_line_ids = picking_move_lines.filtered( lambda ml: float_compare(ml.product_uom_qty, 0.0, precision_rounding=ml.product_uom_id. rounding) > 0 and float_compare(ml.qty_done, 0.0, precision_rounding=ml.product_uom_id.rounding ) == 0) delivery_package = self.picking_id._put_in_pack(move_line_ids) # write shipping weight and product_packaging on 'stock_quant_package' if needed if self.delivery_packaging_id: delivery_package.packaging_id = self.delivery_packaging_id if self.shipping_weight: delivery_package.shipping_weight = self.shipping_weight
def _bkash_form_get_invalid_parameters(self, data): invalid_parameters = [] _logger.info('Received a notification from bKash with IPN version %s', data.get('notify_version')) if data.get('test_ipn'): _logger.warning( 'Received a notification from bKash using sandbox'), # TODO: txn_id: shoudl be false at draft, set afterwards, and verified with txn details if self.acquirer_reference and data.get( 'txn_id') != self.acquirer_reference: invalid_parameters.append( ('txn_id', data.get('txn_id'), self.acquirer_reference)) # check what is buyed if float_compare(float(data.get('mc_gross', '0.0')), (self.amount + self.fees), 2) != 0: invalid_parameters.append( ('mc_gross', data.get('mc_gross'), '%.2f' % self.amount)) # mc_gross is amount + fees if data.get('mc_currency') != self.currency_id.name: invalid_parameters.append(('mc_currency', data.get('mc_currency'), self.currency_id.name)) if 'handling_amount' in data and float_compare( float(data.get('handling_amount')), self.fees, 2) != 0: invalid_parameters.append( ('handling_amount', data.get('handling_amount'), self.fees)) # check buyer if self.payment_token_id and data.get( 'payer_id') != self.payment_token_id.acquirer_ref: invalid_parameters.append(('payer_id', data.get('payer_id'), self.payment_token_id.acquirer_ref)) # check seller if data.get( 'receiver_id' ) and self.acquirer_id.bkash_seller_account and data[ 'receiver_id'] != self.acquirer_id.bkash_seller_account: invalid_parameters.append(('receiver_id', data.get('receiver_id'), self.acquirer_id.bkash_seller_account)) if not data.get( 'receiver_id') or not self.acquirer_id.bkash_seller_account: # Check receiver_email only if receiver_id was not checked. # In bKash, this is possible to configure as receiver_email a different email than the business email (the login email) # In Eagle, there is only one field for the bKash email: the business email. This isn't possible to set a receiver_email # different than the business email. Therefore, if you want such a configuration in your bKash, you are then obliged to fill # the Merchant ID in the bKash payment acquirer in Eagle, so the check is performed on this variable instead of the receiver_email. # At least one of the two checks must be done, to avoid fraudsters. if data.get( 'receiver_email') != self.acquirer_id.bkash_email_account: invalid_parameters.append( ('receiver_email', data.get('receiver_email'), self.acquirer_id.bkash_email_account)) return invalid_parameters
def add_payment(self, data): statement_id = super(PosOrder, self).add_payment(data) statement_lines = self.env['account.bank.statement.line'].search([ ('statement_id', '=', statement_id), ('pos_statement_id', '=', self.id), ('journal_id', '=', data['journal']) ]) statement_lines = statement_lines.filtered(lambda line: float_compare( line.amount, data['amount'], precision_rounding=line.journal_currency_id.rounding) == 0) # we can get multiple statement_lines when there are >1 credit # card payments with the same amount. In that case it doesn't # matter which statement line we pick, just pick one that # isn't already used. for line in statement_lines: if not line.mercury_card_brand: line.mercury_card_brand = data.get('card_brand') line.mercury_card_number = data.get('card_number') line.mercury_card_owner_name = data.get('card_owner_name') line.mercury_ref_no = data.get('ref_no') line.mercury_record_no = data.get('record_no') line.mercury_invoice_no = data.get('invoice_no') break return statement_id
def _onchange_location_or_product_id(self): vals = {} # Once the new line is complete, fetch the new theoretical values. if self.product_id and self.location_id: # Sanity check if a lot has been set. if self.lot_id: if self.tracking == 'none' or self.product_id != self.lot_id.product_id: vals['lot_id'] = None quants = self._gather(self.product_id, self.location_id, lot_id=self.lot_id, package_id=self.package_id, owner_id=self.owner_id, strict=True) reserved_quantity = sum(quants.mapped('reserved_quantity')) quantity = sum(quants.mapped('quantity')) vals['reserved_quantity'] = reserved_quantity # Update `quantity` only if the user manually updated `inventory_quantity`. if float_compare( self.quantity, self.inventory_quantity, precision_rounding=self.product_uom_id.rounding) == 0: vals['quantity'] = quantity # Special case: directly set the quantity to one for serial numbers, # it'll trigger `inventory_quantity` compute. if self.lot_id and self.tracking == 'serial': vals['quantity'] = 1 if vals: self.update(vals)
def _set_inventory_quantity(self): """ Inverse method to create stock move when `inventory_quantity` is set (`inventory_quantity` is only accessible in inventory mode). """ if not self._is_inventory_mode(): return for quant in self: # Get the quantity to create a move for. rounding = quant.product_id.uom_id.rounding diff = float_round(quant.inventory_quantity - quant.quantity, precision_rounding=rounding) diff_float_compared = float_compare(diff, 0, precision_rounding=rounding) # Create and vaidate a move so that the quant matches its `inventory_quantity`. if diff_float_compared == 0: continue elif diff_float_compared > 0: move_vals = quant._get_inventory_move_values( diff, quant.product_id.with_context( force_company=quant.company_id.id or self.env.company.id).property_stock_inventory, quant.location_id) else: move_vals = quant._get_inventory_move_values( -diff, quant.location_id, quant.product_id.with_context( force_company=quant.company_id.id or self.env.company.id).property_stock_inventory, out=True) move = quant.env['stock.move'].with_context( inventory_mode=False).create(move_vals) move._action_done()
def _prepare_invoice_line_from_po_line(self, line): if line.product_id.purchase_method == 'purchase': qty = line.product_qty - line.qty_invoiced else: qty = line.qty_received - line.qty_invoiced if float_compare(qty, 0.0, precision_rounding=line.product_uom.rounding) <= 0: qty = 0.0 taxes = line.taxes_id invoice_line_tax_ids = line.order_id.fiscal_position_id.map_tax(taxes, line.product_id, line.order_id.partner_id) invoice_line = self.env['account.invoice.line'] date = self.date or self.date_invoice data = { 'purchase_line_id': line.id, 'name': line.order_id.name + ': ' + line.name, 'origin': line.order_id.origin, 'uom_id': line.product_uom.id, 'product_id': line.product_id.id, 'account_id': invoice_line.with_context({'journal_id': self.journal_id.id, 'type': 'in_invoice'})._default_account(), 'price_unit': line.order_id.currency_id._convert( line.price_unit, self.currency_id, line.company_id, date or fields.Date.today(), round=False), 'quantity': qty, 'discount': 0.0, 'account_analytic_id': line.account_analytic_id.id, 'analytic_tag_ids': line.analytic_tag_ids.ids, 'invoice_line_tax_ids': invoice_line_tax_ids.ids } account = invoice_line.get_invoice_line_account('in_invoice', line.product_id, line.order_id.fiscal_position_id, self.env.user.company_id) if account: data['account_id'] = account.id return data
def _prepare_account_move_line(self, move): self.ensure_one() if self.product_id.purchase_method == 'purchase': qty = self.product_qty - self.qty_invoiced else: qty = self.qty_received - self.qty_invoiced if float_compare(qty, 0.0, precision_rounding=self.product_uom.rounding) <= 0: qty = 0.0 if self.currency_id == move.company_id.currency_id: currency = False else: currency = move.currency_id return { 'name': '%s: %s' % (self.order_id.name, self.name), 'move_id': move.id, 'currency_id': currency and currency.id or False, 'purchase_line_id': self.id, 'date_maturity': move.invoice_date_due, 'product_uom_id': self.product_uom.id, 'product_id': self.product_id.id, 'price_unit': self.price_unit, 'quantity': qty, 'partner_id': move.partner_id.id, 'analytic_account_id': self.account_analytic_id.id, 'analytic_tag_ids': [(6, 0, self.analytic_tag_ids.ids)], 'tax_ids': [(6, 0, self.taxes_id.ids)], 'display_type': self.display_type, }
def _check_overprocessed_subcontract_qty(self): """ If a subcontracted move use tracked components. Do not allow to add quantity without the produce wizard. Instead update the initial demand and use the register component button. Split or correct a lot/sn is possible. """ overprocessed_moves = self.env['stock.move'] for move in self: if not move.is_subcontract: continue # Extra quantity is allowed when components do not need to be register if not move._has_tracked_subcontract_components(): continue rounding = move.product_uom.rounding if float_compare(move.quantity_done, move.move_orig_ids.production_id.qty_produced, precision_rounding=rounding) > 0: overprocessed_moves |= move if overprocessed_moves: raise UserError( _(""" You have to use 'Records Components' button in order to register quantity for a subcontracted product(s) with tracked component(s): %s. If you want to process more than initially planned, you can use the edit + unlock buttons in order to adapt the initial demand on the operations.""") % ('\n'.join( overprocessed_moves.mapped('product_id.display_name'))))
def _free_reservation(self, product_id, location_id, quantity, lot_id=None, package_id=None, owner_id=None, ml_to_ignore=None): """ When editing a done move line or validating one with some forced quantities, it is possible to impact quants that were not reserved. It is therefore necessary to edit or unlink the move lines that reserved a quantity now unavailable. :param ml_to_ignore: recordset of `stock.move.line` that should NOT be unreserved """ self.ensure_one() if ml_to_ignore is None: ml_to_ignore = self.env['stock.move.line'] ml_to_ignore |= self # Check the available quantity, with the `strict` kw set to `True`. If the available # quantity is greather than the quantity now unavailable, there is nothing to do. available_quantity = self.env['stock.quant']._get_available_quantity( product_id, location_id, lot_id=lot_id, package_id=package_id, owner_id=owner_id, strict=True ) if quantity > available_quantity: # We now have to find the move lines that reserved our now unavailable quantity. We # take care to exclude ourselves and the move lines were work had already been done. outdated_move_lines_domain = [ ('move_id.state', 'not in', ['done', 'cancel']), ('product_id', '=', product_id.id), ('lot_id', '=', lot_id.id if lot_id else False), ('location_id', '=', location_id.id), ('owner_id', '=', owner_id.id if owner_id else False), ('package_id', '=', package_id.id if package_id else False), ('product_qty', '>', 0.0), ('id', 'not in', ml_to_ignore.ids), ] current_picking_first = lambda cand: cand.picking_id != self.move_id.picking_id outdated_candidates = self.env['stock.move.line'].search(outdated_move_lines_domain).sorted(current_picking_first) # As the move's state is not computed over the move lines, we'll have to manually # recompute the moves which we adapted their lines. move_to_recompute_state = self.env['stock.move'] rounding = self.product_uom_id.rounding for candidate in outdated_candidates: if float_compare(candidate.product_qty, quantity, precision_rounding=rounding) <= 0: quantity -= candidate.product_qty move_to_recompute_state |= candidate.move_id if candidate.qty_done: candidate.product_uom_qty = 0.0 else: candidate.unlink() if float_is_zero(quantity, precision_rounding=rounding): break else: # split this move line and assign the new part to our extra move quantity_split = float_round( candidate.product_qty - quantity, precision_rounding=self.product_uom_id.rounding, rounding_method='UP') candidate.product_uom_qty = self.product_id.uom_id._compute_quantity(quantity_split, candidate.product_uom_id, rounding_method='HALF-UP') move_to_recompute_state |= candidate.move_id break move_to_recompute_state._recompute_state()
def check_quantity(self): for quant in self: if float_compare( quant.quantity, 1, precision_rounding=quant.product_uom_id.rounding ) > 0 and quant.lot_id and quant.product_id.tracking == 'serial': raise ValidationError( _('A serial number should only be linked to a single product.' ))
def _onchange_qty_done(self): """ When the user is encoding a move line for a tracked product, we apply some logic to help him. This onchange will warn him if he set `qty_done` to a non-supported value. """ res = {} if self.qty_done and self.product_id.tracking == 'serial': if float_compare(self.qty_done, 1.0, precision_rounding=self.product_id.uom_id.rounding) != 0: message = _('You can only process 1.0 %s of products with unique serial number.') % self.product_id.uom_id.name res['warning'] = {'title': _('Warning'), 'message': message} return res
def _buckaroo_form_get_invalid_parameters(self, data): invalid_parameters = [] data = normalize_keys_upper(data) if self.acquirer_reference and data.get('BRQ_TRANSACTIONS') != self.acquirer_reference: invalid_parameters.append(('Transaction Id', data.get('BRQ_TRANSACTIONS'), self.acquirer_reference)) # check what is buyed if float_compare(float(data.get('BRQ_AMOUNT', '0.0')), self.amount, 2) != 0: invalid_parameters.append(('Amount', data.get('BRQ_AMOUNT'), '%.2f' % self.amount)) if data.get('BRQ_CURRENCY') != self.currency_id.name: invalid_parameters.append(('Currency', data.get('BRQ_CURRENCY'), self.currency_id.name)) return invalid_parameters
def _default_shipping_weight(self): picking = self.env['stock.picking'].browse( self.env.context.get('default_picking_id')) move_line_ids = picking.move_line_ids.filtered(lambda m: float_compare( m.qty_done, 0.0, precision_rounding=m.product_uom_id.rounding ) > 0 and not m.result_package_id) total_weight = 0.0 for ml in move_line_ids: qty = ml.product_uom_id._compute_quantity(ml.qty_done, ml.product_id.uom_id) total_weight += qty * ml.product_id.weight return total_weight
def _transfer_form_get_invalid_parameters(self, data): invalid_parameters = [] if float_compare(float(data.get('amount', '0.0')), self.amount, 2) != 0: invalid_parameters.append( ('amount', data.get('amount'), '%.2f' % self.amount)) if data.get('currency') != self.currency_id.name: invalid_parameters.append( ('currency', data.get('currency'), self.currency_id.name)) return invalid_parameters
def _sips_form_get_invalid_parameters(self, data): invalid_parameters = [] data = self._sips_data_to_object(data.get('Data')) # TODO: txn_id: should be false at draft, set afterwards, and verified with txn details if self.acquirer_reference and data.get('transactionReference') != self.acquirer_reference: invalid_parameters.append(('transactionReference', data.get('transactionReference'), self.acquirer_reference)) # check what is bought if float_compare(float(data.get('amount', '0.0')) / 100, self.amount, 2) != 0: invalid_parameters.append(('amount', data.get('amount'), '%.2f' % self.amount)) return invalid_parameters
def action_show_details(self): """ Open the produce wizard in order to register tracked components for subcontracted product. Otherwise use standard behavior. """ self.ensure_one() if self.is_subcontract: rounding = self.product_uom.rounding production = self.move_orig_ids.production_id if self._has_tracked_subcontract_components() and\ float_compare(production.qty_produced, production.product_uom_qty, precision_rounding=rounding) < 0 and\ float_compare(self.quantity_done, self.product_uom_qty, precision_rounding=rounding) < 0: return self._action_record_components() action = super(StockMove, self).action_show_details() if self.is_subcontract: action['views'] = [ (self.env.ref('stock.view_stock_move_operations').id, 'form') ] action['context'].update({ 'show_lots_m2o': self.has_tracking != 'none', 'show_lots_text': False, }) return action
def _authorize_form_get_invalid_parameters(self, data): invalid_parameters = [] if self.acquirer_reference and data.get( 'x_trans_id') != self.acquirer_reference: invalid_parameters.append( ('Transaction Id', data.get('x_trans_id'), self.acquirer_reference)) # check what is buyed if float_compare(float(data.get('x_amount', '0.0')), self.amount, 2) != 0: invalid_parameters.append( ('Amount', data.get('x_amount'), '%.2f' % self.amount)) return invalid_parameters
def write(self, vals): if vals.get('order_line') and self.state == 'purchase': for order in self: pre_order_line_qty = {order_line: order_line.product_qty for order_line in order.mapped('order_line')} res = super(PurchaseOrder, self).write(vals) if vals.get('order_line') and self.state == 'purchase': for order in self: to_log = {} for order_line in order.order_line: if pre_order_line_qty.get(order_line, False) and float_compare(pre_order_line_qty[order_line], order_line.product_qty, precision_rounding=order_line.product_uom.rounding) > 0: to_log[order_line] = (order_line.product_qty, pre_order_line_qty[order_line]) if to_log: order._log_decrease_ordered_quantity(to_log) return res
def _process(self, cancel_backorder=False): for confirmation in self: if cancel_backorder: for pick_id in confirmation.pick_ids: moves_to_log = {} for move in pick_id.move_lines: if float_compare(move.product_uom_qty, move.quantity_done, precision_rounding=move.product_uom. rounding) > 0: moves_to_log[move] = (move.quantity_done, move.product_uom_qty) pick_id._log_less_quantities_than_expected(moves_to_log) confirmation.pick_ids.with_context( cancel_backorder=cancel_backorder).action_done()
def _payulatam_form_get_invalid_parameters(self, data): invalid_parameters = [] if self.acquirer_reference and data.get( 'transactionId') != self.acquirer_reference: invalid_parameters.append( ('Reference code', data.get('transactionId'), self.acquirer_reference)) if float_compare(float(data.get('TX_VALUE', '0.0')), self.amount, 2) != 0: invalid_parameters.append( ('Amount', data.get('TX_VALUE'), '%.2f' % self.amount)) if data.get('merchantId') != self.acquirer_id.payulatam_merchant_id: invalid_parameters.append(('Merchant Id', data.get('merchantId'), self.acquirer_id.payulatam_merchant_id)) return invalid_parameters
def _prepare_stock_moves(self, picking): """ Prepare the stock moves data for one order line. This function returns a list of dictionary ready to be used in stock.move's create() """ self.ensure_one() res = [] if self.product_id.type not in ['product', 'consu']: return res qty = 0.0 price_unit = self._get_stock_move_price_unit() for move in self.move_ids.filtered(lambda x: x.state != 'cancel' and not x.location_dest_id.usage == "supplier"): qty += move.product_uom._compute_quantity(move.product_uom_qty, self.product_uom, rounding_method='HALF-UP') template = { # truncate to 2000 to avoid triggering index limit error # TODO: remove index in master? 'name': (self.name or '')[:2000], 'product_id': self.product_id.id, 'product_uom': self.product_uom.id, 'date': self.order_id.date_order, 'date_expected': self.date_planned, 'location_id': self.order_id.partner_id.property_stock_supplier.id, 'location_dest_id': self.order_id._get_destination_location(), 'picking_id': picking.id, 'partner_id': self.order_id.dest_address_id.id, 'move_dest_ids': [(4, x) for x in self.move_dest_ids.ids], 'state': 'draft', 'purchase_line_id': self.id, 'company_id': self.order_id.company_id.id, 'price_unit': price_unit, 'picking_type_id': self.order_id.picking_type_id.id, 'group_id': self.order_id.group_id.id, 'origin': self.order_id.name, 'route_ids': self.order_id.picking_type_id.warehouse_id and [(6, 0, [x.id for x in self.order_id.picking_type_id.warehouse_id.route_ids])] or [], 'warehouse_id': self.order_id.picking_type_id.warehouse_id.id, } diff_quantity = self.product_qty - qty if float_compare(diff_quantity, 0.0, precision_rounding=self.product_uom.rounding) > 0: quant_uom = self.product_id.uom_id get_param = self.env['ir.config_parameter'].sudo().get_param if self.product_uom.id != quant_uom.id and get_param('stock.propagate_uom') != '1': product_qty = self.product_uom._compute_quantity(diff_quantity, quant_uom, rounding_method='HALF-UP') template['product_uom'] = quant_uom.id template['product_uom_qty'] = product_qty else: template['product_uom_qty'] = diff_quantity res.append(template) return res
def _ogone_form_get_invalid_parameters(self, data): invalid_parameters = [] # TODO: txn_id: should be false at draft, set afterwards, and verified with txn details if self.acquirer_reference and data.get( 'PAYID') != self.acquirer_reference: invalid_parameters.append( ('PAYID', data.get('PAYID'), self.acquirer_reference)) # check what is bought if float_compare(float(data.get('amount', '0.0')), self.amount, 2) != 0: invalid_parameters.append( ('amount', data.get('amount'), '%.2f' % self.amount)) if data.get('currency') != self.currency_id.name: invalid_parameters.append( ('currency', data.get('currency'), self.currency_id.name)) return invalid_parameters
def _process(self, cancel_backorder=False): if cancel_backorder: for pick_id in self.pick_ids: moves_to_log = {} for move in pick_id.move_lines: if float_compare( move.product_uom_qty, move.quantity_done, precision_rounding=move.product_uom.rounding) > 0: moves_to_log[move] = (move.quantity_done, move.product_uom_qty) pick_id._log_less_quantities_than_expected(moves_to_log) self.pick_ids.action_done() if cancel_backorder: for pick_id in self.pick_ids: backorder_pick = self.env['stock.picking'].search([ ('backorder_id', '=', pick_id.id) ]) backorder_pick.action_cancel() pick_id.message_post( body=_("Back order <em>%s</em> <b>cancelled</b>.") % (",".join([b.name or '' for b in backorder_pick])))
def _get_available_quantity(self, product_id, location_id, lot_id=None, package_id=None, owner_id=None, strict=False, allow_negative=False): """ Return the available quantity, i.e. the sum of `quantity` minus the sum of `reserved_quantity`, for the set of quants sharing the combination of `product_id, location_id` if `strict` is set to False or sharing the *exact same characteristics* otherwise. This method is called in the following usecases: - when a stock move checks its availability - when a stock move actually assign - when editing a move line, to check if the new value is forced or not - when validating a move line with some forced values and have to potentially unlink an equivalent move line in another picking In the two first usecases, `strict` should be set to `False`, as we don't know what exact quants we'll reserve, and the characteristics are meaningless in this context. In the last ones, `strict` should be set to `True`, as we work on a specific set of characteristics. :return: available quantity as a float """ self = self.sudo() quants = self._gather(product_id, location_id, lot_id=lot_id, package_id=package_id, owner_id=owner_id, strict=strict) rounding = product_id.uom_id.rounding if product_id.tracking == 'none': available_quantity = sum(quants.mapped('quantity')) - sum( quants.mapped('reserved_quantity')) if allow_negative: return available_quantity else: return available_quantity if float_compare( available_quantity, 0.0, precision_rounding=rounding) >= 0.0 else 0.0 else: availaible_quantities = { lot_id: 0.0 for lot_id in list(set(quants.mapped('lot_id'))) + ['untracked'] } for quant in quants: if not quant.lot_id: availaible_quantities[ 'untracked'] += quant.quantity - quant.reserved_quantity else: availaible_quantities[ quant. lot_id] += quant.quantity - quant.reserved_quantity if allow_negative: return sum(availaible_quantities.values()) else: return sum([ available_quantity for available_quantity in availaible_quantities.values() if float_compare( available_quantity, 0, precision_rounding=rounding) > 0 ])
def test_01_compute_price_operation_cost(self): """Test calcuation of bom cost with operations.""" workcenter_from1 = Form(self.env['mrp.workcenter']) workcenter_from1.name = 'Workcenter' workcenter_from1.time_efficiency = 100 workcenter_from1.capacity = 2 workcenter_from1.oee_target = 100 workcenter_from1.time_start = 0 workcenter_from1.time_stop = 0 workcenter_from1.costs_hour = 100 workcenter_1 = workcenter_from1.save() routing_form1 = Form(self.Routing) routing_form1.name = 'Assembly Furniture' routing_1 = routing_form1.save() operation_1 = self.operation.create({ 'name': 'Cutting', 'workcenter_id': workcenter_1.id, 'routing_id': routing_1.id, 'time_mode': 'manual', 'time_cycle_manual': 20, 'batch': 'no', 'sequence': 1, }) operation_2 = self.operation.create({ 'name': 'Drilling', 'workcenter_id': workcenter_1.id, 'routing_id': routing_1.id, 'time_mode': 'manual', 'time_cycle_manual': 25, 'batch': 'no', 'sequence': 2, }) operation_3 = self.operation.create({ 'name': 'Fitting', 'workcenter_id': workcenter_1.id, 'routing_id': routing_1.id, 'time_mode': 'manual', 'time_cycle_manual': 30, 'batch': 'no', 'sequence': 3, }) # ----------------------------------------------------------------- # Dinning Table Operation Cost(1 Unit) # ----------------------------------------------------------------- # Operation cost calculate for 1 units # Cutting (20 / 60) * 100 = 33.33 # Drilling (25 / 60) * 100 = 41.67 # Fitting (30 / 60) * 100 = 50.00 # ---------------------------------------- # Operation Cost 1 unit = 125 # ----------------------------------------------------------------- self.bom_1.routing_id = routing_1.id # -------------------------------------------------------------------------- # Table Head Operation Cost (1 Dozen) # -------------------------------------------------------------------------- # Operation cost calculate for 1 dozens # Cutting (20 * 1 / 60) * 100 = 33,33 # Drilling (25 * 1 / 60) * 100 = 41,67 # Fitting (30 * 1 / 60) * 100 = 50 # ---------------------------------------- # Operation Cost 1 dozen (125 per dozen) and 10.42 for 1 Unit # -------------------------------------------------------------------------- self.bom_2.routing_id = routing_1.id self.assertEqual(self.dining_table.standard_price, 1000, "Initial price of the Product should be 1000") self.dining_table.button_bom_cost() # Total cost of Dining Table = (550) + Total cost of operations (125) = 675.0 self.assertEquals(float_round(self.dining_table.standard_price, precision_digits=2), 675.0, "After computing price from BoM price should be 612.5") self.Product.browse([self.dining_table.id, self.table_head.id]).action_bom_cost() # Total cost of Dining Table = (718.75) + Total cost of all operations (125 + 10.42) = 854.17 self.assertEquals(float_compare(self.dining_table.standard_price, 854.17, precision_digits=2), 0, "After computing price from BoM price should be 786.46")
def _update_reserved_quantity(self, product_id, location_id, quantity, lot_id=None, package_id=None, owner_id=None, strict=False): """ Increase the reserved quantity, i.e. increase `reserved_quantity` for the set of quants sharing the combination of `product_id, location_id` if `strict` is set to False or sharing the *exact same characteristics* otherwise. Typically, this method is called when reserving a move or updating a reserved move line. When reserving a chained move, the strict flag should be enabled (to reserve exactly what was brought). When the move is MTS,it could take anything from the stock, so we disable the flag. When editing a move line, we naturally enable the flag, to reflect the reservation according to the edition. :return: a list of tuples (quant, quantity_reserved) showing on which quant the reservation was done and how much the system was able to reserve on it """ self = self.sudo() rounding = product_id.uom_id.rounding quants = self._gather(product_id, location_id, lot_id=lot_id, package_id=package_id, owner_id=owner_id, strict=strict) reserved_quants = [] if float_compare(quantity, 0, precision_rounding=rounding) > 0: # if we want to reserve available_quantity = self._get_available_quantity( product_id, location_id, lot_id=lot_id, package_id=package_id, owner_id=owner_id, strict=strict) if float_compare(quantity, available_quantity, precision_rounding=rounding) > 0: raise UserError( _('It is not possible to reserve more products of %s than you have in stock.' ) % product_id.display_name) elif float_compare(quantity, 0, precision_rounding=rounding) < 0: # if we want to unreserve available_quantity = sum(quants.mapped('reserved_quantity')) if float_compare(abs(quantity), available_quantity, precision_rounding=rounding) > 0: raise UserError( _('It is not possible to unreserve more products of %s than you have in stock.' ) % product_id.display_name) else: return reserved_quants for quant in quants: if float_compare(quantity, 0, precision_rounding=rounding) > 0: max_quantity_on_quant = quant.quantity - quant.reserved_quantity if float_compare(max_quantity_on_quant, 0, precision_rounding=rounding) <= 0: continue max_quantity_on_quant = min(max_quantity_on_quant, quantity) quant.reserved_quantity += max_quantity_on_quant reserved_quants.append((quant, max_quantity_on_quant)) quantity -= max_quantity_on_quant available_quantity -= max_quantity_on_quant else: max_quantity_on_quant = min(quant.reserved_quantity, abs(quantity)) quant.reserved_quantity -= max_quantity_on_quant reserved_quants.append((quant, -max_quantity_on_quant)) quantity += max_quantity_on_quant available_quantity += max_quantity_on_quant if float_is_zero( quantity, precision_rounding=rounding) or float_is_zero( available_quantity, precision_rounding=rounding): break return reserved_quants
def _action_done(self): """ This method is called during a move's `action_done`. It'll actually move a quant from the source location to the destination location, and unreserve if needed in the source location. This method is intended to be called on all the move lines of a move. This method is not intended to be called when editing a `done` move (that's what the override of `write` here is done. """ Quant = self.env['stock.quant'] # First, we loop over all the move lines to do a preliminary check: `qty_done` should not # be negative and, according to the presence of a picking type or a linked inventory # adjustment, enforce some rules on the `lot_id` field. If `qty_done` is null, we unlink # the line. It is mandatory in order to free the reservation and correctly apply # `action_done` on the next move lines. ml_to_delete = self.env['stock.move.line'] for ml in self: # Check here if `ml.qty_done` respects the rounding of `ml.product_uom_id`. uom_qty = float_round(ml.qty_done, precision_rounding=ml.product_uom_id.rounding, rounding_method='HALF-UP') precision_digits = self.env['decimal.precision'].precision_get('Product Unit of Measure') qty_done = float_round(ml.qty_done, precision_digits=precision_digits, rounding_method='HALF-UP') if float_compare(uom_qty, qty_done, precision_digits=precision_digits) != 0: raise UserError(_('The quantity done for the product "%s" doesn\'t respect the rounding precision \ defined on the unit of measure "%s". Please change the quantity done or the \ rounding precision of your unit of measure.') % (ml.product_id.display_name, ml.product_uom_id.name)) qty_done_float_compared = float_compare(ml.qty_done, 0, precision_rounding=ml.product_uom_id.rounding) if qty_done_float_compared > 0: if ml.product_id.tracking != 'none': picking_type_id = ml.move_id.picking_type_id if picking_type_id: if picking_type_id.use_create_lots: # If a picking type is linked, we may have to create a production lot on # the fly before assigning it to the move line if the user checked both # `use_create_lots` and `use_existing_lots`. if ml.lot_name and not ml.lot_id: lot = self.env['stock.production.lot'].create( {'name': ml.lot_name, 'product_id': ml.product_id.id} ) ml.write({'lot_id': lot.id}) elif not picking_type_id.use_create_lots and not picking_type_id.use_existing_lots: # If the user disabled both `use_create_lots` and `use_existing_lots` # checkboxes on the picking type, he's allowed to enter tracked # products without a `lot_id`. continue elif ml.move_id.inventory_id: # If an inventory adjustment is linked, the user is allowed to enter # tracked products without a `lot_id`. continue if not ml.lot_id: raise UserError(_('You need to supply a Lot/Serial number for product %s.') % ml.product_id.display_name) elif qty_done_float_compared < 0: raise UserError(_('No negative quantities allowed')) else: ml_to_delete |= ml ml_to_delete.unlink() # Now, we can actually move the quant. done_ml = self.env['stock.move.line'] for ml in self - ml_to_delete: if ml.product_id.type == 'product': rounding = ml.product_uom_id.rounding # if this move line is force assigned, unreserve elsewhere if needed if not ml.location_id.should_bypass_reservation() and float_compare(ml.qty_done, ml.product_qty, precision_rounding=rounding) > 0: extra_qty = ml.qty_done - ml.product_qty ml._free_reservation(ml.product_id, ml.location_id, extra_qty, lot_id=ml.lot_id, package_id=ml.package_id, owner_id=ml.owner_id, ml_to_ignore=done_ml) # unreserve what's been reserved if not ml.location_id.should_bypass_reservation() and ml.product_id.type == 'product' and ml.product_qty: try: Quant._update_reserved_quantity(ml.product_id, ml.location_id, -ml.product_qty, lot_id=ml.lot_id, package_id=ml.package_id, owner_id=ml.owner_id, strict=True) except UserError: Quant._update_reserved_quantity(ml.product_id, ml.location_id, -ml.product_qty, lot_id=False, package_id=ml.package_id, owner_id=ml.owner_id, strict=True) # move what's been actually done quantity = ml.product_uom_id._compute_quantity(ml.qty_done, ml.move_id.product_id.uom_id, rounding_method='HALF-UP') available_qty, in_date = Quant._update_available_quantity(ml.product_id, ml.location_id, -quantity, lot_id=ml.lot_id, package_id=ml.package_id, owner_id=ml.owner_id) if available_qty < 0 and ml.lot_id: # see if we can compensate the negative quants with some untracked quants untracked_qty = Quant._get_available_quantity(ml.product_id, ml.location_id, lot_id=False, package_id=ml.package_id, owner_id=ml.owner_id, strict=True) if untracked_qty: taken_from_untracked_qty = min(untracked_qty, abs(quantity)) Quant._update_available_quantity(ml.product_id, ml.location_id, -taken_from_untracked_qty, lot_id=False, package_id=ml.package_id, owner_id=ml.owner_id) Quant._update_available_quantity(ml.product_id, ml.location_id, taken_from_untracked_qty, lot_id=ml.lot_id, package_id=ml.package_id, owner_id=ml.owner_id) Quant._update_available_quantity(ml.product_id, ml.location_dest_id, quantity, lot_id=ml.lot_id, package_id=ml.result_package_id, owner_id=ml.owner_id, in_date=in_date) done_ml |= ml # Reset the reserved quantity as we just moved it to the destination location. (self - ml_to_delete).with_context(bypass_reservation_update=True).write({ 'product_uom_qty': 0.00, 'date': fields.Datetime.now(), })
def _stock_account_prepare_anglo_saxon_in_lines_vals(self): ''' Prepare values used to create the journal items (account.move.line) corresponding to the price difference lines for vendor bills. Example: Buy a product having a cost of 9 and a supplier price of 10 and being a storable product and having a perpetual valuation in FIFO. The vendor bill's journal entries looks like: Account | Debit | Credit --------------------------------------------------------------- 101120 Stock Interim Account (Received) | 10.0 | --------------------------------------------------------------- 101100 Account Payable | | 10.0 --------------------------------------------------------------- This method computes values used to make two additional journal items: --------------------------------------------------------------- 101120 Stock Interim Account (Received) | | 1.0 --------------------------------------------------------------- xxxxxx Price Difference Account | 1.0 | --------------------------------------------------------------- :return: A list of Python dictionary to be passed to env['account.move.line'].create. ''' lines_vals_list = [] for move in self: if move.type not in ( 'in_invoice', 'in_refund', 'in_receipt' ) or not move.company_id.anglo_saxon_accounting: continue for line in move.invoice_line_ids.filtered( lambda line: line.product_id.type == 'product' and line. product_id.valuation == 'real_time'): # Filter out lines being not eligible for price difference. if line.product_id.type != 'product' or line.product_id.valuation != 'real_time': continue # Retrieve accounts needed to generate the price difference. debit_pdiff_account = line.product_id.property_account_creditor_price_difference \ or line.product_id.categ_id.property_account_creditor_price_difference_categ debit_pdiff_account = move.fiscal_position_id.map_account( debit_pdiff_account) if line.product_id.cost_method != 'standard' and line.purchase_line_id: po_currency = line.purchase_line_id.currency_id po_company = line.purchase_line_id.company_id # Retrieve stock valuation moves. valuation_stock_moves = self.env['stock.move'].search([ ('purchase_line_id', '=', line.purchase_line_id.id), ('state', '=', 'done'), ('product_qty', '!=', 0.0), ]) if move.type == 'in_refund': valuation_stock_moves = valuation_stock_moves.filtered( lambda stock_move: stock_move._is_out()) else: valuation_stock_moves = valuation_stock_moves.filtered( lambda stock_move: stock_move._is_in()) if valuation_stock_moves: valuation_price_unit_total = 0 valuation_total_qty = 0 for val_stock_move in valuation_stock_moves: # In case val_stock_move is a return move, its valuation entries have been made with the # currency rate corresponding to the original stock move valuation_date = val_stock_move.origin_returned_move_id.date or val_stock_move.date layers_qty = sum( val_stock_move.mapped( 'stock_valuation_layer_ids.quantity')) layers_values = sum( val_stock_move.mapped( 'stock_valuation_layer_ids.value')) valuation_price_unit_total += line.company_currency_id._convert( layers_values, move.currency_id, move.company_id, valuation_date, round=False, ) valuation_total_qty += layers_qty valuation_price_unit = valuation_price_unit_total / valuation_total_qty valuation_price_unit = line.product_id.uom_id._compute_price( valuation_price_unit, line.product_uom_id) elif line.product_id.cost_method == 'fifo': # In this condition, we have a real price-valuated product which has not yet been received valuation_price_unit = po_currency._convert( line.purchase_line_id.price_unit, move.currency_id, po_company, move.date, round=False, ) else: # For average/fifo/lifo costing method, fetch real cost price from incoming moves. price_unit = line.purchase_line_id.product_uom._compute_price( line.purchase_line_id.price_unit, line.product_uom_id) valuation_price_unit = po_currency._convert( price_unit, move.currency_id, po_company, move.date, round=False) else: # Valuation_price unit is always expressed in invoice currency, so that it can always be computed with the good rate price_unit = line.product_id.uom_id._compute_price( line.product_id.standard_price, line.product_uom_id) valuation_price_unit = line.company_currency_id._convert( price_unit, move.currency_id, move.company_id, fields.Date.today(), round=False) invoice_cur_prec = move.currency_id.decimal_places price_unit = line.price_unit * (1 - (line.discount or 0.0) / 100.0) if line.tax_ids: price_unit = line.tax_ids.compute_all( price_unit, currency=move.currency_id, quantity=1.0, is_refund=move.type == 'in_refund')['total_excluded'] if float_compare(valuation_price_unit, price_unit, precision_digits=invoice_cur_prec) != 0 \ and float_compare(line['price_unit'], line.price_unit, precision_digits=invoice_cur_prec) == 0: price_unit_val_dif = price_unit - valuation_price_unit if move.currency_id.compare_amounts( price_unit, valuation_price_unit) != 0 and debit_pdiff_account: # Add price difference account line. vals = { 'name': line.name[:64], 'move_id': move.id, 'currency_id': line.currency_id.id, 'product_id': line.product_id.id, 'product_uom_id': line.product_uom_id.id, 'quantity': line.quantity, 'price_unit': price_unit_val_dif, 'price_subtotal': line.quantity * price_unit_val_dif, 'account_id': debit_pdiff_account.id, 'analytic_account_id': line.analytic_account_id.id, 'analytic_tag_ids': [(6, 0, line.analytic_tag_ids.ids)], 'exclude_from_invoice_tab': True, 'is_anglo_saxon_line': True, } vals.update( line._get_fields_onchange_subtotal( price_subtotal=vals['price_subtotal'])) lines_vals_list.append(vals) # Correct the amount of the current line. vals = { 'name': line.name[:64], 'move_id': move.id, 'currency_id': line.currency_id.id, 'product_id': line.product_id.id, 'product_uom_id': line.product_uom_id.id, 'quantity': line.quantity, 'price_unit': -price_unit_val_dif, 'price_subtotal': line.quantity * -price_unit_val_dif, 'account_id': line.account_id.id, 'analytic_account_id': line.analytic_account_id.id, 'analytic_tag_ids': [(6, 0, line.analytic_tag_ids.ids)], 'exclude_from_invoice_tab': True, 'is_anglo_saxon_line': True, } vals.update( line._get_fields_onchange_subtotal( price_subtotal=vals['price_subtotal'])) lines_vals_list.append(vals) return lines_vals_list