public function process() { $invoice = $this->stateInvoice(); $invoiceItems = $this->requestInvoiceItems(); $data = array_merge($this->requestFields(), ['public_id' => $invoice->public_id, 'invoice_items' => array_merge($invoice->invoice_items->toArray(), $invoiceItems)]); // map the cost and qty fields to the invoice items if (isset($data['cost']) || isset($data['quantity'])) { foreach ($data['invoice_items'] as $key => $item) { // if it's new or we recently created it if (empty($item['public_id']) || in_array($item['public_id'], $this->entities(ENTITY_INVOICE_ITEM))) { $data['invoice_items'][$key]['cost'] = isset($data['cost']) ? $data['cost'] : $item['cost']; $data['invoice_items'][$key]['qty'] = isset($data['quantity']) ? $data['quantity'] : $item['qty']; } } } //var_dump($data); $valid = EntityModel::validate($data, ENTITY_INVOICE, $invoice); if ($valid !== true) { throw new Exception($valid); } $invoice = $this->invoiceRepo->save($data, $invoice); $invoiceItems = array_slice($invoice->invoice_items->toArray(), count($invoiceItems) * -1); $invoiceItemIds = array_map(function ($item) { return $item['public_id']; }, $invoiceItems); $this->setStateEntities(ENTITY_INVOICE_ITEM, $invoiceItemIds); $response = $invoice->load('invoice_items')->present()->skypeBot; return $this->createResponse(SKYPE_CARD_RECEIPT, $response); }
public function process() { $client = $this->requestClient(); $invoiceItems = $this->requestInvoiceItems(); if (!$client) { throw new Exception(trans('texts.client_not_found')); } $data = array_merge($this->requestFields(), ['client_id' => $client->id, 'invoice_items' => $invoiceItems]); //var_dump($data); $valid = EntityModel::validate($data, ENTITY_INVOICE); if ($valid !== true) { throw new Exception($valid); } $invoiceService = app('App\\Services\\InvoiceService'); $invoice = $invoiceService->save($data); $invoiceItemIds = array_map(function ($item) { return $item['public_id']; }, $invoice->invoice_items->toArray()); $this->setStateEntityType(ENTITY_INVOICE); $this->setStateEntities(ENTITY_CLIENT, $client->public_id); $this->setStateEntities(ENTITY_INVOICE, $invoice->public_id); $this->setStateEntities(ENTITY_INVOICE_ITEM, $invoiceItemIds); return $this->createResponse(SKYPE_CARD_RECEIPT, $invoice->present()->skypeBot); }
/** * @param $source * @param $entityType * @param $row * @return bool|mixed */ private function transformRow($source, $entityType, $row) { $transformer = $this->getTransformer($source, $entityType, $this->maps); // Create expesnse category if ($entityType == ENTITY_EXPENSE) { if (!empty($row->expense_category)) { $categoryId = $transformer->getExpenseCategoryId($row->expense_category); if (!$categoryId) { $category = $this->expenseCategoryRepo->save(['name' => $row->expense_category]); $this->addExpenseCategoryToMaps($category); } } if (!empty($row->vendor)) { $vendorId = $transformer->getVendorId($row->vendor); if (!$vendorId) { $vendor = $this->vendorRepo->save(['name' => $row->vendor, 'vendor_contact' => []]); $this->addVendorToMaps($vendor); } } } $resource = $transformer->transform($row); if (!$resource) { return false; } $data = $this->fractal->createData($resource)->toArray(); // if the invoice number is blank we'll assign it if ($entityType == ENTITY_INVOICE && !$data['invoice_number']) { $account = Auth::user()->account; $invoice = Invoice::createNew(); $data['invoice_number'] = $account->getNextInvoiceNumber($invoice); } if (EntityModel::validate($data, $entityType) !== true) { return false; } if ($entityType == ENTITY_INVOICE) { if (empty($this->processedRows[$data['invoice_number']])) { $this->processedRows[$data['invoice_number']] = $data; } else { // Merge invoice items $this->processedRows[$data['invoice_number']]['invoice_items'] = array_merge($this->processedRows[$data['invoice_number']]['invoice_items'], $data['invoice_items']); return true; } } else { $this->processedRows[] = $data; } end($this->processedRows); return key($this->processedRows); }
/** * @param $source * @param $entityType * @param $row * @return bool|mixed */ private function transformRow($source, $entityType, $row) { $transformer = $this->getTransformer($source, $entityType, $this->maps); $resource = $transformer->transform($row); if (!$resource) { return false; } $data = $this->fractal->createData($resource)->toArray(); // if the invoice number is blank we'll assign it if ($entityType == ENTITY_INVOICE && !$data['invoice_number']) { $account = Auth::user()->account; $invoice = Invoice::createNew(); $data['invoice_number'] = $account->getNextInvoiceNumber($invoice); } if (EntityModel::validate($data, $entityType) !== true) { return false; } if ($entityType == ENTITY_INVOICE) { if (empty($this->processedRows[$data['invoice_number']])) { $this->processedRows[$data['invoice_number']] = $data; } else { // Merge invoice items $this->processedRows[$data['invoice_number']]['invoice_items'] = array_merge($this->processedRows[$data['invoice_number']]['invoice_items'], $data['invoice_items']); return true; } } else { $this->processedRows[] = $data; } end($this->processedRows); return key($this->processedRows); }