Example #1
0
 public function saveInvoiceWithParams(Document $invoice = null, User $user, array $data, $type)
 {
     if (!$invoice) {
         $company = $this->companyService->getCompanyByUser($user);
         if (!$company) {
             $this->companyService->setDefaultUserCompany($user);
             $company = $this->companyService->getCompanyByUser($user);
         }
         if ($type == Document::TYPE_INVOICE) {
             $invoice = new Invoice();
         } elseif ($type == Document::TYPE_PURCHASE_INVOICE) {
             $invoice = new Document\PurchaseInvoice();
         } elseif ($type == Document::TYPE_CREDIT_INVOICE) {
             $invoice = new Document\CreditInvoice();
         }
         $invoice->setCompany($company);
         $invoice->setUser($user);
         $invoice->setCreatedBy($user);
     }
     if (isset($data['customer']) && $data['customer'] > 0) {
         $customer = $this->customerService->getCustomerById($data['customer']);
         if ($customer) {
             $invoice->setCustomer($customer);
         }
     }
     if (isset($data['supplier']) && $data['supplier'] > 0) {
         $supplier = $this->supplierService->getSupplierById($data['supplier']);
         if ($supplier) {
             $invoice->setSupplier($supplier);
         }
     }
     if (isset($data['vat']) && $data['vat'] > 0) {
         $vat = $this->settingsService->getVatById($data['vat']);
         if ($vat) {
             $invoice->setVat($vat);
         }
     }
     if (isset($data['docDate'])) {
         $docDate = \DateTime::createFromFormat('d.m.Y', $data['docDate']);
         if (!$docDate) {
             $docDate = new \DateTime();
         }
         $deadLine = clone $docDate;
         $deadlineDays = isset($data['deadlineDays']) && $data['deadlineDays'] > 0 ? $data['deadlineDays'] : 0;
         $deadLine = $deadLine->add(new \DateInterval('P' . $deadlineDays . 'D'));
         $invoice->setDocumentDate($docDate);
         $invoice->setPaymentDate($deadLine);
     }
     if (isset($data['deadlineDays'])) {
         $invoice->setDeadlineDays($data['deadlineDays']);
     }
     if (isset($data['supplierDocNumber'])) {
         $invoice->setSupplierDocumentNumber($data['supplierDocNumber']);
     }
     if (isset($data['amount'])) {
         $invoice->setAmount($data['amount']);
     }
     if (isset($data['delayPercent'])) {
         $invoice->setDelayPercent($data['delayPercent']);
     }
     if (isset($data['taxAmount'])) {
         $invoice->setTaxAmount($data['taxAmount']);
     }
     if (isset($data['paymentType'])) {
         $invoice->setPaymentType($data['paymentType']);
     }
     if (isset($data['amountTax'])) {
         $invoice->setAmountTax($data['amountTax']);
     }
     if (isset($data['amountTax'])) {
         $invoice->setAmountTax($data['amountTax']);
     }
     if (isset($data['comment'])) {
         $invoice->setComment($data['comment']);
     }
     return $this->saveInvoiceRows($invoice, $data);
 }