<commit_before>package org.openmrs.module.openhmis.inventory.api.model;

import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.module.openhmis.commons.api.entity.model.BaseCustomizableInstanceMetadata;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * This class represents an operation performed on item stock.  Examples of an operation are things like transferring
 * item stock from one stockroom to another or selling item stock to a patient.  Each of these types of operations is
 * modeled by a specific {@link IStockOperationType} class that defines what happens when a stock operation is created
 * (PENDING), cancelled, or completed.
 *
 * A stock operation is composed of stock transactions, each of which is a change in item stock for a specific
 * stockroom.  When a stock operation is created transactions the item stock to be operated upon are added to the
 * {@code reserved} set.  As the stock operation status changes the item stock is updated as per the settings for the
 * operation type.
 */
public class StockOperation
		extends BaseCustomizableInstanceMetadata<IStockOperationType, StockOperationAttribute>
		implements Comparable<StockOperation> {
	public static final long serialVersionUID = 0L;

	private Integer id;
	private StockOperationStatus status;
	private Set<ReservedTransaction> reserved;
	private Set<StockOperationTransaction> transactions;

	private String operationNumber;
	protected StockRoom source;
	protected StockRoom destination;
	protected Patient patient;

	@Override
	public Integer getId() {
		return id;
	}

	@Override
	public void setId(Integer id) {
		this.id = id;
	}

	public StockOperationStatus getStatus() {
		return status;
	}

	public void setStatus(StockOperationStatus status) {
		this.status = status;
	}

	public String getOperationNumber() {
		return operationNumber;
	}

	public void setOperationNumber(String operationNumber) {
		this.operationNumber = operationNumber;
	}

	public StockRoom getSource() {
		return source;
	}

	public void setSource(StockRoom newSource) {
		if (this.source == newSource) {
			return;
		}

		// If source exists then remove this operation from it
		if (this.source != null && this.source != this.destination) {
			this.source.removeOperation(this);
		}

		// Update the source
		this.source = newSource;

		// If the new source is not null then add this operation to it
		if (this.source != null) {
			this.source.addOperation(this);
		}
	}

	public StockRoom getDestination() {
		return destination;
	}

	public void setDestination(StockRoom newDestination) {
		if (this.destination == newDestination) {
			return;
		}

		// If destination exists then remove this operation from it
		if (this.destination != null && this.destination != this.source) {
			this.destination.removeOperation(this);
		}

		// Update the destination
		this.destination = newDestination;

		// If the new destination is not null then add this operation to it
		if (this.destination != null) {
			this.destination.addOperation(this);
		}
	}

	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	public ReservedTransaction addReserved(Item item, int quantity) {
		return addReserved(item, null, quantity);
	}

	/**
	 * Adds a new reserved item.
	 *
	 * @param item The item to add.
	 * @param expiration The item expiration or {@code null} if none.
	 * @param quantity The item quantity.
	 * @return The newly created reserved item transaction.
	 */
	public ReservedTransaction addReserved(Item item, Date expiration, int quantity) {
		// Do checks and stuff

		ReservedTransaction tx = new ReservedTransaction();
		tx.setItem(item);
		tx.setQuantity(quantity);
		tx.setExpiration(expiration);
		tx.setCreator(Context.getAuthenticatedUser());
		tx.setDateCreated(new Date());

		return addReserved(tx);
	}

	/**
	 * Adds a new reserved item.
	 * @param tx The reserved item transaction to add.
	 * @return The saved reserved item transaction.<commit_msg>Changed the signature of the StockOperation.addReserved method to be consistent with other overloads.<commit_after>package org.openmrs.module.openhmis.inventory.api.model;

import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.module.openhmis.commons.api.entity.model.BaseCustomizableInstanceMetadata;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * This class represents an operation performed on item stock.  Examples of an operation are things like transferring
 * item stock from one stockroom to another or selling item stock to a patient.  Each of these types of operations is
 * modeled by a specific {@link IStockOperationType} class that defines what happens when a stock operation is created
 * (PENDING), cancelled, or completed.
 *
 * A stock operation is composed of stock transactions, each of which is a change in item stock for a specific
 * stockroom.  When a stock operation is created transactions the item stock to be operated upon are added to the
 * {@code reserved} set.  As the stock operation status changes the item stock is updated as per the settings for the
 * operation type.
 */
public class StockOperation
		extends BaseCustomizableInstanceMetadata<IStockOperationType, StockOperationAttribute>
		implements Comparable<StockOperation> {
	public static final long serialVersionUID = 0L;

	private Integer id;
	private StockOperationStatus status;
	private Set<ReservedTransaction> reserved;
	private Set<StockOperationTransaction> transactions;

	private String operationNumber;
	protected StockRoom source;
	protected StockRoom destination;
	protected Patient patient;

	@Override
	public Integer getId() {
		return id;
	}

	@Override
	public void setId(Integer id) {
		this.id = id;
	}

	public StockOperationStatus getStatus() {
		return status;
	}

	public void setStatus(StockOperationStatus status) {
		this.status = status;
	}

	public String getOperationNumber() {
		return operationNumber;
	}

	public void setOperationNumber(String operationNumber) {
		this.operationNumber = operationNumber;
	}

	public StockRoom getSource() {
		return source;
	}

	public void setSource(StockRoom newSource) {
		if (this.source == newSource) {
			return;
		}

		// If source exists then remove this operation from it
		if (this.source != null && this.source != this.destination) {
			this.source.removeOperation(this);
		}

		// Update the source
		this.source = newSource;

		// If the new source is not null then add this operation to it
		if (this.source != null) {
			this.source.addOperation(this);
		}
	}

	public StockRoom getDestination() {
		return destination;
	}

	public void setDestination(StockRoom newDestination) {
		if (this.destination == newDestination) {
			return;
		}

		// If destination exists then remove this operation from it
		if (this.destination != null && this.destination != this.source) {
			this.destination.removeOperation(this);
		}

		// Update the destination
		this.destination = newDestination;

		// If the new destination is not null then add this operation to it
		if (this.destination != null) {
			this.destination.addOperation(this);
		}
	}

	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}

	public ReservedTransaction addReserved(Item item, int quantity) {
		return addReserved(item, quantity, null);
	}

	/**
	 * Adds a new reserved item.
	 *
	 *
	 * @param item The item to add.
	 * @param quantity The item quantity.
	 * @param expiration The item expiration or {@code null} if none.
	 * @return The newly created reserved item transaction.
	 */
	public ReservedTransaction addReserved(Item item, int quantity, Date expiration) {
		// Do checks and stuff

		ReservedTransaction tx = new ReservedTransaction();
		tx.setItem(item);
		tx.setQuantity(quantity);
		tx.setExpiration(expiration);
		tx.setCreator(Context.getAuthenticatedUser());
		tx.setDateCreated(new Date());

		return addReserved(tx);
	}

	/**
	 * Adds a new reserved item.
	 * @param tx The reserved item transaction to add.
	 * @return The saved reserved item transaction.
{'fraction_non_alphanumeric': 0.08136706397470202, 'fraction_numerical': 0.00024324981756263683, 'mean_word_length': 4.184741488020177, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4268048', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2219, 'n_words': 819}