/** * Compute stock on hand * @param unknown $currentRnum * @param unknown $previousRnum * @return multitype:|multitype:number Ambigous <string, number> */ public function computeStockOnHand($currentRnum, $previousRnum = null) { $currentReplenish = ModelFactory::getInstance('TxnReplenishmentHeader')->with('salesman', 'details')->where('reference_number', $currentRnum)->first(); $previousReplenish = ''; if ($previousRnum) { $previousReplenish = ModelFactory::getInstance('StockOnHand')->with('items')->where('replenishment_number', $previousRnum)->first(); } if (!$currentReplenish) { return false; } $stockOnHand = []; $to = new Carbon($currentReplenish->replenishment_date); $goLive = new Carbon(config('system.go_live_date')); //Replenishment if ($previousReplenish) { foreach ($previousReplenish->items as $item) { if (!isset($stockOnHand[$item->item_code])) { $stockOnHand[$item->item_code] = 0; } $stockOnHand[$item->item_code] += $item->quantity; } $from = (new Carbon($previousReplenish->stock_date))->addDay(); } else { foreach ($currentReplenish->details as $item) { if (!isset($stockOnHand[$item->item_code])) { $stockOnHand[$item->item_code] = 0; } $stockOnHand[$item->item_code] += $item->quantity; } $from = new Carbon($to); } $salesmanCode = $currentReplenish->salesman->salesman_code; $dates = []; while ($from->lte($to)) { $date = $from->format('Y-m-d'); $stockTransfers = ModelFactory::getInstance('TxnStockTransferInHeader')->with('details')->where(\DB::raw('DATE(transfer_date)'), $date)->where('salesman_code', $salesmanCode)->orderBy('transfer_date')->get(); // Stock Transfer $stockTransferItemCount = []; if ($stockTransfers) { foreach ($stockTransfers as $stock) { foreach ($stock->details as $item) { if (!isset($stockOnHand[$item->item_code])) { $stockOnHand[$item->item_code] = 0; } $stockOnHand[$item->item_code] += $item->quantity; } } } $sales = ModelFactory::getInstance('TxnSalesOrderHeader')->with('details', 'customer')->where(\DB::raw('DATE(so_date)'), $date)->where('salesman_code', $salesmanCode)->orderBy('so_date')->get(); // Sales Invoice $salesItemCount = []; if ($sales) { foreach ($sales as $sale) { foreach ($sale->details as $item) { if (!isset($stockOnHand[$item->item_code])) { $stockOnHand[$item->item_code] = 0; } if (false !== strpos($sale->customer->customer_name, '_Van to Warehouse')) { $stockOnHand[$item->item_code] -= $item->order_qty; } elseif (false !== strpos($sale->customer->customer_name, '_Adjustment')) { $stockOnHand[$item->item_code] -= $item->order_qty; } else { $stockOnHand[$item->item_code] -= $item->quantity; } } } } $returns = ModelFactory::getInstance('TxnReturnHeader')->with('details', 'customer')->where(\DB::raw('DATE(return_date)'), $date)->where('salesman_code', $salesmanCode)->orderBy('return_date')->get(); // Returns Invoice $returnsItemCount = []; if ($returns) { foreach ($returns as $return) { foreach ($return->details as $item) { if (!isset($stockOnHand[$item->item_code])) { $stockOnHand[$item->item_code] = 0; } $stockOnHand[$item->item_code] += $item->quantity; } } } if ($from->eq($to)) { foreach ($currentReplenish->details as $item) { if (!isset($stockOnHand[$item->item_code])) { $stockOnHand[$item->item_code] = 0; } $stockOnHand[$item->item_code] -= $item->quantity; } } $stock = ModelFactory::getInstance('StockOnHand'); if ($from->eq($to)) { $stock->replenishment_number = $currentReplenish->reference_number; } else { $stock->replenishment_number = $previousReplenish ? $previousReplenish->replenishment_number : $currentReplenish->reference_number; } $stock->salesman_code = $salesmanCode; $stock->stock_date = new \DateTime($date); $dates[] = $date; if ($from->eq($goLive)) { $stock->beginning = 1; } if ($stock->save()) { foreach ($stockOnHand as $code => $qty) { $stockItem = ModelFactory::getInstance('StockOnHandItems'); $stockItem->stock_on_hand_id = $stock->id; $stockItem->item_code = $code; $stockItem->quantity = $qty; $stockItem->save(); } } $from->addDay(); } }
/** * @param Carbon $start * @param Carbon $end */ public static function displayRange(Carbon $start, Carbon $end) { if ($start->eq($end)) { return $end->format('F j, Y'); } if ($start->year != $end->year) { return $start->format('F j, Y') . ' - ' . $end->format('F j, Y'); } if ($start->month != $end->month) { return $start->format('F j') . ' - ' . $end->format('F j, Y'); } return $start->format('F j') . ' - ' . $end->format('j, Y'); }
/** * Determine if a date is a non-business day * * @param Carbon $dt * @return bool */ public function isExcluded(Carbon $dt) { foreach ($this->exclusions() as $exc) { if ($dt->eq($exc)) { return TRUE; } } foreach ($this->callbacks() as $fn) { if ($fn($dt) == TRUE) { return TRUE; } } return FALSE; }
/** * Datetime exact match check as well as between check * @param string $content * @param string $search * @param string $type * @return bool */ public function filter($content, $search, $type) { $search = is_array($search) ? $search : [$search]; $search = array_map(function ($searchValue) { return is_a($searchValue, Carbon::class) ? $searchValue : new Carbon($searchValue); }, $search); $current = new Carbon($content); switch ($type) { case 'in': return $current->gte($search[0]) && $current->lt($search[1]); break; default: return $current->eq($search[0]); } }
/** * Exclude a more complicated method for something like a holiday, e.g.: Memorial Day - "Last Monday in May" * this could be solved using CoreCallbacks::ignoreNDOW(5, -1, 1) * * @param integer $month 1 based index of month, 1 = Jan, 12 = dec * @param integer $nth Ignore the "nth" $dayOfWeek of the given month (1-6, To get 'LAST' $dayOfWeek, use -1) * @param integer $dayOfWeek Zero based index day of the week - 0 = Sunday, 6 = Saturday * * @return callable */ public static function ignoreNDOW($month, $nth, $dayOfWeek) { return function (Carbon $context) use($month, $nth, $dayOfWeek) { if ($context->month !== $month) { return FALSE; } $cmp = new Carbon($context->format('Y-m-') . '01'); /* Set our walker up to the first of the context's month */ $ticks = 0; /** * @var $stack Carbon[] */ $stack = array(); /* Used to track 'nth' */ for ($i = 0; $i < $context->daysInMonth; $i++) { if ($cmp->dayOfWeek == $dayOfWeek) { $stack[] = clone $cmp; $ticks++; if ($cmp->eq($context)) { /* "FIRST" or generic 'nth' */ if ($nth == $ticks) { return TRUE; } } } $cmp->addDay(); } /* For brevity, checking for the -1 anyways */ if ($nth == -1) { /** * @var $last Carbon */ $last = array_pop($stack); return $last->eq($context); } return FALSE; }; }