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 _paypal_form_get_invalid_parameters(self, data): invalid_parameters = [] _logger.info('Received a notification from Paypal with IPN version %s', data.get('notify_version')) if data.get('test_ipn'): _logger.warning( 'Received a notification from Paypal 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 + self.fees))) # 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.paypal_seller_account and data[ 'receiver_id'] != self.acquirer_id.paypal_seller_account: invalid_parameters.append(('receiver_id', data.get('receiver_id'), self.acquirer_id.paypal_seller_account)) if not data.get( 'receiver_id') or not self.acquirer_id.paypal_seller_account: # Check receiver_email only if receiver_id was not checked. # In Paypal, this is possible to configure as receiver_email a different email than the business email (the login email) # In Swerp, there is only one field for the Paypal 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 Paypal, you are then obliged to fill # the Merchant ID in the Paypal payment acquirer in Swerp, 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.paypal_email_account: invalid_parameters.append( ('receiver_email', data.get('receiver_email'), self.acquirer_id.paypal_email_account)) return invalid_parameters
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': message_base = _('A serial number should only be linked to a single product.') message_quant = _('Please check the following serial number (name, id): ') message_sn = '(%s, %s)' % (quant.lot_id.name, quant.lot_id.id) raise ValidationError("\n".join([message_base, message_quant, message_sn]))
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 _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', 'customer')) 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 _transfer_form_get_invalid_parameters(self, data): invalid_parameters = [] if float_compare(float(data.get('amount') or '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 _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.with_context( purchase_line_id=line.id).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 _payumoney_form_get_invalid_parameters(self, data): invalid_parameters = [] if self.acquirer_reference and data.get( 'mihpayid') != self.acquirer_reference: invalid_parameters.append(('Transaction Id', data.get('mihpayid'), self.acquirer_reference)) #check what is buyed if float_compare(float(data.get('amount', '0.0')), self.amount, 2) != 0: invalid_parameters.append( ('Amount', data.get('amount'), '%.2f' % self.amount)) return invalid_parameters
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 _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 = sum(quants.filtered(lambda q: float_compare(q.quantity, 0, precision_rounding=rounding) > 0).mapped('quantity')) - sum(quants.mapped('reserved_quantity')) 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 _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 _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 _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 _generate_moves(self): vals_list = [] for line in self: if float_utils.float_compare( line.theoretical_qty, line.product_qty, precision_rounding=line.product_id.uom_id.rounding) == 0: continue diff = line.theoretical_qty - line.product_qty if diff < 0: # found more than expected vals = line._get_move_values( abs(diff), line.product_id.property_stock_inventory.id, line.location_id.id, False) else: vals = line._get_move_values( abs(diff), line.location_id.id, line.product_id.property_stock_inventory.id, True) vals_list.append(vals) return self.env['stock.move'].create(vals_list)
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 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 _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 # Always call '_compute_quantity' to round the diff_quantity. Indeed, the PO quantity # is not rounded automatically following the UoM. if 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'] = self.product_uom._compute_quantity( diff_quantity, self.product_uom, rounding_method='HALF-UP') res.append(template) return res
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 = [ ('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), ] # We take the current picking first, then the pickings with the latest scheduled date current_picking_first = lambda cand: ( cand.picking_id != self.move_id.picking_id, -(cand.picking_id.scheduled_date or cand.move_id.date_expected) .timestamp() if cand.picking_id or cand.move_id else -cand.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 _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_uom_qty, precision_rounding=rounding) > 0: qty_done_product_uom = ml.product_uom_id._compute_quantity( ml.qty_done, ml.product_id.uom_id, rounding_method='HALF-UP') extra_qty = qty_done_product_uom - 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: 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) # 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 write(self, vals): if self.env.context.get('bypass_reservation_update'): return super(StockMoveLine, self).write(vals) if 'product_id' in vals and any( vals.get('state', ml.state) != 'draft' and vals['product_id'] != ml.product_id.id for ml in self): raise UserError( _("Changing the product is only allowed in 'Draft' state.")) Quant = self.env['stock.quant'] precision = self.env['decimal.precision'].precision_get( 'Product Unit of Measure') triggers = [('location_id', 'stock.location'), ('location_dest_id', 'stock.location'), ('lot_id', 'stock.production.lot'), ('package_id', 'stock.quant.package'), ('result_package_id', 'stock.quant.package'), ('owner_id', 'res.partner')] updates = {} for key, model in triggers: if key in vals: updates[key] = self.env[model].browse(vals[key]) if 'result_package_id' in updates: for ml in self.filtered(lambda ml: ml.package_level_id): if updates.get('result_package_id'): ml.package_level_id.package_id = updates.get( 'result_package_id') else: # TODO: make package levels less of a pain and fix this package_level = ml.package_level_id ml.package_level_id = False package_level.unlink() # When we try to write on a reserved move line any fields from `triggers` or directly # `product_uom_qty` (the actual reserved quantity), we need to make sure the associated # quants are correctly updated in order to not make them out of sync (i.e. the sum of the # move lines `product_uom_qty` should always be equal to the sum of `reserved_quantity` on # the quants). If the new charateristics are not available on the quants, we chose to # reserve the maximum possible. if updates or 'product_uom_qty' in vals: for ml in self.filtered( lambda ml: ml.state in ['partially_available', 'assigned'] and ml.product_id.type == 'product'): if 'product_uom_qty' in vals: new_product_uom_qty = ml.product_uom_id._compute_quantity( vals['product_uom_qty'], ml.product_id.uom_id, rounding_method='HALF-UP') # Make sure `product_uom_qty` is not negative. if float_compare(new_product_uom_qty, 0, precision_rounding=ml.product_id.uom_id. rounding) < 0: raise UserError( _('Reserving a negative quantity is not allowed.')) else: new_product_uom_qty = ml.product_qty # Unreserve the old charateristics of the move line. if not ml.location_id.should_bypass_reservation(): 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) # Reserve the maximum available of the new charateristics of the move line. if not updates.get('location_id', ml.location_id).should_bypass_reservation(): reserved_qty = 0 try: q = Quant._update_reserved_quantity( ml.product_id, updates.get('location_id', ml.location_id), new_product_uom_qty, lot_id=updates.get('lot_id', ml.lot_id), package_id=updates.get('package_id', ml.package_id), owner_id=updates.get('owner_id', ml.owner_id), strict=True) reserved_qty = sum([x[1] for x in q]) except UserError: pass if reserved_qty != new_product_uom_qty: new_product_uom_qty = ml.product_id.uom_id._compute_quantity( reserved_qty, ml.product_uom_id, rounding_method='HALF-UP') ml.with_context(bypass_reservation_update=True ).product_uom_qty = new_product_uom_qty # When editing a done move line, the reserved availability of a potential chained move is impacted. Take care of running again `_action_assign` on the concerned moves. next_moves = self.env['stock.move'] if updates or 'qty_done' in vals: mls = self.filtered(lambda ml: ml.move_id.state == 'done' and ml. product_id.type == 'product') if not updates: # we can skip those where qty_done is already good up to UoM rounding mls = mls.filtered(lambda ml: not float_is_zero( ml.qty_done - vals['qty_done'], precision_rounding=ml.product_uom_id.rounding)) for ml in mls: # undo the original move line qty_done_orig = ml.move_id.product_uom._compute_quantity( ml.qty_done, ml.move_id.product_id.uom_id, rounding_method='HALF-UP') in_date = Quant._update_available_quantity( ml.product_id, ml.location_dest_id, -qty_done_orig, lot_id=ml.lot_id, package_id=ml.result_package_id, owner_id=ml.owner_id)[1] Quant._update_available_quantity(ml.product_id, ml.location_id, qty_done_orig, lot_id=ml.lot_id, package_id=ml.package_id, owner_id=ml.owner_id, in_date=in_date) # move what's been actually done product_id = ml.product_id location_id = updates.get('location_id', ml.location_id) location_dest_id = updates.get('location_dest_id', ml.location_dest_id) qty_done = vals.get('qty_done', ml.qty_done) lot_id = updates.get('lot_id', ml.lot_id) package_id = updates.get('package_id', ml.package_id) result_package_id = updates.get('result_package_id', ml.result_package_id) owner_id = updates.get('owner_id', ml.owner_id) quantity = ml.move_id.product_uom._compute_quantity( qty_done, ml.move_id.product_id.uom_id, rounding_method='HALF-UP') if not location_id.should_bypass_reservation(): ml._free_reservation(product_id, location_id, quantity, lot_id=lot_id, package_id=package_id, owner_id=owner_id) if not float_is_zero(quantity, precision_digits=precision): available_qty, in_date = Quant._update_available_quantity( product_id, location_id, -quantity, lot_id=lot_id, package_id=package_id, owner_id=owner_id) if available_qty < 0 and lot_id: # see if we can compensate the negative quants with some untracked quants untracked_qty = Quant._get_available_quantity( product_id, location_id, lot_id=False, package_id=package_id, owner_id=owner_id, strict=True) if untracked_qty: taken_from_untracked_qty = min( untracked_qty, abs(available_qty)) Quant._update_available_quantity( product_id, location_id, -taken_from_untracked_qty, lot_id=False, package_id=package_id, owner_id=owner_id) Quant._update_available_quantity( product_id, location_id, taken_from_untracked_qty, lot_id=lot_id, package_id=package_id, owner_id=owner_id) if not location_id.should_bypass_reservation(): ml._free_reservation(ml.product_id, location_id, untracked_qty, lot_id=False, package_id=package_id, owner_id=owner_id) Quant._update_available_quantity( product_id, location_dest_id, quantity, lot_id=lot_id, package_id=result_package_id, owner_id=owner_id, in_date=in_date) # Unreserve and reserve following move in order to have the real reserved quantity on move_line. next_moves |= ml.move_id.move_dest_ids.filtered( lambda move: move.state not in ('done', 'cancel')) # Log a note if ml.picking_id: ml._log_message(ml.picking_id, ml, 'stock.track_move_template', vals) res = super(StockMoveLine, self).write(vals) # Update scrap object linked to move_lines to the new quantity. if 'qty_done' in vals: for move in self.mapped('move_id'): if move.scrapped: move.scrap_ids.write({'scrap_qty': move.quantity_done}) # As stock_account values according to a move's `product_uom_qty`, we consider that any # done stock move should have its `quantity_done` equals to its `product_uom_qty`, and # this is what move's `action_done` will do. So, we replicate the behavior here. if updates or 'qty_done' in vals: moves = self.filtered( lambda ml: ml.move_id.state == 'done').mapped('move_id') for move in moves: move.product_uom_qty = move.quantity_done next_moves._do_unreserve() next_moves._action_assign() return res
def _anglo_saxon_purchase_move_lines(self, i_line, res): """Return the additional move lines for purchase invoices and refunds. i_line: An account.invoice.line object. res: The move line entries produced so far by the parent move_line_get. """ inv = i_line.invoice_id company_currency = inv.company_id.currency_id if i_line.product_id and i_line.product_id.valuation == 'real_time' and i_line.product_id.type == 'product': # get the fiscal position fpos = i_line.invoice_id.fiscal_position_id # get the price difference account at the product acc = i_line.product_id.property_account_creditor_price_difference if not acc: # if not found on the product get the price difference account at the category acc = i_line.product_id.categ_id.property_account_creditor_price_difference_categ acc = fpos.map_account(acc).id # reference_account_id is the stock input account reference_account_id = i_line.product_id.product_tmpl_id.get_product_accounts( fiscal_pos=fpos)['stock_input'].id diff_res = [] # calculate and write down the possible price difference between invoice price and product price for line in res: if line.get( 'invl_id', 0 ) == i_line.id and reference_account_id == line['account_id']: # valuation_price unit is always expressed in invoice currency, so that it can always be computed with the good rate valuation_price_unit = company_currency._convert( i_line.product_id.uom_id._compute_price( i_line.product_id.standard_price, i_line.uom_id), inv.currency_id, company=inv.company_id, date=fields.Date.today(), round=False, ) if i_line.product_id.cost_method != 'standard' and i_line.purchase_line_id: po_currency = i_line.purchase_id.currency_id po_company = i_line.purchase_id.company_id #for average/fifo/lifo costing method, fetch real cost price from incomming moves valuation_price_unit = po_currency._convert( i_line.purchase_line_id.product_uom._compute_price( i_line.purchase_line_id.price_unit, i_line.uom_id), inv.currency_id, company=po_company, date=inv.date or inv.date_invoice, round=False, ) stock_move_obj = self.env['stock.move'] valuation_stock_move = stock_move_obj.search([ ('purchase_line_id', '=', i_line.purchase_line_id.id), ('state', '=', 'done'), ('product_qty', '!=', 0.0) ]) if self.type == 'in_refund': valuation_stock_move = valuation_stock_move.filtered( lambda m: m._is_out()) elif self.type == 'in_invoice': valuation_stock_move = valuation_stock_move.filtered( lambda m: m._is_in()) if valuation_stock_move: valuation_price_unit_total = 0 valuation_total_qty = 0 for val_stock_move in valuation_stock_move: # 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 valuation_price_unit_total += company_currency._convert( abs(val_stock_move.price_unit) * val_stock_move.product_qty, inv.currency_id, company=inv.company_id, date=valuation_date, round=False, ) valuation_total_qty += val_stock_move.product_qty # in Stock Move, price unit is in company_currency valuation_price_unit = valuation_price_unit_total / valuation_total_qty valuation_price_unit = i_line.product_id.uom_id._compute_price( valuation_price_unit, i_line.uom_id) elif i_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( i_line.purchase_line_id.price_unit, inv.currency_id, company=po_company, date=inv.date or inv.date_invoice, round=False, ) interim_account_price = valuation_price_unit * line[ 'quantity'] invoice_cur_prec = inv.currency_id.decimal_places # price with discount and without tax included price_unit = i_line.price_unit * ( 1 - (i_line.discount or 0.0) / 100.0) tax_ids = [] if line['tax_ids']: #line['tax_ids'] is like [(4, tax_id, None), (4, tax_id2, None)...] taxes = self.env['account.tax'].browse( [x[1] for x in line['tax_ids']]) price_unit = taxes.compute_all( price_unit, currency=inv.currency_id, quantity=1.0)['total_excluded'] for tax in taxes: tax_ids.append((4, tax.id, None)) for child in tax.children_tax_ids: if child.type_tax_use != 'none': tax_ids.append((4, child.id, None)) if float_compare( valuation_price_unit, price_unit, precision_digits=invoice_cur_prec ) != 0 and float_compare( line['price_unit'], i_line.price_unit, precision_digits=invoice_cur_prec) == 0: price_before = line.get('price', 0.0) price_unit_val_dif = price_unit - valuation_price_unit price_val_dif = price_before - interim_account_price if inv.currency_id.compare_amounts( price_unit, valuation_price_unit) != 0 and acc: # If the unit prices have not changed and we have a # valuation difference, it means this difference is due to exchange rates, # so we don't create anything, the exchange rate entries will # be processed automatically by the rest of the code. diff_line = { 'type': 'src', 'name': i_line.name[:64], 'price_unit': price_unit_val_dif, 'quantity': line['quantity'], 'price': inv.currency_id.round(price_val_dif), 'account_id': acc, 'product_id': line['product_id'], 'uom_id': line['uom_id'], 'account_analytic_id': line['account_analytic_id'], 'tax_ids': tax_ids, } # We update the original line accordingly # line['price_unit'] doesn't contain the discount, so use price_unit # instead. It could make sense to include the discount in line['price_unit'], # but that doesn't seem a good idea in stable since it is done in # "invoice_line_move_line_get" of "account.invoice". line['price_unit'] = inv.currency_id.round( price_unit - diff_line['price_unit']) line['price'] = inv.currency_id.round( line['price'] - diff_line['price']) diff_res.append(diff_line) return diff_res return []
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")