Created
October 7, 2024 14:38
-
-
Save rasulsh/14cca8f3003e90398df1dd4f7e493f86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function addDocument($data = array()) { | |
// ایجاد یک نمونه از TransactionWrapper با استفاده از registry و اطلاعات اتصال به پایگاه داده | |
$transactionWrapper = new \DDB\TransactionWrapper($this->registry, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE); | |
// اجرای تراکنش با استفاده از متد execute | |
$result = $transactionWrapper->execute(function($tw) use ($data) { | |
try { | |
// دریافت شناسه کاربر | |
$user_id = $this->user->getId(); | |
$user_selected_date = call_user_func($this->language->get('function_jtg'), $this->db->escape($data['user_selected_date'])); | |
$document_note = strip_tags(trim($data['document_note'])); | |
// درج اطلاعات سند در جدول finance_document | |
$tw->query("INSERT INTO `" . DB_PREFIX . "finance_document` SET | |
`user_id` = '" . $tw->escape($user_id) . "', | |
`user_selected_date` = '" . $tw->escape($user_selected_date) . "', | |
`document_note` = '" . $tw->escape($document_note) . "'"); | |
// دریافت شناسه آخرین سند درج شده | |
$document_id = $tw->getLastId(); | |
// آمادهسازی مقادیر برای درج در جدول finance_document_item | |
$values = []; | |
foreach ($data['document_items'] as $item) { | |
$value = empty($item['debtor']) ? $item['creditor'] : $item['debtor'] * (-1); | |
$item_note = strip_tags(trim($item['item_note'])); | |
$values[] = "('" . $document_id . "', '" . | |
$tw->escape($item['contact_id']) . "', '" . | |
$tw->escape($value) . "', '" . | |
$tw->escape($item_note) . "')"; | |
} | |
// درج اطلاعات آیتمهای سند در جدول finance_document_item | |
if (!empty($values)) { | |
$sql = "INSERT INTO `" . DB_PREFIX . "finance_document_item` | |
(`document_id`, `contact_id`, `value`, `item_note`) | |
VALUES " . implode(", ", $values); | |
$tw->query($sql); | |
} | |
// بازگشت شناسه سند درج شده | |
return $document_id; | |
} catch (\Exception $e) { | |
// لاگگیری خطا | |
$this->log->write('Error in addDocument transaction: ' . $e->getMessage()); | |
return false; | |
} | |
}); | |
// بررسی نتیجه تراکنش | |
if ($result !== false) { | |
return $result; // بازگشت شناسه سند درج شده | |
} else { | |
// لاگگیری خطا در صورت شکست تراکنش | |
$this->log->write('Transaction failed in addDocument.'); | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment