Hi,
I have no idea about programming but I can follow logic and instructions.
I am a complete newb.
I run an ecommerce store and I am setting up data files that store order data for my accounting program - I download and import the data (txt files) daily.
I have a sales (invoices) file creation script, I have a customer file creation script - Now I want to create a purchases script.
To do this I am modifying the sales script as most of the data can be the same.
I am stuck however on calculating the purchase price from the sale price for insertion into the text file.
(im using oscommerce if that helps)
So the script section for collecting the sale price for the invoices file creation is this -
$product_price = $order->products[$i]['price'];
What I need it to do is calculate my cost price from the sale price.
To calculate my sell price is easy, I get my cost price and apply a margin to it - as follows
if cost = 0 - 100 then sell price is 1.15
if cost = 100.01 - 200 then sell price is 1.13
and so on (there are about 6 price breaks in my pricing matrix - but you get the idea).
So if my sell price is $80 then my cost is 80 / 1.15
Easy enough right?
So how can I add to the script to calculate my cost price from the sell price and insert that figure into the txt file.
Something like (try not to laugh)
$product_price = $order->products[$i]['price'];
if $product_price = <80 then /1.15
elseif $product_price = <180 then /1.13
elseif $product_price = <380 then /1.12
etc etc
If it helps the more of the page script is here
// some info we may need
$customers_id = $customer_id;
$customers_firstname = $order->customer['firstname'];
$customers_lastname = $order->customer['lastname'];
$customers_street_address = $order->customer['street_address'];
$customers_suburb = $order->customer['suburb'];
$customers_city = $order->customer['city'];
$customers_postcode = $order->customer['postcode'];
$customers_state = $order->customer['state'];
$customers_country = $order->customer['country']['title'];
$customers_telephone = $order->customer['telephone'];
$customers_email_address = $order->customer['email_address'];
$customers_address_format_id = $order->customer['format_id'];
$delivery_name = $order->delivery['firstname'] . ' ' . $order->delivery['lastname'];
$delivery_street_address = $order->delivery['street_address'];
$delivery_suburb = $order->delivery['suburb'];
$delivery_city = $order->delivery['city'];
$delivery_postcode = $order->delivery['postcode'];
$delivery_state = $order->delivery['state'];
$delivery_country = $order->delivery['country']['title'];
$delivery_address_format_id = $order->delivery['format_id'];
$payment_method = $order->info['payment_method'];
$cc_type = $order->info['cc_type'];
$cc_owner = $order->info['cc_owner'];
$cc_number = $order->info['cc_number'];
$cc_expires = $order->info['cc_expires'];
$date_purchased = 'now()';
$orders_status = DEFAULT_ORDERS_STATUS_ID;
$comments = $order->info['comments'];
$currency = $order->info['currency'];
$currency_value = $order->info['currency_value'];
// First we setup the format of our transaction header,
// These can be changed to suit your requirements, but
// read the MYOB docs for more info.
$transaction_header ="LASTNAME\tFIRSTNAME\tADDR1\tADDR2\tADDR3\tADDR4\tTAXINC\tORDERNUM\tORDERDATE\t";
$transaction_header.="SUPPINV\tSHIPVIA\tPRINTED\tITEMNO\tQTY\tDESC\tPRICE\tINCTAX\t";
$transaction_header.="DISC\tTOTAL\tTAXTOT\tJOB\tCOMMENT\tJNLMEMO\tSFRST\tSLAST\tSHIPDATE\tREF\tTAXCODE\t";
$transaction_header.="NOGSTAMT\tGSTAMT\tLCTAMT\tFRTAMT\tINCTXFRT\tFRTTXCD\tFRTNOGST\tFRTGST\tFRTLCT\t";
$transaction_header.="STATUS\tTERMS\tDISCDYS\tDUEDAYS\tPCNTDISC\tAMTPD\t\n";
$end_order ="\n"; //adds a blank line between orders so MYOB knows when one order stops and a new one starts
// Now we setup some of the info that we need to send to the file
$order_date = date('d/m/y');
$ordernum = $insert_id; //this will be the order number
$sale_name = $order->customer['firstname'] . ' ' . $order->customer['lastname']; //matches the format in myob_cust.php
$shipping = $order->info['shipping_cost'];
$frtgst = ($shipping / 11); // Freight GST amount
$frtnogst = ($frtgst * 9); // Shipping cost minus GST
$frtgst = number_format($frtgst, 2, '.', '');
$nongstc = number_format($nongstc, 2, '.', '');
$nongst = number_format($nongst, 2, '.', '');
$frtnogst = number_format($frtnogst, 2, '.', '');
$inctxprc = number_format($inctxprc, 2, '.', '');
// Convert transaction amounts from TEP to QB currency, if needed.
if (CONVERT_MYOB_CURRENCY==1)
{
$shipping = ($shipping * EXCHANGE_RATE);
$shipping_cost = ($shipping_cost * EXCHANGE_RATE);
$total_tax = ($total_tax * EXCHANGE_RATE);
$total_cost = ($total_cost * EXCHANGE_RATE);}
// more info for the TXT file.
$shipping = number_format($shipping, 2, '.', '');
$shipping_cost = number_format($shipping_cost, 2, '.', '');
$total_cost = number_format($total_cost, 2, '.', '');
$transtotal = $cart->show_total() + $shipping;
// Check if the transaction import file exists;
$oldfile = file_exists ($salefile);
// Open transaction import file for appending, create it if necessary. 1 line for each product.
$fp = fopen($salefile,"a");
if (!$oldfile)
{
fputs($fp,$transaction_header);
}
$lines=$ordernum;
$lines++;
for ($i=0; $i<sizeof($order->products); $i++) {
$product_name = $order->products[$i]['name'];
$product_price = $order->products[$i]['price'];
//$inctxprc = ($product_price * $qty);
$product_number = $order->products[$i]['model'];
$class = $order->products[$i]['class'];
$desc = $order->products[$i]['name'];
//$vendor = $order->products[$i]['vendor'];
$qty = $order->products[$i]['qty'];
$product_id = $order->products[$i]['id'];
//$qty = $qty - ($qty * 2); // MUST BE NEGATIVE NUMBER, SO 'FLIP' IT
$total_product_price = ($product_price + $cart->attributes_price($order->products[$i]['id']));
// $product_tax = tep_get_tax_rate($delivery_values['zone_id'], $order->products[$i]['tax_class_id']);
$attributes_exist = '0';
if ($order->products[$i]['attributes']) {
$attributes_exist = '1';
reset($order->products[$i]['attributes']);
while (list($option, $value) = each($order->products[$i]['attributes'])) {
$attributes = tep_db_query("select popt.products_options_name, poval.products_options_values_name, pa.options_values_price, pa.price_prefix from products_options popt, products_options_values poval, products_attributes pa where pa.products_id = '" . $order->products[$i]['id'] . "' and pa.options_id = '" . $option . "' and pa.options_id = popt.products_options_id and pa.options_values_id = '" . $value . "' and pa.options_values_id = poval.products_options_values_id"); $attributes_values = tep_db_fetch_array($attributes);
// If there are attributes, we'll simply append the values to the item description
// on the invoice line
$memo.= ' ';
$memo.= $attributes_values['products_options_values_name'];
$product_price += $attributes_values['options_values_price'];
}
}
if (CONVERT_MYOB_CURRENCY==1) {
$product_price = ($product_price * EXCHANGE_RATE);
$total_product_price = ($total_product_price * EXCHANGE_RATE);
}
$product_price = number_format($product_price, 2, '.', '');
$total_product_price = number_format($total_product_price, 2, '.', '');
$account = $order->products[$i]['accnt'];
$total_product_price = $order->products[$i]['final_price'];
$ItemCost = $total_product_price * $qty;
/*
// CHANGE THESE TO SUIT YOUR MYOB TAX CODES
if ($product_tax == "0.0000") { $taxcode = "E"; }
elseif ($product_tax == "0.0000") {
$taxcode = "G"; }
else { $taxcode = "E"; }*/
$taxcode = "GST"; //Australia has only one tax code
$total = $order->info['total'];
$gst = ($product_price / 10); //product_price is price with no gst added yet
$gst = number_format($gst, 2, '.', '');
// Let's work out how much GST is paid on the items total (no shipping)
$nongsta = ($total - $shipping);
$nongstc = ($nongsta / 11);
$nongst = ($nongstc * 10);
// Let's work out the GST inclusive product total
$inctxtot1 = $product_price *$qty;
$inctxtot2 = ($inctxtot1 / 10);
$inctxtot = ($inctxtot2 * 11);
$inctxprc1 = ($product_price / 10);
$inctxprc = ($inctxprc1 * 11);
// Let's work out how much GST is paid on the shipping
$fgst = ($shipping / 11);
// Create order lines & write them to file
$line ="$sale_name\t\t$delivery_name\t$delivery_street_address\t$delivery_suburb\t$delivery_postcode\t$taxinclusive\t$ordernum\t$order_date\t";
$line.="$suppinv\t$shipvia\t$printed\t$product_number\t$qty\t$desc\t\t$inctxprc\t";
$line.="$disc\t\t$inctxtot\t\t$comments\t$memo\t$sfirst\t$slast\t$order_date\t$referrer\t$taxcode\t";
$line.="$sfirst\t$gst\t\t\t$shipping\t$shiptaxcode\t\t$fgst\t\t";
$line.="$cleared\t$terms\t0\t0\t$discp\t\t\n";
fputs($fp,$line);
$lines++;}
// write an end of order line
fputs($fp,$end_order);
fclose($fp);
?>
Thanks for any help.