public function createCreditInvoiceFromInvoice(Document\Invoice $invoice) { $creditInvoice = new Document\CreditInvoice(); $creditInvoice->setInvoice($invoice); $creditInvoice->setCompany($invoice->getCompany()); $auth = $this->getServiceLocator()->get('zfcuser_auth_service'); if ($auth->hasIdentity()) { $user = $auth->getIdentity(); } $creditInvoice->setCreatedBy($user); $creditInvoice->setPrefix($invoice->getPrefix()); $creditInvoice->setCustomer($invoice->getCustomer()); $creditInvoice->setReferenceNumber($invoice->getReferenceNumber()); $creditInvoice->setCompany($invoice->getCompany()); $creditInvoice->setPaymentType($invoice->getPaymentType()); $creditInvoice->setVat($invoice->getVat()); $creditInvoice->setPrefix($invoice->getPrefix()); $creditInvoice->setDeadlineDays($invoice->getDeadlineDays()); $creditInvoice->setDelayPercent($invoice->getDelayPercent()); $creditInvoice->setDocumentDate(new \DateTime()); $creditInvoice->setOpencart($invoice->getOpencart()); foreach ($invoice->getRows() as $row) { /* @var $row \Application\Entity\Document\Row */ $creditRow = new Document\Row(); $creditRow->setUom($row->getUom()); $creditRow->setArticle($row->getArticle()); $creditRow->setDocument($creditInvoice); $creditRow->setVat($row->getVat()); $creditRow->setName($row->getName()); $creditRow->setWarehouse($row->getWarehouse()); $creditRow->setCode($row->getCode()); $creditRow->setBarCode($row->getBarCode()); $creditRow->setDescription($row->getDescription()); $creditRow->setPrice($row->getPrice()); $creditRow->setAmount($row->getAmount()); $creditRow->setQuantity($row->getCreditedQuantity() - $row->getQuantity()); $creditRow->setCreditingRowId($row->getId()); $creditInvoice->addRow($creditRow); } return $this->saveObject($creditInvoice); }
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); }