/**
  * (Queue Task Callback)
  *
  * Find any contribution records and create corresponding line-item
  * records.
  *
  * @param CRM_Queue_TaskContext $ctx
  * @param int $startId
  *   the first/lowest contribution ID to convert.
  * @param int $endId
  *   the last/highest contribution ID to convert.
  *
  * @return bool
  */
 public static function task_4_2_alpha1_convertContributions(CRM_Queue_TaskContext $ctx, $startId, $endId)
 {
     $upgrade = new CRM_Upgrade_Form();
     $query = "\n INSERT INTO civicrm_line_item(`entity_table` ,`entity_id` ,`price_field_id` ,`label` , `qty` ,`unit_price` ,`line_total` ,`participant_count` ,`price_field_value_id`)\n SELECT 'civicrm_contribution',cc.id, cpf.id as price_field_id, cpfv.label, 1, cc.total_amount, cc.total_amount line_total, 0, cpfv.id as price_field_value\n FROM civicrm_membership_payment cmp\n LEFT JOIN `civicrm_contribution` cc ON cc.id = cmp.contribution_id\n LEFT JOIN civicrm_line_item cli ON cc.id=cli.entity_id and cli.entity_table = 'civicrm_contribution'\n LEFT JOIN civicrm_membership cm ON cm.id=cmp.membership_id\n LEFT JOIN civicrm_membership_type cmt ON cmt.id = cm.membership_type_id\n LEFT JOIN civicrm_price_field cpf ON BINARY cpf.name = cmt.member_of_contact_id\n LEFT JOIN civicrm_price_field_value cpfv ON cpfv.membership_type_id = cm.membership_type_id AND cpf.id = cpfv.price_field_id\n WHERE (cc.id BETWEEN %1 AND %2) AND cli.entity_id IS NULL ;\n ";
     $sqlParams = array(1 => array($startId, 'Integer'), 2 => array($endId, 'Integer'));
     CRM_Core_DAO::executeQuery($query, $sqlParams);
     // create lineitems for contribution done for membership
     $sql = "\nSELECT    cc.id, cmp.membership_id, cpse.price_set_id, cc.total_amount\nFROM      civicrm_contribution cc\nLEFT JOIN civicrm_line_item cli ON cc.id=cli.entity_id AND cli.entity_table = 'civicrm_contribution'\nLEFT JOIN civicrm_membership_payment cmp ON cc.id = cmp.contribution_id\nLEFT JOIN civicrm_participant_payment cpp ON cc.id = cpp.contribution_id\nLEFT JOIN civicrm_price_set_entity cpse on cpse.entity_table = 'civicrm_contribution_page' AND cpse.entity_id = cc.contribution_page_id\nWHERE     (cc.id BETWEEN %1 AND %2)\nAND       cli.entity_id IS NULL AND cc.contribution_page_id IS NOT NULL AND cpp.contribution_id IS NULL\nGROUP BY  cc.id\n";
     $result = CRM_Core_DAO::executeQuery($sql, $sqlParams);
     while ($result->fetch()) {
         $sql = "\nSELECT    cpf.id, cpfv.id as price_field_value_id, cpfv.label, cpfv.amount, cpfv.count\nFROM      civicrm_price_field cpf\nLEFT JOIN civicrm_price_field_value cpfv ON cpf.id = cpfv.price_field_id\nWHERE     cpf.price_set_id = %1\n";
         $lineParams = array('entity_table' => 'civicrm_contribution', 'entity_id' => $result->id);
         if ($result->membership_id) {
             $sql .= " AND cpf.name = %2 AND cpfv.membership_type_id = %3 ";
             $params = array('1' => array($result->price_set_id, 'Integer'), '2' => array('membership_amount', 'String'), '3' => array(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $result->membership_id, 'membership_type_id'), 'Integer'));
             $res = CRM_Core_DAO::executeQuery($sql, $params);
             if ($res->fetch()) {
                 $lineParams += array('price_field_id' => $res->id, 'label' => $res->label, 'qty' => 1, 'unit_price' => $res->amount, 'line_total' => $res->amount, 'participant_count' => $res->count ? $res->count : 0, 'price_field_value_id' => $res->price_field_value_id);
             } else {
                 $lineParams['price_field_id'] = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $result->price_set_id, 'id', 'price_set_id');
                 $lineParams['label'] = 'Membership Amount';
                 $lineParams['qty'] = 1;
                 $lineParams['unit_price'] = $lineParams['line_total'] = $result->total_amount;
                 $lineParams['participant_count'] = 0;
             }
         } else {
             $sql .= "AND cpfv.amount = %2";
             //CRM-12273
             //check if price_set_id is exist, if not use the default contribution amount
             if (isset($result->price_set_id)) {
                 $priceSetId = $result->price_set_id;
             } else {
                 $defaultPriceSets = CRM_Price_BAO_PriceSet::getDefaultPriceSet();
                 foreach ($defaultPriceSets as $key => $pSet) {
                     if ($pSet['name'] == 'contribution_amount') {
                         $priceSetId = $pSet['setID'];
                     }
                 }
             }
             $params = array('1' => array($priceSetId, 'Integer'), '2' => array($result->total_amount, 'String'));
             $res = CRM_Core_DAO::executeQuery($sql, $params);
             if ($res->fetch()) {
                 $lineParams += array('price_field_id' => $res->id, 'label' => $res->label, 'qty' => 1, 'unit_price' => $res->amount, 'line_total' => $res->amount, 'participant_count' => $res->count ? $res->count : 0, 'price_field_value_id' => $res->price_field_value_id);
             } else {
                 $params = array('price_set_id' => $priceSetId, 'name' => 'other_amount');
                 $defaults = array();
                 CRM_Upgrade_Snapshot_V4p2_Price_BAO_Field::retrieve($params, $defaults);
                 if (!empty($defaults)) {
                     $lineParams['price_field_id'] = $defaults['id'];
                     $lineParams['label'] = $defaults['label'];
                     $lineParams['price_field_value_id'] = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_FieldValue', $defaults['id'], 'id', 'price_field_id');
                 } else {
                     $lineParams['price_field_id'] = CRM_Core_DAO::getFieldValue('CRM_Upgrade_Snapshot_V4p2_Price_DAO_Field', $priceSetId, 'id', 'price_set_id');
                     $lineParams['label'] = 'Contribution Amount';
                 }
                 $lineParams['qty'] = 1;
                 $lineParams['participant_count'] = 0;
                 $lineParams['unit_price'] = $lineParams['line_total'] = $result->total_amount;
             }
         }
         CRM_Upgrade_Snapshot_V4p2_Price_BAO_LineItem::create($lineParams);
     }
     return TRUE;
 }