Exemplo n.º 1
0
 public function create($data)
 {
     if (!isset($data) || empty($data)) {
         throw new ValidationException('Data invalid', 'saleRecord');
     }
     if (!isset($data->saleProducts) || empty($data->saleProducts)) {
         throw new ValidationException('There are no product in the order', 'saleProducts');
     }
     $now = new \DateTime();
     $saleRecord = new Order();
     $orderItem = new OrderCart();
     if ($data->phoneNumber) {
         $customer = $this->objectManager->getRepository('Application\\Entity\\Customer')->findOneBy(array('phoneNumber' => $data->phoneNumber));
         if (!isset($customer)) {
             $customer = new Customer();
             $customer->setPhoneNumber($data->phoneNumber);
         }
         $saleRecord->setCustomer($customer);
     }
     $saleRecord->setTotalPrice($data->totalPrice);
     $saleRecord->setCreateTime($now);
     foreach ($data->saleProducts as $product) {
         /**@var $productEntity \Application\Entity\Product*/
         $productEntity = $this->objectManager->getRepository('Application\\Entity\\Product')->findOneBy(array('sku' => $product->sku));
         if ($productEntity == null) {
             // if sku is not exists,add it
             throw new ValidationException("该产品不存在于库存中,也许你需要先进货:)", 'sku');
         }
         $item = clone $orderItem;
         $stock = intval($productEntity->getStock());
         $productEntity->setStock($stock - intval($product->quantity));
         $item->setProduct($productEntity);
         $item->setPrice($product->price);
         $item->setQuantity($product->quantity);
         $item->setCreateTime($now);
         $item->setOrder($saleRecord);
         $saleRecord->addOrderCart($item);
         //@todo not work
     }
     $this->objectManager->persist($saleRecord);
     $this->objectManager->flush();
     return true;
 }
Exemplo n.º 2
0
 public function createCustomerAction()
 {
     $resultModel = new JsonResultModel();
     if ($this->getRequest()->isPost()) {
         try {
             $jsonData = $this->params()->fromPost('customer');
             $customer = new Customer(Json::decode($jsonData, Json::TYPE_ARRAY));
             $customer->setCreateTime(new \DateTime());
             //@todo 这一行抛出异常被捕获处理后,会不会执行下一行??
             $this->customerService->create($customer);
         } catch (ValidationException $e) {
             $resultModel->setErrors($e->getValidationError());
             return $resultModel;
         } catch (\Exception $e) {
             $resultModel->addErrors('undefined', $e->getMessage());
             return $resultModel;
         }
         return $resultModel;
     }
 }
Exemplo n.º 3
0
 public function getCustomerById($id)
 {
     return $this->entityManager->getRepository(Customer::getClass())->findOneBy(array('id' => $id));
 }
Exemplo n.º 4
0
 public function saveInvoice(Document\Invoice $invoice, Parameters $data = null, $layout = null)
 {
     $auth = $this->getServiceLocator()->get('zfcuser_auth_service');
     if ($auth->hasIdentity()) {
         $user = $auth->getIdentity();
     }
     $invoice->setCreatedBy($user);
     if ($data !== null) {
         $opencart = isset($data->opencart);
         $invoice->setOpencart($opencart);
         $invoice->setDocumentDate(\DateTime::createFromFormat('d.m.Y', $data->docDate));
         $invoice->setReferenceNumber($data->referenceNr);
         $invoice->setDeadlineDays($data->deadlineDays);
         $invoice->setDelayPercent($data->delayPercent);
         $invoice->setAmount($data->amount);
         $invoice->setComment($data->comment);
         $invoice->setPaymentType($data->paymentType);
         $company = $this->getOneObjectByField(Company::getClass(), 'id', $data->company);
         $invoice->setCompany($company);
         $invoice->setTaxAmount($data->taxAmount);
         $invoice->setAmountTax($data->amountTax);
         $warehouse = $this->getOneObjectByField(Vat::getClass(), 'id', $data->warehouse);
         $invoice->setWarehouse($warehouse);
         $customer = $this->getOneObjectByField(Customer::getClass(), 'id', $data->customer);
         $vat = $this->getOneObjectByField(Vat::getClass(), 'id', $data->vat);
         $invoice->setCustomer($customer);
         $invoice->setVat($vat);
         $this->saveObject($invoice);
         if (isset($data->saveAndConfirm) || isset($data->confirmedOutgoing)) {
             $type = $opencart ? Document\Number::TYPE_OPENCART_INVOICE : Document\Number::TYPE_INVOICE;
             $number = $this->getOneObjectByField(Document\Number::getClass(), 'type', $type);
             $invoice->setPrefix($number->getPrefix());
             $invoice->setDocumentNumber($this->generateInvoiceNo($opencart));
             if ($customer->getRegNo() !== null && $customer->getRegNo() !== '') {
                 $invoice->setReferenceNumber(BankLink::generateReferenceNumber($customer->getRegNo()) . $invoice->getId());
             } else {
                 $invoice->setReferenceNumber(BankLink::generateReferenceNumber($customer->getId()) . $invoice->getId());
             }
             $invoice->setConfirmed(true);
             $invoice->setStatus(Document\Invoice::STATUS_CONFIRMED);
             $this->saveInvoiceFile($invoice, $layout);
         }
     }
     $this->entityManager->persist($invoice);
     $this->entityManager->flush($invoice);
     return $invoice;
 }
Exemplo n.º 5
0
 public function getInDepthClientsAndSums(User $user)
 {
     $result = array();
     $rawArray = $this->entityManager->getRepository(Invoice::getClass())->getInDepthClientsAndSums($user);
     foreach ($rawArray as $ar) {
         $res = array();
         $client = $this->entityManager->getRepository(Customer::getClass())->findOneBy(array('id' => $ar['id']));
         if ($client) {
             $res['client'] = $client;
             $res['sum'] = $ar['unpaid'];
             $result[] = $res;
         }
     }
     return $result;
 }
Exemplo n.º 6
0
 public function setCustomerValues(Customer $customer)
 {
     if ($customer) {
         $this->get('name')->setValue($customer->getName());
         $this->get('regNo')->setValue($customer->getRegNo());
         $this->get('kmkrNo')->setValue($customer->getKmkrNo());
         $this->get('address')->setValue($customer->getAddress());
         $this->get('zip')->setValue($customer->getZip());
         $this->get('delayPercent')->setValue($customer->getDelayPercent());
         $this->get('deadlineDays')->setValue($customer->getDeadlineDays());
         $this->get('country')->setValue($customer->getCountry());
         $this->get('city')->setValue($customer->getZip());
         $this->get('url')->setValue($customer->getUrl());
         $this->get('phone')->setValue($customer->getPhone());
         $this->get('email')->setValue($customer->getEmail());
         $this->get('mob')->setValue($customer->getMob());
     }
     return $this;
 }