winternet-studio/odoo-jsonrpc-client

一个易于理解的简单Odoo JSON-RPC客户端

v1.2.0 2023-11-05 13:21 UTC

This package is auto-updated.

Last update: 2024-09-05 16:37:46 UTC


README

一个易于理解的简单Odoo JSON-RPC客户端 - 并且带有示例。用PHP编写。

让我们直言不讳 - Odoo的API文档很糟糕...如果你能找到的话!但是使用这个库,我会尽力让它的API更易于使用。

目前还在进行中。欢迎通过pull requests来帮助。

安装

安装此扩展的首选方式是通过 composer

composer require winternet-studio/odoo-jsonrpc-client

使用

连接到Odoo

$baseUri = 'https://yourodooserver.com';
$database = 'databaseName';
$username = 'johndoe@sample.com';
$password = 'mypassword';
$odoo = new \winternet\odoo\JsonRpcClient($baseUri, $database, $username, $password);

echo $odoo->version('major');

$userID = $odoo->authenticate();

继续下面的示例,并查看JsonRpcClient类文件中每个方法的文档。

示例

了解可能的请求、方法、字段和可能值的非常有用的一种方法是查看浏览器在导航Odoo系统时发出的请求。打开开发者工具并查看网络请求。

根据Odoo版本,字段可能会有所不同。以下示例适用于v14。

获取所有模型

$models = $odoo->searchRead('ir.model', ['fields' => ['id', 'model', 'name']]);

获取模型的全部字段

$fields = $odoo->fieldsGet('account.move');

通过ID获取记录

$recordIDs = [74049, 74590];
$fields = ['name', 'create_date', 'amount_total_signed'];
$invoices = $odoo->read('account.move', $recordIDs, $fields);

$recordID = 75098;
$invoice = $odoo->read('account.move', $recordID, [], ['single' => true]);  // return a single record or null if it isn't found
$invoice = $odoo->read('account.move', $recordID, [], ['single' => 'require']);  // return a single record or throw exception if it isn't found

发布当前为草稿的记录

例如,发布付款(account.payment)或发票(account.move)。

$recordIDs = [17113];
$odoo->actionPost('account.payment', $recordIDs);

获取发票

$invoices = $odoo->searchRead('account.move', [
	'where' => [
		[
			'move_type',
			'=',
			'out_invoice',
		],
	],
	'limit' => 3,
	'fields' => ['name', 'create_date', 'amount_total_signed'],
], [
	// 'expandFields' => ['invoice_line_ids' => ['model' => 'account.move.line']],  //enable this line to include invoice lines - see expandFields in execute() method in JsonRpcClient class file
]);

创建发票

此发票示例最初是从浏览器网络请求中复制的。

它被创建为草稿,必须使用上面的示例中的actionPost()方法发布。似乎无法在创建时同时发布它。

$createdInvoice = $odoo->create('account.move', [
	'move_type' => 'out_invoice',
	'date' => '2023-10-31',
	// 'show_name_warning' => false,
	// 'posted_before' => false,
	// 'payment_state' => 'not_paid',
	// 'name' => false,
	'partner_id' => 6060,
	'partner_shipping_id' => 6060,  //to set shipping address (in UI they default it to same as customer)
	'ref' => '',
	'payment_reference' => '',
	// 'partner_bank_id' => 55,
	// 'invoice_vendor_bill_id' => false,
	'invoice_date' => '2023-10-31',
	'invoice_date_due' => '2023-11-07',
	'invoice_payment_term_id' => 26,  //ADJUST TO YOUR INSTANCE. Set to false for no payment term, eg. if setting a date instead.
	'journal_id' => 136,  //ADJUST TO YOUR INSTANCE
	'currency_id' => 3,  //ADJUST TO YOUR INSTANCE
	'invoice_line_ids' => [
		[
			0,
			'virtual_848',  // what is this?
			[
				// 'sequence' => 10,
				// 'product_id' => false,
				'name' => 'Line 1',  //invoice line description
				'account_id' => 5262,    //ADJUST TO YOUR INSTANCE
				// 'analytic_account_id' => false,
				// 'analytic_tag_ids' => [
				//     [
				//         6,
				//         false,
				//         [],
				//     ],
				// ],
				// 'asset_profile_id' => false,
				// 'asset_id' => false,
				'quantity' => 1,
				// 'product_uom_id' => false,
				'price_unit' => 55.00,
				// 'discount' => 0,
				'tax_ids' => [
					[
						6,
						false,
						[366],  //ADJUST TO YOUR INSTANCE. Empty array for no tax. Add entry with integer of tax_id to apply tax.
					],
				],
				// 'partner_id' => 6060,
				// 'amount_currency' => -55,
				// 'currency_id' => 3,
				// 'debit' => 0,  /// isn't this always automatically determined?
				// 'credit' => 614.98,  /// isn't this always automatically determined?
				// 'date_maturity' => false,
				// 'tax_tag_ids' => [
				//     [
				//         6,
				//         false,
				//         [],
				//     ],
				// ],
				// 'recompute_tax_line' => false,
				// 'display_type' => false,
				// 'is_rounding_line' => false,
				// 'exclude_from_invoice_tab' => false,
			],
		],
	],
	'narration' => 'This is the comments field',
	// // Journal Elements which are automatically created:
	// 'line_ids' => [
	//     [
	//         0,
	//         'virtual_945',
	//         [
	//             'analytic_tag_ids' => [
	//                 [
	//                     6,
	//                     false,
	//                     [],
	//                 ],
	//             ],
	//             'tax_ids' => [
	//                 [
	//                     6,
	//                     false,
	//                     [],
	//                 ],
	//             ],
	//             'tax_tag_ids' => [
	//                 [
	//                     6,
	//                     false,
	//                     [],
	//                 ],
	//             ],
	//             'account_id' => 5242,
	//             'sequence' => 10,
	//             'name' => '',
	//             'quantity' => 1,
	//             'price_unit' => -11255,
	//             'discount' => 0,
	//             'debit' => 125846.36,
	//             'credit' => 0,
	//             'amount_currency' => 11255,
	//             'date_maturity' => '2023-11-07',
	//             'currency_id' => 3,
	//             'partner_id' => 6060,
	//             'product_uom_id' => false,
	//             'product_id' => false,
	//             'tax_base_amount' => 0,
	//             'tax_exigible' => true,
	//             'tax_repartition_line_id' => false,
	//             'recompute_tax_line' => false,
	//             'display_type' => false,
	//             'is_rounding_line' => false,
	//             'exclude_from_invoice_tab' => true,
	//             'asset_profile_id' => false,
	//             'asset_id' => false,
	//             'analytic_account_id' => false,
	//         ],
	//     ],
	//     [
	//         0,
	//         '',
	//         [
	//             'analytic_tag_ids' => [
	//                 [
	//                     6,
	//                     false,
	//                     [],
	//                 ],
	//             ],
	//             'tax_ids' => [
	//                 [
	//                     6,
	//                     false,
	//                     [],
	//                 ],
	//             ],
	//             'tax_tag_ids' => [
	//                 [
	//                     6,
	//                     false,
	//                     [],
	//                 ],
	//             ],
	//             'account_id' => 5262,
	//             'sequence' => 10,
	//             'name' => 'Line 1',
	//             'quantity' => 1,
	//             'price_unit' => 55,
	//             'discount' => 0,
	//             'debit' => 0,
	//             'credit' => 614.98,
	//             'amount_currency' => -55,
	//             'date_maturity' => false,
	//             'currency_id' => 3,
	//             'partner_id' => 6060,
	//             'product_uom_id' => false,
	//             'product_id' => false,
	//             'tax_base_amount' => 0,
	//             'tax_exigible' => true,
	//             'tax_repartition_line_id' => false,
	//             'recompute_tax_line' => false,
	//             'display_type' => false,
	//             'is_rounding_line' => false,
	//             'exclude_from_invoice_tab' => false,
	//             'asset_profile_id' => false,
	//             'asset_id' => false,
	//             'analytic_account_id' => false,
	//         ],
	//     ],
	//     [
	//         0,
	//         'virtual_922',
	//         [
	//             'analytic_tag_ids' => [
	//                 [
	//                     6,
	//                     false,
	//                     [],
	//                 ],
	//             ],
	//             'tax_ids' => [
	//                 [
	//                     6,
	//                     false,
	//                     [],
	//                 ],
	//             ],
	//             'tax_tag_ids' => [
	//                 [
	//                     6,
	//                     false,
	//                     [],
	//                 ],
	//             ],
	//             'account_id' => 5255,
	//             'sequence' => 10,
	//             'name' => 'Line 2',
	//             'quantity' => 70,
	//             'price_unit' => 160,
	//             'discount' => 0,
	//             'debit' => 0,
	//             'credit' => 125231.38,
	//             'amount_currency' => -11200,
	//             'date_maturity' => '2023-11-07',
	//             'currency_id' => 3,
	//             'partner_id' => 6060,
	//             'product_uom_id' => false,
	//             'product_id' => false,
	//             'tax_base_amount' => 0,
	//             'tax_exigible' => true,
	//             'tax_repartition_line_id' => false,
	//             'recompute_tax_line' => false,
	//             'display_type' => false,
	//             'is_rounding_line' => false,
	//             'exclude_from_invoice_tab' => false,
	//             'asset_profile_id' => false,
	//             'asset_id' => false,
	//             'analytic_account_id' => false
	//         ],
	//     ],
	// ],
	// 'user_id' => 42,
	// 'invoice_user_id' => 42,
	// 'team_id' => 11,
	// 'invoice_origin' => false,
	// 'qr_code_method' => false,
	// 'invoice_incoterm_id' => false,
	// 'fiscal_position_id' => false,
	// 'invoice_cash_rounding_id' => false,
	// 'invoice_source_email' => false,
	// 'auto_post' => false,  //schedule the record to be automatically posted on the invoice date? Defaults to false
	// 'to_check' => false,
	// 'campaign_id' => false,
	// 'medium_id' => false,
	// 'source_id' => false,
	// 'message_follower_ids' => [],
	// 'activity_ids' => [],
	// 'message_ids' => [],
]);

创建付款

$payment_id = $odoo->create('account.payment', [
	// 'name' => false,
	'payment_type' => 'inbound',
	'partner_type' => 'customer',
	'partner_id' => 6197,
	// 'destination_account_id' => 560,
	// 'is_internal_transfer' => false,
	'company_id' => 4,
	'journal_id' => 22,
	'payment_method_id' => 3,
	// 'payment_token_id' => false,
	// 'partner_bank_id' => false,
	'amount' => 100,
	'currency_id' => 15,
	'date' => '2023-11-05',
	// 'ref' => false,
	// 'message_follower_ids' => [],
	// 'activity_ids' => [],
	// 'message_ids' => [],
]);

获取货币

获取货币,其中数组索引值是货币代码。

$currencies = $odoo->searchRead('res.currency', [], ['indexBy' => 'name']);

更新货币为今天的汇率

使用欧洲中央银行的数据更新汇率。

$odoo->updateExchangeRates();