/**
  * Constructs a ContractManagerAssignmentDetail object from an existing
  * ContractManagerAssignmentDocument object.
  *
  * @param acmDocument the ContractManagerAssignmentDocument to copy from.
  * @param requisitionDocument reference to the related requisition document.
  */
 public ContractManagerAssignmentDetail(
     ContractManagerAssignmentDocument acmDocument, RequisitionDocument requisitionDocument) {
   this.documentNumber = acmDocument.getDocumentNumber();
   this.contractManagerAssignmentDocument = acmDocument;
   this.requisition = requisitionDocument;
   this.requisitionIdentifier = requisitionDocument.getPurapDocumentIdentifier();
   this.deliveryCampusCode = requisitionDocument.getDeliveryCampusCode();
   this.vendorName = requisitionDocument.getVendorName();
 }
 @ConfigureContext(session = parke)
 public void testUpdateItemAccountAmounts_ThreeAccounts() {
   PurapAccountingServiceFixture fixture = PurapAccountingServiceFixture.REQ_PRORATION_THIRDS;
   RequisitionDocument req = fixture.generateRequisitionDocument_OneItem();
   RequisitionItem item = (RequisitionItem) req.getItems().get(0);
   KualiDecimal total = item.getTotalAmount();
   purapAccountingService.updateItemAccountAmounts(item);
   PurApAccountingLine line1 = item.getSourceAccountingLines().get(0);
   assertTrue(line1.getAmount().compareTo(total.divide(new KualiDecimal(3))) == 0);
 }
  // mjmc
  // *************************************************************************************************
  private PurchaseOrderDocument copyNotesAndAttachmentsToPO(
      RequisitionDocument reqDoc, PurchaseOrderDocument poDoc) {

    purapService.saveDocumentNoValidation(poDoc);
    List<Note> notes = (List<Note>) reqDoc.getNotes();
    int noteLength = notes.size();
    if (noteLength > 0) {
      for (Note note : notes) {
        try {
          Note copyingNote =
              SpringContext.getBean(DocumentService.class)
                  .createNoteFromDocument(poDoc, note.getNoteText());
          purapService.saveDocumentNoValidation(poDoc);
          copyingNote.setNotePostedTimestamp(note.getNotePostedTimestamp());
          copyingNote.setAuthorUniversalIdentifier(note.getAuthorUniversalIdentifier());
          copyingNote.setNoteTopicText(note.getNoteTopicText());
          Attachment originalAttachment =
              SpringContext.getBean(AttachmentService.class)
                  .getAttachmentByNoteId(note.getNoteIdentifier());
          NoteExtendedAttribute noteExtendedAttribute = (NoteExtendedAttribute) note.getExtension();
          if (originalAttachment != null
              || (ObjectUtils.isNotNull(noteExtendedAttribute)
                  && noteExtendedAttribute.isCopyNoteIndicator())) {
            if (originalAttachment != null) {
              Attachment newAttachment =
                  SpringContext.getBean(AttachmentService.class)
                      .createAttachment(
                          (PersistableBusinessObject) copyingNote,
                          originalAttachment.getAttachmentFileName(),
                          originalAttachment.getAttachmentMimeTypeCode(),
                          originalAttachment.getAttachmentFileSize().intValue(),
                          originalAttachment.getAttachmentContents(),
                          originalAttachment.getAttachmentTypeCode()); // new Attachment();

              if (ObjectUtils.isNotNull(originalAttachment)
                  && ObjectUtils.isNotNull(newAttachment)) {
                copyingNote.addAttachment(newAttachment);
              }
            }

            poDoc.addNote(copyingNote);
          }

        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    }
    purapService.saveDocumentNoValidation(poDoc);
    return poDoc;
  } //  mjmc end
  public RequisitionDocument createRequisitionDocumentForTax(
      TaxTestCaseFixture taxTestCaseFixture) {

    RequisitionDocument doc = createRequisitionDocument();

    doc.getItem(0).getItemType().setTaxableIndicator(taxTestCaseFixture.isItemTypeTaxable());

    doc.setDeliveryStateCode("IN");

    if (taxTestCaseFixture.isItemTaxAmountNull()) {
      doc.getItem(0).setItemTaxAmount(null);
    } else {
      doc.getItem(0).setItemTaxAmount(new KualiDecimal(100));
    }

    doc.setUseTaxIndicator(taxTestCaseFixture.isUseTax());

    if (taxTestCaseFixture.iscommodityCodeNull()) {
      ((RequisitionItem) doc.getItem(0)).setPurchasingCommodityCode(null);
      ((RequisitionItem) doc.getItem(0)).setCommodityCode(null);
    } else {
      ((RequisitionItem) doc.getItem(0))
          .setCommodityCode(CommodityCodeFixture.COMMODITY_CODE_BASIC_ACTIVE.createCommodityCode());
    }

    String INVALID_VALUE = "XX";

    String fundGroupCode =
        doc.getItem(0)
            .getSourceAccountingLines()
            .get(0)
            .getAccount()
            .getSubFundGroup()
            .getFundGroupCode();
    String subFundGroupCode =
        doc.getItem(0)
            .getSourceAccountingLines()
            .get(0)
            .getAccount()
            .getSubFundGroup()
            .getSubFundGroupCode();
    String objectLevelCode =
        doc.getItem(0)
            .getSourceAccountingLines()
            .get(0)
            .getObjectCode()
            .getFinancialObjectLevelCode();
    String consolidationObjectCode =
        doc.getItem(0)
            .getSourceAccountingLines()
            .get(0)
            .getObjectCode()
            .getFinancialObjectLevel()
            .getFinancialConsolidationObjectCode();

    String parameterSuffix;

    if (taxTestCaseFixture.isDeliveryStateTaxable()) {
      TestUtils.setSystemParameter(
          KfsParameterConstants.PURCHASING_DOCUMENT.class,
          TaxParameters.TAXABLE_DELIVERY_STATES,
          doc.getDeliveryStateCode());
      parameterSuffix = "FOR_TAXABLE_STATES";
    } else {
      TestUtils.setSystemParameter(
          KfsParameterConstants.PURCHASING_DOCUMENT.class,
          TaxParameters.TAXABLE_DELIVERY_STATES,
          INVALID_VALUE);
      parameterSuffix = "FOR_NON_TAXABLE_STATES";
    }

    if (taxTestCaseFixture.isFundGroupCodeTaxable()) {
      TestUtils.setSystemParameter(
          KfsParameterConstants.PURCHASING_DOCUMENT.class,
          "TAXABLE_FUND_GROUPS_" + parameterSuffix,
          fundGroupCode);
      TestUtils.setSystemParameter(
          KfsParameterConstants.PURCHASING_DOCUMENT.class,
          "TAXABLE_SUB_FUND_GROUPS_" + parameterSuffix,
          subFundGroupCode);
    } else {
      // Just put some invalid value
      TestUtils.setSystemParameter(
          KfsParameterConstants.PURCHASING_DOCUMENT.class,
          "TAXABLE_FUND_GROUPS_" + parameterSuffix,
          INVALID_VALUE);
      TestUtils.setSystemParameter(
          KfsParameterConstants.PURCHASING_DOCUMENT.class,
          "TAXABLE_SUB_FUND_GROUPS_" + parameterSuffix,
          INVALID_VALUE);
    }

    if (taxTestCaseFixture.isObjectCodeTaxable()) {
      TestUtils.setSystemParameter(
          KfsParameterConstants.PURCHASING_DOCUMENT.class,
          "TAXABLE_OBJECT_LEVELS_" + parameterSuffix,
          objectLevelCode);
      TestUtils.setSystemParameter(
          KfsParameterConstants.PURCHASING_DOCUMENT.class,
          "TAXABLE_OBJECT_CONSOLIDATIONS_" + parameterSuffix,
          consolidationObjectCode);
    } else {
      TestUtils.setSystemParameter(
          KfsParameterConstants.PURCHASING_DOCUMENT.class,
          "TAXABLE_OBJECT_LEVELS_" + parameterSuffix,
          INVALID_VALUE);
      TestUtils.setSystemParameter(
          KfsParameterConstants.PURCHASING_DOCUMENT.class,
          "TAXABLE_OBJECT_CONSOLIDATIONS_" + parameterSuffix,
          INVALID_VALUE);
    }

    if (taxTestCaseFixture.isSalesTaxEnabled()) {
      TestUtils.setSystemParameter(
          KfsParameterConstants.PURCHASING_DOCUMENT.class,
          PurapParameterConstants.ENABLE_SALES_TAX_IND,
          "Y");
    } else {
      TestUtils.setSystemParameter(
          KfsParameterConstants.PURCHASING_DOCUMENT.class,
          PurapParameterConstants.ENABLE_SALES_TAX_IND,
          "N");
    }

    return doc;
  }
  public RequisitionDocument createRequisitionDocument() {
    RequisitionDocument doc =
        purchasingDocumentFixture.createRequisitionDocument(purapDocumentFixture);
    doc.setRequisitionOrganizationReference1Text(this.requisitionOrganizationReference1Text);
    doc.setRequisitionOrganizationReference2Text(this.requisitionOrganizationReference2Text);
    doc.setRequisitionOrganizationReference3Text(this.requisitionOrganizationReference3Text);
    doc.setAlternate1VendorName(this.alternate1VendorName);
    doc.setAlternate2VendorName(this.alternate2VendorName);
    doc.setAlternate3VendorName(this.alternate3VendorName);
    doc.setAlternate4VendorName(this.alternate4VendorName);
    doc.setAlternate5VendorName(this.alternate5VendorName);
    doc.setOrganizationAutomaticPurchaseOrderLimit(this.organizationAutomaticPurchaseOrderLimit);

    for (RequisitionItemFixture requisitionItemFixture : requisitionItemFixtures) {
      requisitionItemFixture.addTo(doc);
    }
    doc.fixItemReferences();
    doc.setAccountDistributionMethod("S");
    doc.refreshNonUpdateableReferences();
    return doc;
  }
  /**
   * Creates a requisition document based on information from an I Want document.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   * @throws Exception
   */
  @SuppressWarnings("deprecation")
  public ActionForward createReqFromIWantDoc(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    String iWantDocumentNumber = request.getParameter(KRADConstants.PARAMETER_DOC_ID);
    RequisitionForm requisitionForm = (RequisitionForm) form;

    IWantDocument iWantDocument =
        (IWantDocument) getDocumentService().getByDocumentHeaderId(iWantDocumentNumber);
    if (iWantDocument == null) {
      throw new WorkflowException(
          "Could not find IWantDocument with ID '" + iWantDocumentNumber + "'");
    }

    // Make sure the user is authorized to create the req in this manner.
    TransactionalDocumentPresentationController pControl =
        (TransactionalDocumentPresentationController)
            getDocumentHelperService().getDocumentPresentationController(iWantDocument);
    TransactionalDocumentAuthorizer authorizer =
        (TransactionalDocumentAuthorizer)
            getDocumentHelperService().getDocumentAuthorizer(iWantDocument);
    Set<String> iwntEditModes =
        authorizer.getEditModes(
            iWantDocument,
            GlobalVariables.getUserSession().getPerson(),
            pControl.getEditModes(iWantDocument));
    if (!iwntEditModes.contains(CUPurapConstants.IWNT_DOC_CREATE_REQ)) {
      throw new AuthorizationException(
          GlobalVariables.getUserSession().getPrincipalId(),
          CUPurapConstants.IWNT_DOC_CREATE_REQ,
          CuRequisitionDocument.class.getSimpleName(),
          "user is not authorized to create requisitions from IWantDocument '"
              + iWantDocumentNumber
              + "'",
          Collections.<String, Object>emptyMap());
    }

    // Do not allow the req to be created if the IWNT doc is already associated with another req.
    if (iWantDocument != null
        && (StringUtils.isNotBlank(iWantDocument.getReqsDocId())
            || StringUtils.isNotBlank(iWantDocument.getDvDocId()))) {
      throw new WorkflowException(
          "Cannot create requisition from IWantDocument '"
              + iWantDocumentNumber
              + "' because a DV or Requisition has already been created from that document");
    }

    IWantDocumentService iWantDocumentService = SpringContext.getBean(IWantDocumentService.class);

    createDocument(requisitionForm);

    RequisitionDocument requisitionDocument = requisitionForm.getRequisitionDocument();

    iWantDocumentService.setUpRequisitionDetailsFromIWantDoc(
        iWantDocument, requisitionDocument, requisitionForm);

    // Set the requisition doc ID reference on the IWantDocument.
    iWantDocument.setReqsDocId(requisitionDocument.getDocumentNumber());
    SpringContext.getBean(PurapService.class).saveDocumentNoValidation(iWantDocument);

    return mapping.findForward(RiceConstants.MAPPING_BASIC);
  }