create invoice programmatically

Hi,
I tried to add an existing product to an invoice.

But the system doesn’t load correctly the product.

Can you help me?


                          $new_invoice = BeanFactory::newBean('AOS_Invoices');
		            $new_invoice->id = create_guid();
    			    $new_invoice->new_with_id = true;
			   $new_invoice->billing_account_id = "299d5425-ecd6-b7f9-99f6-5be2e9dfc610";
			   $new_invoice->save();
					
//1d7ad393-3dd2-0fd2-e151-5be5714e732e = exixting product id

			$aosProducts = BeanFactory::getBean('AOS_Products','1d7ad393-3dd2-0fd2-e151-5be5714e732e');
			
		
			$aosProductsQuotes = BeanFactory::newBean('AOS_Products_Quotes');
			$aosProductsQuotes->parent_type = 'AOS_Invoices';
			$aosProductsQuotes->parent_id = $new_invoice->id;
			$aosProductsQuotes->maincode = $aosProducts->maincode;
			$aosProductsQuotes->part_number = $aosProducts->part_number;
			$aosProductsQuotes->product_id = $aosProducts->id;
			$aosProductsQuotes->product_total_price = 100;
			$aosProductsQuotes->save();

Thanks
Marco

I think you’re over-complicating the code:

  • normally you don’t have to assign ids explicitly, the Bean code takes care of that. Just add your own fields.

  • normally you don’t manipulate the relationship tables through their Beans. So I think your code should reference only the Invoice and the Products, not the middle table “Products_Quotes”. I am not 100% sure about this regarding Products because it is a somewhat complex and specific relationship…

But try with load_relationship for aos_quotes_aos_invoices_c

EDIT: also, remember to always check return codes (catch nulls!) when calling a Bean retrieval, a relationship retrieval, etc.