Automatic margin calculation for dropshipping

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Showing 1 of 1 servicesAll 2065 services
Automatic margin calculation for dropshipping
Medium
~2-3 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

Implementation of Automatic Margin Calculation in Dropshipping

Margin in dropshipping is the difference between store's retail price and supplier's wholesale price minus other costs: payment processing, shipping, advertising. Automatic calculation is needed so when supplier's cost changes, retail price recalculates by set rules without manager involvement.

What Affects Final Margin

  • Wholesale price — changes with each supplier price sync
  • Payment processing fee — 1.5–3.5% depending on payment system
  • Shipping cost — may be compensated or included in price
  • Returns — statistical percentage built into markup
  • Advertising — CAC (cost per acquisition) for breakeven calculation

Margin Rules Model

Schema::create('margin_rules', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->enum('scope', ['global', 'supplier', 'category', 'product']);
    $table->nullableMorphs('scopeable'); // polymorphic: Supplier, Category, Product
    $table->enum('type', ['percent', 'fixed', 'table']);
    $table->decimal('value', 8, 2)->nullable(); // for percent and fixed
    $table->jsonb('table_config')->nullable(); // for type=table
    $table->decimal('min_retail_price', 10, 2)->nullable(); // lower bound
    $table->boolean('include_acquiring', true)->default(true);
    $table->decimal('acquiring_percent', 5, 2)->default(2.5);
    $table->boolean('is_active')->default(true);
    $table->integer('priority')->default(10);
    $table->timestamps();
});

Complete Margin Calculator

class MarginCalculator
{
    /**
     * Calculate retail price from wholesale accounting for all costs
     */
    public function calculateRetailPrice(
        float $supplierPrice,
        MarginRule $rule,
        ?float $shippingCost = null,
    ): float {
        // Base retail price by markup rule
        $baseRetail = match($rule->type) {
            'percent' => $supplierPrice * (1 + $rule->value / 100),
            'fixed'   => $supplierPrice + $rule->value,
            'table'   => $this->applyTable($supplierPrice, $rule->table_config),
        };

        // Account for payment processing fee
        if ($rule->include_acquiring) {
            $baseRetail = $baseRetail / (1 - $rule->acquiring_percent / 100);
        }

        // Account for shipping cost if included in price
        if ($shippingCost && $rule->include_shipping) {
            $baseRetail += $shippingCost;
        }

        // Apply lower bound
        if ($rule->min_retail_price && $baseRetail < $rule->min_retail_price) {
            $baseRetail = $rule->min_retail_price;
        }

        return round($baseRetail, 2);
    }

    /**
     * Calculate actual margin for reports
     */
    public function calculateMargin(float $retailPrice, float $supplierPrice, float $acquiringPercent = 2.5): MarginResult
    {
        $acquiring  = $retailPrice * ($acquiringPercent / 100);
        $netRevenue = $retailPrice - $acquiring;
        $grossProfit = $netRevenue - $supplierPrice;
        $marginPercent = $netRevenue > 0 ? ($grossProfit / $netRevenue) * 100 : 0;

        return new MarginResult(
            retailPrice:    $retailPrice,
            supplierPrice:  $supplierPrice,
            acquiring:      round($acquiring, 2),
            netRevenue:     round($netRevenue, 2),
            grossProfit:    round($grossProfit, 2),
            marginPercent:  round($marginPercent, 2),
        );
    }

    /**
     * Tiered markup by table
     */
    private function applyTable(float $price, array $table): float
    {
        // Example table_config:
        // [{"max_price": 500, "percent": 50}, {"max_price": 2000, "percent": 35},
        //  {"max_price": 10000, "percent": 25}, {"max_price": null, "percent": 15}]
        foreach ($table as $tier) {
            if ($tier['max_price'] === null || $price <= $tier['max_price']) {
                return $price * (1 + $tier['percent'] / 100);
            }
        }
        return $price * 1.15; // fallback
    }
}

Margin Rule Resolver

Rules applied by specificity order: product > category > supplier > global.

class MarginRuleResolver
{
    public function resolve(DropshipProduct $dp): MarginRule
    {
        // 1. Individual rule for specific product
        $rule = MarginRule::where('scope', 'product')
            ->where('scopeable_type', Product::class)
            ->where('scopeable_id', $dp->product_id)
            ->active()
            ->orderBy('priority')
            ->first();

        if ($rule) return $rule;

        // 2. Rule for product category
        if ($dp->product?->category_id) {
            $categoryIds = $dp->product->category->ancestorsAndSelf()->pluck('id');
            $rule = MarginRule::where('scope', 'category')
                ->where('scopeable_type', Category::class)
                ->whereIn('scopeable_id', $categoryIds)
                ->active()
                ->orderBy('priority')
                ->first();

            if ($rule) return $rule;
        }

        // 3. Rule for supplier
        $rule = MarginRule::where('scope', 'supplier')
            ->where('scopeable_type', Supplier::class)
            ->where('scopeable_id', $dp->supplier_id)
            ->active()
            ->first();

        if ($rule) return $rule;

        // 4. Global default rule
        return MarginRule::where('scope', 'global')->active()->firstOrFail();
    }
}

Automatic Recalculation on Wholesale Price Change

class RecalculateRetailPriceListener
{
    public function handle(SupplierPriceUpdatedEvent $event): void
    {
        $dp   = $event->dropshipProduct;
        $rule = $this->resolver->resolve($dp);

        if (!$dp->product || $dp->product->price_locked) {
            return; // price manually fixed — don't change
        }

        $newRetailPrice = $this->calculator->calculateRetailPrice(
            supplierPrice: $dp->supplier_price,
            rule: $rule,
        );

        $dp->product->update(['price' => $newRetailPrice]);

        Log::debug('Retail price recalculated', [
            'product_id'    => $dp->product_id,
            'supplier_price' => $dp->supplier_price,
            'retail_price'  => $newRetailPrice,
            'rule'          => $rule->name,
        ]);
    }
}

Margin Report

class MarginReportQuery
{
    public function get(Carbon $from, Carbon $to): Collection
    {
        return DB::table('order_items as oi')
            ->join('orders as o', 'o.id', '=', 'oi.order_id')
            ->join('products as p', 'p.id', '=', 'oi.product_id')
            ->join('dropship_products as dp', 'dp.product_id', '=', 'p.id')
            ->join('dropship_suppliers as s', 's.id', '=', 'dp.supplier_id')
            ->whereBetween('o.paid_at', [$from, $to])
            ->where('o.status', 'completed')
            ->selectRaw('
                s.name as supplier,
                SUM(oi.quantity * oi.price) as revenue,
                SUM(oi.quantity * dp.supplier_price) as cost,
                SUM(oi.quantity * (oi.price - dp.supplier_price)) as gross_profit,
                ROUND(
                    SUM(oi.quantity * (oi.price - dp.supplier_price)) /
                    NULLIF(SUM(oi.quantity * oi.price), 0) * 100, 2
                ) as margin_percent
            ')
            ->groupBy('s.id', 's.name')
            ->orderByDesc('gross_profit')
            ->get();
    }
}

Timeline

Basic calculator (percentage markup) + auto-recalculation on price sync — 2–3 business days. Tiered rules, priority hierarchy, fee accounting, margin report — 3–5 business days.