<?php

class cron_jobs extends CI_Controller {

    protected $systemTime;

    function __construct() {
        parent::__construct();
        $this->load->model('cron_model');
        //$this->output->enable_profiler(TRUE);
    }

    // manage inventory items
    function inventory_items() {
        $client = new SoapClient(LIVE_SERVER_PATH . '/services/cron_jobs.php?wsdl', array('trace' => 1, 'exceptions' => 0));
        $api_data = array();
        $api_data['api_user'] = API_USER;
        $api_data['api_password'] = API_PASSWORD;
        $api_data['location'] = LOCATION_CODE;
        $api_data_final = json_encode($api_data);

        $result = $client->__soapCall('get_inventory_items', array('data' => $api_data_final));
        //echo $result; exit;

        $productsInfo = json_decode($result);
        if ($productsInfo) {

            /*
              //end cron job for differen db
              foreach ($productsInfo as $p) {
              $this->db->set_dbprefix('');
              $checkUnique = $this->cron_model->count_data('stock_master', ['stock_id' => $p->stock_master->stock_id]);

              if (empty($checkUnique)) {
              //insert data
              $itemData['stock_id'] = $p->stock_master->stock_id;
              $itemData['gender'] = $p->stock_master->gender;
              $itemData['item_size_id'] = $p->stock_master->item_size_id;
              $itemData['category_id'] = $p->stock_master->category_id;
              $itemData['tax_type_id'] = $p->stock_master->tax_type_id;
              $itemData['description'] = $p->stock_master->description;
              $itemData['long_description'] = $p->stock_master->long_description;
              $itemData['units'] = $p->stock_master->units;
              $itemData['mb_flag'] = $p->stock_master->mb_flag;
              $itemData['sales_account'] = $p->stock_master->sales_account;
              $itemData['cogs_account'] = $p->stock_master->cogs_account;
              $itemData['inventory_account'] = $p->stock_master->inventory_account;
              $itemData['adjustment_account'] = $p->stock_master->adjustment_account;
              $itemData['assembly_account'] = $p->stock_master->assembly_account;
              $itemData['dimension_id'] = $p->stock_master->dimension_id;
              $itemData['dimension2_id'] = $p->stock_master->dimension2_id;
              $itemData['actual_cost'] = $p->stock_master->actual_cost;
              $itemData['last_cost'] = $p->stock_master->last_cost;
              $itemData['material_cost'] = $p->stock_master->material_cost;
              $itemData['labour_cost'] = $p->stock_master->labour_cost;
              $itemData['overhead_cost'] = $p->stock_master->overhead_cost;
              $itemData['inactive'] = $p->stock_master->inactive;
              $itemData['no_sale'] = $p->stock_master->no_sale;
              $itemData['editable'] = $p->stock_master->editable;

              $itemAdd = $this->cron_model->insert_data('stock_master', $itemData);
              if ($itemAdd) {
              if ($p->stock_moves) {
              foreach ($p->stock_moves as $move) {
              $stockMoveData['trans_no'] = $move->trans_no;
              $stockMoveData['stock_id'] = $move->stock_id;
              $stockMoveData['type'] = $move->type;
              $stockMoveData['loc_code'] = $move->loc_code;
              $stockMoveData['tran_date'] = $move->tran_date;
              $stockMoveData['person_id'] = $move->person_id;
              $stockMoveData['price'] = $move->price;
              $stockMoveData['reference'] = $move->reference;
              $stockMoveData['qty'] = $move->qty;
              $stockMoveData['discount_percent'] = $move->discount_percent;
              $stockMoveData['standard_cost'] = $move->standard_cost;
              $stockMoveData['visible'] = $move->visible;
              $stockMoveData['lot_no'] = $move->lot_no;

              $this->cron_model->insert_data('stock_moves', $stockMoveData);
              }
              unset($move);
              }

              if ($p->stock_price) {
              foreach ($p->stock_price as $price) {
              $checkPrice = $this->cron_model->count_data('prices', ['stock_id' => $price->stock_id, 'sales_type_id' => $price->sales_type_id]);
              if (empty($checkPrice)) {
              $priceData['stock_id'] = $price->stock_id;
              $priceData['sales_type_id'] = $price->sales_type_id;
              $priceData['curr_abrev'] = $price->curr_abrev;
              $priceData['price'] = $price->price;

              $this->cron_model->insert_data('prices', $priceData);
              }
              }
              unset($price);
              }
              }
              } else {
              //update data
              $itemData['gender'] = $p->stock_master->gender;
              $itemData['item_size_id'] = $p->stock_master->item_size_id;
              $itemData['category_id'] = $p->stock_master->category_id;
              $itemData['tax_type_id'] = $p->stock_master->tax_type_id;
              $itemData['description'] = $p->stock_master->description;
              $itemData['long_description'] = $p->stock_master->long_description;
              $itemData['units'] = $p->stock_master->units;
              $itemData['mb_flag'] = $p->stock_master->mb_flag;
              $itemData['sales_account'] = $p->stock_master->sales_account;
              $itemData['cogs_account'] = $p->stock_master->cogs_account;
              $itemData['inventory_account'] = $p->stock_master->inventory_account;
              $itemData['adjustment_account'] = $p->stock_master->adjustment_account;
              $itemData['assembly_account'] = $p->stock_master->assembly_account;
              $itemData['dimension_id'] = $p->stock_master->dimension_id;
              $itemData['dimension2_id'] = $p->stock_master->dimension2_id;
              $itemData['actual_cost'] = $p->stock_master->actual_cost;
              $itemData['last_cost'] = $p->stock_master->last_cost;
              $itemData['material_cost'] = $p->stock_master->material_cost;
              $itemData['labour_cost'] = $p->stock_master->labour_cost;
              $itemData['overhead_cost'] = $p->stock_master->overhead_cost;
              $itemData['inactive'] = $p->stock_master->inactive;
              $itemData['no_sale'] = $p->stock_master->no_sale;
              $itemData['editable'] = $p->stock_master->editable;

              $this->cron_model->update_data('stock_master', $itemData, ['stock_id' => $p->stock_master->stock_id]);
              if ($p->stock_moves) {
              foreach ($p->stock_moves as $move) {
              $stockMoveData['trans_no'] = $move->trans_no;
              $stockMoveData['stock_id'] = $move->stock_id;
              $stockMoveData['type'] = $move->type;
              $stockMoveData['loc_code'] = $move->loc_code;
              $stockMoveData['tran_date'] = $move->tran_date;
              $stockMoveData['person_id'] = $move->person_id;
              $stockMoveData['price'] = $move->price;
              $stockMoveData['reference'] = $move->reference;
              $stockMoveData['qty'] = $move->qty;
              $stockMoveData['discount_percent'] = $move->discount_percent;
              $stockMoveData['standard_cost'] = $move->standard_cost;
              $stockMoveData['visible'] = $move->visible;
              $stockMoveData['lot_no'] = $move->lot_no;

              $this->cron_model->insert_data('stock_moves', $stockMoveData);
              }
              unset($move);
              }

              if ($p->stock_price) {
              foreach ($p->stock_price as $price) {
              $checkPrice = $this->cron_model->count_data('prices', ['stock_id' => $price->stock_id, 'sales_type_id' => $price->sales_type_id]);
              if (empty($checkPrice)) {
              $priceData['stock_id'] = $price->stock_id;
              $priceData['sales_type_id'] = $price->sales_type_id;
              $priceData['curr_abrev'] = $price->curr_abrev;
              $priceData['price'] = $price->price;
              $this->cron_model->insert_data('prices', $priceData);
              } else {
              //$priceData['stock_id'] = $price->stock_id;
              //$priceData['sales_type_id'] = $price->sales_type_id;
              $priceData['curr_abrev'] = $price->curr_abrev;
              $priceData['price'] = $price->price;
              $this->cron_model->update_data('prices', $priceData, ['stock_id' => $price->stock_id, 'sales_type_id' => $price->sales_type_id]);
              }
              }
              unset($price);
              }
              }
              }
              unset($p);

              //end cron job for differen db
             */

            // update ospos_items
            $this->db->set_dbprefix('ospos_');
            $items = $this->cron_model->get_products(LOCATION_CODE);
            // print_r($items);exit;
            if ($items) {
                foreach ($items as $item) {
                    $checkItem = $this->cron_model->count_data('items', ['item_number' => $item->stock_id, 'location' => LOCATION_CODE]);
                    if (empty($checkItem)) {
                        $returnData['name'] = !empty($item->name) ? $item->name : $item->description;
                        $returnData['category'] = !empty($item->category) ? $item->category : 0;
                        $returnData['description'] = $item->description;
                        $returnData['item_number'] = $item->stock_id;
                        $returnData['cost_price'] = $item->price;
                        $returnData['unit_price'] = $item->price;
                        $returnData['quantity'] = $item->qty;
                        $returnData['inactive'] = $item->inactive;
                        $returnData['gender'] = $item->gender;
                        $returnData['size'] = $item->size;
                        $returnData['location'] = LOCATION_CODE;

                        $affected = $this->cron_model->insert_data('items', $returnData);
                        if ($affected) {
                            if (!empty($item->tax)) {
                                $affectedData['item_id'] = $affected;
                                $affectedData['name'] = 'VAT';
                                $affectedData['percent'] = $item->tax;
                                $this->cron_model->insert_data('items_taxes', $affectedData);
                            }
                        }
                    } else {
                        $itemId = $this->cron_model->get_single('item_id', 'items', 'item_number', $item->stock_id);
                        $returnData['name'] = !empty($item->name) ? $item->name : $item->description;
                        $returnData['category'] = !empty($item->category) ? $item->category : 0;
                        $returnData['description'] = $item->description;
                        $returnData['cost_price'] = $item->price;
                        $returnData['unit_price'] = $item->price;
                        $returnData['quantity'] = $item->qty;
                        $returnData['inactive'] = $item->inactive;
                        $returnData['gender'] = $item->gender;
                        $returnData['size'] = $item->size;
                        $returnData['location'] = LOCATION_CODE;

                        $affected = $this->cron_model->update_data('items', $returnData, ['item_number' => $item->stock_id]);
                        //if ($affected) {
                        if (!empty($item->tax)) {
                            $check = $this->cron_model->count_data('items_taxes', ['item_id' => $itemId->item_id]);
                            if (empty($check)) {
                                $affectedData['item_id'] = $itemId->item_id;
                                $affectedData['name'] = 'VAT';
                                $affectedData['percent'] = $item->tax;
                                $this->cron_model->insert_data('items_taxes', $affectedData);
                            } else {
                                $affectedData['percent'] = $item->tax;
                                $this->cron_model->update_data('items_taxes', $affectedData, ['item_id' => $itemId->item_id]);
                            }
                        }
                        //}
                    }
                }
                unset($item);
            }
        }
    }

    // manage customers
    function manage_customers() {
        $client = new SoapClient(LIVE_SERVER_PATH . '/services/cron_jobs.php?wsdl', array('trace' => 1, 'exceptions' => 0));
        $api_data = array();
        $api_data['api_user'] = API_USER;
        $api_data['api_password'] = API_PASSWORD;
        $api_data_final = json_encode($api_data);

        $result = $client->__soapCall('get_customers', array('data' => $api_data_final));

        $customerInfo = json_decode($result);
//        echo '<pre>';print_r($customerInfo);exit;

        if ($customerInfo) {
            foreach ($customerInfo as $c) {
                if ($c->debitor->debtor_ref) {
                    $this->db->set_dbprefix('');
                    $checkUnique = $this->cron_model->count_data('debtors_master', ['debtor_ref' => $c->debitor->debtor_ref]);

//                $checkUnique = $this->cron_model->count_data('people', ['person_id' => $c->people->person_id, 'debtors_master_id' => $c->people->debtors_master_id]);
                    if (empty($checkUnique)) {
                        //insert data
                        //debitor master data
                        $debitorData['name'] = $c->debitor->name;
                        $debitorData['debtor_ref'] = $c->debitor->debtor_ref;
                        $debitorData['address'] = $c->debitor->address;
                        $debitorData['tax_id'] = $c->debitor->tax_id;
                        $debitorData['curr_code'] = $c->debitor->curr_code;
                        $debitorData['sales_type'] = $c->debitor->sales_type;
                        $debitorData['dimension_id'] = $c->debitor->dimension_id;
                        $debitorData['dimension2_id'] = $$c->debitor->dimension2_id;
                        $debitorData['credit_status'] = $c->debitor->credit_status;
                        $debitorData['payment_terms'] = $c->debitor->payment_terms;
                        $debitorData['discount'] = $c->debitor->discount;
                        $debitorData['pymt_discount'] = $c->debitor->pymt_discount;
                        $debitorData['credit_limit'] = $c->debitor->credit_limit;
                        $debitorData['notes'] = $c->debitor->notes;
                        $debitorData['inactive'] = $c->debitor->inactive;

                        $debitorId = $this->cron_model->insert_data('debtors_master', $debitorData);
                        if ($debitorId) {
                            // cust_branch recore
                            if ($c->branch) {
                                foreach ($c->branch as $branch) {
                                    $branchData['debtor_no'] = $debitorId;
                                    $branchData['br_name'] = $branch->br_name;
                                    $branchData['branch_ref'] = $branch->branch_ref;
                                    $branchData['br_address'] = $branch->br_address;
                                    $branchData['area'] = $branch->area;
                                    $branchData['salesman'] = $branch->salesman;
                                    $branchData['contact_name'] = $branch->contact_name;
                                    $branchData['default_location'] = $branch->default_location;
                                    $branchData['tax_group_id'] = $branch->tax_group_id;
                                    $branchData['sales_account'] = $branch->sales_account;
                                    $branchData['sales_discount_account'] = $branch->sales_discount_account;
                                    $branchData['receivables_account'] = $branch->receivables_account;
                                    $branchData['payment_discount_account'] = $branch->payment_discount_account;
                                    $branchData['default_ship_via'] = $branch->default_ship_via;
                                    $branchData['disable_trans'] = $branch->disable_trans;
                                    $branchData['br_post_address'] = $branch->br_post_address;
                                    $branchData['group_no'] = $branch->group_no;
                                    $branchData['notes'] = $branch->notes;
                                    $branchData['inactive'] = $branch->inactive;

                                    $branchId = $this->cron_model->insert_data('cust_branch', $branchData);
                                }
                                unset($branch);
                            }

                            // crm_persons recore
                            if ($c->crm_person) {
                                foreach ($c->crm_person as $crm) {
                                    $crmData['debtor_no'] = $debitorId;
                                    $crmData['ref'] = $crm->ref;
                                    $crmData['name'] = $crm->name;
                                    $crmData['name2'] = $crm->name2;
                                    $crmData['address'] = $crm->address;
                                    $crmData['phone'] = $crm->phone;
                                    $crmData['phone2'] = $crm->phone2;
                                    $crmData['fax'] = $crm->fax;
                                    $crmData['email'] = $crm->email;
                                    $crmData['lang'] = $crm->lang;
                                    $crmData['notes'] = $crm->notes;
                                    $crmData['inactive'] = $crm->inactive;

                                    $personId = $this->cron_model->insert_data('crm_persons', $crmData);
                                }
                                unset($crm);
                            }

                            //crm_contact data
                            $crmContact['person_id'] = $personId;
                            $crmContact['type'] = 'cust_branch';
                            $crmContact['action'] = 'general';
                            $crmContact['entity_id'] = $branchId;
                            $this->cron_model->insert_data('crm_contacts', $crmContact);
                            unset($crmContact);
                            $crmContact['person_id'] = $personId;
                            $crmContact['type'] = 'customer';
                            $crmContact['action'] = 'general';
                            $crmContact['entity_id'] = $debitorId;
                            $this->cron_model->insert_data('crm_contacts', $crmContact);

                            // oppos_people recore
                            $this->db->set_dbprefix('ospos_');
                            if ($c->people) {
                                foreach ($c->people as $index => $people) {
                                    $peopleData['first_name'] = $people->first_name;
                                    $peopleData['last_name'] = $people->last_name;
                                    $peopleData['phone_number'] = $people->phone_number;
                                    $peopleData['email'] = $people->email;
                                    $peopleData['address_1'] = $people->address_1;
                                    $peopleData['city'] = $people->city;
                                    $peopleData['state'] = $people->state;
                                    $peopleData['zip'] = $people->zip;
                                    $peopleData['country'] = $people->country;
                                    $peopleData['debtors_master_id'] = $debitorId;
                                    $peopleData['crm_persons_id'] = $personId;

                                    $affected = $this->cron_model->insert_data('people', $peopleData);
                                    if ($affected) {
                                        $customer = $data->customer[$index];
                                        $affectedData['person_id'] = $affected;
                                        $affectedData['taxable'] = !empty($customer->taxable) ? $customer->taxable : 1;
                                        $affectedData['deleted'] = !empty($customer->deleted) ? $customer->deleted : 0;
                                        $affectedData['previous_due'] = $customer->previous_due;
                                        $this->cron_model->insert_data('customers', $affectedData);
                                    }
                                }
                                unset($index);
                                unset($people);
                            }
                        }

//                    $returnData['first_name'] = $c->people->first_name;
//                    $returnData['last_name'] = $c->people->last_name;
//                    $returnData['phone_number'] = $c->people->phone_number;
//                    $returnData['email'] = $c->people->email;
//                    $returnData['address_1'] = $c->people->cost_price;
//                    $returnData['address_2'] = $c->people->address_2;
//                    $returnData['city'] = $c->people->city;
//                    $returnData['state'] = $$c->people->state;
//                    $returnData['zip'] = $c->people->zip;
//                    $returnData['country'] = $c->people->country;
//                    $returnData['comments'] = $c->people->comments;
//                    $returnData['country'] = $c->people->country;
//                    $returnData['person_id'] = $c->people->person_id;
//                    $returnData['debtors_master_id'] = $c->people->debtors_master_id;
//                    $returnData['crm_persons_id'] = $c->people->crm_persons_id;
//
//                    
//                    $affected = $this->cron_model->insert_data('people', $returnData);
//                    if ($affected) {
//                        $affectedData['person_id'] = $c->people->person_id;
//                        $affectedData['taxable'] = !empty($c->person->taxable) ? $c->person->taxable : 1;
//                        $affectedData['deleted'] = !empty($c->person->deleted) ? $c->person->deleted : 0;
//                        $affectedData['previous_due'] = $c->person->previous_due;
//                        $this->cron_model->insert_data('customers', $affectedData);
//                    }
                    } else {
                        //update data
                        //debitor master data
                        $this->db->set_dbprefix('');
                        $debitorId = $this->cron_model->get_single('debtor_no', 'debtors_master', 'debtor_ref', $c->debitor->debtor_ref);

                        $debitorData['name'] = $c->debitor->name;
                        $debitorData['debtor_ref'] = $c->debitor->debtor_ref;
                        $debitorData['address'] = $c->debitor->address;
                        $debitorData['tax_id'] = $c->debitor->tax_id;
                        $debitorData['curr_code'] = $c->debitor->curr_code;
                        $debitorData['sales_type'] = $c->debitor->sales_type;
                        $debitorData['dimension_id'] = $c->debitor->dimension_id;
                        $debitorData['dimension2_id'] = $$c->debitor->dimension2_id;
                        $debitorData['credit_status'] = $c->debitor->credit_status;
                        $debitorData['payment_terms'] = $c->debitor->payment_terms;
                        $debitorData['discount'] = $c->debitor->discount;
                        $debitorData['pymt_discount'] = $c->debitor->pymt_discount;
                        $debitorData['credit_limit'] = $c->debitor->credit_limit;
                        $debitorData['notes'] = $c->debitor->notes;
                        $debitorData['inactive'] = $c->debitor->inactive;

                        $affected = $this->cron_model->update_data('debtors_master', $debitorData, ['debtor_no' => $debitorId->debtor_no]);

                        if ($c->branch) {
                            foreach ($c->branch as $branch) {
                                $branchId = $this->cron_model->get_single('branch_code', 'cust_branch', 'branch_ref', $branch->branch_ref);

                                $branchData['debtor_no'] = $debitorId->debtor_no;
                                $branchData['br_name'] = $branch->br_name;
                                $branchData['branch_ref'] = $branch->branch_ref;
                                $branchData['br_address'] = $branch->br_address;
                                $branchData['area'] = $branch->area;
                                $branchData['salesman'] = $branch->salesman;
                                $branchData['contact_name'] = $branch->contact_name;
                                $branchData['default_location'] = $branch->default_location;
                                $branchData['tax_group_id'] = $branch->tax_group_id;
                                $branchData['sales_account'] = $branch->sales_account;
                                $branchData['sales_discount_account'] = $branch->sales_discount_account;
                                $branchData['receivables_account'] = $branch->receivables_account;
                                $branchData['payment_discount_account'] = $branch->payment_discount_account;
                                $branchData['default_ship_via'] = $branch->default_ship_via;
                                $branchData['disable_trans'] = $branch->disable_trans;
                                $branchData['br_post_address'] = $branch->br_post_address;
                                $branchData['group_no'] = $branch->group_no;
                                $branchData['notes'] = $branch->notes;
                                $branchData['inactive'] = $branch->inactive;

                                $this->cron_model->update_data('cust_branch', $branchData, ['branch_code' => $branchId->branch_code]);
                            }
                            unset($branch);
                        }

                        // crm_persons recore
                        if ($c->crm_person) {
                            foreach ($c->crm_person as $crm) {
                                $crmPersonId = $this->cron_model->get_single('id', 'crm_persons', 'ref', $branch->branch_ref);
                                if ($crmPersonId->id) {
                                    $crmData['ref'] = $crm->ref;
                                    $crmData['name'] = $crm->name;
                                    $crmData['name2'] = $crm->name2;
                                    $crmData['address'] = $crm->address;
                                    $crmData['phone'] = $crm->phone;
                                    $crmData['phone2'] = $crm->phone2;
                                    $crmData['fax'] = $crm->fax;
                                    $crmData['email'] = $crm->email;
                                    $crmData['lang'] = $crm->lang;
                                    $crmData['notes'] = $crm->notes;
                                    $crmData['inactive'] = $crm->inactive;

                                    $this->cron_model->update_data('crm_persons', $crmData, ['id' => $crmPersonId->id]);
                                }
                            }
                            unset($crm);
                        }

                        // oppos_people recore
                        $this->db->set_dbprefix('ospos_');
                        if ($c->people) {
                            foreach ($c->people as $index => $people) {
                                $peopleId = $this->cron_model->get_value('person_id', 'people', ['debtors_master_id' => $debitorId->debtor_no]);

                                $peopleData['first_name'] = $people->first_name;
                                $peopleData['last_name'] = $people->last_name;
                                $peopleData['phone_number'] = $people->phone_number;
                                $peopleData['email'] = $people->email;
                                $peopleData['address_1'] = $people->address_1;
                                $peopleData['city'] = $people->city;
                                $peopleData['state'] = $people->state;
                                $peopleData['zip'] = $people->zip;
                                $peopleData['country'] = $people->country;
                                $peopleData['debtors_master_id'] = $debitorId->debtor_no;
                                $peopleData['crm_persons_id'] = $crmPersonId->id;

                                $affected = $this->cron_model->update_data('people', $peopleData, ['person_id' => $peopleId->person_id]);
                                if ($affected) {
                                    $customer = $data->customer[$index];
                                    $check = $this->cron_model->count_data('customers', ['person_id' => $peopleId->person_id]);
                                    if (empty($check)) {
                                        $affectedData['person_id'] = $peopleId->person_id;
                                        $affectedData['taxable'] = !empty($customer->taxable) ? $customer->taxable : 1;
                                        $affectedData['deleted'] = !empty($customer->deleted) ? $customer->deleted : 0;
                                        $affectedData['previous_due'] = $customer->previous_due;
                                        $this->cron_model->insert_data('customers', $affectedData);
                                    } else {
                                        $affectedData['taxable'] = !empty($customer->taxable) ? $customer->taxable : 1;
                                        $affectedData['deleted'] = !empty($customer->deleted) ? $customer->deleted : 0;
                                        $affectedData['previous_due'] = $customer->previous_due;
                                        $this->cron_model->update_data('customers', $affectedData, ['person_id' => $peopleId->person_id]);
                                    }
                                }
                            }
                            unset($index);
                            unset($people);
                        }

//                    $returnData['first_name'] = $c->people->first_name;
//                    $returnData['last_name'] = $c->people->last_name;
//                    $returnData['phone_number'] = $c->people->phone_number;
//                    $returnData['email'] = $c->people->email;
//                    $returnData['address_1'] = $c->people->cost_price;
//                    $returnData['address_2'] = $c->people->address_2;
//                    $returnData['city'] = $c->people->city;
//                    $returnData['state'] = $$c->people->state;
//                    $returnData['zip'] = $c->people->zip;
//                    $returnData['country'] = $c->people->country;
//                    $returnData['comments'] = $c->people->comments;
//                    $returnData['country'] = $c->people->country;
//                    $returnData['person_id'] = $c->people->person_id;
//                    $returnData['debtors_master_id'] = $c->people->debtors_master_id;
//                    $returnData['crm_persons_id'] = $c->people->crm_persons_id;
//
//                    $affected = $this->cron_model->update_data('people', $returnData, ['person_id' => $c->people->person_id]);
//                    if ($affected) {
//                        $check = $this->cron_model->count_data('customers', ['person_id' => $c->people->person_id]);
//                        if (empty($check)) {
//                            $affectedData['person_id'] = $c->people->person_id;
//                            $affectedData['taxable'] = !empty($c->person->taxable) ? $c->person->taxable : 1;
//                            $affectedData['deleted'] = !empty($c->person->deleted) ? $c->person->deleted : 0;
//                            $affectedData['previous_due'] = $c->person->previous_due;
//                            $this->cron_model->insert_data('customers', $affectedData);
//                        } else {
//                            $affectedData['taxable'] = !empty($c->person->taxable) ? $c->person->taxable : 1;
//                            $affectedData['deleted'] = !empty($c->person->deleted) ? $c->person->deleted : 0;
//                            $affectedData['previous_due'] = $c->person->previous_due;
//                            $this->cron_model->update_data('customers', $affectedData, ['person_id' => $c->people->person_id]);
//                        }
//                    }
                    }
                }
            }
            unset($c);
        }
    }

    // update sales info
    function sales_information() {
        $this->systemTime = json_decode($this->_getTime());
        $client = new SoapClient(LIVE_SERVER_PATH . '/services/cron_jobs.php?wsdl', array('trace' => 1, 'exceptions' => 0));
        $api_data = array();
        $api_data['api_user'] = API_USER;
        $api_data['api_password'] = API_PASSWORD;

        $api_data['sales_data'] = $this->cron_model->sale_info($this->systemTime->now, $this->systemTime->interval);
        $api_data_final = json_encode($api_data);

        $result = $client->__soapCall('sales_information', array('data' => $api_data_final));

        // change status as processing
        $updateData['is_sync'] = 1;
        $this->cron_model->update_data('sales', $updateData, ['is_sync' => 0]);

        if ($result) {
            // change status as completed
            $updateData['is_sync'] = 2;
            $this->cron_model->update_data('sales', $updateData, ['is_sync' => 1]);
        }
    }

    // update stock of all products
    function update_stock() {
        $client = new SoapClient(LIVE_SERVER_PATH . '/services/cron_jobs.php?wsdl', array('trace' => 1, 'exceptions' => 0));
        $api_data = array();
        $api_data['api_user'] = API_USER;
        $api_data['api_password'] = API_PASSWORD;
        $api_data['location'] = LOCATION_CODE;
        $api_data_final = json_encode($api_data);

        $result = $client->__soapCall('get_products', array('data' => $api_data_final));

        $productInfo = json_decode($result);
        if ($productInfo) {
            foreach ($productInfo as $p) {
                $this->cron_model->update_stock($p->stock_id, $p->qty);
            }
            unset($p);
        }
    }

    private function _getTime() {
        $date = new DateTime();
        $now = $date->format('Y-m-d h:i:s');
        $date->modify('+' . CRON_JOB_INTERVAL);
        $interval = $date->format('Y-m-d h:i:s');
        return json_encode(['now' => $now, 'interval' => $interval]);
    }

}

?>