一般情况下,手动创建一条制造单 添加数据,点击保存。可以正常流转该单据。但是是通过后台代码,创建一条制造单(mrp.production)数据 再流转该制造订单时,完成该数据的时候,会提示 待生产的数量必须是正数
这是因为,在页面完成时,调用的是 button_mark_done 方法 该方法会将 制造单的product_qty
更改为 qty_produced
def button_mark_done(self): ... for production in self: production.write({ 'date_finished': fields.Datetime.now(), 'product_qty': production.qty_produced, 'priority': '0', 'is_locked': True, })
如果此时是通过后台代码创建的数据,该qty_produced 为0,就会触发sql约束,product_qty的数值是必须大于0,就会提示待生产的数量必须是正数
_sql_constraints = [ ('name_uniq', 'unique(name, company_id)', 'Reference must be unique per Company!'), ('qty_positive', 'check (product_qty > 0)', 'The quantity to produce must be positive!'), ]
qty_produced的数据记录逻辑是跟 moves_finished_values 字段有关的 在原生逻辑里面 制造订单添加产品 会动态触发_create_update_move_finished 方法 会添加moves_finished_values的数据
_create_update_move_finished 方法
def _create_update_move_finished(self): """ This is a helper function to support complexity of onchange logic for MOs. It is important that the special *2Many commands used here remain as long as function is used within onchanges. """ # keep manual entries list_move_finished = [(4, move.id) for move in self.move_finished_ids.filtered( lambda m: not m.byproduct_id and m.product_id != self.product_id)] list_move_finished = [] moves_finished_values = self._get_moves_finished_values() moves_byproduct_dict = {move.byproduct_id.id: move for move in self.move_finished_ids.filtered(lambda m: m.byproduct_id)} move_finished = self.move_finished_ids.filtered(lambda m: m.product_id == self.product_id) for move_finished_values in moves_finished_values: if move_finished_values.get('byproduct_id') in moves_byproduct_dict: # update existing entries list_move_finished += [(1, moves_byproduct_dict[move_finished_values['byproduct_id']].id, move_finished_values)] elif move_finished_values.get('product_id') == self.product_id.id and move_finished: list_move_finished += [(1, move_finished.id, move_finished_values)] else: # add new entries list_move_finished += [(0, 0, move_finished_values)] self.move_finished_ids = list_move_finished
而如果通过代码创建的 制造单就不会 有moves_finished_values的数据 就会导致qty_produced的数值为0 所有 要想解决该问题,让制造单流转下去 就需要在创建制造单的时候,只能手动添加 move_finished_ids。这样就可以,成功的完成该订单。