diff --git a/app/Models/Payment.php b/app/Models/Payment.php index d32630b89720..4502b389a405 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -94,42 +94,6 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @method static \Illuminate\Database\Eloquent\Builder|Payment onlyTrashed() * @method static \Illuminate\Database\Eloquent\Builder|Payment query() * @method static \Illuminate\Database\Eloquent\Builder|BaseModel scope() - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereAmount($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereApplied($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereAssignedUserId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereClientContactId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereClientId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereCompanyGatewayId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereCompanyId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereCreatedAt($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereCurrencyId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereCustomValue1($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereCustomValue2($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereCustomValue3($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereCustomValue4($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereDate($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereDeletedAt($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereExchangeCurrencyId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereExchangeRate($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereGatewayTypeId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereIdempotencyKey($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereInvitationId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereIsDeleted($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereIsManual($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereMeta($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereNumber($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment wherePayerId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment wherePrivateNotes($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereProjectId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereRefunded($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereStatusId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereTransactionId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereTransactionReference($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereTypeId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereUpdatedAt($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereUserId($value) - * @method static \Illuminate\Database\Eloquent\Builder|Payment whereVendorId($value) * @method static \Illuminate\Database\Eloquent\Builder|Payment withTrashed() * @method static \Illuminate\Database\Eloquent\Builder|Payment withoutTrashed() * @property-read \Illuminate\Database\Eloquent\Collection $company_ledger @@ -137,61 +101,6 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @property-read \Illuminate\Database\Eloquent\Collection $documents * @property-read \Illuminate\Database\Eloquent\Collection $invoices * @property-read \Illuminate\Database\Eloquent\Collection $paymentables - * @property-read \Illuminate\Database\Eloquent\Collection $company_ledger - * @property-read \Illuminate\Database\Eloquent\Collection $credits - * @property-read \Illuminate\Database\Eloquent\Collection $documents - * @property-read \Illuminate\Database\Eloquent\Collection $invoices - * @property-read \Illuminate\Database\Eloquent\Collection $paymentables - * @property-read \Illuminate\Database\Eloquent\Collection $company_ledger - * @property-read \Illuminate\Database\Eloquent\Collection $credits - * @property-read \Illuminate\Database\Eloquent\Collection $documents - * @property-read \Illuminate\Database\Eloquent\Collection $invoices - * @property-read \Illuminate\Database\Eloquent\Collection $paymentables - * @property-read \Illuminate\Database\Eloquent\Collection $company_ledger - * @property-read \Illuminate\Database\Eloquent\Collection $credits - * @property-read \Illuminate\Database\Eloquent\Collection $documents - * @property-read \Illuminate\Database\Eloquent\Collection $invoices - * @property-read \Illuminate\Database\Eloquent\Collection $paymentables - * @property-read \Illuminate\Database\Eloquent\Collection $company_ledger - * @property-read \Illuminate\Database\Eloquent\Collection $credits - * @property-read \Illuminate\Database\Eloquent\Collection $documents - * @property-read \Illuminate\Database\Eloquent\Collection $invoices - * @property-read \Illuminate\Database\Eloquent\Collection $paymentables - * @property-read \Illuminate\Database\Eloquent\Collection $company_ledger - * @property-read \Illuminate\Database\Eloquent\Collection $credits - * @property-read \Illuminate\Database\Eloquent\Collection $documents - * @property-read \Illuminate\Database\Eloquent\Collection $invoices - * @property-read \Illuminate\Database\Eloquent\Collection $paymentables - * @property-read \Illuminate\Database\Eloquent\Collection $company_ledger - * @property-read \Illuminate\Database\Eloquent\Collection $credits - * @property-read \Illuminate\Database\Eloquent\Collection $documents - * @property-read \Illuminate\Database\Eloquent\Collection $invoices - * @property-read \Illuminate\Database\Eloquent\Collection $paymentables - * @property-read \Illuminate\Database\Eloquent\Collection $company_ledger - * @property-read \Illuminate\Database\Eloquent\Collection $credits - * @property-read \Illuminate\Database\Eloquent\Collection $documents - * @property-read \Illuminate\Database\Eloquent\Collection $invoices - * @property-read \Illuminate\Database\Eloquent\Collection $paymentables - * @property-read \Illuminate\Database\Eloquent\Collection $company_ledger - * @property-read \Illuminate\Database\Eloquent\Collection $credits - * @property-read \Illuminate\Database\Eloquent\Collection $documents - * @property-read \Illuminate\Database\Eloquent\Collection $invoices - * @property-read \Illuminate\Database\Eloquent\Collection $paymentables - * @property-read \Illuminate\Database\Eloquent\Collection $company_ledger - * @property-read \Illuminate\Database\Eloquent\Collection $credits - * @property-read \Illuminate\Database\Eloquent\Collection $documents - * @property-read \Illuminate\Database\Eloquent\Collection $invoices - * @property-read \Illuminate\Database\Eloquent\Collection $paymentables - * @property-read \Illuminate\Database\Eloquent\Collection $company_ledger - * @property-read \Illuminate\Database\Eloquent\Collection $credits - * @property-read \Illuminate\Database\Eloquent\Collection $documents - * @property-read \Illuminate\Database\Eloquent\Collection $invoices - * @property-read \Illuminate\Database\Eloquent\Collection $paymentables - * @property-read \Illuminate\Database\Eloquent\Collection $company_ledger - * @property-read \Illuminate\Database\Eloquent\Collection $credits - * @property-read \Illuminate\Database\Eloquent\Collection $documents - * @property-read \Illuminate\Database\Eloquent\Collection $invoices - * @property-read \Illuminate\Database\Eloquent\Collection $paymentables * @mixin \Eloquent */ class Payment extends BaseModel diff --git a/app/Services/Invoice/EInvoice/FacturaEInvoice.php b/app/Services/Invoice/EInvoice/FacturaEInvoice.php index 4f995ddafa80..39f4a2f3bc0f 100644 --- a/app/Services/Invoice/EInvoice/FacturaEInvoice.php +++ b/app/Services/Invoice/EInvoice/FacturaEInvoice.php @@ -12,13 +12,15 @@ namespace App\Services\Invoice\EInvoice; use App\Models\Invoice; +use App\Models\PaymentType; use josemmo\Facturae\Facturae; use App\Services\AbstractService; use josemmo\Facturae\FacturaeItem; use josemmo\Facturae\FacturaeParty; +use josemmo\Facturae\FacturaeCentre; +use josemmo\Facturae\FacturaePayment; use Illuminate\Support\Facades\Storage; use josemmo\Facturae\Common\FacturaeSigner; -use josemmo\Facturae\FacturaeCentre; class FacturaEInvoice extends AbstractService { @@ -169,6 +171,9 @@ class FacturaEInvoice extends AbstractService ->buildItems() ->setDiscount() ->setPoNumber() + ->setLegalTerms() + ->setPayments() + ->setBillingPeriod() ->signDocument(); $disk = config('filesystems.default'); @@ -228,6 +233,134 @@ class FacturaEInvoice extends AbstractService return $this; } + private function setLegalTerms(): self + { + $this->fac->addLegalLiteral(substr($this->invoice->public_notes,0,250)); + + return $this; + } + + private function setBillingPeriod(): self + { + if (\Carbon\Carbon::createFromFormat('Y-m-d', $this->invoice->custom_value3)->format('Y-m-d') === $this->invoice->custom_value3 && + \Carbon\Carbon::createFromFormat('Y-m-d', $this->invoice->custom_value4)->format('Y-m-d') === $this->invoice->custom_value4 + ){ + $this->fac->setBillingPeriod(\Carbon\Carbon::parse($this->invoice->custom_value3)->format('Y-m-d'), \Carbon\Carbon::parse($this->invoice->custom_value4)->format('Y-m-d')); + } + + return $this; + } + + private function setPayments(): self + { + $this->invoice->payments()->each(function ($payment){ + + $payment_data = [ + "dueDate" => \Carbon\Carbon::parse($payment->date)->format('Y-m-d'), + "amount" => $payment->pivot->amount, + ]; + + $data = array_merge($this->resolvePaymentMethod($payment), $payment_data); + + $this->fac->addPayment(new FacturaePayment($data)); + + }); + + return $this; + } + + /** + * + * FacturaePayment::TYPE_CASH Cash + * FacturaePayment::TYPE_DEBIT Domiciled receipt + * FacturaePayment::TYPE_RECEIPT Receipt + * FacturaePayment::TYPE_TRANSFER Transfer + * FacturaePayment::TYPE_ACCEPTED_BILL_OF_EXCHANGE Letter Accepted + * FacturaePayment::TYPE_DOCUMENTARY_CREDIT Letter of credit + * FacturaePayment::TYPE_CONTRACT_AWARD contract award + * FacturaePayment::TYPE_BILL_OF_EXCHANGE Bill of exchange + * FacturaePayment::TYPE_TRANSFERABLE_IOU I will pay to order + * FacturaePayment::TYPE_IOU I Will Pay Not To Order + * FacturaePayment::TYPE_CHEQUE Check + * FacturaePayment::TYPE_REIMBURSEMENT Replacement + * FacturaePayment::TYPE_SPECIAL specials + * FacturaePayment::TYPE_SETOFF Compensation + * FacturaePayment::TYPE_POSTGIRO Money order + * FacturaePayment::TYPE_CERTIFIED_CHEQUE conformed check + * FacturaePayment::TYPE_BANKERS_DRAFT Bank check + * FacturaePayment::TYPE_CASH_ON_DELIVERY Cash on delivery + * FacturaePayment::TYPE_CARD Payment by card + * + * @param \App\Models\Payment $payment + * @return array + */ + private function resolvePaymentMethod(\App\Models\Payment $payment): array + { + $data = []; + + match($payment->type_id){ + PaymentType::BANK_TRANSFER => $method = FacturaePayment::TYPE_TRANSFER , + PaymentType::CASH => $method = FacturaePayment::TYPE_CASH , + PaymentType::ACH => $method = FacturaePayment::TYPE_TRANSFER , + PaymentType::VISA => $method = FacturaePayment::TYPE_CARD , + PaymentType::MASTERCARD => $method = FacturaePayment::TYPE_CARD , + PaymentType::AMERICAN_EXPRESS => $method = FacturaePayment::TYPE_CARD , + PaymentType::DISCOVER => $method = FacturaePayment::TYPE_CARD , + PaymentType::DINERS => $method = FacturaePayment::TYPE_CARD , + PaymentType::EUROCARD => $method = FacturaePayment::TYPE_CARD , + PaymentType::NOVA => $method = FacturaePayment::TYPE_CARD , + PaymentType::CREDIT_CARD_OTHER => $method = FacturaePayment::TYPE_CARD , + PaymentType::PAYPAL => $method = FacturaePayment::TYPE_CARD , + PaymentType::CHECK => $method = FacturaePayment::TYPE_CHEQUE , + PaymentType::CARTE_BLANCHE => $method = FacturaePayment::TYPE_CARD , + PaymentType::UNIONPAY => $method = FacturaePayment::TYPE_CARD , + PaymentType::JCB => $method = FacturaePayment::TYPE_CARD , + PaymentType::LASER => $method = FacturaePayment::TYPE_CARD , + PaymentType::MAESTRO => $method = FacturaePayment::TYPE_CARD , + PaymentType::SOLO => $method = FacturaePayment::TYPE_CARD , + PaymentType::SWITCH => $method = FacturaePayment::TYPE_CARD , + PaymentType::VENMO => $method = FacturaePayment::TYPE_CARD , + PaymentType::ALIPAY => $method = FacturaePayment::TYPE_CARD , + PaymentType::SOFORT => $method = FacturaePayment::TYPE_TRANSFER, + PaymentType::SEPA => $method = FacturaePayment::TYPE_TRANSFER, + PaymentType::GOCARDLESS => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::CRYPTO => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::CREDIT => $method = FacturaePayment::TYPE_DOCUMENTARY_CREDIT , + PaymentType::ZELLE => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::MOLLIE_BANK_TRANSFER => $method = FacturaePayment::TYPE_TRANSFER , + PaymentType::KBC => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::BANCONTACT => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::IDEAL => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::HOSTED_PAGE => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::GIROPAY => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::PRZELEWY24 => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::EPS => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::DIRECT_DEBIT => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::BECS => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::ACSS => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::INSTANT_BANK_PAY => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::FPX => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::KLARNA => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::Interac_E_Transfer => $method = FacturaePayment::TYPE_TRANSFER , + PaymentType::BACS => $method = FacturaePayment::TYPE_SPECIAL , + PaymentType::STRIPE_BANK_TRANSFER => $method = FacturaePayment::TYPE_TRANSFER , + PaymentType::CASH_APP => $method = FacturaePayment::TYPE_SPECIAL , + default => $method = FacturaePayment::TYPE_CARD , + }; + + $data['method'] = $method; + + if($method == FacturaePayment::TYPE_TRANSFER) + { + $data['iban'] = $payment->custom_value1; + $data['bic'] = $payment->custom_value2; + } + + return $data; + + + } + private function buildItems(): self {