예제 #1
0
 function submitJournalEntry()
 {
     global $db, $currencies, $messageStack;
     $entry_count = 0;
     $row_cnt = 0;
     while ($row_cnt < count($this->records)) {
         $order = $this->records[$row_cnt];
         $glEntry = new journal();
         // determine if date is within a known period, if date is before period 1 use period = 0 (and enter beginning balances)
         $glEntry->period = gen_calculate_period($order['post_date'], $hide_error = true);
         // date format YYYY-MM-DD
         if (!$glEntry->period) {
             $glEntry->period = 1;
         }
         // if out of range default to first period (required to be valid period or it won't post)
         // build journal main entry
         $glEntry->journal_id = JOURNAL_ID;
         $glEntry->post_date = $order['post_date'];
         $glEntry->description = sprintf(TEXT_JID_ENTRY, constant('ORD_TEXT_' . JOURNAL_ID . '_WINDOW_TITLE'));
         $glEntry->short_name = $order['account_id'];
         $glEntry->ship_short_name = $order['account_id'];
         $glEntry->gl_acct_id = $order['gl_acct'];
         $glEntry->total_amount = $order['total_amount'];
         $glEntry->currencies_code = DEFAULT_CURRENCY;
         $glEntry->purchase_invoice_id = $order['order_id'];
         $glEntry->admin_id = $_SESSION['admin_id'];
         // set imported dept rep id to current logged in user
         if ($order['waiting']) {
             $glEntry->waiting = '1';
         }
         $glEntry->bill_primary_name = $order['bill_primary_name'];
         $glEntry->bill_contact = $order['bill_contact'];
         $glEntry->bill_address1 = $order['bill_address1'];
         $glEntry->bill_address2 = $order['bill_address2'];
         $glEntry->bill_city_town = $order['bill_city_town'];
         $glEntry->bill_state_province = $order['bill_state_province'];
         $glEntry->bill_postal_code = $order['bill_postal_code'];
         $glEntry->bill_country_code = $order['bill_country_code'];
         $glEntry->bill_telephone1 = $order['telephone1'];
         $glEntry->bill_telephone2 = $order['telephone2'];
         $glEntry->bill_fax = $order['fax'];
         $glEntry->bill_email = $order['email'];
         $glEntry->bill_website = $order['website'];
         switch (JOURNAL_ID) {
             case 4:
             case 6:
                 $glEntry->ship_primary_name = COMPANY_NAME;
                 $glEntry->ship_address1 = COMPANY_ADDRESS1;
                 $glEntry->ship_address2 = COMPANY_ADDRESS2;
                 $glEntry->ship_city_town = COMPANY_CITY_TOWN;
                 $glEntry->ship_state_province = COMPANY_ZONE;
                 $glEntry->ship_postal_code = COMPANY_POSTAL_CODE;
                 $glEntry->ship_country_code = COMPANY_COUNTRY;
                 break;
             default:
                 $glEntry->ship_primary_name = $order['ship_primary_name'];
                 $glEntry->ship_contact = $order['ship_contact'];
                 $glEntry->ship_address1 = $order['ship_address1'];
                 $glEntry->ship_address2 = $order['ship_address2'];
                 $glEntry->ship_city_town = $order['ship_city_town'];
                 $glEntry->ship_state_province = $order['ship_state_province'];
                 $glEntry->ship_postal_code = $order['ship_postal_code'];
                 $glEntry->ship_country_code = $order['ship_country_code'];
         }
         $glEntry->journal_main_array = $glEntry->build_journal_main_array();
         $glEntry->journal_main_array['purchase_invoice_id'] = $order['order_id'];
         // skip validating the invoice ID, just set it
         // Create the account (or update it)
         $glEntry->bill_acct_id = $glEntry->add_account(BB_ACCOUNT_TYPE . 'b', 0, 0, true);
         switch (JOURNAL_ID) {
             default:
                 // update the shipping address
                 $glEntry->ship_acct_id = $glEntry->add_account(BB_ACCOUNT_TYPE . 's', 0, 0, true);
                 break;
             case 4:
                 // skip for purchases (assume default company address)
             // skip for purchases (assume default company address)
             case 6:
         }
         // build journal row entries (2) one for the AP/AR account and the other for the beg bal equity account
         $glEntry->journal_rows = array();
         $total_amount = 0;
         while (true) {
             $credit_debit = false;
             switch (JOURNAL_ID) {
                 case 4:
                     $credit_debit = 'debit_amount';
                     // for journal_id = 4
                 // for journal_id = 4
                 case 10:
                     if (!$credit_debit) {
                         $credit_debit = 'credit_amount';
                     }
                     // for journal_id = 10
                     $glEntry->journal_rows[] = array('gl_type' => BB_GL_TYPE, 'qty' => $currencies->clean_value($order['quantity']), 'sku' => $order['sku'], 'description' => $order['description'], 'gl_account' => $order['inv_gl_acct'], 'taxable' => $order['taxable'] ? $order['taxable'] : 0, $credit_debit => $currencies->clean_value($order['total_cost']), 'post_date' => $order['post_date']);
                     break;
                 case 6:
                     $credit_debit = 'debit_amount';
                     // for journal_id = 6
                 // for journal_id = 6
                 case 12:
                     if (!$credit_debit) {
                         $credit_debit = 'credit_amount';
                     }
                     // for journal_id = 12
                     $glEntry->journal_rows[] = array('gl_type' => BB_GL_TYPE, 'qty' => '1', 'description' => constant('ORD_TEXT_' . JOURNAL_ID . '_WINDOW_TITLE') . '-' . TEXT_IMPORT, 'gl_account' => $order['inv_gl_acct'], 'taxable' => $order['taxable'] ? $order['taxable'] : 0, $credit_debit => $order['total_amount'], 'post_date' => $order['post_date']);
                     break;
             }
             $total_amount += $currencies->clean_value($order['total_cost']);
             $next_order = $this->records[$row_cnt + 1]['order_id'];
             if ((JOURNAL_ID == 4 || JOURNAL_ID == 10) && $order['order_id'] == $next_order) {
                 // more line items
                 $row_cnt++;
                 $order = $this->records[$row_cnt];
             } else {
                 // end of this order, break from while(true) loop
                 break;
             }
         }
         // build the total journal_item row
         switch (JOURNAL_ID) {
             case 6:
                 $total_amount = $order['total_amount'];
                 // and continue
             // and continue
             case 4:
                 $debit_credit = 'credit_amount';
                 break;
             case 12:
                 $total_amount = $order['total_amount'];
                 // and continue
             // and continue
             case 10:
                 $debit_credit = 'debit_amount';
                 break;
         }
         $glEntry->journal_rows[] = array('gl_type' => 'ttl', 'description' => constant('ORD_TEXT_' . $glEntry->journal_id . '_WINDOW_TITLE') . '-' . TEXT_TOTAL, 'gl_account' => $order['gl_acct'], $debit_credit => $total_amount, 'post_date' => $order['post_date']);
         $glEntry->journal_main_array['total_amount'] = $total_amount;
         if (!$glEntry->Post('insert')) {
             return false;
         }
         $entry_count++;
         $row_cnt++;
     }
     $this->line_count = $entry_count;
     return true;
 }