PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.6
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.6
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
← All changes | app/Models/Order.php +132 -7 1.4.2 → 1.6.6 View file →
@@ -42,9 +42,9 @@
42 42 {
43 43 parent::boot();
44 44 static::creating(function ($model) {
45 45 if (empty($model->uuid)) {
46 - $model->uuid = md5(time() . wp_generate_uuid4());
46 + $model->uuid = static::generateOrderUuid();
47 47 }
48 48
49 49 if (!isset($model->config)) {
50 50 $model->config = [];
@@ -56,8 +56,18 @@
56 56 }
57 57 });
58 58
59 59 static::created(function ($model) {
60 + // Every order carries a companion operations row. ReceiptHandler reads
61 + // sales_recorded off it to decide whether a receipt is being seen for the
62 + // first time, and that gates fluent_cart/after_receipt_first_time — an order
63 + // without the row silently never fires its purchase event.
64 + //
65 + // Created here rather than at each call site because orders also come from
66 + // renewals, subscription child orders, the admin and WP-CLI, none of which
67 + // pass through the checkout or dispatch fluent_cart/order_created.
68 + OrderOperation::query()->firstOrCreate(['order_id' => $model->id]);
69 +
60 70 if ($model->invoice_no) {
61 71 do_action('fluent_cart/order/invoice_number_added', [
62 72 'order' => $model
63 73 ]);
@@ -64,8 +74,59 @@
64 74 }
65 75 });
66 76 }
67 77
78 + /**
79 + * Generate a short, human-usable order handle: 12 uppercase alphanumeric
80 + * characters (e.g. A7K2P9X4M1Q8), stored in the `uuid` column and shown as
81 + * "#A7K2P9X4M1Q8" on the UI. Existing orders keep their legacy md5 uuids.
82 + *
83 + * Uniqueness is best-effort at the application level: a chunk of
84 + * candidates is generated and filtered against the table with a single
85 + * whereIn query (no per-candidate round-trips). There is intentionally no
86 + * DB unique constraint on `fct_orders.uuid`, so this check is NOT atomic —
87 + * two concurrent inserts could theoretically race on the same candidate.
88 + * Given the 36^12 (~4.7x10^18) space, a real collision is astronomically
89 + * unlikely, but not cryptographically guaranteed. If a hard guarantee is
90 + * ever required, add a unique index on the column and retry creation on a
91 + * duplicate-key error.
92 + *
93 + * @return string
94 + */
95 + public static function generateOrderUuid()
96 + {
97 + $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
98 + $maxIndex = strlen($chars) - 1;
99 + $chunkSize = 20;
100 +
101 + do {
102 + // Generate a chunk of candidates, then resolve collisions with a
103 + // single query (whereIn) instead of one query per candidate. The
104 + // code is used as the array key so the chunk is self-deduplicated.
105 + $candidates = [];
106 + for ($i = 0; $i < $chunkSize; $i++) {
107 + $code = '';
108 + for ($j = 0; $j < 12; $j++) {
109 + $code .= $chars[wp_rand(0, $maxIndex)];
110 + }
111 + $candidates[$code] = true;
112 + }
113 +
114 + $taken = static::whereIn('uuid', array_keys($candidates))
115 + ->get(['uuid'])
116 + ->pluck('uuid')
117 + ->toArray();
118 +
119 + foreach ($taken as $existing) {
120 + unset($candidates[$existing]);
121 + }
122 + // Loops again only if every candidate in the chunk collided, which
123 + // is astronomically unlikely for a 36^12 (~4.7x10^18) space.
124 + } while (empty($candidates));
125 +
126 + return array_key_first($candidates);
127 + }
128 +
68 129 protected $fillable = [
69 130 'status',
70 131 'parent_id',
71 132 'invoice_no',
@@ -563,8 +624,11 @@
563 624 if ($isCompound) {
564 625 $displayLabel .= ' (' . __('Compound', 'fluent-cart') . ')';
565 626 }
566 627
628 + // Capture clean label (name + percent, no base suffix) for the shared folded-row builder.
629 + $rateLabelClean = $displayLabel;
630 +
567 631 if ($taxableAmount > 0 && !$isMixedInclusive) {
568 632 if ($lineInclusive === null) {
569 633 $displayBase = $this->tax_behavior == 2
570 634 ? max(0, $taxableAmount - $rateTaxAmount)
@@ -581,13 +645,18 @@
581 645 );
582 646 }
583 647
584 648 $displayTaxLines[] = [
585 - 'label' => $displayLabel,
586 - 'order_tax' => $rateTaxAmount,
587 - 'total_tax' => (int) $orderTaxRate->total_tax,
588 - 'rate_id' => (int) $orderTaxRate->tax_rate_id,
649 + 'label' => $displayLabel,
650 + 'rate_label' => $rateLabelClean,
651 + 'order_tax' => $rateTaxAmount,
652 + 'total_tax' => (int) $orderTaxRate->total_tax,
653 + 'rate_id' => (int) $orderTaxRate->tax_rate_id,
654 + 'rate_percent' => $ratePercent,
655 + 'taxable_amount' => isset($displayBase) ? (int) $displayBase : $taxableAmount,
656 + 'inclusive' => $lineInclusive === null ? ((int) $this->tax_behavior === 2) : (bool) $lineInclusive,
589 657 ];
658 + unset($displayBase);
590 659 }
591 660
592 661 return $displayTaxLines;
593 662 }
@@ -619,8 +688,10 @@
619 688 }
620 689 $displayLines[] = [
621 690 'label' => $displayLabel,
622 691 'shipping_tax' => $shippingTax,
692 + 'rate_id' => (int) $orderTaxRate->tax_rate_id,
693 + 'rate_percent' => $ratePercent,
623 694 ];
624 695 }
625 696 return $displayLines;
626 697 }
@@ -684,8 +755,13 @@
684 755 if (!empty($legacyTaxNumber)) {
685 756 return $legacyTaxNumber;
686 757 }
687 758
759 + $topLevelLegacyTaxId = (string) $this->getMeta('tax_id', '');
760 + if (!empty($topLevelLegacyTaxId)) {
761 + return $topLevelLegacyTaxId;
762 + }
763 +
688 764 $orderTaxRate = $this->getPrimaryOrderTaxRate();
689 765
690 766 return (string) Arr::get($orderTaxRate->meta ?? [], 'vat_reverse.vat_number', '');
691 767 }
@@ -696,9 +772,18 @@
696 772 }
697 773
698 774 public function getBusinessInfoAttribute(): array
699 775 {
700 - return $this->getBusinessInfo();
776 + $businessInfo = $this->getBusinessInfo();
777 +
778 + // Only meaningful for a MoR reverse-charge order (no orderTaxRate row) — omit
779 + // for every normal order instead of appending a redundant duplicate of total_paid.
780 + $morVatRemoved = $this->getMoRVatRemovedAmount();
781 + if ($morVatRemoved > 0) {
782 + $businessInfo['net_total_paid'] = $this->netAmount((int) $this->total_paid);
783 + }
784 +
785 + return $businessInfo;
701 786 }
702 787
703 788 public function getIsReverseChargeTaxOrderAttribute(): bool
704 789 {
@@ -738,8 +823,40 @@
738 823 {
739 824 return $this->transactions()->where('status', Status::TRANSACTION_SUCCEEDED)->sum('total');
740 825 }
741 826
827 + /**
828 + * Amount of VAT removed by a merchant-of-record gateway (e.g. Paddle) for a
829 + * reverse-charge order that never ran the tax module (no `fct_order_tax_rate` row).
830 + * Display-only — `total_amount`/`total_paid`/`txn.total` stay gross for such orders
831 + * (cover invariant: the "fully paid" equality that drives due-amount checks, digital
832 + * auto-complete, and dunning reminders depends on it), so this is never subtracted
833 + * into the ledger, only into `getDisplayTotalPaid()` for the admin UI. Zero for
834 + * core-handled reverse charge (tax-rate row present) — those are already net at
835 + * creation time.
836 + */
837 + public function getMoRVatRemovedAmount(): int
838 + {
839 + if ($this->getPrimaryOrderTaxRate()) {
840 + return 0;
841 + }
842 +
843 + return (int) Arr::get($this->getBusinessInfo(), 'mor_vat_removed', 0);
844 + }
845 +
846 + /**
847 + * Nets a raw gross ledger figure (total_paid, a live paid-total sum, a refund amount)
848 + * by the MoR VAT removal — see getMoRVatRemovedAmount(). Single formula for every
849 + * "what did we actually collect/need to refund" comparison, so callers never
850 + * reimplement the subtraction themselves. Never use for due-amount, digital
851 + * auto-complete, or reminder logic; those must keep comparing the gross ledger
852 + * columns (total_amount vs total_paid) so the "fully paid" equality still holds.
853 + */
854 + public function netAmount(int $amount): int
855 + {
856 + return max(0, $amount - $this->getMoRVatRemovedAmount());
857 + }
858 +
742 859 public function getTotalRefundAmount()
743 860 {
744 861 return $this->transactions()->where('status', Status::TRANSACTION_REFUNDED)->sum('total');
745 862 }
@@ -750,9 +867,13 @@
750 867 $totalRefunded = $this->getTotalRefundAmount();
751 868
752 869 $this->total_refund = $totalRefunded;
753 870
754 - if (floatval($totalRefunded) >= floatval($totalPaid)) {
871 + // Net out MoR VAT removal so a full refund of the actually captured amount
872 + // resolves to "fully refunded" — see netAmount().
873 + $netTotalPaid = $this->netAmount($totalPaid);
874 +
875 + if (floatval($totalRefunded) >= floatval($netTotalPaid)) {
755 876 $this->payment_status = Status::PAYMENT_REFUNDED;
756 877 } elseif ($totalPaid > $totalRefunded) {
757 878 $this->payment_status = Status::PAYMENT_PARTIALLY_REFUNDED;
758 879 }
@@ -766,8 +887,12 @@
766 887 {
767 888 $paymentStatus = $type == 'full' ? Status::PAYMENT_REFUNDED : Status::PAYMENT_PARTIALLY_REFUNDED;
768 889 $this->total_refund += $refundedAmount;
769 890 $this->payment_status = $paymentStatus;
891 +
892 + if ($paymentStatus === Status::PAYMENT_REFUNDED && !$this->refunded_at) {
893 + $this->refunded_at = DateTime::gmtNow();
894 + }
770 895
771 896 $this->save();
772 897
773 898 return $this;