/** * Get product information in one specific sales. * * @param $product * * @return array */ protected function getProductSaleInfo($product) { $info = new \stdClass(); $info->productUnitPrice = $product->unit_price * $product->pivot->quantity; $info->productCosts = $product->average_cost * $product->pivot->quantity; if ($product->driver->tax_enabled) { $info->productTax = Calculator::calculate($info->productUnitPrice, $product->getTaxRate()); } else { $info->productTax = 0; } $info->productSales = $product->getTaxRate()->isIncludedInPrice() ? $info->productUnitPrice : $info->productUnitPrice + $info->productTax; $info->productCommission = $info->productUnitPrice * $product->driver->getCommissionRate(); $info->productProfit = $info->productSales - $info->productCosts - $info->productTax - $info->productCommission; return $info; }
/** * Add order item. * * @param array $item * * @throws \InvalidArgumentException * @return $this */ public function addItem(array $item) { // Product id exists? $product = EloquentProductRepository::findOrFail($item['product_id']); // Product stock available? if ((int) $item['quantity'] > $product->inventory) { throw new \InvalidArgumentException('Stock is not available.'); } // Total Items $this->total_items += $item['quantity']; // Total Costs $this->total_costs += $product->average_cost * $item['quantity']; // Total Tax $totalUnitPrice = $product->unit_price * $item['quantity']; if ($product->driver->tax_enabled) { $totalTax = Calculator::calculate($totalUnitPrice, $product->getTaxRate()); $this->total_tax += $totalTax; } else { $totalTax = 0; $this->total_tax += $totalTax; } // Total Sales $this->total_sales += $product->getTaxRate()->isIncludedInPrice() ? $totalUnitPrice : $totalUnitPrice + $totalTax; $this->products()->attach($product->id, array('quantity' => $item['quantity'])); return $this; }