001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.control.user.purchase.server.command; 018 019import com.echothree.control.user.purchase.common.form.CreatePurchaseInvoiceForm; 020import com.echothree.control.user.purchase.common.result.PurchaseResultFactory; 021import com.echothree.model.control.accounting.server.control.AccountingControl; 022import com.echothree.model.control.contact.server.control.ContactControl; 023import com.echothree.model.control.invoice.common.workflow.PurchaseInvoiceStatusConstants; 024import com.echothree.model.control.invoice.server.logic.PurchaseInvoiceLogic; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.control.shipment.server.logic.FreeOnBoardLogic; 030import com.echothree.model.control.term.server.logic.TermLogic; 031import com.echothree.model.control.vendor.server.control.VendorControl; 032import com.echothree.model.data.invoice.server.entity.Invoice; 033import com.echothree.model.data.party.server.entity.Party; 034import com.echothree.model.data.user.common.pk.UserVisitPK; 035import com.echothree.util.common.command.BaseResult; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.common.validation.FieldDefinition; 038import com.echothree.util.common.validation.FieldType; 039import com.echothree.util.server.control.BaseSimpleCommand; 040import com.echothree.util.server.control.CommandSecurityDefinition; 041import com.echothree.util.server.control.PartyTypeDefinition; 042import com.echothree.util.server.control.SecurityRoleDefinition; 043import com.echothree.util.server.persistence.Session; 044import java.util.Arrays; 045import java.util.Collections; 046import java.util.List; 047import javax.enterprise.context.RequestScoped; 048 049@RequestScoped 050public class CreatePurchaseInvoiceCommand 051 extends BaseSimpleCommand<CreatePurchaseInvoiceForm> { 052 053 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 054 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 055 056 static { 057 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 058 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 059 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 060 new SecurityRoleDefinition(SecurityRoleGroups.PurchaseInvoice.name(), SecurityRoles.Create.name()) 061 ))) 062 ))); 063 064 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 065 new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("BillFromPartyName", FieldType.ENTITY_NAME, false, null, null), 067 new FieldDefinition("BillFromContactMechanismName", FieldType.ENTITY_NAME, false, null, null), 068 new FieldDefinition("VendorName", FieldType.ENTITY_NAME, false, null, null), 069 new FieldDefinition("BillToPartyName", FieldType.ENTITY_NAME, false, null, null), 070 new FieldDefinition("BillToContactMechanismName", FieldType.ENTITY_NAME, false, null, null), 071 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null), 072 new FieldDefinition("InvoicedTime", FieldType.DATE_TIME, false, null, null), 073 new FieldDefinition("DueTime", FieldType.DATE_TIME, false, null, null), 074 new FieldDefinition("PaidTime", FieldType.DATE_TIME, false, null, null), 075 new FieldDefinition("TermName", FieldType.ENTITY_NAME, false, null, null), 076 new FieldDefinition("FreeOnBoardName", FieldType.ENTITY_NAME, false, null, null), 077 new FieldDefinition("Reference", FieldType.STRING, false, 1L, 40L), 078 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 079 )); 080 } 081 082 /** Creates a new instance of CreatePurchaseInvoiceCommand */ 083 public CreatePurchaseInvoiceCommand() { 084 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 085 } 086 087 @Override 088 protected BaseResult execute() { 089 var result = PurchaseResultFactory.getCreatePurchaseInvoiceResult(); 090 Invoice invoice = null; 091 var accountingControl = Session.getModelController(AccountingControl.class); 092 var currencyIsoName = form.getCurrencyIsoName(); 093 var currency = currencyIsoName == null ? null : accountingControl.getCurrencyByIsoName(currencyIsoName); 094 095 if(currencyIsoName == null || currency != null) { 096 var termName = form.getTermName(); 097 var freeOnBoardName = form.getFreeOnBoardName(); 098 var term = termName == null ? null : TermLogic.getInstance().getTermByName(this, termName); 099 var freeOnBoard = freeOnBoardName == null ? null : FreeOnBoardLogic.getInstance().getFreeOnBoardByName(this, freeOnBoardName); 100 101 if(!hasExecutionErrors()) { 102 var vendorControl = Session.getModelController(VendorControl.class); 103 var vendorName = form.getVendorName(); 104 var billFromPartyName = form.getBillFromPartyName(); 105 var parameterCount = (vendorName == null ? 0 : 1) + (billFromPartyName == null ? 0 : 1); 106 107 if(parameterCount == 1) { 108 var partyControl = Session.getModelController(PartyControl.class); 109 Party billFrom = null; 110 111 if(vendorName == null) { 112 billFrom = partyControl.getPartyByName(billFromPartyName); 113 114 if(billFrom != null) { 115 if(!billFrom.getLastDetail().getPartyType().getPartyTypeName().equals(PartyTypes.VENDOR.name())) { 116 addExecutionError(ExecutionErrors.InvalidBillFromPartyType.name()); 117 } 118 } else { 119 addExecutionError(ExecutionErrors.UnknownBillFromPartyName.name(), billFromPartyName); 120 } 121 } else { 122 var vendor = vendorControl.getVendorByName(vendorName); 123 124 if(vendor != null) { 125 billFrom = vendor.getParty(); 126 } else { 127 addExecutionError(ExecutionErrors.UnknownVendorName.name(), vendorName); 128 } 129 } 130 131 if(!hasExecutionErrors()) { 132 var contactControl = Session.getModelController(ContactControl.class); 133 var billFromContactMechanismName = form.getBillFromContactMechanismName(); 134 var billFromContactMechanism = billFromContactMechanismName == null? null: contactControl.getPartyContactMechanismByContactMechanismName(this, billFrom, billFromContactMechanismName); 135 136 if(billFromContactMechanismName == null || billFromContactMechanism != null) { 137 var companyName = form.getCompanyName(); 138 var billToPartyName = form.getBillToPartyName(); 139 140 parameterCount = (companyName == null ? 0 : 1) + (billToPartyName == null ? 0 : 1); 141 142 if(parameterCount < 2) { 143 Party billTo = null; 144 145 if(companyName != null) { 146 var partyCompany = partyControl.getPartyCompanyByName(companyName); 147 148 if(partyCompany != null) { 149 billTo = partyCompany.getParty(); 150 } else { 151 addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName); 152 } 153 } else if(billToPartyName != null) { 154 billTo = partyControl.getPartyByName(billToPartyName); 155 156 if(billTo != null) { 157 if(!billTo.getLastDetail().getPartyType().getPartyTypeName().equals(PartyTypes.COMPANY.name())) { 158 addExecutionError(ExecutionErrors.InvalidBillToPartyType.name()); 159 } 160 } else { 161 addExecutionError(ExecutionErrors.UnknownBillToPartyName.name(), billToPartyName); 162 } 163 } else { 164 billTo = getUserSession().getPartyRelationship().getFromParty(); 165 } 166 167 if(!hasExecutionErrors()) { 168 var billToContactMechanismName = form.getBillToContactMechanismName(); 169 var billToContactMechanism = billToContactMechanismName == null ? null : contactControl.getPartyContactMechanismByContactMechanismName(this, billTo, billToContactMechanismName); 170 171 if(billToContactMechanismName == null || billToContactMechanism != null) { 172 var strInvoicedTime = form.getInvoicedTime(); 173 var invoicedTime = strInvoicedTime == null ? null : Long.valueOf(strInvoicedTime); 174 var strDueTime = form.getDueTime(); 175 var dueTime = strDueTime == null ? null : Long.valueOf(strDueTime); 176 var strPaidTime = form.getPaidTime(); 177 var paidTime = strPaidTime == null ? null : Long.valueOf(strPaidTime); 178 var reference = form.getReference(); 179 var description = form.getDescription(); 180 181 invoice = PurchaseInvoiceLogic.getInstance().createInvoice(session, this, billFrom, billFromContactMechanism, billTo, 182 billToContactMechanism, currency, term, freeOnBoard, reference, description, invoicedTime, dueTime, paidTime, 183 PurchaseInvoiceStatusConstants.WorkflowEntrance_NEW_ENTRY, getPartyPK()); 184 } 185 } 186 } else { 187 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 188 } 189 } 190 } 191 } else { 192 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 193 } 194 } 195 } else { 196 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 197 } 198 199 if(invoice != null) { 200 result.setEntityRef(invoice.getPrimaryKey().getEntityRef()); 201 result.setInvoiceName(invoice.getLastDetail().getInvoiceName()); 202 } 203 204 return result; 205 } 206 207}