/** * Rà soát tất cả payment * check nếu payment đã có transactionId => bỏ qua * Nếu không, tạo mới transaction, transactionItem, với transaction.id=payment.id * update transactionId cho payment * */ public function paymenttotransactionAction() { $dbAdapter = $this->getServiceLocator()->get('dbAdapter'); $dbSql = $this->getServiceLocator()->get('dbSql'); $select = $dbSql->select(['p' => \Crm\Model\Contract\PaymentMapper::TABLE_NAME]); $select->join(['c' => \Crm\Model\ContractMapper::TABLE_NAME], 'p.contractId=c.id', ['c.companyId' => 'companyId']); $select->order(['p.id asc']); $paginatorAdapter = new \Zend\Paginator\Adapter\DbSelect($select, $dbAdapter); $paginator = new \Zend\Paginator\Paginator($paginatorAdapter); $paginator->setItemCountPerPage(200); $page = $this->getRequest()->getQuery('page', 1); $totalConvert = $this->getRequest()->getQuery('totalConvert', 0); $paginator->setCurrentPageNumber($page); $transactionItemMapper = $this->getServiceLocator()->get('\\Accounting\\Model\\Transaction\\ItemMapper'); $transactionMapper = $this->getServiceLocator()->get('\\Accounting\\Model\\TransactionMapper'); $paymentMapper = $this->getServiceLocator()->get('\\Crm\\Model\\Contract\\PaymentMapper'); foreach ($paginator as $row) { $row = (array) $row; $payment = new \Crm\Model\Contract\Payment(); $payment->exchangeArray($row); if ($row['transactionId']) { continue; } $transaction = new \Accounting\Model\Transaction(); $transaction->setId($payment->getId()); if ($payment->getCompanyId()) { $transaction->setCompanyId($payment->getCompanyId()); } else { $transaction->setCompanyId($row['c.companyId']); } if ($payment->getPaymentType() == \Crm\Model\Contract\Payment::PAYMENT_TYPE_EXPORT) { $transaction->setType(\Accounting\Model\Transaction::TYPE_PAY); } else { $transaction->setType(\Accounting\Model\Transaction::TYPE_RECEIPT); } $transaction->setItemType(\Accounting\Model\Transaction::ITEM_TYPE_CRM_CONTRACT); $transaction->setItemId($payment->getContractId()); $transaction->setCreatedById($payment->getCreatedById()); $createdDateTime = new \DateTime($payment->getCreatedDateTime()); $transaction->setCreatedDate($createdDateTime->format(DateBase::COMMON_DATE_FORMAT)); $transaction->setCreatedTime($createdDateTime->format(DateBase::COMMON_TIME_FORMAT)); if ($payment->getStatus() == \Crm\Model\Contract\Payment::STATUS_DELETED) { $transaction->setStatus(\Accounting\Model\Transaction::STATUS_INAPPROVED); } elseif ($payment->getStatus() == \Crm\Model\Contract\Payment::STATUS_CHECKED) { $transaction->setStatus(\Accounting\Model\Transaction::STATUS_ACCOUNTING); } else { $transaction->setStatus(\Accounting\Model\Transaction::STATUS_NEW); } $transaction->setAmount($payment->getAmount()); $transaction->setDescription($payment->getDescription()); if ($payment->getCheckedDate()) { $transaction->setApplyDate($payment->getCheckedDate()); } else { $transaction->setApplyDate($createdDateTime->format(DateBase::COMMON_DATE_FORMAT)); } if ($payment->getCheckedById()) { $transaction->setAccountingById($payment->getCheckedById()); } if ($payment->getCheckedDateTime()) { $transaction->setAccountingDateTime($payment->getCheckedDateTime()); } $data = array('id' => $transaction->getId(), 'companyId' => $transaction->getCompanyId(), 'type' => $transaction->getType(), 'applyDate' => $transaction->getApplyDate() ?: null, 'amount' => $transaction->getAmount() ?: null, 'description' => $transaction->getDescription() ?: null, 'status' => $transaction->getStatus(), 'createdDate' => $transaction->getCreatedDate(), 'createdById' => $transaction->getCreatedById(), 'createdTime' => $transaction->getCreatedTime(), 'approvedById' => $transaction->getApprovedById() ?: null, 'approvedDateTime' => $transaction->getApprovedDateTime() ?: null, 'accountingById' => $transaction->getAccountingById() ?: null, 'accountingDateTime' => $transaction->getAccountingDateTime() ?: null, 'paymentById' => $transaction->getPaymentById() ?: null, 'paymentDateTime' => $transaction->getPaymentDateTime() ?: null, 'itemType' => $transaction->getItemType() ?: null, 'itemId' => $transaction->getItemId() ?: null); $insert = $dbSql->insert($transactionMapper::TABLE_NAME); $insert->values($data); $dbAdapter->query($dbSql->buildSqlString($insert), Adapter::QUERY_MODE_EXECUTE); //$transactionMapper->save($transaction); $payment->setTransactionId($transaction->getId()); $paymentMapper->save($payment); $transactionItem = new \Accounting\Model\Transaction\Item(); $transactionItem->setTransactionId($transaction->getId()); $transactionItem->setDate($transaction->getApplyDate()); $transactionItem->setAmount($transaction->getAmount()); if ($transaction->getType() == \Accounting\Model\Transaction::TYPE_RECEIPT) { if ($payment->getType() == $payment::TYPE_MONEY_TRANSFER) { $transactionItem->setCreditAccountId(308); } else { $transactionItem->setCreditAccountId(303); } } else { if ($payment->getType() == $payment::TYPE_MONEY_TRANSFER) { $transactionItem->setDebitAccountId(308); } else { $transactionItem->setDebitAccountId(303); } } $transactionItem->setItemType($transaction->getItemType()); $transactionItem->setItemId($transaction->getItemId()); $transactionItem->setDescription($transaction->getDescription()); $transactionItem->setStatus($transaction->getStatus()); $transactionItemMapper->save($transactionItem); $totalConvert++; } $this->getViewModel()->setTerminal(true); $this->getViewModel()->setVariable('paginator', $paginator); $this->getViewModel()->setVariable('page', $page); $this->getViewModel()->setVariable('totalPages', $paginator->count() + 1); $this->getViewModel()->setVariable('totalConvert', $totalConvert); if ($page <= $paginator->count()) { $this->getViewModel()->setVariable('redirectUri', Uri::build('/system/tool/paymenttotransaction', ['page' => $page + 1, 'totalConvert' => $totalConvert])); } return $this->getViewModel(); }