001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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 java.util.List; 044import javax.enterprise.context.Dependent; 045import javax.inject.Inject; 046 047@Dependent 048public class CreatePurchaseInvoiceCommand 049 extends BaseSimpleCommand<CreatePurchaseInvoiceForm> { 050 051 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 052 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 053 054 static { 055 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 056 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 057 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 058 new SecurityRoleDefinition(SecurityRoleGroups.PurchaseInvoice.name(), SecurityRoles.Create.name()) 059 )) 060 )); 061 062 FORM_FIELD_DEFINITIONS = List.of( 063 new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("BillFromPartyName", FieldType.ENTITY_NAME, false, null, null), 065 new FieldDefinition("BillFromContactMechanismName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("VendorName", FieldType.ENTITY_NAME, false, null, null), 067 new FieldDefinition("BillToPartyName", FieldType.ENTITY_NAME, false, null, null), 068 new FieldDefinition("BillToContactMechanismName", FieldType.ENTITY_NAME, false, null, null), 069 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null), 070 new FieldDefinition("InvoicedTime", FieldType.DATE_TIME, false, null, null), 071 new FieldDefinition("DueTime", FieldType.DATE_TIME, false, null, null), 072 new FieldDefinition("PaidTime", FieldType.DATE_TIME, false, null, null), 073 new FieldDefinition("TermName", FieldType.ENTITY_NAME, false, null, null), 074 new FieldDefinition("FreeOnBoardName", FieldType.ENTITY_NAME, false, null, null), 075 new FieldDefinition("Reference", FieldType.STRING, false, 1L, 40L), 076 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 077 ); 078 } 079 080 @Inject 081 AccountingControl accountingControl; 082 083 @Inject 084 ContactControl contactControl; 085 086 @Inject 087 PartyControl partyControl; 088 089 @Inject 090 VendorControl vendorControl; 091 092 @Inject 093 FreeOnBoardLogic freeOnBoardLogic; 094 095 @Inject 096 PurchaseInvoiceLogic purchaseInvoiceLogic; 097 098 @Inject 099 TermLogic termLogic; 100 101 102 /** Creates a new instance of CreatePurchaseInvoiceCommand */ 103 public CreatePurchaseInvoiceCommand() { 104 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 105 } 106 107 @Override 108 protected BaseResult execute() { 109 var result = PurchaseResultFactory.getCreatePurchaseInvoiceResult(); 110 Invoice invoice = null; 111 var currencyIsoName = form.getCurrencyIsoName(); 112 var currency = currencyIsoName == null ? null : accountingControl.getCurrencyByIsoName(currencyIsoName); 113 114 if(currencyIsoName == null || currency != null) { 115 var termName = form.getTermName(); 116 var freeOnBoardName = form.getFreeOnBoardName(); 117 var term = termName == null ? null : termLogic.getTermByName(this, termName); 118 var freeOnBoard = freeOnBoardName == null ? null : freeOnBoardLogic.getFreeOnBoardByName(this, freeOnBoardName); 119 120 if(!hasExecutionErrors()) { 121 var vendorName = form.getVendorName(); 122 var billFromPartyName = form.getBillFromPartyName(); 123 var parameterCount = (vendorName == null ? 0 : 1) + (billFromPartyName == null ? 0 : 1); 124 125 if(parameterCount == 1) { 126 Party billFrom = null; 127 128 if(vendorName == null) { 129 billFrom = partyControl.getPartyByName(billFromPartyName); 130 131 if(billFrom != null) { 132 if(!billFrom.getLastDetail().getPartyType().getPartyTypeName().equals(PartyTypes.VENDOR.name())) { 133 addExecutionError(ExecutionErrors.InvalidBillFromPartyType.name()); 134 } 135 } else { 136 addExecutionError(ExecutionErrors.UnknownBillFromPartyName.name(), billFromPartyName); 137 } 138 } else { 139 var vendor = vendorControl.getVendorByName(vendorName); 140 141 if(vendor != null) { 142 billFrom = vendor.getParty(); 143 } else { 144 addExecutionError(ExecutionErrors.UnknownVendorName.name(), vendorName); 145 } 146 } 147 148 if(!hasExecutionErrors()) { 149 var billFromContactMechanismName = form.getBillFromContactMechanismName(); 150 var billFromContactMechanism = billFromContactMechanismName == null? null: contactControl.getPartyContactMechanismByContactMechanismName(this, billFrom, billFromContactMechanismName); 151 152 if(billFromContactMechanismName == null || billFromContactMechanism != null) { 153 var companyName = form.getCompanyName(); 154 var billToPartyName = form.getBillToPartyName(); 155 156 parameterCount = (companyName == null ? 0 : 1) + (billToPartyName == null ? 0 : 1); 157 158 if(parameterCount < 2) { 159 Party billTo = null; 160 161 if(companyName != null) { 162 var partyCompany = partyControl.getPartyCompanyByName(companyName); 163 164 if(partyCompany != null) { 165 billTo = partyCompany.getParty(); 166 } else { 167 addExecutionError(ExecutionErrors.UnknownCompanyName.name(), companyName); 168 } 169 } else if(billToPartyName != null) { 170 billTo = partyControl.getPartyByName(billToPartyName); 171 172 if(billTo != null) { 173 if(!billTo.getLastDetail().getPartyType().getPartyTypeName().equals(PartyTypes.COMPANY.name())) { 174 addExecutionError(ExecutionErrors.InvalidBillToPartyType.name()); 175 } 176 } else { 177 addExecutionError(ExecutionErrors.UnknownBillToPartyName.name(), billToPartyName); 178 } 179 } else { 180 billTo = getUserSession().getPartyRelationship().getFromParty(); 181 } 182 183 if(!hasExecutionErrors()) { 184 var billToContactMechanismName = form.getBillToContactMechanismName(); 185 var billToContactMechanism = billToContactMechanismName == null ? null : contactControl.getPartyContactMechanismByContactMechanismName(this, billTo, billToContactMechanismName); 186 187 if(billToContactMechanismName == null || billToContactMechanism != null) { 188 var strInvoicedTime = form.getInvoicedTime(); 189 var invoicedTime = strInvoicedTime == null ? null : Long.valueOf(strInvoicedTime); 190 var strDueTime = form.getDueTime(); 191 var dueTime = strDueTime == null ? null : Long.valueOf(strDueTime); 192 var strPaidTime = form.getPaidTime(); 193 var paidTime = strPaidTime == null ? null : Long.valueOf(strPaidTime); 194 var reference = form.getReference(); 195 var description = form.getDescription(); 196 197 invoice = purchaseInvoiceLogic.createInvoice(session, this, billFrom, billFromContactMechanism, billTo, 198 billToContactMechanism, currency, term, freeOnBoard, reference, description, invoicedTime, dueTime, paidTime, 199 PurchaseInvoiceStatusConstants.WorkflowEntrance_NEW_ENTRY, getPartyPK()); 200 } 201 } 202 } else { 203 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 204 } 205 } 206 } 207 } else { 208 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 209 } 210 } 211 } else { 212 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 213 } 214 215 if(invoice != null) { 216 result.setEntityRef(invoice.getPrimaryKey().getEntityRef()); 217 result.setInvoiceName(invoice.getLastDetail().getInvoiceName()); 218 } 219 220 return result; 221 } 222 223}