Ejemplo n.º 1
0
    public static function createInventoryDetails($related_focus, $module)
    {
        global $adb, $log, $current_user, $currentModule;
        $save_currentModule = $currentModule;
        $currentModule = 'InventoryDetails';
        $related_to = $related_focus->id;
        $taxtype = getInventoryTaxType($module, $related_to);
        if ($taxtype == 'group') {
            $query = "SELECT id as related_to, vtiger_inventoryproductrel.productid, sequence_no, lineitem_id, quantity, listprice, comment as description,\n\t\t\tquantity * listprice AS extgross,\n\t\t\tCOALESCE( discount_percent, COALESCE( discount_amount *100 / ( quantity * listprice ) , 0 ) ) AS discount_percent,\n\t\t\tCOALESCE( discount_amount, COALESCE( discount_percent * quantity * listprice /100, 0 ) ) AS discount_amount,\n\t\t\t(quantity * listprice) - COALESCE( discount_amount, COALESCE( discount_percent * quantity * listprice /100, 0 )) AS extnet,\n\t\t\t((quantity * listprice) - COALESCE( discount_amount, COALESCE( discount_percent * quantity * listprice /100, 0 ))) AS linetotal,\n\t\t\tcase when vtiger_products.productid != '' then vtiger_products.cost_price else vtiger_service.cost_price end as cost_price,\n\t\t\tcase when vtiger_products.productid != '' then vtiger_products.vendor_id else 0 end as vendor_id\n\t\t\tFROM vtiger_inventoryproductrel\n\t\t\tLEFT JOIN vtiger_products ON vtiger_products.productid=vtiger_inventoryproductrel.productid\n\t\t\tLEFT JOIN vtiger_service ON vtiger_service.serviceid=vtiger_inventoryproductrel.productid\n\t\t\tWHERE id = ?";
        } elseif ($taxtype == 'individual') {
            $query = "SELECT id as related_to, vtiger_inventoryproductrel.productid, sequence_no, lineitem_id, quantity, listprice, comment as description,\n\t\t\tcoalesce( tax1 , 0 ) AS tax1, coalesce( tax2 , 0 ) AS tax2, coalesce( tax3 , 0 ) AS tax3,\n\t\t\t( COALESCE( tax1, 0 ) + COALESCE( tax2, 0 ) + COALESCE( tax3, 0 ) ) as tax_percent,\n\t\t\tquantity * listprice AS extgross,\n\t\t\tCOALESCE( discount_percent, COALESCE( discount_amount *100 / ( quantity * listprice ) , 0 ) ) AS discount_percent,\n\t\t\tCOALESCE( discount_amount, COALESCE( discount_percent * quantity * listprice /100, 0 ) ) AS discount_amount,\n\t\t\t(quantity * listprice) - COALESCE( discount_amount, COALESCE( discount_percent * quantity * listprice /100, 0 )) AS extnet,\n\t\t\t((quantity * listprice) - COALESCE( discount_amount, COALESCE( discount_percent * quantity * listprice /100, 0 ))) * ( COALESCE( tax1, 0 ) + COALESCE( tax2, 0 ) + COALESCE( tax3, 0 ) ) /100 AS linetax,\n\t\t\t((quantity * listprice) - COALESCE( discount_amount, COALESCE( discount_percent * quantity * listprice /100, 0 ))) * ( 1 + ( COALESCE( tax1, 0 ) + COALESCE( tax2, 0 ) + COALESCE( tax3, 0 )) /100) AS linetotal,\n\t\t\tcase when vtiger_products.productid != '' then vtiger_products.cost_price else vtiger_service.cost_price end as cost_price,\n\t\t\tcase when vtiger_products.productid != '' then vtiger_products.vendor_id else 0 end as vendor_id\n\t\t\tFROM vtiger_inventoryproductrel\n\t\t\tLEFT JOIN vtiger_products ON vtiger_products.productid=vtiger_inventoryproductrel.productid\n\t\t\tLEFT JOIN vtiger_service ON vtiger_service.serviceid=vtiger_inventoryproductrel.productid\n\t\t\tWHERE id = ?";
        }
        $res_inv_lines = $adb->pquery($query, array($related_to));
        $accountid = '0';
        $contactid = '0';
        switch ($module) {
            case 'Quotes':
                $accountid = $related_focus->column_fields['account_id'];
                $contactid = $related_focus->column_fields['contact_id'];
                break;
            case 'SalesOrder':
                $accountid = $related_focus->column_fields['account_id'];
                $contactid = $related_focus->column_fields['contact_id'];
                break;
            case 'Invoice':
                $accountid = $related_focus->column_fields['account_id'];
                $contactid = $related_focus->column_fields['contact_id'];
                break;
            case 'PurchaseOrder':
                $contactid = $related_focus->column_fields['contact_id'];
                break;
            default:
                break;
        }
        // Delete all InventoryDetails where related with $related_to
        $res_to_del = $adb->pquery('SELECT inventorydetailsid FROM vtiger_inventorydetails
			INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_inventorydetails.inventorydetailsid
			WHERE deleted = 0 AND related_to = ? and lineitem_id not in (select lineitem_id from vtiger_inventoryproductrel where id=?)', array($related_to, $related_to));
        while ($invdrow = $adb->getNextRow($res_to_del, false)) {
            $invdet_focus = new InventoryDetails();
            $invdet_focus->id = $invdrow['inventorydetailsid'];
            $invdet_focus->trash('InventoryDetails', $invdet_focus->id);
        }
        $requestindex = 1;
        while (isset($_REQUEST['deleted' . $requestindex]) and $_REQUEST['deleted' . $requestindex] == 1) {
            $requestindex++;
        }
        // read $res_inv_lines result to create a new InventoryDetail for each register.
        // Remember to take the Vendor if the Product is related with this.
        while ($row = $adb->getNextRow($res_inv_lines, false)) {
            $invdet_focus = array();
            $invdet_focus = new InventoryDetails();
            $rec_exists = $adb->pquery('SELECT inventorydetailsid FROM vtiger_inventorydetails
				INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid = vtiger_inventorydetails.inventorydetailsid
				WHERE deleted = 0 AND lineitem_id = ?', array($row['lineitem_id']));
            if ($adb->num_rows($rec_exists) > 0) {
                $invdet_focus->id = $adb->query_result($rec_exists, 0, 0);
                $invdet_focus->retrieve_entity_info($invdet_focus->id, 'InventoryDetails');
                $invdet_focus->mode = 'edit';
            } else {
                $invdet_focus->id = '';
                $invdet_focus->mode = '';
            }
            foreach ($invdet_focus->column_fields as $fieldname => $val) {
                if (isset($_REQUEST[$fieldname . $requestindex])) {
                    $invdet_focus->column_fields[$fieldname] = vtlib_purify($_REQUEST[$fieldname . $requestindex]);
                } elseif (isset($row[$fieldname])) {
                    $invdet_focus->column_fields[$fieldname] = $row[$fieldname];
                }
            }
            $invdet_focus->column_fields['lineitem_id'] = $row['lineitem_id'];
            $_REQUEST['assigntype'] = 'U';
            $invdet_focus->column_fields['assigned_user_id'] = $current_user->id;
            $invdet_focus->column_fields['account_id'] = $accountid;
            $invdet_focus->column_fields['contact_id'] = $contactid;
            if ($taxtype == 'group') {
                $invdet_focus->column_fields['tax_percent'] = 0;
                $invdet_focus->column_fields['linetax'] = 0;
            }
            $handler = vtws_getModuleHandlerFromName('InventoryDetails', $current_user);
            $meta = $handler->getMeta();
            $invdet_focus->column_fields = DataTransform::sanitizeRetrieveEntityInfo($invdet_focus->column_fields, $meta);
            $invdet_focus->save("InventoryDetails");
            $requestindex++;
            while (isset($_REQUEST['deleted' . $requestindex]) and $_REQUEST['deleted' . $requestindex] == 1) {
                $requestindex++;
            }
        }
        $currentModule = $save_currentModule;
    }