Skip to content

Commit

Permalink
Merge pull request #5997 from msupply-foundation/5918-transfer-indica…
Browse files Browse the repository at this point in the history
…tor-values

Add generate transfer requisition indicator values
  • Loading branch information
fergie-nz authored Jan 23, 2025
2 parents c2c1be0 + 5c7d27f commit eff076d
Showing 1 changed file with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use crate::{
use super::{RequisitionTransferProcessor, RequisitionTransferProcessorRecord};
use chrono::Utc;
use repository::{
ActivityLogType, ApprovalStatusType, EqualFilter, ItemRow, NumberRowType, RepositoryError,
Requisition, RequisitionLine, RequisitionLineRow, RequisitionLineRowRepository, RequisitionRow,
indicator_value::{IndicatorValueFilter, IndicatorValueRepository},
ActivityLogType, ApprovalStatusType, EqualFilter, IndicatorValueRow,
IndicatorValueRowRepository, ItemRow, NumberRowType, RepositoryError, Requisition,
RequisitionLine, RequisitionLineRow, RequisitionLineRowRepository, RequisitionRow,
RequisitionRowRepository, RequisitionStatus, RequisitionType, StorageConnection, StoreFilter,
StoreRepository,
};
Expand Down Expand Up @@ -95,6 +97,33 @@ impl RequisitionTransferProcessor for CreateResponseRequisitionProcessor {
requisition_line_row_repository.upsert_one(line)?;
}

let customer_name_id = StoreRepository::new(connection)
.query_by_filter(
StoreFilter::new().id(EqualFilter::equal_to(&request_requisition.store_row.id)),
)?
.pop()
.ok_or(RepositoryError::NotFound)?
.name_row
.id;

let generate_indicator_value_input = GenerateTransferIndicatorInput {
customer_store_id: request_requisition.store_row.id.clone(),
supplier_store_id: record_for_processing.other_party_store_id.clone(),
customer_name_id,
period_id: request_requisition.period.clone().map(|p| p.id),
};

let new_indicator_values = generate_response_requisition_indicator_values(
connection,
generate_indicator_value_input,
)?;

let indicator_value_repository = IndicatorValueRowRepository::new(connection);

for value in new_indicator_values.iter() {
indicator_value_repository.upsert_one(value)?;
}

let result = format!(
"requisition ({}) lines ({:?}) source requisition ({})",
new_response_requisition.id,
Expand Down Expand Up @@ -243,3 +272,38 @@ fn generate_response_requisition_lines(

Ok(response_lines)
}

struct GenerateTransferIndicatorInput {
customer_store_id: String,
supplier_store_id: String,
customer_name_id: String,
period_id: Option<String>,
}

fn generate_response_requisition_indicator_values(
connection: &StorageConnection,
input: GenerateTransferIndicatorInput,
) -> Result<Vec<IndicatorValueRow>, RepositoryError> {
if let Some(period_id) = input.period_id {
let supplier_store_id = input.supplier_store_id.clone();
let filter = IndicatorValueFilter::new()
.store_id(EqualFilter::equal_to(&input.customer_store_id))
.customer_name_id(EqualFilter::equal_to(&input.customer_name_id))
.period_id(EqualFilter::equal_to(&period_id));

let request_indicator_values =
IndicatorValueRepository::new(connection).query_by_filter(filter)?;

let response_indicator_values = request_indicator_values
.into_iter()
.map(|v| IndicatorValueRow {
id: uuid(),
store_id: supplier_store_id.clone(),
..v.indicator_value_row
})
.collect();

return Ok(response_indicator_values);
}
Ok(vec![])
}

0 comments on commit eff076d

Please sign in to comment.