public function addAgreementProductsCalculation(Costbook $costbook, $quantity, $frequency, $optId)
 {
     $tQty = 0;
     $opportunityProduct = new OpportunityProduct();
     $opportunityProduct->Quantity = (double) $quantity;
     $opportunityProduct->Frequency = (double) $frequency;
     $opportunityProduct->name = 'Love Name - ' . $costbook->id;
     //$opportunityProduct->Product = $costbook;
     $tQty = (double) $quantity;
     if ($opportunityProduct->Frequency > 0) {
         $tQty *= $opportunityProduct->Frequency;
     }
     //Labor Product calculation
     if ($costbook->costofgoodssold == 'Labor') {
         $currencies = Currency::getAll();
         $opportunityProduct->Total_MHR = round($costbook->costperunit * $tQty);
         if ($costbook->departmentreference != null) {
             $deptReference = DepartmentReference::GetById(intval($costbook->departmentreference));
             $burdenCost = new CurrencyValue();
             $burdenCost->value = round($deptReference->burdonCost * $tQty, 2);
             $burdenCost->currency = $currencies[0];
             $opportunityProduct->Burden_Cost = $burdenCost;
             $laborCost = new CurrencyValue();
             $laborCost->value = round($deptReference->laborCost * $tQty, 2);
             $laborCost->currency = $currencies[0];
             $opportunityProduct->Labor_Cost = $laborCost;
         }
     }
     $opportunityProduct->Category_GPM = 40;
     $totalDirectCost = new CurrencyValue();
     $totalDirectCost->value = round($opportunityProduct->Labor_Cost->value + $opportunityProduct->Burden_Cost->value, 2);
     $totalDirectCost->currency = $currencies[0];
     $opportunityProduct->Total_Direct_Cost = $totalDirectCost;
     $finalCost = new CurrencyValue();
     $finalCost->value = round($opportunityProduct->Total_Direct_Cost->value / (1 - $opportunityProduct->Category_GPM / 100), 2);
     $finalCost->currency = $currencies[0];
     $opportunityProduct->Final_Cost = $finalCost;
     $opportunityProduct->opportunity = $opportunity = Opportunity::GetById(intval($optId));
     $opportunityProduct->save();
 }
 public function addOpportunityProductsCalculation(Costbook $costbook, $quantity, $frequency, $optId, $category)
 {
     try {
         $tQty = 0;
         $opportunityProduct = new OpportunityProduct();
         $opportunityProduct->Quantity = (double) $quantity;
         if ($frequency != '') {
             $opportunityProduct->Frequency = (double) $frequency;
         }
         $opportunityProduct->Category = $category;
         $opportunityProduct->Product_Code = $costbook->productcode;
         $opportunityProduct->name = $costbook->productname;
         $currencies = Currency::getAll();
         $opportunityProduct->costbook = $costbook;
         $tQty = (double) $quantity;
         if ($opportunityProduct->Frequency > 0) {
             $tQty *= $opportunityProduct->Frequency;
         }
         //Labor Product calculation
         if ($costbook->costofgoodssold->value == 'Labor') {
             //                $opportunityProduct->Total_MHR = round($costbook->costperunit * $tQty);
             $opportunityProduct->Total_MHR = $tQty;
             if (intval($costbook->laborCost) > 0 && intval($costbook->burdenCost) > 0) {
                 $burdenCost = new CurrencyValue();
                 $burdenCost->value = round(intval($costbook->burdenCost) * $tQty, 2);
                 $burdenCost->currency = $currencies[0];
                 $opportunityProduct->Burden_Cost = $burdenCost;
                 $laborCost = new CurrencyValue();
                 $laborCost->value = round(intval($costbook->laborCost) * $tQty, 2);
                 $laborCost->currency = $currencies[0];
                 $opportunityProduct->Labor_Cost = $laborCost;
             } else {
                 if (intval($costbook->laborCost) > 0 && intval($costbook->burdenCost) <= 0) {
                     $burdenCost = new CurrencyValue();
                     $burdenCost->value = 0.0;
                     $burdenCost->currency = $currencies[0];
                     $opportunityProduct->Burden_Cost = $burdenCost;
                     $laborCost = new CurrencyValue();
                     $laborCost->value = round(intval($costbook->laborCost) * $tQty, 2);
                     $laborCost->currency = $currencies[0];
                     $opportunityProduct->Labor_Cost = $laborCost;
                 } else {
                     if (intval($costbook->burdenCost) > 0 && intval($costbook->laborCost) <= 0) {
                         $burdenCost = new CurrencyValue();
                         $burdenCost->value = round(intval($costbook->burdenCost) * $tQty, 2);
                         $burdenCost->currency = $currencies[0];
                         $opportunityProduct->Burden_Cost = $burdenCost;
                         $laborCost = new CurrencyValue();
                         $laborCost->value = 0.0;
                         $laborCost->currency = $currencies[0];
                         $opportunityProduct->Labor_Cost = $laborCost;
                     } else {
                         $burdenCost = new CurrencyValue();
                         $burdenCost->value = 0.0;
                         $burdenCost->currency = $currencies[0];
                         $opportunityProduct->Burden_Cost = $burdenCost;
                         $laborCost = new CurrencyValue();
                         $laborCost->value = 0.0;
                         $laborCost->currency = $currencies[0];
                         $opportunityProduct->Labor_Cost = $laborCost;
                     }
                 }
             }
             //$opportunityProduct->Total_MHR = $tQty;
         } else {
             $burdenCost = new CurrencyValue();
             $burdenCost->value = 0.0;
             $burdenCost->currency = $currencies[0];
             $opportunityProduct->Burden_Cost = $burdenCost;
             $laborCost = new CurrencyValue();
             $laborCost->value = 0.0;
             $laborCost->currency = $currencies[0];
             $opportunityProduct->Labor_Cost = $laborCost;
         }
         if ($costbook->costofgoodssold->value == 'Material') {
             // $opportunityProduct->Total_MHR = round($tQty);
             $materialCost = new CurrencyValue();
             $materialCost->value = round(intval($costbook->unitdirectcost) * $tQty, 2);
             $materialCost->currency = $currencies[0];
             $opportunityProduct->Materials_Cost = $materialCost;
         } else {
             $materialCost = new CurrencyValue();
             $materialCost->value = 0.0;
             $materialCost->currency = $currencies[0];
             $opportunityProduct->Materials_Cost = $materialCost;
         }
         if ($costbook->costofgoodssold->value == 'Equipment') {
             //$opportunityProduct->Total_MHR = round($tQty);
             $eqmtCost = new CurrencyValue();
             $eqmtCost->value = round($tQty, 2);
             $eqmtCost->currency = $currencies[0];
             $opportunityProduct->Equipment_Cost = $eqmtCost;
         } else {
             $eqmtCost = new CurrencyValue();
             $eqmtCost->value = 0.0;
             $eqmtCost->currency = $currencies[0];
             $opportunityProduct->Equipment_Cost = $eqmtCost;
         }
         if ($costbook->costofgoodssold->value == 'Subcontractor') {
             //$opportunityProduct->Total_MHR = round($tQty);
             $subcontCost = new CurrencyValue();
             $subcontCost->value = round(intval($costbook->unitdirectcost) * $tQty, 2);
             $subcontCost->currency = $currencies[0];
             $opportunityProduct->Sub_Cost = $subcontCost;
         } else {
             $subcontCost = new CurrencyValue();
             $subcontCost->value = 0.0;
             $subcontCost->currency = $currencies[0];
             $opportunityProduct->Sub_Cost = $subcontCost;
         }
         if ($costbook->costofgoodssold->value == 'Other') {
             // $opportunityProduct->Total_MHR = round($tQty);
             $otherCost = new CurrencyValue();
             $otherCost->value = round(intval($costbook->unitdirectcost) * $tQty, 2);
             $otherCost->currency = $currencies[0];
             $opportunityProduct->Other_Cost = $otherCost;
         } else {
             $otherCost = new CurrencyValue();
             $otherCost->value = 0.0;
             $otherCost->currency = $currencies[0];
             $opportunityProduct->Other_Cost = $otherCost;
         }
         $values = Category::getCategoryByName($category);
         if ($values != '') {
             $opportunityProduct->Category_GPM = $values[0]->targetgpm;
         } else {
             $opportunityProduct->Category_GPM = 40;
         }
         $totalDirectCost = new CurrencyValue();
         $totalDirectCost->value = round($opportunityProduct->Labor_Cost->value + $opportunityProduct->Burden_Cost->value + $opportunityProduct->Materials_Cost->value + $opportunityProduct->Equipment_Cost->value + $opportunityProduct->Sub_Cost->value + $opportunityProduct->Other_Cost->value, 2);
         $totalDirectCost->currency = $currencies[0];
         $opportunityProduct->Total_Direct_Cost = $totalDirectCost;
         $finalCost = new CurrencyValue();
         $finalCost->value = round($opportunityProduct->Total_Direct_Cost->value / (1 - $opportunityProduct->Category_GPM / 100), 2);
         $finalCost->currency = $currencies[0];
         $opportunityProduct->Final_Cost = $finalCost;
         $opportunityProduct->opportunity = $opportunity = Opportunity::GetById(intval($optId));
         if (!$opportunityProduct->save()) {
             throw new Exception();
         } else {
             return TRUE;
         }
     } catch (Exception $ex) {
         //echo 'Exception occured'.$ex;       die;
         return FALSE;
     }
 }