Jump to content
  • 0

New Invoice Line Have No Tax


Question

Posted

Version: 3.0.5

Action: Try to  use function "Invoices-edit" to add new invoice line with tax.

 

The following query add new invoice line. New line should has tax but doesn't has.

https://my.blesta.site/api/Invoices/edit.json?invoice_id=20&vars[client_id]=7&vars[lines][0][service_id]=39&vars[lines][0][description]=test&vars[lines][0][qty]=1&vars[lines][0][amount]=2&vars[lines][0][service_id]=39&tax=true"

8 answers to this question

Recommended Posts

  • 0
Posted

You need to make sure that the client is not tax exempt, and update your tax query parameter to be apart of the line item. Simply specifying a 'tax' parameter is not valid, as line items are taxed individually. In your example, you have a redundant "vars[lines][0][service_id]" parameter, and no "vars[lines][0][tax]" parameter.

  • 0
Posted

simply specifying a 'tax' parameter is not valid, as line items are taxed individually

.

Thank you, it is my fault.

 

 

I'm trying to use next code in my custom plugin:

$vars = array(
'client_id' => $invoice->client_id,
'currency' => $this->Invoices->get($invoice->id)->currency,
'lines' => array(
array(
'service_id' => $line->service_id,
'description' => $this->getInvoiceDescription($fields->vs_id),
'qty' => 1,
'amount' => $balance,
'tax' => true
)
)
);
 
$this->Invoices->edit($invoice->id, $vars);

... but it doen't work - line add but tax doesn't add.

If I am changing this row:

 

$this->Invoices->edit($invoice->id, $vars);

 

 

to

 

$this->Invoices->edit(20, $vars);

...then it works correct.

Do you have any idea how to fix it?

  • 0
Posted

I'm trying to use next code in my custom plugin:

$vars = array(
'client_id' => $invoice->client_id,
'currency' => $this->Invoices->get($invoice->id)->currency,
'lines' => array(
array(
'service_id' => $line->service_id,
'description' => $this->getInvoiceDescription($fields->vs_id),
'qty' => 1,
'amount' => $balance,
'tax' => true
)
)
);
 
$this->Invoices->edit($invoice->id, $vars);

... but it doen't work - line add but tax doesn't add.

If I am changing this row:

 

$this->Invoices->edit($invoice->id, $vars);

 

 

to

 

$this->Invoices->edit(20, $vars);

...then it works correct.

Do you have any idea how to fix it?

 

 

What is the value of $invoice->id? Entering "20" works, but the invoice ID does not? It sounds like you're using an invoice that belongs to a different customer, but we would need to see more of the script that you're writing to determine anything else.

  • 0
Posted

What is the value of $invoice->id? Entering "20" works, but the invoice ID does not?

Id of existed invoice.

Entering "20" works fully correct - tax added.

Entering "$invoice->id" result in that the tax is not added.

 

 

It sounds like you're using an invoice that belongs to a different customer

I changed the code in such a way that the client_id was taken directly from the invoice but it doesn't help:



$invoice->data = $this->Invoices->get($invoice->id);
$vars = array(
'client_id' => $invoice->data->client_id,
'currency' => $invoice->data->currency,
'lines' => array(
array(
'service_id' => $line->service_id,
'description' => $this->getInvoiceDescription($fields->vs_id),
'qty' => 1,
'amount' => $balance,
'tax' => true
)
)
);

 

 

but we would need to see more of the script that you're writing to determine anything else.

What exactly do I need to show?

 

  • 0
Posted

Id of existed invoice.

Entering "20" works fully correct - tax added.

Entering "$invoice->id" result in that the tax is not added.

Yes, but what is the value of $invoice->id? 20? 25? 100? Hard-coding 20 or using an $invoice->id of 20 would produce the same result, so either the tax does not actually differ as you say, or 20 is not the value of $invoice->id.

 

 

What exactly do I need to show?

 

Where the invoice object comes from--basically the code before and after what you've shown already that puts it all into context.

  • 0
Posted

Ok...

Install 


$this->Record->query("
CREATE TABLE IF NOT EXISTS `vs_credit_invoices` (
`creation_date` datetime NOT NULL,
`id` int(11) NOT NULL,
`client_id` int(11) NOT NULL,
`status` enum('NEW','DONE') NOT NULL DEFAULT 'NEW',
PRIMARY KEY (`id`),
KEY `i_status` (`status`)
)
"
);






public function createAdditionalTables() {





$this->Record->query("
CREATE TABLE IF NOT EXISTS `vs_credit_invoice_lines` (
`id` int(11) NOT NULL,
`invoice_id` int(11) NOT NULL,
`service_id` int(11) NOT NULL,
`amount` float(12,4) NOT NULL DEFAULT '0.0000',
PRIMARY KEY (`id`),
KEY `i_invoice_id` (`invoice_id`)
)
"
);




}


 
public function modifyExistedTables() {
$this->Record->query("
ALTER TABLE `invoices`
CHANGE `status`
`status` ENUM('active','draft','void','expect_additions') DEFAULT 'active' NOT NULL
"
);
 
public function createAdditionalTriggers() {
$this->Record->query("
DROP TRIGGER IF EXISTS `tbi_invoice_lines`
"
);

$this->Record->query("
CREATE TRIGGER `tbi_invoice_lines` BEFORE INSERT ON `invoice_lines`
FOR EACH ROW
BEGIN
IF (NEW.`description` LIKE '{{NOT_TRIGGERED}}%') THEN
SET NEW.`description` = REPLACE(NEW.`description`, '{{NOT_TRIGGERED}}', '');

ELSE

SET @credit_fl = (
SELECT
mrm.`value`
FROM `services` s
JOIN `module_row_meta` mrm ON (
mrm.`module_row_id`=s.`module_row_id` AND
mrm.`key`='credit_fl'
)
WHERE s.`id`=NEW.`service_id`
);

IF (@credit_fl = 'yes' ) THEN

UPDATE `invoices`
SET
`status`='expect_additions'
WHERE `id`=NEW.`invoice_id`;

UPDATE `invoice_delivery`
SET
`date_sent`='2001-01-01 00:00:00'
WHERE `invoice_id`=NEW.`invoice_id`;

INSERT INTO `vs_credit_invoices`
SELECT
NOW(),
NEW.`invoice_id`,
s.`client_id`,
'NEW'
FROM `services` s
WHERE s.`id`=NEW.`service_id`;

SET @new_line_id = (
SELECT
`AUTO_INCREMENT` AS 'ai'
FROM information_schema.`TABLES`
WHERE `table_schema`=(SELECT DATABASE())
AND `table_name`='invoice_lines'
);

INSERT INTO `vs_credit_invoice_lines`
VALUES (
@new_line_id ,
NEW.`invoice_id`,
NEW.`service_id`,
NEW.`amount`
);

END IF;

END IF;

END;
"
);
}
}

I also  tried without this part. Result was same:



UPDATE `invoices` SET `status`='expect_additions' WHERE `id`=NEW.`invoice_id`; UPDATE `invoice_delivery` SET `date_sent`='2001-01-01 00:00:00' WHERE `invoice_id`=NEW.`invoice_id`;





Main function for fill invoices:

 

 







private function fillCreditInvoices() {
$invoices = $this->getCreditInvoices();

foreach ($invoices as $invoice) {
$count_lines = $this->getCountInvoiceLines($invoice->id);

foreach ($invoice->invoice_lines as $line) {
$service = $this->Services->get($line->service_id);
$fields = $this->formatServiceFields($service->fields);
$credit = $this->Voipswitch->getVsCredit($fields->vs_id);
$account_state = $this->Voipswitch->getVsAccountData($fields->vs_id)->account_state;
$balance = $credit-$account_state;


if ($balance <= 0) {
if ($line->amount == 0) {
$this->deleteInvoiceLine($line->id);

if ($count_lines == 1) {
$this->deleteInvoice($invoice->id);
}
}
}
else {
$invoice->data = $this->Invoices->get($invoice->id);
$vars = array(
'client_id' => $invoice->data->client_id,
'currency' => $invoice->data->currency,
'lines' => array(
array(
'service_id' => $line->service_id,
'description' => $this->getInvoiceDescription($fields->vs_id),
'qty' => 1,
'amount' => $balance,
'tax' => true
)
)
);

if ($line->amount == 0) {
$vars['lines'][0]['id'] = $line->id;
$vars['lines'][0]['description'] = $this->getInvoiceDescription($fields->vs_id, "no");
}
$this->Invoices->edit($invoice->id, $vars);
$this->Voipswitch->makePayment($fields->vs_id, $balance, 1, "Credit restored from Portal");
}
}

$this->activateInvoice($invoice->id);
$this->markInvoiceAsProcessed($invoice->id);
}
}

 

 


Function for get invoices:

 

 







private function getCreditInvoices() {
$query = "
SELECT
ci.*
FROM `vs_credit_invoices` ci
WHERE ci.`status`='NEW'
";
$result = $this->Record->query($query);
$invoices = $result->fetchAll();

foreach ($invoices as &$invoice) {
$query = "
SELECT
cil.*
FROM `vs_credit_invoice_lines` cil
WHERE cil.`invoice_id`=?
";
$result = $this->Record->query($query, $invoice->id);
$invoice_lines = $result->fetchAll();

$invoice->invoice_lines = $invoice_lines;
}

return $invoices;
}

 

 

  • 0
Posted
if ($line->amount == 0) {
$vars['lines'][0]['id'] = $line->id;
$vars['lines'][0]['description'] = $this->getInvoiceDescription($fields->vs_id, "no");
}
$this->Invoices->edit($invoice->id, $vars);

 

I see no reason the tax would act any differently.

 

You can try to debug and see what your code is passing to Invoices::edit() and see if there are any abnormalities:

if ($line->amount == 0) {
$vars['lines'][0]['id'] = $line->id;
$vars['lines'][0]['description'] = $this->getInvoiceDescription($fields->vs_id, "no");
}

print_r($invoice);
print_r($vars);
die;

$this->Invoices->edit($invoice->id, $vars);

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...