|
@@ -31,6 +31,7 @@ class BuyCoursesPlugin extends Plugin
|
|
|
const TABLE_SERVICES_SALE = 'plugin_buycourses_service_sale';
|
|
|
const TABLE_CULQI = 'plugin_buycourses_culqi';
|
|
|
const TABLE_GLOBAL_CONFIG = 'plugin_buycourses_global_config';
|
|
|
+ const TABLE_INVOICE = 'plugin_buycourses_invoices';
|
|
|
const PRODUCT_TYPE_COURSE = 1;
|
|
|
const PRODUCT_TYPE_SESSION = 2;
|
|
|
const PAYMENT_TYPE_PAYPAL = 1;
|
|
@@ -51,6 +52,10 @@ class BuyCoursesPlugin extends Plugin
|
|
|
const SERVICE_TYPE_LP_FINAL_ITEM = 4;
|
|
|
const CULQI_INTEGRATION_TYPE = 'INTEG';
|
|
|
const CULQI_PRODUCTION_TYPE = 'PRODUC';
|
|
|
+ const TAX_APPLIES_TO_ALL = 1;
|
|
|
+ const TAX_APPLIES_TO_ONLY_COURSE = 2;
|
|
|
+ const TAX_APPLIES_TO_ONLY_SESSION = 3;
|
|
|
+ const TAX_APPLIES_TO_ONLY_SERVICES = 4;
|
|
|
|
|
|
public $isAdminPlugin = true;
|
|
|
|
|
@@ -60,7 +65,7 @@ class BuyCoursesPlugin extends Plugin
|
|
|
public function __construct()
|
|
|
{
|
|
|
parent::__construct(
|
|
|
- '1.0',
|
|
|
+ '5.0',
|
|
|
"
|
|
|
Jose Angel Ruiz - NoSoloRed (original author) <br/>
|
|
|
Francis Gonzales and Yannick Warnier - BeezNest (integration) <br/>
|
|
@@ -81,6 +86,8 @@ class BuyCoursesPlugin extends Plugin
|
|
|
'commissions_enable' => 'boolean',
|
|
|
'unregistered_users_enable' => 'boolean',
|
|
|
'hide_free_text' => 'boolean',
|
|
|
+ 'invoicing_enable' => 'boolean',
|
|
|
+ 'tax_enable' => 'boolean',
|
|
|
]
|
|
|
);
|
|
|
}
|
|
@@ -164,6 +171,131 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$this->manageTab(false);
|
|
|
}
|
|
|
|
|
|
+ public function update()
|
|
|
+ {
|
|
|
+ $table = self::TABLE_GLOBAL_CONFIG;
|
|
|
+ $sql = "SHOW COLUMNS FROM $table WHERE Field = 'global_tax_perc'";
|
|
|
+ $res = Database::query($sql);
|
|
|
+
|
|
|
+ if (Database::num_rows($res) === 0) {
|
|
|
+ $sql = "ALTER TABLE $table ADD (
|
|
|
+ sale_email varchar(255) NOT NULL,
|
|
|
+ global_tax_perc int(11) unsigned NOT NULL,
|
|
|
+ tax_applies_to int(11) unsigned NOT NULL,
|
|
|
+ tax_name varchar(255) NOT NULL,
|
|
|
+ seller_name varchar(255) NOT NULL,
|
|
|
+ seller_id varchar(255) NOT NULL,
|
|
|
+ seller_address varchar(255) NOT NULL,
|
|
|
+ seller_email varchar(255) NOT NULL,
|
|
|
+ next_number_invoice int(11) unsigned NOT NULL,
|
|
|
+ invoice_series varchar(255) NOT NULL
|
|
|
+ )";
|
|
|
+ $res = Database::query($sql);
|
|
|
+ if (!$res) {
|
|
|
+ echo Display::return_message($this->get_lang('ErrorUpdateFieldDB'), 'warning');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $table = self::TABLE_ITEM;
|
|
|
+ $sql = "SHOW COLUMNS FROM $table WHERE Field = 'tax_perc'";
|
|
|
+ $res = Database::query($sql);
|
|
|
+
|
|
|
+ if (Database::num_rows($res) === 0) {
|
|
|
+ $sql = "ALTER TABLE $table ADD tax_perc int(11) unsigned NULL";
|
|
|
+ $res = Database::query($sql);
|
|
|
+ if (!$res) {
|
|
|
+ echo Display::return_message($this->get_lang('ErrorUpdateFieldDB'), 'warning');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $table = self::TABLE_SERVICES;
|
|
|
+ $sql = "SHOW COLUMNS FROM $table WHERE Field = 'tax_perc'";
|
|
|
+ $res = Database::query($sql);
|
|
|
+
|
|
|
+ if (Database::num_rows($res) === 0) {
|
|
|
+ $sql = "ALTER TABLE $table ADD tax_perc int(11) unsigned NULL";
|
|
|
+ $res = Database::query($sql);
|
|
|
+ if (!$res) {
|
|
|
+ echo Display::return_message($this->get_lang('ErrorUpdateFieldDB'), 'warning');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $table = self::TABLE_SALE;
|
|
|
+ $sql = "SHOW COLUMNS FROM $table WHERE Field = 'tax_perc'";
|
|
|
+ $res = Database::query($sql);
|
|
|
+
|
|
|
+ if (Database::num_rows($res) === 0) {
|
|
|
+ $sql = "ALTER TABLE $table ADD (
|
|
|
+ price_without_tax decimal(10,2) NULL,
|
|
|
+ tax_perc int(11) unsigned NULL,
|
|
|
+ tax_amount decimal(10,2) NULL,
|
|
|
+ invoice int(11) unsigned NULL
|
|
|
+ )";
|
|
|
+ $res = Database::query($sql);
|
|
|
+ if (!$res) {
|
|
|
+ echo Display::return_message($this->get_lang('ErrorUpdateFieldDB'), 'warning');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $table = self::TABLE_SERVICES_SALE;
|
|
|
+ $sql = "SHOW COLUMNS FROM $table WHERE Field = 'tax_perc'";
|
|
|
+ $res = Database::query($sql);
|
|
|
+
|
|
|
+ if (Database::num_rows($res) === 0) {
|
|
|
+ $sql = "ALTER TABLE $table ADD (
|
|
|
+ price_without_tax decimal(10,2) NULL,
|
|
|
+ tax_perc int(11) unsigned NULL,
|
|
|
+ tax_amount decimal(10,2) NULL,
|
|
|
+ invoice int(11) unsigned NULL
|
|
|
+ )";
|
|
|
+ $res = Database::query($sql);
|
|
|
+ if (!$res) {
|
|
|
+ echo Display::return_message($this->get_lang('ErrorUpdateFieldDB'), 'warning');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $table = self::TABLE_INVOICE;
|
|
|
+ $sql = "CREATE TABLE IF NOT EXISTS $table (
|
|
|
+ id int(11) unsigned NOT NULL AUTO_INCREMENT,
|
|
|
+ sale_id int(11) unsigned NOT NULL,
|
|
|
+ is_service int(11) unsigned NOT NULL,
|
|
|
+ num_invoice int(11) unsigned NOT NULL,
|
|
|
+ year int(4) unsigned NOT NULL,
|
|
|
+ serie varchar(255) NOT NULL,
|
|
|
+ date_invoice datetime NOT NULL,
|
|
|
+ PRIMARY KEY (id)
|
|
|
+ )";
|
|
|
+ $res = Database::query($sql);
|
|
|
+
|
|
|
+ Display::addFlash(
|
|
|
+ Display::return_message(
|
|
|
+ $this->get_lang('Updated'),
|
|
|
+ 'info',
|
|
|
+ false
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ $fieldlabel = 'buycourses_company';
|
|
|
+ $fieldtype = '1';
|
|
|
+ $fieldtitle = $this->get_lang('Company');
|
|
|
+ $fielddefault = '';
|
|
|
+ $field_id = UserManager::create_extra_field($fieldlabel, $fieldtype, $fieldtitle, $fielddefault);
|
|
|
+
|
|
|
+ $fieldlabel = 'buycourses_vat';
|
|
|
+ $fieldtype = '1';
|
|
|
+ $fieldtitle = $this->get_lang('VAT');
|
|
|
+ $fielddefault = '';
|
|
|
+ $field_id = UserManager::create_extra_field($fieldlabel, $fieldtype, $fieldtitle, $fielddefault);
|
|
|
+
|
|
|
+ $fieldlabel = 'buycourses_address';
|
|
|
+ $fieldtype = '1';
|
|
|
+ $fieldtitle = $this->get_lang('Address');
|
|
|
+ $fielddefault = '';
|
|
|
+ $field_id = UserManager::create_extra_field($fieldlabel, $fieldtype, $fieldtitle, $fielddefault);
|
|
|
+
|
|
|
+ header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses');
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* This function verify if the plugin is enable and return the price info for a course or session in the new grid
|
|
|
* catalog for 1.11.x , the main purpose is to show if a course or session is in sale it shows in the main platform
|
|
@@ -525,12 +657,29 @@ class BuyCoursesPlugin extends Plugin
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
+ $price = $item['price'];
|
|
|
+ $taxPerc = null;
|
|
|
+ $priceWithoutTax = $item['price'];
|
|
|
+
|
|
|
+ $taxEnable = $this->get('tax_enable') === 'true';
|
|
|
+ $globalParameters = $this->getGlobalParameters();
|
|
|
+ $taxAppliesTo = $globalParameters['tax_applies_to'];
|
|
|
+ if ($taxEnable &&
|
|
|
+ ($taxAppliesTo == self::TAX_APPLIES_TO_ALL || $taxAppliesTo == self::TAX_APPLIES_TO_ONLY_COURSE)
|
|
|
+ ) {
|
|
|
+ $globalTaxPerc = $globalParameters['global_tax_perc'];
|
|
|
+ $precision = 2;
|
|
|
+ $taxPerc = is_null($item['tax_perc']) ? $globalTaxPerc : $item['tax_perc'];
|
|
|
+ $taxAmount = round($priceWithoutTax * $taxPerc / 100, $precision);
|
|
|
+ $price = $priceWithoutTax + $taxAmount;
|
|
|
+ }
|
|
|
+
|
|
|
$courseItem = [
|
|
|
'id' => $course->getId(),
|
|
|
'title' => $course->getTitle(),
|
|
|
'code' => $course->getCode(),
|
|
|
'course_img' => null,
|
|
|
- 'price' => $item['price'],
|
|
|
+ 'price' => $price,
|
|
|
'currency' => $item['iso_code'],
|
|
|
'teachers' => [],
|
|
|
'enrolled' => $this->getUserStatusForCourse(api_get_user_id(), $course),
|
|
@@ -592,6 +741,24 @@ class BuyCoursesPlugin extends Plugin
|
|
|
]
|
|
|
);
|
|
|
|
|
|
+ $price = $item['price'];
|
|
|
+ $taxPerc = null;
|
|
|
+ $priceWithoutTax = $item['price'];
|
|
|
+ $precision = 2;
|
|
|
+
|
|
|
+ $taxEnable = $this->get('tax_enable') === 'true';
|
|
|
+ $globalParameters = $this->getGlobalParameters();
|
|
|
+ $taxAppliesTo = $globalParameters['tax_applies_to'];
|
|
|
+ if ($taxEnable &&
|
|
|
+ ($taxAppliesTo == self::TAX_APPLIES_TO_ALL || $taxAppliesTo == self::TAX_APPLIES_TO_ONLY_COURSE)
|
|
|
+ ) {
|
|
|
+ $globalTaxPerc = $globalParameters['global_tax_perc'];
|
|
|
+ $precision = 2;
|
|
|
+ $taxPerc = is_null($item['tax_perc']) ? $globalTaxPerc : $item['tax_perc'];
|
|
|
+ $taxAmount = round($priceWithoutTax * $taxPerc / 100, $precision);
|
|
|
+ $price = $priceWithoutTax + $taxAmount;
|
|
|
+ }
|
|
|
+
|
|
|
$courseInfo = [
|
|
|
'id' => $course->getId(),
|
|
|
'title' => $course->getTitle(),
|
|
@@ -599,7 +766,13 @@ class BuyCoursesPlugin extends Plugin
|
|
|
'code' => $course->getCode(),
|
|
|
'visual_code' => $course->getVisualCode(),
|
|
|
'teachers' => [],
|
|
|
- 'price' => $item['price'],
|
|
|
+ 'price' => number_format($price, $precision),
|
|
|
+ 'price_without_tax' => number_format($priceWithoutTax, $precision),
|
|
|
+ 'tax_amount' => number_format($taxAmount, $precision),
|
|
|
+ 'tax_perc' => $taxPerc,
|
|
|
+ 'tax_name' => $globalParameters['tax_name'],
|
|
|
+ 'tax_enable' => $taxEnable &&
|
|
|
+ ($taxAppliesTo == self::TAX_APPLIES_TO_ALL || $taxAppliesTo == self::TAX_APPLIES_TO_ONLY_COURSE),
|
|
|
'currency' => $item['iso_code'],
|
|
|
'course_img' => null,
|
|
|
];
|
|
@@ -658,13 +831,36 @@ class BuyCoursesPlugin extends Plugin
|
|
|
'coach_access_end_date' => $session->getCoachAccessEndDate(),
|
|
|
]);
|
|
|
|
|
|
+ $price = $item['price'];
|
|
|
+ $taxPerc = null;
|
|
|
+ $priceWithoutTax = $item['price'];
|
|
|
+ $precision = 2;
|
|
|
+
|
|
|
+ $taxEnable = $this->get('tax_enable') === 'true';
|
|
|
+ $globalParameters = $this->getGlobalParameters();
|
|
|
+ $taxAppliesTo = $globalParameters['tax_applies_to'];
|
|
|
+ if ($taxEnable &&
|
|
|
+ ($taxAppliesTo == self::TAX_APPLIES_TO_ALL || $taxAppliesTo == self::TAX_APPLIES_TO_ONLY_SESSION)
|
|
|
+ ) {
|
|
|
+ $globalTaxPerc = $globalParameters['global_tax_perc'];
|
|
|
+ $taxPerc = is_null($item['tax_perc']) ? $globalTaxPerc : $item['tax_perc'];
|
|
|
+ $taxAmount = round($priceWithoutTax * $taxPerc / 100, $precision);
|
|
|
+ $price = $priceWithoutTax + $taxAmount;
|
|
|
+ }
|
|
|
+
|
|
|
$sessionInfo = [
|
|
|
'id' => $session->getId(),
|
|
|
'name' => $session->getName(),
|
|
|
'description' => $session->getDescription(),
|
|
|
'dates' => $sessionDates,
|
|
|
'courses' => [],
|
|
|
- 'price' => $item['price'],
|
|
|
+ 'price' => number_format($price, $precision),
|
|
|
+ 'price_without_tax' => number_format($priceWithoutTax, $precision),
|
|
|
+ 'tax_amount' => number_format($taxAmount, $precision),
|
|
|
+ 'tax_perc' => $taxPerc,
|
|
|
+ 'tax_name' => $globalParameters['tax_name'],
|
|
|
+ 'tax_enable' => $taxEnable &&
|
|
|
+ ($taxAppliesTo == self::TAX_APPLIES_TO_ALL || $taxAppliesTo == self::TAX_APPLIES_TO_ONLY_SESSION),
|
|
|
'currency' => $item['iso_code'],
|
|
|
'image' => null,
|
|
|
'nbrCourses' => $session->getNbrCourses(),
|
|
@@ -771,6 +967,26 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$productName = $session->getName();
|
|
|
}
|
|
|
|
|
|
+ $price = $item['price'];
|
|
|
+ $priceWithoutTax = null;
|
|
|
+ $taxPerc = null;
|
|
|
+
|
|
|
+ $taxEnable = $this->get('tax_enable') === 'true';
|
|
|
+ $globalParameters = $this->getGlobalParameters();
|
|
|
+ $taxAppliesTo = $globalParameters['tax_applies_to'];
|
|
|
+ if ($taxEnable &&
|
|
|
+ ($taxAppliesTo == self::TAX_APPLIES_TO_ALL ||
|
|
|
+ ($taxAppliesTo == TAX_APPLIES_TO_ONLY_COURSE && $item['product_type'] == self::PRODUCT_TYPE_COURSE) ||
|
|
|
+ ($taxAppliesTo == TAX_APPLIES_TO_ONLY_SESSION && $item['product_type'] == self::PRODUCT_TYPE_SESSION))
|
|
|
+ ) {
|
|
|
+ $priceWithoutTax = $item['price'];
|
|
|
+ $globalTaxPerc = $globalParameters['global_tax_perc'];
|
|
|
+ $precision = 2;
|
|
|
+ $taxPerc = is_null($item['tax_perc']) ? $globalTaxPerc : $item['tax_perc'];
|
|
|
+ $taxAmount = round($priceWithoutTax * $taxPerc / 100, $precision);
|
|
|
+ $price = $priceWithoutTax + $taxAmount;
|
|
|
+ }
|
|
|
+
|
|
|
$values = [
|
|
|
'reference' => $this->generateReference(
|
|
|
api_get_user_id(),
|
|
@@ -783,7 +999,10 @@ class BuyCoursesPlugin extends Plugin
|
|
|
'product_type' => $item['product_type'],
|
|
|
'product_name' => $productName,
|
|
|
'product_id' => $item['product_id'],
|
|
|
- 'price' => $item['price'],
|
|
|
+ 'price' => $price,
|
|
|
+ 'price_without_tax' => $priceWithoutTax,
|
|
|
+ 'tax_perc' => $taxPerc,
|
|
|
+ 'tax_amount' => $taxAmount,
|
|
|
'status' => self::SALE_STATUS_PENDING,
|
|
|
'payment_type' => intval($paymentType),
|
|
|
];
|
|
@@ -843,6 +1062,87 @@ class BuyCoursesPlugin extends Plugin
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Get data of sales.
|
|
|
+ *
|
|
|
+ * @param int $saleId The sale id
|
|
|
+ * @param int $isService Check if a service
|
|
|
+ *
|
|
|
+ * @return array The sale data
|
|
|
+ */
|
|
|
+ public function getDataSaleInvoice($saleId, $isService)
|
|
|
+ {
|
|
|
+ $data = [];
|
|
|
+ if ($isService) {
|
|
|
+ $sale = $this->getServiceSale($saleId);
|
|
|
+ $data['reference'] = $sale['reference'];
|
|
|
+ $data['product_name'] = $sale['service']['name'];
|
|
|
+ $data['payment_type'] = $sale['payment_type'];
|
|
|
+ $data['user_id'] = $sale['buyer']['id'];
|
|
|
+ $data['price'] = $sale['price'];
|
|
|
+ $data['price_without_tax'] = $sale['price_without_tax'];
|
|
|
+ $data['tax_perc'] = $sale['tax_perc'];
|
|
|
+ $data['tax_amount'] = $sale['tax_amount'];
|
|
|
+ $data['currency_id'] = $sale['currency_id'];
|
|
|
+ $data['date'] = $sale['buy_date'];
|
|
|
+ } else {
|
|
|
+ $sale = $this->getSale($saleId);
|
|
|
+ $data['reference'] = $sale['reference'];
|
|
|
+ $data['product_name'] = $sale['product_name'];
|
|
|
+ $data['payment_type'] = $sale['payment_type'];
|
|
|
+ $data['user_id'] = $sale['user_id'];
|
|
|
+ $data['price'] = $sale['price'];
|
|
|
+ $data['price_without_tax'] = $sale['price_without_tax'];
|
|
|
+ $data['tax_perc'] = $sale['tax_perc'];
|
|
|
+ $data['tax_amount'] = $sale['tax_amount'];
|
|
|
+ $data['currency_id'] = $sale['currency_id'];
|
|
|
+ $data['date'] = $sale['date'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get data of invoice.
|
|
|
+ *
|
|
|
+ * @param int $saleId The sale id
|
|
|
+ * @param int $isService Check if a service
|
|
|
+ *
|
|
|
+ * @return array The invoice data
|
|
|
+ */
|
|
|
+ public function getDataInvoice($saleId, $isService)
|
|
|
+ {
|
|
|
+ return Database::select(
|
|
|
+ '*',
|
|
|
+ Database::get_main_table(self::TABLE_INVOICE),
|
|
|
+ [
|
|
|
+ 'where' => [
|
|
|
+ 'sale_id = ? AND ' => intval($saleId),
|
|
|
+ 'is_service = ?' => intval($isService),
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ 'first'
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get numeration of invoice.
|
|
|
+ *
|
|
|
+ * @param int $saleId The sale id
|
|
|
+ * @param int $isService Check if a service
|
|
|
+ *
|
|
|
+ * @return array The numeration invoice
|
|
|
+ */
|
|
|
+ public function getNumInvoice($saleId, $isService)
|
|
|
+ {
|
|
|
+ $dataInvoice = $this->getDataInvoice($saleId, $isService);
|
|
|
+ if (empty($dataInvoice)) {
|
|
|
+ return '-';
|
|
|
+ }
|
|
|
+
|
|
|
+ return $dataInvoice['serie'].$dataInvoice['year'].'/'.$dataInvoice['num_invoice'];
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Get currency data by ID.
|
|
|
*
|
|
@@ -897,6 +1197,9 @@ class BuyCoursesPlugin extends Plugin
|
|
|
|
|
|
if ($saleIsCompleted) {
|
|
|
$this->updateSaleStatus($sale['id'], self::SALE_STATUS_COMPLETED);
|
|
|
+ if ($this->get('invoicing_enable') === 'true') {
|
|
|
+ $this->setInvoice($sale['id']);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return $saleIsCompleted;
|
|
@@ -926,6 +1229,82 @@ class BuyCoursesPlugin extends Plugin
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Register a invoice.
|
|
|
+ *
|
|
|
+ * @param int $saleId The sale ID
|
|
|
+ * @param int $isService The service type to filter (default : 0)
|
|
|
+ */
|
|
|
+ public function setInvoice($saleId, $isService = 0)
|
|
|
+ {
|
|
|
+ $invoiceTable = Database::get_main_table(self::TABLE_INVOICE);
|
|
|
+ $year = date("Y");
|
|
|
+
|
|
|
+ $globalParameters = $this->getGlobalParameters();
|
|
|
+ $numInvoice = $globalParameters['next_number_invoice'];
|
|
|
+ $serie = $globalParameters['invoice_series'];
|
|
|
+
|
|
|
+ if (empty($numInvoice)) {
|
|
|
+ $item = Database::select(
|
|
|
+ ['MAX(num_invoice) AS num_invoice'],
|
|
|
+ $invoiceTable,
|
|
|
+ [
|
|
|
+ 'where' => ['year = ?' => $year],
|
|
|
+ ],
|
|
|
+ 'first'
|
|
|
+ );
|
|
|
+
|
|
|
+ $numInvoice = 1;
|
|
|
+ if ($item !== false) {
|
|
|
+ $numInvoice = (int) ($item['num_invoice'] + 1);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Database::update(
|
|
|
+ Database::get_main_table(self::TABLE_GLOBAL_CONFIG),
|
|
|
+ ['next_number_invoice' => 0],
|
|
|
+ ['id = ?' => 1]
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ Database::insert(
|
|
|
+ $invoiceTable,
|
|
|
+ [
|
|
|
+ 'sale_id' => $saleId,
|
|
|
+ 'is_service' => $isService,
|
|
|
+ 'num_invoice' => $numInvoice,
|
|
|
+ 'year' => $year,
|
|
|
+ 'serie' => $serie,
|
|
|
+ 'date_invoice' => api_get_utc_datetime(),
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ // Record invoice in the sales table
|
|
|
+ $table = Database::get_main_table(self::TABLE_SALE);
|
|
|
+ if (empty($isService)) {
|
|
|
+ $table = Database::get_main_table(self::TABLE_SERVICES_SALE);
|
|
|
+ }
|
|
|
+ Database::update(
|
|
|
+ $table,
|
|
|
+ ['invoice' => 1],
|
|
|
+ ['id = ?' => $saleId]
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get Tax's types.
|
|
|
+ *
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getTaxAppliesTo()
|
|
|
+ {
|
|
|
+ return [
|
|
|
+ self::TAX_APPLIES_TO_ALL => $this->get_lang('AllCoursesSessionsAndServices'),
|
|
|
+ self::TAX_APPLIES_TO_ONLY_COURSE => $this->get_lang('OnlyCourses'),
|
|
|
+ self::TAX_APPLIES_TO_ONLY_SESSION => $this->get_lang('OnlySessions'),
|
|
|
+ self::TAX_APPLIES_TO_ONLY_SERVICES => $this->get_lang('OnlyServices'),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Get a list of sales by the status.
|
|
|
*
|
|
@@ -1155,6 +1534,7 @@ class BuyCoursesPlugin extends Plugin
|
|
|
'visible' => false,
|
|
|
'currency' => empty($defaultCurrency) ? null : $defaultCurrency['iso_code'],
|
|
|
'price' => 0.00,
|
|
|
+ 'tax_perc' => null,
|
|
|
];
|
|
|
|
|
|
$item = $this->getItemByProduct($course->getId(), self::PRODUCT_TYPE_COURSE);
|
|
@@ -1164,6 +1544,7 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$courseItem['visible'] = true;
|
|
|
$courseItem['currency'] = $item['iso_code'];
|
|
|
$courseItem['price'] = $item['price'];
|
|
|
+ $courseItem['tax_perc'] = $item['tax_perc'];
|
|
|
}
|
|
|
|
|
|
return $courseItem;
|
|
@@ -1197,6 +1578,7 @@ class BuyCoursesPlugin extends Plugin
|
|
|
'visible' => false,
|
|
|
'currency' => empty($defaultCurrency) ? null : $defaultCurrency['iso_code'],
|
|
|
'price' => 0.00,
|
|
|
+ 'tax_perc' => null,
|
|
|
];
|
|
|
|
|
|
$displayStartDate = $session->getDisplayStartDate();
|
|
@@ -1233,6 +1615,7 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$sessionItem['visible'] = true;
|
|
|
$sessionItem['currency'] = $item['iso_code'];
|
|
|
$sessionItem['price'] = $item['price'];
|
|
|
+ $sessionItem['tax_perc'] = $item['tax_perc'];
|
|
|
}
|
|
|
|
|
|
return $sessionItem;
|
|
@@ -1604,6 +1987,7 @@ class BuyCoursesPlugin extends Plugin
|
|
|
'name' => Security::remove_XSS($service['name']),
|
|
|
'description' => Security::remove_XSS($service['description']),
|
|
|
'price' => $service['price'],
|
|
|
+ 'tax_perc' => $service['tax_perc'] != '' ? (int) $service['tax_perc'] : null,
|
|
|
'duration_days' => intval($service['duration_days']),
|
|
|
'applies_to' => intval($service['applies_to']),
|
|
|
'owner_id' => intval($service['owner_id']),
|
|
@@ -1658,15 +2042,16 @@ class BuyCoursesPlugin extends Plugin
|
|
|
'name' => Security::remove_XSS($service['name']),
|
|
|
'description' => Security::remove_XSS($service['description']),
|
|
|
'price' => $service['price'],
|
|
|
- 'duration_days' => intval($service['duration_days']),
|
|
|
- 'applies_to' => intval($service['applies_to']),
|
|
|
- 'owner_id' => intval($service['owner_id']),
|
|
|
- 'visibility' => intval($service['visibility']),
|
|
|
+ 'tax_perc' => $service['tax_perc'] != '' ? (int) $service['tax_perc'] : null,
|
|
|
+ 'duration_days' => (int) $service['duration_days'],
|
|
|
+ 'applies_to' => (int) $service['applies_to'],
|
|
|
+ 'owner_id' => (int) $service['owner_id'],
|
|
|
+ 'visibility' => (int) $service['visibility'],
|
|
|
'image' => 'simg-'.$id.'.png',
|
|
|
'video_url' => $service['video_url'],
|
|
|
'service_information' => $service['service_information'],
|
|
|
],
|
|
|
- ['id = ?' => intval($id)]
|
|
|
+ ['id = ?' => (int) $id]
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -1723,10 +2108,36 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$services = [];
|
|
|
|
|
|
if ($id) {
|
|
|
+ $price = $return['price'];
|
|
|
+ $taxPerc = null;
|
|
|
+ $priceWithoutTax = $priceWithTax = $return['price'];
|
|
|
+ $precision = 2;
|
|
|
+
|
|
|
+ $taxEnable = $this->get('tax_enable') === 'true';
|
|
|
+ $globalParameters = $this->getGlobalParameters();
|
|
|
+ $taxAppliesTo = $globalParameters['tax_applies_to'];
|
|
|
+ if ($taxEnable &&
|
|
|
+ ($taxAppliesTo == self::TAX_APPLIES_TO_ALL || $taxAppliesTo == self::TAX_APPLIES_TO_ONLY_SERVICES)
|
|
|
+ ) {
|
|
|
+ $globalTaxPerc = $globalParameters['global_tax_perc'];
|
|
|
+ $precision = 2;
|
|
|
+ $taxPerc = is_null($return['tax_perc']) ? $globalTaxPerc : $return['tax_perc'];
|
|
|
+ $taxAmount = round($priceWithoutTax * $taxPerc / 100, $precision);
|
|
|
+ $priceWithTax = $priceWithoutTax + $taxAmount;
|
|
|
+ }
|
|
|
+
|
|
|
$services['id'] = $return['id'];
|
|
|
$services['name'] = $return['name'];
|
|
|
$services['description'] = $return['description'];
|
|
|
- $services['price'] = $return['price'];
|
|
|
+ $services['price'] = $price;
|
|
|
+ $services['tax_perc'] = $return['tax_perc'];
|
|
|
+ $services['price_with_tax'] = number_format($priceWithTax, $precision);
|
|
|
+ $services['price_without_tax'] = number_format($priceWithoutTax, $precision);
|
|
|
+ $services['tax_amount'] = number_format($taxAmount, $precision);
|
|
|
+ $services['tax_perc_show'] = $taxPerc;
|
|
|
+ $services['tax_name'] = $globalParameters['tax_name'];
|
|
|
+ $services['tax_enable'] = $taxEnable &&
|
|
|
+ ($taxAppliesTo == self::TAX_APPLIES_TO_ALL || $taxAppliesTo == self::TAX_APPLIES_TO_ONLY_SERVICES);
|
|
|
$services['currency'] = $return['currency'];
|
|
|
$services['duration_days'] = $return['duration_days'];
|
|
|
$services['applies_to'] = $return['applies_to'];
|
|
@@ -1747,6 +2158,7 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$services[$index]['name'] = $service['name'];
|
|
|
$services[$index]['description'] = $service['description'];
|
|
|
$services[$index]['price'] = $service['price'];
|
|
|
+ $services[$index]['tax_perc'] = $service['tax_perc'];
|
|
|
$services[$index]['currency'] = $service['currency'];
|
|
|
$services[$index]['duration_days'] = $service['duration_days'];
|
|
|
$services[$index]['applies_to'] = $service['applies_to'];
|
|
@@ -1818,7 +2230,7 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$conditions = ['WHERE' => ['ss.buyer_id = ?' => $buyerId], 'ORDER' => 'id ASC'];
|
|
|
}
|
|
|
|
|
|
- if (is_numeric($status)) {
|
|
|
+ if (is_numeric($status) && empty($id)) {
|
|
|
$conditions = ['WHERE' => ['ss.status = ?' => $status], 'ORDER' => 'id ASC'];
|
|
|
}
|
|
|
|
|
@@ -1862,7 +2274,6 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$conditions,
|
|
|
$showData
|
|
|
);
|
|
|
-
|
|
|
$servicesSale = [];
|
|
|
|
|
|
if ($id) {
|
|
@@ -1885,6 +2296,9 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$servicesSale['currency_id'] = $return['currency_id'];
|
|
|
$servicesSale['currency'] = $return['currency'];
|
|
|
$servicesSale['price'] = $return['price'];
|
|
|
+ $servicesSale['price_without_tax'] = $return['price_without_tax'];
|
|
|
+ $servicesSale['tax_perc'] = $return['tax_perc'];
|
|
|
+ $servicesSale['tax_amount'] = $return['tax_amount'];
|
|
|
$servicesSale['node_type'] = $return['node_type'];
|
|
|
$servicesSale['node_id'] = $return['node_id'];
|
|
|
$servicesSale['buyer']['id'] = $buyer['user_id'];
|
|
@@ -1895,6 +2309,7 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$servicesSale['date_end'] = $return['date_end'];
|
|
|
$servicesSale['status'] = $return['status'];
|
|
|
$servicesSale['payment_type'] = $return['payment_type'];
|
|
|
+ $servicesSale['invoice'] = $return['invoice'];
|
|
|
|
|
|
return $servicesSale;
|
|
|
}
|
|
@@ -1931,6 +2346,7 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$servicesSale[$index]['date_end'] = $service['date_end'];
|
|
|
$servicesSale[$index]['status'] = $service['status'];
|
|
|
$servicesSale[$index]['payment_type'] = $service['payment_type'];
|
|
|
+ $servicesSale[$index]['invoice'] = $service['invoice'];
|
|
|
}
|
|
|
|
|
|
return $servicesSale;
|
|
@@ -1972,6 +2388,10 @@ class BuyCoursesPlugin extends Plugin
|
|
|
self::SERVICE_STATUS_COMPLETED
|
|
|
);
|
|
|
|
|
|
+ if ($this->get('invoicing_enable') === 'true') {
|
|
|
+ $this->setInvoice($serviceSaleId, 1);
|
|
|
+ }
|
|
|
+
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -2022,10 +2442,27 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$services = [];
|
|
|
|
|
|
foreach ($return as $index => $service) {
|
|
|
+ $price = $service['price'];
|
|
|
+ $taxPerc = null;
|
|
|
+ $priceWithoutTax = $service['price'];
|
|
|
+
|
|
|
+ $taxEnable = $this->get('tax_enable') === 'true';
|
|
|
+ $globalParameters = $this->getGlobalParameters();
|
|
|
+ $taxAppliesTo = $globalParameters['tax_applies_to'];
|
|
|
+ if ($taxEnable &&
|
|
|
+ ($taxAppliesTo == self::TAX_APPLIES_TO_ALL || $taxAppliesTo == self::TAX_APPLIES_TO_ONLY_SERVICES)
|
|
|
+ ) {
|
|
|
+ $globalTaxPerc = $globalParameters['global_tax_perc'];
|
|
|
+ $precision = 2;
|
|
|
+ $taxPerc = is_null($service['tax_perc']) ? $globalTaxPerc : $service['tax_perc'];
|
|
|
+ $taxAmount = round($priceWithoutTax * $taxPerc / 100, $precision);
|
|
|
+ $price = $priceWithoutTax + $taxAmount;
|
|
|
+ }
|
|
|
+
|
|
|
$services[$index]['id'] = $service['id'];
|
|
|
$services[$index]['name'] = $service['name'];
|
|
|
$services[$index]['description'] = $service['description'];
|
|
|
- $services[$index]['price'] = $service['price'];
|
|
|
+ $services[$index]['price'] = number_format($price, $precision);
|
|
|
$services[$index]['currency'] = $service['currency'];
|
|
|
$services[$index]['duration_days'] = $service['duration_days'];
|
|
|
$services[$index]['applies_to'] = $service['applies_to'];
|
|
@@ -2070,6 +2507,23 @@ class BuyCoursesPlugin extends Plugin
|
|
|
}
|
|
|
|
|
|
$currency = $this->getSelectedCurrency();
|
|
|
+ $price = $service['price'];
|
|
|
+ $priceWithoutTax = null;
|
|
|
+ $taxPerc = null;
|
|
|
+
|
|
|
+ $taxEnable = $this->get('tax_enable') === 'true';
|
|
|
+ $globalParameters = $this->getGlobalParameters();
|
|
|
+ $taxAppliesTo = $globalParameters['tax_applies_to'];
|
|
|
+ if ($taxEnable &&
|
|
|
+ ($taxAppliesTo == self::TAX_APPLIES_TO_ALL || $taxAppliesTo == TAX_APPLIES_TO_ONLY_SERVICES)
|
|
|
+ ) {
|
|
|
+ $priceWithoutTax = $service['price'];
|
|
|
+ $globalTaxPerc = $globalParameters['global_tax_perc'];
|
|
|
+ $precision = 2;
|
|
|
+ $taxPerc = is_null($service['tax_perc']) ? $globalTaxPerc : $service['tax_perc'];
|
|
|
+ $taxAmount = round($priceWithoutTax * $taxPerc / 100, $precision);
|
|
|
+ $price = $priceWithoutTax + $taxAmount;
|
|
|
+ }
|
|
|
|
|
|
$values = [
|
|
|
'service_id' => $serviceId,
|
|
@@ -2079,7 +2533,10 @@ class BuyCoursesPlugin extends Plugin
|
|
|
$infoSelect
|
|
|
),
|
|
|
'currency_id' => $currency['id'],
|
|
|
- 'price' => $service['price'],
|
|
|
+ 'price' => $price,
|
|
|
+ 'price_without_tax' => $priceWithoutTax,
|
|
|
+ 'tax_perc' => $taxPerc,
|
|
|
+ 'tax_amount' => $taxAmount,
|
|
|
'node_type' => $service['applies_to'],
|
|
|
'node_id' => intval($infoSelect),
|
|
|
'buyer_id' => $userId,
|
|
@@ -2145,11 +2602,29 @@ class BuyCoursesPlugin extends Plugin
|
|
|
*/
|
|
|
public function saveGlobalParameters($params)
|
|
|
{
|
|
|
+ $sqlParams = [
|
|
|
+ 'terms_and_conditions' => $params['terms_and_conditions'],
|
|
|
+ 'sale_email' => $params['sale_email'],
|
|
|
+ ];
|
|
|
+
|
|
|
+ if ($this->get('tax_enable') === 'true') {
|
|
|
+ $sqlParams['global_tax_perc'] = $params['global_tax_perc'];
|
|
|
+ $sqlParams['tax_applies_to'] = $params['tax_applies_to'];
|
|
|
+ $sqlParams['tax_name'] = $params['tax_name'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->get('invoicing_enable') === 'true') {
|
|
|
+ $sqlParams['seller_name'] = $params['seller_name'];
|
|
|
+ $sqlParams['seller_id'] = $params['seller_id'];
|
|
|
+ $sqlParams['seller_address'] = $params['seller_address'];
|
|
|
+ $sqlParams['seller_email'] = $params['seller_email'];
|
|
|
+ $sqlParams['next_number_invoice'] = $params['next_number_invoice'];
|
|
|
+ $sqlParams['invoice_series'] = $params['invoice_series'];
|
|
|
+ }
|
|
|
+
|
|
|
return Database::update(
|
|
|
Database::get_main_table(self::TABLE_GLOBAL_CONFIG),
|
|
|
- [
|
|
|
- 'terms_and_conditions' => $params['terms_and_conditions'],
|
|
|
- ],
|
|
|
+ $sqlParams,
|
|
|
['id = ?' => 1]
|
|
|
);
|
|
|
}
|