public function createSaleOrderLine($saleOrderId, $productId, $quantity, $unitPrice, $currencyId)
 {
     $unitPrice = floatval($unitPrice);
     $quantity = floatval($quantity);
     $dto = $this->createDto();
     $dto->setSaleOrderId($saleOrderId);
     $dto->setProductId($productId);
     $dto->setQuantity($quantity);
     $dto->setUnitPrice($unitPrice);
     $dto->setCurrencyId($currencyId);
     $productUnitCostInBaseCurrency = ProductManager::getInstance()->calculateProductCost($productId, $quantity);
     $dto->setUnitCost(json_encode($productUnitCostInBaseCurrency));
     $saleOrderDto = SaleOrderManager::getInstance()->selectByPK($saleOrderId);
     $orderDate = $saleOrderDto->getOrderDate();
     $rate = CurrencyRateManager::getInstance()->getCurrencyRateByDate($orderDate, $currencyId);
     $dto->setCurrencyRate($rate);
     if ($saleOrderDto->getNonProfit() == 0) {
         $profit = $quantity * $unitPrice * $rate - ProductManager::getInstance()->calculateProductTotalCost($productUnitCostInBaseCurrency);
         $dto->setTotalProfit($profit);
     } else {
         $dto->setTotalProfit(0);
     }
     ProductManager::getInstance()->updateProductCostForOneUnit($productId);
     return $this->insertDto($dto);
 }
 public function createPurchaseOrderLine($purchaseOrderId, $productId, $quantity, $unitPrice, $currencyId)
 {
     $dto = $this->createDto();
     $dto->setPurchaseOrderId($purchaseOrderId);
     $dto->setProductId($productId);
     $dto->setQuantity($quantity);
     $dto->setUnitPrice($unitPrice);
     $dto->setCurrencyId($currencyId);
     $orderDate = PurchaseOrderManager::getInstance()->selectByPK($purchaseOrderId)->getOrderDate();
     $rate = CurrencyRateManager::getInstance()->getCurrencyRateByDate($orderDate, $currencyId);
     $dto->setCurrencyRate($rate);
     ProductManager::getInstance()->updateProductCostForOneUnit($productId);
     return $this->insertDto($dto);
 }
 public function service()
 {
     $rates = $this->getCbaRates();
     if ($rates !== false) {
         $date = $rates[1];
         $currencyRateManager = CurrencyRateManager::getInstance();
         $selectByField = $currencyRateManager->selectByField('date', $date);
         if (empty($selectByField)) {
             foreach ($rates[0] as $rate) {
                 $iso = $rate[0];
                 $amount = $rate[1];
                 $rate = $rate[2];
                 $currencyRateManager->addRow($iso, $amount, $rate, $date);
             }
         }
     }
 }
 public function updatePaymentOrder($id, $partnerId, $paymentMethodId, $currencyId, $amount, $date, $note, $signature = "[]", $paid = true, $isExpense = false)
 {
     $partnerManager = PartnerManager::getInstance();
     $partner = $partnerManager->selectByPK($partnerId);
     if (empty($partner)) {
         throw new NgsErrorException("Partner does not exists with given id: " . $partnerId);
     }
     $paymentMethodManager = PaymentMethodManager::getInstance();
     $paymentMethod = $paymentMethodManager->selectByPK($paymentMethodId);
     if (empty($paymentMethod)) {
         throw new NgsErrorException("PaymentMethod does not exists with given id: " . $paymentMethodId);
     }
     $dto = $this->selectByPK($id);
     if ($dto) {
         $dto->setPartnerId($partnerId);
         $dto->setPaymentMethodId($paymentMethodId);
         $dto->setCurrencyId($currencyId);
         $rate = CurrencyRateManager::getInstance()->getCurrencyRateByDate($date, $currencyId);
         $dto->setCurrencyRate($rate);
         $dto->setAmount($amount);
         $dto->setDate($date);
         $dto->setNote($note);
         $dto->setIsExpense($isExpense);
         $dto->setPaid($paid);
         $dto->setSignature($signature);
         return $this->updateByPk($dto);
     }
     return false;
 }
 public function updateAllLinesCurrencyRates()
 {
     $allSaleOrders = $this->getSaleOrdersFull();
     foreach ($allSaleOrders as $saleOrder) {
         $saleOrderLinesDtos = $saleOrder->getSaleOrderLinesDtos();
         foreach ($saleOrderLinesDtos as $saleOrderLinesDto) {
             $orderDate = $saleOrder->getOrderDate();
             $currencyId = $saleOrderLinesDto->getCurrencyId();
             $rate = CurrencyRateManager::getInstance()->getCurrencyRateByDate($orderDate, $currencyId);
             $saleOrderLinesDto->setCurrencyRate($rate);
             SaleOrderLineManager::getInstance()->updateByPK($saleOrderLinesDto);
         }
     }
     return count($allSaleOrders);
 }